blob: 29f29bb556bc9551a863c41e11ce29242b743d4d [file] [log] [blame]
Behdad Esfahbod98628ca2013-02-11 13:36:23 -05001/*
2 * Copyright © 2011,2012,2013 Google, Inc.
3 *
4 * This is part of HarfBuzz, a text shaping library.
5 *
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.
11 *
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
16 * DAMAGE.
17 *
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.
23 *
24 * Google Author(s): Behdad Esfahbod
25 */
26
Behdad Esfahbodf9b66052013-02-12 16:13:56 -050027#include "hb-ot-shape-complex-indic-private.hh"
Behdad Esfahbod98628ca2013-02-11 13:36:23 -050028
29/* buffer var allocations */
30#define myanmar_category() complex_var_u8_0() /* myanmar_category_t */
Behdad Esfahbod3a83d332013-02-12 12:14:10 -050031#define myanmar_position() complex_var_u8_1() /* myanmar_position_t */
Behdad Esfahbod98628ca2013-02-11 13:36:23 -050032
33
34/*
35 * Myanmar shaper.
36 */
37
38static const hb_tag_t
Behdad Esfahbodee9c3a12013-02-15 06:22:26 -050039basic_features[] =
Behdad Esfahbod98628ca2013-02-11 13:36:23 -050040{
41 /*
42 * Basic features.
43 * These features are applied in order, one at a time, after initial_reordering.
44 */
45 HB_TAG('r','p','h','f'),
46 HB_TAG('p','r','e','f'),
47 HB_TAG('b','l','w','f'),
48 HB_TAG('p','s','t','f'),
Behdad Esfahbodee9c3a12013-02-15 06:22:26 -050049};
50static const hb_tag_t
51other_features[] =
52{
Behdad Esfahbod98628ca2013-02-11 13:36:23 -050053 /*
54 * Other features.
55 * These features are applied all at once, after final_reordering.
56 */
57 HB_TAG('p','r','e','s'),
58 HB_TAG('a','b','v','s'),
59 HB_TAG('b','l','w','s'),
60 HB_TAG('p','s','t','s'),
61 /* Positioning features, though we don't care about the types. */
62 HB_TAG('d','i','s','t'),
63};
64
Behdad Esfahbod98628ca2013-02-11 13:36:23 -050065static void
66setup_syllables (const hb_ot_shape_plan_t *plan,
67 hb_font_t *font,
68 hb_buffer_t *buffer);
69static void
70initial_reordering (const hb_ot_shape_plan_t *plan,
71 hb_font_t *font,
72 hb_buffer_t *buffer);
73static void
74final_reordering (const hb_ot_shape_plan_t *plan,
75 hb_font_t *font,
76 hb_buffer_t *buffer);
77
78static void
79collect_features_myanmar (hb_ot_shape_planner_t *plan)
80{
81 hb_ot_map_builder_t *map = &plan->map;
82
83 /* Do this before any lookups have been applied. */
84 map->add_gsub_pause (setup_syllables);
85
Behdad Esfahbode7ffcfa2013-02-14 11:05:56 -050086 map->add_global_bool_feature (HB_TAG('l','o','c','l'));
Behdad Esfahbod98628ca2013-02-11 13:36:23 -050087 /* The Indic specs do not require ccmp, but we apply it here since if
88 * there is a use of it, it's typically at the beginning. */
Behdad Esfahbode7ffcfa2013-02-14 11:05:56 -050089 map->add_global_bool_feature (HB_TAG('c','c','m','p'));
Behdad Esfahbod98628ca2013-02-11 13:36:23 -050090
91
Behdad Esfahbod98628ca2013-02-11 13:36:23 -050092 map->add_gsub_pause (initial_reordering);
Behdad Esfahbodee9c3a12013-02-15 06:22:26 -050093 for (unsigned int i = 0; i < ARRAY_LENGTH (basic_features); i++)
94 {
Behdad Esfahboda8cf7b42013-03-19 05:53:26 -040095 map->add_feature (basic_features[i], 1, F_GLOBAL | F_MANUAL_ZWJ);
Behdad Esfahbod98628ca2013-02-11 13:36:23 -050096 map->add_gsub_pause (NULL);
97 }
98 map->add_gsub_pause (final_reordering);
Behdad Esfahbodee9c3a12013-02-15 06:22:26 -050099 for (unsigned int i = 0; i < ARRAY_LENGTH (other_features); i++)
Behdad Esfahboda8cf7b42013-03-19 05:53:26 -0400100 map->add_feature (other_features[i], 1, F_GLOBAL | F_MANUAL_ZWJ);
Behdad Esfahbod98628ca2013-02-11 13:36:23 -0500101}
102
103static void
104override_features_myanmar (hb_ot_shape_planner_t *plan)
105{
Behdad Esfahbodec544862013-02-14 11:25:10 -0500106 plan->map.add_feature (HB_TAG('l','i','g','a'), 0, F_GLOBAL);
Behdad Esfahbod98628ca2013-02-11 13:36:23 -0500107}
108
109
110enum syllable_type_t {
111 consonant_syllable,
112 broken_cluster,
113 non_myanmar_cluster,
114};
115
116#include "hb-ot-shape-complex-myanmar-machine.hh"
117
118
119/* Note: This enum is duplicated in the -machine.rl source file.
120 * Not sure how to avoid duplication. */
121enum myanmar_category_t {
Behdad Esfahbod98628ca2013-02-11 13:36:23 -0500122 OT_As = 18, /* Asat */
123 OT_D = 19, /* Digits except zero */
124 OT_D0 = 20, /* Digit zero */
125 OT_DB = OT_N, /* Dot below */
Behdad Esfahbodf9b66052013-02-12 16:13:56 -0500126 OT_GB = OT_DOTTEDCIRCLE,
Behdad Esfahbod98628ca2013-02-11 13:36:23 -0500127 OT_MH = 21, /* Various consonant medial types */
128 OT_MR = 22, /* Various consonant medial types */
129 OT_MW = 23, /* Various consonant medial types */
130 OT_MY = 24, /* Various consonant medial types */
131 OT_PT = 25, /* Pwo and other tones */
132 OT_VAbv = 26,
133 OT_VBlw = 27,
134 OT_VPre = 28,
135 OT_VPst = 29,
136 OT_VS = 30 /* Variation selectors */
137};
138
Behdad Esfahbod98628ca2013-02-11 13:36:23 -0500139
140static inline bool
141is_one_of (const hb_glyph_info_t &info, unsigned int flags)
142{
143 /* If it ligated, all bets are off. */
144 if (is_a_ligature (info)) return false;
145 return !!(FLAG (info.myanmar_category()) & flags);
146}
147
148/* Note:
149 *
150 * We treat Vowels and placeholders as if they were consonants. This is safe because Vowels
151 * cannot happen in a consonant syllable. The plus side however is, we can call the
152 * consonant syllable logic from the vowel syllable function and get it all right! */
Behdad Esfahbod0572c142013-02-11 16:06:02 -0500153#define CONSONANT_FLAGS (FLAG (OT_C) | FLAG (OT_CM) | FLAG (OT_Ra) | FLAG (OT_V) | FLAG (OT_NBSP) | FLAG (OT_GB))
Behdad Esfahbod98628ca2013-02-11 13:36:23 -0500154static inline bool
155is_consonant (const hb_glyph_info_t &info)
156{
157 return is_one_of (info, CONSONANT_FLAGS);
158}
159
160
161static inline void
162set_myanmar_properties (hb_glyph_info_t &info)
163{
164 hb_codepoint_t u = info.codepoint;
Behdad Esfahbodf9b66052013-02-12 16:13:56 -0500165 unsigned int type = hb_indic_get_categories (u);
166 indic_category_t cat = (indic_category_t) (type & 0x7F);
167 indic_position_t pos = (indic_position_t) (type >> 8);
Behdad Esfahbod98628ca2013-02-11 13:36:23 -0500168
169 /* Myanmar
170 * http://www.microsoft.com/typography/OpenTypeDev/myanmar/intro.htm#analyze
171 */
172 if (unlikely (hb_in_range<hb_codepoint_t> (u, 0xFE00, 0xFE0F)))
Behdad Esfahbodf9b66052013-02-12 16:13:56 -0500173 cat = (indic_category_t) OT_VS;
174 else if (unlikely (u == 0x200C)) cat = (indic_category_t) OT_ZWNJ;
175 else if (unlikely (u == 0x200D)) cat = (indic_category_t) OT_ZWJ;
176
Behdad Esfahbod98628ca2013-02-11 13:36:23 -0500177 switch (u)
178 {
Behdad Esfahbod0572c142013-02-11 16:06:02 -0500179 case 0x002D: case 0x00A0: case 0x00D7: case 0x2012:
180 case 0x2013: case 0x2014: case 0x2015: case 0x2022:
181 case 0x25CC: case 0x25FB: case 0x25FC: case 0x25FD:
182 case 0x25FE:
Behdad Esfahbodf9b66052013-02-12 16:13:56 -0500183 cat = (indic_category_t) OT_GB;
Behdad Esfahbod0572c142013-02-11 16:06:02 -0500184 break;
185
Behdad Esfahbod98628ca2013-02-11 13:36:23 -0500186 case 0x1004: case 0x101B: case 0x105A:
Behdad Esfahbodf9b66052013-02-12 16:13:56 -0500187 cat = (indic_category_t) OT_Ra;
Behdad Esfahbod98628ca2013-02-11 13:36:23 -0500188 break;
189
190 case 0x1032: case 0x1036:
Behdad Esfahbodf9b66052013-02-12 16:13:56 -0500191 cat = (indic_category_t) OT_A;
Behdad Esfahbod98628ca2013-02-11 13:36:23 -0500192 break;
193
194 case 0x103A:
Behdad Esfahbodf9b66052013-02-12 16:13:56 -0500195 cat = (indic_category_t) OT_As;
Behdad Esfahbod98628ca2013-02-11 13:36:23 -0500196 break;
197
198 case 0x1041: case 0x1042: case 0x1043: case 0x1044:
199 case 0x1045: case 0x1046: case 0x1047: case 0x1048:
200 case 0x1049: case 0x1090: case 0x1091: case 0x1092:
201 case 0x1093: case 0x1094: case 0x1095: case 0x1096:
202 case 0x1097: case 0x1098: case 0x1099:
Behdad Esfahbodf9b66052013-02-12 16:13:56 -0500203 cat = (indic_category_t) OT_D;
Behdad Esfahbod98628ca2013-02-11 13:36:23 -0500204 break;
205
206 case 0x1040:
Behdad Esfahbodf9b66052013-02-12 16:13:56 -0500207 cat = (indic_category_t) OT_D; /* XXX The spec says D0, but Uniscribe doesn't seem to do. */
Behdad Esfahbod98628ca2013-02-11 13:36:23 -0500208 break;
209
210 case 0x103E: case 0x1060:
Behdad Esfahbodf9b66052013-02-12 16:13:56 -0500211 cat = (indic_category_t) OT_MH;
Behdad Esfahbod98628ca2013-02-11 13:36:23 -0500212 break;
213
214 case 0x103C:
Behdad Esfahbodf9b66052013-02-12 16:13:56 -0500215 cat = (indic_category_t) OT_MR;
Behdad Esfahbod98628ca2013-02-11 13:36:23 -0500216 break;
217
218 case 0x103D: case 0x1082:
Behdad Esfahbodf9b66052013-02-12 16:13:56 -0500219 cat = (indic_category_t) OT_MW;
Behdad Esfahbod98628ca2013-02-11 13:36:23 -0500220 break;
221
222 case 0x103B: case 0x105E: case 0x105F:
Behdad Esfahbodf9b66052013-02-12 16:13:56 -0500223 cat = (indic_category_t) OT_MY;
Behdad Esfahbod98628ca2013-02-11 13:36:23 -0500224 break;
225
226 case 0x1063: case 0x1064: case 0x1069: case 0x106A:
227 case 0x106B: case 0x106C: case 0x106D: case 0xAA7B:
Behdad Esfahbodf9b66052013-02-12 16:13:56 -0500228 cat = (indic_category_t) OT_PT;
Behdad Esfahbod98628ca2013-02-11 13:36:23 -0500229 break;
230
231 case 0x1038: case 0x1087: case 0x1088: case 0x1089:
232 case 0x108A: case 0x108B: case 0x108C: case 0x108D:
233 case 0x108F: case 0x109A: case 0x109B: case 0x109C:
Behdad Esfahbodf9b66052013-02-12 16:13:56 -0500234 cat = (indic_category_t) OT_SM;
Behdad Esfahbod98628ca2013-02-11 13:36:23 -0500235 break;
236 }
237
Behdad Esfahbodf9b66052013-02-12 16:13:56 -0500238 if (cat == OT_M)
239 {
240 switch ((int) pos)
241 {
242 case POS_PRE_C: cat = (indic_category_t) OT_VPre;
243 pos = POS_PRE_M; break;
244 case POS_ABOVE_C: cat = (indic_category_t) OT_VAbv; break;
245 case POS_BELOW_C: cat = (indic_category_t) OT_VBlw; break;
246 case POS_POST_C: cat = (indic_category_t) OT_VPst; break;
247 }
248 }
249
250 info.myanmar_category() = (myanmar_category_t) cat;
Behdad Esfahbod98628ca2013-02-11 13:36:23 -0500251 info.myanmar_position() = pos;
252}
253
254
255
256static void
257setup_masks_myanmar (const hb_ot_shape_plan_t *plan HB_UNUSED,
258 hb_buffer_t *buffer,
259 hb_font_t *font HB_UNUSED)
260{
261 HB_BUFFER_ALLOCATE_VAR (buffer, myanmar_category);
262 HB_BUFFER_ALLOCATE_VAR (buffer, myanmar_position);
263
264 /* We cannot setup masks here. We save information about characters
265 * and setup masks later on in a pause-callback. */
266
267 unsigned int count = buffer->len;
268 for (unsigned int i = 0; i < count; i++)
269 set_myanmar_properties (buffer->info[i]);
270}
271
272static void
273setup_syllables (const hb_ot_shape_plan_t *plan HB_UNUSED,
274 hb_font_t *font HB_UNUSED,
275 hb_buffer_t *buffer)
276{
277 find_syllables (buffer);
278}
279
280static int
281compare_myanmar_order (const hb_glyph_info_t *pa, const hb_glyph_info_t *pb)
282{
283 int a = pa->myanmar_position();
284 int b = pb->myanmar_position();
285
286 return a < b ? -1 : a == b ? 0 : +1;
287}
288
289
290/* Rules from:
291 * http://www.microsoft.com/typography/OpenTypeDev/myanmar/intro.htm */
292
293static void
294initial_reordering_consonant_syllable (const hb_ot_shape_plan_t *plan,
295 hb_face_t *face,
296 hb_buffer_t *buffer,
297 unsigned int start, unsigned int end)
298{
299 hb_glyph_info_t *info = buffer->info;
300
301 unsigned int base = end;
302 bool has_reph = false;
303
304 {
305 unsigned int limit = start;
306 if (start + 3 <= end &&
307 info[start ].myanmar_category() == OT_Ra &&
308 info[start+1].myanmar_category() == OT_As &&
309 info[start+2].myanmar_category() == OT_H)
310 {
311 limit += 3;
312 base = start;
313 has_reph = true;
314 }
315
316 {
317 if (!has_reph)
318 base = limit;
319
320 for (unsigned int i = limit; i < end; i++)
321 if (is_consonant (info[i]))
322 {
323 base = i;
324 break;
325 }
326 }
327 }
328
329 /* Reorder! */
330 {
331 unsigned int i = start;
332 for (; i < start + (has_reph ? 3 : 0); i++)
333 info[i].myanmar_position() = POS_AFTER_MAIN;
334 for (; i < base; i++)
335 info[i].myanmar_position() = POS_PRE_C;
336 if (i < end)
337 {
338 info[i].myanmar_position() = POS_BASE_C;
339 i++;
340 }
Behdad Esfahbodf9b66052013-02-12 16:13:56 -0500341 indic_position_t pos = POS_AFTER_MAIN;
Behdad Esfahbod98628ca2013-02-11 13:36:23 -0500342 /* The following loop may be ugly, but it implements all of
343 * Myanmar reordering! */
344 for (; i < end; i++)
345 {
346 if (info[i].myanmar_category() == OT_MR) /* Pre-base reordering */
347 {
348 info[i].myanmar_position() = POS_PRE_C;
349 continue;
350 }
351 if (info[i].myanmar_position() < POS_BASE_C) /* Left matra */
352 {
353 continue;
354 }
355
356 if (pos == POS_AFTER_MAIN && info[i].myanmar_category() == OT_VBlw)
357 {
358 pos = POS_BELOW_C;
359 info[i].myanmar_position() = pos;
360 continue;
361 }
362
363 if (pos == POS_BELOW_C && info[i].myanmar_category() == OT_A)
364 {
365 info[i].myanmar_position() = POS_BEFORE_SUB;
366 continue;
367 }
368 if (pos == POS_BELOW_C && info[i].myanmar_category() == OT_VBlw)
369 {
370 info[i].myanmar_position() = pos;
371 continue;
372 }
373 if (pos == POS_BELOW_C && info[i].myanmar_category() != OT_A)
374 {
375 pos = POS_AFTER_SUB;
376 info[i].myanmar_position() = pos;
377 continue;
378 }
379 info[i].myanmar_position() = pos;
380 }
381 }
382
383 buffer->merge_clusters (start, end);
384 /* Sit tight, rock 'n roll! */
385 hb_bubble_sort (info + start, end - start, compare_myanmar_order);
386}
387
388static void
389initial_reordering_broken_cluster (const hb_ot_shape_plan_t *plan,
390 hb_face_t *face,
391 hb_buffer_t *buffer,
392 unsigned int start, unsigned int end)
393{
394 /* We already inserted dotted-circles, so just call the consonant_syllable. */
395 initial_reordering_consonant_syllable (plan, face, buffer, start, end);
396}
397
398static void
399initial_reordering_non_myanmar_cluster (const hb_ot_shape_plan_t *plan HB_UNUSED,
400 hb_face_t *face HB_UNUSED,
401 hb_buffer_t *buffer HB_UNUSED,
402 unsigned int start HB_UNUSED, unsigned int end HB_UNUSED)
403{
404 /* Nothing to do right now. If we ever switch to using the output
405 * buffer in the reordering process, we'd need to next_glyph() here. */
406}
407
408
409static void
410initial_reordering_syllable (const hb_ot_shape_plan_t *plan,
411 hb_face_t *face,
412 hb_buffer_t *buffer,
413 unsigned int start, unsigned int end)
414{
415 syllable_type_t syllable_type = (syllable_type_t) (buffer->info[start].syllable() & 0x0F);
416 switch (syllable_type) {
417 case consonant_syllable: initial_reordering_consonant_syllable (plan, face, buffer, start, end); return;
418 case broken_cluster: initial_reordering_broken_cluster (plan, face, buffer, start, end); return;
419 case non_myanmar_cluster: initial_reordering_non_myanmar_cluster (plan, face, buffer, start, end); return;
420 }
421}
422
423static inline void
424insert_dotted_circles (const hb_ot_shape_plan_t *plan HB_UNUSED,
425 hb_font_t *font,
426 hb_buffer_t *buffer)
427{
428 /* Note: This loop is extra overhead, but should not be measurable. */
429 bool has_broken_syllables = false;
430 unsigned int count = buffer->len;
431 for (unsigned int i = 0; i < count; i++)
432 if ((buffer->info[i].syllable() & 0x0F) == broken_cluster) {
433 has_broken_syllables = true;
434 break;
435 }
436 if (likely (!has_broken_syllables))
437 return;
438
439
440 hb_codepoint_t dottedcircle_glyph;
441 if (!font->get_glyph (0x25CC, 0, &dottedcircle_glyph))
442 return;
443
444 hb_glyph_info_t dottedcircle = {0};
445 dottedcircle.codepoint = 0x25CC;
446 set_myanmar_properties (dottedcircle);
447 dottedcircle.codepoint = dottedcircle_glyph;
448
449 buffer->clear_output ();
450
451 buffer->idx = 0;
452 unsigned int last_syllable = 0;
453 while (buffer->idx < buffer->len)
454 {
455 unsigned int syllable = buffer->cur().syllable();
456 syllable_type_t syllable_type = (syllable_type_t) (syllable & 0x0F);
457 if (unlikely (last_syllable != syllable && syllable_type == broken_cluster))
458 {
459 last_syllable = syllable;
460
461 hb_glyph_info_t info = dottedcircle;
462 info.cluster = buffer->cur().cluster;
463 info.mask = buffer->cur().mask;
464 info.syllable() = buffer->cur().syllable();
465
466 buffer->output_info (info);
467 }
468 else
469 buffer->next_glyph ();
470 }
471
472 buffer->swap_buffers ();
473}
474
475static void
476initial_reordering (const hb_ot_shape_plan_t *plan,
477 hb_font_t *font,
478 hb_buffer_t *buffer)
479{
480 insert_dotted_circles (plan, font, buffer);
481
482 hb_glyph_info_t *info = buffer->info;
483 unsigned int count = buffer->len;
484 if (unlikely (!count)) return;
485 unsigned int last = 0;
486 unsigned int last_syllable = info[0].syllable();
487 for (unsigned int i = 1; i < count; i++)
488 if (last_syllable != info[i].syllable()) {
489 initial_reordering_syllable (plan, font->face, buffer, last, i);
490 last = i;
491 last_syllable = info[last].syllable();
492 }
493 initial_reordering_syllable (plan, font->face, buffer, last, count);
494}
495
496static void
497final_reordering (const hb_ot_shape_plan_t *plan,
498 hb_font_t *font HB_UNUSED,
499 hb_buffer_t *buffer)
500{
501 hb_glyph_info_t *info = buffer->info;
502 unsigned int count = buffer->len;
503
504 /* Zero syllables now... */
505 for (unsigned int i = 0; i < count; i++)
506 info[i].syllable() = 0;
507
508 HB_BUFFER_DEALLOCATE_VAR (buffer, myanmar_category);
509 HB_BUFFER_DEALLOCATE_VAR (buffer, myanmar_position);
510}
511
512
513static hb_ot_shape_normalization_mode_t
514normalization_preference_myanmar (const hb_segment_properties_t *props HB_UNUSED)
515{
516 return HB_OT_SHAPE_NORMALIZATION_MODE_COMPOSED_DIACRITICS_NO_SHORT_CIRCUIT;
517}
518
519
520const hb_ot_complex_shaper_t _hb_ot_complex_shaper_myanmar =
521{
522 "myanmar",
523 collect_features_myanmar,
524 override_features_myanmar,
525 NULL, /* data_create */
526 NULL, /* data_destroy */
527 NULL, /* preprocess_text */
528 normalization_preference_myanmar,
529 NULL, /* decompose */
530 NULL, /* compose */
531 setup_masks_myanmar,
Behdad Esfahbod127daf12013-05-20 09:11:35 -0400532 HB_OT_SHAPE_ZERO_WIDTH_MARKS_BY_GDEF_EARLY,
Behdad Esfahbod98628ca2013-02-11 13:36:23 -0500533 false, /* fallback_position */
534};