Add emoji support
This commit is contained in:
parent
219501b99b
commit
e3566f7fc4
1 changed files with 22 additions and 4 deletions
|
@ -35,10 +35,11 @@ type GraphicContext struct {
|
||||||
painter Painter
|
painter Painter
|
||||||
fillRasterizer *raster.Rasterizer
|
fillRasterizer *raster.Rasterizer
|
||||||
strokeRasterizer *raster.Rasterizer
|
strokeRasterizer *raster.Rasterizer
|
||||||
FontCache draw2d.FontCache
|
FontCache draw2d.FontCache
|
||||||
glyphCache draw2dbase.GlyphCache
|
glyphCache draw2dbase.GlyphCache
|
||||||
glyphBuf *truetype.GlyphBuf
|
glyphBuf *truetype.GlyphBuf
|
||||||
DPI int
|
DPI int
|
||||||
|
Emojis emoji.Table
|
||||||
}
|
}
|
||||||
|
|
||||||
// ImageFilter defines the type of filter to use
|
// ImageFilter defines the type of filter to use
|
||||||
|
@ -79,6 +80,7 @@ func NewGraphicContextWithPainter(img draw.Image, painter Painter) *GraphicConte
|
||||||
draw2dbase.NewGlyphCache(),
|
draw2dbase.NewGlyphCache(),
|
||||||
&truetype.GlyphBuf{},
|
&truetype.GlyphBuf{},
|
||||||
dpi,
|
dpi,
|
||||||
|
make(emoji.Table),
|
||||||
}
|
}
|
||||||
return gc
|
return gc
|
||||||
}
|
}
|
||||||
|
@ -124,6 +126,9 @@ func (gc *GraphicContext) FillString(text string) (width float64) {
|
||||||
return gc.FillStringAt(text, 0, 0)
|
return gc.FillStringAt(text, 0, 0)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const emojiSpacing = 10
|
||||||
|
const emojiScale = 110
|
||||||
|
|
||||||
// FillStringAt draws the text at the specified point (x, y)
|
// FillStringAt draws the text at the specified point (x, y)
|
||||||
func (gc *GraphicContext) FillStringAt(text string, x, y float64) (width float64) {
|
func (gc *GraphicContext) FillStringAt(text string, x, y float64) (width float64) {
|
||||||
f, err := gc.loadCurrentFont()
|
f, err := gc.loadCurrentFont()
|
||||||
|
@ -134,12 +139,25 @@ func (gc *GraphicContext) FillStringAt(text string, x, y float64) (width float64
|
||||||
startx := x
|
startx := x
|
||||||
prev, hasPrev := truetype.Index(0), false
|
prev, hasPrev := truetype.Index(0), false
|
||||||
fontName := gc.GetFontName()
|
fontName := gc.GetFontName()
|
||||||
for _, r := range text {
|
for fragment := range gc.Emojis.Iterate(text) {
|
||||||
index := f.Index(r)
|
if fragment.IsEmoji {
|
||||||
|
img, err := LoadFromPngFile(fragment.Emoji.Path)
|
||||||
|
if err == nil {
|
||||||
|
gc.Save()
|
||||||
|
scale := gc.GetFontSize() / 100
|
||||||
|
gc.Translate(x+scale*emojiSpacing, y-scale*emojiScale)
|
||||||
|
gc.Scale(scale, scale)
|
||||||
|
gc.DrawImage(img)
|
||||||
|
gc.Restore()
|
||||||
|
x += scale*float64(img.Bounds().Size().X) + scale*emojiSpacing*2
|
||||||
|
}
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
index := f.Index(fragment.Rune)
|
||||||
if hasPrev {
|
if hasPrev {
|
||||||
x += fUnitsToFloat64(f.Kern(fixed.Int26_6(gc.Current.Scale), prev, index))
|
x += fUnitsToFloat64(f.Kern(fixed.Int26_6(gc.Current.Scale), prev, index))
|
||||||
}
|
}
|
||||||
glyph := gc.glyphCache.Fetch(gc, fontName, r)
|
glyph := gc.glyphCache.Fetch(gc, fontName, fragment.Rune)
|
||||||
x += glyph.Fill(gc, x, y)
|
x += glyph.Fill(gc, x, y)
|
||||||
prev, hasPrev = index, true
|
prev, hasPrev = index, true
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue