render

package
v1.1.3 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 6, 2025 License: MIT Imports: 15 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ClearFontCache

func ClearFontCache()

ClearFontCache releases all cached font.Face objects.

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

func LoadFont(path string, sizePt float64) (*Font, error)

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

func LoadFontFromBytes(data []byte, sizePt float64) (*Font, error)

LoadFontFromBytes parses a TrueType font from memory. Useful for embedding fonts or loading from resources.

func MustLoadFont

func MustLoadFont(path string, sizePt float64) *Font

MustLoadFont loads a .ttf font from disk and panics on error. Intended for static initialization at package level.

func MustLoadFontFromBytes

func MustLoadFontFromBytes(data []byte, sizePt float64) *Font

MustLoadFontFromBytes parses a TrueType font from bytes and panics on error. Used for embedding fonts with Go’s //go:embed directive.

func (*Font) AscentPx

func (f *Font) AscentPx() float64

AscentPx returns the ascent (distance from baseline to top) in pixels.

func (*Font) BaselineForTopY

func (f *Font) BaselineForTopY(topY float64) float64

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

func (f *Font) CapHeightPx() float64

CapHeightPx estimates the visual cap height (“h” height) in pixels. Falls back to 85% of ascent if glyph metrics are unavailable.

func (*Font) DPI

func (f *Font) DPI() float64

DPI returns the current DPI value.

func (*Font) DescentPx

func (f *Font) DescentPx() float64

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

func (f *Font) Face() 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) HeightPt

func (f *Font) HeightPt() float64

HeightPt returns the font size in points.

func (*Font) HeightPx

func (f *Font) HeightPx() float64

HeightPx returns the font size converted to pixels for the current DPI.

func (*Font) LeadingPx

func (f *Font) LeadingPx() float64

LeadingPx returns the vertical leading (extra space between lines) in pixels.

func (*Font) LineHeightPx

func (f *Font) LineHeightPx() float64

LineHeightPx returns the total line height (ascent + descent + leading) in pixels.

func (*Font) MeasureMultilineString

func (f *Font) MeasureMultilineString(s string, lineHeightPx float64) (width, height float64)

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

func (f *Font) MeasureString(s string) (w, h float64)

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

func (f *Font) SetDPI(dpi float64) *Font

SetDPI sets the font’s DPI scaling. Defaults to 72 if <= 0. Higher DPI simulates scaled rendering for export or preview.

func (*Font) SetFontSizePt

func (f *Font) SetFontSizePt(pt float64) *Font

SetFontSizePt sets the font size in points (1pt = 1/72 inch). Ensures a minimum value > 0 to avoid invalid scaling.

func (*Font) SetLetterSpacingPercent

func (f *Font) SetLetterSpacingPercent(percent float64) *Font

SetLetterSpacingPercent defines tracking (letter spacing) as a percentage of font size. Positive values loosen spacing, negative values tighten spacing.

func (*Font) TopYForBaseline

func (f *Font) TopYForBaseline(baselineY float64) float64

TopYForBaseline returns the top y coordinate for a given baseline y value. Inverse of BaselineForTopY. Matches CSS vertical alignment model.

func (*Font) TrackingPx

func (f *Font) TrackingPx() float64

TrackingPx returns the tracking offset (in pixels) applied between glyphs.

func (*Font) TrueTypeFont

func (f *Font) TrueTypeFont() *truetype.Font

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.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL