-
Notifications
You must be signed in to change notification settings - Fork 4
Library Usage Fonts
Chris W edited this page Dec 9, 2024
·
4 revisions
Goshot provides comprehensive font management capabilities through the fonts
package. This guide covers how to use and customize fonts in your code screenshots.
import (
"github.com/watzon/goshot/pkg/fonts"
"github.com/watzon/goshot/pkg/chrome"
)
// Get a font by name (uses system fonts)
font, err := fonts.GetFont("Arial", nil)
if err != nil {
// Falls back to Inter (sans) or JetBrains Mono (mono) if the font is not found
font, err = fonts.GetFallback(fonts.FallbackSans)
if err != nil {
log.Fatal(err)
}
}
// Use the font in chrome configuration
theme := chrome.Theme{
TitleFont: font.Name,
// ... other theme settings
}
Goshot supports various font styles, weights, and stretches:
style := &fonts.FontStyle{
Weight: fonts.WeightBold,
Stretch: fonts.StretchNormal,
Italic: true,
Condensed: false,
Mono: false,
}
// Get a specific font variant
boldItalicFont, err := fonts.GetFont("Roboto", style)
const (
WeightThin FontWeight = iota + 1 // Thinnest
WeightExtraLight // Extra Light
WeightLight // Light
WeightRegular // Regular (Normal)
WeightMedium // Medium
WeightSemiBold // Semi-bold
WeightBold // Bold
WeightExtraBold // Extra Bold
WeightBlack // Black
WeightHeavy // Heaviest
)
const (
StretchUltraCondensed FontStretch = iota + 1
StretchExtraCondensed
StretchCondensed
StretchSemiCondensed
StretchNormal
StretchSemiExpanded
StretchExpanded
StretchExtraExpanded
StretchUltraExpanded
)
Goshot comes with several high-quality fonts embedded:
-
JetBrains Mono Nerd Font (Monospace)
- Regular
- Medium
- Bold
- Italic
-
Inter (Sans-serif)
- Regular
- Italic
-
Cantarell (Sans-serif)
- Regular
- Bold
- Italic
- Bold Italic
These fonts are used as fallbacks when system fonts are not available:
// Get the fallback monospace font (JetBrains Mono)
monoFont, err := fonts.GetFallback(fonts.FallbackMono)
// Get the fallback sans-serif font (Inter)
sansFont, err := fonts.GetFallback(fonts.FallbackSans)
Goshot can use fonts installed on your system:
// Check if a font is available
if fonts.IsFontAvailable("Arial") {
font, err := fonts.GetFont("Arial", nil)
// ...
}
// List all available system fonts
availableFonts := fonts.ListFonts()
for _, fontName := range availableFonts {
fmt.Printf("Found font: %s\n", fontName)
}
// Get all variants of a font
variants, err := fonts.GetFontVariants("Roboto")
for _, font := range variants {
fmt.Printf("Variant: Weight=%d, Italic=%v\n", font.Style.Weight, font.Style.Italic)
}
Goshot provides special support for monospace fonts:
// Check if a font is monospace
font, _ := fonts.GetFont("JetBrains Mono", nil)
if font.IsMono() {
// Font is monospace
}
// Get a monospace face with specific size and cell width
face, err := font.GetMonoFace(12, 0) // 0 means use natural width
if err != nil {
log.Fatal(err)
}
defer face.Close()
// Measure string width
width, err := font.MeasureString("Hello", 12, nil)
// Get maximum glyph width
maxWidth, err := font.GetMaxWidth()
-
Font Selection
- Use system fonts when available for consistent appearance
- Rely on embedded fonts as reliable fallbacks
- Consider font licensing when bundling additional fonts
-
Performance
- Cache font faces when rendering multiple times
- Close font faces when done to free resources
- Use
IsFontAvailable()
for quick font availability checks
-
Monospace Handling
- Use
GetMonoFace()
for fixed-width rendering - Check
IsMono()
before assuming monospace behavior - Consider font metrics when mixing different fonts
- Use
-
π€ Contributing
- Code of Conduct
- Development Workflow
- Project Structure
- Guidelines
- Testing
- Documentation