-
Notifications
You must be signed in to change notification settings - Fork 7
/
segment.go
408 lines (355 loc) · 9.05 KB
/
segment.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
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
package graphite
const maxSegGrowthFactor = 64
type charInfo struct {
before int // slot index before us, comes before
after int // slot index after us, comes after
base int // index into input string corresponding to this charinfo
// featureIndex int // index into features list in the segment −> Always 0
char rune // Unicode character from character stream
breakWeight int16 // breakweight coming from lb table
flags uint8 // 0,1 segment split.
}
func (ch *charInfo) addFlags(val uint8) { ch.flags |= val }
// Segment represents a line of text.
// It is used internally during shaping and
// returned as the result of the operation.
type Segment struct {
// Start of the segment (may be nil for empty segments)
First *Slot
last *Slot // last slot in the segment
face *GraphiteFace
silf *passes // selected subtable
feats FeaturesValue // applied values
// character info, one per input character
charinfo []charInfo
freeSlots *Slot // linked list of free slots
collisions []slotCollision
// Advance of the whole segment
Advance Position
// Number of slots (output characters).
// Since slots may be added or deleted during shaping,
// it may differ from the number of characters ot the text input.
// It could be directly computed by walking the linked list but is cached
// for performance reasons.
NumGlyphs int
passBits uint32 // if bit set then skip pass
flags uint8 // General purpose flags
dir int8 // text direction
}
func (seg *Segment) currdir() bool { return ((seg.dir>>reverseBit)^seg.dir)&1 != 0 }
const (
initCollisions = 1 + iota
hasCollisions
)
func (seg *Segment) hasCollisionInfo() bool {
return (seg.flags&hasCollisions) != 0 && seg.collisions != nil
}
func (seg *Segment) mergePassBits(val uint32) { seg.passBits &= val }
func (seg *Segment) processRunes(text []rune) {
for slotID, r := range text {
gid, _ := seg.face.cmap.Lookup(r)
if gid == 0 {
gid = seg.silf.findPdseudoGlyph(r)
}
seg.appendSlot(slotID, r, gid)
}
// the initial segment has one slot per input character
seg.NumGlyphs = len(text)
}
func (seg *Segment) newSlot() *Slot {
sl := new(Slot)
sl.userAttrs = make([]int16, seg.silf.userAttibutes)
return sl
}
func (seg *Segment) newJustify() *slotJustify {
return new(slotJustify)
}
func (seg *Segment) appendSlot(index int, cid rune, gid GID) {
sl := seg.newSlot()
info := &seg.charinfo[index]
info.char = cid
// info.featureIndex = featureID
info.base = index
glyph := seg.face.getGlyph(gid)
if glyph != nil {
info.breakWeight = glyph.attrs.get(uint16(seg.silf.attrBreakWeight))
}
sl.setGlyph(seg, gid)
sl.original, sl.Before, sl.After = index, index, index
if seg.last != nil {
seg.last.Next = sl
}
sl.prev = seg.last
seg.last = sl
if seg.First == nil {
seg.First = sl
}
if aPassBits := uint16(seg.silf.attrSkipPasses); glyph != nil && aPassBits != 0 {
m := uint32(glyph.attrs.get(aPassBits))
if len(seg.silf.passes) > 16 {
m |= uint32(glyph.attrs.get(aPassBits+1)) << 16
}
seg.mergePassBits(m)
}
}
func (seg *Segment) freeSlot(aSlot *Slot) {
if aSlot == nil {
return
}
if seg.last == aSlot {
seg.last = aSlot.prev
}
if seg.First == aSlot {
seg.First = aSlot.Next
}
if aSlot.parent != nil {
aSlot.parent.removeChild(aSlot)
}
for aSlot.child != nil {
if aSlot.child.parent == aSlot {
aSlot.child.parent = nil
aSlot.removeChild(aSlot.child)
} else {
aSlot.child = nil
}
}
// update next pointer
aSlot.Next = seg.freeSlots
seg.freeSlots = aSlot
}
const reverseBit = 6
// reverse the slots but keep diacritics in their same position after their bases
func (seg *Segment) reverseSlots() {
seg.dir = seg.dir ^ 1<<reverseBit // invert the reverse flag
if seg.First == seg.last {
return
} // skip 0 or 1 glyph runs
var (
curr = seg.First
t, tlast, tfirst, out *Slot
)
for curr != nil && seg.getSlotBidiClass(curr) == 16 {
curr = curr.Next
}
if curr == nil {
return
}
tfirst = curr.prev
tlast = curr
for curr != nil {
if seg.getSlotBidiClass(curr) == 16 {
d := curr.Next
for d != nil && seg.getSlotBidiClass(d) == 16 {
d = d.Next
}
if d != nil {
d = d.prev
} else {
d = seg.last
}
p := out.Next // one after the diacritics. out can't be null
if p != nil {
p.prev = d
} else {
tlast = d
}
t = d.Next
d.Next = p
curr.prev = out
out.Next = curr
} else { // will always fire first time round the loop
if out != nil {
out.prev = curr
}
t = curr.Next
curr.Next = out
out = curr
}
curr = t
}
out.prev = tfirst
if tfirst != nil {
tfirst.Next = out
} else {
seg.First = out
}
seg.last = tlast
}
func (seg *Segment) positionSlots(font *FontOptions, iStart, iEnd *Slot, isRTL, isFinal bool) Position {
var (
currpos Position
clusterMin float32
bbox rect
reorder = (seg.currdir() != isRTL)
)
if reorder {
seg.reverseSlots()
iStart, iEnd = iEnd, iStart
}
if iStart == nil {
iStart = seg.First
}
if iEnd == nil {
iEnd = seg.last
}
if iStart == nil || iEnd == nil { // only true for empty segments
return currpos
}
if isRTL {
for s, end := iEnd, iStart.prev; s != nil && s != end; s = s.prev {
if s.isBase() {
clusterMin = currpos.X
currpos = s.finalise(seg, font, currpos, &bbox, 0, &clusterMin, isRTL, isFinal, 0)
}
}
} else {
for s, end := iStart, iEnd.Next; s != nil && s != end; s = s.Next {
if s.isBase() {
clusterMin = currpos.X
currpos = s.finalise(seg, font, currpos, &bbox, 0, &clusterMin, isRTL, isFinal, 0)
}
}
}
if reorder {
seg.reverseSlots()
}
return currpos
}
func (seg *Segment) doMirror(aMirror byte) {
for s := seg.First; s != nil; s = s.Next {
g := GID(seg.face.getGlyphAttr(s.glyphID, uint16(aMirror)))
if g != 0 && (seg.dir&4 == 0 || seg.face.getGlyphAttr(s.glyphID, uint16(aMirror)+1) == 0) {
s.setGlyph(seg, g)
}
}
}
func (seg *Segment) getSlotBidiClass(s *Slot) int8 {
if res := s.bidiCls; res != -1 {
return res
}
res := int8(seg.face.getGlyphAttr(s.glyphID, uint16(seg.silf.attrDirectionality)))
s.bidiCls = res
return res
}
// check the bounds and return nil if needed
func (seg *Segment) getCharInfo(index int) *charInfo {
if index < len(seg.charinfo) {
return &seg.charinfo[index]
}
return nil
}
// check the bounds and return nil if needed
func (seg *Segment) getCollisionInfo(s *Slot) *slotCollision {
if s.index < len(seg.collisions) {
return &seg.collisions[s.index]
}
return nil
}
func (seg *Segment) getFeature(findex uint8) int32 {
// findex reference the font feat table
if int(findex) < len(seg.face.feat) {
featRef := seg.face.feat[findex].id
if featVal := seg.feats.FindFeature(featRef); featVal != nil {
return int32(featVal.Value)
}
}
return 0
}
func (seg *Segment) setFeature(findex uint8, val int16) {
// findex reference the font feat table
if int(findex) < len(seg.face.feat) {
featRef := seg.face.feat[findex].id
if featVal := seg.feats.FindFeature(featRef); featVal != nil {
featVal.Value = val
}
}
}
func (seg *Segment) getGlyphMetric(iSlot *Slot, metric, attrLevel uint8, rtl bool) int32 {
if attrLevel > 0 {
is := iSlot.findRoot()
return is.clusterMetric(seg, metric, attrLevel, rtl)
}
return seg.face.getGlyphMetric(iSlot.glyphID, metric)
}
func (seg *Segment) finalise(font *FontOptions, reverse bool) {
if seg.First == nil || seg.last == nil {
return
}
seg.Advance = seg.positionSlots(font, seg.First, seg.last, seg.silf.isRTL, true)
// associateChars(0, seg.numCharinfo);
if reverse && seg.currdir() != (seg.dir&1 != 0) {
seg.reverseSlots()
}
seg.linkClusters(seg.First, seg.last)
}
func (seg *Segment) linkClusters(s, end *Slot) {
end = end.Next
for ; s != end && !s.isBase(); s = s.Next {
}
ls := s
if seg.dir&1 != 0 {
for ; s != end; s = s.Next {
if !s.isBase() {
continue
}
s.sibling = ls
ls = s
}
} else {
for ; s != end; s = s.Next {
if !s.isBase() {
continue
}
ls.sibling = s
ls = s
}
}
}
func (seg *Segment) associateChars(offset, numChars int) {
subSlice := seg.charinfo[offset : offset+numChars]
for i := range subSlice {
subSlice[i].before = -1
subSlice[i].after = -1
}
for s, i := seg.First, 0; s != nil; s, i = s.Next, i+1 {
j := s.Before
if j < 0 {
continue
}
for after := s.After; j <= after; j++ {
c := seg.getCharInfo(j)
if c.before == -1 || i < c.before {
c.before = i
}
if c.after < i {
c.after = i
}
}
s.index = i
}
for s := seg.First; s != nil; s = s.Next {
var a int
for a = s.After + 1; a < offset+numChars && seg.getCharInfo(a).after < 0; a++ {
seg.getCharInfo(a).after = s.index
}
a--
s.After = a
for a = s.Before - 1; a >= offset && seg.getCharInfo(a).before < 0; a-- {
seg.getCharInfo(a).before = s.index
}
a++
s.Before = a
}
}
func (seg *Segment) initCollisions() bool {
seg.collisions = seg.collisions[:0]
seg.collisions = append(seg.collisions, make([]slotCollision, seg.NumGlyphs)...)
for p := seg.First; p != nil; p = p.Next {
if p.index < seg.NumGlyphs {
seg.collisions[p.index].init(seg, p)
} else {
return false
}
}
return true
}