text: represent laid out text as strings to facilitate caching of layouts

Commit https://gioui.org/commit/b331407e81456 added text layout and shaping
based on io.Reader and changed Editor to use it. Unfortunately, as ~inkeliz
discovered, caching of shapes were also lost.

~inkeliz suggested fix,

https://lists.sr.ht/~eliasnaur/gio-patches/patches/15059

adds caching of shapes to Editor to regain lost performance.

This change repairs the cache to work on io.Reader API, in hope that the
already complicated Editor won't need additional caching.

Before this change, text layouts were represented as a slice of (rune, advance)
pairs. Unfortunately, this representation doesn't lend itself to caching of
shaping results, so change the representation of a line of text to be a pair
of text and advances:

	package text

	type Layout {
		Text string
		Advances []fixed.Int26_6
	}

The Text field can then be used in a cache key, assuming Advances is
consistent with it.

The end result is that the two shaper variants of text.Shaper is reduced to
just one, and the Len field field of text.Line is no longer needed.

The changed representation adds a bit of extra work to package opentype.
Cleaning that up is left as a future TODO.

Signed-off-by: Elias Naur <mail@eliasnaur.com>
This commit is contained in:
Elias Naur
2020-11-16 15:45:32 +01:00
parent 67594636e7
commit aee87baefe
6 changed files with 95 additions and 75 deletions
+5 -7
View File
@@ -11,9 +11,7 @@ import (
// A Line contains the measurements of a line of text.
type Line struct {
Layout []Glyph
// Len is the length in UTF8 bytes of the line.
Len int
Layout Layout
// Width is the width of the line.
Width fixed.Int26_6
// Ascent is the height above the baseline.
@@ -25,9 +23,9 @@ type Line struct {
Bounds fixed.Rectangle26_6
}
type Glyph struct {
Rune rune
Advance fixed.Int26_6
type Layout struct {
Text string
Advances []fixed.Int26_6
}
// Style is the font style.
@@ -50,7 +48,7 @@ type Font struct {
// methods must be safe for concurrent use.
type Face interface {
Layout(ppem fixed.Int26_6, maxWidth int, txt io.Reader) ([]Line, error)
Shape(ppem fixed.Int26_6, str []Glyph) op.CallOp
Shape(ppem fixed.Int26_6, str Layout) op.CallOp
}
// Typeface identifies a particular typeface design. The empty