text/parser_ximage.go

Functions

Functions

func FullName

FullName implements ParsedFont.FullName.

func (f *ximageParsedFont) FullName() string {
	if buf, err := f.font.Name(nil, sfnt.NameIDFull); err == nil && buf != "" {
		return buf
	}
	return ""
}

func GlyphAdvance

GlyphAdvance implements ParsedFont.GlyphAdvance.

func (f *ximageParsedFont) GlyphAdvance(glyphIndex uint16, ppem float64) float64 {
	// Create buffer for operations
	var buf sfnt.Buffer

	// Get advance in font units
	advance, err := f.font.GlyphAdvance(&buf, sfnt.GlyphIndex(glyphIndex), fixed.Int26_6(ppem*64), font.HintingFull)
	if err != nil {
		return 0
	}

	return fixedToFloat64(advance)
}

func GlyphBounds

GlyphBounds implements ParsedFont.GlyphBounds.

func (f *ximageParsedFont) GlyphBounds(glyphIndex uint16, ppem float64) Rect {
	var buf sfnt.Buffer

	bounds, _, err := f.font.GlyphBounds(&buf, sfnt.GlyphIndex(glyphIndex), fixed.Int26_6(ppem*64), font.HintingFull)
	if err != nil {
		return Rect{}
	}

	return Rect{
		MinX:	fixedToFloat64(bounds.Min.X),
		MinY:	fixedToFloat64(bounds.Min.Y),
		MaxX:	fixedToFloat64(bounds.Max.X),
		MaxY:	fixedToFloat64(bounds.Max.Y),
	}
}

func GlyphIndex

GlyphIndex implements ParsedFont.GlyphIndex.

func (f *ximageParsedFont) GlyphIndex(r rune) uint16 {
	idx, err := f.font.GlyphIndex(nil, r)
	if err != nil {
		return 0
	}
	return uint16(idx)
}

func Metrics

Metrics implements ParsedFont.Metrics.

func (f *ximageParsedFont) Metrics(ppem float64) FontMetrics {
	var buf sfnt.Buffer

	metrics, err := f.font.Metrics(&buf, fixed.Int26_6(ppem*64), font.HintingFull)
	if err != nil {
		return FontMetrics{}
	}

	return FontMetrics{
		Ascent:		fixedToFloat64(metrics.Ascent),
		Descent:	fixedToFloat64(metrics.Descent),
		LineGap:	fixedToFloat64(metrics.Height) - fixedToFloat64(metrics.Ascent) + fixedToFloat64(metrics.Descent),
		XHeight:	fixedToFloat64(metrics.XHeight),
		CapHeight:	fixedToFloat64(metrics.CapHeight),
	}
}

func Name

Name implements ParsedFont.Name.

func (f *ximageParsedFont) Name() string {
	if buf, err := f.font.Name(nil, sfnt.NameIDFamily); err == nil && buf != "" {
		return buf
	}
	return ""
}

func NumGlyphs

NumGlyphs implements ParsedFont.NumGlyphs.

func (f *ximageParsedFont) NumGlyphs() int {
	return f.font.NumGlyphs()
}

func Parse

Parse implements FontParser.Parse.

func (p *ximageParser) Parse(data []byte) (ParsedFont, error) {
	return p.ParseIndex(data, 0)
}

func ParseIndex

ParseIndex parses a font at the given index within a collection.

For single fonts (.ttf/.otf), index is ignored. For collections

(.ttc/.otc), index selects which font to use (0 = first).

func (p *ximageParser) ParseIndex(data []byte, index int) (ParsedFont, error) {
	// Try single font first (most common case).
	f, err := opentype.Parse(data)
	if err == nil {
		return &ximageParsedFont{font: f}, nil
	}

	// Single parse failed — try as collection (.ttc/.otc).
	coll, collErr := opentype.ParseCollection(data)
	if collErr != nil {
		return nil, fmt.Errorf("text: failed to parse font: %w", err)
	}

	if index >= coll.NumFonts() {
		return nil, fmt.Errorf("text: collection index %d out of range (collection has %d fonts)", index, coll.NumFonts())
	}

	cf, cfErr := coll.Font(index)
	if cfErr != nil {
		return nil, fmt.Errorf("text: failed to get font %d from collection: %w", index, cfErr)
	}
	return &ximageParsedFont{font: cf}, nil
}

func UnitsPerEm

UnitsPerEm implements ParsedFont.UnitsPerEm.

func (f *ximageParsedFont) UnitsPerEm() int {
	return int(f.font.UnitsPerEm())
}