Behdad Esfahbod | fabd311 | 2012-09-05 22:19:28 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright © 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 | |
Behdad Esfahbod | 44be1e5 | 2022-06-03 02:54:33 -0600 | [diff] [blame] | 27 | #ifndef HB_OT_SHAPER_ARABIC_FALLBACK_HH |
| 28 | #define HB_OT_SHAPER_ARABIC_FALLBACK_HH |
Behdad Esfahbod | fabd311 | 2012-09-05 22:19:28 -0400 | [diff] [blame] | 29 | |
Behdad Esfahbod | c77ae40 | 2018-08-25 22:36:36 -0700 | [diff] [blame] | 30 | #include "hb.hh" |
Behdad Esfahbod | fabd311 | 2012-09-05 22:19:28 -0400 | [diff] [blame] | 31 | |
Behdad Esfahbod | c77ae40 | 2018-08-25 22:36:36 -0700 | [diff] [blame] | 32 | #include "hb-ot-shape.hh" |
Behdad Esfahbod | fabd311 | 2012-09-05 22:19:28 -0400 | [diff] [blame] | 33 | #include "hb-ot-layout-gsub-table.hh" |
| 34 | |
| 35 | |
Behdad Esfahbod | 55977f2 | 2014-08-01 16:14:33 -0400 | [diff] [blame] | 36 | /* Features ordered the same as the entries in shaping_table rows, |
Behdad Esfahbod | 16c2371 | 2022-06-18 13:25:46 -0600 | [diff] [blame] | 37 | * followed by rlig. Don't change. |
| 38 | * |
| 39 | * We currently support one subtable per lookup, and one lookup |
| 40 | * per feature. But we allow duplicate features, so we use that! |
| 41 | */ |
Behdad Esfahbod | fabd311 | 2012-09-05 22:19:28 -0400 | [diff] [blame] | 42 | static const hb_tag_t arabic_fallback_features[] = |
| 43 | { |
| 44 | HB_TAG('i','n','i','t'), |
| 45 | HB_TAG('m','e','d','i'), |
| 46 | HB_TAG('f','i','n','a'), |
| 47 | HB_TAG('i','s','o','l'), |
Behdad Esfahbod | 41a079b | 2022-06-18 14:47:10 -0600 | [diff] [blame] | 48 | HB_TAG('r','l','i','g'), |
| 49 | HB_TAG('r','l','i','g'), |
| 50 | HB_TAG('r','l','i','g'), |
Behdad Esfahbod | fabd311 | 2012-09-05 22:19:28 -0400 | [diff] [blame] | 51 | }; |
| 52 | |
Behdad Esfahbod | fabd311 | 2012-09-05 22:19:28 -0400 | [diff] [blame] | 53 | static OT::SubstLookup * |
Behdad Esfahbod | 0beb66e | 2012-12-05 18:46:04 -0500 | [diff] [blame] | 54 | arabic_fallback_synthesize_lookup_single (const hb_ot_shape_plan_t *plan HB_UNUSED, |
Behdad Esfahbod | fabd311 | 2012-09-05 22:19:28 -0400 | [diff] [blame] | 55 | hb_font_t *font, |
| 56 | unsigned int feature_index) |
| 57 | { |
Behdad Esfahbod | c852b86 | 2021-09-19 16:30:12 -0400 | [diff] [blame] | 58 | OT::HBGlyphID16 glyphs[SHAPING_TABLE_LAST - SHAPING_TABLE_FIRST + 1]; |
| 59 | OT::HBGlyphID16 substitutes[SHAPING_TABLE_LAST - SHAPING_TABLE_FIRST + 1]; |
Behdad Esfahbod | fabd311 | 2012-09-05 22:19:28 -0400 | [diff] [blame] | 60 | unsigned int num_glyphs = 0; |
| 61 | |
| 62 | /* Populate arrays */ |
| 63 | for (hb_codepoint_t u = SHAPING_TABLE_FIRST; u < SHAPING_TABLE_LAST + 1; u++) |
| 64 | { |
| 65 | hb_codepoint_t s = shaping_table[u - SHAPING_TABLE_FIRST][feature_index]; |
| 66 | hb_codepoint_t u_glyph, s_glyph; |
| 67 | |
Behdad Esfahbod | 07cfbe2 | 2012-09-06 01:16:39 -0400 | [diff] [blame] | 68 | if (!s || |
Behdad Esfahbod | fabd311 | 2012-09-05 22:19:28 -0400 | [diff] [blame] | 69 | !hb_font_get_glyph (font, u, 0, &u_glyph) || |
| 70 | !hb_font_get_glyph (font, s, 0, &s_glyph) || |
| 71 | u_glyph == s_glyph || |
Behdad Esfahbod | 7627100 | 2014-07-11 14:54:42 -0400 | [diff] [blame] | 72 | u_glyph > 0xFFFFu || s_glyph > 0xFFFFu) |
Behdad Esfahbod | fabd311 | 2012-09-05 22:19:28 -0400 | [diff] [blame] | 73 | continue; |
| 74 | |
Behdad Esfahbod | b986c6a | 2019-03-29 20:17:46 -0700 | [diff] [blame] | 75 | glyphs[num_glyphs] = u_glyph; |
| 76 | substitutes[num_glyphs] = s_glyph; |
Behdad Esfahbod | fabd311 | 2012-09-05 22:19:28 -0400 | [diff] [blame] | 77 | |
| 78 | num_glyphs++; |
| 79 | } |
| 80 | |
Behdad Esfahbod | f28b1c8 | 2014-07-30 02:15:44 -0400 | [diff] [blame] | 81 | if (!num_glyphs) |
Behdad Esfahbod | dbdbfe3 | 2017-10-15 12:11:08 +0200 | [diff] [blame] | 82 | return nullptr; |
Behdad Esfahbod | f28b1c8 | 2014-07-30 02:15:44 -0400 | [diff] [blame] | 83 | |
Behdad Esfahbod | 85846b3 | 2015-09-01 15:07:52 +0100 | [diff] [blame] | 84 | /* Bubble-sort or something equally good! |
Behdad Esfahbod | fabd311 | 2012-09-05 22:19:28 -0400 | [diff] [blame] | 85 | * May not be good-enough for presidential candidate interviews, but good-enough for us... */ |
Behdad Esfahbod | a685bfe | 2018-12-30 20:24:21 -0500 | [diff] [blame] | 86 | hb_stable_sort (&glyphs[0], num_glyphs, |
Behdad Esfahbod | c852b86 | 2021-09-19 16:30:12 -0400 | [diff] [blame] | 87 | (int(*)(const OT::HBUINT16*, const OT::HBUINT16 *)) OT::HBGlyphID16::cmp, |
Behdad Esfahbod | a685bfe | 2018-12-30 20:24:21 -0500 | [diff] [blame] | 88 | &substitutes[0]); |
Behdad Esfahbod | fabd311 | 2012-09-05 22:19:28 -0400 | [diff] [blame] | 89 | |
Behdad Esfahbod | fabd311 | 2012-09-05 22:19:28 -0400 | [diff] [blame] | 90 | |
| 91 | /* Each glyph takes four bytes max, and there's some overhead. */ |
| 92 | char buf[(SHAPING_TABLE_LAST - SHAPING_TABLE_FIRST + 1) * 4 + 128]; |
Behdad Esfahbod | ed7b2e5 | 2018-08-01 23:59:09 -0700 | [diff] [blame] | 93 | hb_serialize_context_t c (buf, sizeof (buf)); |
Behdad Esfahbod | fabd311 | 2012-09-05 22:19:28 -0400 | [diff] [blame] | 94 | OT::SubstLookup *lookup = c.start_serialize<OT::SubstLookup> (); |
| 95 | bool ret = lookup->serialize_single (&c, |
| 96 | OT::LookupFlag::IgnoreMarks, |
Behdad Esfahbod | 815cde9 | 2019-01-07 18:33:04 -0500 | [diff] [blame] | 97 | hb_sorted_array (glyphs, num_glyphs), |
Behdad Esfahbod | f1e95e4 | 2018-12-18 16:49:08 -0500 | [diff] [blame] | 98 | hb_array (substitutes, num_glyphs)); |
Behdad Esfahbod | fabd311 | 2012-09-05 22:19:28 -0400 | [diff] [blame] | 99 | c.end_serialize (); |
Behdad Esfahbod | fabd311 | 2012-09-05 22:19:28 -0400 | [diff] [blame] | 100 | |
ebraminio | 1e48225 | 2020-08-13 23:22:14 +0430 | [diff] [blame] | 101 | return ret && !c.in_error () ? c.copy<OT::SubstLookup> () : nullptr; |
Behdad Esfahbod | fabd311 | 2012-09-05 22:19:28 -0400 | [diff] [blame] | 102 | } |
| 103 | |
Behdad Esfahbod | 16c2371 | 2022-06-18 13:25:46 -0600 | [diff] [blame] | 104 | template <typename T> |
Behdad Esfahbod | fabd311 | 2012-09-05 22:19:28 -0400 | [diff] [blame] | 105 | static OT::SubstLookup * |
Behdad Esfahbod | 0beb66e | 2012-12-05 18:46:04 -0500 | [diff] [blame] | 106 | arabic_fallback_synthesize_lookup_ligature (const hb_ot_shape_plan_t *plan HB_UNUSED, |
Behdad Esfahbod | 16c2371 | 2022-06-18 13:25:46 -0600 | [diff] [blame] | 107 | hb_font_t *font, |
| 108 | const T &ligature_table, |
| 109 | unsigned lookup_flags) |
Behdad Esfahbod | fabd311 | 2012-09-05 22:19:28 -0400 | [diff] [blame] | 110 | { |
Behdad Esfahbod | c852b86 | 2021-09-19 16:30:12 -0400 | [diff] [blame] | 111 | OT::HBGlyphID16 first_glyphs[ARRAY_LENGTH_CONST (ligature_table)]; |
Behdad Esfahbod | fabd311 | 2012-09-05 22:19:28 -0400 | [diff] [blame] | 112 | unsigned int first_glyphs_indirection[ARRAY_LENGTH_CONST (ligature_table)]; |
| 113 | unsigned int ligature_per_first_glyph_count_list[ARRAY_LENGTH_CONST (first_glyphs)]; |
| 114 | unsigned int num_first_glyphs = 0; |
| 115 | |
Behdad Esfahbod | 15dd34b | 2022-06-18 13:47:00 -0600 | [diff] [blame] | 116 | /* We know that all our ligatures have the same number of components. */ |
Behdad Esfahbod | c852b86 | 2021-09-19 16:30:12 -0400 | [diff] [blame] | 117 | OT::HBGlyphID16 ligature_list[ARRAY_LENGTH_CONST (first_glyphs) * ARRAY_LENGTH_CONST(ligature_table[0].ligatures)]; |
Behdad Esfahbod | 82f6b6f | 2012-09-06 01:12:50 -0400 | [diff] [blame] | 118 | unsigned int component_count_list[ARRAY_LENGTH_CONST (ligature_list)]; |
Behdad Esfahbod | 15dd34b | 2022-06-18 13:47:00 -0600 | [diff] [blame] | 119 | OT::HBGlyphID16 component_list[ARRAY_LENGTH_CONST (ligature_list) * |
| 120 | ARRAY_LENGTH_CONST (ligature_table[0].ligatures[0].components)]; |
Behdad Esfahbod | fabd311 | 2012-09-05 22:19:28 -0400 | [diff] [blame] | 121 | unsigned int num_ligatures = 0; |
Behdad Esfahbod | 9684d2d | 2022-06-18 14:07:48 -0600 | [diff] [blame] | 122 | unsigned int num_components = 0; |
Behdad Esfahbod | fabd311 | 2012-09-05 22:19:28 -0400 | [diff] [blame] | 123 | |
| 124 | /* Populate arrays */ |
| 125 | |
| 126 | /* Sort out the first-glyphs */ |
| 127 | for (unsigned int first_glyph_idx = 0; first_glyph_idx < ARRAY_LENGTH (first_glyphs); first_glyph_idx++) |
| 128 | { |
| 129 | hb_codepoint_t first_u = ligature_table[first_glyph_idx].first; |
| 130 | hb_codepoint_t first_glyph; |
| 131 | if (!hb_font_get_glyph (font, first_u, 0, &first_glyph)) |
| 132 | continue; |
Behdad Esfahbod | b986c6a | 2019-03-29 20:17:46 -0700 | [diff] [blame] | 133 | first_glyphs[num_first_glyphs] = first_glyph; |
Behdad Esfahbod | fabd311 | 2012-09-05 22:19:28 -0400 | [diff] [blame] | 134 | ligature_per_first_glyph_count_list[num_first_glyphs] = 0; |
| 135 | first_glyphs_indirection[num_first_glyphs] = first_glyph_idx; |
| 136 | num_first_glyphs++; |
| 137 | } |
Behdad Esfahbod | a685bfe | 2018-12-30 20:24:21 -0500 | [diff] [blame] | 138 | hb_stable_sort (&first_glyphs[0], num_first_glyphs, |
Behdad Esfahbod | c852b86 | 2021-09-19 16:30:12 -0400 | [diff] [blame] | 139 | (int(*)(const OT::HBUINT16*, const OT::HBUINT16 *)) OT::HBGlyphID16::cmp, |
Behdad Esfahbod | a685bfe | 2018-12-30 20:24:21 -0500 | [diff] [blame] | 140 | &first_glyphs_indirection[0]); |
Behdad Esfahbod | fabd311 | 2012-09-05 22:19:28 -0400 | [diff] [blame] | 141 | |
| 142 | /* Now that the first-glyphs are sorted, walk again, populate ligatures. */ |
| 143 | for (unsigned int i = 0; i < num_first_glyphs; i++) |
| 144 | { |
| 145 | unsigned int first_glyph_idx = first_glyphs_indirection[i]; |
| 146 | |
Behdad Esfahbod | 15dd34b | 2022-06-18 13:47:00 -0600 | [diff] [blame] | 147 | for (unsigned int ligature_idx = 0; ligature_idx < ARRAY_LENGTH (ligature_table[0].ligatures); ligature_idx++) |
Behdad Esfahbod | fabd311 | 2012-09-05 22:19:28 -0400 | [diff] [blame] | 148 | { |
Behdad Esfahbod | 15dd34b | 2022-06-18 13:47:00 -0600 | [diff] [blame] | 149 | hb_codepoint_t ligature_u = ligature_table[first_glyph_idx].ligatures[ligature_idx].ligature; |
Behdad Esfahbod | 08715d7 | 2022-06-18 14:04:56 -0600 | [diff] [blame] | 150 | hb_codepoint_t ligature_glyph; |
| 151 | if (!hb_font_get_glyph (font, ligature_u, 0, &ligature_glyph)) |
Behdad Esfahbod | fabd311 | 2012-09-05 22:19:28 -0400 | [diff] [blame] | 152 | continue; |
| 153 | |
Behdad Esfahbod | 9684d2d | 2022-06-18 14:07:48 -0600 | [diff] [blame] | 154 | const auto &components = ligature_table[first_glyph_idx].ligatures[ligature_idx].components; |
| 155 | unsigned component_count = ARRAY_LENGTH_CONST (components); |
| 156 | |
Behdad Esfahbod | 4300a18 | 2023-01-18 10:44:13 -0700 | [diff] [blame] | 157 | bool matched = true; |
| 158 | for (unsigned j = 0; j < component_count; j++) |
Behdad Esfahbod | 20e9f0b | 2022-06-18 14:12:30 -0600 | [diff] [blame] | 159 | { |
Behdad Esfahbod | 4300a18 | 2023-01-18 10:44:13 -0700 | [diff] [blame] | 160 | hb_codepoint_t component_u = ligature_table[first_glyph_idx].ligatures[ligature_idx].components[j]; |
Behdad Esfahbod | 20e9f0b | 2022-06-18 14:12:30 -0600 | [diff] [blame] | 161 | hb_codepoint_t component_glyph; |
| 162 | if (!component_u || |
Behdad Esfahbod | 4300a18 | 2023-01-18 10:44:13 -0700 | [diff] [blame] | 163 | !hb_font_get_nominal_glyph (font, component_u, &component_glyph)) |
| 164 | { |
| 165 | matched = false; |
| 166 | break; |
| 167 | } |
Behdad Esfahbod | 08715d7 | 2022-06-18 14:04:56 -0600 | [diff] [blame] | 168 | |
Behdad Esfahbod | 20e9f0b | 2022-06-18 14:12:30 -0600 | [diff] [blame] | 169 | component_list[num_components++] = component_glyph; |
| 170 | } |
Behdad Esfahbod | 4300a18 | 2023-01-18 10:44:13 -0700 | [diff] [blame] | 171 | if (!matched) |
| 172 | continue; |
Behdad Esfahbod | 08715d7 | 2022-06-18 14:04:56 -0600 | [diff] [blame] | 173 | |
Behdad Esfahbod | 9684d2d | 2022-06-18 14:07:48 -0600 | [diff] [blame] | 174 | component_count_list[num_ligatures] = 1 + component_count; |
Behdad Esfahbod | 08715d7 | 2022-06-18 14:04:56 -0600 | [diff] [blame] | 175 | ligature_list[num_ligatures] = ligature_glyph; |
| 176 | |
Behdad Esfahbod | fabd311 | 2012-09-05 22:19:28 -0400 | [diff] [blame] | 177 | ligature_per_first_glyph_count_list[i]++; |
| 178 | |
Behdad Esfahbod | fabd311 | 2012-09-05 22:19:28 -0400 | [diff] [blame] | 179 | num_ligatures++; |
| 180 | } |
| 181 | } |
| 182 | |
Behdad Esfahbod | f28b1c8 | 2014-07-30 02:15:44 -0400 | [diff] [blame] | 183 | if (!num_ligatures) |
Behdad Esfahbod | dbdbfe3 | 2017-10-15 12:11:08 +0200 | [diff] [blame] | 184 | return nullptr; |
Behdad Esfahbod | f28b1c8 | 2014-07-30 02:15:44 -0400 | [diff] [blame] | 185 | |
Behdad Esfahbod | fabd311 | 2012-09-05 22:19:28 -0400 | [diff] [blame] | 186 | |
Behdad Esfahbod | 82f6b6f | 2012-09-06 01:12:50 -0400 | [diff] [blame] | 187 | /* 16 bytes per ligature ought to be enough... */ |
| 188 | char buf[ARRAY_LENGTH_CONST (ligature_list) * 16 + 128]; |
Behdad Esfahbod | ed7b2e5 | 2018-08-01 23:59:09 -0700 | [diff] [blame] | 189 | hb_serialize_context_t c (buf, sizeof (buf)); |
Behdad Esfahbod | fabd311 | 2012-09-05 22:19:28 -0400 | [diff] [blame] | 190 | OT::SubstLookup *lookup = c.start_serialize<OT::SubstLookup> (); |
| 191 | bool ret = lookup->serialize_ligature (&c, |
Behdad Esfahbod | 16c2371 | 2022-06-18 13:25:46 -0600 | [diff] [blame] | 192 | lookup_flags, |
Behdad Esfahbod | 815cde9 | 2019-01-07 18:33:04 -0500 | [diff] [blame] | 193 | hb_sorted_array (first_glyphs, num_first_glyphs), |
Behdad Esfahbod | f1e95e4 | 2018-12-18 16:49:08 -0500 | [diff] [blame] | 194 | hb_array (ligature_per_first_glyph_count_list, num_first_glyphs), |
| 195 | hb_array (ligature_list, num_ligatures), |
| 196 | hb_array (component_count_list, num_ligatures), |
Behdad Esfahbod | 1db6fdd | 2022-06-18 14:34:46 -0600 | [diff] [blame] | 197 | hb_array (component_list, num_components)); |
Behdad Esfahbod | fabd311 | 2012-09-05 22:19:28 -0400 | [diff] [blame] | 198 | c.end_serialize (); |
Behdad Esfahbod | fabd311 | 2012-09-05 22:19:28 -0400 | [diff] [blame] | 199 | |
ebraminio | 1e48225 | 2020-08-13 23:22:14 +0430 | [diff] [blame] | 200 | return ret && !c.in_error () ? c.copy<OT::SubstLookup> () : nullptr; |
Behdad Esfahbod | fabd311 | 2012-09-05 22:19:28 -0400 | [diff] [blame] | 201 | } |
| 202 | |
| 203 | static OT::SubstLookup * |
| 204 | arabic_fallback_synthesize_lookup (const hb_ot_shape_plan_t *plan, |
| 205 | hb_font_t *font, |
| 206 | unsigned int feature_index) |
| 207 | { |
| 208 | if (feature_index < 4) |
| 209 | return arabic_fallback_synthesize_lookup_single (plan, font, feature_index); |
| 210 | else |
Behdad Esfahbod | 16c2371 | 2022-06-18 13:25:46 -0600 | [diff] [blame] | 211 | { |
| 212 | switch (feature_index) { |
Behdad Esfahbod | 1db6fdd | 2022-06-18 14:34:46 -0600 | [diff] [blame] | 213 | case 4: return arabic_fallback_synthesize_lookup_ligature (plan, font, ligature_3_table, OT::LookupFlag::IgnoreMarks); |
| 214 | case 5: return arabic_fallback_synthesize_lookup_ligature (plan, font, ligature_table, OT::LookupFlag::IgnoreMarks); |
| 215 | case 6: return arabic_fallback_synthesize_lookup_ligature (plan, font, ligature_mark_table, 0); |
Behdad Esfahbod | 16c2371 | 2022-06-18 13:25:46 -0600 | [diff] [blame] | 216 | } |
| 217 | } |
| 218 | assert (false); |
Behdad Esfahbod | 4520911 | 2022-06-19 10:36:24 -0600 | [diff] [blame] | 219 | return nullptr; |
Behdad Esfahbod | fabd311 | 2012-09-05 22:19:28 -0400 | [diff] [blame] | 220 | } |
| 221 | |
Behdad Esfahbod | 16c2371 | 2022-06-18 13:25:46 -0600 | [diff] [blame] | 222 | #define ARABIC_FALLBACK_MAX_LOOKUPS ARRAY_LENGTH_CONST (arabic_fallback_features) |
Behdad Esfahbod | 55977f2 | 2014-08-01 16:14:33 -0400 | [diff] [blame] | 223 | |
Behdad Esfahbod | fabd311 | 2012-09-05 22:19:28 -0400 | [diff] [blame] | 224 | struct arabic_fallback_plan_t |
| 225 | { |
Behdad Esfahbod | f28b1c8 | 2014-07-30 02:15:44 -0400 | [diff] [blame] | 226 | unsigned int num_lookups; |
| 227 | bool free_lookups; |
| 228 | |
Behdad Esfahbod | 55977f2 | 2014-08-01 16:14:33 -0400 | [diff] [blame] | 229 | hb_mask_t mask_array[ARABIC_FALLBACK_MAX_LOOKUPS]; |
| 230 | OT::SubstLookup *lookup_array[ARABIC_FALLBACK_MAX_LOOKUPS]; |
Behdad Esfahbod | 7a4bd97 | 2023-01-31 14:59:39 -0700 | [diff] [blame] | 231 | OT::hb_ot_layout_lookup_accelerator_t *accel_array[ARABIC_FALLBACK_MAX_LOOKUPS]; |
Behdad Esfahbod | fabd311 | 2012-09-05 22:19:28 -0400 | [diff] [blame] | 232 | }; |
| 233 | |
Ken Brown | eee5b5e | 2018-11-12 21:05:39 -0500 | [diff] [blame] | 234 | #if defined(_WIN32) && !defined(HB_NO_WIN1256) |
Behdad Esfahbod | f28b1c8 | 2014-07-30 02:15:44 -0400 | [diff] [blame] | 235 | #define HB_WITH_WIN1256 |
| 236 | #endif |
| 237 | |
| 238 | #ifdef HB_WITH_WIN1256 |
Behdad Esfahbod | 5bfb0b7 | 2022-06-03 02:56:41 -0600 | [diff] [blame] | 239 | #include "hb-ot-shaper-arabic-win1256.hh" |
Behdad Esfahbod | f28b1c8 | 2014-07-30 02:15:44 -0400 | [diff] [blame] | 240 | #endif |
| 241 | |
Behdad Esfahbod | 3506672 | 2018-08-06 06:17:48 -0700 | [diff] [blame] | 242 | struct ManifestLookup |
| 243 | { |
Behdad Esfahbod | 4674655 | 2018-11-10 20:11:10 -0500 | [diff] [blame] | 244 | public: |
Behdad Esfahbod | 1789ccb | 2014-07-31 11:04:00 -0400 | [diff] [blame] | 245 | OT::Tag tag; |
Behdad Esfahbod | ad28f97 | 2021-03-31 12:49:14 -0600 | [diff] [blame] | 246 | OT::Offset16To<OT::SubstLookup> lookupOffset; |
Behdad Esfahbod | 4674655 | 2018-11-10 20:11:10 -0500 | [diff] [blame] | 247 | public: |
| 248 | DEFINE_SIZE_STATIC (6); |
Behdad Esfahbod | f28b1c8 | 2014-07-30 02:15:44 -0400 | [diff] [blame] | 249 | }; |
Behdad Esfahbod | 5639e25 | 2021-03-31 16:04:43 -0600 | [diff] [blame] | 250 | typedef OT::Array16Of<ManifestLookup> Manifest; |
Behdad Esfahbod | f28b1c8 | 2014-07-30 02:15:44 -0400 | [diff] [blame] | 251 | |
| 252 | static bool |
Behdad Esfahbod | 39bd07a | 2018-10-26 21:01:11 -0700 | [diff] [blame] | 253 | arabic_fallback_plan_init_win1256 (arabic_fallback_plan_t *fallback_plan HB_UNUSED, |
| 254 | const hb_ot_shape_plan_t *plan HB_UNUSED, |
| 255 | hb_font_t *font HB_UNUSED) |
Behdad Esfahbod | f28b1c8 | 2014-07-30 02:15:44 -0400 | [diff] [blame] | 256 | { |
| 257 | #ifdef HB_WITH_WIN1256 |
| 258 | /* Does this font look like it's Windows-1256-encoded? */ |
| 259 | hb_codepoint_t g; |
| 260 | if (!(hb_font_get_glyph (font, 0x0627u, 0, &g) && g == 199 /* ALEF */ && |
| 261 | hb_font_get_glyph (font, 0x0644u, 0, &g) && g == 225 /* LAM */ && |
| 262 | hb_font_get_glyph (font, 0x0649u, 0, &g) && g == 236 /* ALEF MAKSURA */ && |
| 263 | hb_font_get_glyph (font, 0x064Au, 0, &g) && g == 237 /* YEH */ && |
| 264 | hb_font_get_glyph (font, 0x0652u, 0, &g) && g == 250 /* SUKUN */)) |
| 265 | return false; |
| 266 | |
| 267 | const Manifest &manifest = reinterpret_cast<const Manifest&> (arabic_win1256_gsub_lookups.manifest); |
Behdad Esfahbod | 41a079b | 2022-06-18 14:47:10 -0600 | [diff] [blame] | 268 | static_assert (sizeof (arabic_win1256_gsub_lookups.manifestData) <= |
Ebrahim Byagowi | 51b720f | 2019-10-01 12:06:22 +0330 | [diff] [blame] | 269 | ARABIC_FALLBACK_MAX_LOOKUPS * sizeof (ManifestLookup), ""); |
Behdad Esfahbod | f28b1c8 | 2014-07-30 02:15:44 -0400 | [diff] [blame] | 270 | |
| 271 | unsigned j = 0; |
| 272 | unsigned int count = manifest.len; |
| 273 | for (unsigned int i = 0; i < count; i++) |
| 274 | { |
Behdad Esfahbod | 1789ccb | 2014-07-31 11:04:00 -0400 | [diff] [blame] | 275 | fallback_plan->mask_array[j] = plan->map.get_1_mask (manifest[i].tag); |
Behdad Esfahbod | f28b1c8 | 2014-07-30 02:15:44 -0400 | [diff] [blame] | 276 | if (fallback_plan->mask_array[j]) |
| 277 | { |
| 278 | fallback_plan->lookup_array[j] = const_cast<OT::SubstLookup*> (&(&manifest+manifest[i].lookupOffset)); |
| 279 | if (fallback_plan->lookup_array[j]) |
| 280 | { |
Behdad Esfahbod | 7a4bd97 | 2023-01-31 14:59:39 -0700 | [diff] [blame] | 281 | fallback_plan->accel_array[j] = OT::hb_ot_layout_lookup_accelerator_t::create (*fallback_plan->lookup_array[j]); |
Behdad Esfahbod | f28b1c8 | 2014-07-30 02:15:44 -0400 | [diff] [blame] | 282 | j++; |
| 283 | } |
| 284 | } |
| 285 | } |
| 286 | |
| 287 | fallback_plan->num_lookups = j; |
| 288 | fallback_plan->free_lookups = false; |
| 289 | |
| 290 | return j > 0; |
| 291 | #else |
| 292 | return false; |
| 293 | #endif |
| 294 | } |
| 295 | |
| 296 | static bool |
| 297 | arabic_fallback_plan_init_unicode (arabic_fallback_plan_t *fallback_plan, |
| 298 | const hb_ot_shape_plan_t *plan, |
| 299 | hb_font_t *font) |
| 300 | { |
Behdad Esfahbod | 16c2371 | 2022-06-18 13:25:46 -0600 | [diff] [blame] | 301 | static_assert ((ARRAY_LENGTH_CONST (arabic_fallback_features) <= ARABIC_FALLBACK_MAX_LOOKUPS), ""); |
Behdad Esfahbod | f28b1c8 | 2014-07-30 02:15:44 -0400 | [diff] [blame] | 302 | unsigned int j = 0; |
Behdad Esfahbod | 55977f2 | 2014-08-01 16:14:33 -0400 | [diff] [blame] | 303 | for (unsigned int i = 0; i < ARRAY_LENGTH(arabic_fallback_features) ; i++) |
Behdad Esfahbod | f28b1c8 | 2014-07-30 02:15:44 -0400 | [diff] [blame] | 304 | { |
| 305 | fallback_plan->mask_array[j] = plan->map.get_1_mask (arabic_fallback_features[i]); |
| 306 | if (fallback_plan->mask_array[j]) |
| 307 | { |
| 308 | fallback_plan->lookup_array[j] = arabic_fallback_synthesize_lookup (plan, font, i); |
| 309 | if (fallback_plan->lookup_array[j]) |
| 310 | { |
Behdad Esfahbod | 7a4bd97 | 2023-01-31 14:59:39 -0700 | [diff] [blame] | 311 | fallback_plan->accel_array[j] = OT::hb_ot_layout_lookup_accelerator_t::create (*fallback_plan->lookup_array[j]); |
Behdad Esfahbod | f28b1c8 | 2014-07-30 02:15:44 -0400 | [diff] [blame] | 312 | j++; |
| 313 | } |
| 314 | } |
| 315 | } |
| 316 | |
| 317 | fallback_plan->num_lookups = j; |
| 318 | fallback_plan->free_lookups = true; |
| 319 | |
| 320 | return j > 0; |
| 321 | } |
| 322 | |
Behdad Esfahbod | fabd311 | 2012-09-05 22:19:28 -0400 | [diff] [blame] | 323 | static arabic_fallback_plan_t * |
| 324 | arabic_fallback_plan_create (const hb_ot_shape_plan_t *plan, |
| 325 | hb_font_t *font) |
| 326 | { |
Behdad Esfahbod | 2337f0d | 2021-07-08 10:58:50 -0600 | [diff] [blame] | 327 | arabic_fallback_plan_t *fallback_plan = (arabic_fallback_plan_t *) hb_calloc (1, sizeof (arabic_fallback_plan_t)); |
Behdad Esfahbod | fabd311 | 2012-09-05 22:19:28 -0400 | [diff] [blame] | 328 | if (unlikely (!fallback_plan)) |
Ebrahim Byagowi | 2dda6dd | 2020-04-20 14:12:45 +0430 | [diff] [blame] | 329 | return const_cast<arabic_fallback_plan_t *> (&Null (arabic_fallback_plan_t)); |
Behdad Esfahbod | fabd311 | 2012-09-05 22:19:28 -0400 | [diff] [blame] | 330 | |
Behdad Esfahbod | f28b1c8 | 2014-07-30 02:15:44 -0400 | [diff] [blame] | 331 | fallback_plan->num_lookups = 0; |
| 332 | fallback_plan->free_lookups = false; |
Behdad Esfahbod | fabd311 | 2012-09-05 22:19:28 -0400 | [diff] [blame] | 333 | |
Behdad Esfahbod | f28b1c8 | 2014-07-30 02:15:44 -0400 | [diff] [blame] | 334 | /* Try synthesizing GSUB table using Unicode Arabic Presentation Forms, |
| 335 | * in case the font has cmap entries for the presentation-forms characters. */ |
| 336 | if (arabic_fallback_plan_init_unicode (fallback_plan, plan, font)) |
| 337 | return fallback_plan; |
| 338 | |
| 339 | /* See if this looks like a Windows-1256-encoded font. If it does, use a |
| 340 | * hand-coded GSUB table. */ |
| 341 | if (arabic_fallback_plan_init_win1256 (fallback_plan, plan, font)) |
| 342 | return fallback_plan; |
| 343 | |
Behdad Esfahbod | b912fbe | 2018-08-06 06:30:12 -0700 | [diff] [blame] | 344 | assert (fallback_plan->num_lookups == 0); |
Behdad Esfahbod | 2337f0d | 2021-07-08 10:58:50 -0600 | [diff] [blame] | 345 | hb_free (fallback_plan); |
Ebrahim Byagowi | 2dda6dd | 2020-04-20 14:12:45 +0430 | [diff] [blame] | 346 | return const_cast<arabic_fallback_plan_t *> (&Null (arabic_fallback_plan_t)); |
Behdad Esfahbod | fabd311 | 2012-09-05 22:19:28 -0400 | [diff] [blame] | 347 | } |
| 348 | |
| 349 | static void |
| 350 | arabic_fallback_plan_destroy (arabic_fallback_plan_t *fallback_plan) |
| 351 | { |
Behdad Esfahbod | b912fbe | 2018-08-06 06:30:12 -0700 | [diff] [blame] | 352 | if (!fallback_plan || fallback_plan->num_lookups == 0) |
Behdad Esfahbod | fabd311 | 2012-09-05 22:19:28 -0400 | [diff] [blame] | 353 | return; |
| 354 | |
Behdad Esfahbod | f28b1c8 | 2014-07-30 02:15:44 -0400 | [diff] [blame] | 355 | for (unsigned int i = 0; i < fallback_plan->num_lookups; i++) |
Behdad Esfahbod | fabd311 | 2012-09-05 22:19:28 -0400 | [diff] [blame] | 356 | if (fallback_plan->lookup_array[i]) |
Behdad Esfahbod | 45fd942 | 2013-05-02 18:06:51 -0400 | [diff] [blame] | 357 | { |
Behdad Esfahbod | 7a4bd97 | 2023-01-31 14:59:39 -0700 | [diff] [blame] | 358 | hb_free (fallback_plan->accel_array[i]); |
Behdad Esfahbod | f28b1c8 | 2014-07-30 02:15:44 -0400 | [diff] [blame] | 359 | if (fallback_plan->free_lookups) |
Behdad Esfahbod | 2337f0d | 2021-07-08 10:58:50 -0600 | [diff] [blame] | 360 | hb_free (fallback_plan->lookup_array[i]); |
Behdad Esfahbod | 45fd942 | 2013-05-02 18:06:51 -0400 | [diff] [blame] | 361 | } |
Behdad Esfahbod | fabd311 | 2012-09-05 22:19:28 -0400 | [diff] [blame] | 362 | |
Behdad Esfahbod | 2337f0d | 2021-07-08 10:58:50 -0600 | [diff] [blame] | 363 | hb_free (fallback_plan); |
Behdad Esfahbod | fabd311 | 2012-09-05 22:19:28 -0400 | [diff] [blame] | 364 | } |
| 365 | |
| 366 | static void |
| 367 | arabic_fallback_plan_shape (arabic_fallback_plan_t *fallback_plan, |
| 368 | hb_font_t *font, |
| 369 | hb_buffer_t *buffer) |
| 370 | { |
Behdad Esfahbod | 690af7a | 2023-06-30 10:36:01 -0600 | [diff] [blame] | 371 | OT::hb_ot_apply_context_t c (0, font, buffer, hb_blob_get_empty ()); |
Behdad Esfahbod | f28b1c8 | 2014-07-30 02:15:44 -0400 | [diff] [blame] | 372 | for (unsigned int i = 0; i < fallback_plan->num_lookups; i++) |
Behdad Esfahbod | fabd311 | 2012-09-05 22:19:28 -0400 | [diff] [blame] | 373 | if (fallback_plan->lookup_array[i]) { |
Behdad Esfahbod | bac1dd6 | 2013-05-02 18:52:24 -0400 | [diff] [blame] | 374 | c.set_lookup_mask (fallback_plan->mask_array[i]); |
Behdad Esfahbod | 7a4bd97 | 2023-01-31 14:59:39 -0700 | [diff] [blame] | 375 | if (fallback_plan->accel_array[i]) |
| 376 | hb_ot_layout_substitute_lookup (&c, |
| 377 | *fallback_plan->lookup_array[i], |
| 378 | *fallback_plan->accel_array[i]); |
Behdad Esfahbod | fabd311 | 2012-09-05 22:19:28 -0400 | [diff] [blame] | 379 | } |
| 380 | } |
| 381 | |
| 382 | |
Behdad Esfahbod | 44be1e5 | 2022-06-03 02:54:33 -0600 | [diff] [blame] | 383 | #endif /* HB_OT_SHAPER_ARABIC_FALLBACK_HH */ |