-
Notifications
You must be signed in to change notification settings - Fork 7
/
graphite.go
352 lines (296 loc) · 7.65 KB
/
graphite.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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
// Package graphite Graphite implements a "smart font" system developed
// specifically to handle the complexities of lesser-known languages of the world.
package graphite
import (
"github.com/benoitkugler/textlayout/fonts"
"github.com/benoitkugler/textlayout/fonts/truetype"
)
const debugMode = 0
type (
GID = fonts.GID
Tag = truetype.Tag
gid = uint16
)
type Position struct {
X, Y float32
}
func (p Position) add(other Position) Position {
return Position{p.X + other.X, p.Y + other.Y}
}
// returns p - other
func (p Position) sub(other Position) Position {
return Position{p.X - other.X, p.Y - other.Y}
}
func (p Position) scale(s float32) Position {
return Position{p.X * s, p.Y * s}
}
type rect struct {
bl, tr Position
}
func (r rect) width() float32 { return r.tr.X - r.bl.X }
func (r rect) height() float32 { return r.tr.Y - r.bl.Y }
func (r rect) scale(s float32) rect {
return rect{r.bl.scale(s), r.tr.scale(s)}
}
func (r rect) addPosition(pos Position) rect {
return rect{r.bl.add(pos), r.tr.add(pos)}
}
func (r rect) widen(other rect) rect {
out := r
if r.bl.X > other.bl.X {
out.bl.X = other.bl.X
}
if r.bl.Y > other.bl.Y {
out.bl.Y = other.bl.Y
}
if r.tr.X < other.tr.X {
out.tr.X = other.tr.X
}
if r.tr.Y < other.tr.Y {
out.tr.Y = other.tr.Y
}
return out
}
func min(x, y float32) float32 {
if x < y {
return x
}
return y
}
func max(x, y float32) float32 {
if x > y {
return x
}
return y
}
func abs(x float32) float32 {
if x < 0 {
return -x
}
return x
}
const iSQRT2 = 0.707106781
const (
kgmetLsb = iota
kgmetRsb
kgmetBbTop
kgmetBbBottom
kgmetBbLeft
kgmetBbRight
kgmetBbHeight
kgmetBbWidth
kgmetAdvWidth
kgmetAdvHeight
kgmetAscent
kgmetDescent
)
type glyphBoxes struct {
subBboxes []rect // same length
slantSubBboxes []rect // same length
slant rect
bitmap uint16
}
type glyph struct {
attrs attributeSet
boxes glyphBoxes
advance struct{ x, y int16 }
bbox rect
}
func (g glyph) getMetric(metric uint8) int32 {
switch metric {
case kgmetLsb:
return int32(g.bbox.bl.X)
case kgmetRsb:
return int32(g.advance.x) - int32(g.bbox.tr.X)
case kgmetBbTop:
return int32(g.bbox.tr.Y)
case kgmetBbBottom:
return int32(g.bbox.bl.Y)
case kgmetBbLeft:
return int32(g.bbox.bl.X)
case kgmetBbRight:
return int32(g.bbox.tr.X)
case kgmetBbHeight:
return int32(g.bbox.tr.Y - g.bbox.bl.Y)
case kgmetBbWidth:
return int32(g.bbox.tr.X - g.bbox.bl.X)
case kgmetAdvWidth:
return int32(g.advance.x)
case kgmetAdvHeight:
return int32(g.advance.y)
default:
return 0
}
}
// FontOptions allows to specify a scale to get position
// in user units rather than in font units.
type FontOptions struct {
scale float32 // scales from design units to ppm
// isHinted bool
}
// NewFontOptions builds options from the given points per em.
func NewFontOptions(ppem uint16, face *GraphiteFace) *FontOptions {
return &FontOptions{scale: float32(ppem) / float32(face.Upem())}
}
var _ fonts.FaceMetrics = GraphiteFace{}
// GraphiteFace contains the specific OpenType tables
// used by the Graphite engine.
// It also wraps the common OpenType metrics record.
type GraphiteFace struct {
fonts.FaceMetrics
names truetype.TableName
cmap truetype.Cmap
silf tableSilf
sill tableSill
feat tableFeat
// aggregation of metrics and attributes
glyphs []glyph
numAttributes uint16 // number of glyph attributes per glyph
ascent, descent int32
}
// LoadGraphite read graphite tables from the given OpenType font.
// It returns an error if the tables are invalid or if the font is
// not a graphite font.
func LoadGraphite(font *truetype.Font) (*GraphiteFace, error) {
var (
out GraphiteFace
err error
)
out.FaceMetrics = font
out.cmap, _ = font.Cmap()
out.names = font.Names
htmx, glyphs := font.Hmtx, font.Glyf
tables := font.Graphite
out.sill, err = parseTableSill(tables.Sill)
if err != nil {
return nil, err
}
out.feat, err = parseTableFeat(tables.Feat)
if err != nil {
return nil, err
}
locations, numAttributes, err := parseTableGloc(tables.Gloc, font.NumGlyphs)
if err != nil {
return nil, err
}
out.numAttributes = numAttributes
attrs, err := parseTableGlat(tables.Glat, locations)
if err != nil {
return nil, err
}
out.silf, err = parseTableSilf(tables.Silf, numAttributes, uint16(len(out.feat)))
if err != nil {
return nil, err
}
out.preprocessGlyphsAttributes(glyphs, htmx, attrs)
return &out, nil
}
// process the 'glyf', 'htmx' and 'glat' tables to extract relevant info.
func (f *GraphiteFace) preprocessGlyphsAttributes(glyphs truetype.TableGlyf, htmx truetype.TableHVmtx,
attrs tableGlat,
) {
// take into account pseudo glyphs (len(glyphs) <= len(attrs))
L := len(glyphs)
f.glyphs = make([]glyph, len(attrs))
for gid, attr := range attrs {
dst := &f.glyphs[gid]
if gid < L {
dst.advance.x = htmx[gid].Advance
data := glyphs[gid]
dst.bbox = rect{
bl: Position{float32(data.Xmin), float32(data.Ymin)},
tr: Position{float32(data.Xmax), float32(data.Ymax)},
}
}
dst.attrs = attr.attributes
if attr.octaboxMetrics != nil {
dst.boxes = attr.octaboxMetrics.computeBoxes(dst.bbox)
}
}
}
// FeaturesForLang selects the features and values for the given language, or
// the default ones if the language is not found.
func (f *GraphiteFace) FeaturesForLang(lang Tag) FeaturesValue {
return f.sill.getFeatures(lang, f.feat)
}
// getGlyph return nil for invalid gid
func (f *GraphiteFace) getGlyph(gid GID) *glyph {
if int(gid) < len(f.glyphs) {
return &f.glyphs[gid]
}
return nil
}
func (f *GraphiteFace) getGlyphAttr(gid GID, attr uint16) int16 {
if glyph := f.getGlyph(gid); glyph != nil {
return glyph.attrs.get(attr)
}
return 0
}
func (f *GraphiteFace) getGlyphMetric(gid GID, metric uint8) int32 {
switch metric {
case kgmetAscent:
return f.ascent
case kgmetDescent:
return f.descent
}
if glyph := f.getGlyph(gid); glyph != nil {
return glyph.getMetric(metric)
}
return 0
}
func (f *GraphiteFace) runGraphite(seg *Segment, silf *passes) {
if seg.dir&3 == 3 && silf.indexBidiPass == 0xFF {
seg.doMirror(silf.attrMirroring)
}
res := silf.runGraphite(seg, 0, silf.indexPosPass, true)
if res {
seg.associateChars(0, len(seg.charinfo))
if silf.hasCollision {
ok := seg.initCollisions()
res = ok
}
if res {
res = silf.runGraphite(seg, silf.indexPosPass, uint8(len(silf.passes)), false)
}
}
if debugMode >= 2 {
seg.positionSlots(nil, nil, nil, seg.currdir(), true)
tr.finaliseOutput(seg)
}
}
// Shape process the given `text` and applies the graphite tables
// found in the font, returning a shaped segment of text.
// `font` is optional: if given, the positions are scaled; otherwise they are
// expressed in font units.
// If `features` is nil, the default features from the `Sill` table are used.
// Note that this not the same as passing an empty slice, which would desactivate any feature.
// `script` is optional and may help to select the correct `Silf` subtable.
// `dir` sets the direction of the text.
func (face *GraphiteFace) Shape(font *FontOptions, text []rune, script Tag, features FeaturesValue, dir int8) *Segment {
var seg Segment
seg.face = face
// allocate memory
seg.charinfo = make([]charInfo, len(text))
// choose silf: for now script is unused
// script = spaceToZero(script) // adapt convention
if len(face.silf) != 0 {
seg.silf = &face.silf[0]
} else {
seg.silf = &passes{}
}
seg.dir = dir
if seg.silf.hasCollision {
seg.flags = 1 << 1
}
if seg.silf.attrSkipPasses != 0 {
seg.passBits = ^uint32(0)
}
if features == nil {
features = face.FeaturesForLang(0)
}
seg.feats = features
seg.processRunes(text)
face.runGraphite(&seg, seg.silf)
seg.finalise(font, true)
return &seg
}