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