text/v2: add a non-image glyphs for index info

This commit is contained in:
Hajime Hoshi 2023-12-05 20:01:25 +09:00
parent 800835d081
commit 668124d25c
4 changed files with 30 additions and 22 deletions

View File

@ -125,6 +125,9 @@ func (g *Game) Draw(screen *ebiten.Image) {
// You can customize how to render each glyph. // You can customize how to render each glyph.
// In this example, multiple colors are used to render glyphs. // In this example, multiple colors are used to render glyphs.
for i, gl := range g.glyphs { for i, gl := range g.glyphs {
if gl.Image == nil {
continue
}
op.GeoM.Reset() op.GeoM.Reset()
op.GeoM.Translate(x, y) op.GeoM.Translate(x, y)
op.GeoM.Translate(gl.X, gl.Y) op.GeoM.Translate(gl.X, gl.Y)

View File

@ -300,16 +300,16 @@ func (g *GoTextFace) appendGlyphsForLine(glyphs []Glyph, line string, indexOffse
_, gs := g.Source.shape(line, g) _, gs := g.Source.shape(line, g)
for _, glyph := range gs { for _, glyph := range gs {
img, imgX, imgY := g.glyphImage(glyph, origin) img, imgX, imgY := g.glyphImage(glyph, origin)
if img != nil { // Append a glyph even if img is nil.
glyphs = append(glyphs, Glyph{ // This is necessary to return index information for control characters.
StartIndexInBytes: indexOffset + glyph.startIndex, glyphs = append(glyphs, Glyph{
EndIndexInBytes: indexOffset + glyph.endIndex, StartIndexInBytes: indexOffset + glyph.startIndex,
GID: uint32(glyph.shapingGlyph.GlyphID), EndIndexInBytes: indexOffset + glyph.endIndex,
Image: img, GID: uint32(glyph.shapingGlyph.GlyphID),
X: float64(imgX), Image: img,
Y: float64(imgY), X: float64(imgX),
}) Y: float64(imgY),
} })
origin = origin.Add(fixed.Point26_6{ origin = origin.Add(fixed.Point26_6{
X: glyph.shapingGlyph.XAdvance, X: glyph.shapingGlyph.XAdvance,
Y: -glyph.shapingGlyph.YAdvance, Y: -glyph.shapingGlyph.YAdvance,

View File

@ -106,6 +106,9 @@ func Draw(dst *ebiten.Image, text string, face Face, options *DrawOptions) {
geoM := options.GeoM geoM := options.GeoM
for _, g := range AppendGlyphs(nil, text, face, &options.LayoutOptions) { for _, g := range AppendGlyphs(nil, text, face, &options.LayoutOptions) {
if g.Image == nil {
continue
}
op := &options.DrawImageOptions op := &options.DrawImageOptions
op.GeoM.Reset() op.GeoM.Reset()
op.GeoM.Translate(g.X, g.Y) op.GeoM.Translate(g.X, g.Y)

View File

@ -108,18 +108,20 @@ func (s *StdFace) appendGlyphsForLine(glyphs []Glyph, line string, indexOffset i
origin.X += s.f.Kern(prevR, r) origin.X += s.f.Kern(prevR, r)
} }
img, imgX, imgY, a := s.glyphImage(r, origin) img, imgX, imgY, a := s.glyphImage(r, origin)
if img != nil {
// Adjust the position to the integers. // Adjust the position to the integers.
// The current glyph images assume that they are rendered on integer positions so far. // The current glyph images assume that they are rendered on integer positions so far.
_, size := utf8.DecodeRuneInString(line[i:]) _, size := utf8.DecodeRuneInString(line[i:])
glyphs = append(glyphs, Glyph{
StartIndexInBytes: indexOffset + i, // Append a glyph even if img is nil.
EndIndexInBytes: indexOffset + i + size, // This is necessary to return index information for control characters.
Image: img, glyphs = append(glyphs, Glyph{
X: float64(imgX), StartIndexInBytes: indexOffset + i,
Y: float64(imgY), EndIndexInBytes: indexOffset + i + size,
}) Image: img,
} X: float64(imgX),
Y: float64(imgY),
})
origin.X += a origin.X += a
prevR = r prevR = r
} }