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