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