2 * Copyright © 2011 Google, Inc.
4 * This is part of HarfBuzz, a text shaping library.
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.
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
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.
24 * Google Author(s): Behdad Esfahbod
27 #include "hb-ot-shape-complex-private.hh"
32 /* buffer var allocations */
33 #define indic_category() complex_var_persistent_u8_0() /* indic_category_t */
34 #define indic_position() complex_var_persistent_u8_1() /* indic_matra_category_t */
36 #define INDIC_TABLE_ELEMENT_TYPE uint8_t
38 /* Cateories used in the OpenType spec:
39 * https://www.microsoft.com/typography/otfntdev/devanot/shaping.aspx
41 /* Note: This enum is duplicated in the -machine.rl source file.
42 * Not sure how to avoid duplication. */
43 enum indic_category_t {
46 OT_Ra, /* Not explicitly listed in the OT spec, but used in the grammar. */
59 /* Visual positions in a syllable from left to right. */
60 enum indic_position_t {
68 /* Categories used in IndicSyllabicCategory.txt from UCD */
69 /* The assignments are guesswork */
70 enum indic_syllabic_category_t {
71 INDIC_SYLLABIC_CATEGORY_OTHER = OT_X,
73 INDIC_SYLLABIC_CATEGORY_AVAGRAHA = OT_X,
74 INDIC_SYLLABIC_CATEGORY_BINDU = OT_SM,
75 INDIC_SYLLABIC_CATEGORY_CONSONANT = OT_C,
76 INDIC_SYLLABIC_CATEGORY_CONSONANT_DEAD = OT_C,
77 INDIC_SYLLABIC_CATEGORY_CONSONANT_FINAL = OT_C,
78 INDIC_SYLLABIC_CATEGORY_CONSONANT_HEAD_LETTER = OT_C,
79 INDIC_SYLLABIC_CATEGORY_CONSONANT_MEDIAL = OT_C,
80 INDIC_SYLLABIC_CATEGORY_CONSONANT_PLACEHOLDER = OT_NBSP,
81 INDIC_SYLLABIC_CATEGORY_CONSONANT_SUBJOINED = OT_C,
82 INDIC_SYLLABIC_CATEGORY_CONSONANT_REPHA = OT_C,
83 INDIC_SYLLABIC_CATEGORY_MODIFYING_LETTER = OT_X,
84 INDIC_SYLLABIC_CATEGORY_NUKTA = OT_N,
85 INDIC_SYLLABIC_CATEGORY_REGISTER_SHIFTER = OT_X,
86 INDIC_SYLLABIC_CATEGORY_TONE_LETTER = OT_X,
87 INDIC_SYLLABIC_CATEGORY_TONE_MARK = OT_X,
88 INDIC_SYLLABIC_CATEGORY_VIRAMA = OT_H,
89 INDIC_SYLLABIC_CATEGORY_VISARGA = OT_SM,
90 INDIC_SYLLABIC_CATEGORY_VOWEL = OT_V,
91 INDIC_SYLLABIC_CATEGORY_VOWEL_DEPENDENT = OT_M,
92 INDIC_SYLLABIC_CATEGORY_VOWEL_INDEPENDENT = OT_V
95 /* Categories used in IndicSMatraCategory.txt from UCD */
96 enum indic_matra_category_t {
97 INDIC_MATRA_CATEGORY_NOT_APPLICABLE = POS_BASE,
99 INDIC_MATRA_CATEGORY_LEFT = POS_PRE,
100 INDIC_MATRA_CATEGORY_TOP = POS_ABOVE,
101 INDIC_MATRA_CATEGORY_BOTTOM = POS_BELOW,
102 INDIC_MATRA_CATEGORY_RIGHT = POS_POST,
104 /* We don't really care much about these since we decompose them
105 * in the generic pre-shaping layer. They will only be used if
106 * the font does not cover the decomposition. In which case, we
107 * define these as aliases to the place we want the split-matra
108 * glyph to show up. Quite arbitrary. */
109 INDIC_MATRA_CATEGORY_BOTTOM_AND_RIGHT = INDIC_MATRA_CATEGORY_BOTTOM,
110 INDIC_MATRA_CATEGORY_LEFT_AND_RIGHT = INDIC_MATRA_CATEGORY_LEFT,
111 INDIC_MATRA_CATEGORY_TOP_AND_BOTTOM = INDIC_MATRA_CATEGORY_BOTTOM,
112 INDIC_MATRA_CATEGORY_TOP_AND_BOTTOM_AND_RIGHT = INDIC_MATRA_CATEGORY_BOTTOM,
113 INDIC_MATRA_CATEGORY_TOP_AND_LEFT = INDIC_MATRA_CATEGORY_LEFT,
114 INDIC_MATRA_CATEGORY_TOP_AND_LEFT_AND_RIGHT = INDIC_MATRA_CATEGORY_LEFT,
115 INDIC_MATRA_CATEGORY_TOP_AND_RIGHT = INDIC_MATRA_CATEGORY_RIGHT,
117 INDIC_MATRA_CATEGORY_INVISIBLE = INDIC_MATRA_CATEGORY_NOT_APPLICABLE,
118 INDIC_MATRA_CATEGORY_OVERSTRUCK = INDIC_MATRA_CATEGORY_NOT_APPLICABLE,
119 INDIC_MATRA_CATEGORY_VISUAL_ORDER_LEFT = INDIC_MATRA_CATEGORY_NOT_APPLICABLE
122 /* Note: We use ASSERT_STATIC_EXPR_ZERO() instead of ASSERT_STATIC_EXPR() and the comma operation
123 * because gcc fails to optimize the latter and fills the table in at runtime. */
124 #define INDIC_COMBINE_CATEGORIES(S,M) \
125 (ASSERT_STATIC_EXPR_ZERO (M == INDIC_MATRA_CATEGORY_NOT_APPLICABLE || (S == INDIC_SYLLABIC_CATEGORY_VIRAMA || S == INDIC_SYLLABIC_CATEGORY_VOWEL_DEPENDENT)) + \
126 ASSERT_STATIC_EXPR_ZERO (S < 16 && M < 16) + \
129 #include "hb-ot-shape-complex-indic-table.hh"
132 * This is a hack for now. We should:
133 * 1. Move this data into the main Indic table,
135 * 2. Probe font lookups to determine consonant positions.
137 static const struct consonant_position_t {
139 indic_position_t position;
140 } consonant_positions[] = {
238 * This is a hack for now. We should move this data into the main Indic table.
240 static const hb_codepoint_t ra_chars[] = {
241 0x0930, /* Devanagari */
242 0x09B0, /* Bengali */
243 0x09F0, /* Bengali */
244 //0x09F1, /* Bengali */
245 //0x0A30, /* Gurmukhi */
246 0x0AB0, /* Gujarati */
248 //0x0BB0, /* Tamil */
249 //0x0C30, /* Telugu */
250 0x0CB0, /* Kannada */
251 //0x0D30, /* Malayalam */
255 compare_codepoint (const void *pa, const void *pb)
257 hb_codepoint_t a = * (hb_codepoint_t *) pa;
258 hb_codepoint_t b = * (hb_codepoint_t *) pb;
260 return a < b ? -1 : a == b ? 0 : +1;
263 static indic_position_t
264 consonant_position (hb_codepoint_t u)
266 consonant_position_t *record;
268 record = (consonant_position_t *) bsearch (&u, consonant_positions,
269 ARRAY_LENGTH (consonant_positions),
270 sizeof (consonant_positions[0]),
273 return record ? record->position : POS_BASE;
277 is_ra (hb_codepoint_t u)
279 return !!bsearch (&u, ra_chars,
280 ARRAY_LENGTH (ra_chars),
281 sizeof (ra_chars[0]),
286 is_joiner (const hb_glyph_info_t &info)
288 return !!(FLAG (info.indic_category()) & (FLAG (OT_ZWJ) | FLAG (OT_ZWNJ)));
292 is_consonant (const hb_glyph_info_t &info)
294 return !!(FLAG (info.indic_category()) & (FLAG (OT_C) | FLAG (OT_Ra)));
297 static const struct {
300 } indic_basic_features[] =
302 {HB_TAG('n','u','k','t'), true},
303 {HB_TAG('a','k','h','n'), false},
304 {HB_TAG('r','p','h','f'), false},
305 {HB_TAG('r','k','r','f'), false},
306 {HB_TAG('p','r','e','f'), false},
307 {HB_TAG('b','l','w','f'), false},
308 {HB_TAG('h','a','l','f'), false},
309 {HB_TAG('v','a','t','u'), true},
310 {HB_TAG('p','s','t','f'), false},
311 {HB_TAG('c','j','c','t'), false},
314 /* Same order as the indic_basic_features array */
328 static const hb_tag_t indic_other_features[] =
330 HB_TAG('p','r','e','s'),
331 HB_TAG('a','b','v','s'),
332 HB_TAG('b','l','w','s'),
333 HB_TAG('p','s','t','s'),
334 HB_TAG('h','a','l','n'),
336 HB_TAG('d','i','s','t'),
337 HB_TAG('a','b','v','m'),
338 HB_TAG('b','l','w','m'),
343 initial_reordering (const hb_ot_map_t *map,
346 void *user_data HB_UNUSED);
348 final_reordering (const hb_ot_map_t *map,
351 void *user_data HB_UNUSED);
354 _hb_ot_shape_complex_collect_features_indic (hb_ot_map_builder_t *map, const hb_segment_properties_t *props)
356 map->add_bool_feature (HB_TAG('l','o','c','l'));
357 /* The Indic specs do not require ccmp, but we apply it here since if
358 * there is a use of it, it's typically at the beginning. */
359 map->add_bool_feature (HB_TAG('c','c','m','p'));
361 map->add_gsub_pause (initial_reordering, NULL);
363 for (unsigned int i = 0; i < ARRAY_LENGTH (indic_basic_features); i++)
364 map->add_bool_feature (indic_basic_features[i].tag, indic_basic_features[i].is_global);
366 map->add_gsub_pause (final_reordering, NULL);
368 for (unsigned int i = 0; i < ARRAY_LENGTH (indic_other_features); i++)
369 map->add_bool_feature (indic_other_features[i], true);
374 _hb_ot_shape_complex_prefer_decomposed_indic (void)
376 /* We want split matras decomposed by the common shaping logic. */
382 _hb_ot_shape_complex_setup_masks_indic (hb_ot_map_t *map, hb_buffer_t *buffer)
384 HB_BUFFER_ALLOCATE_VAR (buffer, indic_category);
385 HB_BUFFER_ALLOCATE_VAR (buffer, indic_position);
387 /* We cannot setup masks here. We save information about characters
388 * and setup masks later on in a pause-callback. */
390 unsigned int count = buffer->len;
391 for (unsigned int i = 0; i < count; i++)
393 unsigned int type = get_indic_categories (buffer->info[i].codepoint);
395 buffer->info[i].indic_category() = type & 0x0F;
396 buffer->info[i].indic_position() = type >> 4;
398 if (buffer->info[i].indic_category() == OT_C) {
399 buffer->info[i].indic_position() = consonant_position (buffer->info[i].codepoint);
400 if (is_ra (buffer->info[i].codepoint))
401 buffer->info[i].indic_category() = OT_Ra;
402 } else if (buffer->info[i].codepoint == 0x200C)
403 buffer->info[i].indic_category() = OT_ZWNJ;
404 else if (buffer->info[i].codepoint == 0x200D)
405 buffer->info[i].indic_category() = OT_ZWJ;
410 compare_indic_order (const hb_glyph_info_t *pa, const hb_glyph_info_t *pb)
412 int a = pa->indic_position();
413 int b = pb->indic_position();
415 return a < b ? -1 : a == b ? 0 : +1;
419 found_consonant_syllable (const hb_ot_map_t *map, hb_buffer_t *buffer, hb_mask_t *mask_array,
420 unsigned int start, unsigned int end)
423 hb_glyph_info_t *info = buffer->info;
426 * https://www.microsoft.com/typography/otfntdev/devanot/shaping.aspx */
428 /* 1. Find base consonant:
430 * The shaping engine finds the base consonant of the syllable, using the
431 * following algorithm: starting from the end of the syllable, move backwards
432 * until a consonant is found that does not have a below-base or post-base
433 * form (post-base forms have to follow below-base forms), or that is not a
434 * pre-base reordering Ra, or arrive at the first consonant. The consonant
435 * stopped at will be the base.
437 * o If the syllable starts with Ra + Halant (in a script that has Reph)
438 * and has more than one consonant, Ra is excluded from candidates for
442 unsigned int base = 0;
444 /* -> starting from the end of the syllable, move backwards */
446 unsigned int limit = start + (info[start].indic_category() == OT_Ra ? 2 : 0);
449 /* -> until a consonant is found */
450 if (is_consonant (info[i]))
452 /* -> that does not have a below-base or post-base form
453 * (post-base forms have to follow below-base forms), */
454 if (info[i].indic_position() != POS_BELOW &&
455 info[i].indic_position() != POS_POST)
461 /* -> or that is not a pre-base reordering Ra,
466 /* -> o If the syllable starts with Ra + Halant (in a script that has Reph)
467 * and has more than one consonant, Ra is excluded from candidates for
470 * IMPLEMENTATION NOTES:
472 * We do this by adjusting limit accordingly before entering the loop.
475 /* -> or arrive at the first consonant. The consonant stopped at will
481 base = start; /* Just in case... */
484 /* 2. Decompose and reorder Matras:
486 * Each matra and any syllable modifier sign in the cluster are moved to the
487 * appropriate position relative to the consonant(s) in the cluster. The
488 * shaping engine decomposes two- or three-part matras into their constituent
489 * parts before any repositioning. Matra characters are classified by which
490 * consonant in a conjunct they have affinity for and are reordered to the
491 * following positions:
493 * o Before first half form in the syllable
494 * o After subjoined consonants
495 * o After post-form consonant
496 * o After main consonant (for above marks)
498 * IMPLEMENTATION NOTES:
500 * The normalize() routine has already decomposed matras for us, so we don't
501 * need to worry about that.
505 /* 3. Reorder marks to canonical order:
507 * Adjacent nukta and halant or nukta and vedic sign are always repositioned
508 * if necessary, so that the nukta is first.
510 * IMPLEMENTATION NOTES:
512 * We don't need to do this: the normalize() routine already did this for us.
516 /* Reorder characters */
518 for (i = start; i < base; i++)
519 info[i].indic_position() = POS_PRE;
520 info[base].indic_position() = POS_BASE;
523 /* Handle beginning Ra */
524 if (start + 3 <= end &&
525 info[start].indic_category() == OT_Ra &&
526 info[start + 1].indic_category() == OT_H &&
527 !is_joiner (info[start + 2]))
529 info[start].indic_position() = POS_POST;
530 info[start].mask = mask_array[RPHF];
533 /* For old-style Indic script tags, move the first post-base Halant after
535 if ((map->get_chosen_script (0) & 0x000000FF) != '2') {
536 /* We should only do this for Indic scripts which have a version two I guess. */
537 for (i = base + 1; i < end; i++)
538 if (info[i].indic_category() == OT_H) {
540 for (j = end - 1; j > i; j--)
541 if ((FLAG (info[j].indic_category()) & (FLAG (OT_C) | FLAG (OT_Ra))))
544 /* Move Halant to after last consonant. */
545 hb_glyph_info_t t = info[i];
546 memmove (&info[i], &info[i + 1], (j - i) * sizeof (info[0]));
553 /* Attach ZWJ, ZWNJ, nukta, and halant to previous char to move with them. */
554 for (i = start + 1; i < end; i++)
555 if ((FLAG (info[i].indic_category()) &
556 (FLAG (OT_ZWNJ) | FLAG (OT_ZWJ) | FLAG (OT_N) | FLAG (OT_H))))
557 info[i].indic_position() = info[i - 1].indic_position();
559 /* We do bubble-sort, skip malicious clusters attempts */
560 if (end - start > 20)
563 /* Sit tight, rock 'n roll! */
564 hb_bubble_sort (info + start, end - start, compare_indic_order);
566 /* Setup masks now */
569 for (i = start; i < base; i++)
570 info[i].mask |= mask_array[HALF] | mask_array[AKHN] | mask_array[CJCT];
572 info[base].mask |= mask_array[AKHN] | mask_array[CJCT];
574 for (i = base + 1; i < end; i++)
575 info[i].mask |= mask_array[BLWF] | mask_array[PSTF] | mask_array[CJCT];
577 /* Apply ZWJ/ZWNJ effects */
578 for (i = start + 1; i < end; i++)
579 if (is_joiner (info[i])) {
580 bool non_joiner = info[i].indic_category() == OT_ZWNJ;
581 unsigned int j = i - 1;
584 info[j].mask &= !mask_array[HALF];
586 info[j].mask &= !mask_array[CJCT];
588 } while (j > start && !is_consonant (info[j]));
594 found_vowel_syllable (const hb_ot_map_t *map, hb_buffer_t *buffer, hb_mask_t *mask_array,
595 unsigned int start, unsigned int end)
598 * Not clear to me how this should work. Do the matras move to before the
599 * independent vowel? No idea.
604 found_standalone_cluster (const hb_ot_map_t *map, hb_buffer_t *buffer, hb_mask_t *mask_array,
605 unsigned int start, unsigned int end)
608 * Easiest thing to do here is to convert the NBSP to consonant and
609 * call found_consonant_syllable.
614 found_non_indic (const hb_ot_map_t *map, hb_buffer_t *buffer, hb_mask_t *mask_array,
615 unsigned int start, unsigned int end)
617 /* Nothing to do right now. If we ever switch to using the output
618 * buffer in the reordering process, we'd need to next_glyph() here. */
621 #include "hb-ot-shape-complex-indic-machine.hh"
624 remove_joiners (hb_buffer_t *buffer)
626 buffer->clear_output ();
627 unsigned int count = buffer->len;
628 for (buffer->idx = 0; buffer->idx < count;)
629 if (unlikely (is_joiner (buffer->info[buffer->idx])))
630 buffer->skip_glyph ();
632 buffer->next_glyph ();
634 buffer->swap_buffers ();
638 initial_reordering (const hb_ot_map_t *map,
641 void *user_data HB_UNUSED)
643 hb_mask_t mask_array[ARRAY_LENGTH (indic_basic_features)] = {0};
644 unsigned int num_masks = ARRAY_LENGTH (indic_basic_features);
645 for (unsigned int i = 0; i < num_masks; i++)
646 mask_array[i] = map->get_1_mask (indic_basic_features[i].tag);
648 find_syllables (map, buffer, mask_array);
650 remove_joiners (buffer);
654 final_reordering (const hb_ot_map_t *map,
657 void *user_data HB_UNUSED)
659 /* 4. Final reordering:
661 * After the localized forms and basic shaping forms GSUB features have been
662 * applied (see below), the shaping engine performs some final glyph
663 * reordering before applying all the remaining font features to the entire
668 * If a pre-base matra character had been reordered before applying basic
669 * features, the glyph can be moved closer to the main consonant based on
670 * whether half-forms had been formed. Actual position for the matra is
671 * defined as “after last standalone halant glyph, after initial matra
672 * position and before the main consonant”. If ZWJ or ZWNJ follow this
673 * halant, position is moved after it.
677 * Reph’s original position is always at the beginning of the syllable,
678 * (i.e. it is not reordered at the character reordering stage). However,
679 * it will be reordered according to the basic-forms shaping results.
680 * Possible positions for reph, depending on the script, are; after main,
681 * before post-base consonant forms, and after post-base consonant forms.
683 * 1. If reph should be positioned after post-base consonant forms,
686 * 2. If the reph repositioning class is not after post-base: target
687 * position is after the first explicit halant glyph between the
688 * first post-reph consonant and last main consonant. If ZWJ or ZWNJ
689 * are following this halant, position is moved after it. If such
690 * position is found, this is the target position. Otherwise,
691 * proceed to the next step.
693 * Note: in old-implementation fonts, where classifications were
694 * fixed in shaping engine, there was no case where reph position
695 * will be found on this step.
697 * 3. If reph should be repositioned after the main consonant: from the
698 * first consonant not ligated with main, or find the first
699 * consonant that is not a potential pre-base reordering Ra.
702 * 4. If reph should be positioned before post-base consonant, find
703 * first post-base classified consonant not ligated with main. If no
704 * consonant is found, the target position should be before the
705 * first matra, syllable modifier sign or vedic sign.
707 * 5. If no consonant is found in steps 3 or 4, move reph to a position
708 * immediately before the first post-base matra, syllable modifier
709 * sign or vedic sign that has a reordering class after the intended
710 * reph position. For example, if the reordering position for reph
711 * is post-main, it will skip above-base matras that also have a
712 * post-main position.
714 * 6. Otherwise, reorder reph to the end of the syllable.
716 * o Reorder pre-base reordering consonants:
718 * If a pre-base reordering consonant is found, reorder it according to
719 * the following rules:
721 * 1. Only reorder a glyph produced by substitution during application
722 * of the feature. (Note that a font may shape a Ra consonant with
723 * the feature generally but block it in certain contexts.)
725 * 2. Try to find a target position the same way as for pre-base matra.
726 * If it is found, reorder pre-base consonant glyph.
728 * 3. If position is not found, reorder immediately before main
736 HB_BUFFER_DEALLOCATE_VAR (buffer, indic_category);
737 HB_BUFFER_DEALLOCATE_VAR (buffer, indic_position);