hb-unicode.hh 14.0 KB
Newer Older
B
Behdad Esfahbod 已提交
1
/*
B
Behdad Esfahbod 已提交
2
 * Copyright © 2009  Red Hat, Inc.
3
 * Copyright © 2011  Codethink Limited
4
 * Copyright © 2010,2011,2012  Google, Inc.
B
Behdad Esfahbod 已提交
5
 *
B
Behdad Esfahbod 已提交
6
 *  This is part of HarfBuzz, a text shaping library.
B
Behdad Esfahbod 已提交
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
 *
 * Permission is hereby granted, without written agreement and without
 * license or royalty fees, to use, copy, modify, and distribute this
 * software and its documentation for any purpose, provided that the
 * above copyright notice and the following two paragraphs appear in
 * all copies of this software.
 *
 * IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE TO ANY PARTY FOR
 * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
 * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN
 * IF THE COPYRIGHT HOLDER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH
 * DAMAGE.
 *
 * THE COPYRIGHT HOLDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING,
 * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
 * FITNESS FOR A PARTICULAR PURPOSE.  THE SOFTWARE PROVIDED HEREUNDER IS
 * ON AN "AS IS" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO
 * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
 *
 * Red Hat Author(s): Behdad Esfahbod
27
 * Codethink Author(s): Ryan Lortie
28
 * Google Author(s): Behdad Esfahbod
B
Behdad Esfahbod 已提交
29 30
 */

31 32
#ifndef HB_UNICODE_HH
#define HB_UNICODE_HH
B
Behdad Esfahbod 已提交
33

34
#include "hb.hh"
B
Behdad Esfahbod 已提交
35 36


37
extern HB_INTERNAL const uint8_t _hb_modified_combining_class[256];
B
Behdad Esfahbod 已提交
38

B
Behdad Esfahbod 已提交
39 40 41 42
/*
 * hb_unicode_funcs_t
 */

B
Behdad Esfahbod 已提交
43
#define HB_UNICODE_FUNCS_IMPLEMENT_CALLBACKS \
B
Behdad Esfahbod 已提交
44
  HB_UNICODE_FUNC_IMPLEMENT (combining_class) \
45
  HB_IF_NOT_DEPRECATED (HB_UNICODE_FUNC_IMPLEMENT (eastasian_width)) \
B
Behdad Esfahbod 已提交
46 47 48
  HB_UNICODE_FUNC_IMPLEMENT (general_category) \
  HB_UNICODE_FUNC_IMPLEMENT (mirroring) \
  HB_UNICODE_FUNC_IMPLEMENT (script) \
49 50
  HB_UNICODE_FUNC_IMPLEMENT (compose) \
  HB_UNICODE_FUNC_IMPLEMENT (decompose) \
51
  HB_IF_NOT_DEPRECATED (HB_UNICODE_FUNC_IMPLEMENT (decompose_compatibility)) \
B
Behdad Esfahbod 已提交
52 53
  /* ^--- Add new callbacks here */

B
Behdad Esfahbod 已提交
54 55
/* Simple callbacks are those taking a hb_codepoint_t and returning a hb_codepoint_t */
#define HB_UNICODE_FUNCS_IMPLEMENT_CALLBACKS_SIMPLE \
56
  HB_UNICODE_FUNC_IMPLEMENT (hb_unicode_combining_class_t, combining_class) \
57
  HB_IF_NOT_DEPRECATED (HB_UNICODE_FUNC_IMPLEMENT (unsigned int, eastasian_width)) \
B
Behdad Esfahbod 已提交
58 59 60 61 62
  HB_UNICODE_FUNC_IMPLEMENT (hb_unicode_general_category_t, general_category) \
  HB_UNICODE_FUNC_IMPLEMENT (hb_codepoint_t, mirroring) \
  HB_UNICODE_FUNC_IMPLEMENT (hb_script_t, script) \
  /* ^--- Add new simple callbacks here */

63 64
struct hb_unicode_funcs_t
{
65 66
  hb_object_header_t header;

67
  hb_unicode_funcs_t *parent;
B
Behdad Esfahbod 已提交
68

B
Behdad Esfahbod 已提交
69
#define HB_UNICODE_FUNC_IMPLEMENT(return_type, name) \
70
  return_type name (hb_codepoint_t unicode) { return func.name (this, unicode, user_data.name); }
B
Behdad Esfahbod 已提交
71 72 73
HB_UNICODE_FUNCS_IMPLEMENT_CALLBACKS_SIMPLE
#undef HB_UNICODE_FUNC_IMPLEMENT

74 75
  hb_bool_t compose (hb_codepoint_t a, hb_codepoint_t b,
		     hb_codepoint_t *ab)
B
Behdad Esfahbod 已提交
76 77
  {
    *ab = 0;
78
    if (unlikely (!a || !b)) return false;
B
Behdad Esfahbod 已提交
79
    return func.compose (this, a, b, ab, user_data.compose);
B
Behdad Esfahbod 已提交
80 81
  }

82 83
  hb_bool_t decompose (hb_codepoint_t ab,
		       hb_codepoint_t *a, hb_codepoint_t *b)
B
Behdad Esfahbod 已提交
84
  {
85
    *a = ab; *b = 0;
B
Behdad Esfahbod 已提交
86 87 88
    return func.decompose (this, ab, a, b, user_data.decompose);
  }

89 90
  unsigned int decompose_compatibility (hb_codepoint_t  u,
					hb_codepoint_t *decomposed)
B
Behdad Esfahbod 已提交
91
  {
92 93 94
#ifdef HB_DISABLE_DEPRECATED
    unsigned int ret  = 0;
#else
B
Behdad Esfahbod 已提交
95
    unsigned int ret = func.decompose_compatibility (this, u, decomposed, user_data.decompose_compatibility);
96
#endif
B
Behdad Esfahbod 已提交
97 98 99 100 101 102 103 104
    if (ret == 1 && u == decomposed[0]) {
      decomposed[0] = 0;
      return 0;
    }
    decomposed[ret] = 0;
    return ret;
  }

105
  unsigned int
106
  modified_combining_class (hb_codepoint_t u)
107
  {
B
Minor  
Behdad Esfahbod 已提交
108
    /* XXX This hack belongs to the USE shaper (for Tai Tham):
109
     * Reorder SAKOT to ensure it comes after any tone marks. */
110
    if (unlikely (u == 0x1A60u)) return 254;
111

112 113
    /* XXX This hack belongs to the Tibetan shaper:
     * Reorder PADMA to ensure it comes after any vowel marks. */
114
    if (unlikely (u == 0x0FC6u)) return 254;
115
    /* Reorder TSA -PHRU to reorder before U+0F74 */
116
    if (unlikely (u == 0x0F39u)) return 127;
117

118
    return _hb_modified_combining_class[combining_class (u)];
119
  }
120

121
  static hb_bool_t
122 123
  is_variation_selector (hb_codepoint_t unicode)
  {
124 125
    /* U+180B..180D MONGOLIAN FREE VARIATION SELECTORs are handled in the
     * Arabic shaper.  No need to match them here. */
126
    return unlikely (hb_in_ranges<hb_codepoint_t> (unicode,
127 128
						   0xFE00u, 0xFE0Fu, /* VARIATION SELECTOR-1..16 */
						   0xE0100u, 0xE01EFu));  /* VARIATION SELECTOR-17..256 */
129 130
  }

131
  /* Default_Ignorable codepoints:
132
   *
133 134 135 136
   * Note: While U+115F, U+1160, U+3164 and U+FFA0 are Default_Ignorable,
   * we do NOT want to hide them, as the way Uniscribe has implemented them
   * is with regular spacing glyphs, and that's the way fonts are made to work.
   * As such, we make exceptions for those four.
137
   * Also ignoring U+1BCA0..1BCA3. https://github.com/harfbuzz/harfbuzz/issues/503
138
   *
139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166
   * Unicode 7.0:
   * $ grep '; Default_Ignorable_Code_Point ' DerivedCoreProperties.txt | sed 's/;.*#/#/'
   * 00AD          # Cf       SOFT HYPHEN
   * 034F          # Mn       COMBINING GRAPHEME JOINER
   * 061C          # Cf       ARABIC LETTER MARK
   * 115F..1160    # Lo   [2] HANGUL CHOSEONG FILLER..HANGUL JUNGSEONG FILLER
   * 17B4..17B5    # Mn   [2] KHMER VOWEL INHERENT AQ..KHMER VOWEL INHERENT AA
   * 180B..180D    # Mn   [3] MONGOLIAN FREE VARIATION SELECTOR ONE..MONGOLIAN FREE VARIATION SELECTOR THREE
   * 180E          # Cf       MONGOLIAN VOWEL SEPARATOR
   * 200B..200F    # Cf   [5] ZERO WIDTH SPACE..RIGHT-TO-LEFT MARK
   * 202A..202E    # Cf   [5] LEFT-TO-RIGHT EMBEDDING..RIGHT-TO-LEFT OVERRIDE
   * 2060..2064    # Cf   [5] WORD JOINER..INVISIBLE PLUS
   * 2065          # Cn       <reserved-2065>
   * 2066..206F    # Cf  [10] LEFT-TO-RIGHT ISOLATE..NOMINAL DIGIT SHAPES
   * 3164          # Lo       HANGUL FILLER
   * FE00..FE0F    # Mn  [16] VARIATION SELECTOR-1..VARIATION SELECTOR-16
   * FEFF          # Cf       ZERO WIDTH NO-BREAK SPACE
   * FFA0          # Lo       HALFWIDTH HANGUL FILLER
   * FFF0..FFF8    # Cn   [9] <reserved-FFF0>..<reserved-FFF8>
   * 1BCA0..1BCA3  # Cf   [4] SHORTHAND FORMAT LETTER OVERLAP..SHORTHAND FORMAT UP STEP
   * 1D173..1D17A  # Cf   [8] MUSICAL SYMBOL BEGIN BEAM..MUSICAL SYMBOL END PHRASE
   * E0000         # Cn       <reserved-E0000>
   * E0001         # Cf       LANGUAGE TAG
   * E0002..E001F  # Cn  [30] <reserved-E0002>..<reserved-E001F>
   * E0020..E007F  # Cf  [96] TAG SPACE..CANCEL TAG
   * E0080..E00FF  # Cn [128] <reserved-E0080>..<reserved-E00FF>
   * E0100..E01EF  # Mn [240] VARIATION SELECTOR-17..VARIATION SELECTOR-256
   * E01F0..E0FFF  # Cn [3600] <reserved-E01F0>..<reserved-E0FFF>
167
   */
168
  static hb_bool_t
169
  is_default_ignorable (hb_codepoint_t ch)
170
  {
171 172 173 174 175 176
    hb_codepoint_t plane = ch >> 16;
    if (likely (plane == 0))
    {
      /* BMP */
      hb_codepoint_t page = ch >> 8;
      switch (page) {
177 178 179
	case 0x00: return unlikely (ch == 0x00ADu);
	case 0x03: return unlikely (ch == 0x034Fu);
	case 0x06: return unlikely (ch == 0x061Cu);
180 181 182
	case 0x17: return hb_in_range<hb_codepoint_t> (ch, 0x17B4u, 0x17B5u);
	case 0x18: return hb_in_range<hb_codepoint_t> (ch, 0x180Bu, 0x180Eu);
	case 0x20: return hb_in_ranges<hb_codepoint_t> (ch, 0x200Bu, 0x200Fu,
B
Behdad Esfahbod 已提交
183 184
					    0x202Au, 0x202Eu,
					    0x2060u, 0x206Fu);
185 186
	case 0xFE: return hb_in_range<hb_codepoint_t> (ch, 0xFE00u, 0xFE0Fu) || ch == 0xFEFFu;
	case 0xFF: return hb_in_range<hb_codepoint_t> (ch, 0xFFF0u, 0xFFF8u);
187 188 189 190 191 192 193
	default: return false;
      }
    }
    else
    {
      /* Other planes */
      switch (plane) {
194
	case 0x01: return hb_in_range<hb_codepoint_t> (ch, 0x1D173u, 0x1D17Au);
195
	case 0x0E: return hb_in_range<hb_codepoint_t> (ch, 0xE0000u, 0xE0FFFu);
196 197 198
	default: return false;
      }
    }
199 200
  }

201
  /* Space estimates based on:
202 203
   * https://unicode.org/charts/PDF/U2000.pdf
   * https://docs.microsoft.com/en-us/typography/develop/character-design-standards/whitespace
204
   */
205 206
  enum space_t {
    NOT_SPACE = 0,
207 208 209 210 211 212 213 214 215
    SPACE_EM   = 1,
    SPACE_EM_2 = 2,
    SPACE_EM_3 = 3,
    SPACE_EM_4 = 4,
    SPACE_EM_5 = 5,
    SPACE_EM_6 = 6,
    SPACE_EM_16 = 16,
    SPACE_4_EM_18,	/* 4/18th of an EM! */
    SPACE,
216 217 218 219
    SPACE_FIGURE,
    SPACE_PUNCTUATION,
    SPACE_NARROW,
  };
220
  static space_t
221 222 223 224 225
  space_fallback_type (hb_codepoint_t u)
  {
    switch (u)
    {
      /* All GC=Zs chars that can use a fallback. */
B
Behdad Esfahbod 已提交
226
      default:	    return NOT_SPACE;	/* U+1680 OGHAM SPACE MARK */
227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242
      case 0x0020u: return SPACE;	/* U+0020 SPACE */
      case 0x00A0u: return SPACE;	/* U+00A0 NO-BREAK SPACE */
      case 0x2000u: return SPACE_EM_2;	/* U+2000 EN QUAD */
      case 0x2001u: return SPACE_EM;	/* U+2001 EM QUAD */
      case 0x2002u: return SPACE_EM_2;	/* U+2002 EN SPACE */
      case 0x2003u: return SPACE_EM;	/* U+2003 EM SPACE */
      case 0x2004u: return SPACE_EM_3;	/* U+2004 THREE-PER-EM SPACE */
      case 0x2005u: return SPACE_EM_4;	/* U+2005 FOUR-PER-EM SPACE */
      case 0x2006u: return SPACE_EM_6;	/* U+2006 SIX-PER-EM SPACE */
      case 0x2007u: return SPACE_FIGURE;	/* U+2007 FIGURE SPACE */
      case 0x2008u: return SPACE_PUNCTUATION;	/* U+2008 PUNCTUATION SPACE */
      case 0x2009u: return SPACE_EM_5;		/* U+2009 THIN SPACE */
      case 0x200Au: return SPACE_EM_16;		/* U+200A HAIR SPACE */
      case 0x202Fu: return SPACE_NARROW;	/* U+202F NARROW NO-BREAK SPACE */
      case 0x205Fu: return SPACE_4_EM_18;	/* U+205F MEDIUM MATHEMATICAL SPACE */
      case 0x3000u: return SPACE_EM;		/* U+3000 IDEOGRAPHIC SPACE */
243 244
    }
  }
245

246
  struct {
247
#define HB_UNICODE_FUNC_IMPLEMENT(name) hb_unicode_##name##_func_t name;
B
Behdad Esfahbod 已提交
248 249
    HB_UNICODE_FUNCS_IMPLEMENT_CALLBACKS
#undef HB_UNICODE_FUNC_IMPLEMENT
250
  } func;
251 252

  struct {
B
Behdad Esfahbod 已提交
253
#define HB_UNICODE_FUNC_IMPLEMENT(name) void *name;
B
Behdad Esfahbod 已提交
254 255
    HB_UNICODE_FUNCS_IMPLEMENT_CALLBACKS
#undef HB_UNICODE_FUNC_IMPLEMENT
256 257
  } user_data;

258
  struct {
B
Behdad Esfahbod 已提交
259
#define HB_UNICODE_FUNC_IMPLEMENT(name) hb_destroy_func_t name;
B
Behdad Esfahbod 已提交
260 261
    HB_UNICODE_FUNCS_IMPLEMENT_CALLBACKS
#undef HB_UNICODE_FUNC_IMPLEMENT
262
  } destroy;
B
Behdad Esfahbod 已提交
263
};
264
DECLARE_NULL_INSTANCE (hb_unicode_funcs_t);
265

266

267 268 269
/*
 * Modified combining marks
 */
270 271 272 273 274 275

/* Hebrew
 *
 * We permute the "fixed-position" classes 10-26 into the order
 * described in the SBL Hebrew manual:
 *
276
 * https://www.sbl-site.org/Fonts/SBLHebrewUserManual1.5x.pdf
277 278
 *
 * (as recommended by:
279
 *  https://forum.fontlab.com/archive-old-microsoft-volt-group/vista-and-diacritic-ordering/msg22823/)
280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305
 *
 * More details here:
 * https://bugzilla.mozilla.org/show_bug.cgi?id=662055
 */
#define HB_MODIFIED_COMBINING_CLASS_CCC10 22 /* sheva */
#define HB_MODIFIED_COMBINING_CLASS_CCC11 15 /* hataf segol */
#define HB_MODIFIED_COMBINING_CLASS_CCC12 16 /* hataf patah */
#define HB_MODIFIED_COMBINING_CLASS_CCC13 17 /* hataf qamats */
#define HB_MODIFIED_COMBINING_CLASS_CCC14 23 /* hiriq */
#define HB_MODIFIED_COMBINING_CLASS_CCC15 18 /* tsere */
#define HB_MODIFIED_COMBINING_CLASS_CCC16 19 /* segol */
#define HB_MODIFIED_COMBINING_CLASS_CCC17 20 /* patah */
#define HB_MODIFIED_COMBINING_CLASS_CCC18 21 /* qamats */
#define HB_MODIFIED_COMBINING_CLASS_CCC19 14 /* holam */
#define HB_MODIFIED_COMBINING_CLASS_CCC20 24 /* qubuts */
#define HB_MODIFIED_COMBINING_CLASS_CCC21 12 /* dagesh */
#define HB_MODIFIED_COMBINING_CLASS_CCC22 25 /* meteg */
#define HB_MODIFIED_COMBINING_CLASS_CCC23 13 /* rafe */
#define HB_MODIFIED_COMBINING_CLASS_CCC24 10 /* shin dot */
#define HB_MODIFIED_COMBINING_CLASS_CCC25 11 /* sin dot */
#define HB_MODIFIED_COMBINING_CLASS_CCC26 26 /* point varika */

/*
 * Arabic
 *
 * Modify to move Shadda (ccc=33) before other marks.  See:
306 307
 * https://unicode.org/faq/normalization.html#8
 * https://unicode.org/faq/normalization.html#9
308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325
 */
#define HB_MODIFIED_COMBINING_CLASS_CCC27 28 /* fathatan */
#define HB_MODIFIED_COMBINING_CLASS_CCC28 29 /* dammatan */
#define HB_MODIFIED_COMBINING_CLASS_CCC29 30 /* kasratan */
#define HB_MODIFIED_COMBINING_CLASS_CCC30 31 /* fatha */
#define HB_MODIFIED_COMBINING_CLASS_CCC31 32 /* damma */
#define HB_MODIFIED_COMBINING_CLASS_CCC32 33 /* kasra */
#define HB_MODIFIED_COMBINING_CLASS_CCC33 27 /* shadda */
#define HB_MODIFIED_COMBINING_CLASS_CCC34 34 /* sukun */
#define HB_MODIFIED_COMBINING_CLASS_CCC35 35 /* superscript alef */

/* Syriac */
#define HB_MODIFIED_COMBINING_CLASS_CCC36 36 /* superscript alaph */

/* Telugu
 *
 * Modify Telugu length marks (ccc=84, ccc=91).
 * These are the only matras in the main Indic scripts range that have
326 327
 * a non-zero ccc.  That makes them reorder with the Halant (ccc=9).
 * Assign 5 and 6, which are otherwise unassigned.
328
 */
329 330
#define HB_MODIFIED_COMBINING_CLASS_CCC84 5 /* length mark */
#define HB_MODIFIED_COMBINING_CLASS_CCC91 6 /* ai length mark */
331 332 333

/* Thai
 *
334 335 336
 * Modify U+0E38 and U+0E39 (ccc=103) to be reordered before U+0E3A (ccc=9).
 * Assign 3, which is unassigned otherwise.
 * Uniscribe does this reordering too.
337 338 339 340 341 342 343 344
 */
#define HB_MODIFIED_COMBINING_CLASS_CCC103 3 /* sara u / sara uu */
#define HB_MODIFIED_COMBINING_CLASS_CCC107 107 /* mai * */

/* Lao */
#define HB_MODIFIED_COMBINING_CLASS_CCC118 118 /* sign u / sign uu */
#define HB_MODIFIED_COMBINING_CLASS_CCC122 122 /* mai * */

345
/* Tibetan
346 347 348
 *
 * In case of multiple vowel-signs, use u first (but after achung)
 * this allows Dzongkha multi-vowel shortcuts to render correctly
349
 */
350
#define HB_MODIFIED_COMBINING_CLASS_CCC129 129 /* sign aa */
351 352
#define HB_MODIFIED_COMBINING_CLASS_CCC130 132 /* sign i */
#define HB_MODIFIED_COMBINING_CLASS_CCC132 131 /* sign u */
353

B
Behdad Esfahbod 已提交
354 355 356
/* Misc */

#define HB_UNICODE_GENERAL_CATEGORY_IS_MARK(gen_cat) \
B
Behdad Esfahbod 已提交
357
	(FLAG_UNSAFE (gen_cat) & \
B
Behdad Esfahbod 已提交
358 359 360 361
	 (FLAG (HB_UNICODE_GENERAL_CATEGORY_SPACING_MARK) | \
	  FLAG (HB_UNICODE_GENERAL_CATEGORY_ENCLOSING_MARK) | \
	  FLAG (HB_UNICODE_GENERAL_CATEGORY_NON_SPACING_MARK)))

362 363 364 365 366 367 368 369

/*
 * Ranges, used for bsearch tables.
 */

struct hb_unicode_range_t
{
  static int
B
Behdad Esfahbod 已提交
370
  cmp (const void *_key, const void *_item)
371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394
  {
    hb_codepoint_t cp = *((hb_codepoint_t *) _key);
    const hb_unicode_range_t *range = (hb_unicode_range_t *) _item;

    if (cp < range->start)
      return -1;
    else if (cp <= range->end)
      return 0;
    else
      return +1;
  }

  hb_codepoint_t start;
  hb_codepoint_t end;
};

/*
 * Emoji.
 */

HB_INTERNAL bool
_hb_unicode_is_emoji_Extended_Pictographic (hb_codepoint_t cp);


B
Behdad Esfahbod 已提交
395 396 397
extern "C" HB_INTERNAL hb_unicode_funcs_t *hb_ucd_get_unicode_funcs ();


398
#endif /* HB_UNICODE_HH */