gfx/harfbuzz/src/hb-ot-shape-complex-arabic.cc

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 /*
michael@0 2 * Copyright © 2010,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 #include "hb-ot-shape-complex-private.hh"
michael@0 28 #include "hb-ot-shape-private.hh"
michael@0 29
michael@0 30
michael@0 31 /* buffer var allocations */
michael@0 32 #define arabic_shaping_action() complex_var_u8_0() /* arabic shaping action */
michael@0 33
michael@0 34
michael@0 35 /*
michael@0 36 * Bits used in the joining tables
michael@0 37 */
michael@0 38 enum {
michael@0 39 JOINING_TYPE_U = 0,
michael@0 40 JOINING_TYPE_L = 1,
michael@0 41 JOINING_TYPE_R = 2,
michael@0 42 JOINING_TYPE_D = 3,
michael@0 43 JOINING_TYPE_C = JOINING_TYPE_D,
michael@0 44 JOINING_GROUP_ALAPH = 4,
michael@0 45 JOINING_GROUP_DALATH_RISH = 5,
michael@0 46 NUM_STATE_MACHINE_COLS = 6,
michael@0 47
michael@0 48 JOINING_TYPE_T = 7,
michael@0 49 JOINING_TYPE_X = 8 /* means: use general-category to choose between U or T. */
michael@0 50 };
michael@0 51
michael@0 52 /*
michael@0 53 * Joining types:
michael@0 54 */
michael@0 55
michael@0 56 #include "hb-ot-shape-complex-arabic-table.hh"
michael@0 57
michael@0 58 static unsigned int get_joining_type (hb_codepoint_t u, hb_unicode_general_category_t gen_cat)
michael@0 59 {
michael@0 60 if (likely (hb_in_range<hb_codepoint_t> (u, JOINING_TABLE_FIRST, JOINING_TABLE_LAST))) {
michael@0 61 unsigned int j_type = joining_table[u - JOINING_TABLE_FIRST];
michael@0 62 if (likely (j_type != JOINING_TYPE_X))
michael@0 63 return j_type;
michael@0 64 }
michael@0 65
michael@0 66 /* Mongolian joining data is not in ArabicJoining.txt yet. */
michael@0 67 if (unlikely (hb_in_range<hb_codepoint_t> (u, 0x1800, 0x18AF)))
michael@0 68 {
michael@0 69 if (unlikely (hb_in_range<hb_codepoint_t> (u, 0x1880, 0x1886)))
michael@0 70 return JOINING_TYPE_U;
michael@0 71
michael@0 72 /* All letters, SIBE SYLLABLE BOUNDARY MARKER, and NIRUGU are D */
michael@0 73 if ((FLAG(gen_cat) & (FLAG (HB_UNICODE_GENERAL_CATEGORY_OTHER_LETTER) |
michael@0 74 FLAG (HB_UNICODE_GENERAL_CATEGORY_MODIFIER_LETTER)))
michael@0 75 || u == 0x1807 || u == 0x180A)
michael@0 76 return JOINING_TYPE_D;
michael@0 77 }
michael@0 78
michael@0 79 /* 'Phags-pa joining data is not in ArabicJoining.txt yet. */
michael@0 80 if (unlikely (hb_in_range<hb_codepoint_t> (u, 0xA840, 0xA872)))
michael@0 81 {
michael@0 82 if (unlikely (u == 0xA872))
michael@0 83 return JOINING_TYPE_L;
michael@0 84
michael@0 85 return JOINING_TYPE_D;
michael@0 86 }
michael@0 87
michael@0 88 if (unlikely (hb_in_range<hb_codepoint_t> (u, 0x200C, 0x200D)))
michael@0 89 {
michael@0 90 return u == 0x200C ? JOINING_TYPE_U : JOINING_TYPE_C;
michael@0 91 }
michael@0 92
michael@0 93 return (FLAG(gen_cat) & (FLAG(HB_UNICODE_GENERAL_CATEGORY_NON_SPACING_MARK) | FLAG(HB_UNICODE_GENERAL_CATEGORY_ENCLOSING_MARK) | FLAG(HB_UNICODE_GENERAL_CATEGORY_FORMAT))) ?
michael@0 94 JOINING_TYPE_T : JOINING_TYPE_U;
michael@0 95 }
michael@0 96
michael@0 97 static const hb_tag_t arabic_features[] =
michael@0 98 {
michael@0 99 HB_TAG('i','n','i','t'),
michael@0 100 HB_TAG('m','e','d','i'),
michael@0 101 HB_TAG('f','i','n','a'),
michael@0 102 HB_TAG('i','s','o','l'),
michael@0 103 /* Syriac */
michael@0 104 HB_TAG('m','e','d','2'),
michael@0 105 HB_TAG('f','i','n','2'),
michael@0 106 HB_TAG('f','i','n','3'),
michael@0 107 HB_TAG_NONE
michael@0 108 };
michael@0 109
michael@0 110
michael@0 111 /* Same order as the feature array */
michael@0 112 enum {
michael@0 113 INIT,
michael@0 114 MEDI,
michael@0 115 FINA,
michael@0 116 ISOL,
michael@0 117
michael@0 118 /* Syriac */
michael@0 119 MED2,
michael@0 120 FIN2,
michael@0 121 FIN3,
michael@0 122
michael@0 123 NONE,
michael@0 124
michael@0 125 ARABIC_NUM_FEATURES = NONE
michael@0 126 };
michael@0 127
michael@0 128 static const struct arabic_state_table_entry {
michael@0 129 uint8_t prev_action;
michael@0 130 uint8_t curr_action;
michael@0 131 uint16_t next_state;
michael@0 132 } arabic_state_table[][NUM_STATE_MACHINE_COLS] =
michael@0 133 {
michael@0 134 /* jt_U, jt_L, jt_R, jt_D, jg_ALAPH, jg_DALATH_RISH */
michael@0 135
michael@0 136 /* State 0: prev was U, not willing to join. */
michael@0 137 { {NONE,NONE,0}, {NONE,ISOL,2}, {NONE,ISOL,1}, {NONE,ISOL,2}, {NONE,ISOL,1}, {NONE,ISOL,6}, },
michael@0 138
michael@0 139 /* State 1: prev was R or ISOL/ALAPH, not willing to join. */
michael@0 140 { {NONE,NONE,0}, {NONE,ISOL,2}, {NONE,ISOL,1}, {NONE,ISOL,2}, {NONE,FIN2,5}, {NONE,ISOL,6}, },
michael@0 141
michael@0 142 /* State 2: prev was D/L in ISOL form, willing to join. */
michael@0 143 { {NONE,NONE,0}, {NONE,ISOL,2}, {INIT,FINA,1}, {INIT,FINA,3}, {INIT,FINA,4}, {INIT,FINA,6}, },
michael@0 144
michael@0 145 /* State 3: prev was D in FINA form, willing to join. */
michael@0 146 { {NONE,NONE,0}, {NONE,ISOL,2}, {MEDI,FINA,1}, {MEDI,FINA,3}, {MEDI,FINA,4}, {MEDI,FINA,6}, },
michael@0 147
michael@0 148 /* State 4: prev was FINA ALAPH, not willing to join. */
michael@0 149 { {NONE,NONE,0}, {NONE,ISOL,2}, {MED2,ISOL,1}, {MED2,ISOL,2}, {MED2,FIN2,5}, {MED2,ISOL,6}, },
michael@0 150
michael@0 151 /* State 5: prev was FIN2/FIN3 ALAPH, not willing to join. */
michael@0 152 { {NONE,NONE,0}, {NONE,ISOL,2}, {ISOL,ISOL,1}, {ISOL,ISOL,2}, {ISOL,FIN2,5}, {ISOL,ISOL,6}, },
michael@0 153
michael@0 154 /* State 6: prev was DALATH/RISH, not willing to join. */
michael@0 155 { {NONE,NONE,0}, {NONE,ISOL,2}, {NONE,ISOL,1}, {NONE,ISOL,2}, {NONE,FIN3,5}, {NONE,ISOL,6}, }
michael@0 156 };
michael@0 157
michael@0 158
michael@0 159 static void
michael@0 160 nuke_joiners (const hb_ot_shape_plan_t *plan,
michael@0 161 hb_font_t *font,
michael@0 162 hb_buffer_t *buffer);
michael@0 163
michael@0 164 static void
michael@0 165 arabic_fallback_shape (const hb_ot_shape_plan_t *plan,
michael@0 166 hb_font_t *font,
michael@0 167 hb_buffer_t *buffer);
michael@0 168
michael@0 169 static void
michael@0 170 collect_features_arabic (hb_ot_shape_planner_t *plan)
michael@0 171 {
michael@0 172 hb_ot_map_builder_t *map = &plan->map;
michael@0 173
michael@0 174 /* For Language forms (in ArabicOT speak), we do the iso/fina/medi/init together,
michael@0 175 * then rlig and calt each in their own stage. This makes IranNastaliq's ALLAH
michael@0 176 * ligature work correctly. It's unfortunate though...
michael@0 177 *
michael@0 178 * This also makes Arial Bold in Windows7 work. See:
michael@0 179 * https://bugzilla.mozilla.org/show_bug.cgi?id=644184
michael@0 180 *
michael@0 181 * TODO: Add test cases for these two.
michael@0 182 */
michael@0 183
michael@0 184 map->add_gsub_pause (nuke_joiners);
michael@0 185
michael@0 186 map->add_global_bool_feature (HB_TAG('c','c','m','p'));
michael@0 187 map->add_global_bool_feature (HB_TAG('l','o','c','l'));
michael@0 188
michael@0 189 map->add_gsub_pause (NULL);
michael@0 190
michael@0 191 for (unsigned int i = 0; i < ARABIC_NUM_FEATURES; i++)
michael@0 192 map->add_feature (arabic_features[i], 1, i < 4 ? F_HAS_FALLBACK : F_NONE); /* The first four features have fallback. */
michael@0 193
michael@0 194 map->add_gsub_pause (NULL);
michael@0 195
michael@0 196 map->add_feature (HB_TAG('r','l','i','g'), 1, F_GLOBAL|F_HAS_FALLBACK);
michael@0 197 map->add_gsub_pause (arabic_fallback_shape);
michael@0 198
michael@0 199 map->add_global_bool_feature (HB_TAG('c','a','l','t'));
michael@0 200 map->add_gsub_pause (NULL);
michael@0 201
michael@0 202 map->add_global_bool_feature (HB_TAG('m','s','e','t'));
michael@0 203 }
michael@0 204
michael@0 205 #include "hb-ot-shape-complex-arabic-fallback.hh"
michael@0 206
michael@0 207 struct arabic_shape_plan_t
michael@0 208 {
michael@0 209 ASSERT_POD ();
michael@0 210
michael@0 211 /* The "+ 1" in the next array is to accommodate for the "NONE" command,
michael@0 212 * which is not an OpenType feature, but this simplifies the code by not
michael@0 213 * having to do a "if (... < NONE) ..." and just rely on the fact that
michael@0 214 * mask_array[NONE] == 0. */
michael@0 215 hb_mask_t mask_array[ARABIC_NUM_FEATURES + 1];
michael@0 216
michael@0 217 bool do_fallback;
michael@0 218 arabic_fallback_plan_t *fallback_plan;
michael@0 219 };
michael@0 220
michael@0 221 static void *
michael@0 222 data_create_arabic (const hb_ot_shape_plan_t *plan)
michael@0 223 {
michael@0 224 arabic_shape_plan_t *arabic_plan = (arabic_shape_plan_t *) calloc (1, sizeof (arabic_shape_plan_t));
michael@0 225 if (unlikely (!arabic_plan))
michael@0 226 return NULL;
michael@0 227
michael@0 228 arabic_plan->do_fallback = plan->props.script == HB_SCRIPT_ARABIC;
michael@0 229 for (unsigned int i = 0; i < ARABIC_NUM_FEATURES; i++) {
michael@0 230 arabic_plan->mask_array[i] = plan->map.get_1_mask (arabic_features[i]);
michael@0 231 if (i < 4)
michael@0 232 arabic_plan->do_fallback = arabic_plan->do_fallback && plan->map.needs_fallback (arabic_features[i]);
michael@0 233 }
michael@0 234
michael@0 235 return arabic_plan;
michael@0 236 }
michael@0 237
michael@0 238 static void
michael@0 239 data_destroy_arabic (void *data)
michael@0 240 {
michael@0 241 arabic_shape_plan_t *arabic_plan = (arabic_shape_plan_t *) data;
michael@0 242
michael@0 243 arabic_fallback_plan_destroy (arabic_plan->fallback_plan);
michael@0 244
michael@0 245 free (data);
michael@0 246 }
michael@0 247
michael@0 248 static void
michael@0 249 arabic_joining (hb_buffer_t *buffer)
michael@0 250 {
michael@0 251 unsigned int count = buffer->len;
michael@0 252 unsigned int prev = (unsigned int) -1, state = 0;
michael@0 253
michael@0 254 HB_BUFFER_ALLOCATE_VAR (buffer, arabic_shaping_action);
michael@0 255
michael@0 256 /* Check pre-context */
michael@0 257 if (!(buffer->flags & HB_BUFFER_FLAG_BOT))
michael@0 258 for (unsigned int i = 0; i < buffer->context_len[0]; i++)
michael@0 259 {
michael@0 260 unsigned int this_type = get_joining_type (buffer->context[0][i], buffer->unicode->general_category (buffer->context[0][i]));
michael@0 261
michael@0 262 if (unlikely (this_type == JOINING_TYPE_T))
michael@0 263 continue;
michael@0 264
michael@0 265 const arabic_state_table_entry *entry = &arabic_state_table[state][this_type];
michael@0 266 state = entry->next_state;
michael@0 267 break;
michael@0 268 }
michael@0 269
michael@0 270 for (unsigned int i = 0; i < count; i++)
michael@0 271 {
michael@0 272 unsigned int this_type = get_joining_type (buffer->info[i].codepoint, _hb_glyph_info_get_general_category (&buffer->info[i]));
michael@0 273
michael@0 274 if (unlikely (this_type == JOINING_TYPE_T)) {
michael@0 275 buffer->info[i].arabic_shaping_action() = NONE;
michael@0 276 continue;
michael@0 277 }
michael@0 278
michael@0 279 const arabic_state_table_entry *entry = &arabic_state_table[state][this_type];
michael@0 280
michael@0 281 if (entry->prev_action != NONE && prev != (unsigned int) -1)
michael@0 282 for (; prev < i; prev++)
michael@0 283 buffer->info[prev].arabic_shaping_action() = entry->prev_action;
michael@0 284
michael@0 285 buffer->info[i].arabic_shaping_action() = entry->curr_action;
michael@0 286
michael@0 287 prev = i;
michael@0 288 state = entry->next_state;
michael@0 289 }
michael@0 290
michael@0 291 if (!(buffer->flags & HB_BUFFER_FLAG_EOT))
michael@0 292 for (unsigned int i = 0; i < buffer->context_len[1]; i++)
michael@0 293 {
michael@0 294 unsigned int this_type = get_joining_type (buffer->context[1][i], buffer->unicode->general_category (buffer->context[1][i]));
michael@0 295
michael@0 296 if (unlikely (this_type == JOINING_TYPE_T))
michael@0 297 continue;
michael@0 298
michael@0 299 const arabic_state_table_entry *entry = &arabic_state_table[state][this_type];
michael@0 300 if (entry->prev_action != NONE && prev != (unsigned int) -1)
michael@0 301 buffer->info[prev].arabic_shaping_action() = entry->prev_action;
michael@0 302 break;
michael@0 303 }
michael@0 304
michael@0 305
michael@0 306 HB_BUFFER_DEALLOCATE_VAR (buffer, arabic_shaping_action);
michael@0 307 }
michael@0 308
michael@0 309 static void
michael@0 310 setup_masks_arabic (const hb_ot_shape_plan_t *plan,
michael@0 311 hb_buffer_t *buffer,
michael@0 312 hb_font_t *font HB_UNUSED)
michael@0 313 {
michael@0 314 const arabic_shape_plan_t *arabic_plan = (const arabic_shape_plan_t *) plan->data;
michael@0 315
michael@0 316 arabic_joining (buffer);
michael@0 317 unsigned int count = buffer->len;
michael@0 318 for (unsigned int i = 0; i < count; i++)
michael@0 319 buffer->info[i].mask |= arabic_plan->mask_array[buffer->info[i].arabic_shaping_action()];
michael@0 320 }
michael@0 321
michael@0 322
michael@0 323 static void
michael@0 324 nuke_joiners (const hb_ot_shape_plan_t *plan HB_UNUSED,
michael@0 325 hb_font_t *font HB_UNUSED,
michael@0 326 hb_buffer_t *buffer)
michael@0 327 {
michael@0 328 unsigned int count = buffer->len;
michael@0 329 for (unsigned int i = 0; i < count; i++)
michael@0 330 if (_hb_glyph_info_is_zwj (&buffer->info[i]))
michael@0 331 _hb_glyph_info_flip_joiners (&buffer->info[i]);
michael@0 332 }
michael@0 333
michael@0 334 static void
michael@0 335 arabic_fallback_shape (const hb_ot_shape_plan_t *plan,
michael@0 336 hb_font_t *font,
michael@0 337 hb_buffer_t *buffer)
michael@0 338 {
michael@0 339 const arabic_shape_plan_t *arabic_plan = (const arabic_shape_plan_t *) plan->data;
michael@0 340
michael@0 341 if (!arabic_plan->do_fallback)
michael@0 342 return;
michael@0 343
michael@0 344 retry:
michael@0 345 arabic_fallback_plan_t *fallback_plan = (arabic_fallback_plan_t *) hb_atomic_ptr_get (&arabic_plan->fallback_plan);
michael@0 346 if (unlikely (!fallback_plan))
michael@0 347 {
michael@0 348 /* This sucks. We need a font to build the fallback plan... */
michael@0 349 fallback_plan = arabic_fallback_plan_create (plan, font);
michael@0 350 if (unlikely (!hb_atomic_ptr_cmpexch (&(const_cast<arabic_shape_plan_t *> (arabic_plan))->fallback_plan, NULL, fallback_plan))) {
michael@0 351 arabic_fallback_plan_destroy (fallback_plan);
michael@0 352 goto retry;
michael@0 353 }
michael@0 354 }
michael@0 355
michael@0 356 arabic_fallback_plan_shape (fallback_plan, font, buffer);
michael@0 357 }
michael@0 358
michael@0 359
michael@0 360 const hb_ot_complex_shaper_t _hb_ot_complex_shaper_arabic =
michael@0 361 {
michael@0 362 "arabic",
michael@0 363 collect_features_arabic,
michael@0 364 NULL, /* override_features */
michael@0 365 data_create_arabic,
michael@0 366 data_destroy_arabic,
michael@0 367 NULL, /* preprocess_text_arabic */
michael@0 368 HB_OT_SHAPE_NORMALIZATION_MODE_DEFAULT,
michael@0 369 NULL, /* decompose */
michael@0 370 NULL, /* compose */
michael@0 371 setup_masks_arabic,
michael@0 372 HB_OT_SHAPE_ZERO_WIDTH_MARKS_BY_GDEF_LATE,
michael@0 373 true, /* fallback_position */
michael@0 374 };

mercurial