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-ot-shape-complex-khmer.hh"
28 #include "hb-ot-layout.hh"
29
30
31 /*
32 * Khmer shaper.
33 */
34
35 static const hb_ot_map_feature_t
36 khmer_features[] =
37 {
38 /*
39 * Basic features.
40 * These features are applied in order, one at a time, after reordering.
41 */
42 {HB_TAG('p','r','e','f'), F_MANUAL_JOINERS},
43 {HB_TAG('b','l','w','f'), F_MANUAL_JOINERS},
44 {HB_TAG('a','b','v','f'), F_MANUAL_JOINERS},
45 {HB_TAG('p','s','t','f'), F_MANUAL_JOINERS},
46 {HB_TAG('c','f','a','r'), F_MANUAL_JOINERS},
47 /*
48 * Other features.
49 * These features are applied all at once after clearing syllables.
50 */
51 {HB_TAG('p','r','e','s'), F_GLOBAL_MANUAL_JOINERS},
52 {HB_TAG('a','b','v','s'), F_GLOBAL_MANUAL_JOINERS},
53 {HB_TAG('b','l','w','s'), F_GLOBAL_MANUAL_JOINERS},
54 {HB_TAG('p','s','t','s'), F_GLOBAL_MANUAL_JOINERS},
55 /*
56 * Positioning features.
57 * We don't care about the types.
58 */
59 {HB_TAG('d','i','s','t'), F_GLOBAL},
60 {HB_TAG('a','b','v','m'), F_GLOBAL},
61 {HB_TAG('b','l','w','m'), F_GLOBAL},
62 };
63
64 /*
65 * Must be in the same order as the khmer_features array.
66 */
67 enum {
68 PREF,
69 BLWF,
70 ABVF,
71 PSTF,
72 CFAR,
73
74 _PRES,
75 _ABVS,
76 _BLWS,
77 _PSTS,
78
79 _DIST,
80 _ABVM,
81 _BLWM,
82
83 KHMER_NUM_FEATURES,
84 KHMER_BASIC_FEATURES = _PRES, /* Don't forget to update this! */
85 };
86
87 static void
88 setup_syllables (const hb_ot_shape_plan_t *plan,
89 hb_font_t *font,
90 hb_buffer_t *buffer);
91 static void
92 reorder (const hb_ot_shape_plan_t *plan,
93 hb_font_t *font,
94 hb_buffer_t *buffer);
95 static void
96 clear_syllables (const hb_ot_shape_plan_t *plan,
97 hb_font_t *font,
98 hb_buffer_t *buffer);
99
100 static void
collect_features_khmer(hb_ot_shape_planner_t * plan)101 collect_features_khmer (hb_ot_shape_planner_t *plan)
102 {
103 hb_ot_map_builder_t *map = &plan->map;
104
105 /* Do this before any lookups have been applied. */
106 map->add_gsub_pause (setup_syllables);
107 map->add_gsub_pause (reorder);
108
109 /* Testing suggests that Uniscribe does NOT pause between basic
110 * features. Test with KhmerUI.ttf and the following three
111 * sequences:
112 *
113 * U+1789,U+17BC
114 * U+1789,U+17D2,U+1789
115 * U+1789,U+17D2,U+1789,U+17BC
116 *
117 * https://github.com/harfbuzz/harfbuzz/issues/974
118 */
119 map->enable_feature (HB_TAG('l','o','c','l'));
120 map->enable_feature (HB_TAG('c','c','m','p'));
121
122 unsigned int i = 0;
123 for (; i < KHMER_BASIC_FEATURES; i++)
124 map->add_feature (khmer_features[i]);
125
126 map->add_gsub_pause (clear_syllables);
127
128 for (; i < KHMER_NUM_FEATURES; i++)
129 map->add_feature (khmer_features[i]);
130 }
131
132 static void
override_features_khmer(hb_ot_shape_planner_t * plan)133 override_features_khmer (hb_ot_shape_planner_t *plan)
134 {
135 hb_ot_map_builder_t *map = &plan->map;
136
137 /* Khmer spec has 'clig' as part of required shaping features:
138 * "Apply feature 'clig' to form ligatures that are desired for
139 * typographical correctness.", hence in overrides... */
140 map->enable_feature (HB_TAG('c','l','i','g'));
141
142 /* Uniscribe does not apply 'kern' in Khmer. */
143 if (hb_options ().uniscribe_bug_compatible)
144 {
145 map->disable_feature (HB_TAG('k','e','r','n'));
146 }
147
148 map->disable_feature (HB_TAG('l','i','g','a'));
149 }
150
151
152 struct would_substitute_feature_t
153 {
initwould_substitute_feature_t154 void init (const hb_ot_map_t *map, hb_tag_t feature_tag, bool zero_context_)
155 {
156 zero_context = zero_context_;
157 map->get_stage_lookups (0/*GSUB*/,
158 map->get_feature_stage (0/*GSUB*/, feature_tag),
159 &lookups, &count);
160 }
161
would_substitutewould_substitute_feature_t162 bool would_substitute (const hb_codepoint_t *glyphs,
163 unsigned int glyphs_count,
164 hb_face_t *face) const
165 {
166 for (unsigned int i = 0; i < count; i++)
167 if (hb_ot_layout_lookup_would_substitute_fast (face, lookups[i].index, glyphs, glyphs_count, zero_context))
168 return true;
169 return false;
170 }
171
172 private:
173 const hb_ot_map_t::lookup_map_t *lookups;
174 unsigned int count;
175 bool zero_context;
176 };
177
178 struct khmer_shape_plan_t
179 {
get_virama_glyphkhmer_shape_plan_t180 bool get_virama_glyph (hb_font_t *font, hb_codepoint_t *pglyph) const
181 {
182 hb_codepoint_t glyph = virama_glyph;
183 if (unlikely (virama_glyph == (hb_codepoint_t) -1))
184 {
185 if (!font->get_nominal_glyph (0x17D2u, &glyph))
186 glyph = 0;
187 /* Technically speaking, the spec says we should apply 'locl' to virama too.
188 * Maybe one day... */
189
190 /* Our get_nominal_glyph() function needs a font, so we can't get the virama glyph
191 * during shape planning... Instead, overwrite it here. It's safe. Don't worry! */
192 virama_glyph = glyph;
193 }
194
195 *pglyph = glyph;
196 return glyph != 0;
197 }
198
199 mutable hb_codepoint_t virama_glyph;
200
201 would_substitute_feature_t pref;
202
203 hb_mask_t mask_array[KHMER_NUM_FEATURES];
204 };
205
206 static void *
data_create_khmer(const hb_ot_shape_plan_t * plan)207 data_create_khmer (const hb_ot_shape_plan_t *plan)
208 {
209 khmer_shape_plan_t *khmer_plan = (khmer_shape_plan_t *) calloc (1, sizeof (khmer_shape_plan_t));
210 if (unlikely (!khmer_plan))
211 return nullptr;
212
213 khmer_plan->virama_glyph = (hb_codepoint_t) -1;
214
215 khmer_plan->pref.init (&plan->map, HB_TAG('p','r','e','f'), true);
216
217 for (unsigned int i = 0; i < ARRAY_LENGTH (khmer_plan->mask_array); i++)
218 khmer_plan->mask_array[i] = (khmer_features[i].flags & F_GLOBAL) ?
219 0 : plan->map.get_1_mask (khmer_features[i].tag);
220
221 return khmer_plan;
222 }
223
224 static void
data_destroy_khmer(void * data)225 data_destroy_khmer (void *data)
226 {
227 free (data);
228 }
229
230
231 enum syllable_type_t {
232 consonant_syllable,
233 broken_cluster,
234 non_khmer_cluster,
235 };
236
237 #include "hb-ot-shape-complex-khmer-machine.hh"
238
239 static void
setup_masks_khmer(const hb_ot_shape_plan_t * plan HB_UNUSED,hb_buffer_t * buffer,hb_font_t * font HB_UNUSED)240 setup_masks_khmer (const hb_ot_shape_plan_t *plan HB_UNUSED,
241 hb_buffer_t *buffer,
242 hb_font_t *font HB_UNUSED)
243 {
244 HB_BUFFER_ALLOCATE_VAR (buffer, khmer_category);
245
246 /* We cannot setup masks here. We save information about characters
247 * and setup masks later on in a pause-callback. */
248
249 unsigned int count = buffer->len;
250 hb_glyph_info_t *info = buffer->info;
251 for (unsigned int i = 0; i < count; i++)
252 set_khmer_properties (info[i]);
253 }
254
255 static void
setup_syllables(const hb_ot_shape_plan_t * plan HB_UNUSED,hb_font_t * font HB_UNUSED,hb_buffer_t * buffer)256 setup_syllables (const hb_ot_shape_plan_t *plan HB_UNUSED,
257 hb_font_t *font HB_UNUSED,
258 hb_buffer_t *buffer)
259 {
260 find_syllables (buffer);
261 foreach_syllable (buffer, start, end)
262 buffer->unsafe_to_break (start, end);
263 }
264
265
266 /* Rules from:
267 * https://docs.microsoft.com/en-us/typography/script-development/devanagari */
268
269 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)270 reorder_consonant_syllable (const hb_ot_shape_plan_t *plan,
271 hb_face_t *face HB_UNUSED,
272 hb_buffer_t *buffer,
273 unsigned int start, unsigned int end)
274 {
275 const khmer_shape_plan_t *khmer_plan = (const khmer_shape_plan_t *) plan->data;
276 hb_glyph_info_t *info = buffer->info;
277
278 /* Setup masks. */
279 {
280 /* Post-base */
281 hb_mask_t mask = khmer_plan->mask_array[BLWF] | khmer_plan->mask_array[ABVF] | khmer_plan->mask_array[PSTF];
282 for (unsigned int i = start + 1; i < end; i++)
283 info[i].mask |= mask;
284 }
285
286 unsigned int num_coengs = 0;
287 for (unsigned int i = start + 1; i < end; i++)
288 {
289 /* """
290 * When a COENG + (Cons | IndV) combination are found (and subscript count
291 * is less than two) the character combination is handled according to the
292 * subscript type of the character following the COENG.
293 *
294 * ...
295 *
296 * Subscript Type 2 - The COENG + RO characters are reordered to immediately
297 * before the base glyph. Then the COENG + RO characters are assigned to have
298 * the 'pref' OpenType feature applied to them.
299 * """
300 */
301 if (info[i].khmer_category() == OT_Coeng && num_coengs <= 2 && i + 1 < end)
302 {
303 num_coengs++;
304
305 if (info[i + 1].khmer_category() == OT_Ra)
306 {
307 for (unsigned int j = 0; j < 2; j++)
308 info[i + j].mask |= khmer_plan->mask_array[PREF];
309
310 /* Move the Coeng,Ro sequence to the start. */
311 buffer->merge_clusters (start, i + 2);
312 hb_glyph_info_t t0 = info[i];
313 hb_glyph_info_t t1 = info[i + 1];
314 memmove (&info[start + 2], &info[start], (i - start) * sizeof (info[0]));
315 info[start] = t0;
316 info[start + 1] = t1;
317
318 /* Mark the subsequent stuff with 'cfar'. Used in Khmer.
319 * Read the feature spec.
320 * This allows distinguishing the following cases with MS Khmer fonts:
321 * U+1784,U+17D2,U+179A,U+17D2,U+1782
322 * U+1784,U+17D2,U+1782,U+17D2,U+179A
323 */
324 if (khmer_plan->mask_array[CFAR])
325 for (unsigned int j = i + 2; j < end; j++)
326 info[j].mask |= khmer_plan->mask_array[CFAR];
327
328 num_coengs = 2; /* Done. */
329 }
330 }
331
332 /* Reorder left matra piece. */
333 else if (info[i].khmer_category() == OT_VPre)
334 {
335 /* Move to the start. */
336 buffer->merge_clusters (start, i + 1);
337 hb_glyph_info_t t = info[i];
338 memmove (&info[start + 1], &info[start], (i - start) * sizeof (info[0]));
339 info[start] = t;
340 }
341 }
342 }
343
344 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)345 initial_reordering_syllable (const hb_ot_shape_plan_t *plan,
346 hb_face_t *face,
347 hb_buffer_t *buffer,
348 unsigned int start, unsigned int end)
349 {
350 syllable_type_t syllable_type = (syllable_type_t) (buffer->info[start].syllable() & 0x0F);
351 switch (syllable_type)
352 {
353 case broken_cluster: /* We already inserted dotted-circles, so just call the consonant_syllable. */
354 case consonant_syllable:
355 reorder_consonant_syllable (plan, face, buffer, start, end);
356 break;
357
358 case non_khmer_cluster:
359 break;
360 }
361 }
362
363 static inline void
insert_dotted_circles(const hb_ot_shape_plan_t * plan HB_UNUSED,hb_font_t * font,hb_buffer_t * buffer)364 insert_dotted_circles (const hb_ot_shape_plan_t *plan HB_UNUSED,
365 hb_font_t *font,
366 hb_buffer_t *buffer)
367 {
368 /* Note: This loop is extra overhead, but should not be measurable. */
369 bool has_broken_syllables = false;
370 unsigned int count = buffer->len;
371 hb_glyph_info_t *info = buffer->info;
372 for (unsigned int i = 0; i < count; i++)
373 if ((info[i].syllable() & 0x0F) == broken_cluster)
374 {
375 has_broken_syllables = true;
376 break;
377 }
378 if (likely (!has_broken_syllables))
379 return;
380
381
382 hb_codepoint_t dottedcircle_glyph;
383 if (!font->get_nominal_glyph (0x25CCu, &dottedcircle_glyph))
384 return;
385
386 hb_glyph_info_t dottedcircle = {0};
387 dottedcircle.codepoint = 0x25CCu;
388 set_khmer_properties (dottedcircle);
389 dottedcircle.codepoint = dottedcircle_glyph;
390
391 buffer->clear_output ();
392
393 buffer->idx = 0;
394 unsigned int last_syllable = 0;
395 while (buffer->idx < buffer->len && buffer->successful)
396 {
397 unsigned int syllable = buffer->cur().syllable();
398 syllable_type_t syllable_type = (syllable_type_t) (syllable & 0x0F);
399 if (unlikely (last_syllable != syllable && syllable_type == broken_cluster))
400 {
401 last_syllable = syllable;
402
403 hb_glyph_info_t ginfo = dottedcircle;
404 ginfo.cluster = buffer->cur().cluster;
405 ginfo.mask = buffer->cur().mask;
406 ginfo.syllable() = buffer->cur().syllable();
407 /* TODO Set glyph_props? */
408
409 /* Insert dottedcircle after possible Repha. */
410 while (buffer->idx < buffer->len && buffer->successful &&
411 last_syllable == buffer->cur().syllable() &&
412 buffer->cur().khmer_category() == OT_Repha)
413 buffer->next_glyph ();
414
415 buffer->output_info (ginfo);
416 }
417 else
418 buffer->next_glyph ();
419 }
420 buffer->swap_buffers ();
421 }
422
423 static void
reorder(const hb_ot_shape_plan_t * plan,hb_font_t * font,hb_buffer_t * buffer)424 reorder (const hb_ot_shape_plan_t *plan,
425 hb_font_t *font,
426 hb_buffer_t *buffer)
427 {
428 insert_dotted_circles (plan, font, buffer);
429
430 foreach_syllable (buffer, start, end)
431 initial_reordering_syllable (plan, font->face, buffer, start, end);
432
433 HB_BUFFER_DEALLOCATE_VAR (buffer, khmer_category);
434 }
435
436 static void
clear_syllables(const hb_ot_shape_plan_t * plan HB_UNUSED,hb_font_t * font HB_UNUSED,hb_buffer_t * buffer)437 clear_syllables (const hb_ot_shape_plan_t *plan HB_UNUSED,
438 hb_font_t *font HB_UNUSED,
439 hb_buffer_t *buffer)
440 {
441 hb_glyph_info_t *info = buffer->info;
442 unsigned int count = buffer->len;
443 for (unsigned int i = 0; i < count; i++)
444 info[i].syllable() = 0;
445 }
446
447
448 static bool
decompose_khmer(const hb_ot_shape_normalize_context_t * c,hb_codepoint_t ab,hb_codepoint_t * a,hb_codepoint_t * b)449 decompose_khmer (const hb_ot_shape_normalize_context_t *c,
450 hb_codepoint_t ab,
451 hb_codepoint_t *a,
452 hb_codepoint_t *b)
453 {
454 switch (ab)
455 {
456 /*
457 * Decompose split matras that don't have Unicode decompositions.
458 */
459
460 /* Khmer */
461 case 0x17BEu : *a = 0x17C1u; *b= 0x17BEu; return true;
462 case 0x17BFu : *a = 0x17C1u; *b= 0x17BFu; return true;
463 case 0x17C0u : *a = 0x17C1u; *b= 0x17C0u; return true;
464 case 0x17C4u : *a = 0x17C1u; *b= 0x17C4u; return true;
465 case 0x17C5u : *a = 0x17C1u; *b= 0x17C5u; return true;
466 }
467
468 return (bool) c->unicode->decompose (ab, a, b);
469 }
470
471 static bool
compose_khmer(const hb_ot_shape_normalize_context_t * c,hb_codepoint_t a,hb_codepoint_t b,hb_codepoint_t * ab)472 compose_khmer (const hb_ot_shape_normalize_context_t *c,
473 hb_codepoint_t a,
474 hb_codepoint_t b,
475 hb_codepoint_t *ab)
476 {
477 /* Avoid recomposing split matras. */
478 if (HB_UNICODE_GENERAL_CATEGORY_IS_MARK (c->unicode->general_category (a)))
479 return false;
480
481 return (bool) c->unicode->compose (a, b, ab);
482 }
483
484
485 const hb_ot_complex_shaper_t _hb_ot_complex_shaper_khmer =
486 {
487 collect_features_khmer,
488 override_features_khmer,
489 data_create_khmer,
490 data_destroy_khmer,
491 nullptr, /* preprocess_text */
492 nullptr, /* postprocess_glyphs */
493 HB_OT_SHAPE_NORMALIZATION_MODE_COMPOSED_DIACRITICS_NO_SHORT_CIRCUIT,
494 decompose_khmer,
495 compose_khmer,
496 setup_masks_khmer,
497 HB_TAG_NONE, /* gpos_tag */
498 nullptr, /* reorder_marks */
499 HB_OT_SHAPE_ZERO_WIDTH_MARKS_NONE,
500 false, /* fallback_position */
501 };
502