Compute glyphLayout after text rendering. Implement correct cursor

movement.
This commit is contained in:
gmp
2026-06-03 19:01:05 -04:00
parent ccd258306e
commit a5d1c4bee6
6 changed files with 102 additions and 31 deletions
+34 -6
View File
@@ -8,6 +8,7 @@ import (
_ "image/png"
"log"
"time"
"unicode/utf8"
"gioui.org/f32"
"gioui.org/gesture"
@@ -60,8 +61,9 @@ type Renderer struct {
clicks map[string]clickReg
Keys map[string]keyReg // Exported Keys map
scrolls map[string]scrollReg
displayLineCount int // number of display lines from last drawWrappedText
lastLineY Dp // last line baseline offset from text origin, in Dp
displayLineCount int // number of display lines from last drawWrappedText
lastLineY Dp // last line baseline offset from text origin, in Dp (derived from GlyphLayout)
glyphLayout GlyphLayout // captured per-glyph layout from last drawWrappedText
}
// New creates a new Renderer.
@@ -229,6 +231,13 @@ func (r *Renderer) LastLineY() Dp {
return r.lastLineY
}
// GlyphLayout returns the glyph layout data captured during the last
// drawWrappedText call. Used by the logic goroutine to position the cursor
// and navigate by glyph instead of byte offset.
func (r *Renderer) GlyphLayout() GlyphLayout {
return r.glyphLayout
}
// clippableElement is implemented by elements that need their own clip region
// around all their content and interaction registrations.
type clippableElement interface {
@@ -472,12 +481,28 @@ func (r *Renderer) drawWrappedText(gtx layout.Context, str string, reg Region, w
var glyphs [32]text.Glyph
line := glyphs[:0]
lineCount := 0
var lastGlyphY float32 // shaper Y value of last glyph drawn
// Capture per-glyph layout data for cursor positioning and navigation.
var layout GlyphLayout
byteOffset := 0
for g, ok := r.shp.NextGlyph(); ok; g, ok = r.shp.NextGlyph() {
// Record layout data for this glyph.
// g.X is in fixed.Int26_6 — shift >> 6 for device pixels, divide by scale for Dp.
// g.Y is the baseline in device pixels.
layout.ByteOffsets = append(layout.ByteOffsets, byteOffset)
layout.X = append(layout.X, Dp(float32(g.X>>6)/r.scale.Scale()))
layout.Y = append(layout.Y, Dp(float32(g.Y)/r.scale.Scale()))
layout.Advance = append(layout.Advance, Dp(float32(g.Advance>>6)/r.scale.Scale()))
// Advance byteOffset by g.Runes.
for i := uint16(0); i < g.Runes; i++ {
_, sz := utf8.DecodeRuneInString(str[byteOffset:])
byteOffset += sz
}
line = append(line, g)
if g.Flags&text.FlagLineBreak != 0 || cap(line)-len(line) == 0 {
r.drawLine(gtx, line, reg.X, y, col)
lastGlyphY = float32(line[len(line)-1].Y)
line = line[:0]
if g.Flags&text.FlagLineBreak != 0 {
lineCount++
@@ -486,7 +511,6 @@ func (r *Renderer) drawWrappedText(gtx layout.Context, str string, reg Region, w
}
if len(line) > 0 {
r.drawLine(gtx, line, reg.X, y, col)
lastGlyphY = float32(line[len(line)-1].Y)
lineCount++
}
call := m.Stop()
@@ -494,7 +518,11 @@ func (r *Renderer) drawWrappedText(gtx layout.Context, str string, reg Region, w
textClip.Pop()
r.displayLineCount = lineCount
r.lastLineY = Dp(lastGlyphY / r.scale.Scale()) // pixels → Dp
// Store captured layout; derive lastLineY from it.
r.glyphLayout = layout
if len(layout.Y) > 0 {
r.lastLineY = layout.Y[len(layout.Y)-1]
}
}
func (r *Renderer) drawPng(gtx layout.Context, img image.Image, reg Region, width, height Dp) {
+12
View File
@@ -63,3 +63,15 @@ func FromDp(r Region, scale float32) RegionPx {
H: ToPx(r.H, scale),
}
}
// GlyphLayout holds per-glyph layout data captured during text shaping.
// Each index i represents one glyph (one rune in the source string).
// ByteOffsets[i] is the byte position in the buffer, (X[i], Y[i]) is the
// glyph's screen location in Dp (X relative to text region origin, Y is the
// shaper baseline), and Advance[i] is the glyph's width in Dp.
type GlyphLayout struct {
ByteOffsets []int // byte offset of each glyph in the buffer
X []Dp // screen X (Dp) of each glyph, relative to text region origin
Y []Dp // screen Y (Dp) baseline of each glyph (shaper value)
Advance []Dp // advance width (Dp) of each glyph
}