-
Notifications
You must be signed in to change notification settings - Fork 7
/
logging.go
611 lines (541 loc) · 15.7 KB
/
logging.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
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
package graphite
import (
"encoding/json"
"fmt"
)
// this file implements tracing helpers, which are only used
// in debug mode
var tr = &traceOutput{}
type traceOutput struct {
colliderEnv colliderEnv
Outputdir string `json:"outputdir"`
Id string `json:"id"`
Output []slotJSON `json:"output"`
Chars []charInfo `json:"chars"`
Passes []passJSON `json:"passes"`
Advance Position `json:"advance"`
}
type colliderEnv struct {
sl *Slot
val int
}
func (tr *traceOutput) appendPass(s *passes, seg *Segment, i uint8) {
sd, pd := "ltr", "ltr"
if seg.currdir() {
sd = "rtl"
}
if s.isRTL != s.passes[i].isReverseDirection {
pd = "rtl"
}
debug := passJSON{
ID: i + 1,
Slotsdir: sd,
Passdir: pd,
Slots: seg.slotsJSON(),
Rules: make([]ruleDump, 0),
}
tr.Passes = append(tr.Passes, debug)
}
func (tr *traceOutput) finaliseOutput(seg *Segment) {
tr.Outputdir = "ltr"
if seg.currdir() {
tr.Outputdir = "rtl"
}
tr.Output = seg.slotsJSON()
tr.Advance = seg.Advance
tr.Chars = seg.charinfo
}
func (ci charInfo) MarshalJSON() ([]byte, error) {
type charInfoSlotJSON struct {
Before int `json:"before"`
After int `json:"after"`
}
type charInfoJSON struct {
Offset int `json:"offset"`
Unicode rune `json:"unicode"`
Break int16 `json:"break"`
Flags uint8 `json:"flags"`
Slot charInfoSlotJSON `json:"slot"`
}
out := charInfoJSON{
Offset: ci.base,
Unicode: ci.char,
Break: ci.breakWeight,
Flags: ci.flags,
Slot: charInfoSlotJSON{
Before: ci.before,
After: ci.after,
},
}
return json.Marshal(out)
}
type slotCharInfoJSON struct {
Original int `json:"original"`
Before int `json:"before"`
After int `json:"after"`
}
func (pos Position) MarshalJSON() ([]byte, error) {
return []byte(fmt.Sprintf("[%.4f, %.4f]", pos.X, pos.Y)), nil
}
func (r rect) MarshalJSON() ([]byte, error) {
return []byte(fmt.Sprintf("[%.4f, %.4f, %.4f, %.4f]", r.bl.X, r.bl.Y, r.tr.X, r.tr.Y)), nil
}
func (s *Slot) objectID() string {
if s == nil {
return "0000-00-0000"
}
return fmt.Sprintf("%p", s)
}
type slotParentJSON struct {
Id string `json:"id"`
Level int32 `json:"level"`
Offset Position `json:"offset"`
}
type collisionSeq struct {
Seqclass Position `json:"seqclass"`
Seqorder uint16 `json:"seqorder"`
Seqabove Position `json:"seqabove"`
Seqbelow Position `json:"seqbelow"`
Seqvalign Position `json:"seqvalign"`
}
type collisionJSON struct {
Limit rect `json:"limit"`
Offset Position `json:"offset"`
Margin Position `json:"margin"`
Excludeoffset Position `json:"excludeoffset"`
Flags uint16 `json:"flags"`
Exclude GID `json:"exclude"`
}
type collisionJSONWithSeq struct {
collisionJSON
collisionSeq
}
func (col collisionJSONWithSeq) MarshalJSON() ([]byte, error) {
if col.Seqorder != 0 {
type noMethod collisionJSONWithSeq
return json.Marshal(noMethod(col))
}
return json.Marshal(col.collisionJSON)
}
type slotJSON struct {
Collision *collisionJSONWithSeq `json:"collision,omitempty"`
Parent *slotParentJSON `json:"parent,omitempty"`
Id string `json:"id"`
User []int16 `json:"user"`
Children []string `json:"children,omitempty"`
Charinfo slotCharInfoJSON `json:"charinfo"`
Origin Position `json:"origin"`
Shift Position `json:"shift"`
Advance Position `json:"advance"`
Justification float32 `json:"justification,omitempty"`
Break int32 `json:"break"`
Gid GID `json:"gid"`
Bidi uint8 `json:"bidi,omitempty"`
Insert bool `json:"insert"`
}
// returns a JSON compatible representation of the slot
func (s *Slot) json(seg *Segment) slotJSON {
out := slotJSON{
Id: s.objectID(),
Gid: s.glyphID,
Charinfo: slotCharInfoJSON{
Original: s.original,
Before: s.Before,
After: s.After,
},
Origin: s.Position,
Shift: Position{
X: float32(s.getAttr(nil, acShiftX, 0)),
Y: float32(s.getAttr(nil, acShiftY, 0)),
},
Advance: s.Advance,
Insert: s.CanInsertBefore(),
Break: s.getAttr(seg, acBreak, 0),
Justification: s.just,
Bidi: s.bidiLevel,
User: append([]int16(nil), s.userAttrs...),
}
if !s.isBase() {
out.Parent = &slotParentJSON{
Id: s.parent.objectID(),
Level: s.getAttr(nil, acAttLevel, 0),
Offset: s.attach.sub(s.with),
}
}
if s.child != nil {
for c := s.child; c != nil; c = c.sibling {
out.Children = append(out.Children, c.objectID())
}
}
if cslot := seg.getCollisionInfo(s); cslot != nil {
// Note: the reason for using Positions to lump together related attributes is to make the
// JSON output slightly more compact.
out.Collision = &collisionJSONWithSeq{
collisionJSON: collisionJSON{
Offset: cslot.offset,
Limit: cslot.limit,
Flags: cslot.flags,
Margin: Position{float32(cslot.margin), float32(cslot.marginWt)},
Exclude: cslot.exclGlyph,
Excludeoffset: cslot.exclOffset,
},
collisionSeq: collisionSeq{
Seqclass: Position{float32(cslot.seqClass), float32(cslot.seqProxClass)},
Seqorder: cslot.seqOrder,
Seqabove: Position{float32(cslot.seqAboveXoff), float32(cslot.seqAboveWt)},
Seqbelow: Position{float32(cslot.seqBelowXlim), float32(cslot.seqBelowWt)},
Seqvalign: Position{float32(cslot.seqValignHt), float32(cslot.seqValignWt)},
},
}
}
return out
}
func (seg *Segment) slotsJSON() (out []slotJSON) {
for s := seg.First; s != nil; s = s.Next {
out = append(out, s.json(seg))
}
return out
}
type passJSON struct {
Collisions *passCollisions `json:"collisions,omitempty"`
Constraint *bool `json:"constraint,omitempty"`
Slotsdir string `json:"slotsdir"`
Passdir string `json:"passdir"`
Slots []slotJSON `json:"slots"`
Rules []ruleDump `json:"rules"`
ID uint8 `json:"id"`
}
type ruleDump struct {
Output *ruleOutput `json:"output"`
Cursor string `json:"cursor"`
Considered []ruleJSON `json:"considered"`
}
type slotRange struct {
Start string `json:"start"`
End string `json:"end"`
}
type ruleOutput struct {
Range slotRange `json:"range"`
Slots []slotJSON `json:"slots"`
Postshift Position `json:"postshift"`
}
func inputSlot(slots *slotMap, n int) *Slot {
s := slots.get(int(slots.preContext) + n)
if !s.isCopied() {
return s
}
if s.prev != nil {
return s.prev.Next
}
if s.Next != nil {
return s.Next.prev
}
return slots.segment.last
}
func outputSlot(slots *slotMap, n int) *Slot {
s := slots.get(int(slots.preContext) + n - 1)
if s != nil {
return s.Next
}
return slots.segment.First
}
type ruleJSON struct {
Input struct {
Start string `json:"start"`
Length uint16 `json:"length"`
} `json:"input,omitempty"`
ID uint16 `json:"id"`
Failed bool `json:"failed"`
}
func (tr *traceOutput) startDumpRule(fsm *finiteStateMachine, length int) {
pass := &tr.Passes[len(tr.Passes)-1]
var considered []ruleJSON
for _, ruleIndex := range fsm.rules[:length] {
r := fsm.ruleTable[ruleIndex]
if uint16(r.preContext) > fsm.slots.preContext {
continue
}
rj := ruleJSON{
ID: ruleIndex,
Failed: true,
Input: struct {
Start string `json:"start"`
Length uint16 `json:"length"`
}{
Start: inputSlot(&fsm.slots, -int(r.preContext)).objectID(),
Length: r.sortKey,
},
}
considered = append(considered, rj)
}
pass.Rules = append(pass.Rules, ruleDump{Considered: considered})
}
func (tr *traceOutput) dumpRuleOutput(fsm *finiteStateMachine, ruleIndex uint16, lastSlot *Slot) {
r := fsm.ruleTable[ruleIndex]
rj := ruleJSON{
ID: ruleIndex,
Failed: false,
Input: struct {
Start string `json:"start"`
Length uint16 `json:"length"`
}{
Start: inputSlot(&fsm.slots, 0).objectID(),
Length: r.sortKey - uint16(r.preContext),
},
}
pass := &tr.Passes[len(tr.Passes)-1]
rule := &pass.Rules[len(pass.Rules)-1]
rule.Considered = append(rule.Considered, rj)
oj := ruleOutput{
Range: slotRange{inputSlot(&fsm.slots, 0).objectID(), lastSlot.objectID()},
}
rsbPrepos := fsm.slots.segment.Advance
if lastSlot != nil {
rsbPrepos = lastSlot.Position
}
fsm.slots.segment.positionSlots(nil, nil, nil, fsm.slots.segment.currdir(), true)
for slot := outputSlot(&fsm.slots, 0); slot != lastSlot; slot = slot.Next {
oj.Slots = append(oj.Slots, slot.json(fsm.slots.segment))
}
if lastSlot != nil {
oj.Postshift = lastSlot.Position
} else {
oj.Postshift = fsm.slots.segment.Advance
}
oj.Postshift = oj.Postshift.sub(rsbPrepos)
rule.Output = &oj
}
func (tr *traceOutput) dumpRuleCursor(s *Slot) {
pass := &tr.Passes[len(tr.Passes)-1]
rule := &pass.Rules[len(pass.Rules)-1]
rule.Cursor = s.objectID()
}
func (tr *traceOutput) setCurrentPassConstraint(b bool) {
tr.Passes[len(tr.Passes)-1].Constraint = &b
}
type passCollisions struct {
phases []collisionPhase
numLoops uint8
}
func (pc passCollisions) MarshalJSON() ([]byte, error) {
tmp := []interface{}{
map[string]uint8{"num-loops": pc.numLoops},
}
for _, v := range pc.phases {
tmp = append(tmp, v)
}
return json.Marshal(tmp)
}
type phaseMove interface {
isPhaseMove()
}
func (*collisionMove) isPhaseMove() {}
func (kernMove) isPhaseMove() {}
type collisionPhase struct {
Phase string `json:"phase"`
Moves []phaseMove `json:"moves"`
Loop int `json:"loop"`
}
func (cl collisionPhase) MarshalJSON() ([]byte, error) {
if cl.Loop == -1 { // ignore Loop here
type noLoop struct {
Phase string `json:"phase"`
Moves []phaseMove `json:"moves"`
}
tmp := noLoop{Phase: cl.Phase, Moves: cl.Moves}
return json.Marshal(tmp)
}
type noMethod collisionPhase
return json.Marshal(noMethod(cl))
}
type collisionMove struct {
Slot string `json:"slot"`
Vectors []collisionVector `json:"vectors"`
Target collisionMoveTarget `json:"target"`
BestAxis int `json:"bestAxis"`
Limit rect `json:"limit"`
Result Position `json:"result"`
Gid GID `json:"gid"`
StillBad bool `json:"stillBad"`
}
type collisionMoveTarget struct {
Fix string `json:"fix"`
Bbox rect `json:"bbox"`
SlantBox rect `json:"slantBox"`
Origin Position `json:"origin"`
CurrShift Position `json:"currShift"`
CurrOffset Position `json:"currOffset"`
}
type collisionVector struct {
Direction string `json:"direction"`
Removals [][]interface{} `json:"removals"`
Ranges []interface{} `json:"ranges"`
TargetMin float32 `json:"targetMin"`
BestCost float32 `json:"bestCost"`
BestVal float32 `json:"bestVal"`
}
func (tr *traceOutput) startDumpCollisions(numLoops uint8) {
tr.Passes[len(tr.Passes)-1].Collisions = &passCollisions{numLoops: numLoops}
}
func (tr *traceOutput) startDumpCollisionPhase(phase string, loop int) {
cl := tr.Passes[len(tr.Passes)-1].Collisions
cl.phases = append(cl.phases, collisionPhase{Phase: phase, Loop: loop})
}
func (tr *traceOutput) addCollisionMove(sc *shiftCollider, seg *Segment) {
cl := tr.Passes[len(tr.Passes)-1].Collisions
phase := &cl.phases[len(cl.phases)-1]
phase.Moves = append(phase.Moves, &collisionMove{
Slot: sc.target.objectID(),
Gid: sc.target.glyphID,
Limit: sc.limit,
Target: collisionMoveTarget{
Origin: sc.origin,
CurrShift: sc.currShift,
CurrOffset: seg.getCollisionInfo(sc.target).offset,
Bbox: seg.face.getGlyph(sc.target.glyphID).bbox,
SlantBox: seg.face.getGlyph(sc.target.glyphID).boxes.slant,
Fix: "shift",
},
})
}
func (tr *traceOutput) currentPhaseMove() phaseMove {
cl := tr.Passes[len(tr.Passes)-1].Collisions
phase := &cl.phases[len(cl.phases)-1]
return phase.Moves[len(phase.Moves)-1]
}
func (tr *traceOutput) endCollisionMove(resultPos Position, bestAxis int, isCol bool) {
move := tr.currentPhaseMove().(*collisionMove)
move.Result = resultPos
//<< "scraping" << _scraping[bestAxis]
move.BestAxis = bestAxis
move.StillBad = isCol
}
func (tr *traceOutput) addCollisionVector(sc *shiftCollider, seg *Segment, axis int,
tleft, bestCost, bestVal float32,
) {
var out collisionVector
switch axis {
case 0:
out.Direction = "x"
case 1:
out.Direction = "y"
case 2:
out.Direction = "sum (NE-SW)"
case 3:
out.Direction = "diff (NW-SE)"
default:
out.Direction = "???"
}
out.TargetMin = tleft
out.Removals = sc.ranges[axis].formatDebugs(seg)
out.Ranges = sc.debugAxis(seg, axis)
out.BestCost = bestCost
out.BestVal = bestVal + tleft
move := tr.currentPhaseMove().(*collisionMove)
move.Vectors = append(move.Vectors, out)
}
type zoneDebug struct {
env colliderEnv
excl exclusion
isDel bool
}
type fl5 [5]float32
func (fs fl5) MarshalJSON() ([]byte, error) {
return []byte(fmt.Sprintf("[%.4f, %.4f, %.4f, %.4f, %.4f]",
fs[0], fs[1], fs[2], fs[3], fs[4])), nil
}
func (zo zones) formatDebugs(seg *Segment) [][]interface{} {
var out [][]interface{}
for _, s := range zo.debugs {
l := []interface{}{
s.env.sl.objectID(),
s.env.val,
}
if s.isDel {
l = append(l, "remove", Position{s.excl.x, s.excl.xm})
} else {
l = append(l, "exclude", fl5{
s.excl.x, s.excl.xm,
s.excl.sm, s.excl.smx, s.excl.c,
})
}
out = append(out, l)
}
return out
}
func (sc *shiftCollider) debugAxis(seg *Segment, axis int) []interface{} {
var out []interface{}
out = append(out, Position{sc.ranges[axis].pos, sc.ranges[axis].posm})
// *dbgout << json::flat << json::array << _ranges[axis].position();
for _, s := range sc.ranges[axis].exclusions {
l := []interface{}{
Position{s.x, s.xm},
s.sm,
s.smx,
s.c,
}
out = append(out, l)
}
return out
}
type kernTarget struct {
Fix string `json:"fix"`
Bbox rect `json:"bbox"`
SlantBox rect `json:"slantBox"`
Origin Position `json:"origin"`
OffsetPrev Position `json:"offsetPrev"`
}
type kernSlice struct {
Neighbor string `json:"neighbor"`
I int `json:"i"`
TargetEdge float32 `json:"targetEdge"`
NearEdge float32 `json:"nearEdge"`
}
type kernMove struct {
Slot string `json:"slot"`
Slices []kernSlice `json:"slices"`
Target kernTarget `json:"target"`
Limit rect `json:"limit"`
Miny float32 `json:"miny"`
Maxy float32 `json:"maxy"`
Slicewidth float32 `json:"slicewidth"`
Xbound float32 `json:"xbound"`
MinGap float32 `json:"minGap"`
Needed float32 `json:"needed"`
Result float32 `json:"result"`
Gid GID `json:"gid"`
StillBad bool `json:"stillBad"`
}
func (tr *traceOutput) addKern(kc *kernCollider, seg *Segment, result, resultNeeded float32) {
var out kernMove
out.Slot = kc.target.objectID()
out.Gid = kc.target.glyphID
out.Limit = kc.limit
out.Miny = kc.miny
out.Maxy = kc.maxy
out.Slicewidth = kc.sliceWidth
out.Target = kernTarget{
Origin: kc.target.Position,
// CurrShift: kc.currShift,
OffsetPrev: kc.offsetPrev,
Bbox: seg.face.getGlyph(kc.target.glyphID).bbox,
SlantBox: seg.face.getGlyph(kc.target.glyphID).boxes.slant,
Fix: "kern",
}
for is := range kc.edges {
out.Slices = append(out.Slices, kernSlice{
I: is,
TargetEdge: kc.edges[is],
Neighbor: kc.slotNear[is].objectID(),
NearEdge: kc.nearEdges[is],
})
}
out.Xbound = kc.xbound
out.MinGap = kc.mingap
out.Needed = resultNeeded
out.Result = result
out.StillBad = (result != resultNeeded)
cl := tr.Passes[len(tr.Passes)-1].Collisions
phase := &cl.phases[len(cl.phases)-1]
phase.Moves = append(phase.Moves, out)
}