1 /*
   2  * Copyright © 2010,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 
  27 #include "hb-ot-shape-complex-arabic-private.hh"
  28 #include "hb-ot-shape-private.hh"
  29 
  30 
  31 /* buffer var allocations */
  32 #define arabic_shaping_action() complex_var_u8_0() /* arabic shaping action */
  33 
  34 
  35 /*
  36  * Joining types:
  37  */
  38 
  39 /*
  40  * Bits used in the joining tables
  41  */
  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,
  51 
  52   JOINING_TYPE_T = 7,
  53   JOINING_TYPE_X = 8  /* means: use general-category to choose between U or T. */
  54 };
  55 
  56 #include "hb-ot-shape-complex-arabic-table.hh"
  57 
  58 static unsigned int get_joining_type (hb_codepoint_t u, hb_unicode_general_category_t gen_cat)
  59 {
  60   unsigned int j_type = joining_type(u);
  61   if (likely (j_type != JOINING_TYPE_X))
  62     return j_type;
  63 
  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;
  69 }
  70 
  71 #define FEATURE_IS_SYRIAC(tag) hb_in_range<unsigned char> ((unsigned char) (tag), '2', '3')
  72 
  73 static const hb_tag_t arabic_features[] =
  74 {
  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
  83 };
  84 
  85 
  86 /* Same order as the feature array */
  87 enum {
  88   ISOL,
  89   FINA,
  90   FIN2,
  91   FIN3,
  92   MEDI,
  93   MED2,
  94   INIT,
  95 
  96   NONE,
  97 
  98   ARABIC_NUM_FEATURES = NONE
  99 };
 100 
 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] =
 106 {
 107   /*   jt_U,          jt_L,          jt_R,          jt_D,          jg_ALAPH,      jg_DALATH_RISH */
 108 
 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}, },
 111 
 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}, },
 114 
 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}, },
 117 
 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}, },
 120 
 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}, },
 123 
 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}, },
 126 
 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}, }
 129 };
 130 
 131 
 132 static void
 133 nuke_joiners (const hb_ot_shape_plan_t *plan,
 134               hb_font_t *font,
 135               hb_buffer_t *buffer);
 136 
 137 static void
 138 arabic_fallback_shape (const hb_ot_shape_plan_t *plan,
 139                        hb_font_t *font,
 140                        hb_buffer_t *buffer);
 141 
 142 static void
 143 collect_features_arabic (hb_ot_shape_planner_t *plan)
 144 {
 145   hb_ot_map_builder_t *map = &plan->map;
 146 
 147   /* We apply features according to the Arabic spec, with pauses
 148    * in between most.
 149    *
 150    * The pause between init/medi/... and rlig is required.  See eg:
 151    * https://bugzilla.mozilla.org/show_bug.cgi?id=644184
 152    *
 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.
 158    *
 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.
 164    */
 165 
 166   map->add_gsub_pause (nuke_joiners);
 167 
 168   map->add_global_bool_feature (HB_TAG('c','c','m','p'));
 169   map->add_global_bool_feature (HB_TAG('l','o','c','l'));
 170 
 171   map->add_gsub_pause (NULL);
 172 
 173   for (unsigned int i = 0; i < ARABIC_NUM_FEATURES; i++)
 174   {
 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);
 178   }
 179 
 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);
 183 
 184   map->add_global_bool_feature (HB_TAG('c','a','l','t'));
 185   map->add_gsub_pause (NULL);
 186 
 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'));
 197 }
 198 
 199 #include "hb-ot-shape-complex-arabic-fallback.hh"
 200 
 201 struct arabic_shape_plan_t
 202 {
 203   ASSERT_POD ();
 204 
 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];
 210 
 211   bool do_fallback;
 212   arabic_fallback_plan_t *fallback_plan;
 213 };
 214 
 215 void *
 216 data_create_arabic (const hb_ot_shape_plan_t *plan)
 217 {
 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;
 221 
 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]));
 228   }
 229 
 230   return arabic_plan;
 231 }
 232 
 233 void
 234 data_destroy_arabic (void *data)
 235 {
 236   arabic_shape_plan_t *arabic_plan = (arabic_shape_plan_t *) data;
 237 
 238   arabic_fallback_plan_destroy (arabic_plan->fallback_plan);
 239 
 240   free (data);
 241 }
 242 
 243 static void
 244 arabic_joining (hb_buffer_t *buffer)
 245 {
 246   unsigned int count = buffer->len;
 247   hb_glyph_info_t *info = buffer->info;
 248   unsigned int prev = (unsigned int) -1, state = 0;
 249 
 250   /* Check pre-context */
 251   for (unsigned int i = 0; i < buffer->context_len[0]; i++)
 252   {
 253     unsigned int this_type = get_joining_type (buffer->context[0][i], buffer->unicode->general_category (buffer->context[0][i]));
 254 
 255     if (unlikely (this_type == JOINING_TYPE_T))
 256       continue;
 257 
 258     const arabic_state_table_entry *entry = &arabic_state_table[state][this_type];
 259     state = entry->next_state;
 260     break;
 261   }
 262 
 263   for (unsigned int i = 0; i < count; i++)
 264   {
 265     unsigned int this_type = get_joining_type (info[i].codepoint, _hb_glyph_info_get_general_category (&info[i]));
 266 
 267     if (unlikely (this_type == JOINING_TYPE_T)) {
 268       info[i].arabic_shaping_action() = NONE;
 269       continue;
 270     }
 271 
 272     const arabic_state_table_entry *entry = &arabic_state_table[state][this_type];
 273 
 274     if (entry->prev_action != NONE && prev != (unsigned int) -1)
 275       info[prev].arabic_shaping_action() = entry->prev_action;
 276 
 277     info[i].arabic_shaping_action() = entry->curr_action;
 278 
 279     prev = i;
 280     state = entry->next_state;
 281   }
 282 
 283   for (unsigned int i = 0; i < buffer->context_len[1]; i++)
 284   {
 285     unsigned int this_type = get_joining_type (buffer->context[1][i], buffer->unicode->general_category (buffer->context[1][i]));
 286 
 287     if (unlikely (this_type == JOINING_TYPE_T))
 288       continue;
 289 
 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;
 294   }
 295 }
 296 
 297 static void
 298 mongolian_variation_selectors (hb_buffer_t *buffer)
 299 {
 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();
 306 }
 307 
 308 void
 309 setup_masks_arabic_plan (const arabic_shape_plan_t *arabic_plan,
 310                          hb_buffer_t               *buffer,
 311                          hb_script_t                script)
 312 {
 313   HB_BUFFER_ALLOCATE_VAR (buffer, arabic_shaping_action);
 314 
 315   arabic_joining (buffer);
 316   if (script == HB_SCRIPT_MONGOLIAN)
 317     mongolian_variation_selectors (buffer);
 318 
 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()];
 323 
 324   HB_BUFFER_DEALLOCATE_VAR (buffer, arabic_shaping_action);
 325 }
 326 
 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)
 331 {
 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);
 334 }
 335 
 336 
 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)
 341 {
 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]);
 347 }
 348 
 349 static void
 350 arabic_fallback_shape (const hb_ot_shape_plan_t *plan,
 351                        hb_font_t *font,
 352                        hb_buffer_t *buffer)
 353 {
 354   const arabic_shape_plan_t *arabic_plan = (const arabic_shape_plan_t *) plan->data;
 355 
 356   if (!arabic_plan->do_fallback)
 357     return;
 358 
 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))
 362   {
 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;
 368     }
 369   }
 370 
 371   arabic_fallback_plan_shape (fallback_plan, font, buffer);
 372 }
 373 
 374 
 375 const hb_ot_complex_shaper_t _hb_ot_complex_shaper_arabic =
 376 {
 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 */
 389 };