Imported Upstream version 3.4.0
[platform/upstream/harfbuzz.git] / src / hb-ot-shape-complex-khmer.cc
1 /*
2  * Copyright © 2011,2012  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-khmer.hh"
32 #include "hb-ot-shape-complex-khmer-machine.hh"
33 #include "hb-ot-layout.hh"
34
35
36 /*
37  * Khmer shaper.
38  */
39
40 static const hb_ot_map_feature_t
41 khmer_features[] =
42 {
43   /*
44    * Basic features.
45    * These features are applied all at once, before reordering, constrained
46    * to the syllable.
47    */
48   {HB_TAG('p','r','e','f'), F_MANUAL_JOINERS},
49   {HB_TAG('b','l','w','f'), F_MANUAL_JOINERS},
50   {HB_TAG('a','b','v','f'), F_MANUAL_JOINERS},
51   {HB_TAG('p','s','t','f'), F_MANUAL_JOINERS},
52   {HB_TAG('c','f','a','r'), F_MANUAL_JOINERS},
53   /*
54    * Other features.
55    * These features are applied all at once after clearing syllables.
56    */
57   {HB_TAG('p','r','e','s'), F_GLOBAL_MANUAL_JOINERS},
58   {HB_TAG('a','b','v','s'), F_GLOBAL_MANUAL_JOINERS},
59   {HB_TAG('b','l','w','s'), F_GLOBAL_MANUAL_JOINERS},
60   {HB_TAG('p','s','t','s'), F_GLOBAL_MANUAL_JOINERS},
61 };
62
63 /*
64  * Must be in the same order as the khmer_features array.
65  */
66 enum {
67   KHMER_PREF,
68   KHMER_BLWF,
69   KHMER_ABVF,
70   KHMER_PSTF,
71   KHMER_CFAR,
72
73   _KHMER_PRES,
74   _KHMER_ABVS,
75   _KHMER_BLWS,
76   _KHMER_PSTS,
77
78   KHMER_NUM_FEATURES,
79   KHMER_BASIC_FEATURES = _KHMER_PRES, /* Don't forget to update this! */
80 };
81
82 static void
83 setup_syllables_khmer (const hb_ot_shape_plan_t *plan,
84                        hb_font_t *font,
85                        hb_buffer_t *buffer);
86 static void
87 reorder_khmer (const hb_ot_shape_plan_t *plan,
88                hb_font_t *font,
89                hb_buffer_t *buffer);
90
91 static void
92 collect_features_khmer (hb_ot_shape_planner_t *plan)
93 {
94   hb_ot_map_builder_t *map = &plan->map;
95
96   /* Do this before any lookups have been applied. */
97   map->add_gsub_pause (setup_syllables_khmer);
98   map->add_gsub_pause (reorder_khmer);
99
100   /* Testing suggests that Uniscribe does NOT pause between basic
101    * features.  Test with KhmerUI.ttf and the following three
102    * sequences:
103    *
104    *   U+1789,U+17BC
105    *   U+1789,U+17D2,U+1789
106    *   U+1789,U+17D2,U+1789,U+17BC
107    *
108    * https://github.com/harfbuzz/harfbuzz/issues/974
109    */
110   map->enable_feature (HB_TAG('l','o','c','l'));
111   map->enable_feature (HB_TAG('c','c','m','p'));
112
113   unsigned int i = 0;
114   for (; i < KHMER_BASIC_FEATURES; i++)
115     map->add_feature (khmer_features[i]);
116
117   map->add_gsub_pause (_hb_clear_syllables);
118
119   for (; i < KHMER_NUM_FEATURES; i++)
120     map->add_feature (khmer_features[i]);
121 }
122
123 static void
124 override_features_khmer (hb_ot_shape_planner_t *plan)
125 {
126   hb_ot_map_builder_t *map = &plan->map;
127
128   /* Khmer spec has 'clig' as part of required shaping features:
129    * "Apply feature 'clig' to form ligatures that are desired for
130    * typographical correctness.", hence in overrides... */
131   map->enable_feature (HB_TAG('c','l','i','g'));
132
133   /* Uniscribe does not apply 'kern' in Khmer. */
134   if (hb_options ().uniscribe_bug_compatible)
135   {
136     map->disable_feature (HB_TAG('k','e','r','n'));
137   }
138
139   map->disable_feature (HB_TAG('l','i','g','a'));
140 }
141
142
143 struct khmer_shape_plan_t
144 {
145   hb_mask_t mask_array[KHMER_NUM_FEATURES];
146 };
147
148 static void *
149 data_create_khmer (const hb_ot_shape_plan_t *plan)
150 {
151   khmer_shape_plan_t *khmer_plan = (khmer_shape_plan_t *) hb_calloc (1, sizeof (khmer_shape_plan_t));
152   if (unlikely (!khmer_plan))
153     return nullptr;
154
155   for (unsigned int i = 0; i < ARRAY_LENGTH (khmer_plan->mask_array); i++)
156     khmer_plan->mask_array[i] = (khmer_features[i].flags & F_GLOBAL) ?
157                                  0 : plan->map.get_1_mask (khmer_features[i].tag);
158
159   return khmer_plan;
160 }
161
162 static void
163 data_destroy_khmer (void *data)
164 {
165   hb_free (data);
166 }
167
168 static void
169 setup_masks_khmer (const hb_ot_shape_plan_t *plan HB_UNUSED,
170                    hb_buffer_t              *buffer,
171                    hb_font_t                *font HB_UNUSED)
172 {
173   HB_BUFFER_ALLOCATE_VAR (buffer, khmer_category);
174
175   /* We cannot setup masks here.  We save information about characters
176    * and setup masks later on in a pause-callback. */
177
178   unsigned int count = buffer->len;
179   hb_glyph_info_t *info = buffer->info;
180   for (unsigned int i = 0; i < count; i++)
181     set_khmer_properties (info[i]);
182 }
183
184 static void
185 setup_syllables_khmer (const hb_ot_shape_plan_t *plan HB_UNUSED,
186                        hb_font_t *font HB_UNUSED,
187                        hb_buffer_t *buffer)
188 {
189   find_syllables_khmer (buffer);
190   foreach_syllable (buffer, start, end)
191     buffer->unsafe_to_break (start, end);
192 }
193
194
195 /* Rules from:
196  * https://docs.microsoft.com/en-us/typography/script-development/devanagari */
197
198 static void
199 reorder_consonant_syllable (const hb_ot_shape_plan_t *plan,
200                             hb_face_t *face HB_UNUSED,
201                             hb_buffer_t *buffer,
202                             unsigned int start, unsigned int end)
203 {
204   const khmer_shape_plan_t *khmer_plan = (const khmer_shape_plan_t *) plan->data;
205   hb_glyph_info_t *info = buffer->info;
206
207   /* Setup masks. */
208   {
209     /* Post-base */
210     hb_mask_t mask = khmer_plan->mask_array[KHMER_BLWF] |
211                      khmer_plan->mask_array[KHMER_ABVF] |
212                      khmer_plan->mask_array[KHMER_PSTF];
213     for (unsigned int i = start + 1; i < end; i++)
214       info[i].mask  |= mask;
215   }
216
217   unsigned int num_coengs = 0;
218   for (unsigned int i = start + 1; i < end; i++)
219   {
220     /* """
221      * When a COENG + (Cons | IndV) combination are found (and subscript count
222      * is less than two) the character combination is handled according to the
223      * subscript type of the character following the COENG.
224      *
225      * ...
226      *
227      * Subscript Type 2 - The COENG + RO characters are reordered to immediately
228      * before the base glyph. Then the COENG + RO characters are assigned to have
229      * the 'pref' OpenType feature applied to them.
230      * """
231      */
232     if (info[i].khmer_category() == OT_Coeng && num_coengs <= 2 && i + 1 < end)
233     {
234       num_coengs++;
235
236       if (info[i + 1].khmer_category() == OT_Ra)
237       {
238         for (unsigned int j = 0; j < 2; j++)
239           info[i + j].mask |= khmer_plan->mask_array[KHMER_PREF];
240
241         /* Move the Coeng,Ro sequence to the start. */
242         buffer->merge_clusters (start, i + 2);
243         hb_glyph_info_t t0 = info[i];
244         hb_glyph_info_t t1 = info[i + 1];
245         memmove (&info[start + 2], &info[start], (i - start) * sizeof (info[0]));
246         info[start] = t0;
247         info[start + 1] = t1;
248
249         /* Mark the subsequent stuff with 'cfar'.  Used in Khmer.
250          * Read the feature spec.
251          * This allows distinguishing the following cases with MS Khmer fonts:
252          * U+1784,U+17D2,U+179A,U+17D2,U+1782
253          * U+1784,U+17D2,U+1782,U+17D2,U+179A
254          */
255         if (khmer_plan->mask_array[KHMER_CFAR])
256           for (unsigned int j = i + 2; j < end; j++)
257             info[j].mask |= khmer_plan->mask_array[KHMER_CFAR];
258
259         num_coengs = 2; /* Done. */
260       }
261     }
262
263     /* Reorder left matra piece. */
264     else if (info[i].khmer_category() == OT_VPre)
265     {
266       /* Move to the start. */
267       buffer->merge_clusters (start, i + 1);
268       hb_glyph_info_t t = info[i];
269       memmove (&info[start + 1], &info[start], (i - start) * sizeof (info[0]));
270       info[start] = t;
271     }
272   }
273 }
274
275 static void
276 reorder_syllable_khmer (const hb_ot_shape_plan_t *plan,
277                         hb_face_t *face,
278                         hb_buffer_t *buffer,
279                         unsigned int start, unsigned int end)
280 {
281   khmer_syllable_type_t syllable_type = (khmer_syllable_type_t) (buffer->info[start].syllable() & 0x0F);
282   switch (syllable_type)
283   {
284     case khmer_broken_cluster: /* We already inserted dotted-circles, so just call the consonant_syllable. */
285     case khmer_consonant_syllable:
286      reorder_consonant_syllable (plan, face, buffer, start, end);
287      break;
288
289     case khmer_non_khmer_cluster:
290       break;
291   }
292 }
293
294 static void
295 reorder_khmer (const hb_ot_shape_plan_t *plan,
296                hb_font_t *font,
297                hb_buffer_t *buffer)
298 {
299   if (buffer->message (font, "start reordering khmer"))
300   {
301     hb_syllabic_insert_dotted_circles (font, buffer,
302                                        khmer_broken_cluster,
303                                        OT_DOTTEDCIRCLE,
304                                        OT_Repha);
305
306     foreach_syllable (buffer, start, end)
307       reorder_syllable_khmer (plan, font->face, buffer, start, end);
308     (void) buffer->message (font, "end reordering khmer");
309   }
310   HB_BUFFER_DEALLOCATE_VAR (buffer, khmer_category);
311 }
312
313
314 static bool
315 decompose_khmer (const hb_ot_shape_normalize_context_t *c,
316                  hb_codepoint_t  ab,
317                  hb_codepoint_t *a,
318                  hb_codepoint_t *b)
319 {
320   switch (ab)
321   {
322     /*
323      * Decompose split matras that don't have Unicode decompositions.
324      */
325
326     /* Khmer */
327     case 0x17BEu  : *a = 0x17C1u; *b= 0x17BEu; return true;
328     case 0x17BFu  : *a = 0x17C1u; *b= 0x17BFu; return true;
329     case 0x17C0u  : *a = 0x17C1u; *b= 0x17C0u; return true;
330     case 0x17C4u  : *a = 0x17C1u; *b= 0x17C4u; return true;
331     case 0x17C5u  : *a = 0x17C1u; *b= 0x17C5u; return true;
332   }
333
334   return (bool) c->unicode->decompose (ab, a, b);
335 }
336
337 static bool
338 compose_khmer (const hb_ot_shape_normalize_context_t *c,
339                hb_codepoint_t  a,
340                hb_codepoint_t  b,
341                hb_codepoint_t *ab)
342 {
343   /* Avoid recomposing split matras. */
344   if (HB_UNICODE_GENERAL_CATEGORY_IS_MARK (c->unicode->general_category (a)))
345     return false;
346
347   return (bool) c->unicode->compose (a, b, ab);
348 }
349
350
351 const hb_ot_complex_shaper_t _hb_ot_complex_shaper_khmer =
352 {
353   collect_features_khmer,
354   override_features_khmer,
355   data_create_khmer,
356   data_destroy_khmer,
357   nullptr, /* preprocess_text */
358   nullptr, /* postprocess_glyphs */
359   HB_OT_SHAPE_NORMALIZATION_MODE_COMPOSED_DIACRITICS_NO_SHORT_CIRCUIT,
360   decompose_khmer,
361   compose_khmer,
362   setup_masks_khmer,
363   HB_TAG_NONE, /* gpos_tag */
364   nullptr, /* reorder_marks */
365   HB_OT_SHAPE_ZERO_WIDTH_MARKS_NONE,
366   false, /* fallback_position */
367 };
368
369
370 #endif