Add emojis to bound calculation

This commit is contained in:
Hamcha 2018-11-16 15:19:48 +01:00
parent e3566f7fc4
commit 9941d77460
Signed by: hamcha
GPG Key ID: A40413D21021EAEE
1 changed files with 22 additions and 5 deletions

View File

@ -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))
}