gio/text/shaper.go -rw-r--r-- 2.4 KiB View raw
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
// SPDX-License-Identifier: Unlicense OR MIT

package text

import (
	"unicode/utf8"

	"gioui.org/op/clip"
	"gioui.org/unit"
	"golang.org/x/image/math/fixed"
)

// Shaper implements layout and shaping of text and a cache of
// computed results.
//
// Specify the default and fallback font by calling Register with the
// empty Font.
type Shaper struct {
	def   Typeface
	faces map[Font]*face
}

type face struct {
	face        Face
	layoutCache layoutCache
	pathCache   pathCache
}

func (s *Shaper) Register(font Font, tf Face) {
	if s.faces == nil {
		s.def = font.Typeface
		s.faces = make(map[Font]*face)
	}
	// Treat all font sizes equally.
	font.Size = unit.Value{}
	if font.Weight == 0 {
		font.Weight = Normal
	}
	s.faces[font] = &face{
		face: tf,
	}
}

func (s *Shaper) Layout(c unit.Converter, font Font, str string, opts LayoutOptions) *Layout {
	tf := s.faceForFont(font)
	return tf.layout(fixed.I(c.Px(font.Size)), str, opts)
}

func (s *Shaper) Shape(c unit.Converter, font Font, str String) clip.Op {
	tf := s.faceForFont(font)
	return tf.shape(fixed.I(c.Px(font.Size)), str)
}

func (s *Shaper) faceForStyle(font Font) *face {
	tf := s.faces[font]
	if tf == nil {
		font := font
		font.Weight = Normal
		tf = s.faces[font]
	}
	if tf == nil {
		font := font
		font.Style = Regular
		tf = s.faces[font]
	}
	if tf == nil {
		font := font
		font.Style = Regular
		font.Weight = Normal
		tf = s.faces[font]
	}
	return tf
}

func (s *Shaper) faceForFont(font Font) *face {
	font.Size = unit.Value{}
	tf := s.faceForStyle(font)
	if tf == nil {
		font.Typeface = s.def
		tf = s.faceForStyle(font)
	}
	return tf
}

func (t *face) layout(ppem fixed.Int26_6, str string, opts LayoutOptions) *Layout {
	if t == nil {
		return fallbackLayout(str)
	}
	lk := layoutKey{
		ppem: ppem,
		str:  str,
		opts: opts,
	}
	if l, ok := t.layoutCache.Get(lk); ok {
		return l
	}
	l := t.face.Layout(ppem, str, opts)
	t.layoutCache.Put(lk, l)
	return l
}

func (t *face) shape(ppem fixed.Int26_6, str String) clip.Op {
	if t == nil {
		return clip.Op{}
	}
	pk := pathKey{
		ppem: ppem,
		str:  str.String,
	}
	if clip, ok := t.pathCache.Get(pk); ok {
		return clip
	}
	clip := t.face.Shape(ppem, str)
	t.pathCache.Put(pk, clip)
	return clip
}

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
}