hb-icu.cc 6.2 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"
B
Behdad Esfahbod 已提交
35

B
Behdad Esfahbod 已提交
36
#include <unicode/uversion.h>
B
Behdad Esfahbod 已提交
37 38
#include <unicode/uchar.h>

B
Behdad Esfahbod 已提交
39 40 41
HB_BEGIN_DECLS


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

48
  return hb_script_from_string (uscript_getShortName (script));
B
Behdad Esfahbod 已提交
49 50
}

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

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

61 62 63 64 65
  return USCRIPT_UNKNOWN;
}


static unsigned int
66
hb_icu_get_combining_class (hb_unicode_funcs_t *ufuncs HB_UNUSED,
67
			    hb_codepoint_t      unicode,
68
			    void               *user_data HB_UNUSED)
69 70 71 72 73 74

{
  return u_getCombiningClass (unicode);
}

static unsigned int
75
hb_icu_get_eastasian_width (hb_unicode_funcs_t *ufuncs HB_UNUSED,
76
			    hb_codepoint_t      unicode,
77
			    void               *user_data HB_UNUSED)
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
{
  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
94
hb_icu_get_general_category (hb_unicode_funcs_t *ufuncs HB_UNUSED,
95
			     hb_codepoint_t      unicode,
96
			     void               *user_data HB_UNUSED)
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 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
{
  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_COMBINING_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
145
hb_icu_get_mirroring (hb_unicode_funcs_t *ufuncs HB_UNUSED,
146
		      hb_codepoint_t      unicode,
147
		      void               *user_data HB_UNUSED)
148 149 150 151 152
{
  return u_charMirror(unicode);
}

static hb_script_t
153
hb_icu_get_script (hb_unicode_funcs_t *ufuncs HB_UNUSED,
154
		   hb_codepoint_t      unicode,
155
		   void               *user_data HB_UNUSED)
156 157 158 159
{
  UErrorCode status = U_ZERO_ERROR;
  UScriptCode scriptCode = uscript_getScript(unicode, &status);

160 161 162
  if (unlikely (status != U_ZERO_ERROR))
    return HB_SCRIPT_UNKNOWN;

163
  return hb_icu_script_to_script (scriptCode);
B
Behdad Esfahbod 已提交
164 165
}

166 167
extern HB_INTERNAL hb_unicode_funcs_t _hb_unicode_funcs_icu;
hb_unicode_funcs_t _hb_icu_unicode_funcs = {
168 169
  HB_OBJECT_HEADER_STATIC,

170
  NULL, /* parent */
B
Behdad Esfahbod 已提交
171
  TRUE, /* immutable */
B
Behdad Esfahbod 已提交
172
  {
173 174 175 176
    hb_icu_get_combining_class,
    hb_icu_get_eastasian_width,
    hb_icu_get_general_category,
    hb_icu_get_mirroring,
177 178 179 180 181
    hb_icu_get_script,
    /* TODO
    hb_icu_compose,
    hb_icu_decompose,
    */
B
Behdad Esfahbod 已提交
182
  }
B
Behdad Esfahbod 已提交
183 184 185 186 187
};

hb_unicode_funcs_t *
hb_icu_get_unicode_funcs (void)
{
188
  return &_hb_icu_unicode_funcs;
B
Behdad Esfahbod 已提交
189
}
B
Behdad Esfahbod 已提交
190 191 192


HB_END_DECLS