• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
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 
27 #include "hb-ot-shape-complex-indic-private.hh"
28 
29 /* buffer var allocations */
30 #define myanmar_category() complex_var_u8_0() /* myanmar_category_t */
31 #define myanmar_position() complex_var_u8_1() /* myanmar_position_t */
32 
33 
34 /*
35  * Myanmar shaper.
36  */
37 
38 static const hb_tag_t
39 basic_features[] =
40 {
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'),
49 };
50 static const hb_tag_t
51 other_features[] =
52 {
53   /*
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   /* Pre-release version of Windows 8 Myanmar font had abvm,blwm
64    * features.  The released Windows 8 version of the font (as well
65    * as the released spec) used 'mark' instead.  The Windows 8
66    * shaper however didn't apply 'mark' but did apply 'mkmk'.
67    * Perhaps it applied abvm/blwm.  This was fixed in a Windows 8
68    * update, so now it applies mark/mkmk.  We are guessing that
69    * it still applies abvm/blwm too.
70    */
71   HB_TAG('a','b','v','m'),
72   HB_TAG('b','l','w','m'),
73 };
74 
75 static void
76 setup_syllables (const hb_ot_shape_plan_t *plan,
77 		 hb_font_t *font,
78 		 hb_buffer_t *buffer);
79 static void
80 initial_reordering (const hb_ot_shape_plan_t *plan,
81 		    hb_font_t *font,
82 		    hb_buffer_t *buffer);
83 static void
84 final_reordering (const hb_ot_shape_plan_t *plan,
85 		  hb_font_t *font,
86 		  hb_buffer_t *buffer);
87 
88 static void
collect_features_myanmar(hb_ot_shape_planner_t * plan)89 collect_features_myanmar (hb_ot_shape_planner_t *plan)
90 {
91   hb_ot_map_builder_t *map = &plan->map;
92 
93   /* Do this before any lookups have been applied. */
94   map->add_gsub_pause (setup_syllables);
95 
96   map->add_global_bool_feature (HB_TAG('l','o','c','l'));
97   /* The Indic specs do not require ccmp, but we apply it here since if
98    * there is a use of it, it's typically at the beginning. */
99   map->add_global_bool_feature (HB_TAG('c','c','m','p'));
100 
101 
102   map->add_gsub_pause (initial_reordering);
103   for (unsigned int i = 0; i < ARRAY_LENGTH (basic_features); i++)
104   {
105     map->add_feature (basic_features[i], 1, F_GLOBAL | F_MANUAL_ZWJ);
106     map->add_gsub_pause (NULL);
107   }
108   map->add_gsub_pause (final_reordering);
109   for (unsigned int i = 0; i < ARRAY_LENGTH (other_features); i++)
110     map->add_feature (other_features[i], 1, F_GLOBAL | F_MANUAL_ZWJ);
111 }
112 
113 static void
override_features_myanmar(hb_ot_shape_planner_t * plan)114 override_features_myanmar (hb_ot_shape_planner_t *plan)
115 {
116   plan->map.add_feature (HB_TAG('l','i','g','a'), 0, F_GLOBAL);
117 }
118 
119 
120 enum syllable_type_t {
121   consonant_syllable,
122   broken_cluster,
123   non_myanmar_cluster,
124 };
125 
126 #include "hb-ot-shape-complex-myanmar-machine.hh"
127 
128 
129 /* Note: This enum is duplicated in the -machine.rl source file.
130  * Not sure how to avoid duplication. */
131 enum myanmar_category_t {
132   OT_As  = 18, /* Asat */
133   OT_D   = 19, /* Digits except zero */
134   OT_D0  = 20, /* Digit zero */
135   OT_DB  = OT_N, /* Dot below */
136   OT_GB  = OT_DOTTEDCIRCLE,
137   OT_MH  = 21, /* Various consonant medial types */
138   OT_MR  = 22, /* Various consonant medial types */
139   OT_MW  = 23, /* Various consonant medial types */
140   OT_MY  = 24, /* Various consonant medial types */
141   OT_PT  = 25, /* Pwo and other tones */
142   OT_VAbv = 26,
143   OT_VBlw = 27,
144   OT_VPre = 28,
145   OT_VPst = 29,
146   OT_VS   = 30 /* Variation selectors */
147 };
148 
149 
150 static inline bool
is_one_of(const hb_glyph_info_t & info,unsigned int flags)151 is_one_of (const hb_glyph_info_t &info, unsigned int flags)
152 {
153   /* If it ligated, all bets are off. */
154   if (_hb_glyph_info_ligated (&info)) return false;
155   return !!(FLAG (info.myanmar_category()) & flags);
156 }
157 
158 /* Note:
159  *
160  * We treat Vowels and placeholders as if they were consonants.  This is safe because Vowels
161  * cannot happen in a consonant syllable.  The plus side however is, we can call the
162  * consonant syllable logic from the vowel syllable function and get it all right! */
163 #define CONSONANT_FLAGS (FLAG (OT_C) | FLAG (OT_CM) | FLAG (OT_Ra) | FLAG (OT_V) | FLAG (OT_NBSP) | FLAG (OT_GB))
164 static inline bool
is_consonant(const hb_glyph_info_t & info)165 is_consonant (const hb_glyph_info_t &info)
166 {
167   return is_one_of (info, CONSONANT_FLAGS);
168 }
169 
170 
171 static inline void
set_myanmar_properties(hb_glyph_info_t & info)172 set_myanmar_properties (hb_glyph_info_t &info)
173 {
174   hb_codepoint_t u = info.codepoint;
175   unsigned int type = hb_indic_get_categories (u);
176   indic_category_t cat = (indic_category_t) (type & 0x7F);
177   indic_position_t pos = (indic_position_t) (type >> 8);
178 
179   /* Myanmar
180    * http://www.microsoft.com/typography/OpenTypeDev/myanmar/intro.htm#analyze
181    */
182   if (unlikely (hb_in_range<hb_codepoint_t> (u, 0xFE00, 0xFE0F)))
183     cat = (indic_category_t) OT_VS;
184   else if (unlikely (u == 0x200C)) cat = (indic_category_t) OT_ZWNJ;
185   else if (unlikely (u == 0x200D)) cat = (indic_category_t) OT_ZWJ;
186 
187   switch (u)
188   {
189     case 0x002D: case 0x00A0: case 0x00D7: case 0x2012:
190     case 0x2013: case 0x2014: case 0x2015: case 0x2022:
191     case 0x25CC: case 0x25FB: case 0x25FC: case 0x25FD:
192     case 0x25FE:
193       cat = (indic_category_t) OT_GB;
194       break;
195 
196     case 0x1004: case 0x101B: case 0x105A:
197       cat = (indic_category_t) OT_Ra;
198       break;
199 
200     case 0x1032: case 0x1036:
201       cat = (indic_category_t) OT_A;
202       break;
203 
204     case 0x103A:
205       cat = (indic_category_t) OT_As;
206       break;
207 
208     case 0x1041: case 0x1042: case 0x1043: case 0x1044:
209     case 0x1045: case 0x1046: case 0x1047: case 0x1048:
210     case 0x1049: case 0x1090: case 0x1091: case 0x1092:
211     case 0x1093: case 0x1094: case 0x1095: case 0x1096:
212     case 0x1097: case 0x1098: case 0x1099:
213       cat = (indic_category_t) OT_D;
214       break;
215 
216     case 0x1040:
217       cat = (indic_category_t) OT_D; /* XXX The spec says D0, but Uniscribe doesn't seem to do. */
218       break;
219 
220     case 0x103E: case 0x1060:
221       cat = (indic_category_t) OT_MH;
222       break;
223 
224     case 0x103C:
225       cat = (indic_category_t) OT_MR;
226       break;
227 
228     case 0x103D: case 0x1082:
229       cat = (indic_category_t) OT_MW;
230       break;
231 
232     case 0x103B: case 0x105E: case 0x105F:
233       cat = (indic_category_t) OT_MY;
234       break;
235 
236     case 0x1063: case 0x1064: case 0x1069: case 0x106A:
237     case 0x106B: case 0x106C: case 0x106D: case 0xAA7B:
238       cat = (indic_category_t) OT_PT;
239       break;
240 
241     case 0x1038: case 0x1087: case 0x1088: case 0x1089:
242     case 0x108A: case 0x108B: case 0x108C: case 0x108D:
243     case 0x108F: case 0x109A: case 0x109B: case 0x109C:
244       cat = (indic_category_t) OT_SM;
245       break;
246   }
247 
248   if (cat == OT_M)
249   {
250     switch ((int) pos)
251     {
252       case POS_PRE_C:	cat = (indic_category_t) OT_VPre;
253 			pos = POS_PRE_M;                  break;
254       case POS_ABOVE_C:	cat = (indic_category_t) OT_VAbv; break;
255       case POS_BELOW_C:	cat = (indic_category_t) OT_VBlw; break;
256       case POS_POST_C:	cat = (indic_category_t) OT_VPst; break;
257     }
258   }
259 
260   info.myanmar_category() = (myanmar_category_t) cat;
261   info.myanmar_position() = pos;
262 }
263 
264 
265 
266 static void
setup_masks_myanmar(const hb_ot_shape_plan_t * plan HB_UNUSED,hb_buffer_t * buffer,hb_font_t * font HB_UNUSED)267 setup_masks_myanmar (const hb_ot_shape_plan_t *plan HB_UNUSED,
268 		   hb_buffer_t              *buffer,
269 		   hb_font_t                *font HB_UNUSED)
270 {
271   HB_BUFFER_ALLOCATE_VAR (buffer, myanmar_category);
272   HB_BUFFER_ALLOCATE_VAR (buffer, myanmar_position);
273 
274   /* We cannot setup masks here.  We save information about characters
275    * and setup masks later on in a pause-callback. */
276 
277   unsigned int count = buffer->len;
278   for (unsigned int i = 0; i < count; i++)
279     set_myanmar_properties (buffer->info[i]);
280 }
281 
282 static void
setup_syllables(const hb_ot_shape_plan_t * plan HB_UNUSED,hb_font_t * font HB_UNUSED,hb_buffer_t * buffer)283 setup_syllables (const hb_ot_shape_plan_t *plan HB_UNUSED,
284 		 hb_font_t *font HB_UNUSED,
285 		 hb_buffer_t *buffer)
286 {
287   find_syllables (buffer);
288 }
289 
290 static int
compare_myanmar_order(const hb_glyph_info_t * pa,const hb_glyph_info_t * pb)291 compare_myanmar_order (const hb_glyph_info_t *pa, const hb_glyph_info_t *pb)
292 {
293   int a = pa->myanmar_position();
294   int b = pb->myanmar_position();
295 
296   return a < b ? -1 : a == b ? 0 : +1;
297 }
298 
299 
300 /* Rules from:
301  * http://www.microsoft.com/typography/OpenTypeDev/myanmar/intro.htm */
302 
303 static void
initial_reordering_consonant_syllable(const hb_ot_shape_plan_t * plan,hb_face_t * face,hb_buffer_t * buffer,unsigned int start,unsigned int end)304 initial_reordering_consonant_syllable (const hb_ot_shape_plan_t *plan,
305 				       hb_face_t *face,
306 				       hb_buffer_t *buffer,
307 				       unsigned int start, unsigned int end)
308 {
309   hb_glyph_info_t *info = buffer->info;
310 
311   unsigned int base = end;
312   bool has_reph = false;
313 
314   {
315     unsigned int limit = start;
316     if (start + 3 <= end &&
317 	info[start  ].myanmar_category() == OT_Ra &&
318 	info[start+1].myanmar_category() == OT_As &&
319 	info[start+2].myanmar_category() == OT_H)
320     {
321       limit += 3;
322       base = start;
323       has_reph = true;
324     }
325 
326     {
327       if (!has_reph)
328 	base = limit;
329 
330       for (unsigned int i = limit; i < end; i++)
331 	if (is_consonant (info[i]))
332 	{
333 	  base = i;
334 	  break;
335 	}
336     }
337   }
338 
339   /* Reorder! */
340   {
341     unsigned int i = start;
342     for (; i < start + (has_reph ? 3 : 0); i++)
343       info[i].myanmar_position() = POS_AFTER_MAIN;
344     for (; i < base; i++)
345       info[i].myanmar_position() = POS_PRE_C;
346     if (i < end)
347     {
348       info[i].myanmar_position() = POS_BASE_C;
349       i++;
350     }
351     indic_position_t pos = POS_AFTER_MAIN;
352     /* The following loop may be ugly, but it implements all of
353      * Myanmar reordering! */
354     for (; i < end; i++)
355     {
356       if (info[i].myanmar_category() == OT_MR) /* Pre-base reordering */
357       {
358 	info[i].myanmar_position() = POS_PRE_C;
359 	continue;
360       }
361       if (info[i].myanmar_position() < POS_BASE_C) /* Left matra */
362       {
363 	continue;
364       }
365 
366       if (pos == POS_AFTER_MAIN && info[i].myanmar_category() == OT_VBlw)
367       {
368 	pos = POS_BELOW_C;
369 	info[i].myanmar_position() = pos;
370 	continue;
371       }
372 
373       if (pos == POS_BELOW_C && info[i].myanmar_category() == OT_A)
374       {
375 	info[i].myanmar_position() = POS_BEFORE_SUB;
376 	continue;
377       }
378       if (pos == POS_BELOW_C && info[i].myanmar_category() == OT_VBlw)
379       {
380 	info[i].myanmar_position() = pos;
381 	continue;
382       }
383       if (pos == POS_BELOW_C && info[i].myanmar_category() != OT_A)
384       {
385         pos = POS_AFTER_SUB;
386 	info[i].myanmar_position() = pos;
387 	continue;
388       }
389       info[i].myanmar_position() = pos;
390     }
391   }
392 
393   buffer->merge_clusters (start, end);
394   /* Sit tight, rock 'n roll! */
395   hb_bubble_sort (info + start, end - start, compare_myanmar_order);
396 }
397 
398 static void
initial_reordering_broken_cluster(const hb_ot_shape_plan_t * plan,hb_face_t * face,hb_buffer_t * buffer,unsigned int start,unsigned int end)399 initial_reordering_broken_cluster (const hb_ot_shape_plan_t *plan,
400 				   hb_face_t *face,
401 				   hb_buffer_t *buffer,
402 				   unsigned int start, unsigned int end)
403 {
404   /* We already inserted dotted-circles, so just call the consonant_syllable. */
405   initial_reordering_consonant_syllable (plan, face, buffer, start, end);
406 }
407 
408 static void
initial_reordering_non_myanmar_cluster(const hb_ot_shape_plan_t * plan HB_UNUSED,hb_face_t * face HB_UNUSED,hb_buffer_t * buffer HB_UNUSED,unsigned int start HB_UNUSED,unsigned int end HB_UNUSED)409 initial_reordering_non_myanmar_cluster (const hb_ot_shape_plan_t *plan HB_UNUSED,
410 					hb_face_t *face HB_UNUSED,
411 					hb_buffer_t *buffer HB_UNUSED,
412 					unsigned int start HB_UNUSED, unsigned int end HB_UNUSED)
413 {
414   /* Nothing to do right now.  If we ever switch to using the output
415    * buffer in the reordering process, we'd need to next_glyph() here. */
416 }
417 
418 
419 static void
initial_reordering_syllable(const hb_ot_shape_plan_t * plan,hb_face_t * face,hb_buffer_t * buffer,unsigned int start,unsigned int end)420 initial_reordering_syllable (const hb_ot_shape_plan_t *plan,
421 			     hb_face_t *face,
422 			     hb_buffer_t *buffer,
423 			     unsigned int start, unsigned int end)
424 {
425   syllable_type_t syllable_type = (syllable_type_t) (buffer->info[start].syllable() & 0x0F);
426   switch (syllable_type) {
427   case consonant_syllable:	initial_reordering_consonant_syllable  (plan, face, buffer, start, end); return;
428   case broken_cluster:		initial_reordering_broken_cluster      (plan, face, buffer, start, end); return;
429   case non_myanmar_cluster:	initial_reordering_non_myanmar_cluster (plan, face, buffer, start, end); return;
430   }
431 }
432 
433 static inline void
insert_dotted_circles(const hb_ot_shape_plan_t * plan HB_UNUSED,hb_font_t * font,hb_buffer_t * buffer)434 insert_dotted_circles (const hb_ot_shape_plan_t *plan HB_UNUSED,
435 		       hb_font_t *font,
436 		       hb_buffer_t *buffer)
437 {
438   /* Note: This loop is extra overhead, but should not be measurable. */
439   bool has_broken_syllables = false;
440   unsigned int count = buffer->len;
441   for (unsigned int i = 0; i < count; i++)
442     if ((buffer->info[i].syllable() & 0x0F) == broken_cluster) {
443       has_broken_syllables = true;
444       break;
445     }
446   if (likely (!has_broken_syllables))
447     return;
448 
449 
450   hb_codepoint_t dottedcircle_glyph;
451   if (!font->get_glyph (0x25CC, 0, &dottedcircle_glyph))
452     return;
453 
454   hb_glyph_info_t dottedcircle = {0};
455   dottedcircle.codepoint = 0x25CC;
456   set_myanmar_properties (dottedcircle);
457   dottedcircle.codepoint = dottedcircle_glyph;
458 
459   buffer->clear_output ();
460 
461   buffer->idx = 0;
462   unsigned int last_syllable = 0;
463   while (buffer->idx < buffer->len)
464   {
465     unsigned int syllable = buffer->cur().syllable();
466     syllable_type_t syllable_type = (syllable_type_t) (syllable & 0x0F);
467     if (unlikely (last_syllable != syllable && syllable_type == broken_cluster))
468     {
469       last_syllable = syllable;
470 
471       hb_glyph_info_t info = dottedcircle;
472       info.cluster = buffer->cur().cluster;
473       info.mask = buffer->cur().mask;
474       info.syllable() = buffer->cur().syllable();
475 
476       buffer->output_info (info);
477     }
478     else
479       buffer->next_glyph ();
480   }
481 
482   buffer->swap_buffers ();
483 }
484 
485 static void
initial_reordering(const hb_ot_shape_plan_t * plan,hb_font_t * font,hb_buffer_t * buffer)486 initial_reordering (const hb_ot_shape_plan_t *plan,
487 		    hb_font_t *font,
488 		    hb_buffer_t *buffer)
489 {
490   insert_dotted_circles (plan, font, buffer);
491 
492   hb_glyph_info_t *info = buffer->info;
493   unsigned int count = buffer->len;
494   if (unlikely (!count)) return;
495   unsigned int last = 0;
496   unsigned int last_syllable = info[0].syllable();
497   for (unsigned int i = 1; i < count; i++)
498     if (last_syllable != info[i].syllable()) {
499       initial_reordering_syllable (plan, font->face, buffer, last, i);
500       last = i;
501       last_syllable = info[last].syllable();
502     }
503   initial_reordering_syllable (plan, font->face, buffer, last, count);
504 }
505 
506 static void
final_reordering(const hb_ot_shape_plan_t * plan,hb_font_t * font HB_UNUSED,hb_buffer_t * buffer)507 final_reordering (const hb_ot_shape_plan_t *plan,
508 		  hb_font_t *font HB_UNUSED,
509 		  hb_buffer_t *buffer)
510 {
511   hb_glyph_info_t *info = buffer->info;
512   unsigned int count = buffer->len;
513 
514   /* Zero syllables now... */
515   for (unsigned int i = 0; i < count; i++)
516     info[i].syllable() = 0;
517 
518   HB_BUFFER_DEALLOCATE_VAR (buffer, myanmar_category);
519   HB_BUFFER_DEALLOCATE_VAR (buffer, myanmar_position);
520 }
521 
522 
523 static hb_ot_shape_normalization_mode_t
normalization_preference_myanmar(const hb_segment_properties_t * props HB_UNUSED)524 normalization_preference_myanmar (const hb_segment_properties_t *props HB_UNUSED)
525 {
526   return HB_OT_SHAPE_NORMALIZATION_MODE_COMPOSED_DIACRITICS_NO_SHORT_CIRCUIT;
527 }
528 
529 
530 const hb_ot_complex_shaper_t _hb_ot_complex_shaper_myanmar =
531 {
532   "myanmar",
533   collect_features_myanmar,
534   override_features_myanmar,
535   NULL, /* data_create */
536   NULL, /* data_destroy */
537   NULL, /* preprocess_text */
538   normalization_preference_myanmar,
539   NULL, /* decompose */
540   NULL, /* compose */
541   setup_masks_myanmar,
542   HB_OT_SHAPE_ZERO_WIDTH_MARKS_BY_GDEF_EARLY,
543   false, /* fallback_position */
544 };
545