hb-unicode.cc 6.0 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
 */

B
Behdad Esfahbod 已提交
42 43 44 45 46 47
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)
{
  return 0;
48 49
}

B
Behdad Esfahbod 已提交
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
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;
}

static hb_unicode_general_category_t
hb_unicode_get_general_category_nil (hb_unicode_funcs_t *ufuncs    HB_UNUSED,
				     hb_codepoint_t      unicode   HB_UNUSED,
				     void               *user_data HB_UNUSED)
{
  return HB_UNICODE_GENERAL_CATEGORY_OTHER_LETTER;
}

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)
{
  return unicode;
}

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)
{
  return HB_SCRIPT_UNKNOWN;
}
81

82

83
hb_unicode_funcs_t _hb_unicode_funcs_nil = {
84 85
  HB_OBJECT_HEADER_STATIC,

86
  NULL, /* parent */
87
  TRUE, /* immutable */
88
  {
B
Behdad Esfahbod 已提交
89
#define HB_UNICODE_FUNC_IMPLEMENT(name) hb_unicode_get_##name##_nil,
B
Behdad Esfahbod 已提交
90 91
    HB_UNICODE_FUNCS_IMPLEMENT_CALLBACKS
#undef HB_UNICODE_FUNC_IMPLEMENT
92
  }
B
Behdad Esfahbod 已提交
93 94
};

95

96 97 98 99 100 101
hb_unicode_funcs_t *
hb_unicode_funcs_get_default (void)
{
  return &_hb_unicode_funcs_default;
}

B
Behdad Esfahbod 已提交
102
hb_unicode_funcs_t *
103
hb_unicode_funcs_create (hb_unicode_funcs_t *parent)
B
Behdad Esfahbod 已提交
104 105 106
{
  hb_unicode_funcs_t *ufuncs;

107
  if (!(ufuncs = hb_object_create<hb_unicode_funcs_t> ()))
B
Behdad Esfahbod 已提交
108 109
    return &_hb_unicode_funcs_nil;

110 111
  if (!parent)
    parent = &_hb_unicode_funcs_nil;
112

113 114
  hb_unicode_funcs_make_immutable (parent);
  ufuncs->parent = hb_unicode_funcs_reference (parent);
115

116 117 118 119 120 121
  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;
122

B
Behdad Esfahbod 已提交
123 124 125
  return ufuncs;
}

126 127 128 129 130 131
hb_unicode_funcs_t *
hb_unicode_funcs_get_empty (void)
{
  return &_hb_unicode_funcs_nil;
}

B
Behdad Esfahbod 已提交
132 133 134
hb_unicode_funcs_t *
hb_unicode_funcs_reference (hb_unicode_funcs_t *ufuncs)
{
135
  return hb_object_reference (ufuncs);
B
Behdad Esfahbod 已提交
136 137 138 139 140
}

void
hb_unicode_funcs_destroy (hb_unicode_funcs_t *ufuncs)
{
141
  if (!hb_object_destroy (ufuncs)) return;
B
Behdad Esfahbod 已提交
142

B
Behdad Esfahbod 已提交
143
#define HB_UNICODE_FUNC_IMPLEMENT(name) \
B
Behdad Esfahbod 已提交
144 145 146
  if (ufuncs->destroy.name) ufuncs->destroy.name (ufuncs->user_data.name);
    HB_UNICODE_FUNCS_IMPLEMENT_CALLBACKS
#undef HB_UNICODE_FUNC_IMPLEMENT
147

148
  hb_unicode_funcs_destroy (ufuncs->parent);
149

B
Behdad Esfahbod 已提交
150 151 152
  free (ufuncs);
}

153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169
hb_bool_t
hb_unicode_funcs_set_user_data (hb_unicode_funcs_t *ufuncs,
			        hb_user_data_key_t *key,
			        void *              data,
			        hb_destroy_func_t   destroy)
{
  return hb_object_set_user_data (ufuncs, key, data, destroy);
}

void *
hb_unicode_funcs_get_user_data (hb_unicode_funcs_t *ufuncs,
			        hb_user_data_key_t *key)
{
  return hb_object_get_user_data (ufuncs, key);
}


170 171 172
void
hb_unicode_funcs_make_immutable (hb_unicode_funcs_t *ufuncs)
{
173
  if (hb_object_is_inert (ufuncs))
174 175 176 177 178
    return;

  ufuncs->immutable = TRUE;
}

B
Behdad Esfahbod 已提交
179 180 181 182 183 184
hb_bool_t
hb_unicode_funcs_is_immutable (hb_unicode_funcs_t *ufuncs)
{
  return ufuncs->immutable;
}

185 186 187
hb_unicode_funcs_t *
hb_unicode_funcs_get_parent (hb_unicode_funcs_t *ufuncs)
{
188
  return ufuncs->parent ? ufuncs->parent : &_hb_unicode_funcs_nil;
189 190
}

B
Behdad Esfahbod 已提交
191

B
Behdad Esfahbod 已提交
192
#define HB_UNICODE_FUNC_IMPLEMENT(name)						\
B
Behdad Esfahbod 已提交
193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214
										\
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;									\
										\
  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;						\
  } else {									\
    ufuncs->get.name = ufuncs->parent->get.name;				\
    ufuncs->user_data.name = ufuncs->parent->user_data.name;			\
    ufuncs->destroy.name = NULL;						\
  }										\
B
Behdad Esfahbod 已提交
215 216 217 218 219 220 221
}

    HB_UNICODE_FUNCS_IMPLEMENT_CALLBACKS
#undef HB_UNICODE_FUNC_IMPLEMENT


#define HB_UNICODE_FUNC_IMPLEMENT(return_type, name)				\
B
Behdad Esfahbod 已提交
222 223 224 225 226 227 228
										\
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 已提交
229
    HB_UNICODE_FUNCS_IMPLEMENT_CALLBACKS_SIMPLE
B
Behdad Esfahbod 已提交
230
#undef HB_UNICODE_FUNC_IMPLEMENT
B
Behdad Esfahbod 已提交
231 232


B
Behdad Esfahbod 已提交
233
HB_END_DECLS