From 9735687d3e88f0daeb994fbcef0cb2d95f1e4dcb Mon Sep 17 00:00:00 2001 From: Hajime Hoshi Date: Mon, 13 Nov 2023 01:31:25 +0900 Subject: [PATCH] text/v2: refactoring --- text/v2/std.go | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/text/v2/std.go b/text/v2/std.go index 1aba07918..cc0daac50 100644 --- a/text/v2/std.go +++ b/text/v2/std.go @@ -95,15 +95,17 @@ func (s *StdFace) advance(text string) float64 { func (s *StdFace) appendGlyphs(glyphs []Glyph, text string, originX, originY float64) []Glyph { s.copyCheck() - x := float64ToFixed26_6(originX) - y := float64ToFixed26_6(originY) + origin := fixed.Point26_6{ + X: float64ToFixed26_6(originX), + Y: float64ToFixed26_6(originY), + } prevR := rune(-1) for i, r := range text { if prevR >= 0 { - x += s.f.Kern(prevR, r) + origin.X += s.f.Kern(prevR, r) } - img, imgX, imgY, a := s.glyphImage(r, x, y) + img, imgX, imgY, a := s.glyphImage(r, origin) if img != nil { // Adjust the position to the integers. // The current glyph images assume that they are rendered on integer positions so far. @@ -115,18 +117,18 @@ func (s *StdFace) appendGlyphs(glyphs []Glyph, text string, originX, originY flo Y: imgY, }) } - x += a + origin.X += a prevR = r } return glyphs } -func (s *StdFace) glyphImage(r rune, x, y fixed.Int26_6) (*ebiten.Image, float64, float64, fixed.Int26_6) { +func (s *StdFace) glyphImage(r rune, origin fixed.Point26_6) (*ebiten.Image, float64, float64, fixed.Int26_6) { b, a, _ := s.f.GlyphBounds(r) offset := fixed.Point26_6{ - X: (adjustOffsetGranularity(x) + b.Min.X) & ((1 << 6) - 1), - Y: (fixed.I(y.Floor()) + b.Min.Y) & ((1 << 6) - 1), + X: (adjustOffsetGranularity(origin.X) + b.Min.X) & ((1 << 6) - 1), + Y: (fixed.I(origin.Y.Floor()) + b.Min.Y) & ((1 << 6) - 1), } key := glyphImageCacheKey{ rune: r, @@ -136,8 +138,8 @@ func (s *StdFace) glyphImage(r rune, x, y fixed.Int26_6) (*ebiten.Image, float64 img := theGlyphImageCache.getOrCreate(s, key, func() *ebiten.Image { return s.glyphImageImpl(r, offset) }) - imgX := fixed26_6ToFloat64(x + b.Min.X - offset.X) - imgY := fixed26_6ToFloat64(y + b.Min.Y - offset.Y) + imgX := fixed26_6ToFloat64(origin.X + b.Min.X - offset.X) + imgY := fixed26_6ToFloat64(origin.Y + b.Min.Y - offset.Y) return img, imgX, imgY, a }