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-indic.hh"
28 #include "hb-ot-shape-complex-vowel-constraints.hh"
29 #include "hb-ot-layout.hh"
30
31
32 /*
33 * Indic shaper.
34 */
35
36
37 /*
38 * Indic configurations. Note that we do not want to keep every single script-specific
39 * behavior in these tables necessarily. This should mainly be used for per-script
40 * properties that are cheaper keeping here, than in the code. Ie. if, say, one and
41 * only one script has an exception, that one script can be if'ed directly in the code,
42 * instead of adding a new flag in these structs.
43 */
44
45 enum base_position_t {
46 BASE_POS_LAST_SINHALA,
47 BASE_POS_LAST
48 };
49 enum reph_position_t {
50 REPH_POS_AFTER_MAIN = POS_AFTER_MAIN,
51 REPH_POS_BEFORE_SUB = POS_BEFORE_SUB,
52 REPH_POS_AFTER_SUB = POS_AFTER_SUB,
53 REPH_POS_BEFORE_POST = POS_BEFORE_POST,
54 REPH_POS_AFTER_POST = POS_AFTER_POST
55 };
56 enum reph_mode_t {
57 REPH_MODE_IMPLICIT, /* Reph formed out of initial Ra,H sequence. */
58 REPH_MODE_EXPLICIT, /* Reph formed out of initial Ra,H,ZWJ sequence. */
59 REPH_MODE_LOG_REPHA /* Encoded Repha character, needs reordering. */
60 };
61 enum blwf_mode_t {
62 BLWF_MODE_PRE_AND_POST, /* Below-forms feature applied to pre-base and post-base. */
63 BLWF_MODE_POST_ONLY /* Below-forms feature applied to post-base only. */
64 };
65 struct indic_config_t
66 {
67 hb_script_t script;
68 bool has_old_spec;
69 hb_codepoint_t virama;
70 base_position_t base_pos;
71 reph_position_t reph_pos;
72 reph_mode_t reph_mode;
73 blwf_mode_t blwf_mode;
74 };
75
76 static const indic_config_t indic_configs[] =
77 {
78 /* Default. Should be first. */
79 {HB_SCRIPT_INVALID, false, 0,BASE_POS_LAST, REPH_POS_BEFORE_POST,REPH_MODE_IMPLICIT, BLWF_MODE_PRE_AND_POST},
80 {HB_SCRIPT_DEVANAGARI,true, 0x094Du,BASE_POS_LAST, REPH_POS_BEFORE_POST,REPH_MODE_IMPLICIT, BLWF_MODE_PRE_AND_POST},
81 {HB_SCRIPT_BENGALI, true, 0x09CDu,BASE_POS_LAST, REPH_POS_AFTER_SUB, REPH_MODE_IMPLICIT, BLWF_MODE_PRE_AND_POST},
82 {HB_SCRIPT_GURMUKHI, true, 0x0A4Du,BASE_POS_LAST, REPH_POS_BEFORE_SUB, REPH_MODE_IMPLICIT, BLWF_MODE_PRE_AND_POST},
83 {HB_SCRIPT_GUJARATI, true, 0x0ACDu,BASE_POS_LAST, REPH_POS_BEFORE_POST,REPH_MODE_IMPLICIT, BLWF_MODE_PRE_AND_POST},
84 {HB_SCRIPT_ORIYA, true, 0x0B4Du,BASE_POS_LAST, REPH_POS_AFTER_MAIN, REPH_MODE_IMPLICIT, BLWF_MODE_PRE_AND_POST},
85 {HB_SCRIPT_TAMIL, true, 0x0BCDu,BASE_POS_LAST, REPH_POS_AFTER_POST, REPH_MODE_IMPLICIT, BLWF_MODE_PRE_AND_POST},
86 {HB_SCRIPT_TELUGU, true, 0x0C4Du,BASE_POS_LAST, REPH_POS_AFTER_POST, REPH_MODE_EXPLICIT, BLWF_MODE_POST_ONLY},
87 {HB_SCRIPT_KANNADA, true, 0x0CCDu,BASE_POS_LAST, REPH_POS_AFTER_POST, REPH_MODE_IMPLICIT, BLWF_MODE_POST_ONLY},
88 {HB_SCRIPT_MALAYALAM, true, 0x0D4Du,BASE_POS_LAST, REPH_POS_AFTER_MAIN, REPH_MODE_LOG_REPHA,BLWF_MODE_PRE_AND_POST},
89 {HB_SCRIPT_SINHALA, false,0x0DCAu,BASE_POS_LAST_SINHALA,
90 REPH_POS_AFTER_POST, REPH_MODE_EXPLICIT, BLWF_MODE_PRE_AND_POST},
91 };
92
93
94
95 /*
96 * Indic shaper.
97 */
98
99 static const hb_ot_map_feature_t
100 indic_features[] =
101 {
102 /*
103 * Basic features.
104 * These features are applied in order, one at a time, after initial_reordering.
105 */
106 {HB_TAG('n','u','k','t'), F_GLOBAL_MANUAL_JOINERS},
107 {HB_TAG('a','k','h','n'), F_GLOBAL_MANUAL_JOINERS},
108 {HB_TAG('r','p','h','f'), F_MANUAL_JOINERS},
109 {HB_TAG('r','k','r','f'), F_GLOBAL_MANUAL_JOINERS},
110 {HB_TAG('p','r','e','f'), F_MANUAL_JOINERS},
111 {HB_TAG('b','l','w','f'), F_MANUAL_JOINERS},
112 {HB_TAG('a','b','v','f'), F_MANUAL_JOINERS},
113 {HB_TAG('h','a','l','f'), F_MANUAL_JOINERS},
114 {HB_TAG('p','s','t','f'), F_MANUAL_JOINERS},
115 {HB_TAG('v','a','t','u'), F_GLOBAL_MANUAL_JOINERS},
116 {HB_TAG('c','j','c','t'), F_GLOBAL_MANUAL_JOINERS},
117 /*
118 * Other features.
119 * These features are applied all at once, after final_reordering
120 * but before clearing syllables.
121 * Default Bengali font in Windows for example has intermixed
122 * lookups for init,pres,abvs,blws features.
123 */
124 {HB_TAG('i','n','i','t'), F_MANUAL_JOINERS},
125 {HB_TAG('p','r','e','s'), F_GLOBAL_MANUAL_JOINERS},
126 {HB_TAG('a','b','v','s'), F_GLOBAL_MANUAL_JOINERS},
127 {HB_TAG('b','l','w','s'), F_GLOBAL_MANUAL_JOINERS},
128 {HB_TAG('p','s','t','s'), F_GLOBAL_MANUAL_JOINERS},
129 {HB_TAG('h','a','l','n'), F_GLOBAL_MANUAL_JOINERS},
130 /*
131 * Positioning features.
132 * We don't care about the types.
133 */
134 {HB_TAG('d','i','s','t'), F_GLOBAL},
135 {HB_TAG('a','b','v','m'), F_GLOBAL},
136 {HB_TAG('b','l','w','m'), F_GLOBAL},
137 };
138
139 /*
140 * Must be in the same order as the indic_features array.
141 */
142 enum {
143 _NUKT,
144 _AKHN,
145 RPHF,
146 _RKRF,
147 PREF,
148 BLWF,
149 ABVF,
150 HALF,
151 PSTF,
152 _VATU,
153 _CJCT,
154
155 INIT,
156 _PRES,
157 _ABVS,
158 _BLWS,
159 _PSTS,
160 _HALN,
161
162 _DIST,
163 _ABVM,
164 _BLWM,
165
166 INDIC_NUM_FEATURES,
167 INDIC_BASIC_FEATURES = INIT, /* Don't forget to update this! */
168 };
169
170 static void
171 setup_syllables (const hb_ot_shape_plan_t *plan,
172 hb_font_t *font,
173 hb_buffer_t *buffer);
174 static void
175 initial_reordering (const hb_ot_shape_plan_t *plan,
176 hb_font_t *font,
177 hb_buffer_t *buffer);
178 static void
179 final_reordering (const hb_ot_shape_plan_t *plan,
180 hb_font_t *font,
181 hb_buffer_t *buffer);
182 static void
183 clear_syllables (const hb_ot_shape_plan_t *plan,
184 hb_font_t *font,
185 hb_buffer_t *buffer);
186
187 static void
collect_features_indic(hb_ot_shape_planner_t * plan)188 collect_features_indic (hb_ot_shape_planner_t *plan)
189 {
190 hb_ot_map_builder_t *map = &plan->map;
191
192 /* Do this before any lookups have been applied. */
193 map->add_gsub_pause (setup_syllables);
194
195 map->enable_feature (HB_TAG('l','o','c','l'));
196 /* The Indic specs do not require ccmp, but we apply it here since if
197 * there is a use of it, it's typically at the beginning. */
198 map->enable_feature (HB_TAG('c','c','m','p'));
199
200
201 unsigned int i = 0;
202 map->add_gsub_pause (initial_reordering);
203
204 for (; i < INDIC_BASIC_FEATURES; i++) {
205 map->add_feature (indic_features[i]);
206 map->add_gsub_pause (nullptr);
207 }
208
209 map->add_gsub_pause (final_reordering);
210
211 for (; i < INDIC_NUM_FEATURES; i++)
212 map->add_feature (indic_features[i]);
213
214 map->enable_feature (HB_TAG('c','a','l','t'));
215 map->enable_feature (HB_TAG('c','l','i','g'));
216
217 map->add_gsub_pause (clear_syllables);
218 }
219
220 static void
override_features_indic(hb_ot_shape_planner_t * plan)221 override_features_indic (hb_ot_shape_planner_t *plan)
222 {
223 plan->map.disable_feature (HB_TAG('l','i','g','a'));
224 }
225
226
227 struct would_substitute_feature_t
228 {
initwould_substitute_feature_t229 void init (const hb_ot_map_t *map, hb_tag_t feature_tag, bool zero_context_)
230 {
231 zero_context = zero_context_;
232 map->get_stage_lookups (0/*GSUB*/,
233 map->get_feature_stage (0/*GSUB*/, feature_tag),
234 &lookups, &count);
235 }
236
would_substitutewould_substitute_feature_t237 bool would_substitute (const hb_codepoint_t *glyphs,
238 unsigned int glyphs_count,
239 hb_face_t *face) const
240 {
241 for (unsigned int i = 0; i < count; i++)
242 if (hb_ot_layout_lookup_would_substitute (face, lookups[i].index, glyphs, glyphs_count, zero_context))
243 return true;
244 return false;
245 }
246
247 private:
248 const hb_ot_map_t::lookup_map_t *lookups;
249 unsigned int count;
250 bool zero_context;
251 };
252
253 struct indic_shape_plan_t
254 {
load_virama_glyphindic_shape_plan_t255 bool load_virama_glyph (hb_font_t *font, hb_codepoint_t *pglyph) const
256 {
257 hb_codepoint_t glyph = virama_glyph.get_relaxed ();
258 if (unlikely (glyph == (hb_codepoint_t) -1))
259 {
260 if (!config->virama || !font->get_nominal_glyph (config->virama, &glyph))
261 glyph = 0;
262 /* Technically speaking, the spec says we should apply 'locl' to virama too.
263 * Maybe one day... */
264
265 /* Our get_nominal_glyph() function needs a font, so we can't get the virama glyph
266 * during shape planning... Instead, overwrite it here. */
267 virama_glyph.set_relaxed ((int) glyph);
268 }
269
270 *pglyph = glyph;
271 return glyph != 0;
272 }
273
274 const indic_config_t *config;
275
276 bool is_old_spec;
277 #ifndef HB_NO_UNISCRIBE_BUG_COMPATIBLE
278 bool uniscribe_bug_compatible;
279 #else
280 static constexpr bool uniscribe_bug_compatible = false;
281 #endif
282 mutable hb_atomic_int_t virama_glyph;
283
284 would_substitute_feature_t rphf;
285 would_substitute_feature_t pref;
286 would_substitute_feature_t blwf;
287 would_substitute_feature_t pstf;
288
289 hb_mask_t mask_array[INDIC_NUM_FEATURES];
290 };
291
292 static void *
data_create_indic(const hb_ot_shape_plan_t * plan)293 data_create_indic (const hb_ot_shape_plan_t *plan)
294 {
295 indic_shape_plan_t *indic_plan = (indic_shape_plan_t *) calloc (1, sizeof (indic_shape_plan_t));
296 if (unlikely (!indic_plan))
297 return nullptr;
298
299 indic_plan->config = &indic_configs[0];
300 for (unsigned int i = 1; i < ARRAY_LENGTH (indic_configs); i++)
301 if (plan->props.script == indic_configs[i].script) {
302 indic_plan->config = &indic_configs[i];
303 break;
304 }
305
306 indic_plan->is_old_spec = indic_plan->config->has_old_spec && ((plan->map.chosen_script[0] & 0x000000FFu) != '2');
307 #ifndef HB_NO_UNISCRIBE_BUG_COMPATIBLE
308 indic_plan->uniscribe_bug_compatible = hb_options ().uniscribe_bug_compatible;
309 #endif
310 indic_plan->virama_glyph.set_relaxed (-1);
311
312 /* Use zero-context would_substitute() matching for new-spec of the main
313 * Indic scripts, and scripts with one spec only, but not for old-specs.
314 * The new-spec for all dual-spec scripts says zero-context matching happens.
315 *
316 * However, testing with Malayalam shows that old and new spec both allow
317 * context. Testing with Bengali new-spec however shows that it doesn't.
318 * So, the heuristic here is the way it is. It should *only* be changed,
319 * as we discover more cases of what Windows does. DON'T TOUCH OTHERWISE.
320 */
321 bool zero_context = !indic_plan->is_old_spec && plan->props.script != HB_SCRIPT_MALAYALAM;
322 indic_plan->rphf.init (&plan->map, HB_TAG('r','p','h','f'), zero_context);
323 indic_plan->pref.init (&plan->map, HB_TAG('p','r','e','f'), zero_context);
324 indic_plan->blwf.init (&plan->map, HB_TAG('b','l','w','f'), zero_context);
325 indic_plan->pstf.init (&plan->map, HB_TAG('p','s','t','f'), zero_context);
326
327 for (unsigned int i = 0; i < ARRAY_LENGTH (indic_plan->mask_array); i++)
328 indic_plan->mask_array[i] = (indic_features[i].flags & F_GLOBAL) ?
329 0 : plan->map.get_1_mask (indic_features[i].tag);
330
331 return indic_plan;
332 }
333
334 static void
data_destroy_indic(void * data)335 data_destroy_indic (void *data)
336 {
337 free (data);
338 }
339
340 static indic_position_t
consonant_position_from_face(const indic_shape_plan_t * indic_plan,const hb_codepoint_t consonant,const hb_codepoint_t virama,hb_face_t * face)341 consonant_position_from_face (const indic_shape_plan_t *indic_plan,
342 const hb_codepoint_t consonant,
343 const hb_codepoint_t virama,
344 hb_face_t *face)
345 {
346 /* For old-spec, the order of glyphs is Consonant,Virama,
347 * whereas for new-spec, it's Virama,Consonant. However,
348 * some broken fonts (like Free Sans) simply copied lookups
349 * from old-spec to new-spec without modification.
350 * And oddly enough, Uniscribe seems to respect those lookups.
351 * Eg. in the sequence U+0924,U+094D,U+0930, Uniscribe finds
352 * base at 0. The font however, only has lookups matching
353 * 930,94D in 'blwf', not the expected 94D,930 (with new-spec
354 * table). As such, we simply match both sequences. Seems
355 * to work. */
356 hb_codepoint_t glyphs[3] = {virama, consonant, virama};
357 if (indic_plan->blwf.would_substitute (glyphs , 2, face) ||
358 indic_plan->blwf.would_substitute (glyphs+1, 2, face))
359 return POS_BELOW_C;
360 if (indic_plan->pstf.would_substitute (glyphs , 2, face) ||
361 indic_plan->pstf.would_substitute (glyphs+1, 2, face))
362 return POS_POST_C;
363 if (indic_plan->pref.would_substitute (glyphs , 2, face) ||
364 indic_plan->pref.would_substitute (glyphs+1, 2, face))
365 return POS_POST_C;
366 return POS_BASE_C;
367 }
368
369
370 enum syllable_type_t {
371 consonant_syllable,
372 vowel_syllable,
373 standalone_cluster,
374 symbol_cluster,
375 broken_cluster,
376 non_indic_cluster,
377 };
378
379 #include "hb-ot-shape-complex-indic-machine.hh"
380
381
382 static void
setup_masks_indic(const hb_ot_shape_plan_t * plan HB_UNUSED,hb_buffer_t * buffer,hb_font_t * font HB_UNUSED)383 setup_masks_indic (const hb_ot_shape_plan_t *plan HB_UNUSED,
384 hb_buffer_t *buffer,
385 hb_font_t *font HB_UNUSED)
386 {
387 HB_BUFFER_ALLOCATE_VAR (buffer, indic_category);
388 HB_BUFFER_ALLOCATE_VAR (buffer, indic_position);
389
390 /* We cannot setup masks here. We save information about characters
391 * and setup masks later on in a pause-callback. */
392
393 unsigned int count = buffer->len;
394 hb_glyph_info_t *info = buffer->info;
395 for (unsigned int i = 0; i < count; i++)
396 set_indic_properties (info[i]);
397 }
398
399 static void
setup_syllables(const hb_ot_shape_plan_t * plan HB_UNUSED,hb_font_t * font HB_UNUSED,hb_buffer_t * buffer)400 setup_syllables (const hb_ot_shape_plan_t *plan HB_UNUSED,
401 hb_font_t *font HB_UNUSED,
402 hb_buffer_t *buffer)
403 {
404 find_syllables (buffer);
405 foreach_syllable (buffer, start, end)
406 buffer->unsafe_to_break (start, end);
407 }
408
409 static int
compare_indic_order(const hb_glyph_info_t * pa,const hb_glyph_info_t * pb)410 compare_indic_order (const hb_glyph_info_t *pa, const hb_glyph_info_t *pb)
411 {
412 int a = pa->indic_position();
413 int b = pb->indic_position();
414
415 return a < b ? -1 : a == b ? 0 : +1;
416 }
417
418
419
420 static void
update_consonant_positions(const hb_ot_shape_plan_t * plan,hb_font_t * font,hb_buffer_t * buffer)421 update_consonant_positions (const hb_ot_shape_plan_t *plan,
422 hb_font_t *font,
423 hb_buffer_t *buffer)
424 {
425 const indic_shape_plan_t *indic_plan = (const indic_shape_plan_t *) plan->data;
426
427 if (indic_plan->config->base_pos != BASE_POS_LAST)
428 return;
429
430 hb_codepoint_t virama;
431 if (indic_plan->load_virama_glyph (font, &virama))
432 {
433 hb_face_t *face = font->face;
434 unsigned int count = buffer->len;
435 hb_glyph_info_t *info = buffer->info;
436 for (unsigned int i = 0; i < count; i++)
437 if (info[i].indic_position() == POS_BASE_C)
438 {
439 hb_codepoint_t consonant = info[i].codepoint;
440 info[i].indic_position() = consonant_position_from_face (indic_plan, consonant, virama, face);
441 }
442 }
443 }
444
445
446 /* Rules from:
447 * https://docs.microsqoft.com/en-us/typography/script-development/devanagari */
448
449 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)450 initial_reordering_consonant_syllable (const hb_ot_shape_plan_t *plan,
451 hb_face_t *face,
452 hb_buffer_t *buffer,
453 unsigned int start, unsigned int end)
454 {
455 const indic_shape_plan_t *indic_plan = (const indic_shape_plan_t *) plan->data;
456 hb_glyph_info_t *info = buffer->info;
457
458 /* https://github.com/harfbuzz/harfbuzz/issues/435#issuecomment-335560167
459 * // For compatibility with legacy usage in Kannada,
460 * // Ra+h+ZWJ must behave like Ra+ZWJ+h...
461 */
462 if (buffer->props.script == HB_SCRIPT_KANNADA &&
463 start + 3 <= end &&
464 is_one_of (info[start ], FLAG (OT_Ra)) &&
465 is_one_of (info[start+1], FLAG (OT_H)) &&
466 is_one_of (info[start+2], FLAG (OT_ZWJ)))
467 {
468 buffer->merge_clusters (start+1, start+3);
469 hb_glyph_info_t tmp = info[start+1];
470 info[start+1] = info[start+2];
471 info[start+2] = tmp;
472 }
473
474 /* 1. Find base consonant:
475 *
476 * The shaping engine finds the base consonant of the syllable, using the
477 * following algorithm: starting from the end of the syllable, move backwards
478 * until a consonant is found that does not have a below-base or post-base
479 * form (post-base forms have to follow below-base forms), or that is not a
480 * pre-base-reordering Ra, or arrive at the first consonant. The consonant
481 * stopped at will be the base.
482 *
483 * o If the syllable starts with Ra + Halant (in a script that has Reph)
484 * and has more than one consonant, Ra is excluded from candidates for
485 * base consonants.
486 */
487
488 unsigned int base = end;
489 bool has_reph = false;
490
491 {
492 /* -> If the syllable starts with Ra + Halant (in a script that has Reph)
493 * and has more than one consonant, Ra is excluded from candidates for
494 * base consonants. */
495 unsigned int limit = start;
496 if (indic_plan->mask_array[RPHF] &&
497 start + 3 <= end &&
498 (
499 (indic_plan->config->reph_mode == REPH_MODE_IMPLICIT && !is_joiner (info[start + 2])) ||
500 (indic_plan->config->reph_mode == REPH_MODE_EXPLICIT && info[start + 2].indic_category() == OT_ZWJ)
501 ))
502 {
503 /* See if it matches the 'rphf' feature. */
504 hb_codepoint_t glyphs[3] = {info[start].codepoint,
505 info[start + 1].codepoint,
506 indic_plan->config->reph_mode == REPH_MODE_EXPLICIT ?
507 info[start + 2].codepoint : 0};
508 if (indic_plan->rphf.would_substitute (glyphs, 2, face) ||
509 (indic_plan->config->reph_mode == REPH_MODE_EXPLICIT &&
510 indic_plan->rphf.would_substitute (glyphs, 3, face)))
511 {
512 limit += 2;
513 while (limit < end && is_joiner (info[limit]))
514 limit++;
515 base = start;
516 has_reph = true;
517 }
518 } else if (indic_plan->config->reph_mode == REPH_MODE_LOG_REPHA && info[start].indic_category() == OT_Repha)
519 {
520 limit += 1;
521 while (limit < end && is_joiner (info[limit]))
522 limit++;
523 base = start;
524 has_reph = true;
525 }
526
527 switch (indic_plan->config->base_pos)
528 {
529 case BASE_POS_LAST:
530 {
531 /* -> starting from the end of the syllable, move backwards */
532 unsigned int i = end;
533 bool seen_below = false;
534 do {
535 i--;
536 /* -> until a consonant is found */
537 if (is_consonant (info[i]))
538 {
539 /* -> that does not have a below-base or post-base form
540 * (post-base forms have to follow below-base forms), */
541 if (info[i].indic_position() != POS_BELOW_C &&
542 (info[i].indic_position() != POS_POST_C || seen_below))
543 {
544 base = i;
545 break;
546 }
547 if (info[i].indic_position() == POS_BELOW_C)
548 seen_below = true;
549
550 /* -> or that is not a pre-base-reordering Ra,
551 *
552 * IMPLEMENTATION NOTES:
553 *
554 * Our pre-base-reordering Ra's are marked POS_POST_C, so will be skipped
555 * by the logic above already.
556 */
557
558 /* -> or arrive at the first consonant. The consonant stopped at will
559 * be the base. */
560 base = i;
561 }
562 else
563 {
564 /* A ZWJ after a Halant stops the base search, and requests an explicit
565 * half form.
566 * A ZWJ before a Halant, requests a subjoined form instead, and hence
567 * search continues. This is particularly important for Bengali
568 * sequence Ra,H,Ya that should form Ya-Phalaa by subjoining Ya. */
569 if (start < i &&
570 info[i].indic_category() == OT_ZWJ &&
571 info[i - 1].indic_category() == OT_H)
572 break;
573 }
574 } while (i > limit);
575 }
576 break;
577
578 case BASE_POS_LAST_SINHALA:
579 {
580 /* Sinhala base positioning is slightly different from main Indic, in that:
581 * 1. Its ZWJ behavior is different,
582 * 2. We don't need to look into the font for consonant positions.
583 */
584
585 if (!has_reph)
586 base = limit;
587
588 /* Find the last base consonant that is not blocked by ZWJ. If there is
589 * a ZWJ right before a base consonant, that would request a subjoined form. */
590 for (unsigned int i = limit; i < end; i++)
591 if (is_consonant (info[i]))
592 {
593 if (limit < i && info[i - 1].indic_category() == OT_ZWJ)
594 break;
595 else
596 base = i;
597 }
598
599 /* Mark all subsequent consonants as below. */
600 for (unsigned int i = base + 1; i < end; i++)
601 if (is_consonant (info[i]))
602 info[i].indic_position() = POS_BELOW_C;
603 }
604 break;
605 }
606
607 /* -> If the syllable starts with Ra + Halant (in a script that has Reph)
608 * and has more than one consonant, Ra is excluded from candidates for
609 * base consonants.
610 *
611 * Only do this for unforced Reph. (ie. not for Ra,H,ZWJ. */
612 if (has_reph && base == start && limit - base <= 2) {
613 /* Have no other consonant, so Reph is not formed and Ra becomes base. */
614 has_reph = false;
615 }
616 }
617
618
619 /* 2. Decompose and reorder Matras:
620 *
621 * Each matra and any syllable modifier sign in the syllable are moved to the
622 * appropriate position relative to the consonant(s) in the syllable. The
623 * shaping engine decomposes two- or three-part matras into their constituent
624 * parts before any repositioning. Matra characters are classified by which
625 * consonant in a conjunct they have affinity for and are reordered to the
626 * following positions:
627 *
628 * o Before first half form in the syllable
629 * o After subjoined consonants
630 * o After post-form consonant
631 * o After main consonant (for above marks)
632 *
633 * IMPLEMENTATION NOTES:
634 *
635 * The normalize() routine has already decomposed matras for us, so we don't
636 * need to worry about that.
637 */
638
639
640 /* 3. Reorder marks to canonical order:
641 *
642 * Adjacent nukta and halant or nukta and vedic sign are always repositioned
643 * if necessary, so that the nukta is first.
644 *
645 * IMPLEMENTATION NOTES:
646 *
647 * We don't need to do this: the normalize() routine already did this for us.
648 */
649
650
651 /* Reorder characters */
652
653 for (unsigned int i = start; i < base; i++)
654 info[i].indic_position() = hb_min (POS_PRE_C, (indic_position_t) info[i].indic_position());
655
656 if (base < end)
657 info[base].indic_position() = POS_BASE_C;
658
659 /* Mark final consonants. A final consonant is one appearing after a matra.
660 * Happens in Sinhala. */
661 for (unsigned int i = base + 1; i < end; i++)
662 if (info[i].indic_category() == OT_M) {
663 for (unsigned int j = i + 1; j < end; j++)
664 if (is_consonant (info[j])) {
665 info[j].indic_position() = POS_FINAL_C;
666 break;
667 }
668 break;
669 }
670
671 /* Handle beginning Ra */
672 if (has_reph)
673 info[start].indic_position() = POS_RA_TO_BECOME_REPH;
674
675 /* For old-style Indic script tags, move the first post-base Halant after
676 * last consonant.
677 *
678 * Reports suggest that in some scripts Uniscribe does this only if there
679 * is *not* a Halant after last consonant already. We know that is the
680 * case for Kannada, while it reorders unconditionally in other scripts,
681 * eg. Malayalam, Bengali, and Devanagari. We don't currently know about
682 * other scripts, so we blacklist Kannada.
683 *
684 * Kannada test case:
685 * U+0C9A,U+0CCD,U+0C9A,U+0CCD
686 * With some versions of Lohit Kannada.
687 * https://bugs.freedesktop.org/show_bug.cgi?id=59118
688 *
689 * Malayalam test case:
690 * U+0D38,U+0D4D,U+0D31,U+0D4D,U+0D31,U+0D4D
691 * With lohit-ttf-20121122/Lohit-Malayalam.ttf
692 *
693 * Bengali test case:
694 * U+0998,U+09CD,U+09AF,U+09CD
695 * With Windows XP vrinda.ttf
696 * https://github.com/harfbuzz/harfbuzz/issues/1073
697 *
698 * Devanagari test case:
699 * U+091F,U+094D,U+0930,U+094D
700 * With chandas.ttf
701 * https://github.com/harfbuzz/harfbuzz/issues/1071
702 */
703 if (indic_plan->is_old_spec)
704 {
705 bool disallow_double_halants = buffer->props.script == HB_SCRIPT_KANNADA;
706 for (unsigned int i = base + 1; i < end; i++)
707 if (info[i].indic_category() == OT_H)
708 {
709 unsigned int j;
710 for (j = end - 1; j > i; j--)
711 if (is_consonant (info[j]) ||
712 (disallow_double_halants && info[j].indic_category() == OT_H))
713 break;
714 if (info[j].indic_category() != OT_H && j > i) {
715 /* Move Halant to after last consonant. */
716 hb_glyph_info_t t = info[i];
717 memmove (&info[i], &info[i + 1], (j - i) * sizeof (info[0]));
718 info[j] = t;
719 }
720 break;
721 }
722 }
723
724 /* Attach misc marks to previous char to move with them. */
725 {
726 indic_position_t last_pos = POS_START;
727 for (unsigned int i = start; i < end; i++)
728 {
729 if ((FLAG_UNSAFE (info[i].indic_category()) & (JOINER_FLAGS | FLAG (OT_N) | FLAG (OT_RS) | MEDIAL_FLAGS | FLAG (OT_H))))
730 {
731 info[i].indic_position() = last_pos;
732 if (unlikely (info[i].indic_category() == OT_H &&
733 info[i].indic_position() == POS_PRE_M))
734 {
735 /*
736 * Uniscribe doesn't move the Halant with Left Matra.
737 * TEST: U+092B,U+093F,U+094DE
738 * We follow. This is important for the Sinhala
739 * U+0DDA split matra since it decomposes to U+0DD9,U+0DCA
740 * where U+0DD9 is a left matra and U+0DCA is the virama.
741 * We don't want to move the virama with the left matra.
742 * TEST: U+0D9A,U+0DDA
743 */
744 for (unsigned int j = i; j > start; j--)
745 if (info[j - 1].indic_position() != POS_PRE_M) {
746 info[i].indic_position() = info[j - 1].indic_position();
747 break;
748 }
749 }
750 } else if (info[i].indic_position() != POS_SMVD) {
751 last_pos = (indic_position_t) info[i].indic_position();
752 }
753 }
754 }
755 /* For post-base consonants let them own anything before them
756 * since the last consonant or matra. */
757 {
758 unsigned int last = base;
759 for (unsigned int i = base + 1; i < end; i++)
760 if (is_consonant (info[i]))
761 {
762 for (unsigned int j = last + 1; j < i; j++)
763 if (info[j].indic_position() < POS_SMVD)
764 info[j].indic_position() = info[i].indic_position();
765 last = i;
766 } else if (info[i].indic_category() == OT_M)
767 last = i;
768 }
769
770
771 {
772 /* Use syllable() for sort accounting temporarily. */
773 unsigned int syllable = info[start].syllable();
774 for (unsigned int i = start; i < end; i++)
775 info[i].syllable() = i - start;
776
777 /* Sit tight, rock 'n roll! */
778 hb_stable_sort (info + start, end - start, compare_indic_order);
779 /* Find base again */
780 base = end;
781 for (unsigned int i = start; i < end; i++)
782 if (info[i].indic_position() == POS_BASE_C)
783 {
784 base = i;
785 break;
786 }
787 /* Things are out-of-control for post base positions, they may shuffle
788 * around like crazy. In old-spec mode, we move halants around, so in
789 * that case merge all clusters after base. Otherwise, check the sort
790 * order and merge as needed.
791 * For pre-base stuff, we handle cluster issues in final reordering.
792 *
793 * We could use buffer->sort() for this, if there was no special
794 * reordering of pre-base stuff happening later...
795 * We don't want to merge_clusters all of that, which buffer->sort()
796 * would.
797 */
798 if (indic_plan->is_old_spec || end - start > 127)
799 buffer->merge_clusters (base, end);
800 else
801 {
802 /* Note! syllable() is a one-byte field. */
803 for (unsigned int i = base; i < end; i++)
804 if (info[i].syllable() != 255)
805 {
806 unsigned int max = i;
807 unsigned int j = start + info[i].syllable();
808 while (j != i)
809 {
810 max = hb_max (max, j);
811 unsigned int next = start + info[j].syllable();
812 info[j].syllable() = 255; /* So we don't process j later again. */
813 j = next;
814 }
815 if (i != max)
816 buffer->merge_clusters (i, max + 1);
817 }
818 }
819
820 /* Put syllable back in. */
821 for (unsigned int i = start; i < end; i++)
822 info[i].syllable() = syllable;
823 }
824
825 /* Setup masks now */
826
827 {
828 hb_mask_t mask;
829
830 /* Reph */
831 for (unsigned int i = start; i < end && info[i].indic_position() == POS_RA_TO_BECOME_REPH; i++)
832 info[i].mask |= indic_plan->mask_array[RPHF];
833
834 /* Pre-base */
835 mask = indic_plan->mask_array[HALF];
836 if (!indic_plan->is_old_spec &&
837 indic_plan->config->blwf_mode == BLWF_MODE_PRE_AND_POST)
838 mask |= indic_plan->mask_array[BLWF];
839 for (unsigned int i = start; i < base; i++)
840 info[i].mask |= mask;
841 /* Base */
842 mask = 0;
843 if (base < end)
844 info[base].mask |= mask;
845 /* Post-base */
846 mask = indic_plan->mask_array[BLWF] | indic_plan->mask_array[ABVF] | indic_plan->mask_array[PSTF];
847 for (unsigned int i = base + 1; i < end; i++)
848 info[i].mask |= mask;
849 }
850
851 if (indic_plan->is_old_spec &&
852 buffer->props.script == HB_SCRIPT_DEVANAGARI)
853 {
854 /* Old-spec eye-lash Ra needs special handling. From the
855 * spec:
856 *
857 * "The feature 'below-base form' is applied to consonants
858 * having below-base forms and following the base consonant.
859 * The exception is vattu, which may appear below half forms
860 * as well as below the base glyph. The feature 'below-base
861 * form' will be applied to all such occurrences of Ra as well."
862 *
863 * Test case: U+0924,U+094D,U+0930,U+094d,U+0915
864 * with Sanskrit 2003 font.
865 *
866 * However, note that Ra,Halant,ZWJ is the correct way to
867 * request eyelash form of Ra, so we wouldbn't inhibit it
868 * in that sequence.
869 *
870 * Test case: U+0924,U+094D,U+0930,U+094d,U+200D,U+0915
871 */
872 for (unsigned int i = start; i + 1 < base; i++)
873 if (info[i ].indic_category() == OT_Ra &&
874 info[i+1].indic_category() == OT_H &&
875 (i + 2 == base ||
876 info[i+2].indic_category() != OT_ZWJ))
877 {
878 info[i ].mask |= indic_plan->mask_array[BLWF];
879 info[i+1].mask |= indic_plan->mask_array[BLWF];
880 }
881 }
882
883 unsigned int pref_len = 2;
884 if (indic_plan->mask_array[PREF] && base + pref_len < end)
885 {
886 /* Find a Halant,Ra sequence and mark it for pre-base-reordering processing. */
887 for (unsigned int i = base + 1; i + pref_len - 1 < end; i++) {
888 hb_codepoint_t glyphs[2];
889 for (unsigned int j = 0; j < pref_len; j++)
890 glyphs[j] = info[i + j].codepoint;
891 if (indic_plan->pref.would_substitute (glyphs, pref_len, face))
892 {
893 for (unsigned int j = 0; j < pref_len; j++)
894 info[i++].mask |= indic_plan->mask_array[PREF];
895 break;
896 }
897 }
898 }
899
900 /* Apply ZWJ/ZWNJ effects */
901 for (unsigned int i = start + 1; i < end; i++)
902 if (is_joiner (info[i])) {
903 bool non_joiner = info[i].indic_category() == OT_ZWNJ;
904 unsigned int j = i;
905
906 do {
907 j--;
908
909 /* ZWJ/ZWNJ should disable CJCT. They do that by simply
910 * being there, since we don't skip them for the CJCT
911 * feature (ie. F_MANUAL_ZWJ) */
912
913 /* A ZWNJ disables HALF. */
914 if (non_joiner)
915 info[j].mask &= ~indic_plan->mask_array[HALF];
916
917 } while (j > start && !is_consonant (info[j]));
918 }
919 }
920
921 static void
initial_reordering_standalone_cluster(const hb_ot_shape_plan_t * plan,hb_face_t * face,hb_buffer_t * buffer,unsigned int start,unsigned int end)922 initial_reordering_standalone_cluster (const hb_ot_shape_plan_t *plan,
923 hb_face_t *face,
924 hb_buffer_t *buffer,
925 unsigned int start, unsigned int end)
926 {
927 /* We treat placeholder/dotted-circle as if they are consonants, so we
928 * should just chain. Only if not in compatibility mode that is... */
929
930 const indic_shape_plan_t *indic_plan = (const indic_shape_plan_t *) plan->data;
931 if (indic_plan->uniscribe_bug_compatible)
932 {
933 /* For dotted-circle, this is what Uniscribe does:
934 * If dotted-circle is the last glyph, it just does nothing.
935 * Ie. It doesn't form Reph. */
936 if (buffer->info[end - 1].indic_category() == OT_DOTTEDCIRCLE)
937 return;
938 }
939
940 initial_reordering_consonant_syllable (plan, face, buffer, start, end);
941 }
942
943 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)944 initial_reordering_syllable (const hb_ot_shape_plan_t *plan,
945 hb_face_t *face,
946 hb_buffer_t *buffer,
947 unsigned int start, unsigned int end)
948 {
949 syllable_type_t syllable_type = (syllable_type_t) (buffer->info[start].syllable() & 0x0F);
950 switch (syllable_type)
951 {
952 case vowel_syllable: /* We made the vowels look like consonants. So let's call the consonant logic! */
953 case consonant_syllable:
954 initial_reordering_consonant_syllable (plan, face, buffer, start, end);
955 break;
956
957 case broken_cluster: /* We already inserted dotted-circles, so just call the standalone_cluster. */
958 case standalone_cluster:
959 initial_reordering_standalone_cluster (plan, face, buffer, start, end);
960 break;
961
962 case symbol_cluster:
963 case non_indic_cluster:
964 break;
965 }
966 }
967
968 static inline void
insert_dotted_circles(const hb_ot_shape_plan_t * plan HB_UNUSED,hb_font_t * font,hb_buffer_t * buffer)969 insert_dotted_circles (const hb_ot_shape_plan_t *plan HB_UNUSED,
970 hb_font_t *font,
971 hb_buffer_t *buffer)
972 {
973 if (unlikely (buffer->flags & HB_BUFFER_FLAG_DO_NOT_INSERT_DOTTED_CIRCLE))
974 return;
975
976 /* Note: This loop is extra overhead, but should not be measurable.
977 * TODO Use a buffer scratch flag to remove the loop. */
978 bool has_broken_syllables = false;
979 unsigned int count = buffer->len;
980 hb_glyph_info_t *info = buffer->info;
981 for (unsigned int i = 0; i < count; i++)
982 if ((info[i].syllable() & 0x0F) == broken_cluster)
983 {
984 has_broken_syllables = true;
985 break;
986 }
987 if (likely (!has_broken_syllables))
988 return;
989
990
991 hb_codepoint_t dottedcircle_glyph;
992 if (!font->get_nominal_glyph (0x25CCu, &dottedcircle_glyph))
993 return;
994
995 hb_glyph_info_t dottedcircle = {0};
996 dottedcircle.codepoint = 0x25CCu;
997 set_indic_properties (dottedcircle);
998 dottedcircle.codepoint = dottedcircle_glyph;
999
1000 buffer->clear_output ();
1001
1002 buffer->idx = 0;
1003 unsigned int last_syllable = 0;
1004 while (buffer->idx < buffer->len && buffer->successful)
1005 {
1006 unsigned int syllable = buffer->cur().syllable();
1007 syllable_type_t syllable_type = (syllable_type_t) (syllable & 0x0F);
1008 if (unlikely (last_syllable != syllable && syllable_type == broken_cluster))
1009 {
1010 last_syllable = syllable;
1011
1012 hb_glyph_info_t ginfo = dottedcircle;
1013 ginfo.cluster = buffer->cur().cluster;
1014 ginfo.mask = buffer->cur().mask;
1015 ginfo.syllable() = buffer->cur().syllable();
1016
1017 /* Insert dottedcircle after possible Repha. */
1018 while (buffer->idx < buffer->len && buffer->successful &&
1019 last_syllable == buffer->cur().syllable() &&
1020 buffer->cur().indic_category() == OT_Repha)
1021 buffer->next_glyph ();
1022
1023 buffer->output_info (ginfo);
1024 }
1025 else
1026 buffer->next_glyph ();
1027 }
1028 buffer->swap_buffers ();
1029 }
1030
1031 static void
initial_reordering(const hb_ot_shape_plan_t * plan,hb_font_t * font,hb_buffer_t * buffer)1032 initial_reordering (const hb_ot_shape_plan_t *plan,
1033 hb_font_t *font,
1034 hb_buffer_t *buffer)
1035 {
1036 update_consonant_positions (plan, font, buffer);
1037 insert_dotted_circles (plan, font, buffer);
1038
1039 foreach_syllable (buffer, start, end)
1040 initial_reordering_syllable (plan, font->face, buffer, start, end);
1041 }
1042
1043 static void
final_reordering_syllable(const hb_ot_shape_plan_t * plan,hb_buffer_t * buffer,unsigned int start,unsigned int end)1044 final_reordering_syllable (const hb_ot_shape_plan_t *plan,
1045 hb_buffer_t *buffer,
1046 unsigned int start, unsigned int end)
1047 {
1048 const indic_shape_plan_t *indic_plan = (const indic_shape_plan_t *) plan->data;
1049 hb_glyph_info_t *info = buffer->info;
1050
1051
1052 /* This function relies heavily on halant glyphs. Lots of ligation
1053 * and possibly multiple substitutions happened prior to this
1054 * phase, and that might have messed up our properties. Recover
1055 * from a particular case of that where we're fairly sure that a
1056 * class of OT_H is desired but has been lost. */
1057 /* We don't call load_virama_glyph(), since we know it's already
1058 * loaded. */
1059 hb_codepoint_t virama_glyph = indic_plan->virama_glyph.get_relaxed ();
1060 if (virama_glyph)
1061 {
1062 for (unsigned int i = start; i < end; i++)
1063 if (info[i].codepoint == virama_glyph &&
1064 _hb_glyph_info_ligated (&info[i]) &&
1065 _hb_glyph_info_multiplied (&info[i]))
1066 {
1067 /* This will make sure that this glyph passes is_halant() test. */
1068 info[i].indic_category() = OT_H;
1069 _hb_glyph_info_clear_ligated_and_multiplied (&info[i]);
1070 }
1071 }
1072
1073
1074 /* 4. Final reordering:
1075 *
1076 * After the localized forms and basic shaping forms GSUB features have been
1077 * applied (see below), the shaping engine performs some final glyph
1078 * reordering before applying all the remaining font features to the entire
1079 * syllable.
1080 */
1081
1082 bool try_pref = !!indic_plan->mask_array[PREF];
1083
1084 /* Find base again */
1085 unsigned int base;
1086 for (base = start; base < end; base++)
1087 if (info[base].indic_position() >= POS_BASE_C)
1088 {
1089 if (try_pref && base + 1 < end)
1090 {
1091 for (unsigned int i = base + 1; i < end; i++)
1092 if ((info[i].mask & indic_plan->mask_array[PREF]) != 0)
1093 {
1094 if (!(_hb_glyph_info_substituted (&info[i]) &&
1095 _hb_glyph_info_ligated_and_didnt_multiply (&info[i])))
1096 {
1097 /* Ok, this was a 'pref' candidate but didn't form any.
1098 * Base is around here... */
1099 base = i;
1100 while (base < end && is_halant (info[base]))
1101 base++;
1102 info[base].indic_position() = POS_BASE_C;
1103
1104 try_pref = false;
1105 }
1106 break;
1107 }
1108 }
1109 /* For Malayalam, skip over unformed below- (but NOT post-) forms. */
1110 if (buffer->props.script == HB_SCRIPT_MALAYALAM)
1111 {
1112 for (unsigned int i = base + 1; i < end; i++)
1113 {
1114 while (i < end && is_joiner (info[i]))
1115 i++;
1116 if (i == end || !is_halant (info[i]))
1117 break;
1118 i++; /* Skip halant. */
1119 while (i < end && is_joiner (info[i]))
1120 i++;
1121 if (i < end && is_consonant (info[i]) && info[i].indic_position() == POS_BELOW_C)
1122 {
1123 base = i;
1124 info[base].indic_position() = POS_BASE_C;
1125 }
1126 }
1127 }
1128
1129 if (start < base && info[base].indic_position() > POS_BASE_C)
1130 base--;
1131 break;
1132 }
1133 if (base == end && start < base &&
1134 is_one_of (info[base - 1], FLAG (OT_ZWJ)))
1135 base--;
1136 if (base < end)
1137 while (start < base &&
1138 is_one_of (info[base], (FLAG (OT_N) | FLAG (OT_H))))
1139 base--;
1140
1141
1142 /* o Reorder matras:
1143 *
1144 * If a pre-base matra character had been reordered before applying basic
1145 * features, the glyph can be moved closer to the main consonant based on
1146 * whether half-forms had been formed. Actual position for the matra is
1147 * defined as “after last standalone halant glyph, after initial matra
1148 * position and before the main consonant”. If ZWJ or ZWNJ follow this
1149 * halant, position is moved after it.
1150 *
1151 * IMPLEMENTATION NOTES:
1152 *
1153 * It looks like the last sentence is wrong. Testing, with Windows 7 Uniscribe
1154 * and Devanagari shows that the behavior is best described as:
1155 *
1156 * "If ZWJ follows this halant, matra is NOT repositioned after this halant.
1157 * If ZWNJ follows this halant, position is moved after it."
1158 *
1159 * Test case, with Adobe Devanagari or Nirmala UI:
1160 *
1161 * U+091F,U+094D,U+200C,U+092F,U+093F
1162 * (Matra moves to the middle, after ZWNJ.)
1163 *
1164 * U+091F,U+094D,U+200D,U+092F,U+093F
1165 * (Matra does NOT move, stays to the left.)
1166 *
1167 * https://github.com/harfbuzz/harfbuzz/issues/1070
1168 */
1169
1170 if (start + 1 < end && start < base) /* Otherwise there can't be any pre-base matra characters. */
1171 {
1172 /* If we lost track of base, alas, position before last thingy. */
1173 unsigned int new_pos = base == end ? base - 2 : base - 1;
1174
1175 /* Malayalam / Tamil do not have "half" forms or explicit virama forms.
1176 * The glyphs formed by 'half' are Chillus or ligated explicit viramas.
1177 * We want to position matra after them.
1178 */
1179 if (buffer->props.script != HB_SCRIPT_MALAYALAM && buffer->props.script != HB_SCRIPT_TAMIL)
1180 {
1181 search:
1182 while (new_pos > start &&
1183 !(is_one_of (info[new_pos], (FLAG (OT_M) | FLAG (OT_H)))))
1184 new_pos--;
1185
1186 /* If we found no Halant we are done.
1187 * Otherwise only proceed if the Halant does
1188 * not belong to the Matra itself! */
1189 if (is_halant (info[new_pos]) &&
1190 info[new_pos].indic_position() != POS_PRE_M)
1191 {
1192 #if 0 // See comment above
1193 /* -> If ZWJ or ZWNJ follow this halant, position is moved after it. */
1194 if (new_pos + 1 < end && is_joiner (info[new_pos + 1]))
1195 new_pos++;
1196 #endif
1197 if (new_pos + 1 < end)
1198 {
1199 /* -> If ZWJ follows this halant, matra is NOT repositioned after this halant. */
1200 if (info[new_pos + 1].indic_category() == OT_ZWJ)
1201 {
1202 /* Keep searching. */
1203 if (new_pos > start)
1204 {
1205 new_pos--;
1206 goto search;
1207 }
1208 }
1209 /* -> If ZWNJ follows this halant, position is moved after it.
1210 *
1211 * IMPLEMENTATION NOTES:
1212 *
1213 * This is taken care of by the state-machine. A Halant,ZWNJ is a terminating
1214 * sequence for a consonant syllable; any pre-base matras occurring after it
1215 * will belong to the subsequent syllable.
1216 */
1217 }
1218 }
1219 else
1220 new_pos = start; /* No move. */
1221 }
1222
1223 if (start < new_pos && info[new_pos].indic_position () != POS_PRE_M)
1224 {
1225 /* Now go see if there's actually any matras... */
1226 for (unsigned int i = new_pos; i > start; i--)
1227 if (info[i - 1].indic_position () == POS_PRE_M)
1228 {
1229 unsigned int old_pos = i - 1;
1230 if (old_pos < base && base <= new_pos) /* Shouldn't actually happen. */
1231 base--;
1232
1233 hb_glyph_info_t tmp = info[old_pos];
1234 memmove (&info[old_pos], &info[old_pos + 1], (new_pos - old_pos) * sizeof (info[0]));
1235 info[new_pos] = tmp;
1236
1237 /* Note: this merge_clusters() is intentionally *after* the reordering.
1238 * Indic matra reordering is special and tricky... */
1239 buffer->merge_clusters (new_pos, hb_min (end, base + 1));
1240
1241 new_pos--;
1242 }
1243 } else {
1244 for (unsigned int i = start; i < base; i++)
1245 if (info[i].indic_position () == POS_PRE_M) {
1246 buffer->merge_clusters (i, hb_min (end, base + 1));
1247 break;
1248 }
1249 }
1250 }
1251
1252
1253 /* o Reorder reph:
1254 *
1255 * Reph’s original position is always at the beginning of the syllable,
1256 * (i.e. it is not reordered at the character reordering stage). However,
1257 * it will be reordered according to the basic-forms shaping results.
1258 * Possible positions for reph, depending on the script, are; after main,
1259 * before post-base consonant forms, and after post-base consonant forms.
1260 */
1261
1262 /* Two cases:
1263 *
1264 * - If repha is encoded as a sequence of characters (Ra,H or Ra,H,ZWJ), then
1265 * we should only move it if the sequence ligated to the repha form.
1266 *
1267 * - If repha is encoded separately and in the logical position, we should only
1268 * move it if it did NOT ligate. If it ligated, it's probably the font trying
1269 * to make it work without the reordering.
1270 */
1271 if (start + 1 < end &&
1272 info[start].indic_position() == POS_RA_TO_BECOME_REPH &&
1273 ((info[start].indic_category() == OT_Repha) ^
1274 _hb_glyph_info_ligated_and_didnt_multiply (&info[start])))
1275 {
1276 unsigned int new_reph_pos;
1277 reph_position_t reph_pos = indic_plan->config->reph_pos;
1278
1279 /* 1. If reph should be positioned after post-base consonant forms,
1280 * proceed to step 5.
1281 */
1282 if (reph_pos == REPH_POS_AFTER_POST)
1283 {
1284 goto reph_step_5;
1285 }
1286
1287 /* 2. If the reph repositioning class is not after post-base: target
1288 * position is after the first explicit halant glyph between the
1289 * first post-reph consonant and last main consonant. If ZWJ or ZWNJ
1290 * are following this halant, position is moved after it. If such
1291 * position is found, this is the target position. Otherwise,
1292 * proceed to the next step.
1293 *
1294 * Note: in old-implementation fonts, where classifications were
1295 * fixed in shaping engine, there was no case where reph position
1296 * will be found on this step.
1297 */
1298 {
1299 new_reph_pos = start + 1;
1300 while (new_reph_pos < base && !is_halant (info[new_reph_pos]))
1301 new_reph_pos++;
1302
1303 if (new_reph_pos < base && is_halant (info[new_reph_pos]))
1304 {
1305 /* ->If ZWJ or ZWNJ are following this halant, position is moved after it. */
1306 if (new_reph_pos + 1 < base && is_joiner (info[new_reph_pos + 1]))
1307 new_reph_pos++;
1308 goto reph_move;
1309 }
1310 }
1311
1312 /* 3. If reph should be repositioned after the main consonant: find the
1313 * first consonant not ligated with main, or find the first
1314 * consonant that is not a potential pre-base-reordering Ra.
1315 */
1316 if (reph_pos == REPH_POS_AFTER_MAIN)
1317 {
1318 new_reph_pos = base;
1319 while (new_reph_pos + 1 < end && info[new_reph_pos + 1].indic_position() <= POS_AFTER_MAIN)
1320 new_reph_pos++;
1321 if (new_reph_pos < end)
1322 goto reph_move;
1323 }
1324
1325 /* 4. If reph should be positioned before post-base consonant, find
1326 * first post-base classified consonant not ligated with main. If no
1327 * consonant is found, the target position should be before the
1328 * first matra, syllable modifier sign or vedic sign.
1329 */
1330 /* This is our take on what step 4 is trying to say (and failing, BADLY). */
1331 if (reph_pos == REPH_POS_AFTER_SUB)
1332 {
1333 new_reph_pos = base;
1334 while (new_reph_pos + 1 < end &&
1335 !( FLAG_UNSAFE (info[new_reph_pos + 1].indic_position()) & (FLAG (POS_POST_C) | FLAG (POS_AFTER_POST) | FLAG (POS_SMVD))))
1336 new_reph_pos++;
1337 if (new_reph_pos < end)
1338 goto reph_move;
1339 }
1340
1341 /* 5. If no consonant is found in steps 3 or 4, move reph to a position
1342 * immediately before the first post-base matra, syllable modifier
1343 * sign or vedic sign that has a reordering class after the intended
1344 * reph position. For example, if the reordering position for reph
1345 * is post-main, it will skip above-base matras that also have a
1346 * post-main position.
1347 */
1348 reph_step_5:
1349 {
1350 /* Copied from step 2. */
1351 new_reph_pos = start + 1;
1352 while (new_reph_pos < base && !is_halant (info[new_reph_pos]))
1353 new_reph_pos++;
1354
1355 if (new_reph_pos < base && is_halant (info[new_reph_pos]))
1356 {
1357 /* ->If ZWJ or ZWNJ are following this halant, position is moved after it. */
1358 if (new_reph_pos + 1 < base && is_joiner (info[new_reph_pos + 1]))
1359 new_reph_pos++;
1360 goto reph_move;
1361 }
1362 }
1363
1364 /* 6. Otherwise, reorder reph to the end of the syllable.
1365 */
1366 {
1367 new_reph_pos = end - 1;
1368 while (new_reph_pos > start && info[new_reph_pos].indic_position() == POS_SMVD)
1369 new_reph_pos--;
1370
1371 /*
1372 * If the Reph is to be ending up after a Matra,Halant sequence,
1373 * position it before that Halant so it can interact with the Matra.
1374 * However, if it's a plain Consonant,Halant we shouldn't do that.
1375 * Uniscribe doesn't do this.
1376 * TEST: U+0930,U+094D,U+0915,U+094B,U+094D
1377 */
1378 if (!indic_plan->uniscribe_bug_compatible &&
1379 unlikely (is_halant (info[new_reph_pos])))
1380 {
1381 for (unsigned int i = base + 1; i < new_reph_pos; i++)
1382 if (info[i].indic_category() == OT_M) {
1383 /* Ok, got it. */
1384 new_reph_pos--;
1385 }
1386 }
1387
1388 goto reph_move;
1389 }
1390
1391 reph_move:
1392 {
1393 /* Move */
1394 buffer->merge_clusters (start, new_reph_pos + 1);
1395 hb_glyph_info_t reph = info[start];
1396 memmove (&info[start], &info[start + 1], (new_reph_pos - start) * sizeof (info[0]));
1397 info[new_reph_pos] = reph;
1398
1399 if (start < base && base <= new_reph_pos)
1400 base--;
1401 }
1402 }
1403
1404
1405 /* o Reorder pre-base-reordering consonants:
1406 *
1407 * If a pre-base-reordering consonant is found, reorder it according to
1408 * the following rules:
1409 */
1410
1411 if (try_pref && base + 1 < end) /* Otherwise there can't be any pre-base-reordering Ra. */
1412 {
1413 for (unsigned int i = base + 1; i < end; i++)
1414 if ((info[i].mask & indic_plan->mask_array[PREF]) != 0)
1415 {
1416 /* 1. Only reorder a glyph produced by substitution during application
1417 * of the <pref> feature. (Note that a font may shape a Ra consonant with
1418 * the feature generally but block it in certain contexts.)
1419 */
1420 /* Note: We just check that something got substituted. We don't check that
1421 * the <pref> feature actually did it...
1422 *
1423 * Reorder pref only if it ligated. */
1424 if (_hb_glyph_info_ligated_and_didnt_multiply (&info[i]))
1425 {
1426 /*
1427 * 2. Try to find a target position the same way as for pre-base matra.
1428 * If it is found, reorder pre-base consonant glyph.
1429 *
1430 * 3. If position is not found, reorder immediately before main
1431 * consonant.
1432 */
1433
1434 unsigned int new_pos = base;
1435 /* Malayalam / Tamil do not have "half" forms or explicit virama forms.
1436 * The glyphs formed by 'half' are Chillus or ligated explicit viramas.
1437 * We want to position matra after them.
1438 */
1439 if (buffer->props.script != HB_SCRIPT_MALAYALAM && buffer->props.script != HB_SCRIPT_TAMIL)
1440 {
1441 while (new_pos > start &&
1442 !(is_one_of (info[new_pos - 1], FLAG(OT_M) | FLAG (OT_H))))
1443 new_pos--;
1444 }
1445
1446 if (new_pos > start && is_halant (info[new_pos - 1]))
1447 {
1448 /* -> If ZWJ or ZWNJ follow this halant, position is moved after it. */
1449 if (new_pos < end && is_joiner (info[new_pos]))
1450 new_pos++;
1451 }
1452
1453 {
1454 unsigned int old_pos = i;
1455
1456 buffer->merge_clusters (new_pos, old_pos + 1);
1457 hb_glyph_info_t tmp = info[old_pos];
1458 memmove (&info[new_pos + 1], &info[new_pos], (old_pos - new_pos) * sizeof (info[0]));
1459 info[new_pos] = tmp;
1460
1461 if (new_pos <= base && base < old_pos)
1462 base++;
1463 }
1464 }
1465
1466 break;
1467 }
1468 }
1469
1470
1471 /* Apply 'init' to the Left Matra if it's a word start. */
1472 if (info[start].indic_position () == POS_PRE_M)
1473 {
1474 if (!start ||
1475 !(FLAG_UNSAFE (_hb_glyph_info_get_general_category (&info[start - 1])) &
1476 FLAG_RANGE (HB_UNICODE_GENERAL_CATEGORY_FORMAT, HB_UNICODE_GENERAL_CATEGORY_NON_SPACING_MARK)))
1477 info[start].mask |= indic_plan->mask_array[INIT];
1478 else
1479 buffer->unsafe_to_break (start - 1, start + 1);
1480 }
1481
1482
1483 /*
1484 * Finish off the clusters and go home!
1485 */
1486 if (indic_plan->uniscribe_bug_compatible)
1487 {
1488 switch ((hb_tag_t) plan->props.script)
1489 {
1490 case HB_SCRIPT_TAMIL:
1491 case HB_SCRIPT_SINHALA:
1492 break;
1493
1494 default:
1495 /* Uniscribe merges the entire syllable into a single cluster... Except for Tamil & Sinhala.
1496 * This means, half forms are submerged into the main consonant's cluster.
1497 * This is unnecessary, and makes cursor positioning harder, but that's what
1498 * Uniscribe does. */
1499 buffer->merge_clusters (start, end);
1500 break;
1501 }
1502 }
1503 }
1504
1505
1506 static void
final_reordering(const hb_ot_shape_plan_t * plan,hb_font_t * font HB_UNUSED,hb_buffer_t * buffer)1507 final_reordering (const hb_ot_shape_plan_t *plan,
1508 hb_font_t *font HB_UNUSED,
1509 hb_buffer_t *buffer)
1510 {
1511 unsigned int count = buffer->len;
1512 if (unlikely (!count)) return;
1513
1514 foreach_syllable (buffer, start, end)
1515 final_reordering_syllable (plan, buffer, start, end);
1516
1517 HB_BUFFER_DEALLOCATE_VAR (buffer, indic_category);
1518 HB_BUFFER_DEALLOCATE_VAR (buffer, indic_position);
1519 }
1520
1521
1522 static void
clear_syllables(const hb_ot_shape_plan_t * plan HB_UNUSED,hb_font_t * font HB_UNUSED,hb_buffer_t * buffer)1523 clear_syllables (const hb_ot_shape_plan_t *plan HB_UNUSED,
1524 hb_font_t *font HB_UNUSED,
1525 hb_buffer_t *buffer)
1526 {
1527 hb_glyph_info_t *info = buffer->info;
1528 unsigned int count = buffer->len;
1529 for (unsigned int i = 0; i < count; i++)
1530 info[i].syllable() = 0;
1531 }
1532
1533
1534 static void
preprocess_text_indic(const hb_ot_shape_plan_t * plan,hb_buffer_t * buffer,hb_font_t * font)1535 preprocess_text_indic (const hb_ot_shape_plan_t *plan,
1536 hb_buffer_t *buffer,
1537 hb_font_t *font)
1538 {
1539 _hb_preprocess_text_vowel_constraints (plan, buffer, font);
1540 }
1541
1542 static bool
decompose_indic(const hb_ot_shape_normalize_context_t * c,hb_codepoint_t ab,hb_codepoint_t * a,hb_codepoint_t * b)1543 decompose_indic (const hb_ot_shape_normalize_context_t *c,
1544 hb_codepoint_t ab,
1545 hb_codepoint_t *a,
1546 hb_codepoint_t *b)
1547 {
1548 switch (ab)
1549 {
1550 /* Don't decompose these. */
1551 case 0x0931u : return false; /* DEVANAGARI LETTER RRA */
1552 // https://github.com/harfbuzz/harfbuzz/issues/779
1553 case 0x09DCu : return false; /* BENGALI LETTER RRA */
1554 case 0x09DDu : return false; /* BENGALI LETTER RHA */
1555 case 0x0B94u : return false; /* TAMIL LETTER AU */
1556
1557
1558 /*
1559 * Decompose split matras that don't have Unicode decompositions.
1560 */
1561
1562 #if 0
1563 /* Gujarati */
1564 /* This one has no decomposition in Unicode, but needs no decomposition either. */
1565 /* case 0x0AC9u : return false; */
1566
1567 /* Oriya */
1568 case 0x0B57u : *a = no decomp, -> RIGHT; return true;
1569 #endif
1570 }
1571
1572 if ((ab == 0x0DDAu || hb_in_range<hb_codepoint_t> (ab, 0x0DDCu, 0x0DDEu)))
1573 {
1574 /*
1575 * Sinhala split matras... Let the fun begin.
1576 *
1577 * These four characters have Unicode decompositions. However, Uniscribe
1578 * decomposes them "Khmer-style", that is, it uses the character itself to
1579 * get the second half. The first half of all four decompositions is always
1580 * U+0DD9.
1581 *
1582 * Now, there are buggy fonts, namely, the widely used lklug.ttf, that are
1583 * broken with Uniscribe. But we need to support them. As such, we only
1584 * do the Uniscribe-style decomposition if the character is transformed into
1585 * its "sec.half" form by the 'pstf' feature. Otherwise, we fall back to
1586 * Unicode decomposition.
1587 *
1588 * Note that we can't unconditionally use Unicode decomposition. That would
1589 * break some other fonts, that are designed to work with Uniscribe, and
1590 * don't have positioning features for the Unicode-style decomposition.
1591 *
1592 * Argh...
1593 *
1594 * The Uniscribe behavior is now documented in the newly published Sinhala
1595 * spec in 2012:
1596 *
1597 * https://docs.microsoft.com/en-us/typography/script-development/sinhala#shaping
1598 */
1599
1600
1601 const indic_shape_plan_t *indic_plan = (const indic_shape_plan_t *) c->plan->data;
1602 hb_codepoint_t glyph;
1603 if (indic_plan->uniscribe_bug_compatible ||
1604 (c->font->get_nominal_glyph (ab, &glyph) &&
1605 indic_plan->pstf.would_substitute (&glyph, 1, c->font->face)))
1606 {
1607 /* Ok, safe to use Uniscribe-style decomposition. */
1608 *a = 0x0DD9u;
1609 *b = ab;
1610 return true;
1611 }
1612 }
1613
1614 return (bool) c->unicode->decompose (ab, a, b);
1615 }
1616
1617 static bool
compose_indic(const hb_ot_shape_normalize_context_t * c,hb_codepoint_t a,hb_codepoint_t b,hb_codepoint_t * ab)1618 compose_indic (const hb_ot_shape_normalize_context_t *c,
1619 hb_codepoint_t a,
1620 hb_codepoint_t b,
1621 hb_codepoint_t *ab)
1622 {
1623 /* Avoid recomposing split matras. */
1624 if (HB_UNICODE_GENERAL_CATEGORY_IS_MARK (c->unicode->general_category (a)))
1625 return false;
1626
1627 /* Composition-exclusion exceptions that we want to recompose. */
1628 if (a == 0x09AFu && b == 0x09BCu) { *ab = 0x09DFu; return true; }
1629
1630 return (bool) c->unicode->compose (a, b, ab);
1631 }
1632
1633
1634 const hb_ot_complex_shaper_t _hb_ot_complex_shaper_indic =
1635 {
1636 collect_features_indic,
1637 override_features_indic,
1638 data_create_indic,
1639 data_destroy_indic,
1640 preprocess_text_indic,
1641 nullptr, /* postprocess_glyphs */
1642 HB_OT_SHAPE_NORMALIZATION_MODE_COMPOSED_DIACRITICS_NO_SHORT_CIRCUIT,
1643 decompose_indic,
1644 compose_indic,
1645 setup_masks_indic,
1646 HB_TAG_NONE, /* gpos_tag */
1647 nullptr, /* reorder_marks */
1648 HB_OT_SHAPE_ZERO_WIDTH_MARKS_NONE,
1649 false, /* fallback_position */
1650 };
1651