-
Notifications
You must be signed in to change notification settings - Fork 7
/
ot_thai.go
353 lines (316 loc) · 9.08 KB
/
ot_thai.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
package harfbuzz
import (
"github.com/benoitkugler/textlayout/language"
)
// ported from harfbuzz/src/hb-ot-shape-complex-thai.cc Copyright © 2010,2012 Google, Inc. Behdad Esfahbod
/* Thai / Lao shaper */
var _ otComplexShaper = complexShaperThai{}
type complexShaperThai struct {
complexShaperNil
}
/* PUA shaping */
// thai_consonant_type_t
const (
tcNC = iota
tcAC
tcRC
tcDC
tcNOTCONSONANT
numConsonantTypes = tcNOTCONSONANT
)
func getConsonantType(u rune) uint8 {
switch u {
case 0x0E1B, 0x0E1D, 0x0E1F /* , 0x0E2C*/ :
return tcAC
case 0x0E0D, 0x0E10:
return tcRC
case 0x0E0E, 0x0E0F:
return tcDC
}
if 0x0E01 <= u && u <= 0x0E2E {
return tcNC
}
return tcNOTCONSONANT
}
// thai_mark_type_t
const (
tmAV = iota
tmBV
tmT
tmNOTMARK
numMarkTypes = tmNOTMARK
)
func getMarkType(u rune) uint8 {
if u == 0x0E31 || (0x0E34 <= u && u <= 0x0E37) ||
u == 0x0E47 || (0x0E4D <= u && u <= 0x0E4E) {
return tmAV
}
if 0x0E38 <= u && u <= 0x0E3A {
return tmBV
}
if 0x0E48 <= u && u <= 0x0E4C {
return tmT
}
return tmNOTMARK
}
// thai_action_t
const (
tcNOP = iota
tcSD /* Shift combining-mark down */
tcSL /* Shift combining-mark left */
tcSDL /* Shift combining-mark down-left */
tcRD /* Remove descender from base */
)
type thaiPuaMapping struct {
u, winPua, macPua rune
}
var (
sdMappings = [...]thaiPuaMapping{
{0x0E48, 0xF70A, 0xF88B}, /* MAI EK */
{0x0E49, 0xF70B, 0xF88E}, /* MAI THO */
{0x0E4A, 0xF70C, 0xF891}, /* MAI TRI */
{0x0E4B, 0xF70D, 0xF894}, /* MAI CHATTAWA */
{0x0E4C, 0xF70E, 0xF897}, /* THANTHAKHAT */
{0x0E38, 0xF718, 0xF89B}, /* SARA U */
{0x0E39, 0xF719, 0xF89C}, /* SARA UU */
{0x0E3A, 0xF71A, 0xF89D}, /* PHINTHU */
{0x0000, 0x0000, 0x0000},
}
sdlMappings = [...]thaiPuaMapping{
{0x0E48, 0xF705, 0xF88C}, /* MAI EK */
{0x0E49, 0xF706, 0xF88F}, /* MAI THO */
{0x0E4A, 0xF707, 0xF892}, /* MAI TRI */
{0x0E4B, 0xF708, 0xF895}, /* MAI CHATTAWA */
{0x0E4C, 0xF709, 0xF898}, /* THANTHAKHAT */
{0x0000, 0x0000, 0x0000},
}
slMappings = [...]thaiPuaMapping{
{0x0E48, 0xF713, 0xF88A}, /* MAI EK */
{0x0E49, 0xF714, 0xF88D}, /* MAI THO */
{0x0E4A, 0xF715, 0xF890}, /* MAI TRI */
{0x0E4B, 0xF716, 0xF893}, /* MAI CHATTAWA */
{0x0E4C, 0xF717, 0xF896}, /* THANTHAKHAT */
{0x0E31, 0xF710, 0xF884}, /* MAI HAN-AKAT */
{0x0E34, 0xF701, 0xF885}, /* SARA I */
{0x0E35, 0xF702, 0xF886}, /* SARA II */
{0x0E36, 0xF703, 0xF887}, /* SARA UE */
{0x0E37, 0xF704, 0xF888}, /* SARA UEE */
{0x0E47, 0xF712, 0xF889}, /* MAITAIKHU */
{0x0E4D, 0xF711, 0xF899}, /* NIKHAHIT */
{0x0000, 0x0000, 0x0000},
}
rdMappings = [...]thaiPuaMapping{
{0x0E0D, 0xF70F, 0xF89A}, /* YO YING */
{0x0E10, 0xF700, 0xF89E}, /* THO THAN */
{0x0000, 0x0000, 0x0000},
}
)
func thaiPuaShape(u rune, action uint8, font *Font) rune {
var puaMappings []thaiPuaMapping
switch action {
case tcNOP:
return u
case tcSD:
puaMappings = sdMappings[:]
case tcSDL:
puaMappings = sdlMappings[:]
case tcSL:
puaMappings = slMappings[:]
case tcRD:
puaMappings = rdMappings[:]
}
for _, pua := range puaMappings {
if pua.u == u {
_, ok := font.face.NominalGlyph(pua.winPua)
if ok {
return pua.winPua
}
_, ok = font.face.NominalGlyph(pua.macPua)
if ok {
return pua.macPua
}
break
}
}
return u
}
const (
/* Cluster above looks like: */
tcT0 = iota /* ⣤ */
tcT1 /* ⣼ */
tcT2 /* ⣾ */
tcT3 /* ⣿ */
numAboveStates
)
var thaiAboveStartState = [numConsonantTypes + 1] /* For NOT_CONSONANT */ uint8{
tcT0, /* NC */
tcT1, /* AC */
tcT0, /* RC */
tcT0, /* DC */
tcT3, /* NOT_CONSONANT */
}
var thaiAboveStateMachine = [numAboveStates][numMarkTypes]struct {
action uint8
nextState uint8
}{ /*AV*/ /*BV*/ /*T*/
/*T0*/ {{tcNOP, tcT3}, {tcNOP, tcT0}, {tcSD, tcT3}},
/*T1*/ {{tcSL, tcT2}, {tcNOP, tcT1}, {tcSDL, tcT2}},
/*T2*/ {{tcNOP, tcT3}, {tcNOP, tcT2}, {tcSL, tcT3}},
/*T3*/ {{tcNOP, tcT3}, {tcNOP, tcT3}, {tcNOP, tcT3}},
}
// thai_below_state_t
const (
tbB0 = iota /* No descender */
tbB1 /* Removable descender */
tbB2 /* Strict descender */
numBelowStates
)
var thaiBelowStartState = [numConsonantTypes + 1] /* For NOT_CONSONANT */ uint8{
tbB0, /* NC */
tbB0, /* AC */
tbB1, /* RC */
tbB2, /* DC */
tbB2, /* NOT_CONSONANT */
}
var thaiBelowStateMachine = [numBelowStates][numMarkTypes]struct {
action uint8
nextState uint8
}{ /*AV*/ /*BV*/ /*T*/
/*B0*/ {{tcNOP, tbB0}, {tcNOP, tbB2}, {tcNOP, tbB0}},
/*B1*/ {{tcNOP, tbB1}, {tcRD, tbB2}, {tcNOP, tbB1}},
/*B2*/ {{tcNOP, tbB2}, {tcSD, tbB2}, {tcNOP, tbB2}},
}
func doThaiPuaShaping(buffer *Buffer, font *Font) {
aboveState := thaiAboveStartState[tcNOTCONSONANT]
belowState := thaiBelowStartState[tcNOTCONSONANT]
base := 0
info := buffer.Info
// unsigned int count = buffer.len;
for i := range info {
mt := getMarkType(info[i].codepoint)
if mt == tmNOTMARK {
ct := getConsonantType(info[i].codepoint)
aboveState = thaiAboveStartState[ct]
belowState = thaiBelowStartState[ct]
base = i
continue
}
aboveEdge := &thaiAboveStateMachine[aboveState][mt]
belowEdge := &thaiBelowStateMachine[belowState][mt]
aboveState = aboveEdge.nextState
belowState = belowEdge.nextState
// at least one of the above/below actions is NOP.
action := belowEdge.action
if aboveEdge.action != tcNOP {
action = aboveEdge.action
}
buffer.unsafeToBreak(base, i)
if action == tcRD {
info[base].codepoint = thaiPuaShape(info[base].codepoint, action, font)
} else {
info[i].codepoint = thaiPuaShape(info[i].codepoint, action, font)
}
}
}
/* We only get one script at a time, so a script-agnostic implementation
* is adequate here. */
func isSaraAm(x rune) bool { return x & ^0x0080 == 0x0E33 }
func nikhahitFromSaraAm(x rune) rune { return x - 0x0E33 + 0x0E4D }
func saraAaFromSaraAm(x rune) rune { return x - 1 }
func isToneMark(x rune) bool {
u := x & ^0x0080
return 0x0E34 <= u && u <= 0x0E37 ||
0x0E47 <= u && u <= 0x0E4E ||
0x0E31 <= u && u <= 0x0E31
}
/* This function implements the shaping logic documented here:
*
* https://linux.thai.net/~thep/th-otf/shaping.html
*
* The first shaping rule listed there is needed even if the font has Thai
* OpenType tables. The rest do fallback positioning based on PUA codepoints.
* We implement that only if there exist no Thai GSUB in the font.
*/
func (complexShaperThai) preprocessText(plan *otShapePlan, buffer *Buffer, font *Font) {
/* The following is NOT specified in the MS OT Thai spec, however, it seems
* to be what Uniscribe and other engines implement. According to Eric Muller:
*
* When you have a SARA AM, decompose it in NIKHAHIT + SARA AA, *and* move the
* NIKHAHIT backwards over any tone mark (0E48-0E4B).
*
* <0E14, 0E4B, 0E33> . <0E14, 0E4D, 0E4B, 0E32>
*
* This reordering is legit only when the NIKHAHIT comes from a SARA AM, not
* when it's there to start with. The string <0E14, 0E4B, 0E4D> is probably
* not what a user wanted, but the rendering is nevertheless nikhahit above
* chattawa.
*
* Same for Lao.
*
* Note:
*
* Uniscribe also does some below-marks reordering. Namely, it positions U+0E3A
* after U+0E38 and U+0E39. We do that by modifying the ccc for U+0E3A.
* See unicode.modified_combining_class (). Lao does NOT have a U+0E3A
* equivalent.
*/
/*
* Here are the characters of significance:
*
* Thai Lao
* SARA AM: U+0E33 U+0EB3
* SARA AA: U+0E32 U+0EB2
* Nikhahit: U+0E4D U+0ECD
*
* Testing shows that Uniscribe reorder the following marks:
* Thai: <0E31,0E34..0E37,0E47..0E4E>
* Lao: <0EB1,0EB4..0EB7,0EC7..0ECE>
*
* Note how the Lao versions are the same as Thai + 0x80.
*/
buffer.clearOutput()
count := len(buffer.Info)
for buffer.idx = 0; buffer.idx < count; {
u := buffer.cur(0).codepoint
if !isSaraAm(u) {
buffer.nextGlyph()
continue
}
/* Is SARA AM. Decompose and reorder. */
buffer.outputRune(nikhahitFromSaraAm(u))
buffer.prev().setContinuation()
buffer.replaceGlyph(saraAaFromSaraAm(u))
/* Make Nikhahit be recognized as a ccc=0 mark when zeroing widths. */
end := len(buffer.outInfo)
buffer.outInfo[end-2].setGeneralCategory(nonSpacingMark)
/* Ok, let's see... */
start := end - 2
for start > 0 && isToneMark(buffer.outInfo[start-1].codepoint) {
start--
}
if start+2 < end {
/* Move Nikhahit (end-2) to the beginning */
buffer.mergeOutClusters(start, end)
t := buffer.outInfo[end-2]
copy(buffer.outInfo[start+1:], buffer.outInfo[start:end-2])
buffer.outInfo[start] = t
} else {
/* Since we decomposed, and NIKHAHIT is combining, merge clusters with the
* previous cluster. */
if start != 0 && buffer.ClusterLevel == MonotoneGraphemes {
buffer.mergeOutClusters(start-1, end)
}
}
}
buffer.swapBuffers()
/* If font has Thai GSUB, we are done. */
if plan.props.Script == language.Thai && !plan.map_.foundScript[0] {
doThaiPuaShaping(buffer, font)
}
}
func (complexShaperThai) marksBehavior() (zeroWidthMarks, bool) {
return zeroWidthMarksByGdefLate, false
}
func (complexShaperThai) normalizationPreference() normalizationMode {
return nmDefault
}