• 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.hh"
28 
29 #ifndef HB_NO_OT_SHAPE
30 
31 #include "hb-ot-shape-complex-myanmar.hh"
32 
33 
34 /*
35  * Myanmar shaper.
36  */
37 
38 static const hb_tag_t
39 myanmar_basic_features[] =
40 {
41   /*
42    * Basic features.
43    * These features are applied in order, one at a time, after 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 myanmar_other_features[] =
52 {
53   /*
54    * Other features.
55    * These features are applied all at once, after clearing syllables.
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 };
62 
63 static void
64 setup_syllables_myanmar (const hb_ot_shape_plan_t *plan,
65 			 hb_font_t *font,
66 			 hb_buffer_t *buffer);
67 static void
68 reorder_myanmar (const hb_ot_shape_plan_t *plan,
69 		 hb_font_t *font,
70 		 hb_buffer_t *buffer);
71 
72 static void
collect_features_myanmar(hb_ot_shape_planner_t * plan)73 collect_features_myanmar (hb_ot_shape_planner_t *plan)
74 {
75   hb_ot_map_builder_t *map = &plan->map;
76 
77   /* Do this before any lookups have been applied. */
78   map->add_gsub_pause (setup_syllables_myanmar);
79 
80   map->enable_feature (HB_TAG('l','o','c','l'));
81   /* The Indic specs do not require ccmp, but we apply it here since if
82    * there is a use of it, it's typically at the beginning. */
83   map->enable_feature (HB_TAG('c','c','m','p'));
84 
85 
86   map->add_gsub_pause (reorder_myanmar);
87 
88   for (unsigned int i = 0; i < ARRAY_LENGTH (myanmar_basic_features); i++)
89   {
90     map->enable_feature (myanmar_basic_features[i], F_MANUAL_ZWJ);
91     map->add_gsub_pause (nullptr);
92   }
93 
94   map->add_gsub_pause (_hb_clear_syllables);
95 
96   for (unsigned int i = 0; i < ARRAY_LENGTH (myanmar_other_features); i++)
97     map->enable_feature (myanmar_other_features[i], F_MANUAL_ZWJ);
98 }
99 
100 static void
override_features_myanmar(hb_ot_shape_planner_t * plan)101 override_features_myanmar (hb_ot_shape_planner_t *plan)
102 {
103   plan->map.disable_feature (HB_TAG('l','i','g','a'));
104 }
105 
106 
107 enum myanmar_syllable_type_t {
108   myanmar_consonant_syllable,
109   myanmar_punctuation_cluster,
110   myanmar_broken_cluster,
111   myanmar_non_myanmar_cluster,
112 };
113 
114 #include "hb-ot-shape-complex-myanmar-machine.hh"
115 
116 
117 static void
setup_masks_myanmar(const hb_ot_shape_plan_t * plan HB_UNUSED,hb_buffer_t * buffer,hb_font_t * font HB_UNUSED)118 setup_masks_myanmar (const hb_ot_shape_plan_t *plan HB_UNUSED,
119 		     hb_buffer_t              *buffer,
120 		     hb_font_t                *font HB_UNUSED)
121 {
122   HB_BUFFER_ALLOCATE_VAR (buffer, myanmar_category);
123   HB_BUFFER_ALLOCATE_VAR (buffer, myanmar_position);
124 
125   /* We cannot setup masks here.  We save information about characters
126    * and setup masks later on in a pause-callback. */
127 
128   unsigned int count = buffer->len;
129   hb_glyph_info_t *info = buffer->info;
130   for (unsigned int i = 0; i < count; i++)
131     set_myanmar_properties (info[i]);
132 }
133 
134 static void
setup_syllables_myanmar(const hb_ot_shape_plan_t * plan HB_UNUSED,hb_font_t * font HB_UNUSED,hb_buffer_t * buffer)135 setup_syllables_myanmar (const hb_ot_shape_plan_t *plan HB_UNUSED,
136 			 hb_font_t *font HB_UNUSED,
137 			 hb_buffer_t *buffer)
138 {
139   find_syllables_myanmar (buffer);
140   foreach_syllable (buffer, start, end)
141     buffer->unsafe_to_break (start, end);
142 }
143 
144 static int
compare_myanmar_order(const hb_glyph_info_t * pa,const hb_glyph_info_t * pb)145 compare_myanmar_order (const hb_glyph_info_t *pa, const hb_glyph_info_t *pb)
146 {
147   int a = pa->myanmar_position();
148   int b = pb->myanmar_position();
149 
150   return a < b ? -1 : a == b ? 0 : +1;
151 }
152 
153 
154 /* Rules from:
155  * https://docs.microsoft.com/en-us/typography/script-development/myanmar */
156 
157 static void
initial_reordering_consonant_syllable(hb_buffer_t * buffer,unsigned int start,unsigned int end)158 initial_reordering_consonant_syllable (hb_buffer_t *buffer,
159 				       unsigned int start, unsigned int end)
160 {
161   hb_glyph_info_t *info = buffer->info;
162 
163   unsigned int base = end;
164   bool has_reph = false;
165 
166   {
167     unsigned int limit = start;
168     if (start + 3 <= end &&
169 	info[start  ].myanmar_category() == OT_Ra &&
170 	info[start+1].myanmar_category() == OT_As &&
171 	info[start+2].myanmar_category() == OT_H)
172     {
173       limit += 3;
174       base = start;
175       has_reph = true;
176     }
177 
178     {
179       if (!has_reph)
180 	base = limit;
181 
182       for (unsigned int i = limit; i < end; i++)
183 	if (is_consonant (info[i]))
184 	{
185 	  base = i;
186 	  break;
187 	}
188     }
189   }
190 
191   /* Reorder! */
192   {
193     unsigned int i = start;
194     for (; i < start + (has_reph ? 3 : 0); i++)
195       info[i].myanmar_position() = POS_AFTER_MAIN;
196     for (; i < base; i++)
197       info[i].myanmar_position() = POS_PRE_C;
198     if (i < end)
199     {
200       info[i].myanmar_position() = POS_BASE_C;
201       i++;
202     }
203     indic_position_t pos = POS_AFTER_MAIN;
204     /* The following loop may be ugly, but it implements all of
205      * Myanmar reordering! */
206     for (; i < end; i++)
207     {
208       if (info[i].myanmar_category() == OT_MR) /* Pre-base reordering */
209       {
210 	info[i].myanmar_position() = POS_PRE_C;
211 	continue;
212       }
213       if (info[i].myanmar_position() < POS_BASE_C) /* Left matra */
214       {
215 	continue;
216       }
217       if (info[i].myanmar_category() == OT_VS)
218       {
219 	info[i].myanmar_position() = info[i - 1].myanmar_position();
220 	continue;
221       }
222 
223       if (pos == POS_AFTER_MAIN && info[i].myanmar_category() == OT_VBlw)
224       {
225 	pos = POS_BELOW_C;
226 	info[i].myanmar_position() = pos;
227 	continue;
228       }
229 
230       if (pos == POS_BELOW_C && info[i].myanmar_category() == OT_A)
231       {
232 	info[i].myanmar_position() = POS_BEFORE_SUB;
233 	continue;
234       }
235       if (pos == POS_BELOW_C && info[i].myanmar_category() == OT_VBlw)
236       {
237 	info[i].myanmar_position() = pos;
238 	continue;
239       }
240       if (pos == POS_BELOW_C && info[i].myanmar_category() != OT_A)
241       {
242 	pos = POS_AFTER_SUB;
243 	info[i].myanmar_position() = pos;
244 	continue;
245       }
246       info[i].myanmar_position() = pos;
247     }
248   }
249 
250   /* Sit tight, rock 'n roll! */
251   buffer->sort (start, end, compare_myanmar_order);
252 }
253 
254 static void
reorder_syllable_myanmar(const hb_ot_shape_plan_t * plan HB_UNUSED,hb_face_t * face HB_UNUSED,hb_buffer_t * buffer,unsigned int start,unsigned int end)255 reorder_syllable_myanmar (const hb_ot_shape_plan_t *plan HB_UNUSED,
256 			  hb_face_t *face HB_UNUSED,
257 			  hb_buffer_t *buffer,
258 			  unsigned int start, unsigned int end)
259 {
260   myanmar_syllable_type_t syllable_type = (myanmar_syllable_type_t) (buffer->info[start].syllable() & 0x0F);
261   switch (syllable_type) {
262 
263     case myanmar_broken_cluster: /* We already inserted dotted-circles, so just call the consonant_syllable. */
264     case myanmar_consonant_syllable:
265       initial_reordering_consonant_syllable  (buffer, start, end);
266       break;
267 
268     case myanmar_punctuation_cluster:
269     case myanmar_non_myanmar_cluster:
270       break;
271   }
272 }
273 
274 static inline void
insert_dotted_circles_myanmar(const hb_ot_shape_plan_t * plan HB_UNUSED,hb_font_t * font,hb_buffer_t * buffer)275 insert_dotted_circles_myanmar (const hb_ot_shape_plan_t *plan HB_UNUSED,
276 			       hb_font_t *font,
277 			       hb_buffer_t *buffer)
278 {
279   if (unlikely (buffer->flags & HB_BUFFER_FLAG_DO_NOT_INSERT_DOTTED_CIRCLE))
280     return;
281 
282   /* Note: This loop is extra overhead, but should not be measurable.
283    * TODO Use a buffer scratch flag to remove the loop. */
284   bool has_broken_syllables = false;
285   unsigned int count = buffer->len;
286   hb_glyph_info_t *info = buffer->info;
287   for (unsigned int i = 0; i < count; i++)
288     if ((info[i].syllable() & 0x0F) == myanmar_broken_cluster)
289     {
290       has_broken_syllables = true;
291       break;
292     }
293   if (likely (!has_broken_syllables))
294     return;
295 
296 
297   hb_codepoint_t dottedcircle_glyph;
298   if (!font->get_nominal_glyph (0x25CCu, &dottedcircle_glyph))
299     return;
300 
301   hb_glyph_info_t dottedcircle = {0};
302   dottedcircle.codepoint = 0x25CCu;
303   set_myanmar_properties (dottedcircle);
304   dottedcircle.codepoint = dottedcircle_glyph;
305 
306   buffer->clear_output ();
307 
308   buffer->idx = 0;
309   unsigned int last_syllable = 0;
310   while (buffer->idx < buffer->len && buffer->successful)
311   {
312     unsigned int syllable = buffer->cur().syllable();
313     myanmar_syllable_type_t syllable_type = (myanmar_syllable_type_t) (syllable & 0x0F);
314     if (unlikely (last_syllable != syllable && syllable_type == myanmar_broken_cluster))
315     {
316       last_syllable = syllable;
317 
318       hb_glyph_info_t ginfo = dottedcircle;
319       ginfo.cluster = buffer->cur().cluster;
320       ginfo.mask = buffer->cur().mask;
321       ginfo.syllable() = buffer->cur().syllable();
322 
323       buffer->output_info (ginfo);
324     }
325     else
326       buffer->next_glyph ();
327   }
328   buffer->swap_buffers ();
329 }
330 
331 static void
reorder_myanmar(const hb_ot_shape_plan_t * plan,hb_font_t * font,hb_buffer_t * buffer)332 reorder_myanmar (const hb_ot_shape_plan_t *plan,
333 		 hb_font_t *font,
334 		 hb_buffer_t *buffer)
335 {
336   insert_dotted_circles_myanmar (plan, font, buffer);
337 
338   foreach_syllable (buffer, start, end)
339     reorder_syllable_myanmar (plan, font->face, buffer, start, end);
340 
341   HB_BUFFER_DEALLOCATE_VAR (buffer, myanmar_category);
342   HB_BUFFER_DEALLOCATE_VAR (buffer, myanmar_position);
343 }
344 
345 
346 const hb_ot_complex_shaper_t _hb_ot_complex_shaper_myanmar =
347 {
348   collect_features_myanmar,
349   override_features_myanmar,
350   nullptr, /* data_create */
351   nullptr, /* data_destroy */
352   nullptr, /* preprocess_text */
353   nullptr, /* postprocess_glyphs */
354   HB_OT_SHAPE_NORMALIZATION_MODE_COMPOSED_DIACRITICS_NO_SHORT_CIRCUIT,
355   nullptr, /* decompose */
356   nullptr, /* compose */
357   setup_masks_myanmar,
358   HB_TAG_NONE, /* gpos_tag */
359   nullptr, /* reorder_marks */
360   HB_OT_SHAPE_ZERO_WIDTH_MARKS_BY_GDEF_EARLY,
361   false, /* fallback_position */
362 };
363 
364 
365 /* Ugly Zawgyi encoding.
366  * Disable all auto processing.
367  * https://github.com/harfbuzz/harfbuzz/issues/1162 */
368 const hb_ot_complex_shaper_t _hb_ot_complex_shaper_myanmar_zawgyi =
369 {
370   nullptr, /* collect_features */
371   nullptr, /* override_features */
372   nullptr, /* data_create */
373   nullptr, /* data_destroy */
374   nullptr, /* preprocess_text */
375   nullptr, /* postprocess_glyphs */
376   HB_OT_SHAPE_NORMALIZATION_MODE_NONE,
377   nullptr, /* decompose */
378   nullptr, /* compose */
379   nullptr, /* setup_masks */
380   HB_TAG_NONE, /* gpos_tag */
381   nullptr, /* reorder_marks */
382   HB_OT_SHAPE_ZERO_WIDTH_MARKS_NONE,
383   false, /* fallback_position */
384 };
385 
386 
387 #endif
388