Documentation
¶
Index ¶
- func ClearFontCache()
- func SetFontCacheCapacity(capacity int)
- type Font
- func (f *Font) AscentPx() float64
- func (f *Font) BaselineForTopY(topY float64) float64
- func (f *Font) CapHeightPx() float64
- func (f *Font) DPI() float64
- func (f *Font) DescentPx() float64
- func (f *Font) DrawString(dst draw.Image, col color.Color, s string, x, baselineY float64) fixed.Point26_6
- func (f *Font) Face() font.Face
- func (f *Font) HeightPt() float64
- func (f *Font) HeightPx() float64
- func (f *Font) LeadingPx() float64
- func (f *Font) LineHeightPx() float64
- func (f *Font) MeasureMultilineString(s string, lineHeightPx float64) (width, height float64)
- func (f *Font) MeasureString(s string) (w, h float64)
- func (f *Font) SetDPI(dpi float64) *Font
- func (f *Font) SetFontSizePt(pt float64) *Font
- func (f *Font) SetLetterSpacingPercent(percent float64) *Font
- func (f *Font) TopYForBaseline(baselineY float64) float64
- func (f *Font) TrackingPx() float64
- func (f *Font) TrueTypeFont() *truetype.Font
- type PatternPainter
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func SetFontCacheCapacity ¶
func SetFontCacheCapacity(capacity int)
SetFontCacheCapacity changes the max number of cached font faces.
Types ¶
type Font ¶
type Font struct {
// contains filtered or unexported fields
}
Font wraps a TrueType font with pixel-accurate rendering helpers. It mimics CSS and Figma layout behavior for text measurement and positioning.
func LoadFont ¶
LoadFont loads a .ttf file from disk and returns a Font object at the given point size. 1pt = 1/72 inch. Defaults to 72 DPI (1pt = 1px).
func LoadFontFromBytes ¶
LoadFontFromBytes parses a TrueType font from memory. Useful for embedding fonts or loading from resources.
func MustLoadFont ¶
MustLoadFont loads a .ttf font from disk and panics on error. Intended for static initialization at package level.
func MustLoadFontFromBytes ¶
MustLoadFontFromBytes parses a TrueType font from bytes and panics on error. Used for embedding fonts with Go’s //go:embed directive.
func (*Font) BaselineForTopY ¶
BaselineForTopY returns the baseline y coordinate for a given top y value. Matches CSS line box behavior: baseline = top + ascent + (leading / 2).
func (*Font) CapHeightPx ¶
CapHeightPx estimates the visual cap height (“h” height) in pixels. Falls back to 85% of ascent if glyph metrics are unavailable.
func (*Font) DescentPx ¶
DescentPx returns the descent (distance from baseline to bottom) in pixels.
func (*Font) DrawString ¶
func (f *Font) DrawString(dst draw.Image, col color.Color, s string, x, baselineY float64) fixed.Point26_6
DrawString draws a single line of text on the destination image. Tracking and kerning are applied between glyphs, not after the final one. The baseline is aligned to pixel grid to avoid blur.
func (*Font) Face ¶
Face returns a truetype.Face configured with the current size and DPI. Faces are cached to prevent redundant allocations and ensure consistent rendering.
func (*Font) LeadingPx ¶
LeadingPx returns the vertical leading (extra space between lines) in pixels.
func (*Font) LineHeightPx ¶
LineHeightPx returns the total line height (ascent + descent + leading) in pixels.
func (*Font) MeasureMultilineString ¶
MeasureMultilineString measures a multi-line text block in pixels. Width = max line width. Height = number of lines × lineHeightPx. If lineHeightPx <= 0, the font’s intrinsic line height is used.
func (*Font) MeasureString ¶
MeasureString measures the pixel width and height of a single-line string. Width includes glyph advances and tracking between characters. Height equals the line height in pixels.
func (*Font) SetDPI ¶
SetDPI sets the font’s DPI scaling. Defaults to 72 if <= 0. Higher DPI simulates scaled rendering for export or preview.
func (*Font) SetFontSizePt ¶
SetFontSizePt sets the font size in points (1pt = 1/72 inch). Ensures a minimum value > 0 to avoid invalid scaling.
func (*Font) SetLetterSpacingPercent ¶
SetLetterSpacingPercent defines tracking (letter spacing) as a percentage of font size. Positive values loosen spacing, negative values tighten spacing.
func (*Font) TopYForBaseline ¶
TopYForBaseline returns the top y coordinate for a given baseline y value. Inverse of BaselineForTopY. Matches CSS vertical alignment model.
func (*Font) TrackingPx ¶
TrackingPx returns the tracking offset (in pixels) applied between glyphs.
func (*Font) TrueTypeFont ¶
TrueTypeFont exposes the underlying truetype.Font instance.
type PatternPainter ¶
type PatternPainter struct {
// contains filtered or unexported fields
}
PatternPainter implements the freetype/raster.Painter interface. It renders spans into an RGBA overlay using a pattern fill, blending with a base image and an optional alpha mask.
It supports different blending modes and opacity control when the pattern implements the `patterns.BlendedPattern` interface.
func NewPatternPainter ¶
func NewPatternPainter(overlay, base *image.RGBA, mask *image.Alpha, p patterns.Pattern) *PatternPainter
NewPatternPainter creates and returns a new PatternPainter.
Parameters:
- overlay: destination RGBA layer where the pattern will be painted
- base: background RGBA layer to blend over
- mask: optional alpha mask (can be nil)
- p: pattern implementing the patterns.Pattern interface
func (*PatternPainter) Paint ¶
func (r *PatternPainter) Paint(ss []raster.Span, _ bool)
Paint renders a list of raster spans (`ss`) onto the overlay image. Each span is filled using the current pattern, blended with the base image according to the pattern's blend mode and opacity.
If a mask is provided, it modulates the per-pixel alpha coverage. This function is typically called by a rasterizer during vector path filling.