Fri, 16 Jan 2015 18:13:44 +0100
Integrate suggestion from review to improve consistency with existing code.
michael@0 | 1 | /* |
michael@0 | 2 | * Copyright © 2012 Google, Inc. |
michael@0 | 3 | * |
michael@0 | 4 | * This is part of HarfBuzz, a text shaping library. |
michael@0 | 5 | * |
michael@0 | 6 | * Permission is hereby granted, without written agreement and without |
michael@0 | 7 | * license or royalty fees, to use, copy, modify, and distribute this |
michael@0 | 8 | * software and its documentation for any purpose, provided that the |
michael@0 | 9 | * above copyright notice and the following two paragraphs appear in |
michael@0 | 10 | * all copies of this software. |
michael@0 | 11 | * |
michael@0 | 12 | * IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE TO ANY PARTY FOR |
michael@0 | 13 | * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES |
michael@0 | 14 | * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN |
michael@0 | 15 | * IF THE COPYRIGHT HOLDER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH |
michael@0 | 16 | * DAMAGE. |
michael@0 | 17 | * |
michael@0 | 18 | * THE COPYRIGHT HOLDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, |
michael@0 | 19 | * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND |
michael@0 | 20 | * FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS |
michael@0 | 21 | * ON AN "AS IS" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO |
michael@0 | 22 | * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS. |
michael@0 | 23 | * |
michael@0 | 24 | * Google Author(s): Behdad Esfahbod |
michael@0 | 25 | */ |
michael@0 | 26 | |
michael@0 | 27 | #ifndef HB_OT_SHAPE_COMPLEX_ARABIC_FALLBACK_HH |
michael@0 | 28 | #define HB_OT_SHAPE_COMPLEX_ARABIC_FALLBACK_HH |
michael@0 | 29 | |
michael@0 | 30 | #include "hb-private.hh" |
michael@0 | 31 | |
michael@0 | 32 | #include "hb-ot-shape-private.hh" |
michael@0 | 33 | #include "hb-ot-layout-gsub-table.hh" |
michael@0 | 34 | |
michael@0 | 35 | |
michael@0 | 36 | static const hb_tag_t arabic_fallback_features[] = |
michael@0 | 37 | { |
michael@0 | 38 | HB_TAG('i','n','i','t'), |
michael@0 | 39 | HB_TAG('m','e','d','i'), |
michael@0 | 40 | HB_TAG('f','i','n','a'), |
michael@0 | 41 | HB_TAG('i','s','o','l'), |
michael@0 | 42 | HB_TAG('r','l','i','g'), |
michael@0 | 43 | }; |
michael@0 | 44 | |
michael@0 | 45 | /* Same order as the fallback feature array */ |
michael@0 | 46 | enum { |
michael@0 | 47 | FALLBACK_INIT, |
michael@0 | 48 | FALLBACK_MEDI, |
michael@0 | 49 | FALLBACK_FINA, |
michael@0 | 50 | FALLBACK_ISOL, |
michael@0 | 51 | FALLBACK_RLIG, |
michael@0 | 52 | ARABIC_NUM_FALLBACK_FEATURES |
michael@0 | 53 | }; |
michael@0 | 54 | |
michael@0 | 55 | static OT::SubstLookup * |
michael@0 | 56 | arabic_fallback_synthesize_lookup_single (const hb_ot_shape_plan_t *plan HB_UNUSED, |
michael@0 | 57 | hb_font_t *font, |
michael@0 | 58 | unsigned int feature_index) |
michael@0 | 59 | { |
michael@0 | 60 | OT::GlyphID glyphs[SHAPING_TABLE_LAST - SHAPING_TABLE_FIRST + 1]; |
michael@0 | 61 | OT::GlyphID substitutes[SHAPING_TABLE_LAST - SHAPING_TABLE_FIRST + 1]; |
michael@0 | 62 | unsigned int num_glyphs = 0; |
michael@0 | 63 | |
michael@0 | 64 | /* Populate arrays */ |
michael@0 | 65 | for (hb_codepoint_t u = SHAPING_TABLE_FIRST; u < SHAPING_TABLE_LAST + 1; u++) |
michael@0 | 66 | { |
michael@0 | 67 | hb_codepoint_t s = shaping_table[u - SHAPING_TABLE_FIRST][feature_index]; |
michael@0 | 68 | hb_codepoint_t u_glyph, s_glyph; |
michael@0 | 69 | |
michael@0 | 70 | if (!s || |
michael@0 | 71 | !hb_font_get_glyph (font, u, 0, &u_glyph) || |
michael@0 | 72 | !hb_font_get_glyph (font, s, 0, &s_glyph) || |
michael@0 | 73 | u_glyph == s_glyph || |
michael@0 | 74 | u_glyph > 0xFFFF || s_glyph > 0xFFFF) |
michael@0 | 75 | continue; |
michael@0 | 76 | |
michael@0 | 77 | glyphs[num_glyphs].set (u_glyph); |
michael@0 | 78 | substitutes[num_glyphs].set (s_glyph); |
michael@0 | 79 | |
michael@0 | 80 | num_glyphs++; |
michael@0 | 81 | } |
michael@0 | 82 | |
michael@0 | 83 | /* Bubble-sort! |
michael@0 | 84 | * May not be good-enough for presidential candidate interviews, but good-enough for us... */ |
michael@0 | 85 | hb_bubble_sort (&glyphs[0], num_glyphs, OT::GlyphID::cmp, &substitutes[0]); |
michael@0 | 86 | |
michael@0 | 87 | OT::Supplier<OT::GlyphID> glyphs_supplier (glyphs, num_glyphs); |
michael@0 | 88 | OT::Supplier<OT::GlyphID> substitutes_supplier (substitutes, num_glyphs); |
michael@0 | 89 | |
michael@0 | 90 | /* Each glyph takes four bytes max, and there's some overhead. */ |
michael@0 | 91 | char buf[(SHAPING_TABLE_LAST - SHAPING_TABLE_FIRST + 1) * 4 + 128]; |
michael@0 | 92 | OT::hb_serialize_context_t c (buf, sizeof (buf)); |
michael@0 | 93 | OT::SubstLookup *lookup = c.start_serialize<OT::SubstLookup> (); |
michael@0 | 94 | bool ret = lookup->serialize_single (&c, |
michael@0 | 95 | OT::LookupFlag::IgnoreMarks, |
michael@0 | 96 | glyphs_supplier, |
michael@0 | 97 | substitutes_supplier, |
michael@0 | 98 | num_glyphs); |
michael@0 | 99 | c.end_serialize (); |
michael@0 | 100 | /* TODO sanitize the results? */ |
michael@0 | 101 | |
michael@0 | 102 | return ret ? c.copy<OT::SubstLookup> () : NULL; |
michael@0 | 103 | } |
michael@0 | 104 | |
michael@0 | 105 | static OT::SubstLookup * |
michael@0 | 106 | arabic_fallback_synthesize_lookup_ligature (const hb_ot_shape_plan_t *plan HB_UNUSED, |
michael@0 | 107 | hb_font_t *font) |
michael@0 | 108 | { |
michael@0 | 109 | OT::GlyphID first_glyphs[ARRAY_LENGTH_CONST (ligature_table)]; |
michael@0 | 110 | unsigned int first_glyphs_indirection[ARRAY_LENGTH_CONST (ligature_table)]; |
michael@0 | 111 | unsigned int ligature_per_first_glyph_count_list[ARRAY_LENGTH_CONST (first_glyphs)]; |
michael@0 | 112 | unsigned int num_first_glyphs = 0; |
michael@0 | 113 | |
michael@0 | 114 | /* We know that all our ligatures are 2-component */ |
michael@0 | 115 | OT::GlyphID ligature_list[ARRAY_LENGTH_CONST (first_glyphs) * ARRAY_LENGTH_CONST(ligature_table[0].ligatures)]; |
michael@0 | 116 | unsigned int component_count_list[ARRAY_LENGTH_CONST (ligature_list)]; |
michael@0 | 117 | OT::GlyphID component_list[ARRAY_LENGTH_CONST (ligature_list) * 1/* One extra component per ligature */]; |
michael@0 | 118 | unsigned int num_ligatures = 0; |
michael@0 | 119 | |
michael@0 | 120 | /* Populate arrays */ |
michael@0 | 121 | |
michael@0 | 122 | /* Sort out the first-glyphs */ |
michael@0 | 123 | for (unsigned int first_glyph_idx = 0; first_glyph_idx < ARRAY_LENGTH (first_glyphs); first_glyph_idx++) |
michael@0 | 124 | { |
michael@0 | 125 | hb_codepoint_t first_u = ligature_table[first_glyph_idx].first; |
michael@0 | 126 | hb_codepoint_t first_glyph; |
michael@0 | 127 | if (!hb_font_get_glyph (font, first_u, 0, &first_glyph)) |
michael@0 | 128 | continue; |
michael@0 | 129 | first_glyphs[num_first_glyphs].set (first_glyph); |
michael@0 | 130 | ligature_per_first_glyph_count_list[num_first_glyphs] = 0; |
michael@0 | 131 | first_glyphs_indirection[num_first_glyphs] = first_glyph_idx; |
michael@0 | 132 | num_first_glyphs++; |
michael@0 | 133 | } |
michael@0 | 134 | hb_bubble_sort (&first_glyphs[0], num_first_glyphs, OT::GlyphID::cmp, &first_glyphs_indirection[0]); |
michael@0 | 135 | |
michael@0 | 136 | /* Now that the first-glyphs are sorted, walk again, populate ligatures. */ |
michael@0 | 137 | for (unsigned int i = 0; i < num_first_glyphs; i++) |
michael@0 | 138 | { |
michael@0 | 139 | unsigned int first_glyph_idx = first_glyphs_indirection[i]; |
michael@0 | 140 | |
michael@0 | 141 | for (unsigned int second_glyph_idx = 0; second_glyph_idx < ARRAY_LENGTH (ligature_table[0].ligatures); second_glyph_idx++) |
michael@0 | 142 | { |
michael@0 | 143 | hb_codepoint_t second_u = ligature_table[first_glyph_idx].ligatures[second_glyph_idx].second; |
michael@0 | 144 | hb_codepoint_t ligature_u = ligature_table[first_glyph_idx].ligatures[second_glyph_idx].ligature; |
michael@0 | 145 | hb_codepoint_t second_glyph, ligature_glyph; |
michael@0 | 146 | if (!second_u || |
michael@0 | 147 | !hb_font_get_glyph (font, second_u, 0, &second_glyph) || |
michael@0 | 148 | !hb_font_get_glyph (font, ligature_u, 0, &ligature_glyph)) |
michael@0 | 149 | continue; |
michael@0 | 150 | |
michael@0 | 151 | ligature_per_first_glyph_count_list[i]++; |
michael@0 | 152 | |
michael@0 | 153 | ligature_list[num_ligatures].set (ligature_glyph); |
michael@0 | 154 | component_count_list[num_ligatures] = 2; |
michael@0 | 155 | component_list[num_ligatures].set (second_glyph); |
michael@0 | 156 | num_ligatures++; |
michael@0 | 157 | } |
michael@0 | 158 | } |
michael@0 | 159 | |
michael@0 | 160 | OT::Supplier<OT::GlyphID> first_glyphs_supplier (first_glyphs, num_first_glyphs); |
michael@0 | 161 | OT::Supplier<unsigned int > ligature_per_first_glyph_count_supplier (ligature_per_first_glyph_count_list, num_first_glyphs); |
michael@0 | 162 | OT::Supplier<OT::GlyphID> ligatures_supplier (ligature_list, num_ligatures); |
michael@0 | 163 | OT::Supplier<unsigned int > component_count_supplier (component_count_list, num_ligatures); |
michael@0 | 164 | OT::Supplier<OT::GlyphID> component_supplier (component_list, num_ligatures); |
michael@0 | 165 | |
michael@0 | 166 | /* 16 bytes per ligature ought to be enough... */ |
michael@0 | 167 | char buf[ARRAY_LENGTH_CONST (ligature_list) * 16 + 128]; |
michael@0 | 168 | OT::hb_serialize_context_t c (buf, sizeof (buf)); |
michael@0 | 169 | OT::SubstLookup *lookup = c.start_serialize<OT::SubstLookup> (); |
michael@0 | 170 | bool ret = lookup->serialize_ligature (&c, |
michael@0 | 171 | OT::LookupFlag::IgnoreMarks, |
michael@0 | 172 | first_glyphs_supplier, |
michael@0 | 173 | ligature_per_first_glyph_count_supplier, |
michael@0 | 174 | num_first_glyphs, |
michael@0 | 175 | ligatures_supplier, |
michael@0 | 176 | component_count_supplier, |
michael@0 | 177 | component_supplier); |
michael@0 | 178 | |
michael@0 | 179 | c.end_serialize (); |
michael@0 | 180 | /* TODO sanitize the results? */ |
michael@0 | 181 | |
michael@0 | 182 | return ret ? c.copy<OT::SubstLookup> () : NULL; |
michael@0 | 183 | } |
michael@0 | 184 | |
michael@0 | 185 | static OT::SubstLookup * |
michael@0 | 186 | arabic_fallback_synthesize_lookup (const hb_ot_shape_plan_t *plan, |
michael@0 | 187 | hb_font_t *font, |
michael@0 | 188 | unsigned int feature_index) |
michael@0 | 189 | { |
michael@0 | 190 | if (feature_index < 4) |
michael@0 | 191 | return arabic_fallback_synthesize_lookup_single (plan, font, feature_index); |
michael@0 | 192 | else |
michael@0 | 193 | return arabic_fallback_synthesize_lookup_ligature (plan, font); |
michael@0 | 194 | } |
michael@0 | 195 | |
michael@0 | 196 | struct arabic_fallback_plan_t |
michael@0 | 197 | { |
michael@0 | 198 | ASSERT_POD (); |
michael@0 | 199 | |
michael@0 | 200 | hb_mask_t mask_array[ARABIC_NUM_FALLBACK_FEATURES]; |
michael@0 | 201 | OT::SubstLookup *lookup_array[ARABIC_NUM_FALLBACK_FEATURES]; |
michael@0 | 202 | hb_ot_layout_lookup_accelerator_t accel_array[ARABIC_NUM_FALLBACK_FEATURES]; |
michael@0 | 203 | }; |
michael@0 | 204 | |
michael@0 | 205 | static const arabic_fallback_plan_t arabic_fallback_plan_nil = {}; |
michael@0 | 206 | |
michael@0 | 207 | static arabic_fallback_plan_t * |
michael@0 | 208 | arabic_fallback_plan_create (const hb_ot_shape_plan_t *plan, |
michael@0 | 209 | hb_font_t *font) |
michael@0 | 210 | { |
michael@0 | 211 | arabic_fallback_plan_t *fallback_plan = (arabic_fallback_plan_t *) calloc (1, sizeof (arabic_fallback_plan_t)); |
michael@0 | 212 | if (unlikely (!fallback_plan)) |
michael@0 | 213 | return const_cast<arabic_fallback_plan_t *> (&arabic_fallback_plan_nil); |
michael@0 | 214 | |
michael@0 | 215 | for (unsigned int i = 0; i < ARABIC_NUM_FALLBACK_FEATURES; i++) |
michael@0 | 216 | { |
michael@0 | 217 | fallback_plan->mask_array[i] = plan->map.get_1_mask (arabic_fallback_features[i]); |
michael@0 | 218 | if (fallback_plan->mask_array[i]) { |
michael@0 | 219 | fallback_plan->lookup_array[i] = arabic_fallback_synthesize_lookup (plan, font, i); |
michael@0 | 220 | if (fallback_plan->lookup_array[i]) |
michael@0 | 221 | fallback_plan->accel_array[i].init (*fallback_plan->lookup_array[i]); |
michael@0 | 222 | } |
michael@0 | 223 | } |
michael@0 | 224 | |
michael@0 | 225 | return fallback_plan; |
michael@0 | 226 | } |
michael@0 | 227 | |
michael@0 | 228 | static void |
michael@0 | 229 | arabic_fallback_plan_destroy (arabic_fallback_plan_t *fallback_plan) |
michael@0 | 230 | { |
michael@0 | 231 | if (!fallback_plan || fallback_plan == &arabic_fallback_plan_nil) |
michael@0 | 232 | return; |
michael@0 | 233 | |
michael@0 | 234 | for (unsigned int i = 0; i < ARABIC_NUM_FALLBACK_FEATURES; i++) |
michael@0 | 235 | if (fallback_plan->lookup_array[i]) |
michael@0 | 236 | { |
michael@0 | 237 | fallback_plan->accel_array[i].fini (fallback_plan->lookup_array[i]); |
michael@0 | 238 | free (fallback_plan->lookup_array[i]); |
michael@0 | 239 | } |
michael@0 | 240 | |
michael@0 | 241 | free (fallback_plan); |
michael@0 | 242 | } |
michael@0 | 243 | |
michael@0 | 244 | static void |
michael@0 | 245 | arabic_fallback_plan_shape (arabic_fallback_plan_t *fallback_plan, |
michael@0 | 246 | hb_font_t *font, |
michael@0 | 247 | hb_buffer_t *buffer) |
michael@0 | 248 | { |
michael@0 | 249 | OT::hb_apply_context_t c (0, font, buffer); |
michael@0 | 250 | for (unsigned int i = 0; i < ARABIC_NUM_FALLBACK_FEATURES; i++) |
michael@0 | 251 | if (fallback_plan->lookup_array[i]) { |
michael@0 | 252 | c.set_lookup_mask (fallback_plan->mask_array[i]); |
michael@0 | 253 | hb_ot_layout_substitute_lookup (&c, |
michael@0 | 254 | *fallback_plan->lookup_array[i], |
michael@0 | 255 | fallback_plan->accel_array[i]); |
michael@0 | 256 | } |
michael@0 | 257 | } |
michael@0 | 258 | |
michael@0 | 259 | |
michael@0 | 260 | #endif /* HB_OT_SHAPE_COMPLEX_ARABIC_FALLBACK_HH */ |