2009-11-03 22:35:10 +01:00
|
|
|
/*
|
2011-04-21 23:14:28 +02:00
|
|
|
* Copyright © 2009 Red Hat, Inc.
|
|
|
|
* Copyright © 2009 Keith Stribley
|
|
|
|
* Copyright © 2011 Google, Inc.
|
2009-11-03 22:35:10 +01:00
|
|
|
*
|
2010-04-22 06:11:43 +02:00
|
|
|
* This is part of HarfBuzz, a text shaping library.
|
2009-11-03 22:35:10 +01: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 23:14:28 +02:00
|
|
|
* Google Author(s): Behdad Esfahbod
|
2009-11-03 22:35:10 +01:00
|
|
|
*/
|
|
|
|
|
2018-08-26 07:36:36 +02:00
|
|
|
#include "hb.hh"
|
2009-11-03 22:35:10 +01:00
|
|
|
|
2019-06-18 07:41:49 +02:00
|
|
|
#ifdef HAVE_ICU
|
|
|
|
|
2009-11-03 22:35:10 +01:00
|
|
|
#include "hb-icu.h"
|
|
|
|
|
2018-08-26 07:36:36 +02:00
|
|
|
#include "hb-machinery.hh"
|
2009-11-03 22:35:10 +01:00
|
|
|
|
|
|
|
#include <unicode/uchar.h>
|
2017-10-18 00:06:31 +02:00
|
|
|
#include <unicode/unorm2.h>
|
2012-01-19 04:16:49 +01:00
|
|
|
#include <unicode/ustring.h>
|
2016-02-26 00:36:17 +01:00
|
|
|
#include <unicode/utf16.h>
|
2012-08-13 16:52:52 +02:00
|
|
|
#include <unicode/uversion.h>
|
2010-07-23 21:11:18 +02:00
|
|
|
|
2019-09-26 19:45:59 +02:00
|
|
|
/* ICU extra semicolon, fixed since 65, https://github.com/unicode-org/icu/commit/480bec3 */
|
|
|
|
#if U_ICU_VERSION_MAJOR_NUM < 65 && (defined(__GNUC__) || defined(__clang__))
|
|
|
|
#define HB_ICU_EXTRA_SEMI_IGNORED
|
|
|
|
#pragma GCC diagnostic push
|
|
|
|
#pragma GCC diagnostic ignored "-Wextra-semi-stmt"
|
|
|
|
#endif
|
2010-07-23 21:11:18 +02:00
|
|
|
|
2018-10-27 13:40:43 +02:00
|
|
|
/**
|
|
|
|
* SECTION:hb-icu
|
|
|
|
* @title: hb-icu
|
|
|
|
* @short_description: ICU integration
|
|
|
|
* @include: hb-icu.h
|
|
|
|
*
|
2019-05-20 18:38:13 +02:00
|
|
|
* Functions for using HarfBuzz with the International Components for Unicode
|
|
|
|
* (ICU) library. HarfBuzz supports using ICU to provide Unicode data, by attaching
|
|
|
|
* ICU functions to the virtual methods in a #hb_unicode_funcs_t function
|
|
|
|
* structure.
|
2018-10-27 13:40:43 +02:00
|
|
|
**/
|
|
|
|
|
2019-05-20 18:38:13 +02:00
|
|
|
/**
|
|
|
|
* hb_icu_script_to_script:
|
|
|
|
* @script: The UScriptCode identifier to query
|
|
|
|
*
|
|
|
|
* Fetches the #hb_script_t script that corresponds to the
|
|
|
|
* specified UScriptCode identifier.
|
|
|
|
*
|
|
|
|
* Return value: the #hb_script_t script found
|
|
|
|
*
|
|
|
|
**/
|
2018-10-27 13:40:43 +02:00
|
|
|
|
2011-04-20 08:54:42 +02:00
|
|
|
hb_script_t
|
|
|
|
hb_icu_script_to_script (UScriptCode script)
|
2009-11-03 22:35:10 +01:00
|
|
|
{
|
2011-04-21 20:58:23 +02:00
|
|
|
if (unlikely (script == USCRIPT_INVALID_CODE))
|
|
|
|
return HB_SCRIPT_INVALID;
|
|
|
|
|
2011-08-26 09:18:53 +02:00
|
|
|
return hb_script_from_string (uscript_getShortName (script), -1);
|
2009-11-03 22:35:10 +01:00
|
|
|
}
|
|
|
|
|
2019-05-20 18:38:13 +02:00
|
|
|
/**
|
|
|
|
* hb_icu_script_from_script:
|
|
|
|
* @script: The #hb_script_t script to query
|
|
|
|
*
|
|
|
|
* Fetches the UScriptCode identifier that corresponds to the
|
|
|
|
* specified #hb_script_t script.
|
|
|
|
*
|
|
|
|
* Return value: the UScriptCode identifier found
|
|
|
|
*
|
|
|
|
**/
|
2011-04-20 08:54:42 +02:00
|
|
|
UScriptCode
|
|
|
|
hb_icu_script_from_script (hb_script_t script)
|
2009-11-03 22:35:10 +01:00
|
|
|
{
|
2011-04-21 20:58:23 +02:00
|
|
|
if (unlikely (script == HB_SCRIPT_INVALID))
|
|
|
|
return USCRIPT_INVALID_CODE;
|
2010-11-17 20:35:34 +01:00
|
|
|
|
2018-11-29 17:34:22 +01:00
|
|
|
unsigned int numScriptCode = 1 + u_getIntPropertyMaxValue (UCHAR_SCRIPT);
|
|
|
|
for (unsigned int i = 0; i < numScriptCode; i++)
|
2011-04-21 20:58:23 +02:00
|
|
|
if (unlikely (hb_icu_script_to_script ((UScriptCode) i) == script))
|
|
|
|
return (UScriptCode) i;
|
2011-04-20 06:19:20 +02:00
|
|
|
|
2011-04-20 08:54:42 +02:00
|
|
|
return USCRIPT_UNKNOWN;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-08-01 22:23:44 +02:00
|
|
|
static hb_unicode_combining_class_t
|
2011-07-21 04:16:13 +02:00
|
|
|
hb_icu_unicode_combining_class (hb_unicode_funcs_t *ufuncs HB_UNUSED,
|
|
|
|
hb_codepoint_t unicode,
|
|
|
|
void *user_data HB_UNUSED)
|
2011-04-20 08:54:42 +02:00
|
|
|
|
|
|
|
{
|
2012-08-01 22:23:44 +02:00
|
|
|
return (hb_unicode_combining_class_t) u_getCombiningClass (unicode);
|
2011-04-20 08:54:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static hb_unicode_general_category_t
|
2011-07-21 04:16:13 +02:00
|
|
|
hb_icu_unicode_general_category (hb_unicode_funcs_t *ufuncs HB_UNUSED,
|
|
|
|
hb_codepoint_t unicode,
|
|
|
|
void *user_data HB_UNUSED)
|
2011-04-20 08:54:42 +02:00
|
|
|
{
|
|
|
|
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;
|
2011-07-21 06:12:33 +02:00
|
|
|
case U_COMBINING_SPACING_MARK: return HB_UNICODE_GENERAL_CATEGORY_SPACING_MARK;
|
2011-04-20 08:54:42 +02:00
|
|
|
|
|
|
|
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 04:16:13 +02:00
|
|
|
hb_icu_unicode_mirroring (hb_unicode_funcs_t *ufuncs HB_UNUSED,
|
|
|
|
hb_codepoint_t unicode,
|
|
|
|
void *user_data HB_UNUSED)
|
2011-04-20 08:54:42 +02:00
|
|
|
{
|
|
|
|
return u_charMirror(unicode);
|
|
|
|
}
|
|
|
|
|
|
|
|
static hb_script_t
|
2011-07-21 04:16:13 +02:00
|
|
|
hb_icu_unicode_script (hb_unicode_funcs_t *ufuncs HB_UNUSED,
|
|
|
|
hb_codepoint_t unicode,
|
|
|
|
void *user_data HB_UNUSED)
|
2011-04-20 08:54:42 +02:00
|
|
|
{
|
|
|
|
UErrorCode status = U_ZERO_ERROR;
|
|
|
|
UScriptCode scriptCode = uscript_getScript(unicode, &status);
|
|
|
|
|
2012-01-19 04:32:52 +01:00
|
|
|
if (unlikely (U_FAILURE (status)))
|
2011-05-02 18:37:45 +02:00
|
|
|
return HB_SCRIPT_UNKNOWN;
|
|
|
|
|
2011-04-20 08:54:42 +02:00
|
|
|
return hb_icu_script_to_script (scriptCode);
|
2009-11-03 22:35:10 +01:00
|
|
|
}
|
|
|
|
|
2011-07-21 04:16:13 +02: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,
|
2020-07-29 23:17:30 +02:00
|
|
|
void *user_data)
|
2011-07-21 04:16:13 +02:00
|
|
|
{
|
2020-07-29 22:23:40 +02:00
|
|
|
const UNormalizer2 *normalizer = (const UNormalizer2 *) user_data;
|
|
|
|
UChar32 ret = unorm2_composePair (normalizer, a, b);
|
|
|
|
if (ret < 0) return false;
|
|
|
|
*ab = ret;
|
|
|
|
return true;
|
2011-07-21 04:16:13 +02: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,
|
2020-07-29 23:17:30 +02:00
|
|
|
void *user_data)
|
2011-07-21 04:16:13 +02:00
|
|
|
{
|
2020-07-29 22:23:40 +02:00
|
|
|
const UNormalizer2 *normalizer = (const UNormalizer2 *) user_data;
|
|
|
|
UChar decomposed[4];
|
|
|
|
int len;
|
|
|
|
UErrorCode icu_err = U_ZERO_ERROR;
|
|
|
|
len = unorm2_getRawDecomposition (normalizer, ab, decomposed,
|
|
|
|
ARRAY_LENGTH (decomposed), &icu_err);
|
|
|
|
if (U_FAILURE (icu_err) || len < 0) return false;
|
|
|
|
|
|
|
|
len = u_countChar32 (decomposed, len);
|
|
|
|
if (len == 1)
|
2012-08-12 03:26:25 +02:00
|
|
|
{
|
2020-07-29 22:23:40 +02:00
|
|
|
U16_GET_UNSAFE (decomposed, 0, *a);
|
2011-07-21 05:19:49 +02:00
|
|
|
*b = 0;
|
2020-07-29 22:23:40 +02:00
|
|
|
return *a != ab;
|
|
|
|
}
|
|
|
|
else if (len == 2)
|
|
|
|
{
|
2019-09-26 19:45:59 +02:00
|
|
|
len = 0;
|
2020-07-29 22:23:40 +02:00
|
|
|
U16_NEXT_UNSAFE (decomposed, len, *a);
|
|
|
|
U16_NEXT_UNSAFE (decomposed, len, *b);
|
2011-07-21 05:19:49 +02:00
|
|
|
}
|
2020-07-29 22:23:40 +02:00
|
|
|
return true;
|
2011-07-21 04:16:13 +02:00
|
|
|
}
|
|
|
|
|
2012-08-01 03:36:16 +02:00
|
|
|
|
2018-11-21 18:32:48 +01:00
|
|
|
#if HB_USE_ATEXIT
|
2018-12-17 19:01:01 +01:00
|
|
|
static void free_static_icu_funcs ();
|
2018-08-14 22:50:24 +02:00
|
|
|
#endif
|
2017-10-27 23:26:45 +02:00
|
|
|
|
2018-08-12 22:39:01 +02:00
|
|
|
static struct hb_icu_unicode_funcs_lazy_loader_t : hb_unicode_funcs_lazy_loader_t<hb_icu_unicode_funcs_lazy_loader_t>
|
2012-08-12 03:26:25 +02:00
|
|
|
{
|
2018-12-17 19:01:01 +01:00
|
|
|
static hb_unicode_funcs_t *create ()
|
2017-10-27 23:26:45 +02:00
|
|
|
{
|
2018-08-14 02:02:21 +02:00
|
|
|
void *user_data = nullptr;
|
|
|
|
UErrorCode icu_err = U_ZERO_ERROR;
|
|
|
|
user_data = (void *) unorm2_getNFCInstance (&icu_err);
|
|
|
|
assert (user_data);
|
2018-08-13 01:29:41 +02:00
|
|
|
|
2018-08-12 22:39:01 +02:00
|
|
|
hb_unicode_funcs_t *funcs = hb_unicode_funcs_create (nullptr);
|
2017-10-27 23:26:45 +02:00
|
|
|
|
2018-10-09 06:56:33 +02:00
|
|
|
hb_unicode_funcs_set_combining_class_func (funcs, hb_icu_unicode_combining_class, nullptr, nullptr);
|
|
|
|
hb_unicode_funcs_set_general_category_func (funcs, hb_icu_unicode_general_category, nullptr, nullptr);
|
|
|
|
hb_unicode_funcs_set_mirroring_func (funcs, hb_icu_unicode_mirroring, nullptr, nullptr);
|
|
|
|
hb_unicode_funcs_set_script_func (funcs, hb_icu_unicode_script, nullptr, nullptr);
|
2018-10-09 07:07:36 +02:00
|
|
|
hb_unicode_funcs_set_compose_func (funcs, hb_icu_unicode_compose, user_data, nullptr);
|
|
|
|
hb_unicode_funcs_set_decompose_func (funcs, hb_icu_unicode_decompose, user_data, nullptr);
|
2017-10-27 23:26:45 +02:00
|
|
|
|
|
|
|
hb_unicode_funcs_make_immutable (funcs);
|
|
|
|
|
2018-11-21 18:32:48 +01:00
|
|
|
#if HB_USE_ATEXIT
|
2018-08-12 22:39:01 +02:00
|
|
|
atexit (free_static_icu_funcs);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
return funcs;
|
|
|
|
}
|
|
|
|
} static_icu_funcs;
|
2009-11-03 22:35:10 +01:00
|
|
|
|
2018-11-21 18:32:48 +01:00
|
|
|
#if HB_USE_ATEXIT
|
2018-08-12 22:39:01 +02:00
|
|
|
static
|
2018-12-17 19:01:01 +01:00
|
|
|
void free_static_icu_funcs ()
|
2018-08-12 22:39:01 +02:00
|
|
|
{
|
2018-08-13 02:19:55 +02:00
|
|
|
static_icu_funcs.free_instance ();
|
2018-08-12 22:39:01 +02:00
|
|
|
}
|
2012-08-12 03:26:25 +02:00
|
|
|
#endif
|
2017-10-27 23:26:45 +02:00
|
|
|
|
2019-05-20 18:38:13 +02:00
|
|
|
/**
|
|
|
|
* hb_icu_get_unicode_funcs:
|
|
|
|
*
|
|
|
|
* Fetches a Unicode-functions structure that is populated
|
|
|
|
* with the appropriate ICU function for each method.
|
|
|
|
*
|
2020-04-13 16:14:06 +02:00
|
|
|
* Return value: (transfer none): a pointer to the #hb_unicode_funcs_t Unicode-functions structure
|
2019-05-20 18:38:13 +02:00
|
|
|
*
|
|
|
|
* Since: 0.9.38
|
|
|
|
**/
|
2018-08-12 22:39:01 +02:00
|
|
|
hb_unicode_funcs_t *
|
2018-12-17 19:01:01 +01:00
|
|
|
hb_icu_get_unicode_funcs ()
|
2018-08-12 22:39:01 +02:00
|
|
|
{
|
2018-08-13 19:30:41 +02:00
|
|
|
return static_icu_funcs.get_unconst ();
|
2009-11-03 22:35:10 +01:00
|
|
|
}
|
2019-06-18 07:41:49 +02:00
|
|
|
|
2019-09-26 19:45:59 +02:00
|
|
|
#ifdef HB_ICU_EXTRA_SEMI_IGNORED
|
|
|
|
#pragma GCC diagnostic pop
|
|
|
|
#endif
|
2019-06-18 07:41:49 +02:00
|
|
|
|
|
|
|
#endif
|