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
+11 -15
View File
@@ -14,13 +14,10 @@ import (
type Shaper interface {
// Layout a text according to a set of options.
Layout(font Font, size fixed.Int26_6, maxWidth int, txt io.Reader) ([]Line, error)
// Shape a line of text and return a clipping operation for its outline.
Shape(font Font, size fixed.Int26_6, layout []Glyph) op.CallOp
// LayoutString is like Layout, but for strings.
// LayoutString is Layout for strings.
LayoutString(font Font, size fixed.Int26_6, maxWidth int, str string) []Line
// ShapeString is like Shape for lines previously laid out by LayoutString.
ShapeString(font Font, size fixed.Int26_6, str string, layout []Glyph) op.CallOp
// Shape a line of text and return a clipping operation for its outline.
Shape(font Font, size fixed.Int26_6, layout Layout) op.CallOp
}
// A FontFace is a Font and a matching Face.
@@ -91,24 +88,23 @@ func NewCache(collection []FontFace) *Cache {
return c
}
// Layout implements the Shaper interface.
func (s *Cache) Layout(font Font, size fixed.Int26_6, maxWidth int, txt io.Reader) ([]Line, error) {
cache := s.lookup(font)
return cache.face.Layout(size, maxWidth, txt)
}
func (s *Cache) Shape(font Font, size fixed.Int26_6, layout []Glyph) op.CallOp {
cache := s.lookup(font)
return cache.face.Shape(size, layout)
}
// LayoutString is a caching implementation of the Shaper interface.
func (s *Cache) LayoutString(font Font, size fixed.Int26_6, maxWidth int, str string) []Line {
cache := s.lookup(font)
return cache.layout(size, maxWidth, str)
}
func (s *Cache) ShapeString(font Font, size fixed.Int26_6, str string, layout []Glyph) op.CallOp {
// Shape is a caching implementation of the Shaper interface. Shape assumes that the layout
// argument is unchanged from a call to Layout or LayoutString.
func (s *Cache) Shape(font Font, size fixed.Int26_6, layout Layout) op.CallOp {
cache := s.lookup(font)
return cache.shape(size, str, layout)
return cache.shape(size, layout)
}
func (f *faceCache) layout(ppem fixed.Int26_6, maxWidth int, str string) []Line {
@@ -128,13 +124,13 @@ func (f *faceCache) layout(ppem fixed.Int26_6, maxWidth int, str string) []Line
return l
}
func (f *faceCache) shape(ppem fixed.Int26_6, str string, layout []Glyph) op.CallOp {
func (f *faceCache) shape(ppem fixed.Int26_6, layout Layout) op.CallOp {
if f == nil {
return op.CallOp{}
}
pk := pathKey{
ppem: ppem,
str: str,
str: layout.Text,
}
if clip, ok := f.pathCache.Get(pk); ok {
return clip
+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