-
Notifications
You must be signed in to change notification settings - Fork 7
/
ot_indic.go
1665 lines (1493 loc) · 50.5 KB
/
ot_indic.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
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package harfbuzz
import (
"fmt"
"sort"
"github.com/benoitkugler/textlayout/fonts"
tt "github.com/benoitkugler/textlayout/fonts/truetype"
"github.com/benoitkugler/textlayout/language"
)
// ported from harfbuzz/src/hb-ot-shape-complex-indic.cc, .hh Copyright © 2011,2012 Google, Inc. Behdad Esfahbod
// UniscribeBugCompatible alters shaping of indic and khmer scripts:
// - when `false`, it applies the recommended shaping choices
// - when `true`, Uniscribe behavior is reproduced
var UniscribeBugCompatible = false
var _ otComplexShaper = (*complexShaperIndic)(nil)
// Indic shaper.
type complexShaperIndic struct {
complexShaperNil
plan indicShapePlan
}
/* Note:
*
* We treat Vowels and placeholders as if they were consonants. This is safe because Vowels
* cannot happen in a consonant syllable. The plus side however is, we can call the
* consonant syllable logic from the vowel syllable function and get it all right! */
const (
medialFlags = 1 << otCM
consonantFlags = 1<<otC | 1<<otCS | 1<<otRa | medialFlags | 1<<otV | 1<<otPLACEHOLDER | 1<<otDOTTEDCIRCLE
joinerFlags = 1<<otZWJ | 1<<otZWNJ
)
func isOneOf(info *GlyphInfo, flags uint) bool {
/* If it ligated, all bets are off. */
if info.ligated() {
return false
}
return 1<<info.complexCategory&flags != 0
}
func isJoiner(info *GlyphInfo) bool {
return isOneOf(info, joinerFlags)
}
func isConsonant(info *GlyphInfo) bool {
return isOneOf(info, consonantFlags)
}
func isHalant(info *GlyphInfo) bool {
return isOneOf(info, 1<<otH)
}
func isDeva(u rune) bool { return u & ^0x7F == 0x0900 }
func isBeng(u rune) bool { return u & ^0x7F == 0x0980 }
func isGuru(u rune) bool { return u & ^0x7F == 0x0A00 }
func isGujr(u rune) bool { return u & ^0x7F == 0x0A80 }
func isOrya(u rune) bool { return u & ^0x7F == 0x0B00 }
func isTaml(u rune) bool { return u & ^0x7F == 0x0B80 }
func isTelu(u rune) bool { return u & ^0x7F == 0x0C00 }
func isKnda(u rune) bool { return u & ^0x7F == 0x0C80 }
func isMlym(u rune) bool { return u & ^0x7F == 0x0D00 }
func isSinh(u rune) bool { return u & ^0x7F == 0x0D80 }
func matraPositionIndic(u rune, side uint8) uint8 {
switch side {
case posPreC:
return posPreM
case posPostC:
switch {
case isDeva(u):
return posAfterSub
case isBeng(u):
return posAfterPost
case isGuru(u):
return posAfterPost
case isGujr(u):
return posAfterPost
case isOrya(u):
return posAfterPost
case isTaml(u):
return posAfterPost
case isTelu(u):
if u <= 0x0C42 {
return posBeforeSub
}
return posAfterSub
case isKnda(u):
if u < 0x0CC3 || u > 0xCD6 {
return posBeforeSub
}
return posAfterSub
case isMlym(u):
return posAfterPost
case isSinh(u):
return posAfterSub
default:
return posAfterSub
}
case posAboveC: /* BENG and MLYM don't have top matras. */
switch {
case isDeva(u):
return posAfterSub
case isGuru(u):
return posAfterPost /* Deviate from spec */
case isGujr(u):
return posAfterSub
case isOrya(u):
return posAfterMain
case isTaml(u):
return posAfterSub
case isTelu(u):
return posBeforeSub
case isKnda(u):
return posBeforeSub
case isSinh(u):
return posAfterSub
default:
return posAfterSub
}
case posBelowC:
switch {
case isDeva(u):
return posAfterSub
case isBeng(u):
return posAfterSub
case isGuru(u):
return posAfterPost
case isGujr(u):
return posAfterPost
case isOrya(u):
return posAfterSub
case isTaml(u):
return posAfterPost
case isTelu(u):
return posBeforeSub
case isKnda(u):
return posBeforeSub
case isMlym(u):
return posAfterPost
case isSinh(u):
return posAfterSub
default:
return posAfterSub
}
}
return side
}
func isRa(u rune) bool {
switch u {
case 0x0930, /* Devanagari */
0x09B0, /* Bengali */
0x09F0, /* Bengali */
0x0A30, /* Gurmukhi */ /* No Reph */
0x0AB0, /* Gujarati */
0x0B30, /* Oriya */
0x0BB0, /* Tamil */ /* No Reph */
0x0C30, /* Telugu */ /* Reph formed only with ZWJ */
0x0CB0, /* Kannada */
0x0D30, /* Malayalam */ /* No Reph, Logical Repha */
0x0DBB: /* Sinhala */ /* Reph formed only with ZWJ */
return true
default:
return false
}
}
func (info *GlyphInfo) setIndicProperties() {
u := info.codepoint
info.complexCategory, info.complexAux = computeIndicProperties(u)
}
func computeIndicProperties(u rune) (cat, pos uint8) {
type_ := indicGetCategories(u)
cat = uint8(type_ & 0xFF)
pos = uint8(type_ >> 8)
/*
* Re-assign category
*/
/* The following act more like the Bindus. */
if 0x0953 <= u && u <= 0x0954 {
cat = otSM
/* The following act like consonants. */
} else if (0x0A72 <= u && u <= 0x0A73) || (0x1CF5 <= u && u <= 0x1CF6) {
cat = otC
} else if 0x1CE2 <= u && u <= 0x1CE8 {
cat = otA
} else if u == 0x1CED {
cat = otA
/* The following take marks in standalone clusters, similar to Avagraha. */
} else if 0xA8F2 <= u && u <= 0xA8F7 || 0x1CE9 <= u && u <= 0x1CEC || 0x1CEE <= u && u <= 0x1CF1 {
cat = otSymbol
} else if u == 0x0A51 {
/* https://github.com/harfbuzz/harfbuzz/issues/524 */
cat = otM
pos = posBelowC
/* According to ScriptExtensions.txt, these Grantha marks may also be used in Tamil,
* so the Indic shaper needs to know their categories. */
} else if u == 0x11301 || u == 0x11303 {
cat = otSM
} else if u == 0x1133B || u == 0x1133C {
cat = otN
} else if u == 0x0AFB {
cat = otN /* https://github.com/harfbuzz/harfbuzz/issues/552 */
} else if u == 0x0B55 {
cat = otN /* https://github.com/harfbuzz/harfbuzz/issues/2849 */
} else if u == 0x0980 {
cat = otPLACEHOLDER /* https://github.com/harfbuzz/harfbuzz/issues/538 */
} else if u == 0x09FC {
cat = otPLACEHOLDER /* https://github.com/harfbuzz/harfbuzz/pull/1613 */
} else if u == 0x0C80 {
cat = otPLACEHOLDER /* https://github.com/harfbuzz/harfbuzz/pull/623 */
} else if 0x2010 <= u && u <= 0x2011 {
cat = otPLACEHOLDER
} else if u == 0x25CC {
cat = otDOTTEDCIRCLE
}
/*
* Re-assign position.
*/
if 1<<cat&consonantFlags != 0 {
pos = posBaseC
if isRa(u) {
cat = otRa
}
} else if cat == otM {
pos = matraPositionIndic(u, pos)
} else if 1<<cat&(1<<otSM /* | FLAG (otVd) */ |1<<otA|1<<otSymbol) != 0 {
pos = posSmvd
}
if u == 0x0B01 {
pos = posBeforeSub /* Oriya Bindu is BeforeSub in the spec. */
}
return cat, pos
}
type indicWouldSubstituteFeature struct {
lookups []lookupMap
// count int
zeroContext bool
}
func newIndicWouldSubstituteFeature(map_ *otMap, featureTag tt.Tag, zeroContext bool) indicWouldSubstituteFeature {
var out indicWouldSubstituteFeature
out.zeroContext = zeroContext
out.lookups = map_.getStageLookups(0 /*GSUB*/, map_.getFeatureStage(0 /*GSUB*/, featureTag))
return out
}
func (ws indicWouldSubstituteFeature) wouldSubstitute(glyphs []fonts.GID, font *Font) bool {
for _, lk := range ws.lookups {
if otLayoutLookupWouldSubstitute(font, lk.index, glyphs, ws.zeroContext) {
return true
}
}
return false
}
/*
* Indic configurations. Note that we do not want to keep every single script-specific
* behavior in these tables necessarily. This should mainly be used for per-script
* properties that are cheaper keeping here, than in the code. Ie. if, say, one and
* only one script has an exception, that one script can be if'ed directly in the code,
* instead of adding a new flag in these structs.
*/
// base_position_t
const (
basePosLastSinhala = iota
basePosLast
)
// reph_position_t
const (
rephPosAfterMain = posAfterMain
rephPosBeforeSub = posBeforeSub
rephPosAfterSub = posAfterSub
rephPosBeforePost = posBeforePost
rephPosAfterPost = posAfterPost
)
// reph_mode_t
const (
rephModeImplicit = iota /* Reph formed out of initial Ra,H sequence. */
rephModeExplicit /* Reph formed out of initial Ra,H,ZWJ sequence. */
rephModeLogRepha /* Encoded Repha character, needs reordering. */
)
// blwf_mode_t
const (
blwfModePreAndPost = iota /* Below-forms feature applied to pre-base and post-base. */
blwfModePostOnly /* Below-forms feature applied to post-base only. */
)
type indicConfig struct {
script language.Script
hasOldSpec bool
virama rune
basePos uint8
rephPos uint8
rephMode uint8
blwfMode uint8
}
var indicConfigs = [...]indicConfig{
/* Default. Should be first. */
{0, false, 0, basePosLast, rephPosBeforePost, rephModeImplicit, blwfModePreAndPost},
{language.Devanagari, true, 0x094D, basePosLast, rephPosBeforePost, rephModeImplicit, blwfModePreAndPost},
{language.Bengali, true, 0x09CD, basePosLast, rephPosAfterSub, rephModeImplicit, blwfModePreAndPost},
{language.Gurmukhi, true, 0x0A4D, basePosLast, rephPosBeforeSub, rephModeImplicit, blwfModePreAndPost},
{language.Gujarati, true, 0x0ACD, basePosLast, rephPosBeforePost, rephModeImplicit, blwfModePreAndPost},
{language.Oriya, true, 0x0B4D, basePosLast, rephPosAfterMain, rephModeImplicit, blwfModePreAndPost},
{language.Tamil, true, 0x0BCD, basePosLast, rephPosAfterPost, rephModeImplicit, blwfModePreAndPost},
{language.Telugu, true, 0x0C4D, basePosLast, rephPosAfterPost, rephModeExplicit, blwfModePostOnly},
{language.Kannada, true, 0x0CCD, basePosLast, rephPosAfterPost, rephModeImplicit, blwfModePostOnly},
{language.Malayalam, true, 0x0D4D, basePosLast, rephPosAfterMain, rephModeLogRepha, blwfModePreAndPost},
{
language.Sinhala, false, 0x0DCA, basePosLastSinhala,
rephPosAfterPost, rephModeExplicit, blwfModePreAndPost,
},
}
/*
* Indic shaper.
*/
var indicFeatures = [...]otMapFeature{
/*
* Basic features.
* These features are applied in order, one at a time, after initial_reordering.
*/
{tt.NewTag('n', 'u', 'k', 't'), ffGlobalManualJoiners},
{tt.NewTag('a', 'k', 'h', 'n'), ffGlobalManualJoiners},
{tt.NewTag('r', 'p', 'h', 'f'), ffManualJoiners},
{tt.NewTag('r', 'k', 'r', 'f'), ffGlobalManualJoiners},
{tt.NewTag('p', 'r', 'e', 'f'), ffManualJoiners},
{tt.NewTag('b', 'l', 'w', 'f'), ffManualJoiners},
{tt.NewTag('a', 'b', 'v', 'f'), ffManualJoiners},
{tt.NewTag('h', 'a', 'l', 'f'), ffManualJoiners},
{tt.NewTag('p', 's', 't', 'f'), ffManualJoiners},
{tt.NewTag('v', 'a', 't', 'u'), ffGlobalManualJoiners},
{tt.NewTag('c', 'j', 'c', 't'), ffGlobalManualJoiners},
/*
* Other features.
* These features are applied all at once, after final_reordering
* but before clearing syllables.
* Default Bengali font in Windows for example has intermixed
* lookups for init,pres,abvs,blws features.
*/
{tt.NewTag('i', 'n', 'i', 't'), ffManualJoiners},
{tt.NewTag('p', 'r', 'e', 's'), ffGlobalManualJoiners},
{tt.NewTag('a', 'b', 'v', 's'), ffGlobalManualJoiners},
{tt.NewTag('b', 'l', 'w', 's'), ffGlobalManualJoiners},
{tt.NewTag('p', 's', 't', 's'), ffGlobalManualJoiners},
{tt.NewTag('h', 'a', 'l', 'n'), ffGlobalManualJoiners},
}
// in the same order as the indicFeatures array
const (
indicNukt = iota
indicAkhn
indicRphf
indicRkrf
indicPref
indicBlwf
indicAbvf
indicHalf
indicPstf
indicVatu
indicCjct
indicInit
indicPres
indicAbvs
indicBlws
indicPsts
indicHaln
indicNumFeatures
indicBasicFeatures = indicInit /* Don't forget to update this! */
)
func (cs *complexShaperIndic) collectFeatures(plan *otShapePlanner) {
map_ := &plan.map_
/* Do this before any lookups have been applied. */
map_.addGSUBPause(setupSyllablesIndic)
map_.enableFeature(tt.NewTag('l', 'o', 'c', 'l'))
/* The Indic specs do not require ccmp, but we apply it here since if
* there is a use of it, it's typically at the beginning. */
map_.enableFeature(tt.NewTag('c', 'c', 'm', 'p'))
i := 0
map_.addGSUBPause(cs.initialReorderingIndic)
for ; i < indicBasicFeatures; i++ {
map_.addFeatureExt(indicFeatures[i].tag, indicFeatures[i].flags, 1)
map_.addGSUBPause(nil)
}
map_.addGSUBPause(cs.plan.finalReorderingIndic)
for ; i < indicNumFeatures; i++ {
map_.addFeatureExt(indicFeatures[i].tag, indicFeatures[i].flags, 1)
}
map_.addGSUBPause(clearSyllables)
}
func (complexShaperIndic) overrideFeatures(plan *otShapePlanner) {
plan.map_.disableFeature(tt.NewTag('l', 'i', 'g', 'a'))
}
type indicShapePlan struct {
blwf indicWouldSubstituteFeature
pstf indicWouldSubstituteFeature
vatu indicWouldSubstituteFeature
rphf indicWouldSubstituteFeature
pref indicWouldSubstituteFeature
maskArray [indicNumFeatures]GlyphMask
config indicConfig
viramaGlyph fonts.GID // cached value
isOldSpec bool
uniscribeBugCompatible bool
}
func (indicPlan *indicShapePlan) loadViramaGlyph(font *Font) fonts.GID {
if indicPlan.viramaGlyph == ^fonts.GID(0) {
glyph, ok := font.face.NominalGlyph(indicPlan.config.virama)
if indicPlan.config.virama == 0 || !ok {
glyph = 0
}
/* Technically speaking, the spec says we should apply 'locl' to virama too.
* Maybe one day... */
/* Our get_nominal_glyph() function needs a font, so we can't get the virama glyph
* during shape planning... Instead, overwrite it here. */
indicPlan.viramaGlyph = glyph
}
return indicPlan.viramaGlyph
}
func (cs *complexShaperIndic) dataCreate(plan *otShapePlan) {
var indicPlan indicShapePlan
indicPlan.config = indicConfigs[0]
for i := 1; i < len(indicConfigs); i++ {
if plan.props.Script == indicConfigs[i].script {
indicPlan.config = indicConfigs[i]
break
}
}
indicPlan.isOldSpec = indicPlan.config.hasOldSpec && ((plan.map_.chosenScript[0] & 0x000000FF) != '2')
indicPlan.uniscribeBugCompatible = UniscribeBugCompatible
indicPlan.viramaGlyph = ^fonts.GID(0)
/* Use zero-context wouldSubstitute() matching for new-spec of the main
* Indic scripts, and scripts with one spec only, but not for old-specs.
* The new-spec for all dual-spec scripts says zero-context matching happens.
*
* However, testing with Malayalam shows that old and new spec both allow
* context. Testing with Bengali new-spec however shows that it doesn't.
* So, the heuristic here is the way it is. It should *only* be changed,
* as we discover more cases of what Windows does. DON'T TOUCH OTHERWISE. */
zeroContext := !indicPlan.isOldSpec && plan.props.Script != language.Malayalam
indicPlan.rphf = newIndicWouldSubstituteFeature(&plan.map_, tt.NewTag('r', 'p', 'h', 'f'), zeroContext)
indicPlan.pref = newIndicWouldSubstituteFeature(&plan.map_, tt.NewTag('p', 'r', 'e', 'f'), zeroContext)
indicPlan.blwf = newIndicWouldSubstituteFeature(&plan.map_, tt.NewTag('b', 'l', 'w', 'f'), zeroContext)
indicPlan.pstf = newIndicWouldSubstituteFeature(&plan.map_, tt.NewTag('p', 's', 't', 'f'), zeroContext)
indicPlan.vatu = newIndicWouldSubstituteFeature(&plan.map_, tt.NewTag('v', 'a', 't', 'u'), zeroContext)
for i := range indicPlan.maskArray {
if indicFeatures[i].flags&ffGLOBAL != 0 {
indicPlan.maskArray[i] = 0
} else {
indicPlan.maskArray[i] = plan.map_.getMask1(indicFeatures[i].tag)
}
}
cs.plan = indicPlan
}
func (indicPlan *indicShapePlan) consonantPositionFromFace(consonant, virama fonts.GID, font *Font) uint8 {
/* For old-spec, the order of glyphs is Consonant,Virama,
* whereas for new-spec, it's Virama,Consonant. However,
* some broken fonts (like Free Sans) simply copied lookups
* from old-spec to new-spec without modification.
* And oddly enough, Uniscribe seems to respect those lookups.
* Eg. in the sequence U+0924,U+094D,U+0930, Uniscribe finds
* base at 0. The font however, only has lookups matching
* 930,94D in 'blwf', not the expected 94D,930 (with new-spec
* table). As such, we simply match both sequences. Seems
* to work.
*
* Vatu is done as well, for:
* https://github.com/harfbuzz/harfbuzz/issues/1587
*/
glyphs := [3]fonts.GID{virama, consonant, virama}
if indicPlan.blwf.wouldSubstitute(glyphs[0:2], font) ||
indicPlan.blwf.wouldSubstitute(glyphs[1:3], font) ||
indicPlan.vatu.wouldSubstitute(glyphs[0:2], font) ||
indicPlan.vatu.wouldSubstitute(glyphs[1:3], font) {
return posBelowC
}
if indicPlan.pstf.wouldSubstitute(glyphs[0:2], font) ||
indicPlan.pstf.wouldSubstitute(glyphs[1:3], font) {
return posPostC
}
if indicPlan.pref.wouldSubstitute(glyphs[0:2], font) ||
indicPlan.pref.wouldSubstitute(glyphs[1:3], font) {
return posPostC
}
return posBaseC
}
func (cs *complexShaperIndic) setupMasks(plan *otShapePlan, buffer *Buffer, _ *Font) {
/* We cannot setup masks here. We save information about characters
* and setup masks later on in a pause-callback. */
info := buffer.Info
for i := range info {
info[i].setIndicProperties()
}
}
func setupSyllablesIndic(_ *otShapePlan, _ *Font, buffer *Buffer) {
findSyllablesIndic(buffer)
iter, count := buffer.syllableIterator()
for start, end := iter.next(); start < count; start, end = iter.next() {
buffer.unsafeToBreak(start, end)
}
}
func foundSyllableIndic(syllableType uint8, ts, te int, info []GlyphInfo, syllableSerial *uint8) {
for i := ts; i < te; i++ {
info[i].syllable = (*syllableSerial << 4) | syllableType
}
*syllableSerial++
if *syllableSerial == 16 {
*syllableSerial = 1
}
}
func (indicPlan *indicShapePlan) updateConsonantPositionsIndic(font *Font, buffer *Buffer) {
if indicPlan.config.basePos != basePosLast {
return
}
virama := indicPlan.loadViramaGlyph(font)
if virama != 0 {
info := buffer.Info
for i := range info {
if info[i].complexAux == posBaseC {
consonant := info[i].Glyph
info[i].complexAux = indicPlan.consonantPositionFromFace(consonant, virama, font)
}
}
}
}
/* Rules from:
* https://docs.microsqoft.com/en-us/typography/script-development/devanagari */
func (indicPlan *indicShapePlan) initialReorderingConsonantSyllable(font *Font, buffer *Buffer, start, end int) {
info := buffer.Info
/* https://github.com/harfbuzz/harfbuzz/issues/435#issuecomment-335560167
* For compatibility with legacy usage in Kannada,
* Ra+h+ZWJ must behave like Ra+ZWJ+h... */
if buffer.Props.Script == language.Kannada &&
start+3 <= end &&
isOneOf(&info[start], 1<<otRa) &&
isOneOf(&info[start+1], 1<<otH) &&
isOneOf(&info[start+2], 1<<otZWJ) {
buffer.mergeClusters(start+1, start+3)
info[start+1], info[start+2] = info[start+2], info[start+1]
}
/* 1. Find base consonant:
*
* The shaping engine finds the base consonant of the syllable, using the
* following algorithm: starting from the end of the syllable, move backwards
* until a consonant is found that does not have a below-base or post-base
* form (post-base forms have to follow below-base forms), or that is not a
* pre-base-reordering Ra, or arrive at the first consonant. The consonant
* stopped at will be the base.
*
* o If the syllable starts with Ra + Halant (in a script that has Reph)
* and has more than one consonant, Ra is excluded from candidates for
* base consonants.
*/
base := end
hasReph := false
{
/* . If the syllable starts with Ra + Halant (in a script that has Reph)
* and has more than one consonant, Ra is excluded from candidates for
* base consonants. */
limit := start
if indicPlan.maskArray[indicRphf] != 0 && start+3 <= end &&
((indicPlan.config.rephMode == rephModeImplicit && !isJoiner(&info[start+2])) ||
(indicPlan.config.rephMode == rephModeExplicit && info[start+2].complexCategory == otZWJ)) {
/* See if it matches the 'rphf' feature. */
glyphs := [3]fonts.GID{info[start].Glyph, info[start+1].Glyph, 0}
if indicPlan.config.rephMode == rephModeExplicit {
glyphs[2] = info[start+2].Glyph
}
if indicPlan.rphf.wouldSubstitute(glyphs[:2], font) ||
(indicPlan.config.rephMode == rephModeExplicit &&
indicPlan.rphf.wouldSubstitute(glyphs[:3], font)) {
limit += 2
for limit < end && isJoiner(&info[limit]) {
limit++
}
base = start
hasReph = true
}
} else if indicPlan.config.rephMode == rephModeLogRepha && info[start].complexCategory == otRepha {
limit += 1
for limit < end && isJoiner(&info[limit]) {
limit++
}
base = start
hasReph = true
}
switch indicPlan.config.basePos {
case basePosLast:
{
/* . starting from the end of the syllable, move backwards */
i := end
seenBelow := false
for do := true; do; do = i > limit {
i--
/* . until a consonant is found */
if isConsonant(&info[i]) {
/* . that does not have a below-base or post-base form
* (post-base forms have to follow below-base forms), */
if info[i].complexAux != posBelowC &&
(info[i].complexAux != posPostC || seenBelow) {
base = i
break
}
if info[i].complexAux == posBelowC {
seenBelow = true
}
/* . or that is not a pre-base-reordering Ra,
*
* IMPLEMENTATION NOTES:
*
* Our pre-base-reordering Ra's are marked posPostC, so will be skipped
* by the logic above already.
*/
/* . or arrive at the first consonant. The consonant stopped at will
* be the base. */
base = i
} else {
/* A ZWJ after a Halant stops the base search, and requests an explicit
* half form.
* A ZWJ before a Halant, requests a subjoined form instead, and hence
* search continues. This is particularly important for Bengali
* sequence Ra,H,Ya that should form Ya-Phalaa by subjoining Ya. */
if start < i &&
info[i].complexCategory == otZWJ &&
info[i-1].complexCategory == otH {
break
}
}
}
}
case basePosLastSinhala:
{
/* Sinhala base positioning is slightly different from main Indic, in that:
* 1. Its ZWJ behavior is different,
* 2. We don't need to look into the font for consonant positions.
*/
if !hasReph {
base = limit
}
/* Find the last base consonant that is not blocked by ZWJ. If there is
* a ZWJ right before a base consonant, that would request a subjoined form. */
for i := limit; i < end; i++ {
if isConsonant(&info[i]) {
if limit < i && info[i-1].complexCategory == otZWJ {
break
} else {
base = i
}
}
}
/* Mark all subsequent consonants as below. */
for i := base + 1; i < end; i++ {
if isConsonant(&info[i]) {
info[i].complexAux = posBelowC
}
}
}
}
/* . If the syllable starts with Ra + Halant (in a script that has Reph)
* and has more than one consonant, Ra is excluded from candidates for
* base consonants.
*
* Only do this for unforced Reph. (ie. not for Ra,H,ZWJ. */
if hasReph && base == start && limit-base <= 2 {
/* Have no other consonant, so Reph is not formed and Ra becomes base. */
hasReph = false
}
}
/* 2. Decompose and reorder Matras:
*
* Each matra and any syllable modifier sign in the syllable are moved to the
* appropriate position relative to the consonant(s) in the syllable. The
* shaping engine decomposes two- or three-part matras into their constituent
* parts before any repositioning. Matra characters are classified by which
* consonant in a conjunct they have affinity for and are reordered to the
* following positions:
*
* o Before first half form in the syllable
* o After subjoined consonants
* o After post-form consonant
* o After main consonant (for above marks)
*
* IMPLEMENTATION NOTES:
*
* The normalize() routine has already decomposed matras for us, so we don't
* need to worry about that.
*/
/* 3. Reorder marks to canonical order:
*
* Adjacent nukta and halant or nukta and vedic sign are always repositioned
* if necessary, so that the nukta is first.
*
* IMPLEMENTATION NOTES:
*
* We don't need to do this: the normalize() routine already did this for us.
*/
/* Reorder characters */
for i := start; i < base; i++ {
info[i].complexAux = min8(posPreC, info[i].complexAux)
}
if base < end {
info[base].complexAux = posBaseC
}
/* Mark final consonants. A final consonant is one appearing after a matra.
* Happens in Sinhala. */
for i := base + 1; i < end; i++ {
if info[i].complexCategory == otM {
for j := i + 1; j < end; j++ {
if isConsonant(&info[j]) {
info[j].complexAux = posFinalC
break
}
}
break
}
}
/* Handle beginning Ra */
if hasReph {
info[start].complexAux = posRaToBecomeReph
}
/* For old-style Indic script tags, move the first post-base Halant after
* last consonant.
*
* Reports suggest that in some scripts Uniscribe does this only if there
* is *not* a Halant after last consonant already. We know that is the
* case for Kannada, while it reorders unconditionally in other scripts,
* eg. Malayalam, Bengali, and Devanagari. We don't currently know about
* other scripts, so we block Kannada.
*
* Kannada test case:
* U+0C9A,U+0CCD,U+0C9A,U+0CCD
* With some versions of Lohit Kannada.
* https://bugs.freedesktop.org/show_bug.cgi?id=59118
*
* Malayalam test case:
* U+0D38,U+0D4D,U+0D31,U+0D4D,U+0D31,U+0D4D
* With lohit-ttf-20121122/Lohit-Malayalam.ttf
*
* Bengali test case:
* U+0998,U+09CD,U+09AF,U+09CD
* With Windows XP vrinda.ttf
* https://github.com/harfbuzz/harfbuzz/issues/1073
*
* Devanagari test case:
* U+091F,U+094D,U+0930,U+094D
* With chandas.ttf
* https://github.com/harfbuzz/harfbuzz/issues/1071
*/
if indicPlan.isOldSpec {
disallowDoubleHalants := buffer.Props.Script == language.Kannada
for i := base + 1; i < end; i++ {
if info[i].complexCategory == otH {
var j int
for j = end - 1; j > i; j-- {
if isConsonant(&info[j]) ||
(disallowDoubleHalants && info[j].complexCategory == otH) {
break
}
}
if info[j].complexCategory != otH && j > i {
/* Move Halant to after last consonant. */
if debugMode >= 2 {
fmt.Printf("INDIC - halant: switching glyph %d to %d (and shifting between)", i, j)
}
t := info[i]
copy(info[i:j], info[i+1:])
info[j] = t
}
break
}
}
}
/* Attach misc marks to previous char to move with them. */
{
var lastPos uint8 = posStart
for i := start; i < end; i++ {
if 1<<info[i].complexCategory&(joinerFlags|1<<otN|1<<otRS|medialFlags|1<<otH) != 0 {
info[i].complexAux = lastPos
if info[i].complexCategory == otH && info[i].complexAux == posPreM {
/*
* Uniscribe doesn't move the Halant with Left Matra.
* TEST: U+092B,U+093F,U+094DE
* We follow. This is important for the Sinhala
* U+0DDA split matra since it decomposes to U+0DD9,U+0DCA
* where U+0DD9 is a left matra and U+0DCA is the virama.
* We don't want to move the virama with the left matra.
* TEST: U+0D9A,U+0DDA
*/
for j := i; j > start; j-- {
if info[j-1].complexAux != posPreM {
info[i].complexAux = info[j-1].complexAux
break
}
}
}
} else if info[i].complexAux != posSmvd {
lastPos = info[i].complexAux
}
}
}
/* For post-base consonants let them own anything before them
* since the last consonant or matra. */
{
last := base
for i := base + 1; i < end; i++ {
if isConsonant(&info[i]) {
for j := last + 1; j < i; j++ {
if info[j].complexAux < posSmvd {
info[j].complexAux = info[i].complexAux
}
}
last = i
} else if info[i].complexCategory == otM {
last = i
}
}
}
{
/* Use Syllable for sort accounting temporarily. */
syllable := info[start].syllable
for i := start; i < end; i++ {
info[i].syllable = uint8(i - start)
}
/* Sit tight, rock 'n roll! */
if debugMode >= 2 {
fmt.Printf("INDIC - post-base: sorting between glyph %d and %d\n", start, end)
}
subSlice := info[start:end]
sort.SliceStable(subSlice, func(i, j int) bool { return subSlice[i].complexAux < subSlice[j].complexAux })
/* Find base again */
base = end
for i := start; i < end; i++ {
if info[i].complexAux == posBaseC {
base = i
break
}
}
// Things are out-of-control for post base positions, they may shuffle
// around like crazy. In old-spec mode, we move halants around, so in
// that case merge all clusters after base. Otherwise, check the sort
// order and merge as needed.
// For pre-base stuff, we handle cluster issues in final reordering.
//
// We could use buffer.sort() for this, if there was no special
// reordering of pre-base stuff happening later...
// We don't want to mergeClusters all of that, which buffer.sort()
// would. Here's a concrete example:
//
// Assume there's a pre-base consonant and explicit Halant before base,
// followed by a prebase-reordering (left) Matra:
//
// C,H,ZWNJ,B,M
//
// At this point in reordering we would have:
//
// M,C,H,ZWNJ,B
//
// whereas in final reordering we will bring the Matra closer to Base:
//
// C,H,ZWNJ,M,B
//
// That's why we don't want to merge-clusters anything before the Base
// at this point. But if something moved from after Base to before it,
// we should merge clusters from base to them. In final-reordering, we
// only move things around before base, and merge-clusters up to base.
// These two merge-clusters from the two sides of base will interlock
// to merge things correctly. See:
// https://github.com/harfbuzz/harfbuzz/issues/2272
if indicPlan.isOldSpec || end-start > 127 {
buffer.mergeClusters(base, end)
} else {
/* Note! Syllable is a one-byte field. */
for i := base; i < end; i++ {
if info[i].syllable != 255 {
ma, mi := i, i
j := start + int(info[i].syllable)
for j != i {
mi = min(mi, j)
ma = max(ma, j)
next := start + int(info[j].syllable)
info[j].syllable = 255 /* So we don't process j later again. */
j = next
}
buffer.mergeClusters(max(base, mi), ma+1)
}
}
}
/* Put syllable back in. */
for i := start; i < end; i++ {
info[i].syllable = syllable
}
}
/* Setup masks now */
{
var mask GlyphMask
/* Reph */
for i := start; i < end && info[i].complexAux == posRaToBecomeReph; i++ {
info[i].Mask |= indicPlan.maskArray[indicRphf]
}
/* Pre-base */
mask = indicPlan.maskArray[indicHalf]
if !indicPlan.isOldSpec &&
indicPlan.config.blwfMode == blwfModePreAndPost {
mask |= indicPlan.maskArray[indicBlwf]
}
for i := start; i < base; i++ {
info[i].Mask |= mask
}
/* Base */
mask = 0
if base < end {
info[base].Mask |= mask
}
/* Post-base */
mask = indicPlan.maskArray[indicBlwf] |
indicPlan.maskArray[indicAbvf] |