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