text/draw_emoji.go

Functions Structs

Functions

func Clear

Clear removes all entries from the cache.

func (c *BitmapGlyphCache) Clear() {
	c.entries = make(map[bitmapCacheKey]*CachedBitmap)
}

func DrawWithEmoji

DrawWithEmoji renders text with full color emoji support.

This function detects glyph types and routes to appropriate renderers:

- Outline glyphs: Standard font.Drawer rendering

- Bitmap glyphs: PNG bitmap scaling and compositing

- COLR glyphs: Layer-based color glyph rendering (future)

 

Position (x, y) is the baseline origin.

For fonts without color tables, this behaves identically to Draw().

func DrawWithEmoji(dst draw.Image, text string, face Face, x, y float64, col color.Color) {
	if text == "" || face == nil {
		return
	}

	// Check if the font has color tables.
	parsed := face.Source().Parsed()
	colorFont, hasColor := parsed.(ColorFont)

	if !hasColor || !colorFont.HasColorTables() {
		// No color support, fall back to standard rendering.
		Draw(dst, text, face, x, y, col)
		return
	}

	// Color-aware rendering.
	drawWithColorSupport(dst, text, face, x, y, col, colorFont)
}

func Get

Get retrieves a cached bitmap, or nil if not cached.

func (c *BitmapGlyphCache) Get(fontID uintptr, glyphID, ppem uint16) *CachedBitmap {
	key := bitmapCacheKey{fontID: fontID, glyphID: glyphID, ppem: ppem}
	return c.entries[key]
}

func NewBitmapGlyphCache

NewBitmapGlyphCache creates a new bitmap glyph cache.

maxSize is the maximum number of cached entries.

func NewBitmapGlyphCache(maxSize int) *BitmapGlyphCache {
	return &BitmapGlyphCache{
		entries:	make(map[bitmapCacheKey]*CachedBitmap),
		maxSize:	maxSize,
	}
}

func Put

Put stores a bitmap in the cache.

func (c *BitmapGlyphCache) Put(fontID uintptr, glyphID, ppem uint16, bitmap *emoji.BitmapGlyph, img image.Image) {
	// Simple eviction: clear when full.
	if len(c.entries) >= c.maxSize {
		c.Clear()
	}

	key := bitmapCacheKey{fontID: fontID, glyphID: glyphID, ppem: ppem}
	c.entries[key] = &CachedBitmap{
		Img:		img,
		OriginX:	bitmap.OriginX,
		OriginY:	bitmap.OriginY,
		Width:		bitmap.Width,
		Height:		bitmap.Height,
	}
}

func Size

Size returns the number of cached entries.

func (c *BitmapGlyphCache) Size() int {
	return len(c.entries)
}

Structs

type BitmapGlyphCache struct

BitmapGlyphCache caches decoded bitmap glyphs to avoid repeated PNG decoding.

This is important for performance when rendering the same emoji multiple times.

type BitmapGlyphCache struct {
	entries	map[bitmapCacheKey]*CachedBitmap
	maxSize	int
}

type CachedBitmap struct

CachedBitmap holds a decoded and optionally scaled bitmap.

type CachedBitmap struct {
	Img	image.Image
	OriginX	float32
	OriginY	float32
	Width	int
	Height	int
}