text: remove String, Layout and add Glyph

In preparation for using Shaper with an io.Reader, rework the API to not refer
to strings. In particular, introduce Glyph for holding the rune in addition to
the advance. For fast traversing of the underlying text, add Len to Line with
the UTF8 length.

Layout is a useless wrapper around []Line; remove it while we're
here.

Signed-off-by: Elias Naur <mail@eliasnaur.com>
This commit is contained in:
Elias Naur
2020-01-13 19:54:11 +01:00
parent e25b1639b9
commit 16d2a3ac0a
6 changed files with 95 additions and 116 deletions
+3 -3
View File
@@ -20,7 +20,7 @@ type pathCache struct {
type layoutElem struct {
next, prev *layoutElem
key layoutKey
layout *Layout
layout []Line
}
type path struct {
@@ -42,7 +42,7 @@ type pathKey struct {
const maxSize = 1000
func (l *layoutCache) Get(k layoutKey) (*Layout, bool) {
func (l *layoutCache) Get(k layoutKey) ([]Line, bool) {
if lt, ok := l.m[k]; ok {
l.remove(lt)
l.insert(lt)
@@ -51,7 +51,7 @@ func (l *layoutCache) Get(k layoutKey) (*Layout, bool) {
return nil, false
}
func (l *layoutCache) Put(k layoutKey, lt *Layout) {
func (l *layoutCache) Put(k layoutKey, lt []Line) {
if l.m == nil {
l.m = make(map[layoutKey]*layoutElem)
l.head = new(layoutElem)
+12 -25
View File
@@ -3,8 +3,6 @@
package text
import (
"unicode/utf8"
"golang.org/x/image/font"
"gioui.org/op"
@@ -14,8 +12,10 @@ import (
// Shaper implements layout and shaping of text.
type Shaper interface {
Layout(c unit.Converter, font Font, str string, opts LayoutOptions) *Layout
Shape(c unit.Converter, font Font, str String) op.CallOp
// Layout a text according to a set of options.
Layout(c unit.Converter, font Font, str string, opts LayoutOptions) []Line
// Shape a line of text previously laid out by Layout.
Shape(c unit.Converter, font Font, str string, layout []Glyph) op.CallOp
Metrics(c unit.Converter, font Font) font.Metrics
}
@@ -50,14 +50,14 @@ func (s *FontRegistry) Register(font Font, tf Face) {
}
}
func (s *FontRegistry) Layout(c unit.Converter, font Font, str string, opts LayoutOptions) *Layout {
func (s *FontRegistry) Layout(c unit.Converter, font Font, str string, opts LayoutOptions) []Line {
tf := s.faceForFont(font)
return tf.layout(fixed.I(c.Px(font.Size)), str, opts)
}
func (s *FontRegistry) Shape(c unit.Converter, font Font, str String) op.CallOp {
func (s *FontRegistry) Shape(c unit.Converter, font Font, str string, layout []Glyph) op.CallOp {
tf := s.faceForFont(font)
return tf.shape(fixed.I(c.Px(font.Size)), str)
return tf.shape(fixed.I(c.Px(font.Size)), str, layout)
}
func (s *FontRegistry) Metrics(c unit.Converter, font Font) font.Metrics {
@@ -96,9 +96,9 @@ func (s *FontRegistry) faceForFont(font Font) *face {
return tf
}
func (t *face) layout(ppem fixed.Int26_6, str string, opts LayoutOptions) *Layout {
func (t *face) layout(ppem fixed.Int26_6, str string, opts LayoutOptions) []Line {
if t == nil {
return fallbackLayout(str)
return nil
}
lk := layoutKey{
ppem: ppem,
@@ -113,18 +113,18 @@ func (t *face) layout(ppem fixed.Int26_6, str string, opts LayoutOptions) *Layou
return l
}
func (t *face) shape(ppem fixed.Int26_6, str String) op.CallOp {
func (t *face) shape(ppem fixed.Int26_6, str string, layout []Glyph) op.CallOp {
if t == nil {
return op.CallOp{}
}
pk := pathKey{
ppem: ppem,
str: str.String,
str: str,
}
if clip, ok := t.pathCache.Get(pk); ok {
return clip
}
clip := t.face.Shape(ppem, str)
clip := t.face.Shape(ppem, layout)
t.pathCache.Put(pk, clip)
return clip
}
@@ -132,16 +132,3 @@ func (t *face) shape(ppem fixed.Int26_6, str String) op.CallOp {
func (t *face) metrics(ppem fixed.Int26_6) font.Metrics {
return t.face.Metrics(ppem)
}
func fallbackLayout(str string) *Layout {
l := &Layout{
Lines: []Line{
{Text: String{
String: str,
}},
},
}
strlen := utf8.RuneCountInString(str)
l.Lines[0].Text.Advances = make([]fixed.Int26_6, strlen)
return l
}
+8 -13
View File
@@ -11,7 +11,9 @@ import (
// A Line contains the measurements of a line of text.
type Line struct {
Text String
Layout []Glyph
// Len is the length in UTF8 bytes of the line.
Len int
// Width is the width of the line.
Width fixed.Int26_6
// Ascent is the height above the baseline.
@@ -23,16 +25,9 @@ type Line struct {
Bounds fixed.Rectangle26_6
}
type String struct {
String string
// Advances contain the advance of each rune in String.
Advances []fixed.Int26_6
}
// A Layout contains the measurements of a body of text as
// a list of Lines.
type Layout struct {
Lines []Line
type Glyph struct {
Rune rune
Advance fixed.Int26_6
}
// LayoutOptions specify the constraints of a text layout.
@@ -59,8 +54,8 @@ type Font struct {
// Face implements text layout and shaping for a particular font.
type Face interface {
Layout(ppem fixed.Int26_6, str string, opts LayoutOptions) *Layout
Shape(ppem fixed.Int26_6, str String) op.CallOp
Layout(ppem fixed.Int26_6, str string, opts LayoutOptions) []Line
Shape(ppem fixed.Int26_6, str []Glyph) op.CallOp
Metrics(ppem fixed.Int26_6) font.Metrics
}