en Text Formatting - ZeroHawkeye/wordZero GitHub Wiki
Text Formatting
WordZero provides comprehensive text formatting capabilities that allow you to create rich, visually appealing documents. This chapter covers font settings, text effects, paragraph formatting, and advanced text manipulation features.
🎨 Font Formatting
Basic Font Properties
import "github.com/ZeroHawkeye/wordZero/pkg/document"
doc := document.New()
// Create paragraph with formatted text
para := doc.AddParagraph("")
// Basic formatting options
para.AddFormattedText("Bold text", &document.TextFormat{
Bold: true,
})
para.AddFormattedText("Italic text", &document.TextFormat{
Italic: true,
})
para.AddFormattedText("Underlined text", &document.TextFormat{
Underline: true,
})
para.AddFormattedText("Strikethrough text", &document.TextFormat{
Strikethrough: true,
})
Font Families and Sizes
// Different font families
para.AddFormattedText("Times New Roman", &document.TextFormat{
FontName: "Times New Roman",
FontSize: 12,
})
para.AddFormattedText("Arial", &document.TextFormat{
FontName: "Arial",
FontSize: 14,
})
para.AddFormattedText("Calibri", &document.TextFormat{
FontName: "Calibri",
FontSize: 16,
})
// Code font
para.AddFormattedText("Consolas (monospace)", &document.TextFormat{
FontName: "Consolas",
FontSize: 10,
})
🌈 Text Colors
Basic Color Formatting
// Primary colors
para.AddFormattedText("Red text", &document.TextFormat{
FontColor: "FF0000",
})
para.AddFormattedText("Green text", &document.TextFormat{
FontColor: "00FF00",
})
para.AddFormattedText("Blue text", &document.TextFormat{
FontColor: "0000FF",
})
// Custom colors (hex format)
para.AddFormattedText("Custom purple", &document.TextFormat{
FontColor: "800080",
})
para.AddFormattedText("Dark orange", &document.TextFormat{
FontColor: "FF8C00",
})
Background Colors
// Text with background color
para.AddFormattedText("Highlighted text", &document.TextFormat{
FontColor: "FFFFFF",
BackgroundColor: "FFFF00", // Yellow background
})
para.AddFormattedText("Important note", &document.TextFormat{
FontColor: "FFFFFF",
BackgroundColor: "FF0000", // Red background
Bold: true,
})
📐 Paragraph Formatting
Text Alignment
// Left aligned (default)
leftPara := doc.AddParagraph("Left aligned text")
leftPara.SetAlignment(document.AlignmentLeft)
// Center aligned
centerPara := doc.AddParagraph("Center aligned text")
centerPara.SetAlignment(document.AlignmentCenter)
// Right aligned
rightPara := doc.AddParagraph("Right aligned text")
rightPara.SetAlignment(document.AlignmentRight)
// Justified
justifiedPara := doc.AddParagraph("Justified text that will be distributed evenly across the line width.")
justifiedPara.SetAlignment(document.AlignmentJustify)
Line Spacing
// Single spacing (default)
singleSpaced := doc.AddParagraph("Single spaced paragraph")
singleSpaced.SetLineSpacing(1.0)
// 1.5 line spacing
oneAndHalfSpaced := doc.AddParagraph("1.5 line spaced paragraph")
oneAndHalfSpaced.SetLineSpacing(1.5)
// Double spacing
doubleSpaced := doc.AddParagraph("Double spaced paragraph")
doubleSpaced.SetLineSpacing(2.0)
// Custom spacing (in points)
customSpaced := doc.AddParagraph("Custom line spacing")
customSpaced.SetLineSpacingPoints(18) // 18 points
Paragraph Spacing
// Space before paragraph
para.SetSpaceBefore(12) // 12 points before
// Space after paragraph
para.SetSpaceAfter(6) // 6 points after
// Both before and after
para.SetSpacing(12, 6) // 12 before, 6 after
Indentation
// First line indent
para.SetFirstLineIndent(36) // 0.5 inch (36 points)
// Left indent
para.SetLeftIndent(72) // 1 inch (72 points)
// Right indent
para.SetRightIndent(36) // 0.5 inch
// Hanging indent (negative first line indent)
para.SetFirstLineIndent(-36)
para.SetLeftIndent(72)
🔤 Advanced Text Effects
Multiple Formatting Combinations
// Complex formatting combination
para.AddFormattedText("Complex formatting", &document.TextFormat{
Bold: true,
Italic: true,
Underline: true,
FontName: "Arial",
FontSize: 14,
FontColor: "000080", // Navy blue
BackgroundColor: "FFFFCC", // Light yellow
})
Superscript and Subscript
// Mathematical expressions
mathPara := doc.AddParagraph("")
mathPara.AddFormattedText("E=mc", nil)
mathPara.AddFormattedText("2", &document.TextFormat{
Superscript: true,
})
// Chemical formulas
chemPara := doc.AddParagraph("")
chemPara.AddFormattedText("H", nil)
chemPara.AddFormattedText("2", &document.TextFormat{
Subscript: true,
})
chemPara.AddFormattedText("O", nil)
Text Borders
// Text with border
borderedText := &document.TextFormat{
Bold: true,
FontSize: 14,
Border: &document.TextBorder{
Style: "single",
Size: 4,
Color: "000000",
},
}
para.AddFormattedText("Bordered text", borderedText)
📊 Formatting Templates
Creating Reusable Formats
// Define common formatting templates
titleFormat := &document.TextFormat{
Bold: true,
FontName: "Arial",
FontSize: 18,
FontColor: "2F5597",
}
emphasisFormat := &document.TextFormat{
Italic: true,
FontColor: "CC0000",
}
codeFormat := &document.TextFormat{
FontName: "Consolas",
FontSize: 10,
BackgroundColor: "F8F8F8",
}
// Use templates
para.AddFormattedText("Title Text", titleFormat)
para.AddFormattedText("Emphasized text", emphasisFormat)
para.AddFormattedText("code()", codeFormat)
Style Inheritance
// Base format
baseFormat := &document.TextFormat{
FontName: "Arial",
FontSize: 12,
}
// Derived formats
headingFormat := &document.TextFormat{
FontName: baseFormat.FontName,
FontSize: baseFormat.FontSize + 4,
Bold: true,
FontColor: "2F5597",
}
bodyFormat := &document.TextFormat{
FontName: baseFormat.FontName,
FontSize: baseFormat.FontSize,
FontColor: "000000",
}
📋 Complete Formatting Example
Here's a comprehensive example demonstrating various formatting features:
package main
import (
"fmt"
"log"
"github.com/ZeroHawkeye/wordZero/pkg/document"
"github.com/ZeroHawkeye/wordZero/pkg/style"
)
func createFormattedDocument() error {
doc := document.New()
// Document title with custom formatting
title := doc.AddParagraph("")
title.AddFormattedText("Advanced Text Formatting Guide", &document.TextFormat{
Bold: true,
FontName: "Arial",
FontSize: 20,
FontColor: "2F5597",
})
title.SetAlignment(document.AlignmentCenter)
title.SetSpaceAfter(24)
// Subtitle
subtitle := doc.AddParagraph("")
subtitle.AddFormattedText("Comprehensive Text Styling with WordZero", &document.TextFormat{
Italic: true,
FontName: "Arial",
FontSize: 14,
FontColor: "666666",
})
subtitle.SetAlignment(document.AlignmentCenter)
subtitle.SetSpaceAfter(18)
// Section 1: Basic Formatting
section1 := doc.AddParagraph("1. Basic Text Formatting")
section1.SetStyle(style.StyleHeading1)
basicPara := doc.AddParagraph("")
basicPara.AddFormattedText("This demonstrates ", nil)
basicPara.AddFormattedText("bold", &document.TextFormat{Bold: true})
basicPara.AddFormattedText(", ", nil)
basicPara.AddFormattedText("italic", &document.TextFormat{Italic: true})
basicPara.AddFormattedText(", ", nil)
basicPara.AddFormattedText("underlined", &document.TextFormat{Underline: true})
basicPara.AddFormattedText(", and ", nil)
basicPara.AddFormattedText("strikethrough", &document.TextFormat{Strikethrough: true})
basicPara.AddFormattedText(" text formatting.", nil)
basicPara.SetSpaceAfter(12)
// Section 2: Colors and Highlights
section2 := doc.AddParagraph("2. Colors and Highlights")
section2.SetStyle(style.StyleHeading1)
colorPara := doc.AddParagraph("")
colorPara.AddFormattedText("Text can be ", nil)
colorPara.AddFormattedText("red", &document.TextFormat{FontColor: "FF0000"})
colorPara.AddFormattedText(", ", nil)
colorPara.AddFormattedText("green", &document.TextFormat{FontColor: "00AA00"})
colorPara.AddFormattedText(", ", nil)
colorPara.AddFormattedText("blue", &document.TextFormat{FontColor: "0000FF"})
colorPara.AddFormattedText(", or ", nil)
colorPara.AddFormattedText("highlighted", &document.TextFormat{
FontColor: "FFFFFF",
BackgroundColor: "FFAA00",
Bold: true,
})
colorPara.AddFormattedText(".", nil)
colorPara.SetSpaceAfter(12)
// Section 3: Different Fonts
section3 := doc.AddParagraph("3. Font Families")
section3.SetStyle(style.StyleHeading1)
fontPara1 := doc.AddParagraph("")
fontPara1.AddFormattedText("Times New Roman: ", &document.TextFormat{Bold: true})
fontPara1.AddFormattedText("The quick brown fox jumps over the lazy dog.", &document.TextFormat{
FontName: "Times New Roman",
FontSize: 12,
})
fontPara1.SetSpaceAfter(6)
fontPara2 := doc.AddParagraph("")
fontPara2.AddFormattedText("Arial: ", &document.TextFormat{Bold: true})
fontPara2.AddFormattedText("The quick brown fox jumps over the lazy dog.", &document.TextFormat{
FontName: "Arial",
FontSize: 12,
})
fontPara2.SetSpaceAfter(6)
fontPara3 := doc.AddParagraph("")
fontPara3.AddFormattedText("Consolas (code): ", &document.TextFormat{Bold: true})
fontPara3.AddFormattedText("func main() { fmt.Println(\"Hello\") }", &document.TextFormat{
FontName: "Consolas",
FontSize: 10,
BackgroundColor: "F8F8F8",
})
fontPara3.SetSpaceAfter(12)
// Section 4: Paragraph Alignment
section4 := doc.AddParagraph("4. Paragraph Alignment")
section4.SetStyle(style.StyleHeading1)
leftPara := doc.AddParagraph("Left aligned paragraph (default alignment)")
leftPara.SetAlignment(document.AlignmentLeft)
leftPara.SetSpaceAfter(6)
centerPara := doc.AddParagraph("Center aligned paragraph")
centerPara.SetAlignment(document.AlignmentCenter)
centerPara.SetSpaceAfter(6)
rightPara := doc.AddParagraph("Right aligned paragraph")
rightPara.SetAlignment(document.AlignmentRight)
rightPara.SetSpaceAfter(6)
justifyPara := doc.AddParagraph("Justified paragraph with longer text that demonstrates how text is distributed evenly across the full width of the line, creating straight edges on both left and right sides.")
justifyPara.SetAlignment(document.AlignmentJustify)
justifyPara.SetSpaceAfter(12)
// Section 5: Special Effects
section5 := doc.AddParagraph("5. Special Effects")
section5.SetStyle(style.StyleHeading1)
specialPara := doc.AddParagraph("")
specialPara.AddFormattedText("Mathematical expression: E=mc", nil)
specialPara.AddFormattedText("2", &document.TextFormat{Superscript: true})
specialPara.AddFormattedText(" and chemical formula: H", nil)
specialPara.AddFormattedText("2", &document.TextFormat{Subscript: true})
specialPara.AddFormattedText("O", nil)
return doc.Save("text_formatting_guide.docx")
}
func main() {
if err := createFormattedDocument(); err != nil {
log.Fatalf("Error creating document: %v", err)
}
fmt.Println("Text formatting guide created successfully!")
}
💡 Best Practices
1. Consistent Formatting
- Use consistent font families throughout the document
- Maintain consistent color schemes
- Apply uniform spacing and alignment
2. Readability
- Choose appropriate font sizes for different content types
- Ensure sufficient contrast between text and background
- Use formatting sparingly to avoid visual clutter
3. Professional Appearance
- Stick to standard fonts for compatibility
- Use subtle colors for business documents
- Maintain consistent paragraph spacing
4. Performance
- Reuse TextFormat objects for similar formatting
- Avoid excessive format changes within paragraphs
- Use styles for consistent formatting across documents
⚠️ Common Issues
Font Compatibility
- Not all fonts are available on all systems
- Use standard fonts for better compatibility
- Test documents on different platforms
Color Representation
- Colors may appear differently on different displays
- Use standard color palettes for consistency
- Consider accessibility requirements
Formatting Inheritance
- Understand how paragraph and character formatting interact
- Be careful with nested formatting
- Test complex formatting combinations
Next Steps
Continue learning about document structure:
- Table Operations - Formatting text in tables
- Advanced Features - Complex document features
- Best Practices - Optimization techniques
Master text formatting to create professional, visually appealing documents!