hb-icu.cc 12.1 KB
Newer Older
B
Behdad Esfahbod 已提交
1
/*
B
Behdad Esfahbod 已提交
2 3 4
 * Copyright © 2009  Red Hat, Inc.
 * Copyright © 2009  Keith Stribley
 * Copyright © 2011  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
B
Behdad Esfahbod 已提交
27
 * Google Author(s): Behdad Esfahbod
B
Behdad Esfahbod 已提交
28 29
 */

30
#include "hb-private.hh"
B
Behdad Esfahbod 已提交
31 32 33

#include "hb-icu.h"

34
#include "hb-unicode-private.hh"
35
#include "hb-machinery-private.hh"
B
Behdad Esfahbod 已提交
36 37

#include <unicode/uchar.h>
38
#include <unicode/unorm2.h>
39
#include <unicode/ustring.h>
K
Kal Conley 已提交
40
#include <unicode/utf16.h>
B
Behdad Esfahbod 已提交
41
#include <unicode/uversion.h>
B
Behdad Esfahbod 已提交
42 43


44 45
hb_script_t
hb_icu_script_to_script (UScriptCode script)
B
Behdad Esfahbod 已提交
46
{
47 48 49
  if (unlikely (script == USCRIPT_INVALID_CODE))
    return HB_SCRIPT_INVALID;

50
  return hb_script_from_string (uscript_getShortName (script), -1);
B
Behdad Esfahbod 已提交
51 52
}

53 54
UScriptCode
hb_icu_script_from_script (hb_script_t script)
B
Behdad Esfahbod 已提交
55
{
56 57
  if (unlikely (script == HB_SCRIPT_INVALID))
    return USCRIPT_INVALID_CODE;
B
Behdad Esfahbod 已提交
58

59 60 61
  for (unsigned int i = 0; i < USCRIPT_CODE_LIMIT; i++)
    if (unlikely (hb_icu_script_to_script ((UScriptCode) i) == script))
      return (UScriptCode) i;
62

63 64 65 66
  return USCRIPT_UNKNOWN;
}


67
static hb_unicode_combining_class_t
B
Minor  
Behdad Esfahbod 已提交
68 69 70
hb_icu_unicode_combining_class (hb_unicode_funcs_t *ufuncs HB_UNUSED,
				hb_codepoint_t      unicode,
				void               *user_data HB_UNUSED)
71 72

{
73
  return (hb_unicode_combining_class_t) u_getCombiningClass (unicode);
74 75 76
}

static unsigned int
B
Minor  
Behdad Esfahbod 已提交
77 78 79
hb_icu_unicode_eastasian_width (hb_unicode_funcs_t *ufuncs HB_UNUSED,
				hb_codepoint_t      unicode,
				void               *user_data HB_UNUSED)
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
{
  switch (u_getIntPropertyValue(unicode, UCHAR_EAST_ASIAN_WIDTH))
  {
  case U_EA_WIDE:
  case U_EA_FULLWIDTH:
    return 2;
  case U_EA_NEUTRAL:
  case U_EA_AMBIGUOUS:
  case U_EA_HALFWIDTH:
  case U_EA_NARROW:
    return 1;
  }
  return 1;
}

static hb_unicode_general_category_t
B
Minor  
Behdad Esfahbod 已提交
96 97 98
hb_icu_unicode_general_category (hb_unicode_funcs_t *ufuncs HB_UNUSED,
				 hb_codepoint_t      unicode,
				 void               *user_data HB_UNUSED)
99 100 101 102 103 104 105 106 107 108 109 110 111
{
  switch (u_getIntPropertyValue(unicode, UCHAR_GENERAL_CATEGORY))
  {
  case U_UNASSIGNED:			return HB_UNICODE_GENERAL_CATEGORY_UNASSIGNED;

  case U_UPPERCASE_LETTER:		return HB_UNICODE_GENERAL_CATEGORY_UPPERCASE_LETTER;
  case U_LOWERCASE_LETTER:		return HB_UNICODE_GENERAL_CATEGORY_LOWERCASE_LETTER;
  case U_TITLECASE_LETTER:		return HB_UNICODE_GENERAL_CATEGORY_TITLECASE_LETTER;
  case U_MODIFIER_LETTER:		return HB_UNICODE_GENERAL_CATEGORY_MODIFIER_LETTER;
  case U_OTHER_LETTER:			return HB_UNICODE_GENERAL_CATEGORY_OTHER_LETTER;

  case U_NON_SPACING_MARK:		return HB_UNICODE_GENERAL_CATEGORY_NON_SPACING_MARK;
  case U_ENCLOSING_MARK:		return HB_UNICODE_GENERAL_CATEGORY_ENCLOSING_MARK;
112
  case U_COMBINING_SPACING_MARK:	return HB_UNICODE_GENERAL_CATEGORY_SPACING_MARK;
113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146

  case U_DECIMAL_DIGIT_NUMBER:		return HB_UNICODE_GENERAL_CATEGORY_DECIMAL_NUMBER;
  case U_LETTER_NUMBER:			return HB_UNICODE_GENERAL_CATEGORY_LETTER_NUMBER;
  case U_OTHER_NUMBER:			return HB_UNICODE_GENERAL_CATEGORY_OTHER_NUMBER;

  case U_SPACE_SEPARATOR:		return HB_UNICODE_GENERAL_CATEGORY_SPACE_SEPARATOR;
  case U_LINE_SEPARATOR:		return HB_UNICODE_GENERAL_CATEGORY_LINE_SEPARATOR;
  case U_PARAGRAPH_SEPARATOR:		return HB_UNICODE_GENERAL_CATEGORY_PARAGRAPH_SEPARATOR;

  case U_CONTROL_CHAR:			return HB_UNICODE_GENERAL_CATEGORY_CONTROL;
  case U_FORMAT_CHAR:			return HB_UNICODE_GENERAL_CATEGORY_FORMAT;
  case U_PRIVATE_USE_CHAR:		return HB_UNICODE_GENERAL_CATEGORY_PRIVATE_USE;
  case U_SURROGATE:			return HB_UNICODE_GENERAL_CATEGORY_SURROGATE;


  case U_DASH_PUNCTUATION:		return HB_UNICODE_GENERAL_CATEGORY_DASH_PUNCTUATION;
  case U_START_PUNCTUATION:		return HB_UNICODE_GENERAL_CATEGORY_OPEN_PUNCTUATION;
  case U_END_PUNCTUATION:		return HB_UNICODE_GENERAL_CATEGORY_CLOSE_PUNCTUATION;
  case U_CONNECTOR_PUNCTUATION:		return HB_UNICODE_GENERAL_CATEGORY_CONNECT_PUNCTUATION;
  case U_OTHER_PUNCTUATION:		return HB_UNICODE_GENERAL_CATEGORY_OTHER_PUNCTUATION;

  case U_MATH_SYMBOL:			return HB_UNICODE_GENERAL_CATEGORY_MATH_SYMBOL;
  case U_CURRENCY_SYMBOL:		return HB_UNICODE_GENERAL_CATEGORY_CURRENCY_SYMBOL;
  case U_MODIFIER_SYMBOL:		return HB_UNICODE_GENERAL_CATEGORY_MODIFIER_SYMBOL;
  case U_OTHER_SYMBOL:			return HB_UNICODE_GENERAL_CATEGORY_OTHER_SYMBOL;

  case U_INITIAL_PUNCTUATION:		return HB_UNICODE_GENERAL_CATEGORY_INITIAL_PUNCTUATION;
  case U_FINAL_PUNCTUATION:		return HB_UNICODE_GENERAL_CATEGORY_FINAL_PUNCTUATION;
  }

  return HB_UNICODE_GENERAL_CATEGORY_UNASSIGNED;
}

static hb_codepoint_t
B
Minor  
Behdad Esfahbod 已提交
147 148 149
hb_icu_unicode_mirroring (hb_unicode_funcs_t *ufuncs HB_UNUSED,
			  hb_codepoint_t      unicode,
			  void               *user_data HB_UNUSED)
150 151 152 153 154
{
  return u_charMirror(unicode);
}

static hb_script_t
B
Minor  
Behdad Esfahbod 已提交
155 156 157
hb_icu_unicode_script (hb_unicode_funcs_t *ufuncs HB_UNUSED,
		       hb_codepoint_t      unicode,
		       void               *user_data HB_UNUSED)
158 159 160 161
{
  UErrorCode status = U_ZERO_ERROR;
  UScriptCode scriptCode = uscript_getScript(unicode, &status);

B
Behdad Esfahbod 已提交
162
  if (unlikely (U_FAILURE (status)))
163 164
    return HB_SCRIPT_UNKNOWN;

165
  return hb_icu_script_to_script (scriptCode);
B
Behdad Esfahbod 已提交
166 167
}

B
Minor  
Behdad Esfahbod 已提交
168 169 170 171 172 173 174
static hb_bool_t
hb_icu_unicode_compose (hb_unicode_funcs_t *ufuncs HB_UNUSED,
			hb_codepoint_t      a,
			hb_codepoint_t      b,
			hb_codepoint_t     *ab,
			void               *user_data HB_UNUSED)
{
175 176
#if U_ICU_VERSION_MAJOR_NUM >= 49
  {
177 178
    const UNormalizer2 *normalizer = (const UNormalizer2 *) user_data;
    UChar32 ret = unorm2_composePair (normalizer, a, b);
179 180 181 182 183 184 185 186
    if (ret < 0) return false;
    *ab = ret;
    return true;
  }
#endif

  /* We don't ifdef-out the fallback code such that compiler always
   * sees it and makes sure it's compilable. */
187 188

  UChar utf16[4], normalized[5];
B
Behdad Esfahbod 已提交
189
  unsigned int len;
190 191 192 193
  hb_bool_t ret, err;
  UErrorCode icu_err;

  len = 0;
194
  err = false;
195
  U16_APPEND (utf16, len, ARRAY_LENGTH (utf16), a, err);
196
  if (err) return false;
197
  U16_APPEND (utf16, len, ARRAY_LENGTH (utf16), b, err);
198
  if (err) return false;
199 200

  icu_err = U_ZERO_ERROR;
201
  len = unorm2_normalize (unorm2_getNFCInstance (&icu_err), utf16, len, normalized, ARRAY_LENGTH (normalized), &icu_err);
B
Behdad Esfahbod 已提交
202
  if (U_FAILURE (icu_err))
203
    return false;
204
  if (u_countChar32 (normalized, len) == 1) {
205
    U16_GET_UNSAFE (normalized, 0, *ab);
206
    ret = true;
207
  } else {
208
    ret = false;
209 210 211
  }

  return ret;
B
Minor  
Behdad Esfahbod 已提交
212 213 214 215 216 217 218 219 220
}

static hb_bool_t
hb_icu_unicode_decompose (hb_unicode_funcs_t *ufuncs HB_UNUSED,
			  hb_codepoint_t      ab,
			  hb_codepoint_t     *a,
			  hb_codepoint_t     *b,
			  void               *user_data HB_UNUSED)
{
221 222
#if U_ICU_VERSION_MAJOR_NUM >= 49
  {
223
    const UNormalizer2 *normalizer = (const UNormalizer2 *) user_data;
224 225 226
    UChar decomposed[4];
    int len;
    UErrorCode icu_err = U_ZERO_ERROR;
227
    len = unorm2_getRawDecomposition (normalizer, ab, decomposed,
228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247
				      ARRAY_LENGTH (decomposed), &icu_err);
    if (U_FAILURE (icu_err) || len < 0) return false;

    len = u_countChar32 (decomposed, len);
    if (len == 1) {
      U16_GET_UNSAFE (decomposed, 0, *a);
      *b = 0;
      return *a != ab;
    } else if (len == 2) {
      len =0;
      U16_NEXT_UNSAFE (decomposed, len, *a);
      U16_NEXT_UNSAFE (decomposed, len, *b);
    }
    return true;
  }
#endif

  /* We don't ifdef-out the fallback code such that compiler always
   * sees it and makes sure it's compilable. */

248
  UChar utf16[2], normalized[2 * HB_UNICODE_MAX_DECOMPOSITION_LEN + 1];
B
Behdad Esfahbod 已提交
249
  unsigned int len;
250 251 252
  hb_bool_t ret, err;
  UErrorCode icu_err;

253 254 255 256
  /* This function is a monster! Maybe it wasn't a good idea adding a
   * pairwise decompose API... */
  /* Watchout for the dragons.  Err, watchout for macros changing len. */

257
  len = 0;
258
  err = false;
259
  U16_APPEND (utf16, len, ARRAY_LENGTH (utf16), ab, err);
260
  if (err) return false;
261 262

  icu_err = U_ZERO_ERROR;
263
  len = unorm2_normalize (unorm2_getNFDInstance (&icu_err), utf16, len, normalized, ARRAY_LENGTH (normalized), &icu_err);
B
Behdad Esfahbod 已提交
264
  if (U_FAILURE (icu_err))
265
    return false;
266

267
  len = u_countChar32 (normalized, len);
268 269 270 271 272 273

  if (len == 1) {
    U16_GET_UNSAFE (normalized, 0, *a);
    *b = 0;
    ret = *a != ab;
  } else if (len == 2) {
274 275 276 277
    len =0;
    U16_NEXT_UNSAFE (normalized, len, *a);
    U16_NEXT_UNSAFE (normalized, len, *b);

278 279 280 281 282
    /* Here's the ugly part: if ab decomposes to a single character and
     * that character decomposes again, we have to detect that and undo
     * the second part :-(. */
    UChar recomposed[20];
    icu_err = U_ZERO_ERROR;
283
    unorm2_normalize (unorm2_getNFCInstance (&icu_err), normalized, len, recomposed, ARRAY_LENGTH (recomposed), &icu_err);
B
Behdad Esfahbod 已提交
284
    if (U_FAILURE (icu_err))
285
      return false;
286 287 288 289
    hb_codepoint_t c;
    U16_GET_UNSAFE (recomposed, 0, c);
    if (c != *a && c != ab) {
      *a = c;
290 291
      *b = 0;
    }
292
    ret = true;
293 294 295
  } else {
    /* If decomposed to more than two characters, take the last one,
     * and recompose the rest to get the first component. */
296 297
    U16_PREV_UNSAFE (normalized, len, *b); /* Changes len in-place. */
    UChar recomposed[18 * 2];
298
    icu_err = U_ZERO_ERROR;
299
    len = unorm2_normalize (unorm2_getNFCInstance (&icu_err), normalized, len, recomposed, ARRAY_LENGTH (recomposed), &icu_err);
B
Behdad Esfahbod 已提交
300
    if (U_FAILURE (icu_err))
301
      return false;
302
    /* We expect that recomposed has exactly one character now. */
303 304
    if (unlikely (u_countChar32 (recomposed, len) != 1))
      return false;
305
    U16_GET_UNSAFE (recomposed, 0, *a);
306
    ret = true;
307 308 309
  }

  return ret;
B
Minor  
Behdad Esfahbod 已提交
310 311
}

312 313 314 315 316 317 318
static unsigned int
hb_icu_unicode_decompose_compatibility (hb_unicode_funcs_t *ufuncs HB_UNUSED,
					hb_codepoint_t      u,
					hb_codepoint_t     *decomposed,
					void               *user_data HB_UNUSED)
{
  UChar utf16[2], normalized[2 * HB_UNICODE_MAX_DECOMPOSITION_LEN + 1];
B
Behdad Esfahbod 已提交
319
  unsigned int len;
320 321 322 323 324 325
  int32_t utf32_len;
  hb_bool_t err;
  UErrorCode icu_err;

  /* Copy @u into a UTF-16 array to be passed to ICU. */
  len = 0;
326
  err = false;
327 328 329 330 331 332
  U16_APPEND (utf16, len, ARRAY_LENGTH (utf16), u, err);
  if (err)
    return 0;

  /* Normalise the codepoint using NFKD mode. */
  icu_err = U_ZERO_ERROR;
333
  len = unorm2_normalize (unorm2_getNFKDInstance (&icu_err), utf16, len, normalized, ARRAY_LENGTH (normalized), &icu_err);
B
Behdad Esfahbod 已提交
334
  if (U_FAILURE (icu_err))
335 336 337 338 339
    return 0;

  /* Convert the decomposed form from UTF-16 to UTF-32. */
  icu_err = U_ZERO_ERROR;
  u_strToUTF32 ((UChar32*) decomposed, HB_UNICODE_MAX_DECOMPOSITION_LEN, &utf32_len, normalized, len, &icu_err);
B
Behdad Esfahbod 已提交
340
  if (U_FAILURE (icu_err))
341 342 343 344 345
    return 0;

  return utf32_len;
}

346
#ifdef HB_USE_ATEXIT
347
static void free_static_icu_funcs (void);
348
#endif
349

350
static struct hb_icu_unicode_funcs_lazy_loader_t : hb_unicode_funcs_lazy_loader_t<hb_icu_unicode_funcs_lazy_loader_t>
351
{
352
  static inline hb_unicode_funcs_t *create (void)
353
  {
354
    void *user_data = nullptr;
355
#if U_ICU_VERSION_MAJOR_NUM >= 49
356 357 358
    UErrorCode icu_err = U_ZERO_ERROR;
    user_data = (void *) unorm2_getNFCInstance (&icu_err);
    assert (user_data);
359 360
#endif

361
    hb_unicode_funcs_t *funcs = hb_unicode_funcs_create (nullptr);
362 363

#define HB_UNICODE_FUNC_IMPLEMENT(name) \
364
    hb_unicode_funcs_set_##name##_func (funcs, hb_icu_unicode_##name, user_data, nullptr);
365
      HB_UNICODE_FUNCS_IMPLEMENT_CALLBACKS
B
Minor  
Behdad Esfahbod 已提交
366
#undef HB_UNICODE_FUNC_IMPLEMENT
367 368 369

    hb_unicode_funcs_make_immutable (funcs);

370 371 372 373 374 375 376
#ifdef HB_USE_ATEXIT
    atexit (free_static_icu_funcs);
#endif

    return funcs;
  }
} static_icu_funcs;
B
Behdad Esfahbod 已提交
377

378
#ifdef HB_USE_ATEXIT
379 380 381
static
void free_static_icu_funcs (void)
{
B
Behdad Esfahbod 已提交
382
  static_icu_funcs.free_instance ();
383
}
384
#endif
385

386 387 388
hb_unicode_funcs_t *
hb_icu_get_unicode_funcs (void)
{
389
  return static_icu_funcs.get_unconst ();
B
Behdad Esfahbod 已提交
390
}