Fix offset calculation: derive from first glyph position like Gio's textView

Co-authored-by: Qwen-Coder <qwen-coder@alibabacloud.com>
This commit is contained in:
Greg Pomerantz 2026-05-09 11:32:28 -04:00
parent 1ee75c1cd1
commit 4798e8fe3d

View File

@ -250,24 +250,33 @@ func (r *Renderer) drawLineText(gtx layout.Context, shp *text.Shaper, x, y unit.
m := op.Record(gtx.Ops) m := op.Record(gtx.Ops)
var glyphs [32]text.Glyph var glyphs [32]text.Glyph
line := glyphs[:0] line := glyphs[:0]
var firstGlyph text.Glyph
first := true
for g, ok := shp.NextGlyph(); ok; g, ok = shp.NextGlyph() { for g, ok := shp.NextGlyph(); ok; g, ok = shp.NextGlyph() {
if first {
firstGlyph = g
first = false
}
line = append(line, g) line = append(line, g)
if g.Flags&text.FlagLineBreak != 0 || cap(line)-len(line) == 0 { if g.Flags&text.FlagLineBreak != 0 || cap(line)-len(line) == 0 {
r.drawLine(gtx, shp, line, x, y, col) r.drawLine(gtx, shp, line, firstGlyph, x, y, col)
line = line[:0] line = line[:0]
} }
} }
if len(line) > 0 { if len(line) > 0 {
r.drawLine(gtx, shp, line, x, y, col) r.drawLine(gtx, shp, line, firstGlyph, x, y, col)
} }
call := m.Stop() call := m.Stop()
call.Add(gtx.Ops) call.Add(gtx.Ops)
} }
// drawLine draws a line of glyphs using shaper.Shape() and shaper.Bitmaps(). // drawLine draws a line of glyphs using shaper.Shape() and shaper.Bitmaps().
func (r *Renderer) drawLine(gtx layout.Context, shp *text.Shaper, line []text.Glyph, x, y unit.Dp, col color.NRGBA) { func (r *Renderer) drawLine(gtx layout.Context, shp *text.Shaper, line []text.Glyph, firstGlyph text.Glyph, x, y unit.Dp, col color.NRGBA) {
// Apply offset transform // Derive offset from first glyph position + desired position
off := f32.Point{X: float32(x), Y: float32(y)} // Gio's textView: lineOff = glyphPos - viewportMin
// We want: offset = (x, y) - firstGlyphPos
firstPos := f32.Point{X: float32(firstGlyph.X) / 64.0, Y: float32(firstGlyph.Y)}
off := f32.Point{X: float32(x), Y: float32(y)}.Sub(firstPos)
t := op.Affine(f32.Affine2D{}.Offset(off)).Push(gtx.Ops) t := op.Affine(f32.Affine2D{}.Offset(off)).Push(gtx.Ops)
// Draw vector glyphs // Draw vector glyphs