Fix text offset: use (x, y) for X and subtract baseline Y so text top aligns with y
Co-authored-by: Qwen-Coder <qwen-coder@alibabacloud.com>
This commit is contained in:
parent
965d6074f3
commit
eb9754fde0
|
|
@ -4,6 +4,7 @@ import (
|
||||||
"image"
|
"image"
|
||||||
"image/color"
|
"image/color"
|
||||||
|
|
||||||
|
"gioui.org/f32"
|
||||||
"gioui.org/layout"
|
"gioui.org/layout"
|
||||||
"gioui.org/op"
|
"gioui.org/op"
|
||||||
"gioui.org/op/clip"
|
"gioui.org/op/clip"
|
||||||
|
|
@ -259,10 +260,17 @@ func (r *Renderer) drawLineText(gtx layout.Context, shp *text.Shaper, x, y unit.
|
||||||
|
|
||||||
// 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, x, y unit.Dp, col color.NRGBA) {
|
||||||
// Offset coordinate system to (x, y).
|
if len(line) == 0 {
|
||||||
// The shaper's glyph positions (g.X, g.Y) are absolute within the text block.
|
return
|
||||||
// g.Y for the first line is the ascent, so the top of the text will be at y.
|
}
|
||||||
t := op.Offset(image.Pt(gtx.Dp(x), gtx.Dp(y))).Push(gtx.Ops)
|
first := line[0]
|
||||||
|
// shaper.Shape(line) returns a path where glyph positions are relative to
|
||||||
|
// the first glyph (first.X == 0 in the returned path). So we offset by
|
||||||
|
// (x, y) and let the shaper handle the rest.
|
||||||
|
// first.Y is the baseline; we subtract it so the top of the text is at y.
|
||||||
|
offX := float32(gtx.Dp(x))
|
||||||
|
offY := float32(gtx.Dp(y)) - float32(first.Y)
|
||||||
|
t := op.Affine(f32.Affine2D{}.Offset(f32.Pt(offX, offY))).Push(gtx.Ops)
|
||||||
|
|
||||||
// Draw vector glyphs
|
// Draw vector glyphs
|
||||||
path := shp.Shape(line)
|
path := shp.Shape(line)
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user