Skip to content

Library Usage Fonts

Chris W edited this page Dec 9, 2024 · 4 revisions

Font Management

Goshot provides comprehensive font management capabilities through the fonts package. This guide covers how to use and customize fonts in your code screenshots.

Basic Usage

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
}

Font Styles

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)

Font Weights

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
)

Font Stretches

const (
    StretchUltraCondensed FontStretch = iota + 1
    StretchExtraCondensed
    StretchCondensed
    StretchSemiCondensed
    StretchNormal
    StretchSemiExpanded
    StretchExpanded
    StretchExtraExpanded
    StretchUltraExpanded
)

Embedded Fonts

Goshot comes with several high-quality fonts embedded:

  1. JetBrains Mono Nerd Font (Monospace)

    • Regular
    • Medium
    • Bold
    • Italic
  2. Inter (Sans-serif)

    • Regular
    • Italic
  3. 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)

System Fonts

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)
}

Advanced Features

Monospace Rendering

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()

Font Measurements

// Measure string width
width, err := font.MeasureString("Hello", 12, nil)

// Get maximum glyph width
maxWidth, err := font.GetMaxWidth()

Best Practices

  1. Font Selection

    • Use system fonts when available for consistent appearance
    • Rely on embedded fonts as reliable fallbacks
    • Consider font licensing when bundling additional fonts
  2. Performance

    • Cache font faces when rendering multiple times
    • Close font faces when done to free resources
    • Use IsFontAvailable() for quick font availability checks
  3. Monospace Handling

    • Use GetMonoFace() for fixed-width rendering
    • Check IsMono() before assuming monospace behavior
    • Consider font metrics when mixing different fonts