From a7539fd29b6ff2ffbe5c6c01b7b17d5471cae31f Mon Sep 17 00:00:00 2001 From: Hamcha Date: Fri, 16 Nov 2018 17:36:38 +0100 Subject: [PATCH] Fix bound calculation with just emojis (again, super hacky) --- draw2dimg/ftgc.go | 29 +++++++++++++++++++++++++++-- 1 file changed, 27 insertions(+), 2 deletions(-) diff --git a/draw2dimg/ftgc.go b/draw2dimg/ftgc.go index ce4ca77..d44111f 100644 --- a/draw2dimg/ftgc.go +++ b/draw2dimg/ftgc.go @@ -275,10 +275,35 @@ 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 + // Get sample letter for approximated emoji calculations + const letter = 'M' + mindex := f.Index(letter) + mtop, mleft, mheight, mwidth := 10e6, 10e6, -10e6, -10e6 + if err := gc.glyphBuf.Load(gc.Current.Font, fixed.Int26_6(gc.Current.Scale), mindex, font.HintingNone); err != nil { + log.Println(err) + return 0, 0, 0, 0 + } + e0 := 0 + for _, e1 := range gc.glyphBuf.Ends { + ps := gc.glyphBuf.Points[e0:e1] + for _, p := range ps { + x, y := pointToF64Point(p) + mtop = math.Min(mtop, y) + mheight = math.Max(mheight, y) + mleft = math.Min(mleft, x) + mwidth = math.Max(mwidth, x) + } + } + mtop *= 1.2 + mwidth *= 1.55 + mheight += math.Abs(mtop-mheight) * 0.2 + // Actually iterate through the string for fragment := range gc.Emojis.Iterate(s) { if fragment.IsEmoji { - cursor += fUnitsToFloat64(fixed.Int26_6(gc.Current.Scale)) * 1.15 - left = math.Min(left, cursor) + cursor += mwidth + top = math.Min(top, mtop) + bottom = math.Max(bottom, mheight) + left = math.Min(left, mleft) right = math.Max(right, cursor) continue }