text/emoji/colr.go

Functions Structs

Functions

func Empty

Empty returns true if the rectangle has zero area.

func (r Rect) Empty() bool {
	return r.MinX >= r.MaxX || r.MinY >= r.MaxY
}

func GetGlyph

GetGlyph returns the COLRGlyph for the given glyph ID.

Returns ErrGlyphNotInCOLR if the glyph is not a color glyph.

func (p *COLRParser) GetGlyph(glyphID uint16, paletteIndex int) (*COLRGlyph, error) {
	record, found := p.findBaseGlyph(glyphID)
	if !found {
		return nil, ErrGlyphNotInCOLR
	}

	glyph := &COLRGlyph{
		GlyphID:	glyphID,
		Layers:		make([]ColorLayer, record.numLayers),
		Version:	p.version,
	}

	// Extract layers
	for i := uint16(0); i < record.numLayers; i++ {
		layerIdx := record.firstLayer + i
		if int(layerIdx) >= len(p.layers) {
			return nil, ErrInvalidCOLRData
		}

		layer := p.layers[layerIdx]
		glyph.Layers[i] = ColorLayer{
			GlyphID:	layer.glyphID,
			PaletteIndex:	layer.paletteIndex,
		}

		// Resolve color from palette
		if !glyph.Layers[i].IsForeground() {
			if paletteIndex < len(p.palettes) && int(layer.paletteIndex) < len(p.palettes[paletteIndex]) {
				glyph.Layers[i].Color = p.palettes[paletteIndex][layer.paletteIndex]
			}
		}
	}

	return glyph, nil
}

func HasGlyph

HasGlyph returns true if the glyph ID is a color glyph.

func (p *COLRParser) HasGlyph(glyphID uint16) bool {
	_, found := p.findBaseGlyph(glyphID)
	return found
}

func Height

Height returns the height of the rectangle.

func (r Rect) Height() float64 {
	return r.MaxY - r.MinY
}

func IsForeground

IsForeground returns true if this layer uses the foreground text color.

func (l ColorLayer) IsForeground() bool {
	return l.PaletteIndex == 0xFFFF
}

func NewCOLRParser

NewCOLRParser creates a new COLR parser from table data.

colrData is the raw COLR table, cpalData is the raw CPAL table.

func NewCOLRParser(colrData, cpalData []byte) (*COLRParser, error) {
	if len(colrData) == 0 {
		return nil, ErrNoCOLRTable
	}
	if len(cpalData) == 0 {
		return nil, ErrNoCPALTable
	}

	p := &COLRParser{
		colrData:	colrData,
		cpalData:	cpalData,
	}

	if err := p.parseCOLRHeader(); err != nil {
		return nil, err
	}

	if err := p.parseCPAL(); err != nil {
		return nil, err
	}

	return p, nil
}

func NumPalettes

NumPalettes returns the number of color palettes.

func (p *COLRParser) NumPalettes() int {
	return len(p.palettes)
}

func PaletteColors

PaletteColors returns the colors in a palette.

func (p *COLRParser) PaletteColors(paletteIndex int) []Color {
	if paletteIndex < 0 || paletteIndex >= len(p.palettes) {
		return nil
	}
	return p.palettes[paletteIndex]
}

func RGBA

RGBA implements color.Color interface.

func (c Color) RGBA() (r, g, b, a uint32) {
	return uint32(c.R) * 257, uint32(c.G) * 257, uint32(c.B) * 257, uint32(c.A) * 257
}

func RenderCOLRToImage

RenderCOLRToImage renders a COLR glyph to an RGBA image.

This is a simplified renderer that composites layers.

For production use, each layer glyph should be rasterized

at the given size and composited with its color.

 

Parameters:

- glyph: The COLR glyph to render

- renderLayer: Function to render a single layer glyph to an alpha mask

- width, height: Size of the output image

- foreground: Color to use for foreground (text) layers

func RenderCOLRToImage(
	glyph *COLRGlyph,
	renderLayer func(glyphID uint16) *image.Alpha,
	width, height int,
	foreground color.RGBA,
) *image.RGBA {
	if glyph == nil || len(glyph.Layers) == 0 {
		return nil
	}

	result := image.NewRGBA(image.Rect(0, 0, width, height))

	for _, layer := range glyph.Layers {
		// Render the layer glyph to an alpha mask
		mask := renderLayer(layer.GlyphID)
		if mask == nil {
			continue
		}

		// Determine layer color
		var layerColor color.RGBA
		if layer.IsForeground() {
			layerColor = foreground
		} else {
			layerColor = layer.Color.ToRGBA()
		}

		// Create a uniform color image
		colorImg := image.NewUniform(layerColor)

		// Composite layer onto result
		draw.DrawMask(result, result.Bounds(), colorImg, image.Point{}, mask, image.Point{}, draw.Over)
	}

	return result
}

func ToRGBA

ToRGBA returns the color as color.RGBA.

func (c Color) ToRGBA() color.RGBA {
	return color.RGBA{R: c.R, G: c.G, B: c.B, A: c.A}
}

func Width

Width returns the width of the rectangle.

func (r Rect) Width() float64 {
	return r.MaxX - r.MinX
}

Structs

type COLRGlyph struct

COLRGlyph represents a color glyph from COLR table.

It consists of multiple colored layers stacked on top of each other.

type COLRGlyph struct {
	// GlyphID is the original glyph ID for this color glyph.
	GlyphID	uint16

	// Layers contains the color layers, bottom to top.
	Layers	[]ColorLayer

	// Bounds is the bounding box in font units.
	Bounds	Rect

	// Version is the COLR table version (0 or 1).
	Version	uint16
}

type ColorLayer struct

ColorLayer represents one layer of a color glyph.

Each layer is a glyph rendered in a specific color.

type ColorLayer struct {
	// GlyphID is the glyph to render for this layer.
	GlyphID	uint16

	// PaletteIndex is the index into the CPAL color palette.
	// 0xFFFF indicates foreground color (use the text color).
	PaletteIndex	uint16

	// Color is the resolved color from the palette.
	// This is set after calling ResolvePalette.
	Color	Color
}

type Color struct

Color represents an RGBA color from CPAL palette.

type Color struct {
	R, G, B, A uint8
}

type Rect struct

Rect represents a bounding rectangle.

type Rect struct {
	MinX, MinY	float64
	MaxX, MaxY	float64
}

type COLRParser struct

COLRParser parses COLR/CPAL tables from font data.

type COLRParser struct {
	colrData	[]byte
	cpalData	[]byte

	// Parsed COLR header
	version		uint16
	numGlyphs	uint16
	baseGlyphs	[]baseGlyphRecord
	layers		[]layerRecord

	// Parsed CPAL palette
	palettes	[][]Color
}