-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathshape.go
189 lines (158 loc) · 5.51 KB
/
shape.go
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
package harfbuzz
import (
"fmt"
"sync"
)
// ported from harfbuzz/src/hb-shape.cc, harfbuzz/src/hb-shape-plan.cc Copyright © 2009, 2012 Behdad Esfahbod
/**
* Shaping is the central operation of HarfBuzz. Shaping operates on buffers,
* which are sequences of Unicode characters that use the same font and have
* the same text direction, script, and language. After shaping the buffer
* contains the output glyphs and their positions.
**/
// Shape shapes the buffer using `font`, turning its Unicode characters content to
// positioned glyphs. If `features` is not empty, it will be used to control the
// features applied during shaping. If two features have the same tag but
// overlapping ranges the value of the feature with the higher index takes
// precedence.
//
// The shapping plan depends on the font capabilities. See `NewFont` and `Face` and
// its extension interfaces for more details.
//
// It also depends on the properties of the segment of text : the `Props`
// field of the buffer must be set before calling `Shape`.
func (b *Buffer) Shape(font *Font, features []Feature) {
shapePlan := newShapePlanCached(font, b.Props, features, font.varCoords())
shapePlan.execute(font, b, features)
}
type shaperKind uint8
const (
skFallback shaperKind = iota
skOpentype
skGraphite
)
// shaper shapes a string of runes.
// Depending on the font used, different shapers will be choosen.
type shaper interface {
kind() shaperKind
// used to defer costly setup : a shaper object
// is always created to be used as key for caching,
// but this method is only called for new shaper
compile(props SegmentProperties, userFeatures []Feature)
shape(*Font, *Buffer, []Feature)
}
// Shape plans are an internal mechanism. Each plan contains state
// describing how HarfBuzz will shape a particular text segment, based on
// the combination of segment properties and the capabilities in the
// font face in use.
//
// Shape plans are not used for shaping directly, but can be queried to
// access certain information about how shaping will perform, given a set
// of specific input parameters (script, language, direction, features,
// etc.).
//
// Most client programs will not need to deal with shape plans directly.
type shapePlan struct {
shaper shaper
props SegmentProperties
userFeatures []Feature
}
func (plan *shapePlan) init(copy bool, font *Font, props SegmentProperties,
userFeatures []Feature, coords []float32) {
plan.props = props
if !copy {
plan.userFeatures = userFeatures
} else {
plan.userFeatures = append([]Feature(nil), userFeatures...)
/* Make start/end uniform to easier catch bugs. */
for i := range plan.userFeatures {
if plan.userFeatures[i].Start != FeatureGlobalStart {
plan.userFeatures[i].Start = 1
}
if plan.userFeatures[i].End != FeatureGlobalEnd {
plan.userFeatures[i].End = 2
}
}
}
// Choose shaper.
if font.gr != nil {
plan.shaper = (*shaperGraphite)(font.gr)
} else if font.otTables != nil {
plan.shaper = newShaperOpentype(font.otTables, coords)
} else {
plan.shaper = shaperFallback{}
}
}
func (plan shapePlan) userFeaturesMatch(other shapePlan) bool {
if len(plan.userFeatures) != len(other.userFeatures) {
return false
}
for i, feat := range plan.userFeatures {
if feat.Tag != other.userFeatures[i].Tag || feat.Value != other.userFeatures[i].Value ||
(feat.Start == FeatureGlobalStart && feat.End == FeatureGlobalEnd) !=
(other.userFeatures[i].Start == FeatureGlobalStart && other.userFeatures[i].End == FeatureGlobalEnd) {
return false
}
}
return true
}
func (plan shapePlan) equal(other shapePlan) bool {
return plan.props == other.props &&
plan.userFeaturesMatch(other) && plan.shaper.kind() == other.shaper.kind()
}
// Constructs a shaping plan for a combination of @face, @userFeatures, @props,
// plus the variation-space coordinates @coords.
// See newShapePlanCached for caching support.
func newShapePlan(font *Font, props SegmentProperties,
userFeatures []Feature, coords []float32) *shapePlan {
if debugMode >= 1 {
fmt.Printf("NEW SHAPE PLAN: face:%p features:%v coords:%v\n", &font.face, userFeatures, coords)
}
var sp shapePlan
sp.init(true, font, props, userFeatures, coords)
if debugMode >= 1 {
fmt.Println("NEW SHAPE PLAN - compiling shaper plan")
}
sp.shaper.compile(props, userFeatures)
return &sp
}
// Executes the given shaping plan on the specified `buffer`, using
// the given `font` and `features`.
func (sp *shapePlan) execute(font *Font, buffer *Buffer, features []Feature) {
if debugMode >= 1 {
fmt.Printf("EXECUTE shape plan %p features:%v shaper:%T\n", sp, features, sp.shaper)
}
sp.shaper.shape(font, buffer, features)
}
/*
* Caching
*/
var (
planCache = map[Face][]*shapePlan{}
planCacheLock sync.Mutex
)
// creates (or returns) a cached shaping plan suitable for reuse, for a combination
// of `face`, `userFeatures`, `props`, plus the variation-space coordinates `coords`.
func newShapePlanCached(font *Font, props SegmentProperties,
userFeatures []Feature, coords []float32) *shapePlan {
var key shapePlan
key.init(false, font, props, userFeatures, coords)
planCacheLock.Lock()
defer planCacheLock.Unlock()
plans := planCache[font.face]
for _, plan := range plans {
if plan.equal(key) {
if debugMode >= 1 {
fmt.Printf("\tPLAN %p fulfilled from cache\n", plan)
}
return plan
}
}
plan := newShapePlan(font, props, userFeatures, coords)
plans = append(plans, plan)
planCache[font.face] = plans
if debugMode >= 1 {
fmt.Printf("\tPLAN %p inserted into cache\n", plan)
}
return plan
}