harfbuzz/src/hb-icu.cc

329 lines
10 KiB
C++
Raw Normal View History

2009-11-03 21:35:10 +00:00
/*
2011-04-21 21:14:28 +00:00
* Copyright © 2009 Red Hat, Inc.
* Copyright © 2009 Keith Stribley
* Copyright © 2011 Google, Inc.
2009-11-03 21:35:10 +00:00
*
2010-04-22 04:11:43 +00:00
* This is part of HarfBuzz, a text shaping library.
2009-11-03 21:35:10 +00:00
*
* 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
2011-04-21 21:14:28 +00:00
* Google Author(s): Behdad Esfahbod
2009-11-03 21:35:10 +00:00
*/
#include "hb-private.hh"
2009-11-03 21:35:10 +00:00
#include "hb-icu.h"
2011-04-20 06:00:47 +00:00
#include "hb-unicode-private.hh"
2009-11-03 21:35:10 +00:00
2010-05-21 12:29:12 +00:00
#include <unicode/uversion.h>
2009-11-03 21:35:10 +00:00
#include <unicode/uchar.h>
2011-07-21 03:19:49 +00:00
#include <unicode/unorm.h>
#include <unicode/ustring.h>
2009-11-03 21:35:10 +00:00
2010-07-23 19:11:18 +00:00
hb_script_t
hb_icu_script_to_script (UScriptCode script)
2009-11-03 21:35:10 +00:00
{
if (unlikely (script == USCRIPT_INVALID_CODE))
return HB_SCRIPT_INVALID;
return hb_script_from_string (uscript_getShortName (script), -1);
2009-11-03 21:35:10 +00:00
}
UScriptCode
hb_icu_script_from_script (hb_script_t script)
2009-11-03 21:35:10 +00:00
{
if (unlikely (script == HB_SCRIPT_INVALID))
return USCRIPT_INVALID_CODE;
2010-11-17 19:35:34 +00:00
for (unsigned int i = 0; i < USCRIPT_CODE_LIMIT; i++)
if (unlikely (hb_icu_script_to_script ((UScriptCode) i) == script))
return (UScriptCode) i;
return USCRIPT_UNKNOWN;
}
2012-08-01 20:23:44 +00:00
static hb_unicode_combining_class_t
2011-07-21 02:16:13 +00:00
hb_icu_unicode_combining_class (hb_unicode_funcs_t *ufuncs HB_UNUSED,
hb_codepoint_t unicode,
void *user_data HB_UNUSED)
{
2012-08-01 20:23:44 +00:00
return (hb_unicode_combining_class_t) u_getCombiningClass (unicode);
}
static unsigned int
2011-07-21 02:16:13 +00:00
hb_icu_unicode_eastasian_width (hb_unicode_funcs_t *ufuncs HB_UNUSED,
hb_codepoint_t unicode,
void *user_data HB_UNUSED)
{
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
2011-07-21 02:16:13 +00:00
hb_icu_unicode_general_category (hb_unicode_funcs_t *ufuncs HB_UNUSED,
hb_codepoint_t unicode,
void *user_data HB_UNUSED)
{
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;
case U_COMBINING_SPACING_MARK: return HB_UNICODE_GENERAL_CATEGORY_SPACING_MARK;
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
2011-07-21 02:16:13 +00:00
hb_icu_unicode_mirroring (hb_unicode_funcs_t *ufuncs HB_UNUSED,
hb_codepoint_t unicode,
void *user_data HB_UNUSED)
{
return u_charMirror(unicode);
}
static hb_script_t
2011-07-21 02:16:13 +00:00
hb_icu_unicode_script (hb_unicode_funcs_t *ufuncs HB_UNUSED,
hb_codepoint_t unicode,
void *user_data HB_UNUSED)
{
UErrorCode status = U_ZERO_ERROR;
UScriptCode scriptCode = uscript_getScript(unicode, &status);
2012-01-19 03:32:52 +00:00
if (unlikely (U_FAILURE (status)))
return HB_SCRIPT_UNKNOWN;
return hb_icu_script_to_script (scriptCode);
2009-11-03 21:35:10 +00:00
}
2011-07-21 02:16:13 +00:00
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)
{
2011-07-21 03:19:49 +00:00
if (!a || !b)
2012-06-06 00:35:40 +00:00
return false;
2011-07-21 03:19:49 +00:00
UChar utf16[4], normalized[5];
2012-08-07 20:57:48 +00:00
unsigned int len;
2011-07-21 03:19:49 +00:00
hb_bool_t ret, err;
UErrorCode icu_err;
len = 0;
2012-06-06 00:35:40 +00:00
err = false;
2011-07-21 03:19:49 +00:00
U16_APPEND (utf16, len, ARRAY_LENGTH (utf16), a, err);
2012-06-06 00:35:40 +00:00
if (err) return false;
2011-07-21 03:19:49 +00:00
U16_APPEND (utf16, len, ARRAY_LENGTH (utf16), b, err);
2012-06-06 00:35:40 +00:00
if (err) return false;
2011-07-21 03:19:49 +00:00
icu_err = U_ZERO_ERROR;
len = unorm_normalize (utf16, len, UNORM_NFC, 0, normalized, ARRAY_LENGTH (normalized), &icu_err);
2012-01-19 03:32:52 +00:00
if (U_FAILURE (icu_err))
2012-06-06 00:35:40 +00:00
return false;
if (u_countChar32 (normalized, len) == 1) {
2011-07-21 03:19:49 +00:00
U16_GET_UNSAFE (normalized, 0, *ab);
2012-06-06 00:35:40 +00:00
ret = true;
2011-07-21 03:19:49 +00:00
} else {
2012-06-06 00:35:40 +00:00
ret = false;
2011-07-21 03:19:49 +00:00
}
return ret;
2011-07-21 02:16:13 +00:00
}
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)
{
UChar utf16[2], normalized[2 * HB_UNICODE_MAX_DECOMPOSITION_LEN + 1];
2012-08-07 20:57:48 +00:00
unsigned int len;
2011-07-21 03:19:49 +00:00
hb_bool_t ret, err;
UErrorCode icu_err;
/* 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. */
2011-07-21 03:19:49 +00:00
len = 0;
2012-06-06 00:35:40 +00:00
err = false;
2011-07-21 03:19:49 +00:00
U16_APPEND (utf16, len, ARRAY_LENGTH (utf16), ab, err);
2012-06-06 00:35:40 +00:00
if (err) return false;
2011-07-21 03:19:49 +00:00
icu_err = U_ZERO_ERROR;
len = unorm_normalize (utf16, len, UNORM_NFD, 0, normalized, ARRAY_LENGTH (normalized), &icu_err);
2012-01-19 03:32:52 +00:00
if (U_FAILURE (icu_err))
2012-06-06 00:35:40 +00:00
return false;
2011-07-21 03:19:49 +00:00
len = u_countChar32 (normalized, len);
2011-07-21 03:19:49 +00:00
if (len == 1) {
U16_GET_UNSAFE (normalized, 0, *a);
*b = 0;
ret = *a != ab;
} else if (len == 2) {
len =0;
U16_NEXT_UNSAFE (normalized, len, *a);
U16_NEXT_UNSAFE (normalized, len, *b);
2011-07-21 03:19:49 +00:00
/* 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;
unorm_normalize (normalized, len, UNORM_NFC, 0, recomposed, ARRAY_LENGTH (recomposed), &icu_err);
2012-01-19 03:32:52 +00:00
if (U_FAILURE (icu_err))
2012-06-06 00:35:40 +00:00
return false;
hb_codepoint_t c;
U16_GET_UNSAFE (recomposed, 0, c);
if (c != *a && c != ab) {
*a = c;
2011-07-21 03:19:49 +00:00
*b = 0;
}
2012-06-06 00:35:40 +00:00
ret = true;
2011-07-21 03:19:49 +00:00
} else {
/* If decomposed to more than two characters, take the last one,
* and recompose the rest to get the first component. */
U16_PREV_UNSAFE (normalized, len, *b); /* Changes len in-place. */
UChar recomposed[18 * 2];
2011-07-21 03:19:49 +00:00
icu_err = U_ZERO_ERROR;
len = unorm_normalize (normalized, len, UNORM_NFC, 0, recomposed, ARRAY_LENGTH (recomposed), &icu_err);
2012-01-19 03:32:52 +00:00
if (U_FAILURE (icu_err))
2012-06-06 00:35:40 +00:00
return false;
2011-07-21 03:19:49 +00:00
/* We expect that recomposed has exactly one character now. */
if (unlikely (u_countChar32 (recomposed, len) != 1))
return false;
2011-07-21 03:19:49 +00:00
U16_GET_UNSAFE (recomposed, 0, *a);
2012-06-06 00:35:40 +00:00
ret = true;
2011-07-21 03:19:49 +00:00
}
return ret;
2011-07-21 02:16:13 +00:00
}
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];
2012-08-07 20:57:48 +00:00
unsigned int len;
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;
err = FALSE;
U16_APPEND (utf16, len, ARRAY_LENGTH (utf16), u, err);
if (err)
return 0;
/* Normalise the codepoint using NFKD mode. */
icu_err = U_ZERO_ERROR;
len = unorm_normalize (utf16, len, UNORM_NFKD, 0, normalized, ARRAY_LENGTH (normalized), &icu_err);
if (icu_err)
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);
if (icu_err)
return 0;
return utf32_len;
}
extern HB_INTERNAL const hb_unicode_funcs_t _hb_icu_unicode_funcs;
const hb_unicode_funcs_t _hb_icu_unicode_funcs = {
HB_OBJECT_HEADER_STATIC,
NULL, /* parent */
2012-06-06 00:35:40 +00:00
true, /* immutable */
{
2011-07-21 02:16:13 +00:00
#define HB_UNICODE_FUNC_IMPLEMENT(name) hb_icu_unicode_##name,
HB_UNICODE_FUNCS_IMPLEMENT_CALLBACKS
2011-07-21 02:16:13 +00:00
#undef HB_UNICODE_FUNC_IMPLEMENT
}
};
2009-11-03 21:35:10 +00:00
hb_unicode_funcs_t *
hb_icu_get_unicode_funcs (void)
{
return const_cast<hb_unicode_funcs_t *> (&_hb_icu_unicode_funcs);
2009-11-03 21:35:10 +00:00
}
2010-07-23 19:11:18 +00:00