From 668124d25c6fb3e23fc472dd4b8481eee94b0561 Mon Sep 17 00:00:00 2001 From: Hajime Hoshi Date: Tue, 5 Dec 2023 20:01:25 +0900 Subject: [PATCH] text/v2: add a non-image glyphs for index info --- examples/text/main.go | 3 +++ text/v2/gotext.go | 20 ++++++++++---------- text/v2/layout.go | 3 +++ text/v2/std.go | 26 ++++++++++++++------------ 4 files changed, 30 insertions(+), 22 deletions(-) diff --git a/examples/text/main.go b/examples/text/main.go index 2bbcb92d6..8a0090d21 100644 --- a/examples/text/main.go +++ b/examples/text/main.go @@ -125,6 +125,9 @@ func (g *Game) Draw(screen *ebiten.Image) { // You can customize how to render each glyph. // In this example, multiple colors are used to render glyphs. for i, gl := range g.glyphs { + if gl.Image == nil { + continue + } op.GeoM.Reset() op.GeoM.Translate(x, y) op.GeoM.Translate(gl.X, gl.Y) diff --git a/text/v2/gotext.go b/text/v2/gotext.go index 9e709d069..0ad63d92f 100644 --- a/text/v2/gotext.go +++ b/text/v2/gotext.go @@ -300,16 +300,16 @@ func (g *GoTextFace) appendGlyphsForLine(glyphs []Glyph, line string, indexOffse _, gs := g.Source.shape(line, g) for _, glyph := range gs { img, imgX, imgY := g.glyphImage(glyph, origin) - if img != nil { - glyphs = append(glyphs, Glyph{ - StartIndexInBytes: indexOffset + glyph.startIndex, - EndIndexInBytes: indexOffset + glyph.endIndex, - GID: uint32(glyph.shapingGlyph.GlyphID), - Image: img, - X: float64(imgX), - Y: float64(imgY), - }) - } + // Append a glyph even if img is nil. + // This is necessary to return index information for control characters. + glyphs = append(glyphs, Glyph{ + StartIndexInBytes: indexOffset + glyph.startIndex, + EndIndexInBytes: indexOffset + glyph.endIndex, + GID: uint32(glyph.shapingGlyph.GlyphID), + Image: img, + X: float64(imgX), + Y: float64(imgY), + }) origin = origin.Add(fixed.Point26_6{ X: glyph.shapingGlyph.XAdvance, Y: -glyph.shapingGlyph.YAdvance, diff --git a/text/v2/layout.go b/text/v2/layout.go index eeb251864..fdb7e1ed6 100644 --- a/text/v2/layout.go +++ b/text/v2/layout.go @@ -106,6 +106,9 @@ func Draw(dst *ebiten.Image, text string, face Face, options *DrawOptions) { geoM := options.GeoM for _, g := range AppendGlyphs(nil, text, face, &options.LayoutOptions) { + if g.Image == nil { + continue + } op := &options.DrawImageOptions op.GeoM.Reset() op.GeoM.Translate(g.X, g.Y) diff --git a/text/v2/std.go b/text/v2/std.go index 46cfaeded..4c8703d91 100644 --- a/text/v2/std.go +++ b/text/v2/std.go @@ -108,18 +108,20 @@ func (s *StdFace) appendGlyphsForLine(glyphs []Glyph, line string, indexOffset i origin.X += s.f.Kern(prevR, r) } 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. - _, size := utf8.DecodeRuneInString(line[i:]) - glyphs = append(glyphs, Glyph{ - StartIndexInBytes: indexOffset + i, - EndIndexInBytes: indexOffset + i + size, - Image: img, - X: float64(imgX), - Y: float64(imgY), - }) - } + + // Adjust the position to the integers. + // The current glyph images assume that they are rendered on integer positions so far. + _, size := utf8.DecodeRuneInString(line[i:]) + + // Append a glyph even if img is nil. + // This is necessary to return index information for control characters. + glyphs = append(glyphs, Glyph{ + StartIndexInBytes: indexOffset + i, + EndIndexInBytes: indexOffset + i + size, + Image: img, + X: float64(imgX), + Y: float64(imgY), + }) origin.X += a prevR = r }