From 9576b659d785bdc360ec426534c90fdf1ba14869 Mon Sep 17 00:00:00 2001 From: Chris Waldon Date: Wed, 16 Mar 2022 16:01:40 -0400 Subject: [PATCH] text: [API] remove Text and Advances from Layout These fields are no longer needed with the new text shaper. Advances is redundant to the glyph information, and Text should never be used during layout, as you should traverse the cluster list instead. This commit also removed the now-unused string field from the path LRU cache key. References: https://todo.sr.ht/~eliasnaur/gio/146 Signed-off-by: Chris Waldon --- font/opentype/internal/shaping.go | 1 - text/lru.go | 1 - text/lru_test.go | 4 ++-- text/shaper.go | 1 - text/text.go | 2 -- widget/editor.go | 2 -- widget/editor_test.go | 10 ++++++++-- 7 files changed, 10 insertions(+), 11 deletions(-) diff --git a/font/opentype/internal/shaping.go b/font/opentype/internal/shaping.go index 75028ac2..4880db8f 100644 --- a/font/opentype/internal/shaping.go +++ b/font/opentype/internal/shaping.go @@ -401,7 +401,6 @@ func (o output) ToLine() text.Line { advances = append(advances, glyph.XAdvance) } layout := text.Layout{ - Advances: advances, Glyphs: toGioGlyphs(o.Shaped.Glyphs), Runes: o.RuneRange, Direction: toSystemDirection(o.Shaped.Direction), diff --git a/text/lru.go b/text/lru.go index 55b8ad4b..c678bfbb 100644 --- a/text/lru.go +++ b/text/lru.go @@ -40,7 +40,6 @@ type layoutKey struct { type pathKey struct { ppem fixed.Int26_6 - str string gidHash uint64 } diff --git a/text/lru_test.go b/text/lru_test.go index bbce9d73..0c5cb528 100644 --- a/text/lru_test.go +++ b/text/lru_test.go @@ -24,10 +24,10 @@ func TestLayoutLRU(t *testing.T) { func TestPathLRU(t *testing.T) { c := new(pathCache) put := func(i int) { - c.Put(pathKey{str: strconv.Itoa(i), gidHash: uint64(i)}, Layout{Runes: Range{Count: i}}, clip.PathSpec{}) + c.Put(pathKey{gidHash: uint64(i)}, Layout{Runes: Range{Count: i}}, clip.PathSpec{}) } get := func(i int) bool { - _, ok := c.Get(pathKey{str: strconv.Itoa(i), gidHash: uint64(i)}, Layout{Runes: Range{Count: i}}) + _, ok := c.Get(pathKey{gidHash: uint64(i)}, Layout{Runes: Range{Count: i}}) return ok } testLRU(t, put, get) diff --git a/text/shaper.go b/text/shaper.go index f71ac0c0..338d215e 100644 --- a/text/shaper.go +++ b/text/shaper.go @@ -168,7 +168,6 @@ func (f *faceCache) shape(ppem fixed.Int26_6, layout Layout) clip.PathSpec { } pk := pathKey{ ppem: ppem, - str: layout.Text, gidHash: f.hashGIDs(layout), } if clip, ok := f.pathCache.Get(pk, layout); ok { diff --git a/text/text.go b/text/text.go index fa61a36e..20ab0476 100644 --- a/text/text.go +++ b/text/text.go @@ -92,8 +92,6 @@ type Layout struct { // so the first cluster will describe the beginning of the text and may // refer to the final glyphs in the Glyphs field if the text is RTL. Clusters []GlyphCluster - Text string - Advances []fixed.Int26_6 // Runes describes the position of the text data this layout represents // within the overall body of text being shaped. Runes Range diff --git a/widget/editor.go b/widget/editor.go index 79d94a88..cc77fc7a 100644 --- a/widget/editor.go +++ b/widget/editor.go @@ -1476,8 +1476,6 @@ func nullLayout(rr io.RuneReader) ([]text.Line, error) { Runes: text.Range{ Count: n, }, - Text: buf.String(), - Advances: make([]fixed.Int26_6, n), }, }, }, rerr diff --git a/widget/editor_test.go b/widget/editor_test.go index 018c33fb..9f49c8b0 100644 --- a/widget/editor_test.go +++ b/widget/editor_test.go @@ -966,8 +966,14 @@ func (q *testQueue) Events(_ event.Tag) []event.Event { } func printLines(e *Editor) { - for n, line := range e.lines { - text := strings.TrimSuffix(line.Layout.Text, "\n") + for _, line := range e.lines { + start := e.runeOffset(line.Layout.Runes.Offset) + buf := make([]byte, 0, 4*line.Layout.Runes.Count) + e.Seek(int64(start), 0) + n, _ := e.Read(buf) + buf = buf[:n] + asStr := string([]rune(string(buf))[:line.Layout.Runes.Count]) + text := strings.TrimSuffix(asStr, "\n") fmt.Printf("%d: %s\n", n, text) } }