font/opentype,text,widget: use clip.Op for text shapes, not a macro

This change avoids a macro wrapping every text shape, and prepares text
shaping for scoped clip operations.

Signed-off-by: Elias Naur <mail@eliasnaur.com>
This commit is contained in:
Elias Naur
2021-10-02 18:11:31 +02:00
parent cd5a78f3d8
commit c1298cd755
7 changed files with 26 additions and 29 deletions
+5 -7
View File
@@ -119,7 +119,7 @@ func (f *Font) Layout(ppem fixed.Int26_6, maxWidth int, txt io.Reader) ([]text.L
return layoutText(&buf, ppem, maxWidth, fonts, glyphs)
}
func (f *Font) Shape(ppem fixed.Int26_6, str text.Layout) op.CallOp {
func (f *Font) Shape(ppem fixed.Int26_6, str text.Layout) clip.Op {
var buf sfnt.Buffer
return textPath(&buf, ppem, []*opentype{{Font: f.font, Hinting: font.HintingFull}}, str)
}
@@ -139,7 +139,7 @@ func (c *Collection) Layout(ppem fixed.Int26_6, maxWidth int, txt io.Reader) ([]
return layoutText(&buf, ppem, maxWidth, c.fonts, glyphs)
}
func (c *Collection) Shape(ppem fixed.Int26_6, str text.Layout) op.CallOp {
func (c *Collection) Shape(ppem fixed.Int26_6, str text.Layout) clip.Op {
var buf sfnt.Buffer
return textPath(&buf, ppem, c.fonts, str)
}
@@ -261,11 +261,10 @@ func toLayout(glyphs []glyph) text.Layout {
return text.Layout{Text: buf.String(), Advances: advs}
}
func textPath(buf *sfnt.Buffer, ppem fixed.Int26_6, fonts []*opentype, str text.Layout) op.CallOp {
func textPath(buf *sfnt.Buffer, ppem fixed.Int26_6, fonts []*opentype, str text.Layout) clip.Op {
var lastPos f32.Point
var builder clip.Path
ops := new(op.Ops)
m := op.Record(ops)
var x fixed.Int26_6
builder.Begin(ops)
rune := 0
@@ -324,10 +323,9 @@ func textPath(buf *sfnt.Buffer, ppem fixed.Int26_6, fonts []*opentype, str text.
x += str.Advances[rune]
rune++
}
clip.Outline{
return clip.Outline{
Path: builder.End(),
}.Op().Add(ops)
return m.Stop()
}.Op()
}
func readGlyphs(r io.Reader) ([]glyph, error) {
+6 -5
View File
@@ -17,6 +17,7 @@ import (
"gioui.org/internal/ops"
"gioui.org/op"
"gioui.org/op/clip"
"gioui.org/text"
)
@@ -69,7 +70,7 @@ func TestCollectionAsFace(t *testing.T) {
// All shapes from the original fonts should be distinct because the glyphs are distinct, including the replacement
// glyphs.
distinctShapes := []op.CallOp{shapeValid1, shapeInvalid1, shapeValid2, shapeInvalid2}
distinctShapes := []clip.Op{shapeValid1, shapeInvalid1, shapeValid2, shapeInvalid2}
for i := 0; i < len(distinctShapes); i++ {
for j := i + 1; j < len(distinctShapes); j++ {
if areShapesEqual(distinctShapes[i], distinctShapes[j]) {
@@ -173,20 +174,20 @@ func mergeFonts(ttf1, ttf2 []byte) []byte {
}
// shapeRune uses a given Face to shape exactly one rune at a fixed size, then returns the resulting shape data.
func shapeRune(f text.Face, r rune) (op.CallOp, error) {
func shapeRune(f text.Face, r rune) (clip.Op, error) {
ppem := fixed.I(200)
lines, err := f.Layout(ppem, 2000, strings.NewReader(string(r)))
if err != nil {
return op.CallOp{}, err
return clip.Op{}, err
}
if len(lines) != 1 {
return op.CallOp{}, fmt.Errorf("unexpected rendering for \"U+%08X\": got %d lines (expected: 1)", r, len(lines))
return clip.Op{}, fmt.Errorf("unexpected rendering for \"U+%08X\": got %d lines (expected: 1)", r, len(lines))
}
return f.Shape(ppem, lines[0].Layout), nil
}
// areShapesEqual returns true iff both given text shapes are produced with identical operations.
func areShapesEqual(shape1, shape2 op.CallOp) bool {
func areShapesEqual(shape1, shape2 clip.Op) bool {
var ops1, ops2 op.Ops
shape1.Add(&ops1)
shape2.Add(&ops2)