2 * Copyright © 2010,2012 Google, Inc.
4 * This is part of HarfBuzz, a text shaping library.
6 * Permission is hereby granted, without written agreement and without
7 * license or royalty fees, to use, copy, modify, and distribute this
8 * software and its documentation for any purpose, provided that the
9 * above copyright notice and the following two paragraphs appear in
10 * all copies of this software.
12 * IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE TO ANY PARTY FOR
13 * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
14 * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN
15 * IF THE COPYRIGHT HOLDER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH
18 * THE COPYRIGHT HOLDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING,
19 * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
20 * FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS
21 * ON AN "AS IS" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO
22 * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
24 * Google Author(s): Behdad Esfahbod
27 #include "hb-ot-shape-complex-private.hh"
30 /* Thai / Lao shaper */
36 enum thai_consonant_type_t
43 NUM_CONSONANT_TYPES
= NOT_CONSONANT
46 static thai_consonant_type_t
47 get_consonant_type (hb_codepoint_t u
)
49 if (u
== 0x0E1B || u
== 0x0E1D || u
== 0x0E1F/* || u == 0x0E2C*/)
51 if (u
== 0x0E0D || u
== 0x0E10)
53 if (u
== 0x0E0E || u
== 0x0E0F)
55 if (hb_in_range
<hb_codepoint_t
> (u
, 0x0E01, 0x0E2E))
67 NUM_MARK_TYPES
= NOT_MARK
70 static thai_mark_type_t
71 get_mark_type (hb_codepoint_t u
)
73 if (u
== 0x0E31 || hb_in_range
<hb_codepoint_t
> (u
, 0x0E34, 0x0E37) ||
74 u
== 0x0E47 || hb_in_range
<hb_codepoint_t
> (u
, 0x0E4D, 0x0E4E))
76 if (hb_in_range
<hb_codepoint_t
> (u
, 0x0E38, 0x0E3A))
78 if (hb_in_range
<hb_codepoint_t
> (u
, 0x0E48, 0x0E4C))
87 SD
, /* Shift combining-mark down */
88 SL
, /* Shift combining-mark left */
89 SDL
, /* Shift combining-mark down-left */
90 RD
/* Remove descender from base */
94 thai_pua_shape (hb_codepoint_t u
, thai_action_t action
, hb_font_t
*font
)
96 struct thai_pua_mapping_t
{
98 hb_codepoint_t win_pua
;
99 hb_codepoint_t mac_pua
;
100 } const *pua_mappings
= NULL
;
101 static const thai_pua_mapping_t SD_mappings
[] = {
102 {0x0E48, 0xF70A, 0xF88B}, /* MAI EK */
103 {0x0E49, 0xF70B, 0xF88E}, /* MAI THO */
104 {0x0E4A, 0xF70C, 0xF891}, /* MAI TRI */
105 {0x0E4B, 0xF70D, 0xF894}, /* MAI CHATTAWA */
106 {0x0E4C, 0xF70E, 0xF897}, /* THANTHAKHAT */
107 {0x0E38, 0xF718, 0xF89B}, /* SARA U */
108 {0x0E39, 0xF719, 0xF89C}, /* SARA UU */
109 {0x0E3A, 0xF71A, 0xF89D}, /* PHINTHU */
110 {0x0000, 0x0000, 0x0000}
112 static const thai_pua_mapping_t SDL_mappings
[] = {
113 {0x0E48, 0xF705, 0xF88C}, /* MAI EK */
114 {0x0E49, 0xF706, 0xF88F}, /* MAI THO */
115 {0x0E4A, 0xF707, 0xF892}, /* MAI TRI */
116 {0x0E4B, 0xF708, 0xF895}, /* MAI CHATTAWA */
117 {0x0E4C, 0xF709, 0xF898}, /* THANTHAKHAT */
118 {0x0000, 0x0000, 0x0000}
120 static const thai_pua_mapping_t SL_mappings
[] = {
121 {0x0E48, 0xF713, 0xF88A}, /* MAI EK */
122 {0x0E49, 0xF714, 0xF88D}, /* MAI THO */
123 {0x0E4A, 0xF715, 0xF890}, /* MAI TRI */
124 {0x0E4B, 0xF716, 0xF893}, /* MAI CHATTAWA */
125 {0x0E4C, 0xF717, 0xF896}, /* THANTHAKHAT */
126 {0x0E31, 0xF710, 0xF884}, /* MAI HAN-AKAT */
127 {0x0E34, 0xF701, 0xF885}, /* SARA I */
128 {0x0E35, 0xF702, 0xF886}, /* SARA II */
129 {0x0E36, 0xF703, 0xF887}, /* SARA UE */
130 {0x0E37, 0xF704, 0xF888}, /* SARA UEE */
131 {0x0E47, 0xF712, 0xF889}, /* MAITAIKHU */
132 {0x0E4D, 0xF711, 0xF899}, /* NIKHAHIT */
133 {0x0000, 0x0000, 0x0000}
135 static const thai_pua_mapping_t RD_mappings
[] = {
136 {0x0E0D, 0xF70F, 0xF89A}, /* YO YING */
137 {0x0E10, 0xF700, 0xF89E}, /* THO THAN */
138 {0x0000, 0x0000, 0x0000}
142 default: assert (false); /* Fallthrough */
144 case SD
: pua_mappings
= SD_mappings
; break;
145 case SDL
: pua_mappings
= SDL_mappings
; break;
146 case SL
: pua_mappings
= SL_mappings
; break;
147 case RD
: pua_mappings
= RD_mappings
; break;
149 for (; pua_mappings
->u
; pua_mappings
++)
150 if (pua_mappings
->u
== u
)
152 hb_codepoint_t glyph
;
153 if (hb_font_get_glyph (font
, pua_mappings
->win_pua
, 0, &glyph
))
154 return pua_mappings
->win_pua
;
155 if (hb_font_get_glyph (font
, pua_mappings
->mac_pua
, 0, &glyph
))
156 return pua_mappings
->mac_pua
;
163 static enum thai_above_state_t
164 { /* Cluster above looks like: */
170 } thai_above_start_state
[NUM_CONSONANT_TYPES
+ 1/* For NOT_CONSONANT */] =
176 T3
, /* NOT_CONSONANT */
179 static const struct thai_above_state_machine_edge_t
{
180 thai_action_t action
;
181 thai_above_state_t next_state
;
182 } thai_above_state_machine
[NUM_ABOVE_STATES
][NUM_MARK_TYPES
] =
183 { /*AV*/ /*BV*/ /*T*/
184 /*T0*/ {{NOP
,T3
}, {NOP
,T0
}, {SD
, T3
}},
185 /*T1*/ {{SL
, T2
}, {NOP
,T1
}, {SDL
,T2
}},
186 /*T2*/ {{NOP
,T3
}, {NOP
,T2
}, {SL
, T3
}},
187 /*T3*/ {{NOP
,T3
}, {NOP
,T3
}, {NOP
,T3
}},
191 static enum thai_below_state_t
193 B0
, /* No descender */
194 B1
, /* Removable descender */
195 B2
, /* Strict descender */
197 } thai_below_start_state
[NUM_CONSONANT_TYPES
+ 1/* For NOT_CONSONANT */] =
203 B2
, /* NOT_CONSONANT */
206 static const struct thai_below_state_machine_edge_t
{
207 thai_action_t action
;
208 thai_below_state_t next_state
;
209 } thai_below_state_machine
[NUM_BELOW_STATES
][NUM_MARK_TYPES
] =
210 { /*AV*/ /*BV*/ /*T*/
211 /*B0*/ {{NOP
,B0
}, {NOP
,B2
}, {NOP
, B0
}},
212 /*B1*/ {{NOP
,B1
}, {RD
, B2
}, {NOP
, B1
}},
213 /*B2*/ {{NOP
,B2
}, {SD
, B2
}, {NOP
, B2
}},
218 do_thai_pua_shaping (const hb_ot_shape_plan_t
*plan HB_UNUSED
,
222 thai_above_state_t above_state
= thai_above_start_state
[NOT_CONSONANT
];
223 thai_below_state_t below_state
= thai_below_start_state
[NOT_CONSONANT
];
224 unsigned int base
= 0;
226 hb_glyph_info_t
*info
= buffer
->info
;
227 unsigned int count
= buffer
->len
;
228 for (unsigned int i
= 0; i
< count
; i
++)
230 thai_mark_type_t mt
= get_mark_type (info
[i
].codepoint
);
232 if (mt
== NOT_MARK
) {
233 thai_consonant_type_t ct
= get_consonant_type (info
[i
].codepoint
);
234 above_state
= thai_above_start_state
[ct
];
235 below_state
= thai_below_start_state
[ct
];
240 const thai_above_state_machine_edge_t
&above_edge
= thai_above_state_machine
[above_state
][mt
];
241 const thai_below_state_machine_edge_t
&below_edge
= thai_below_state_machine
[below_state
][mt
];
242 above_state
= above_edge
.next_state
;
243 below_state
= below_edge
.next_state
;
245 /* At least one of the above/below actions is NOP. */
246 thai_action_t action
= above_edge
.action
!= NOP
? above_edge
.action
: below_edge
.action
;
249 info
[base
].codepoint
= thai_pua_shape (info
[base
].codepoint
, action
, font
);
251 info
[i
].codepoint
= thai_pua_shape (info
[i
].codepoint
, action
, font
);
257 preprocess_text_thai (const hb_ot_shape_plan_t
*plan
,
261 /* This function implements the shaping logic documented here:
263 * http://linux.thai.net/~thep/th-otf/shaping.html
265 * The first shaping rule listed there is needed even if the font has Thai
266 * OpenType tables. The rest do fallback positioning based on PUA codepoints.
267 * We implement that only if there exist no Thai GSUB in the font.
270 /* The following is NOT specified in the MS OT Thai spec, however, it seems
271 * to be what Uniscribe and other engines implement. According to Eric Muller:
273 * When you have a SARA AM, decompose it in NIKHAHIT + SARA AA, *and* move the
274 * NIKHAHIT backwards over any tone mark (0E48-0E4B).
276 * <0E14, 0E4B, 0E33> -> <0E14, 0E4D, 0E4B, 0E32>
278 * This reordering is legit only when the NIKHAHIT comes from a SARA AM, not
279 * when it's there to start with. The string <0E14, 0E4B, 0E4D> is probably
280 * not what a user wanted, but the rendering is nevertheless nikhahit above
287 * Uniscribe also does some below-marks reordering. Namely, it positions U+0E3A
288 * after U+0E38 and U+0E39. We do that by modifying the ccc for U+0E3A.
289 * See unicode->modified_combining_class (). Lao does NOT have a U+0E3A
295 * Here are the characters of significance:
298 * SARA AM: U+0E33 U+0EB3
299 * SARA AA: U+0E32 U+0EB2
300 * Nikhahit: U+0E4D U+0ECD
302 * Testing shows that Uniscribe reorder the following marks:
303 * Thai: <0E31,0E34..0E37,0E47..0E4E>
304 * Lao: <0EB1,0EB4..0EB7,0EC7..0ECE>
306 * Note how the Lao versions are the same as Thai + 0x80.
309 /* We only get one script at a time, so a script-agnostic implementation
310 * is adequate here. */
311 #define IS_SARA_AM(x) (((x) & ~0x0080) == 0x0E33)
312 #define NIKHAHIT_FROM_SARA_AM(x) ((x) - 0xE33 + 0xE4D)
313 #define SARA_AA_FROM_SARA_AM(x) ((x) - 1)
314 #define IS_TONE_MARK(x) (hb_in_ranges<hb_codepoint_t> ((x) & ~0x0080, 0x0E34, 0x0E37, 0x0E47, 0x0E4E, 0x0E31, 0x0E31))
316 buffer
->clear_output ();
317 unsigned int count
= buffer
->len
;
318 for (buffer
->idx
= 0; buffer
->idx
< count
;)
320 hb_codepoint_t u
= buffer
->cur().codepoint
;
321 if (likely (!IS_SARA_AM (u
))) {
322 buffer
->next_glyph ();
326 /* Is SARA AM. Decompose and reorder. */
327 hb_codepoint_t decomposed
[2] = {hb_codepoint_t (NIKHAHIT_FROM_SARA_AM (u
)),
328 hb_codepoint_t (SARA_AA_FROM_SARA_AM (u
))};
329 buffer
->replace_glyphs (1, 2, decomposed
);
330 if (unlikely (buffer
->in_error
))
333 /* Ok, let's see... */
334 unsigned int end
= buffer
->out_len
;
335 unsigned int start
= end
- 2;
336 while (start
> 0 && IS_TONE_MARK (buffer
->out_info
[start
- 1].codepoint
))
341 /* Move Nikhahit (end-2) to the beginning */
342 buffer
->merge_out_clusters (start
, end
);
343 hb_glyph_info_t t
= buffer
->out_info
[end
- 2];
344 memmove (buffer
->out_info
+ start
+ 1,
345 buffer
->out_info
+ start
,
346 sizeof (buffer
->out_info
[0]) * (end
- start
- 2));
347 buffer
->out_info
[start
] = t
;
351 /* Since we decomposed, and NIKHAHIT is combining, merge clusters with the
352 * previous cluster. */
354 buffer
->merge_out_clusters (start
- 1, end
);
357 buffer
->swap_buffers ();
359 /* If font has Thai GSUB, we are done. */
360 if (plan
->props
.script
== HB_SCRIPT_THAI
&& !plan
->map
.found_script
[0])
361 do_thai_pua_shaping (plan
, buffer
, font
);
364 const hb_ot_complex_shaper_t _hb_ot_complex_shaper_thai
=
367 NULL
, /* collect_features */
368 NULL
, /* override_features */
369 NULL
, /* data_create */
370 NULL
, /* data_destroy */
371 preprocess_text_thai
,
372 NULL
, /* normalization_preference */
373 NULL
, /* decompose */
375 NULL
, /* setup_masks */
376 HB_OT_SHAPE_ZERO_WIDTH_MARKS_BY_UNICODE
,
377 false,/* fallback_position */