mirror of
https://git.sr.ht/~eliasnaur/gio
synced 2026-07-07 18:35:34 +00:00
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:
@@ -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)
|
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
|
var buf sfnt.Buffer
|
||||||
return textPath(&buf, ppem, []*opentype{{Font: f.font, Hinting: font.HintingFull}}, str)
|
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)
|
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
|
var buf sfnt.Buffer
|
||||||
return textPath(&buf, ppem, c.fonts, str)
|
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}
|
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 lastPos f32.Point
|
||||||
var builder clip.Path
|
var builder clip.Path
|
||||||
ops := new(op.Ops)
|
ops := new(op.Ops)
|
||||||
m := op.Record(ops)
|
|
||||||
var x fixed.Int26_6
|
var x fixed.Int26_6
|
||||||
builder.Begin(ops)
|
builder.Begin(ops)
|
||||||
rune := 0
|
rune := 0
|
||||||
@@ -324,10 +323,9 @@ func textPath(buf *sfnt.Buffer, ppem fixed.Int26_6, fonts []*opentype, str text.
|
|||||||
x += str.Advances[rune]
|
x += str.Advances[rune]
|
||||||
rune++
|
rune++
|
||||||
}
|
}
|
||||||
clip.Outline{
|
return clip.Outline{
|
||||||
Path: builder.End(),
|
Path: builder.End(),
|
||||||
}.Op().Add(ops)
|
}.Op()
|
||||||
return m.Stop()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func readGlyphs(r io.Reader) ([]glyph, error) {
|
func readGlyphs(r io.Reader) ([]glyph, error) {
|
||||||
|
|||||||
@@ -17,6 +17,7 @@ import (
|
|||||||
|
|
||||||
"gioui.org/internal/ops"
|
"gioui.org/internal/ops"
|
||||||
"gioui.org/op"
|
"gioui.org/op"
|
||||||
|
"gioui.org/op/clip"
|
||||||
"gioui.org/text"
|
"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
|
// All shapes from the original fonts should be distinct because the glyphs are distinct, including the replacement
|
||||||
// glyphs.
|
// glyphs.
|
||||||
distinctShapes := []op.CallOp{shapeValid1, shapeInvalid1, shapeValid2, shapeInvalid2}
|
distinctShapes := []clip.Op{shapeValid1, shapeInvalid1, shapeValid2, shapeInvalid2}
|
||||||
for i := 0; i < len(distinctShapes); i++ {
|
for i := 0; i < len(distinctShapes); i++ {
|
||||||
for j := i + 1; j < len(distinctShapes); j++ {
|
for j := i + 1; j < len(distinctShapes); j++ {
|
||||||
if areShapesEqual(distinctShapes[i], 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.
|
// 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)
|
ppem := fixed.I(200)
|
||||||
lines, err := f.Layout(ppem, 2000, strings.NewReader(string(r)))
|
lines, err := f.Layout(ppem, 2000, strings.NewReader(string(r)))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return op.CallOp{}, err
|
return clip.Op{}, err
|
||||||
}
|
}
|
||||||
if len(lines) != 1 {
|
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
|
return f.Shape(ppem, lines[0].Layout), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// areShapesEqual returns true iff both given text shapes are produced with identical operations.
|
// 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
|
var ops1, ops2 op.Ops
|
||||||
shape1.Add(&ops1)
|
shape1.Add(&ops1)
|
||||||
shape2.Add(&ops2)
|
shape2.Add(&ops2)
|
||||||
|
|||||||
+5
-6
@@ -3,9 +3,8 @@
|
|||||||
package text
|
package text
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"gioui.org/op/clip"
|
||||||
"golang.org/x/image/math/fixed"
|
"golang.org/x/image/math/fixed"
|
||||||
|
|
||||||
"gioui.org/op"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type layoutCache struct {
|
type layoutCache struct {
|
||||||
@@ -27,7 +26,7 @@ type layoutElem struct {
|
|||||||
type path struct {
|
type path struct {
|
||||||
next, prev *path
|
next, prev *path
|
||||||
key pathKey
|
key pathKey
|
||||||
val op.CallOp
|
val clip.Op
|
||||||
}
|
}
|
||||||
|
|
||||||
type layoutKey struct {
|
type layoutKey struct {
|
||||||
@@ -82,16 +81,16 @@ func (l *layoutCache) insert(lt *layoutElem) {
|
|||||||
lt.next.prev = lt
|
lt.next.prev = lt
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *pathCache) Get(k pathKey) (op.CallOp, bool) {
|
func (c *pathCache) Get(k pathKey) (clip.Op, bool) {
|
||||||
if v, ok := c.m[k]; ok {
|
if v, ok := c.m[k]; ok {
|
||||||
c.remove(v)
|
c.remove(v)
|
||||||
c.insert(v)
|
c.insert(v)
|
||||||
return v.val, true
|
return v.val, true
|
||||||
}
|
}
|
||||||
return op.CallOp{}, false
|
return clip.Op{}, false
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *pathCache) Put(k pathKey, v op.CallOp) {
|
func (c *pathCache) Put(k pathKey, v clip.Op) {
|
||||||
if c.m == nil {
|
if c.m == nil {
|
||||||
c.m = make(map[pathKey]*path)
|
c.m = make(map[pathKey]*path)
|
||||||
c.head = new(path)
|
c.head = new(path)
|
||||||
|
|||||||
+2
-2
@@ -6,7 +6,7 @@ import (
|
|||||||
"strconv"
|
"strconv"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"gioui.org/op"
|
"gioui.org/op/clip"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestLayoutLRU(t *testing.T) {
|
func TestLayoutLRU(t *testing.T) {
|
||||||
@@ -24,7 +24,7 @@ func TestLayoutLRU(t *testing.T) {
|
|||||||
func TestPathLRU(t *testing.T) {
|
func TestPathLRU(t *testing.T) {
|
||||||
c := new(pathCache)
|
c := new(pathCache)
|
||||||
put := func(i int) {
|
put := func(i int) {
|
||||||
c.Put(pathKey{str: strconv.Itoa(i)}, op.CallOp{})
|
c.Put(pathKey{str: strconv.Itoa(i)}, clip.Op{})
|
||||||
}
|
}
|
||||||
get := func(i int) bool {
|
get := func(i int) bool {
|
||||||
_, ok := c.Get(pathKey{str: strconv.Itoa(i)})
|
_, ok := c.Get(pathKey{str: strconv.Itoa(i)})
|
||||||
|
|||||||
+5
-5
@@ -8,7 +8,7 @@ import (
|
|||||||
|
|
||||||
"golang.org/x/image/math/fixed"
|
"golang.org/x/image/math/fixed"
|
||||||
|
|
||||||
"gioui.org/op"
|
"gioui.org/op/clip"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Shaper implements layout and shaping of text.
|
// Shaper implements layout and shaping of text.
|
||||||
@@ -18,7 +18,7 @@ type Shaper interface {
|
|||||||
// LayoutString is Layout for strings.
|
// LayoutString is Layout for strings.
|
||||||
LayoutString(font Font, size fixed.Int26_6, maxWidth int, str string) []Line
|
LayoutString(font Font, size fixed.Int26_6, maxWidth int, str string) []Line
|
||||||
// Shape a line of text and return a clipping operation for its outline.
|
// Shape a line of text and return a clipping operation for its outline.
|
||||||
Shape(font Font, size fixed.Int26_6, layout Layout) op.CallOp
|
Shape(font Font, size fixed.Int26_6, layout Layout) clip.Op
|
||||||
}
|
}
|
||||||
|
|
||||||
// A FontFace is a Font and a matching Face.
|
// A FontFace is a Font and a matching Face.
|
||||||
@@ -121,7 +121,7 @@ func (c *Cache) LayoutString(font Font, size fixed.Int26_6, maxWidth int, str st
|
|||||||
|
|
||||||
// Shape is a caching implementation of the Shaper interface. Shape assumes that the layout
|
// Shape is a caching implementation of the Shaper interface. Shape assumes that the layout
|
||||||
// argument is unchanged from a call to Layout or LayoutString.
|
// argument is unchanged from a call to Layout or LayoutString.
|
||||||
func (c *Cache) Shape(font Font, size fixed.Int26_6, layout Layout) op.CallOp {
|
func (c *Cache) Shape(font Font, size fixed.Int26_6, layout Layout) clip.Op {
|
||||||
cache := c.lookup(font)
|
cache := c.lookup(font)
|
||||||
return cache.shape(size, layout)
|
return cache.shape(size, layout)
|
||||||
}
|
}
|
||||||
@@ -143,9 +143,9 @@ func (f *faceCache) layout(ppem fixed.Int26_6, maxWidth int, str string) []Line
|
|||||||
return l
|
return l
|
||||||
}
|
}
|
||||||
|
|
||||||
func (f *faceCache) shape(ppem fixed.Int26_6, layout Layout) op.CallOp {
|
func (f *faceCache) shape(ppem fixed.Int26_6, layout Layout) clip.Op {
|
||||||
if f == nil {
|
if f == nil {
|
||||||
return op.CallOp{}
|
return clip.Op{}
|
||||||
}
|
}
|
||||||
pk := pathKey{
|
pk := pathKey{
|
||||||
ppem: ppem,
|
ppem: ppem,
|
||||||
|
|||||||
+2
-3
@@ -5,9 +5,8 @@ package text
|
|||||||
import (
|
import (
|
||||||
"io"
|
"io"
|
||||||
|
|
||||||
|
"gioui.org/op/clip"
|
||||||
"golang.org/x/image/math/fixed"
|
"golang.org/x/image/math/fixed"
|
||||||
|
|
||||||
"gioui.org/op"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// A Line contains the measurements of a line of text.
|
// A Line contains the measurements of a line of text.
|
||||||
@@ -49,7 +48,7 @@ type Font struct {
|
|||||||
// methods must be safe for concurrent use.
|
// methods must be safe for concurrent use.
|
||||||
type Face interface {
|
type Face interface {
|
||||||
Layout(ppem fixed.Int26_6, maxWidth int, txt io.Reader) ([]Line, error)
|
Layout(ppem fixed.Int26_6, maxWidth int, txt io.Reader) ([]Line, error)
|
||||||
Shape(ppem fixed.Int26_6, str Layout) op.CallOp
|
Shape(ppem fixed.Int26_6, str Layout) clip.Op
|
||||||
}
|
}
|
||||||
|
|
||||||
// Typeface identifies a particular typeface design. The empty
|
// Typeface identifies a particular typeface design. The empty
|
||||||
|
|||||||
+1
-1
@@ -180,7 +180,7 @@ type SelectEvent struct{}
|
|||||||
|
|
||||||
type line struct {
|
type line struct {
|
||||||
offset image.Point
|
offset image.Point
|
||||||
clip op.CallOp
|
clip clip.Op
|
||||||
selected bool
|
selected bool
|
||||||
selectionYOffs int
|
selectionYOffs int
|
||||||
selectionSize image.Point
|
selectionSize image.Point
|
||||||
|
|||||||
Reference in New Issue
Block a user