-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathot_khmer.go
341 lines (286 loc) · 8.78 KB
/
ot_khmer.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
package harfbuzz
import (
"fmt"
"github.com/benoitkugler/textlayout/fonts"
tt "github.com/benoitkugler/textlayout/fonts/truetype"
)
// ported from harfbuzz/src/hb-ot-shape-complex-khmer.cc Copyright © 2011,2012 Google, Inc. Behdad Esfahbod
var _ otComplexShaper = (*complexShaperKhmer)(nil)
// Khmer shaper
type complexShaperKhmer struct {
plan khmerShapePlan
}
var khmerFeatures = [...]otMapFeature{
/*
* Basic features.
* These features are applied in order, one at a time, after reordering.
*/
{tt.NewTag('p', 'r', 'e', 'f'), ffManualJoiners},
{tt.NewTag('b', 'l', 'w', 'f'), ffManualJoiners},
{tt.NewTag('a', 'b', 'v', 'f'), ffManualJoiners},
{tt.NewTag('p', 's', 't', 'f'), ffManualJoiners},
{tt.NewTag('c', 'f', 'a', 'r'), ffManualJoiners},
/*
* Other features.
* These features are applied all at once after clearing syllables.
*/
{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},
}
// Must be in the same order as the khmerFeatures array.
const (
khmerPref = iota
khmerBlwf
khmerAbvf
khmerPstf
khmerCfar
khmerPres
khmerAbvs
khmerBlws
khmerPsts
khmerNumFeatures
khmerBasicFeatures = khmerPres /* Don't forget to update this! */
)
func (cs *complexShaperKhmer) collectFeatures(plan *otShapePlanner) {
map_ := &plan.map_
/* Do this before any lookups have been applied. */
map_.addGSUBPause(setupSyllablesKhmer)
map_.addGSUBPause(cs.reorderKhmer)
/* Testing suggests that Uniscribe does NOT pause between basic
* features. Test with KhmerUI.ttf and the following three
* sequences:
*
* U+1789,U+17BC
* U+1789,U+17D2,U+1789
* U+1789,U+17D2,U+1789,U+17BC
*
* https://github.com/harfbuzz/harfbuzz/issues/974
*/
map_.enableFeature(tt.NewTag('l', 'o', 'c', 'l'))
map_.enableFeature(tt.NewTag('c', 'c', 'm', 'p'))
i := 0
for ; i < khmerBasicFeatures; i++ {
map_.addFeatureExt(khmerFeatures[i].tag, khmerFeatures[i].flags, 1)
}
map_.addGSUBPause(clearSyllables)
for ; i < khmerNumFeatures; i++ {
map_.addFeatureExt(khmerFeatures[i].tag, khmerFeatures[i].flags, 1)
}
}
func (complexShaperKhmer) overrideFeatures(plan *otShapePlanner) {
map_ := &plan.map_
/* Khmer spec has 'clig' as part of required shaping features:
* "Apply feature 'clig' to form ligatures that are desired for
* typographical correctness.", hence in overrides... */
map_.enableFeature(tt.NewTag('c', 'l', 'i', 'g'))
/* Uniscribe does not apply 'kern' in Khmer. */
if UniscribeBugCompatible {
map_.disableFeature(tt.NewTag('k', 'e', 'r', 'n'))
}
map_.disableFeature(tt.NewTag('l', 'i', 'g', 'a'))
}
type khmerShapePlan struct {
viramaGlyph fonts.GID
maskArray [khmerNumFeatures]GlyphMask
}
func (cs *complexShaperKhmer) dataCreate(plan *otShapePlan) {
var khmerPlan khmerShapePlan
khmerPlan.viramaGlyph = ^fonts.GID(0)
for i := range khmerPlan.maskArray {
if khmerFeatures[i].flags&ffGLOBAL == 0 {
khmerPlan.maskArray[i] = plan.map_.getMask1(khmerFeatures[i].tag)
}
}
cs.plan = khmerPlan
}
func (cs *complexShaperKhmer) setupMasks(_ *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 {
setKhmerProperties(&info[i])
}
}
/* Note: This enum is duplicated in the -machine.rl source file.
* Not sure how to avoid duplication. */
const (
otRobatic = 20
otXgroup = 21
otYgroup = 22
)
func setKhmerProperties(info *GlyphInfo) {
u := info.codepoint
type_ := indicGetCategories(u)
cat := uint8(type_ & 0xFF)
pos := uint8(type_ >> 8)
/*
* Re-assign category
*
* These categories are experimentally extracted from what Uniscribe allows.
*/
switch u {
case 0x179A:
cat = otRa
case 0x17CC, 0x17C9, 0x17CA:
cat = otRobatic
case 0x17C6, 0x17CB, 0x17CD, 0x17CE, 0x17CF, 0x17D0, 0x17D1:
cat = otXgroup
case 0x17C7, 0x17C8, 0x17DD, 0x17D3: /* Just guessing. Uniscribe doesn't categorize it. */
cat = otYgroup
}
/*
* Re-assign position.
*/
if cat == otM {
switch pos {
case posPreC:
cat = otVPre
case posBelowC:
cat = otVBlw
case posAboveC:
cat = otVAbv
case posPostC:
cat = otVPst
}
}
info.complexCategory = cat
}
func setupSyllablesKhmer(_ *otShapePlan, _ *Font, buffer *Buffer) {
findSyllablesKhmer(buffer)
iter, count := buffer.syllableIterator()
for start, end := iter.next(); start < count; start, end = iter.next() {
buffer.unsafeToBreak(start, end)
}
}
func foundSyllableKhmer(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
}
}
/* Rules from:
* https://docs.microsoft.com/en-us/typography/script-development/devanagari */
func (khmerPlan *khmerShapePlan) reorderConsonantSyllable(buffer *Buffer, start, end int) {
info := buffer.Info
/* Setup masks. */
{
/* Post-base */
mask := khmerPlan.maskArray[khmerBlwf] |
khmerPlan.maskArray[khmerAbvf] |
khmerPlan.maskArray[khmerPstf]
for i := start + 1; i < end; i++ {
info[i].Mask |= mask
}
}
numCoengs := 0
for i := start + 1; i < end; i++ {
/* """
* When a COENG + (Cons | IndV) combination are found (and subscript count
* is less than two) the character combination is handled according to the
* subscript type of the character following the COENG.
*
* ...
*
* Subscript Type 2 - The COENG + RO characters are reordered to immediately
* before the base glyph. Then the COENG + RO characters are assigned to have
* the 'pref' OpenType feature applied to them.
* """
*/
if info[i].complexCategory == otCoeng && numCoengs <= 2 && i+1 < end {
numCoengs++
if info[i+1].complexCategory == otRa {
for j := 0; j < 2; j++ {
info[i+j].Mask |= khmerPlan.maskArray[khmerPref]
}
/* Move the Coeng,Ro sequence to the start. */
buffer.mergeClusters(start, i+2)
t0 := info[i]
t1 := info[i+1]
copy(info[start+2:], info[start:i])
info[start] = t0
info[start+1] = t1
/* Mark the subsequent stuff with 'cfar'. Used in Khmer.
* Read the feature spec.
* This allows distinguishing the following cases with MS Khmer fonts:
* U+1784,U+17D2,U+179A,U+17D2,U+1782
* U+1784,U+17D2,U+1782,U+17D2,U+179A
*/
if khmerPlan.maskArray[khmerCfar] != 0 {
for j := i + 2; j < end; j++ {
info[j].Mask |= khmerPlan.maskArray[khmerCfar]
}
}
numCoengs = 2 /* Done. */
}
} else if info[i].complexCategory == otVPre { /* Reorder left matra piece. */
/* Move to the start. */
buffer.mergeClusters(start, i+1)
t := info[i]
copy(info[start+1:], info[start:i])
info[start] = t
}
}
}
func (cs *complexShaperKhmer) reorderSyllableKhmer(buffer *Buffer, start, end int) {
syllableType := buffer.Info[start].syllable & 0x0F
switch syllableType {
case khmerBrokenCluster, /* We already inserted dotted-circles, so just call the consonant_syllable. */
khmerConsonantSyllable:
cs.plan.reorderConsonantSyllable(buffer, start, end)
}
}
func (cs *complexShaperKhmer) reorderKhmer(_ *otShapePlan, font *Font, buffer *Buffer) {
if debugMode >= 1 {
fmt.Println("KHMER - start reordering khmer")
}
syllabicInsertDottedCircles(font, buffer, khmerBrokenCluster, otDOTTEDCIRCLE, otRepha, -1)
iter, count := buffer.syllableIterator()
for start, end := iter.next(); start < count; start, end = iter.next() {
cs.reorderSyllableKhmer(buffer, start, end)
}
if debugMode >= 1 {
fmt.Println("KHMER - end reordering khmer")
}
}
func (complexShaperKhmer) decompose(c *otNormalizeContext, ab rune) (rune, rune, bool) {
switch ab {
/*
* Decompose split matras that don't have Unicode decompositions.
*/
/* Khmer */
case 0x17BE:
return 0x17C1, 0x17BE, true
case 0x17BF:
return 0x17C1, 0x17BF, true
case 0x17C0:
return 0x17C1, 0x17C0, true
case 0x17C4:
return 0x17C1, 0x17C4, true
case 0x17C5:
return 0x17C1, 0x17C5, true
}
return uni.decompose(ab)
}
func (complexShaperKhmer) compose(_ *otNormalizeContext, a, b rune) (rune, bool) {
/* Avoid recomposing split matras. */
if uni.generalCategory(a).isMark() {
return 0, false
}
return uni.compose(a, b)
}
func (complexShaperKhmer) marksBehavior() (zeroWidthMarks, bool) {
return zeroWidthMarksNone, false
}
func (complexShaperKhmer) normalizationPreference() normalizationMode {
return nmComposedDiacriticsNoShortCircuit
}
func (complexShaperKhmer) gposTag() tt.Tag { return 0 }
func (complexShaperKhmer) preprocessText(*otShapePlan, *Buffer, *Font) {}
func (complexShaperKhmer) postprocessGlyphs(*otShapePlan, *Buffer, *Font) {
}
func (complexShaperKhmer) reorderMarks(*otShapePlan, *Buffer, int, int) {}