Disable flaky WebNavigationApiTest.Failures on Windows.
[chromium-blink-merge.git] / third_party / harfbuzz-ng / src / hb-ot-shape-complex-arabic.cc
blobae90864127104969b5a99243e8ba49218e2e6f25
1 /*
2 * Copyright © 2010,2012 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
16 * DAMAGE.
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"
28 #include "hb-ot-shape-private.hh"
31 /* buffer var allocations */
32 #define arabic_shaping_action() complex_var_u8_0() /* arabic shaping action */
36 * Bits used in the joining tables
38 enum {
39 JOINING_TYPE_U = 0,
40 JOINING_TYPE_L = 1,
41 JOINING_TYPE_R = 2,
42 JOINING_TYPE_D = 3,
43 JOINING_TYPE_C = JOINING_TYPE_D,
44 JOINING_GROUP_ALAPH = 4,
45 JOINING_GROUP_DALATH_RISH = 5,
46 NUM_STATE_MACHINE_COLS = 6,
48 JOINING_TYPE_T = 7,
49 JOINING_TYPE_X = 8 /* means: use general-category to choose between U or T. */
53 * Joining types:
56 #include "hb-ot-shape-complex-arabic-table.hh"
58 static unsigned int get_joining_type (hb_codepoint_t u, hb_unicode_general_category_t gen_cat)
60 unsigned int j_type = joining_type(u);
61 if (likely (j_type != JOINING_TYPE_X))
62 return j_type;
64 return (FLAG(gen_cat) &
65 (FLAG(HB_UNICODE_GENERAL_CATEGORY_NON_SPACING_MARK) |
66 FLAG(HB_UNICODE_GENERAL_CATEGORY_ENCLOSING_MARK) |
67 FLAG(HB_UNICODE_GENERAL_CATEGORY_FORMAT))
68 ) ? JOINING_TYPE_T : JOINING_TYPE_U;
71 #define FEATURE_IS_SYRIAC(tag) hb_in_range<unsigned char> ((unsigned char) (tag), '2', '3')
73 static const hb_tag_t arabic_features[] =
75 HB_TAG('i','s','o','l'),
76 HB_TAG('f','i','n','a'),
77 HB_TAG('f','i','n','2'),
78 HB_TAG('f','i','n','3'),
79 HB_TAG('m','e','d','i'),
80 HB_TAG('m','e','d','2'),
81 HB_TAG('i','n','i','t'),
82 HB_TAG_NONE
86 /* Same order as the feature array */
87 enum {
88 ISOL,
89 FINA,
90 FIN2,
91 FIN3,
92 MEDI,
93 MED2,
94 INIT,
96 NONE,
98 ARABIC_NUM_FEATURES = NONE
101 static const struct arabic_state_table_entry {
102 uint8_t prev_action;
103 uint8_t curr_action;
104 uint16_t next_state;
105 } arabic_state_table[][NUM_STATE_MACHINE_COLS] =
107 /* jt_U, jt_L, jt_R, jt_D, jg_ALAPH, jg_DALATH_RISH */
109 /* State 0: prev was U, not willing to join. */
110 { {NONE,NONE,0}, {NONE,ISOL,2}, {NONE,ISOL,1}, {NONE,ISOL,2}, {NONE,ISOL,1}, {NONE,ISOL,6}, },
112 /* State 1: prev was R or ISOL/ALAPH, not willing to join. */
113 { {NONE,NONE,0}, {NONE,ISOL,2}, {NONE,ISOL,1}, {NONE,ISOL,2}, {NONE,FIN2,5}, {NONE,ISOL,6}, },
115 /* State 2: prev was D/L in ISOL form, willing to join. */
116 { {NONE,NONE,0}, {NONE,ISOL,2}, {INIT,FINA,1}, {INIT,FINA,3}, {INIT,FINA,4}, {INIT,FINA,6}, },
118 /* State 3: prev was D in FINA form, willing to join. */
119 { {NONE,NONE,0}, {NONE,ISOL,2}, {MEDI,FINA,1}, {MEDI,FINA,3}, {MEDI,FINA,4}, {MEDI,FINA,6}, },
121 /* State 4: prev was FINA ALAPH, not willing to join. */
122 { {NONE,NONE,0}, {NONE,ISOL,2}, {MED2,ISOL,1}, {MED2,ISOL,2}, {MED2,FIN2,5}, {MED2,ISOL,6}, },
124 /* State 5: prev was FIN2/FIN3 ALAPH, not willing to join. */
125 { {NONE,NONE,0}, {NONE,ISOL,2}, {ISOL,ISOL,1}, {ISOL,ISOL,2}, {ISOL,FIN2,5}, {ISOL,ISOL,6}, },
127 /* State 6: prev was DALATH/RISH, not willing to join. */
128 { {NONE,NONE,0}, {NONE,ISOL,2}, {NONE,ISOL,1}, {NONE,ISOL,2}, {NONE,FIN3,5}, {NONE,ISOL,6}, }
132 static void
133 nuke_joiners (const hb_ot_shape_plan_t *plan,
134 hb_font_t *font,
135 hb_buffer_t *buffer);
137 static void
138 arabic_fallback_shape (const hb_ot_shape_plan_t *plan,
139 hb_font_t *font,
140 hb_buffer_t *buffer);
142 static void
143 collect_features_arabic (hb_ot_shape_planner_t *plan)
145 hb_ot_map_builder_t *map = &plan->map;
147 /* We apply features according to the Arabic spec, with pauses
148 * in between most.
150 * The pause between init/medi/... and rlig is required. See eg:
151 * https://bugzilla.mozilla.org/show_bug.cgi?id=644184
153 * The pauses between init/medi/... themselves are not necessarily
154 * needed as only one of those features is applied to any character.
155 * The only difference it makes is when fonts have contextual
156 * substitutions. We now follow the order of the spec, which makes
157 * for better experience if that's what Uniscribe is doing.
159 * At least for Arabic, looks like Uniscribe has a pause between
160 * rlig and calt. Otherwise the IranNastaliq's ALLAH ligature won't
161 * work. However, testing shows that rlig and calt are applied
162 * together for Mongolian in Uniscribe. As such, we only add a
163 * pause for Arabic, not other scripts.
166 map->add_gsub_pause (nuke_joiners);
168 map->add_global_bool_feature (HB_TAG('c','c','m','p'));
169 map->add_global_bool_feature (HB_TAG('l','o','c','l'));
171 map->add_gsub_pause (NULL);
173 for (unsigned int i = 0; i < ARABIC_NUM_FEATURES; i++)
175 bool has_fallback = plan->props.script == HB_SCRIPT_ARABIC && !FEATURE_IS_SYRIAC (arabic_features[i]);
176 map->add_feature (arabic_features[i], 1, has_fallback ? F_HAS_FALLBACK : F_NONE);
177 map->add_gsub_pause (NULL);
180 map->add_feature (HB_TAG('r','l','i','g'), 1, F_GLOBAL|F_HAS_FALLBACK);
181 if (plan->props.script == HB_SCRIPT_ARABIC)
182 map->add_gsub_pause (arabic_fallback_shape);
184 map->add_global_bool_feature (HB_TAG('c','a','l','t'));
185 map->add_gsub_pause (NULL);
187 /* The spec includes 'cswh'. Earlier versions of Windows
188 * used to enable this by default, but testing suggests
189 * that Windows 8 and later do not enable it by default,
190 * and spec now says 'Off by default'.
191 * We disabled this in ae23c24c32.
192 * Note that IranNastaliq uses this feature extensively
193 * to fixup broken glyph sequences. Oh well...
194 * Test case: U+0643,U+0640,U+0631. */
195 //map->add_global_bool_feature (HB_TAG('c','s','w','h'));
196 map->add_global_bool_feature (HB_TAG('m','s','e','t'));
199 #include "hb-ot-shape-complex-arabic-fallback.hh"
201 struct arabic_shape_plan_t
203 ASSERT_POD ();
205 /* The "+ 1" in the next array is to accommodate for the "NONE" command,
206 * which is not an OpenType feature, but this simplifies the code by not
207 * having to do a "if (... < NONE) ..." and just rely on the fact that
208 * mask_array[NONE] == 0. */
209 hb_mask_t mask_array[ARABIC_NUM_FEATURES + 1];
211 bool do_fallback;
212 arabic_fallback_plan_t *fallback_plan;
215 static void *
216 data_create_arabic (const hb_ot_shape_plan_t *plan)
218 arabic_shape_plan_t *arabic_plan = (arabic_shape_plan_t *) calloc (1, sizeof (arabic_shape_plan_t));
219 if (unlikely (!arabic_plan))
220 return NULL;
222 arabic_plan->do_fallback = plan->props.script == HB_SCRIPT_ARABIC;
223 for (unsigned int i = 0; i < ARABIC_NUM_FEATURES; i++) {
224 arabic_plan->mask_array[i] = plan->map.get_1_mask (arabic_features[i]);
225 arabic_plan->do_fallback = arabic_plan->do_fallback &&
226 (FEATURE_IS_SYRIAC (arabic_features[i]) ||
227 plan->map.needs_fallback (arabic_features[i]));
230 return arabic_plan;
233 static void
234 data_destroy_arabic (void *data)
236 arabic_shape_plan_t *arabic_plan = (arabic_shape_plan_t *) data;
238 arabic_fallback_plan_destroy (arabic_plan->fallback_plan);
240 free (data);
243 static void
244 arabic_joining (hb_buffer_t *buffer)
246 unsigned int count = buffer->len;
247 hb_glyph_info_t *info = buffer->info;
248 unsigned int prev = (unsigned int) -1, state = 0;
250 /* Check pre-context */
251 for (unsigned int i = 0; i < buffer->context_len[0]; i++)
253 unsigned int this_type = get_joining_type (buffer->context[0][i], buffer->unicode->general_category (buffer->context[0][i]));
255 if (unlikely (this_type == JOINING_TYPE_T))
256 continue;
258 const arabic_state_table_entry *entry = &arabic_state_table[state][this_type];
259 state = entry->next_state;
260 break;
263 for (unsigned int i = 0; i < count; i++)
265 unsigned int this_type = get_joining_type (info[i].codepoint, _hb_glyph_info_get_general_category (&info[i]));
267 if (unlikely (this_type == JOINING_TYPE_T)) {
268 info[i].arabic_shaping_action() = NONE;
269 continue;
272 const arabic_state_table_entry *entry = &arabic_state_table[state][this_type];
274 if (entry->prev_action != NONE && prev != (unsigned int) -1)
275 info[prev].arabic_shaping_action() = entry->prev_action;
277 info[i].arabic_shaping_action() = entry->curr_action;
279 prev = i;
280 state = entry->next_state;
283 for (unsigned int i = 0; i < buffer->context_len[1]; i++)
285 unsigned int this_type = get_joining_type (buffer->context[1][i], buffer->unicode->general_category (buffer->context[1][i]));
287 if (unlikely (this_type == JOINING_TYPE_T))
288 continue;
290 const arabic_state_table_entry *entry = &arabic_state_table[state][this_type];
291 if (entry->prev_action != NONE && prev != (unsigned int) -1)
292 info[prev].arabic_shaping_action() = entry->prev_action;
293 break;
297 static void
298 mongolian_variation_selectors (hb_buffer_t *buffer)
300 /* Copy arabic_shaping_action() from base to Mongolian variation selectors. */
301 unsigned int count = buffer->len;
302 hb_glyph_info_t *info = buffer->info;
303 for (unsigned int i = 1; i < count; i++)
304 if (unlikely (hb_in_range (info[i].codepoint, 0x180Bu, 0x180Du)))
305 info[i].arabic_shaping_action() = info[i - 1].arabic_shaping_action();
308 static void
309 setup_masks_arabic (const hb_ot_shape_plan_t *plan,
310 hb_buffer_t *buffer,
311 hb_font_t *font HB_UNUSED)
313 HB_BUFFER_ALLOCATE_VAR (buffer, arabic_shaping_action);
315 const arabic_shape_plan_t *arabic_plan = (const arabic_shape_plan_t *) plan->data;
317 arabic_joining (buffer);
318 if (plan->props.script == HB_SCRIPT_MONGOLIAN)
319 mongolian_variation_selectors (buffer);
321 unsigned int count = buffer->len;
322 hb_glyph_info_t *info = buffer->info;
323 for (unsigned int i = 0; i < count; i++)
324 info[i].mask |= arabic_plan->mask_array[info[i].arabic_shaping_action()];
326 HB_BUFFER_DEALLOCATE_VAR (buffer, arabic_shaping_action);
330 static void
331 nuke_joiners (const hb_ot_shape_plan_t *plan HB_UNUSED,
332 hb_font_t *font HB_UNUSED,
333 hb_buffer_t *buffer)
335 unsigned int count = buffer->len;
336 hb_glyph_info_t *info = buffer->info;
337 for (unsigned int i = 0; i < count; i++)
338 if (_hb_glyph_info_is_zwj (&info[i]))
339 _hb_glyph_info_flip_joiners (&info[i]);
342 static void
343 arabic_fallback_shape (const hb_ot_shape_plan_t *plan,
344 hb_font_t *font,
345 hb_buffer_t *buffer)
347 const arabic_shape_plan_t *arabic_plan = (const arabic_shape_plan_t *) plan->data;
349 if (!arabic_plan->do_fallback)
350 return;
352 retry:
353 arabic_fallback_plan_t *fallback_plan = (arabic_fallback_plan_t *) hb_atomic_ptr_get (&arabic_plan->fallback_plan);
354 if (unlikely (!fallback_plan))
356 /* This sucks. We need a font to build the fallback plan... */
357 fallback_plan = arabic_fallback_plan_create (plan, font);
358 if (unlikely (!hb_atomic_ptr_cmpexch (&(const_cast<arabic_shape_plan_t *> (arabic_plan))->fallback_plan, NULL, fallback_plan))) {
359 arabic_fallback_plan_destroy (fallback_plan);
360 goto retry;
364 arabic_fallback_plan_shape (fallback_plan, font, buffer);
368 const hb_ot_complex_shaper_t _hb_ot_complex_shaper_arabic =
370 "arabic",
371 collect_features_arabic,
372 NULL, /* override_features */
373 data_create_arabic,
374 data_destroy_arabic,
375 NULL, /* preprocess_text_arabic */
376 HB_OT_SHAPE_NORMALIZATION_MODE_DEFAULT,
377 NULL, /* decompose */
378 NULL, /* compose */
379 setup_masks_arabic,
380 HB_OT_SHAPE_ZERO_WIDTH_MARKS_BY_GDEF_LATE,
381 true, /* fallback_position */