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