Home | History | Annotate | Download | only in src
      1 /*
      2  * Copyright  2010,2011,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 #ifndef HB_OT_SHAPE_COMPLEX_PRIVATE_HH
     28 #define HB_OT_SHAPE_COMPLEX_PRIVATE_HH
     29 
     30 #include "hb-private.hh"
     31 
     32 #include "hb-ot-shape-private.hh"
     33 #include "hb-ot-shape-normalize-private.hh"
     34 
     35 
     36 
     37 /* buffer var allocations, used by complex shapers */
     38 #define complex_var_u8_0()	var2.u8[2]
     39 #define complex_var_u8_1()	var2.u8[3]
     40 
     41 
     42 enum hb_ot_shape_zero_width_marks_type_t {
     43   HB_OT_SHAPE_ZERO_WIDTH_MARKS_NONE,
     44   HB_OT_SHAPE_ZERO_WIDTH_MARKS_BY_UNICODE,
     45   HB_OT_SHAPE_ZERO_WIDTH_MARKS_BY_GDEF
     46 };
     47 
     48 
     49 /* Master OT shaper list */
     50 #define HB_COMPLEX_SHAPERS_IMPLEMENT_SHAPERS \
     51   HB_COMPLEX_SHAPER_IMPLEMENT (default) /* should be first */ \
     52   HB_COMPLEX_SHAPER_IMPLEMENT (arabic) \
     53   HB_COMPLEX_SHAPER_IMPLEMENT (indic) \
     54   HB_COMPLEX_SHAPER_IMPLEMENT (myanmar) \
     55   HB_COMPLEX_SHAPER_IMPLEMENT (sea) \
     56   HB_COMPLEX_SHAPER_IMPLEMENT (thai) \
     57   /* ^--- Add new shapers here */
     58 
     59 
     60 struct hb_ot_complex_shaper_t
     61 {
     62   char name[8];
     63 
     64   /* collect_features()
     65    * Called during shape_plan().
     66    * Shapers should use plan->map to add their features and callbacks.
     67    * May be NULL.
     68    */
     69   void (*collect_features) (hb_ot_shape_planner_t *plan);
     70 
     71   /* override_features()
     72    * Called during shape_plan().
     73    * Shapers should use plan->map to override features and add callbacks after
     74    * common features are added.
     75    * May be NULL.
     76    */
     77   void (*override_features) (hb_ot_shape_planner_t *plan);
     78 
     79 
     80   /* data_create()
     81    * Called at the end of shape_plan().
     82    * Whatever shapers return will be accessible through plan->data later.
     83    * If NULL is returned, means a plan failure.
     84    */
     85   void *(*data_create) (const hb_ot_shape_plan_t *plan);
     86 
     87   /* data_destroy()
     88    * Called when the shape_plan is being destroyed.
     89    * plan->data is passed here for destruction.
     90    * If NULL is returned, means a plan failure.
     91    * May be NULL.
     92    */
     93   void (*data_destroy) (void *data);
     94 
     95 
     96   /* preprocess_text()
     97    * Called during shape().
     98    * Shapers can use to modify text before shaping starts.
     99    * May be NULL.
    100    */
    101   void (*preprocess_text) (const hb_ot_shape_plan_t *plan,
    102 			   hb_buffer_t              *buffer,
    103 			   hb_font_t                *font);
    104 
    105 
    106   /* normalization_preference()
    107    * Called during shape().
    108    * May be NULL.
    109    */
    110   hb_ot_shape_normalization_mode_t
    111   (*normalization_preference) (const hb_segment_properties_t *props);
    112 
    113   /* decompose()
    114    * Called during shape()'s normalization.
    115    * May be NULL.
    116    */
    117   bool (*decompose) (const hb_ot_shape_normalize_context_t *c,
    118 		     hb_codepoint_t  ab,
    119 		     hb_codepoint_t *a,
    120 		     hb_codepoint_t *b);
    121 
    122   /* compose()
    123    * Called during shape()'s normalization.
    124    * May be NULL.
    125    */
    126   bool (*compose) (const hb_ot_shape_normalize_context_t *c,
    127 		   hb_codepoint_t  a,
    128 		   hb_codepoint_t  b,
    129 		   hb_codepoint_t *ab);
    130 
    131   /* setup_masks()
    132    * Called during shape().
    133    * Shapers should use map to get feature masks and set on buffer.
    134    * Shapers may NOT modify characters.
    135    * May be NULL.
    136    */
    137   void (*setup_masks) (const hb_ot_shape_plan_t *plan,
    138 		       hb_buffer_t              *buffer,
    139 		       hb_font_t                *font);
    140 
    141   hb_ot_shape_zero_width_marks_type_t zero_width_marks;
    142 
    143   bool fallback_position;
    144 };
    145 
    146 #define HB_COMPLEX_SHAPER_IMPLEMENT(name) extern HB_INTERNAL const hb_ot_complex_shaper_t _hb_ot_complex_shaper_##name;
    147 HB_COMPLEX_SHAPERS_IMPLEMENT_SHAPERS
    148 #undef HB_COMPLEX_SHAPER_IMPLEMENT
    149 
    150 
    151 static inline const hb_ot_complex_shaper_t *
    152 hb_ot_shape_complex_categorize (const hb_ot_shape_planner_t *planner)
    153 {
    154   switch ((hb_tag_t) planner->props.script)
    155   {
    156     default:
    157       return &_hb_ot_complex_shaper_default;
    158 
    159 
    160     /* Unicode-1.1 additions */
    161     case HB_SCRIPT_ARABIC:
    162     case HB_SCRIPT_MONGOLIAN:
    163     case HB_SCRIPT_SYRIAC:
    164 
    165     /* Unicode-5.0 additions */
    166     case HB_SCRIPT_NKO:
    167     case HB_SCRIPT_PHAGS_PA:
    168 
    169     /* Unicode-6.0 additions */
    170     case HB_SCRIPT_MANDAIC:
    171 
    172       /* For Arabic script, use the Arabic shaper even if no OT script tag was found.
    173        * This is because we do fallback shaping for Arabic script (and not others). */
    174       if (planner->map.chosen_script[0] != HB_OT_TAG_DEFAULT_SCRIPT ||
    175 	  planner->props.script == HB_SCRIPT_ARABIC)
    176 	return &_hb_ot_complex_shaper_arabic;
    177       else
    178 	return &_hb_ot_complex_shaper_default;
    179 
    180 
    181     /* Unicode-1.1 additions */
    182     case HB_SCRIPT_THAI:
    183     case HB_SCRIPT_LAO:
    184 
    185       return &_hb_ot_complex_shaper_thai;
    186 
    187 
    188 
    189     /* ^--- Add new shapers here */
    190 
    191 
    192 #if 0
    193     /* Note:
    194      *
    195      * These disabled scripts are listed in ucd/IndicSyllabicCategory.txt, but according
    196      * to Martin Hosken and Jonathan Kew do not require complex shaping.
    197      *
    198      * TODO We should automate figuring out which scripts do not need complex shaping
    199      *
    200      * TODO We currently keep data for these scripts in our indic table.  Need to fix the
    201      * generator to not do that.
    202      */
    203 
    204 
    205     /* Simple? */
    206 
    207     /* Unicode-3.2 additions */
    208     case HB_SCRIPT_BUHID:
    209     case HB_SCRIPT_HANUNOO:
    210 
    211     /* Unicode-5.1 additions */
    212     case HB_SCRIPT_SAURASHTRA:
    213 
    214     /* Unicode-6.0 additions */
    215     case HB_SCRIPT_BATAK:
    216     case HB_SCRIPT_BRAHMI:
    217 
    218 
    219     /* Simple */
    220 
    221     /* Unicode-1.1 additions */
    222     /* These have their own shaper now. */
    223     case HB_SCRIPT_LAO:
    224     case HB_SCRIPT_THAI:
    225 
    226     /* Unicode-2.0 additions */
    227     case HB_SCRIPT_TIBETAN:
    228 
    229     /* Unicode-3.2 additions */
    230     case HB_SCRIPT_TAGALOG:
    231     case HB_SCRIPT_TAGBANWA:
    232 
    233     /* Unicode-4.0 additions */
    234     case HB_SCRIPT_LIMBU:
    235     case HB_SCRIPT_TAI_LE:
    236 
    237     /* Unicode-4.1 additions */
    238     case HB_SCRIPT_KHAROSHTHI:
    239     case HB_SCRIPT_SYLOTI_NAGRI:
    240 
    241     /* Unicode-5.1 additions */
    242     case HB_SCRIPT_KAYAH_LI:
    243 
    244     /* Unicode-5.2 additions */
    245     case HB_SCRIPT_TAI_VIET:
    246 
    247 
    248 #endif
    249 
    250     /* Unicode-1.1 additions */
    251     case HB_SCRIPT_BENGALI:
    252     case HB_SCRIPT_DEVANAGARI:
    253     case HB_SCRIPT_GUJARATI:
    254     case HB_SCRIPT_GURMUKHI:
    255     case HB_SCRIPT_KANNADA:
    256     case HB_SCRIPT_MALAYALAM:
    257     case HB_SCRIPT_ORIYA:
    258     case HB_SCRIPT_TAMIL:
    259     case HB_SCRIPT_TELUGU:
    260 
    261     /* Unicode-3.0 additions */
    262     case HB_SCRIPT_SINHALA:
    263 
    264     /* Unicode-4.1 additions */
    265     case HB_SCRIPT_BUGINESE:
    266 
    267     /* Unicode-5.0 additions */
    268     case HB_SCRIPT_BALINESE:
    269 
    270     /* Unicode-5.1 additions */
    271     case HB_SCRIPT_LEPCHA:
    272     case HB_SCRIPT_REJANG:
    273     case HB_SCRIPT_SUNDANESE:
    274 
    275     /* Unicode-5.2 additions */
    276     case HB_SCRIPT_JAVANESE:
    277     case HB_SCRIPT_KAITHI:
    278     case HB_SCRIPT_MEETEI_MAYEK:
    279 
    280     /* Unicode-6.0 additions */
    281 
    282     /* Unicode-6.1 additions */
    283     case HB_SCRIPT_CHAKMA:
    284     case HB_SCRIPT_SHARADA:
    285     case HB_SCRIPT_TAKRI:
    286 
    287       /* If the designer designed the font for the 'DFLT' script,
    288        * use the default shaper.  Otherwise, use the Indic shaper.
    289        * Note that for some simple scripts, there may not be *any*
    290        * GSUB/GPOS needed, so there may be no scripts found! */
    291       if (planner->map.chosen_script[0] == HB_TAG ('D','F','L','T'))
    292 	return &_hb_ot_complex_shaper_default;
    293       else
    294 	return &_hb_ot_complex_shaper_indic;
    295 
    296     case HB_SCRIPT_KHMER:
    297       /* A number of Khmer fonts in the wild don't have a 'pref' feature,
    298        * and as such won't shape properly via the Indic shaper;
    299        * however, they typically have 'liga' / 'clig' features that implement
    300        * the necessary "reordering" by means of ligature substitutions.
    301        * So we send such pref-less fonts through the generic shaper instead. */
    302       if (planner->map.found_script[0] &&
    303 	  hb_ot_layout_language_find_feature (planner->face, HB_OT_TAG_GSUB,
    304 					      planner->map.script_index[0],
    305 					      planner->map.language_index[0],
    306 					      HB_TAG ('p','r','e','f'),
    307 					      NULL))
    308 	return &_hb_ot_complex_shaper_indic;
    309       else
    310 	return &_hb_ot_complex_shaper_default;
    311 
    312     case HB_SCRIPT_MYANMAR:
    313       /* For Myanmar, we only want to use the Myanmar shaper if the "new" script
    314        * tag is found.  For "old" script tag we want to use the default shaper. */
    315       if (planner->map.chosen_script[0] == HB_TAG ('m','y','m','2'))
    316 	return &_hb_ot_complex_shaper_myanmar;
    317       else
    318 	return &_hb_ot_complex_shaper_default;
    319 
    320     /* Unicode-4.1 additions */
    321     case HB_SCRIPT_NEW_TAI_LUE:
    322 
    323     /* Unicode-5.1 additions */
    324     case HB_SCRIPT_CHAM:
    325 
    326     /* Unicode-5.2 additions */
    327     case HB_SCRIPT_TAI_THAM:
    328 
    329       /* If the designer designed the font for the 'DFLT' script,
    330        * use the default shaper.  Otherwise, use the Indic shaper.
    331        * Note that for some simple scripts, there may not be *any*
    332        * GSUB/GPOS needed, so there may be no scripts found! */
    333       if (planner->map.chosen_script[0] == HB_TAG ('D','F','L','T'))
    334 	return &_hb_ot_complex_shaper_default;
    335       else
    336 	return &_hb_ot_complex_shaper_sea;
    337   }
    338 }
    339 
    340 
    341 #endif /* HB_OT_SHAPE_COMPLEX_PRIVATE_HH */
    342