hb-unicode.cc 7.2 KB
Newer Older
B
Behdad Esfahbod 已提交
1
/*
B
Behdad Esfahbod 已提交
2
 * Copyright © 2009  Red Hat, Inc.
3
 * Copyright © 2011 Codethink Limited
B
Behdad Esfahbod 已提交
4
 * Copyright © 2010,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
27
 * Codethink Author(s): Ryan Lortie
28
 * Google Author(s): Behdad Esfahbod
B
Behdad Esfahbod 已提交
29 30
 */

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

33
#include "hb-unicode-private.hh"
B
Behdad Esfahbod 已提交
34

B
Behdad Esfahbod 已提交
35 36 37
HB_BEGIN_DECLS


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

42 43 44 45
static unsigned int
hb_unicode_get_combining_class_nil (hb_unicode_funcs_t *ufuncs    HB_UNUSED,
				    hb_codepoint_t      unicode   HB_UNUSED,
				    void               *user_data HB_UNUSED)
46
{
47 48 49 50 51 52 53 54 55
  return 0;
}

static unsigned int
hb_unicode_get_eastasian_width_nil (hb_unicode_funcs_t *ufuncs    HB_UNUSED,
				    hb_codepoint_t      unicode   HB_UNUSED,
				    void               *user_data HB_UNUSED)
{
  return 1;
56 57 58 59
}

static hb_unicode_general_category_t
hb_unicode_get_general_category_nil (hb_unicode_funcs_t *ufuncs    HB_UNUSED,
60 61
				     hb_codepoint_t      unicode   HB_UNUSED,
				     void               *user_data HB_UNUSED)
62 63 64 65
{
  return HB_UNICODE_GENERAL_CATEGORY_OTHER_LETTER;
}

66 67 68 69
static hb_codepoint_t
hb_unicode_get_mirroring_nil (hb_unicode_funcs_t *ufuncs    HB_UNUSED,
			      hb_codepoint_t      unicode   HB_UNUSED,
			      void               *user_data HB_UNUSED)
70
{
71
  return unicode;
72 73
}

74 75 76 77
static hb_script_t
hb_unicode_get_script_nil (hb_unicode_funcs_t *ufuncs    HB_UNUSED,
			   hb_codepoint_t      unicode   HB_UNUSED,
			   void               *user_data HB_UNUSED)
78
{
79
  return HB_SCRIPT_UNKNOWN;
80 81
}

82

83
hb_unicode_funcs_t _hb_unicode_funcs_nil = {
B
Behdad Esfahbod 已提交
84
  HB_REFERENCE_COUNT_INVALID, /* ref_count */
85
  NULL, /* parent */
86
  TRUE, /* immutable */
87
  {
88 89 90 91 92
    hb_unicode_get_combining_class_nil,
    hb_unicode_get_eastasian_width_nil,
    hb_unicode_get_general_category_nil,
    hb_unicode_get_mirroring_nil,
    hb_unicode_get_script_nil,
93
  }
B
Behdad Esfahbod 已提交
94 95
};

96

B
Behdad Esfahbod 已提交
97
hb_unicode_funcs_t *
98
hb_unicode_funcs_create (hb_unicode_funcs_t *parent)
B
Behdad Esfahbod 已提交
99 100 101 102 103 104
{
  hb_unicode_funcs_t *ufuncs;

  if (!HB_OBJECT_DO_CREATE (hb_unicode_funcs_t, ufuncs))
    return &_hb_unicode_funcs_nil;

105 106
  if (parent != NULL)
  {
107
    hb_unicode_funcs_make_immutable (parent);
108 109 110 111 112 113 114 115 116 117 118 119
    ufuncs->parent = hb_unicode_funcs_reference (parent);

    ufuncs->get = parent->get;

    /* We can safely copy user_data from parent since we hold a reference
     * onto it and it's immutable.  We should not copy the destroy notifiers
     * though. */
    ufuncs->user_data = parent->user_data;
  }
  else
  {
    ufuncs->get = _hb_unicode_funcs_nil.get;
120
  }
121

B
Behdad Esfahbod 已提交
122 123 124 125 126 127 128 129 130 131 132 133 134 135
  return ufuncs;
}

hb_unicode_funcs_t *
hb_unicode_funcs_reference (hb_unicode_funcs_t *ufuncs)
{
  HB_OBJECT_DO_REFERENCE (ufuncs);
}

void
hb_unicode_funcs_destroy (hb_unicode_funcs_t *ufuncs)
{
  HB_OBJECT_DO_DESTROY (ufuncs);

136 137 138 139 140 141 142 143
#define DESTROY(name) if (ufuncs->destroy.name) ufuncs->destroy.name (ufuncs->user_data.name)
  DESTROY (combining_class);
  DESTROY (eastasian_width);
  DESTROY (general_category);
  DESTROY (mirroring);
  DESTROY (script);
#undef DESTROY

144 145 146
  if (ufuncs->parent != NULL)
    hb_unicode_funcs_destroy (ufuncs->parent);

B
Behdad Esfahbod 已提交
147 148 149
  free (ufuncs);
}

150 151 152 153 154 155 156 157 158
void
hb_unicode_funcs_make_immutable (hb_unicode_funcs_t *ufuncs)
{
  if (HB_OBJECT_IS_INERT (ufuncs))
    return;

  ufuncs->immutable = TRUE;
}

B
Behdad Esfahbod 已提交
159 160 161 162 163 164
hb_bool_t
hb_unicode_funcs_is_immutable (hb_unicode_funcs_t *ufuncs)
{
  return ufuncs->immutable;
}

165 166 167 168 169 170
hb_unicode_funcs_t *
hb_unicode_funcs_get_parent (hb_unicode_funcs_t *ufuncs)
{
  return ufuncs->parent;
}

B
Behdad Esfahbod 已提交
171

172 173
#define IMPLEMENT(return_type, name)                                           \
                                                                               \
174 175 176 177 178 179 180 181 182
void                                                                           \
hb_unicode_funcs_set_##name##_func (hb_unicode_funcs_t             *ufuncs,    \
                                    hb_unicode_get_##name##_func_t  func,      \
                                    void                           *user_data, \
                                    hb_destroy_func_t               destroy)   \
{                                                                              \
  if (ufuncs->immutable)                                                       \
    return;                                                                    \
                                                                               \
183 184 185 186 187 188 189
  if (ufuncs->destroy.name)                                                    \
    ufuncs->destroy.name (ufuncs->user_data.name);                             \
                                                                               \
  if (func) {                                                                  \
    ufuncs->get.name = func;                                                   \
    ufuncs->user_data.name = user_data;                                        \
    ufuncs->destroy.name = destroy;                                            \
190
  } else if (ufuncs->parent != NULL) {                                         \
191 192 193
    ufuncs->get.name = ufuncs->parent->get.name;                               \
    ufuncs->user_data.name = ufuncs->parent->user_data.name;                   \
    ufuncs->destroy.name = NULL;                                               \
194
  } else {                                                                     \
195 196 197
    ufuncs->get.name = hb_unicode_get_##name##_nil;                            \
    ufuncs->user_data.name = NULL;                                             \
    ufuncs->destroy.name = NULL;                                               \
198
  }                                                                            \
199 200 201 202 203 204 205
}                                                                              \
                                                                               \
return_type                                                                    \
hb_unicode_get_##name (hb_unicode_funcs_t *ufuncs,                             \
		       hb_codepoint_t      unicode)                            \
{                                                                              \
  return ufuncs->get.name (ufuncs, unicode, ufuncs->user_data.name);           \
B
Behdad Esfahbod 已提交
206 207
}

208 209 210 211 212
IMPLEMENT (unsigned int, combining_class)
IMPLEMENT (unsigned int, eastasian_width)
IMPLEMENT (hb_unicode_general_category_t, general_category)
IMPLEMENT (hb_codepoint_t, mirroring)
IMPLEMENT (hb_script_t, script)
B
Behdad Esfahbod 已提交
213

214
#undef IMPLEMENT
B
Behdad Esfahbod 已提交
215 216


B
Behdad Esfahbod 已提交
217
HB_END_DECLS