From 9941d7746001563417ca26eee03488fb0918df7e Mon Sep 17 00:00:00 2001 From: Hamcha Date: Fri, 16 Nov 2018 15:19:48 +0100 Subject: [PATCH] Add emojis to bound calculation --- draw2dimg/ftgc.go | 27 ++++++++++++++++++++++----- 1 file changed, 22 insertions(+), 5 deletions(-) diff --git a/draw2dimg/ftgc.go b/draw2dimg/ftgc.go index 141ac63..5482a23 100644 --- a/draw2dimg/ftgc.go +++ b/draw2dimg/ftgc.go @@ -179,12 +179,25 @@ func (gc *GraphicContext) StrokeStringAt(text string, x, y float64) (width float startx := x prev, hasPrev := truetype.Index(0), false fontName := gc.GetFontName() - for _, r := range text { - index := f.Index(r) + for fragment := range gc.Emojis.Iterate(text) { + 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 { 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.Stroke(gc, x, y) prev, hasPrev = index, true } @@ -262,8 +275,12 @@ func (gc *GraphicContext) GetStringBounds(s string) (left, top, right, bottom fl top, left, bottom, right = 10e6, 10e6, -10e6, -10e6 cursor := 0.0 prev, hasPrev := truetype.Index(0), false - for _, rune := range s { - index := f.Index(rune) + for fragment := range gc.Emojis.Iterate(s) { + if fragment.IsEmoji { + cursor += gc.Current.Scale*emojiScale + gc.Current.Scale*emojiSpacing*2 + continue + } + index := f.Index(fragment.Rune) if hasPrev { cursor += fUnitsToFloat64(f.Kern(fixed.Int26_6(gc.Current.Scale), prev, index)) }