text,widget: minimize loss of positional precision in shaping

This commit combs through the logic of computing glyph sizes and positions,
attempting to remove all unnecessary rounding and truncation. This is in
an effort to help text display consistently when different-length strings
are displayed near one another.

The specific problem prompting this change was end-aligned text stacked in
rows with a common suffix. If the rows displayed different values, they
would shape such that those final glyphs were at different fractional x
coordinates, and then they would be aligned with rounding that could display
them at different x positions in spite of the fact that both suffixes are
the same glyphs.

By removing rounding from Alignment.Align, the largest problem is fixed, but
I'm also removing other unnecessary loss of precision that can circumstantially
contribute to this sort of visual issue.

Signed-off-by: Chris Waldon <christopher.waldon.dev@gmail.com>
This commit is contained in:
Chris Waldon
2023-04-05 10:16:39 -04:00
committed by Elias Naur
parent c0d3f67b04
commit 73787b8478
4 changed files with 32 additions and 24 deletions
+11 -8
View File
@@ -563,16 +563,15 @@ func (s *shaperImpl) Shape(pathOps *op.Ops, gs []Glyph) clip.PathSpec {
}
ppem, faceIdx, gid := splitGlyphID(g.ID)
face := s.orderer.faceFor(faceIdx)
ppemInt := ppem.Round()
scaleFactor := float32(ppemInt) / float32(face.Upem())
scaleFactor := fixedToFloat(ppem) / float32(face.Upem())
glyphData := face.GlyphData(gid)
switch glyphData := glyphData.(type) {
case api.GlyphOutline:
outline := glyphData
// Move to glyph position.
pos := f32.Point{
X: float32(g.X-x)/64 - float32(g.Offset.X)/64,
Y: -float32(g.Offset.Y) / 64,
X: fixedToFloat((g.X - x) - g.Offset.X),
Y: -fixedToFloat(g.Offset.Y),
}
builder.Move(pos.Sub(lastPos))
lastPos = pos
@@ -617,6 +616,10 @@ func (s *shaperImpl) Shape(pathOps *op.Ops, gs []Glyph) clip.PathSpec {
return builder.End()
}
func fixedToFloat(i fixed.Int26_6) float32 {
return float32(i) / 64.0
}
// Bitmaps returns an op.CallOp that will display all bitmap glyphs within gs.
// The positioning of the bitmaps uses the same logic as Shape(), so the returned
// CallOp can be added at the same offset as the path data returned by Shape()
@@ -656,10 +659,10 @@ func (s *shaperImpl) Bitmaps(ops *op.Ops, gs []Glyph) op.CallOp {
imgOp = bitmapData.img
imgSize = bitmapData.size
}
off := op.Offset(image.Point{
X: ((g.X - x) - g.Offset.X).Round(),
Y: g.Offset.Y.Round() - g.Ascent.Round(),
}).Push(ops)
off := op.Affine(f32.Affine2D{}.Offset(f32.Point{
X: fixedToFloat((g.X - x) - g.Offset.X),
Y: fixedToFloat(g.Offset.Y - g.Ascent),
})).Push(ops)
cl := clip.Rect{Max: imgSize}.Push(ops)
glyphSize := image.Rectangle{
+2 -2
View File
@@ -100,9 +100,9 @@ func (a Alignment) Align(dir system.TextDirection, width fixed.Int26_6, maxWidth
}
switch a {
case Middle:
return fixed.I(((mw - width) / 2).Floor())
return (mw - width) / 2
case End:
return fixed.I((mw - width).Floor())
return (mw - width)
case Start:
return 0
default: