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