Home | History | Annotate | Download | only in src
      1 /*
      2  * Copyright  2009  Red Hat, Inc.
      3  * Copyright  2011  Codethink Limited
      4  * Copyright  2010,2011,2012  Google, Inc.
      5  *
      6  *  This is part of HarfBuzz, a text shaping library.
      7  *
      8  * Permission is hereby granted, without written agreement and without
      9  * license or royalty fees, to use, copy, modify, and distribute this
     10  * software and its documentation for any purpose, provided that the
     11  * above copyright notice and the following two paragraphs appear in
     12  * all copies of this software.
     13  *
     14  * IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE TO ANY PARTY FOR
     15  * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
     16  * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN
     17  * IF THE COPYRIGHT HOLDER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH
     18  * DAMAGE.
     19  *
     20  * THE COPYRIGHT HOLDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING,
     21  * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
     22  * FITNESS FOR A PARTICULAR PURPOSE.  THE SOFTWARE PROVIDED HEREUNDER IS
     23  * ON AN "AS IS" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO
     24  * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
     25  *
     26  * Red Hat Author(s): Behdad Esfahbod
     27  * Codethink Author(s): Ryan Lortie
     28  * Google Author(s): Behdad Esfahbod
     29  */
     30 
     31 #ifndef HB_UNICODE_PRIVATE_HH
     32 #define HB_UNICODE_PRIVATE_HH
     33 
     34 #include "hb-private.hh"
     35 #include "hb-object-private.hh"
     36 
     37 
     38 extern HB_INTERNAL const uint8_t _hb_modified_combining_class[256];
     39 
     40 /*
     41  * hb_unicode_funcs_t
     42  */
     43 
     44 #define HB_UNICODE_FUNCS_IMPLEMENT_CALLBACKS \
     45   HB_UNICODE_FUNC_IMPLEMENT (combining_class) \
     46   HB_UNICODE_FUNC_IMPLEMENT (eastasian_width) \
     47   HB_UNICODE_FUNC_IMPLEMENT (general_category) \
     48   HB_UNICODE_FUNC_IMPLEMENT (mirroring) \
     49   HB_UNICODE_FUNC_IMPLEMENT (script) \
     50   HB_UNICODE_FUNC_IMPLEMENT (compose) \
     51   HB_UNICODE_FUNC_IMPLEMENT (decompose) \
     52   HB_UNICODE_FUNC_IMPLEMENT (decompose_compatibility) \
     53   /* ^--- Add new callbacks here */
     54 
     55 /* Simple callbacks are those taking a hb_codepoint_t and returning a hb_codepoint_t */
     56 #define HB_UNICODE_FUNCS_IMPLEMENT_CALLBACKS_SIMPLE \
     57   HB_UNICODE_FUNC_IMPLEMENT (hb_unicode_combining_class_t, combining_class) \
     58   HB_UNICODE_FUNC_IMPLEMENT (unsigned int, eastasian_width) \
     59   HB_UNICODE_FUNC_IMPLEMENT (hb_unicode_general_category_t, general_category) \
     60   HB_UNICODE_FUNC_IMPLEMENT (hb_codepoint_t, mirroring) \
     61   HB_UNICODE_FUNC_IMPLEMENT (hb_script_t, script) \
     62   /* ^--- Add new simple callbacks here */
     63 
     64 struct hb_unicode_funcs_t {
     65   hb_object_header_t header;
     66   ASSERT_POD ();
     67 
     68   hb_unicode_funcs_t *parent;
     69 
     70   bool immutable;
     71 
     72 #define HB_UNICODE_FUNC_IMPLEMENT(return_type, name) \
     73   inline return_type name (hb_codepoint_t unicode) { return func.name (this, unicode, user_data.name); }
     74 HB_UNICODE_FUNCS_IMPLEMENT_CALLBACKS_SIMPLE
     75 #undef HB_UNICODE_FUNC_IMPLEMENT
     76 
     77   inline hb_bool_t compose (hb_codepoint_t a, hb_codepoint_t b,
     78 			    hb_codepoint_t *ab)
     79   {
     80     *ab = 0;
     81     if (unlikely (!a || !b)) return false;
     82     return func.compose (this, a, b, ab, user_data.compose);
     83   }
     84 
     85   inline hb_bool_t decompose (hb_codepoint_t ab,
     86 			      hb_codepoint_t *a, hb_codepoint_t *b)
     87   {
     88     *a = ab; *b = 0;
     89     return func.decompose (this, ab, a, b, user_data.decompose);
     90   }
     91 
     92   inline unsigned int decompose_compatibility (hb_codepoint_t  u,
     93 					       hb_codepoint_t *decomposed)
     94   {
     95     unsigned int ret = func.decompose_compatibility (this, u, decomposed, user_data.decompose_compatibility);
     96     if (ret == 1 && u == decomposed[0]) {
     97       decomposed[0] = 0;
     98       return 0;
     99     }
    100     decomposed[ret] = 0;
    101     return ret;
    102   }
    103 
    104 
    105   inline unsigned int
    106   modified_combining_class (hb_codepoint_t unicode)
    107   {
    108     /* XXX This hack belongs to the Myanmar shaper. */
    109     if (unlikely (unicode == 0x1037u)) unicode = 0x103Au;
    110 
    111     /* XXX This hack belongs to the SEA shaper (for Tai Tham):
    112      * Reorder SAKOT to ensure it comes after any tone marks. */
    113     if (unlikely (unicode == 0x1A60u)) return 254;
    114 
    115     /* XXX This hack belongs to the Tibetan shaper:
    116      * Reorder PADMA to ensure it comes after any vowel marks. */
    117     if (unlikely (unicode == 0x0FC6u)) return 254;
    118     /* Reorder TSA -PHRU to reorder before U+0F74 */
    119     if (unlikely (unicode == 0x0F39u)) return 127;
    120 
    121     return _hb_modified_combining_class[combining_class (unicode)];
    122   }
    123 
    124   static inline hb_bool_t
    125   is_variation_selector (hb_codepoint_t unicode)
    126   {
    127     /* U+180B..180D MONGOLIAN FREE VARIATION SELECTORs are handled in the
    128      * Arabic shaper.  No need to match them here. */
    129     return unlikely (hb_in_ranges (unicode,
    130 				   0xFE00u, 0xFE0Fu, /* VARIATION SELECTOR-1..16 */
    131 				   0xE0100u, 0xE01EFu));  /* VARIATION SELECTOR-17..256 */
    132   }
    133 
    134   /* Default_Ignorable codepoints:
    135    *
    136    * Note: While U+115F, U+1160, U+3164 and U+FFA0 are Default_Ignorable,
    137    * we do NOT want to hide them, as the way Uniscribe has implemented them
    138    * is with regular spacing glyphs, and that's the way fonts are made to work.
    139    * As such, we make exceptions for those four.
    140    *
    141    * Unicode 7.0:
    142    * $ grep '; Default_Ignorable_Code_Point ' DerivedCoreProperties.txt | sed 's/;.*#/#/'
    143    * 00AD          # Cf       SOFT HYPHEN
    144    * 034F          # Mn       COMBINING GRAPHEME JOINER
    145    * 061C          # Cf       ARABIC LETTER MARK
    146    * 115F..1160    # Lo   [2] HANGUL CHOSEONG FILLER..HANGUL JUNGSEONG FILLER
    147    * 17B4..17B5    # Mn   [2] KHMER VOWEL INHERENT AQ..KHMER VOWEL INHERENT AA
    148    * 180B..180D    # Mn   [3] MONGOLIAN FREE VARIATION SELECTOR ONE..MONGOLIAN FREE VARIATION SELECTOR THREE
    149    * 180E          # Cf       MONGOLIAN VOWEL SEPARATOR
    150    * 200B..200F    # Cf   [5] ZERO WIDTH SPACE..RIGHT-TO-LEFT MARK
    151    * 202A..202E    # Cf   [5] LEFT-TO-RIGHT EMBEDDING..RIGHT-TO-LEFT OVERRIDE
    152    * 2060..2064    # Cf   [5] WORD JOINER..INVISIBLE PLUS
    153    * 2065          # Cn       <reserved-2065>
    154    * 2066..206F    # Cf  [10] LEFT-TO-RIGHT ISOLATE..NOMINAL DIGIT SHAPES
    155    * 3164          # Lo       HANGUL FILLER
    156    * FE00..FE0F    # Mn  [16] VARIATION SELECTOR-1..VARIATION SELECTOR-16
    157    * FEFF          # Cf       ZERO WIDTH NO-BREAK SPACE
    158    * FFA0          # Lo       HALFWIDTH HANGUL FILLER
    159    * FFF0..FFF8    # Cn   [9] <reserved-FFF0>..<reserved-FFF8>
    160    * 1BCA0..1BCA3  # Cf   [4] SHORTHAND FORMAT LETTER OVERLAP..SHORTHAND FORMAT UP STEP
    161    * 1D173..1D17A  # Cf   [8] MUSICAL SYMBOL BEGIN BEAM..MUSICAL SYMBOL END PHRASE
    162    * E0000         # Cn       <reserved-E0000>
    163    * E0001         # Cf       LANGUAGE TAG
    164    * E0002..E001F  # Cn  [30] <reserved-E0002>..<reserved-E001F>
    165    * E0020..E007F  # Cf  [96] TAG SPACE..CANCEL TAG
    166    * E0080..E00FF  # Cn [128] <reserved-E0080>..<reserved-E00FF>
    167    * E0100..E01EF  # Mn [240] VARIATION SELECTOR-17..VARIATION SELECTOR-256
    168    * E01F0..E0FFF  # Cn [3600] <reserved-E01F0>..<reserved-E0FFF>
    169    */
    170   static inline hb_bool_t
    171   is_default_ignorable (hb_codepoint_t ch)
    172   {
    173     hb_codepoint_t plane = ch >> 16;
    174     if (likely (plane == 0))
    175     {
    176       /* BMP */
    177       hb_codepoint_t page = ch >> 8;
    178       switch (page) {
    179 	case 0x00: return unlikely (ch == 0x00ADu);
    180 	case 0x03: return unlikely (ch == 0x034Fu);
    181 	case 0x06: return unlikely (ch == 0x061Cu);
    182 	case 0x17: return hb_in_range (ch, 0x17B4u, 0x17B5u);
    183 	case 0x18: return hb_in_range (ch, 0x180Bu, 0x180Eu);
    184 	case 0x20: return hb_in_ranges (ch, 0x200Bu, 0x200Fu,
    185 					    0x202Au, 0x202Eu,
    186 					    0x2060u, 0x206Fu);
    187 	case 0xFE: return hb_in_range (ch, 0xFE00u, 0xFE0Fu) || ch == 0xFEFFu;
    188 	case 0xFF: return hb_in_range (ch, 0xFFF0u, 0xFFF8u);
    189 	default: return false;
    190       }
    191     }
    192     else
    193     {
    194       /* Other planes */
    195       switch (plane) {
    196 	case 0x01: return hb_in_ranges (ch, 0x1BCA0u, 0x1BCA3u,
    197 					    0x1D173u, 0x1D17Au);
    198 	case 0x0E: return hb_in_range (ch, 0xE0000u, 0xE0FFFu);
    199 	default: return false;
    200       }
    201     }
    202   }
    203 
    204   /* Space estimates based on:
    205    * http://www.unicode.org/charts/PDF/U2000.pdf
    206    * https://www.microsoft.com/typography/developers/fdsspec/spaces.aspx
    207    */
    208   enum space_t {
    209     NOT_SPACE = 0,
    210     SPACE_EM   = 1,
    211     SPACE_EM_2 = 2,
    212     SPACE_EM_3 = 3,
    213     SPACE_EM_4 = 4,
    214     SPACE_EM_5 = 5,
    215     SPACE_EM_6 = 6,
    216     SPACE_EM_16 = 16,
    217     SPACE_4_EM_18,	/* 4/18th of an EM! */
    218     SPACE,
    219     SPACE_FIGURE,
    220     SPACE_PUNCTUATION,
    221     SPACE_NARROW,
    222   };
    223   static inline space_t
    224   space_fallback_type (hb_codepoint_t u)
    225   {
    226     switch (u)
    227     {
    228       /* All GC=Zs chars that can use a fallback. */
    229       default:	    return NOT_SPACE;	/* U+1680 OGHAM SPACE MARK */
    230       case 0x0020u: return SPACE;	/* U+0020 SPACE */
    231       case 0x00A0u: return SPACE;	/* U+00A0 NO-BREAK SPACE */
    232       case 0x2000u: return SPACE_EM_2;	/* U+2000 EN QUAD */
    233       case 0x2001u: return SPACE_EM;	/* U+2001 EM QUAD */
    234       case 0x2002u: return SPACE_EM_2;	/* U+2002 EN SPACE */
    235       case 0x2003u: return SPACE_EM;	/* U+2003 EM SPACE */
    236       case 0x2004u: return SPACE_EM_3;	/* U+2004 THREE-PER-EM SPACE */
    237       case 0x2005u: return SPACE_EM_4;	/* U+2005 FOUR-PER-EM SPACE */
    238       case 0x2006u: return SPACE_EM_6;	/* U+2006 SIX-PER-EM SPACE */
    239       case 0x2007u: return SPACE_FIGURE;	/* U+2007 FIGURE SPACE */
    240       case 0x2008u: return SPACE_PUNCTUATION;	/* U+2008 PUNCTUATION SPACE */
    241       case 0x2009u: return SPACE_EM_5;		/* U+2009 THIN SPACE */
    242       case 0x200Au: return SPACE_EM_16;		/* U+200A HAIR SPACE */
    243       case 0x202Fu: return SPACE_NARROW;	/* U+202F NARROW NO-BREAK SPACE */
    244       case 0x205Fu: return SPACE_4_EM_18;	/* U+205F MEDIUM MATHEMATICAL SPACE */
    245       case 0x3000u: return SPACE_EM;		/* U+3000 IDEOGRAPHIC SPACE */
    246     }
    247   }
    248 
    249   struct {
    250 #define HB_UNICODE_FUNC_IMPLEMENT(name) hb_unicode_##name##_func_t name;
    251     HB_UNICODE_FUNCS_IMPLEMENT_CALLBACKS
    252 #undef HB_UNICODE_FUNC_IMPLEMENT
    253   } func;
    254 
    255   struct {
    256 #define HB_UNICODE_FUNC_IMPLEMENT(name) void *name;
    257     HB_UNICODE_FUNCS_IMPLEMENT_CALLBACKS
    258 #undef HB_UNICODE_FUNC_IMPLEMENT
    259   } user_data;
    260 
    261   struct {
    262 #define HB_UNICODE_FUNC_IMPLEMENT(name) hb_destroy_func_t name;
    263     HB_UNICODE_FUNCS_IMPLEMENT_CALLBACKS
    264 #undef HB_UNICODE_FUNC_IMPLEMENT
    265   } destroy;
    266 };
    267 
    268 
    269 extern HB_INTERNAL const hb_unicode_funcs_t _hb_unicode_funcs_nil;
    270 
    271 
    272 /* Modified combining marks */
    273 
    274 /* Hebrew
    275  *
    276  * We permute the "fixed-position" classes 10-26 into the order
    277  * described in the SBL Hebrew manual:
    278  *
    279  * http://www.sbl-site.org/Fonts/SBLHebrewUserManual1.5x.pdf
    280  *
    281  * (as recommended by:
    282  *  http://forum.fontlab.com/archive-old-microsoft-volt-group/vista-and-diacritic-ordering-t6751.0.html)
    283  *
    284  * More details here:
    285  * https://bugzilla.mozilla.org/show_bug.cgi?id=662055
    286  */
    287 #define HB_MODIFIED_COMBINING_CLASS_CCC10 22 /* sheva */
    288 #define HB_MODIFIED_COMBINING_CLASS_CCC11 15 /* hataf segol */
    289 #define HB_MODIFIED_COMBINING_CLASS_CCC12 16 /* hataf patah */
    290 #define HB_MODIFIED_COMBINING_CLASS_CCC13 17 /* hataf qamats */
    291 #define HB_MODIFIED_COMBINING_CLASS_CCC14 23 /* hiriq */
    292 #define HB_MODIFIED_COMBINING_CLASS_CCC15 18 /* tsere */
    293 #define HB_MODIFIED_COMBINING_CLASS_CCC16 19 /* segol */
    294 #define HB_MODIFIED_COMBINING_CLASS_CCC17 20 /* patah */
    295 #define HB_MODIFIED_COMBINING_CLASS_CCC18 21 /* qamats */
    296 #define HB_MODIFIED_COMBINING_CLASS_CCC19 14 /* holam */
    297 #define HB_MODIFIED_COMBINING_CLASS_CCC20 24 /* qubuts */
    298 #define HB_MODIFIED_COMBINING_CLASS_CCC21 12 /* dagesh */
    299 #define HB_MODIFIED_COMBINING_CLASS_CCC22 25 /* meteg */
    300 #define HB_MODIFIED_COMBINING_CLASS_CCC23 13 /* rafe */
    301 #define HB_MODIFIED_COMBINING_CLASS_CCC24 10 /* shin dot */
    302 #define HB_MODIFIED_COMBINING_CLASS_CCC25 11 /* sin dot */
    303 #define HB_MODIFIED_COMBINING_CLASS_CCC26 26 /* point varika */
    304 
    305 /*
    306  * Arabic
    307  *
    308  * Modify to move Shadda (ccc=33) before other marks.  See:
    309  * http://unicode.org/faq/normalization.html#8
    310  * http://unicode.org/faq/normalization.html#9
    311  */
    312 #define HB_MODIFIED_COMBINING_CLASS_CCC27 28 /* fathatan */
    313 #define HB_MODIFIED_COMBINING_CLASS_CCC28 29 /* dammatan */
    314 #define HB_MODIFIED_COMBINING_CLASS_CCC29 30 /* kasratan */
    315 #define HB_MODIFIED_COMBINING_CLASS_CCC30 31 /* fatha */
    316 #define HB_MODIFIED_COMBINING_CLASS_CCC31 32 /* damma */
    317 #define HB_MODIFIED_COMBINING_CLASS_CCC32 33 /* kasra */
    318 #define HB_MODIFIED_COMBINING_CLASS_CCC33 27 /* shadda */
    319 #define HB_MODIFIED_COMBINING_CLASS_CCC34 34 /* sukun */
    320 #define HB_MODIFIED_COMBINING_CLASS_CCC35 35 /* superscript alef */
    321 
    322 /* Syriac */
    323 #define HB_MODIFIED_COMBINING_CLASS_CCC36 36 /* superscript alaph */
    324 
    325 /* Telugu
    326  *
    327  * Modify Telugu length marks (ccc=84, ccc=91).
    328  * These are the only matras in the main Indic scripts range that have
    329  * a non-zero ccc.  That makes them reorder with the Halant that is
    330  * ccc=9.  Just zero them, we don't need them in our Indic shaper.
    331  */
    332 #define HB_MODIFIED_COMBINING_CLASS_CCC84 0 /* length mark */
    333 #define HB_MODIFIED_COMBINING_CLASS_CCC91 0 /* ai length mark */
    334 
    335 /* Thai
    336  *
    337  * Modify U+0E38 and U+0E39 (ccc=103) to be reordered before U+0E3A (ccc=9).
    338  * Assign 3, which is unassigned otherwise.
    339  * Uniscribe does this reordering too.
    340  */
    341 #define HB_MODIFIED_COMBINING_CLASS_CCC103 3 /* sara u / sara uu */
    342 #define HB_MODIFIED_COMBINING_CLASS_CCC107 107 /* mai * */
    343 
    344 /* Lao */
    345 #define HB_MODIFIED_COMBINING_CLASS_CCC118 118 /* sign u / sign uu */
    346 #define HB_MODIFIED_COMBINING_CLASS_CCC122 122 /* mai * */
    347 
    348 /* Tibetan
    349  * Modify U+0F74 (ccc=132) to reorder before ccc=130 marks.
    350  */
    351 #define HB_MODIFIED_COMBINING_CLASS_CCC129 129 /* sign aa */
    352 #define HB_MODIFIED_COMBINING_CLASS_CCC130 130 /* sign i */
    353 #define HB_MODIFIED_COMBINING_CLASS_CCC132 128 /* sign u */
    354 
    355 
    356 /* Misc */
    357 
    358 #define HB_UNICODE_GENERAL_CATEGORY_IS_MARK(gen_cat) \
    359 	(FLAG_SAFE (gen_cat) & \
    360 	 (FLAG (HB_UNICODE_GENERAL_CATEGORY_SPACING_MARK) | \
    361 	  FLAG (HB_UNICODE_GENERAL_CATEGORY_ENCLOSING_MARK) | \
    362 	  FLAG (HB_UNICODE_GENERAL_CATEGORY_NON_SPACING_MARK)))
    363 
    364 #define HB_UNICODE_GENERAL_CATEGORY_IS_NON_ENCLOSING_MARK_OR_MODIFIER_SYMBOL(gen_cat) \
    365 	(FLAG_SAFE (gen_cat) & \
    366 	 (FLAG (HB_UNICODE_GENERAL_CATEGORY_SPACING_MARK) | \
    367 	  FLAG (HB_UNICODE_GENERAL_CATEGORY_NON_SPACING_MARK) | \
    368 	  FLAG (HB_UNICODE_GENERAL_CATEGORY_MODIFIER_SYMBOL)))
    369 
    370 #endif /* HB_UNICODE_PRIVATE_HH */
    371