Add a Notification Settings Button to all web notifications behind the web platform...
[chromium-blink-merge.git] / third_party / harfbuzz-ng / src / hb-ot-shape-complex-arabic.cc
blobcde02e0a567f2b9e6ad2839ffb6159668accce48
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-arabic-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 * Joining types:
40 * Bits used in the joining tables
42 enum hb_arabic_joining_type_t {
43 JOINING_TYPE_U = 0,
44 JOINING_TYPE_L = 1,
45 JOINING_TYPE_R = 2,
46 JOINING_TYPE_D = 3,
47 JOINING_TYPE_C = JOINING_TYPE_D,
48 JOINING_GROUP_ALAPH = 4,
49 JOINING_GROUP_DALATH_RISH = 5,
50 NUM_STATE_MACHINE_COLS = 6,
52 JOINING_TYPE_T = 7,
53 JOINING_TYPE_X = 8 /* means: use general-category to choose between U or T. */
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_SAFE(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 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 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 void
309 setup_masks_arabic_plan (const arabic_shape_plan_t *arabic_plan,
310 hb_buffer_t *buffer,
311 hb_script_t script)
313 HB_BUFFER_ALLOCATE_VAR (buffer, arabic_shaping_action);
315 arabic_joining (buffer);
316 if (script == HB_SCRIPT_MONGOLIAN)
317 mongolian_variation_selectors (buffer);
319 unsigned int count = buffer->len;
320 hb_glyph_info_t *info = buffer->info;
321 for (unsigned int i = 0; i < count; i++)
322 info[i].mask |= arabic_plan->mask_array[info[i].arabic_shaping_action()];
324 HB_BUFFER_DEALLOCATE_VAR (buffer, arabic_shaping_action);
327 static void
328 setup_masks_arabic (const hb_ot_shape_plan_t *plan,
329 hb_buffer_t *buffer,
330 hb_font_t *font HB_UNUSED)
332 const arabic_shape_plan_t *arabic_plan = (const arabic_shape_plan_t *) plan->data;
333 setup_masks_arabic_plan (arabic_plan, buffer, plan->props.script);
337 static void
338 nuke_joiners (const hb_ot_shape_plan_t *plan HB_UNUSED,
339 hb_font_t *font HB_UNUSED,
340 hb_buffer_t *buffer)
342 unsigned int count = buffer->len;
343 hb_glyph_info_t *info = buffer->info;
344 for (unsigned int i = 0; i < count; i++)
345 if (_hb_glyph_info_is_zwj (&info[i]))
346 _hb_glyph_info_flip_joiners (&info[i]);
349 static void
350 arabic_fallback_shape (const hb_ot_shape_plan_t *plan,
351 hb_font_t *font,
352 hb_buffer_t *buffer)
354 const arabic_shape_plan_t *arabic_plan = (const arabic_shape_plan_t *) plan->data;
356 if (!arabic_plan->do_fallback)
357 return;
359 retry:
360 arabic_fallback_plan_t *fallback_plan = (arabic_fallback_plan_t *) hb_atomic_ptr_get (&arabic_plan->fallback_plan);
361 if (unlikely (!fallback_plan))
363 /* This sucks. We need a font to build the fallback plan... */
364 fallback_plan = arabic_fallback_plan_create (plan, font);
365 if (unlikely (!hb_atomic_ptr_cmpexch (&(const_cast<arabic_shape_plan_t *> (arabic_plan))->fallback_plan, NULL, fallback_plan))) {
366 arabic_fallback_plan_destroy (fallback_plan);
367 goto retry;
371 arabic_fallback_plan_shape (fallback_plan, font, buffer);
375 const hb_ot_complex_shaper_t _hb_ot_complex_shaper_arabic =
377 "arabic",
378 collect_features_arabic,
379 NULL, /* override_features */
380 data_create_arabic,
381 data_destroy_arabic,
382 NULL, /* preprocess_text_arabic */
383 HB_OT_SHAPE_NORMALIZATION_MODE_DEFAULT,
384 NULL, /* decompose */
385 NULL, /* compose */
386 setup_masks_arabic,
387 HB_OT_SHAPE_ZERO_WIDTH_MARKS_BY_GDEF_LATE,
388 true, /* fallback_position */