1 /*
   2  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   3  *
   4  * This code is free software; you can redistribute it and/or modify it
   5  * under the terms of the GNU General Public License version 2 only, as
   6  * published by the Free Software Foundation.  Oracle designates this
   7  * particular file as subject to the "Classpath" exception as provided
   8  * by Oracle in the LICENSE file that accompanied this code.
   9  *
  10  * This code is distributed in the hope that it will be useful, but WITHOUT
  11  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  12  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  13  * version 2 for more details (a copy is included in the LICENSE file that
  14  * accompanied this code).
  15  *
  16  * You should have received a copy of the GNU General Public License version
  17  * 2 along with this work; if not, write to the Free Software Foundation,
  18  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  19  *
  20  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  21  * or visit www.oracle.com if you need additional information or have any
  22  * questions.
  23  */
  24 
  25 // This file is available under and governed by the GNU General Public
  26 // License version 2 only, as published by the Free Software Foundation.
  27 // However, the following notice accompanied the original version of this
  28 // file:
  29 //
  30 /*
  31  * Copyright © 2010,2011,2012  Google, Inc.
  32  *
  33  *  This is part of HarfBuzz, a text shaping library.
  34  *
  35  * Permission is hereby granted, without written agreement and without
  36  * license or royalty fees, to use, copy, modify, and distribute this
  37  * software and its documentation for any purpose, provided that the
  38  * above copyright notice and the following two paragraphs appear in
  39  * all copies of this software.
  40  *
  41  * IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE TO ANY PARTY FOR
  42  * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
  43  * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN
  44  * IF THE COPYRIGHT HOLDER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH
  45  * DAMAGE.
  46  *
  47  * THE COPYRIGHT HOLDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING,
  48  * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
  49  * FITNESS FOR A PARTICULAR PURPOSE.  THE SOFTWARE PROVIDED HEREUNDER IS
  50  * ON AN "AS IS" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO
  51  * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
  52  *
  53  * Google Author(s): Behdad Esfahbod
  54  */
  55 
  56 #ifndef HB_OT_SHAPE_COMPLEX_PRIVATE_HH
  57 #define HB_OT_SHAPE_COMPLEX_PRIVATE_HH
  58 
  59 #include "hb-private.hh"
  60 
  61 #include "hb-ot-shape-private.hh"
  62 #include "hb-ot-shape-normalize-private.hh"
  63 
  64 
  65 
  66 /* buffer var allocations, used by complex shapers */
  67 #define complex_var_u8_0()      var2.u8[2]
  68 #define complex_var_u8_1()      var2.u8[3]
  69 
  70 
  71 enum hb_ot_shape_zero_width_marks_type_t {
  72   HB_OT_SHAPE_ZERO_WIDTH_MARKS_NONE,
  73 //  HB_OT_SHAPE_ZERO_WIDTH_MARKS_BY_UNICODE_EARLY,
  74   HB_OT_SHAPE_ZERO_WIDTH_MARKS_BY_UNICODE_LATE,
  75   HB_OT_SHAPE_ZERO_WIDTH_MARKS_BY_GDEF_EARLY,
  76   HB_OT_SHAPE_ZERO_WIDTH_MARKS_BY_GDEF_LATE,
  77 
  78   HB_OT_SHAPE_ZERO_WIDTH_MARKS_DEFAULT = HB_OT_SHAPE_ZERO_WIDTH_MARKS_BY_UNICODE_LATE
  79 };
  80 
  81 
  82 /* Master OT shaper list */
  83 #define HB_COMPLEX_SHAPERS_IMPLEMENT_SHAPERS \
  84   HB_COMPLEX_SHAPER_IMPLEMENT (default) /* should be first */ \
  85   HB_COMPLEX_SHAPER_IMPLEMENT (arabic) \
  86   HB_COMPLEX_SHAPER_IMPLEMENT (hangul) \
  87   HB_COMPLEX_SHAPER_IMPLEMENT (hebrew) \
  88   HB_COMPLEX_SHAPER_IMPLEMENT (myanmar_old) \
  89   HB_COMPLEX_SHAPER_IMPLEMENT (indic) \
  90   HB_COMPLEX_SHAPER_IMPLEMENT (myanmar) \
  91   HB_COMPLEX_SHAPER_IMPLEMENT (thai) \
  92   HB_COMPLEX_SHAPER_IMPLEMENT (tibetan) \
  93   HB_COMPLEX_SHAPER_IMPLEMENT (use) \
  94   /* ^--- Add new shapers here */
  95 
  96 
  97 struct hb_ot_complex_shaper_t
  98 {
  99   char name[8];
 100 
 101   /* collect_features()
 102    * Called during shape_plan().
 103    * Shapers should use plan->map to add their features and callbacks.
 104    * May be NULL.
 105    */
 106   void (*collect_features) (hb_ot_shape_planner_t *plan);
 107 
 108   /* override_features()
 109    * Called during shape_plan().
 110    * Shapers should use plan->map to override features and add callbacks after
 111    * common features are added.
 112    * May be NULL.
 113    */
 114   void (*override_features) (hb_ot_shape_planner_t *plan);
 115 
 116 
 117   /* data_create()
 118    * Called at the end of shape_plan().
 119    * Whatever shapers return will be accessible through plan->data later.
 120    * If NULL is returned, means a plan failure.
 121    */
 122   void *(*data_create) (const hb_ot_shape_plan_t *plan);
 123 
 124   /* data_destroy()
 125    * Called when the shape_plan is being destroyed.
 126    * plan->data is passed here for destruction.
 127    * If NULL is returned, means a plan failure.
 128    * May be NULL.
 129    */
 130   void (*data_destroy) (void *data);
 131 
 132 
 133   /* preprocess_text()
 134    * Called during shape().
 135    * Shapers can use to modify text before shaping starts.
 136    * May be NULL.
 137    */
 138   void (*preprocess_text) (const hb_ot_shape_plan_t *plan,
 139                            hb_buffer_t              *buffer,
 140                            hb_font_t                *font);
 141 
 142 
 143   hb_ot_shape_normalization_mode_t normalization_preference;
 144 
 145   /* decompose()
 146    * Called during shape()'s normalization.
 147    * May be NULL.
 148    */
 149   bool (*decompose) (const hb_ot_shape_normalize_context_t *c,
 150                      hb_codepoint_t  ab,
 151                      hb_codepoint_t *a,
 152                      hb_codepoint_t *b);
 153 
 154   /* compose()
 155    * Called during shape()'s normalization.
 156    * May be NULL.
 157    */
 158   bool (*compose) (const hb_ot_shape_normalize_context_t *c,
 159                    hb_codepoint_t  a,
 160                    hb_codepoint_t  b,
 161                    hb_codepoint_t *ab);
 162 
 163   /* setup_masks()
 164    * Called during shape().
 165    * Shapers should use map to get feature masks and set on buffer.
 166    * Shapers may NOT modify characters.
 167    * May be NULL.
 168    */
 169   void (*setup_masks) (const hb_ot_shape_plan_t *plan,
 170                        hb_buffer_t              *buffer,
 171                        hb_font_t                *font);
 172 
 173   hb_ot_shape_zero_width_marks_type_t zero_width_marks;
 174 
 175   bool fallback_position;
 176 };
 177 
 178 #define HB_COMPLEX_SHAPER_IMPLEMENT(name) extern HB_INTERNAL const hb_ot_complex_shaper_t _hb_ot_complex_shaper_##name;
 179 HB_COMPLEX_SHAPERS_IMPLEMENT_SHAPERS
 180 #undef HB_COMPLEX_SHAPER_IMPLEMENT
 181 
 182 
 183 static inline const hb_ot_complex_shaper_t *
 184 hb_ot_shape_complex_categorize (const hb_ot_shape_planner_t *planner)
 185 {
 186   switch ((hb_tag_t) planner->props.script)
 187   {
 188     default:
 189       return &_hb_ot_complex_shaper_default;
 190 
 191 
 192     /* Unicode-1.1 additions */
 193     case HB_SCRIPT_ARABIC:
 194 
 195     /* Unicode-3.0 additions */
 196     case HB_SCRIPT_MONGOLIAN:
 197     case HB_SCRIPT_SYRIAC:
 198 
 199     /* Unicode-5.0 additions */
 200     case HB_SCRIPT_NKO:
 201     case HB_SCRIPT_PHAGS_PA:
 202 
 203     /* Unicode-6.0 additions */
 204     case HB_SCRIPT_MANDAIC:
 205 
 206     /* Unicode-7.0 additions */
 207     case HB_SCRIPT_MANICHAEAN:
 208     case HB_SCRIPT_PSALTER_PAHLAVI:
 209 
 210       /* For Arabic script, use the Arabic shaper even if no OT script tag was found.
 211        * This is because we do fallback shaping for Arabic script (and not others).
 212        * But note that Arabic shaping is applicable only to horizontal layout; for
 213        * vertical text, just use the generic shaper instead. */
 214       if ((planner->map.chosen_script[0] != HB_OT_TAG_DEFAULT_SCRIPT ||
 215            planner->props.script == HB_SCRIPT_ARABIC) &&
 216           HB_DIRECTION_IS_HORIZONTAL(planner->props.direction))
 217         return &_hb_ot_complex_shaper_arabic;
 218       else
 219         return &_hb_ot_complex_shaper_default;
 220 
 221 
 222     /* Unicode-1.1 additions */
 223     case HB_SCRIPT_THAI:
 224     case HB_SCRIPT_LAO:
 225 
 226       return &_hb_ot_complex_shaper_thai;
 227 
 228 
 229     /* Unicode-1.1 additions */
 230     case HB_SCRIPT_HANGUL:
 231 
 232       return &_hb_ot_complex_shaper_hangul;
 233 
 234 
 235     /* Unicode-2.0 additions */
 236     case HB_SCRIPT_TIBETAN:
 237 
 238       return &_hb_ot_complex_shaper_tibetan;
 239 
 240 
 241     /* Unicode-1.1 additions */
 242     case HB_SCRIPT_HEBREW:
 243 
 244       return &_hb_ot_complex_shaper_hebrew;
 245 
 246 
 247     /* ^--- Add new shapers here */
 248 
 249 #if 0
 250     /* Unicode-4.1 additions */
 251     case HB_SCRIPT_NEW_TAI_LUE:
 252 #endif
 253 
 254     /* Unicode-1.1 additions */
 255     case HB_SCRIPT_BENGALI:
 256     case HB_SCRIPT_DEVANAGARI:
 257     case HB_SCRIPT_GUJARATI:
 258     case HB_SCRIPT_GURMUKHI:
 259     case HB_SCRIPT_KANNADA:
 260     case HB_SCRIPT_MALAYALAM:
 261     case HB_SCRIPT_ORIYA:
 262     case HB_SCRIPT_TAMIL:
 263     case HB_SCRIPT_TELUGU:
 264 
 265     /* Unicode-3.0 additions */
 266     case HB_SCRIPT_SINHALA:
 267 
 268     /* Unicode-5.2 additions */
 269     case HB_SCRIPT_JAVANESE:
 270 
 271       /* If the designer designed the font for the 'DFLT' script,
 272        * use the default shaper.  Otherwise, use the specific shaper.
 273        * Note that for some simple scripts, there may not be *any*
 274        * GSUB/GPOS needed, so there may be no scripts found! */
 275       if (planner->map.chosen_script[0] == HB_TAG ('D','F','L','T'))
 276         return &_hb_ot_complex_shaper_default;
 277       else
 278         return &_hb_ot_complex_shaper_indic;
 279 
 280     case HB_SCRIPT_KHMER:
 281       /* A number of Khmer fonts in the wild don't have a 'pref' feature,
 282        * and as such won't shape properly via the Indic shaper;
 283        * however, they typically have 'liga' / 'clig' features that implement
 284        * the necessary "reordering" by means of ligature substitutions.
 285        * So we send such pref-less fonts through the generic shaper instead. */
 286       if (planner->map.found_script[0] &&
 287           hb_ot_layout_language_find_feature (planner->face, HB_OT_TAG_GSUB,
 288                                               planner->map.script_index[0],
 289                                               planner->map.language_index[0],
 290                                               HB_TAG ('p','r','e','f'),
 291                                               NULL))
 292         return &_hb_ot_complex_shaper_indic;
 293       else
 294         return &_hb_ot_complex_shaper_default;
 295 
 296     case HB_SCRIPT_MYANMAR:
 297       if (planner->map.chosen_script[0] == HB_TAG ('m','y','m','2'))
 298         return &_hb_ot_complex_shaper_myanmar;
 299       else if (planner->map.chosen_script[0] == HB_TAG ('m','y','m','r'))
 300         return &_hb_ot_complex_shaper_myanmar_old;
 301       else
 302         return &_hb_ot_complex_shaper_default;
 303 
 304 
 305     /* Unicode-2.0 additions */
 306     //case HB_SCRIPT_TIBETAN:
 307 
 308     /* Unicode-3.0 additions */
 309     //case HB_SCRIPT_MONGOLIAN:
 310     //case HB_SCRIPT_SINHALA:
 311 
 312     /* Unicode-3.2 additions */
 313     case HB_SCRIPT_BUHID:
 314     case HB_SCRIPT_HANUNOO:
 315     case HB_SCRIPT_TAGALOG:
 316     case HB_SCRIPT_TAGBANWA:
 317 
 318     /* Unicode-4.0 additions */
 319     case HB_SCRIPT_LIMBU:
 320     case HB_SCRIPT_TAI_LE:
 321 
 322     /* Unicode-4.1 additions */
 323     case HB_SCRIPT_BUGINESE:
 324     case HB_SCRIPT_KHAROSHTHI:
 325     case HB_SCRIPT_SYLOTI_NAGRI:
 326     case HB_SCRIPT_TIFINAGH:
 327 
 328     /* Unicode-5.0 additions */
 329     case HB_SCRIPT_BALINESE:
 330     //case HB_SCRIPT_NKO:
 331     //case HB_SCRIPT_PHAGS_PA:
 332 
 333     /* Unicode-5.1 additions */
 334     case HB_SCRIPT_CHAM:
 335     case HB_SCRIPT_KAYAH_LI:
 336     case HB_SCRIPT_LEPCHA:
 337     case HB_SCRIPT_REJANG:
 338     case HB_SCRIPT_SAURASHTRA:
 339     case HB_SCRIPT_SUNDANESE:
 340 
 341     /* Unicode-5.2 additions */
 342     case HB_SCRIPT_EGYPTIAN_HIEROGLYPHS:
 343     //case HB_SCRIPT_JAVANESE:
 344     case HB_SCRIPT_KAITHI:
 345     case HB_SCRIPT_MEETEI_MAYEK:
 346     case HB_SCRIPT_TAI_THAM:
 347     case HB_SCRIPT_TAI_VIET:
 348 
 349     /* Unicode-6.0 additions */
 350     case HB_SCRIPT_BATAK:
 351     case HB_SCRIPT_BRAHMI:
 352     //case HB_SCRIPT_MANDAIC:
 353 
 354     /* Unicode-6.1 additions */
 355     case HB_SCRIPT_CHAKMA:
 356     case HB_SCRIPT_SHARADA:
 357     case HB_SCRIPT_TAKRI:
 358 
 359     /* Unicode-7.0 additions */
 360     case HB_SCRIPT_DUPLOYAN:
 361     case HB_SCRIPT_GRANTHA:
 362     case HB_SCRIPT_KHOJKI:
 363     case HB_SCRIPT_KHUDAWADI:
 364     case HB_SCRIPT_MAHAJANI:
 365     //case HB_SCRIPT_MANICHAEAN:
 366     case HB_SCRIPT_MODI:
 367     case HB_SCRIPT_PAHAWH_HMONG:
 368     //case HB_SCRIPT_PSALTER_PAHLAVI:
 369     case HB_SCRIPT_SIDDHAM:
 370     case HB_SCRIPT_TIRHUTA:
 371 
 372       /* If the designer designed the font for the 'DFLT' script,
 373        * use the default shaper.  Otherwise, use the specific shaper.
 374        * Note that for some simple scripts, there may not be *any*
 375        * GSUB/GPOS needed, so there may be no scripts found! */
 376       if (planner->map.chosen_script[0] == HB_TAG ('D','F','L','T'))
 377         return &_hb_ot_complex_shaper_default;
 378       else
 379         return &_hb_ot_complex_shaper_use;
 380   }
 381 }
 382 
 383 
 384 #endif /* HB_OT_SHAPE_COMPLEX_PRIVATE_HH */