2 * Copyright © 2009 Red Hat, Inc.
4 * This is part of HarfBuzz, a text shaping library.
6 * Permission is hereby granted, without written agreement and without
7 * license or royalty fees, to use, copy, modify, and distribute this
8 * software and its documentation for any purpose, provided that the
9 * above copyright notice and the following two paragraphs appear in
10 * all copies of this software.
12 * IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE TO ANY PARTY FOR
13 * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
14 * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN
15 * IF THE COPYRIGHT HOLDER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH
18 * THE COPYRIGHT HOLDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING,
19 * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
20 * FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS
21 * ON AN "AS IS" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO
22 * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
24 * Red Hat Author(s): Behdad Esfahbod
27 #include "hb-private.hh"
29 #include "hb-ot-layout-private.hh"
31 #include "hb-font-private.hh"
33 #include "hb-open-file-private.hh"
45 hb_font_get_glyph_nil (hb_font_t *font HB_UNUSED,
46 const void *user_data HB_UNUSED,
47 hb_codepoint_t unicode HB_UNUSED,
48 hb_codepoint_t variation_selector HB_UNUSED)
52 hb_font_get_glyph_advance_nil (hb_font_t *font HB_UNUSED,
53 const void *user_data HB_UNUSED,
54 hb_codepoint_t glyph HB_UNUSED,
55 hb_position_t *x_advance HB_UNUSED,
56 hb_position_t *y_advance HB_UNUSED)
60 hb_font_get_glyph_extents_nil (hb_font_t *font HB_UNUSED,
61 const void *user_data HB_UNUSED,
62 hb_codepoint_t glyph HB_UNUSED,
63 hb_glyph_extents_t *extents HB_UNUSED)
67 hb_font_get_contour_point_nil (hb_font_t *font HB_UNUSED,
68 const void *user_data HB_UNUSED,
69 unsigned int point_index HB_UNUSED,
70 hb_codepoint_t glyph HB_UNUSED,
71 hb_position_t *x HB_UNUSED,
72 hb_position_t *y HB_UNUSED)
76 hb_font_get_kerning_nil (hb_font_t *font HB_UNUSED,
77 const void *user_data HB_UNUSED,
78 hb_codepoint_t first_glyph HB_UNUSED,
79 hb_codepoint_t second_glyph HB_UNUSED)
83 static hb_font_funcs_t _hb_font_funcs_nil = {
84 HB_OBJECT_HEADER_STATIC,
88 hb_font_get_glyph_nil,
89 hb_font_get_glyph_advance_nil,
90 hb_font_get_glyph_extents_nil,
91 hb_font_get_contour_point_nil,
92 hb_font_get_kerning_nil
98 hb_font_funcs_create (void)
100 hb_font_funcs_t *ffuncs;
102 if (!(ffuncs = hb_object_create<hb_font_funcs_t> ()))
103 return &_hb_font_funcs_nil;
105 ffuncs->v = _hb_font_funcs_nil.v;
111 hb_font_funcs_reference (hb_font_funcs_t *ffuncs)
113 return hb_object_reference (ffuncs);
117 hb_font_funcs_destroy (hb_font_funcs_t *ffuncs)
119 if (!hb_object_destroy (ffuncs)) return;
125 hb_font_funcs_set_user_data (hb_font_funcs_t *ffuncs,
126 hb_user_data_key_t *key,
128 hb_destroy_func_t destroy)
130 return hb_object_set_user_data (ffuncs, key, data, destroy);
134 hb_font_funcs_get_user_data (hb_font_funcs_t *ffuncs,
135 hb_user_data_key_t *key)
137 return hb_object_get_user_data (ffuncs, key);
142 hb_font_funcs_make_immutable (hb_font_funcs_t *ffuncs)
144 if (hb_object_is_inert (ffuncs))
147 ffuncs->immutable = TRUE;
151 hb_font_funcs_is_immutable (hb_font_funcs_t *ffuncs)
153 return ffuncs->immutable;
158 hb_font_funcs_set_glyph_func (hb_font_funcs_t *ffuncs,
159 hb_font_get_glyph_func_t glyph_func)
161 if (ffuncs->immutable)
164 ffuncs->v.get_glyph = glyph_func ? glyph_func : hb_font_get_glyph_nil;
168 hb_font_funcs_set_glyph_advance_func (hb_font_funcs_t *ffuncs,
169 hb_font_get_glyph_advance_func_t glyph_advance_func)
171 if (ffuncs->immutable)
174 ffuncs->v.get_glyph_advance = glyph_advance_func ? glyph_advance_func : hb_font_get_glyph_advance_nil;
178 hb_font_funcs_set_glyph_extents_func (hb_font_funcs_t *ffuncs,
179 hb_font_get_glyph_extents_func_t glyph_extents_func)
181 if (ffuncs->immutable)
184 ffuncs->v.get_glyph_extents = glyph_extents_func ? glyph_extents_func : hb_font_get_glyph_extents_nil;
188 hb_font_funcs_set_contour_point_func (hb_font_funcs_t *ffuncs,
189 hb_font_get_contour_point_func_t contour_point_func)
191 if (ffuncs->immutable)
194 ffuncs->v.get_contour_point = contour_point_func ? contour_point_func : hb_font_get_contour_point_nil;
198 hb_font_funcs_set_kerning_func (hb_font_funcs_t *ffuncs,
199 hb_font_get_kerning_func_t kerning_func)
201 if (ffuncs->immutable)
204 ffuncs->v.get_kerning = kerning_func ? kerning_func : hb_font_get_kerning_nil;
208 hb_font_get_glyph_func_t
209 hb_font_funcs_get_glyph_func (hb_font_funcs_t *ffuncs)
211 return ffuncs->v.get_glyph;
214 hb_font_get_glyph_advance_func_t
215 hb_font_funcs_get_glyph_advance_func (hb_font_funcs_t *ffuncs)
217 return ffuncs->v.get_glyph_advance;
220 hb_font_get_glyph_extents_func_t
221 hb_font_funcs_get_glyph_extents_func (hb_font_funcs_t *ffuncs)
223 return ffuncs->v.get_glyph_extents;
226 hb_font_get_contour_point_func_t
227 hb_font_funcs_get_contour_point_func (hb_font_funcs_t *ffuncs)
229 return ffuncs->v.get_contour_point;
232 hb_font_get_kerning_func_t
233 hb_font_funcs_get_kerning_func (hb_font_funcs_t *ffuncs)
235 return ffuncs->v.get_kerning;
241 hb_font_get_glyph (hb_font_t *font,
242 hb_codepoint_t unicode, hb_codepoint_t variation_selector)
244 return font->klass->v.get_glyph (font, font->user_data,
245 unicode, variation_selector);
249 hb_font_get_glyph_advance (hb_font_t *font,
250 hb_codepoint_t glyph,
251 hb_position_t *x_advance, hb_position_t *y_advance)
253 *x_advance = *y_advance = 0;
254 return font->klass->v.get_glyph_advance (font, font->user_data,
255 glyph, x_advance, y_advance);
259 hb_font_get_glyph_extents (hb_font_t *font,
260 hb_codepoint_t glyph, hb_glyph_extents_t *extents)
262 memset (extents, 0, sizeof (*extents));
263 return font->klass->v.get_glyph_extents (font, font->user_data,
268 hb_font_get_contour_point (hb_font_t *font,
269 unsigned int point_index,
270 hb_codepoint_t glyph, hb_position_t *x, hb_position_t *y)
273 return font->klass->v.get_contour_point (font, font->user_data,
279 hb_font_get_kerning (hb_font_t *font,
280 hb_codepoint_t first_glyph, hb_codepoint_t second_glyph)
282 return font->klass->v.get_kerning (font, font->user_data,
283 first_glyph, second_glyph);
291 static hb_face_t _hb_face_nil = {
292 HB_OBJECT_HEADER_STATIC,
294 NULL, /* get_table */
295 NULL, /* user_data */
303 hb_face_create_for_tables (hb_get_table_func_t get_table,
305 hb_destroy_func_t destroy)
309 if (!get_table || !(face = hb_object_create<hb_face_t> ())) {
312 return &_hb_face_nil;
315 face->get_table = get_table;
316 face->user_data = user_data;
317 face->destroy = destroy;
319 face->ot_layout = _hb_ot_layout_create (face);
325 typedef struct _hb_face_for_data_closure_t {
328 } hb_face_for_data_closure_t;
330 static hb_face_for_data_closure_t *
331 _hb_face_for_data_closure_create (hb_blob_t *blob, unsigned int index)
333 hb_face_for_data_closure_t *closure;
335 closure = (hb_face_for_data_closure_t *) malloc (sizeof (hb_face_for_data_closure_t));
336 if (unlikely (!closure))
339 closure->blob = blob;
340 closure->index = index;
346 _hb_face_for_data_closure_destroy (hb_face_for_data_closure_t *closure)
348 hb_blob_destroy (closure->blob);
353 _hb_face_for_data_get_table (hb_tag_t tag, void *user_data)
355 hb_face_for_data_closure_t *data = (hb_face_for_data_closure_t *) user_data;
357 const OpenTypeFontFile &ot_file = *Sanitizer<OpenTypeFontFile>::lock_instance (data->blob);
358 const OpenTypeFontFace &ot_face = ot_file.get_face (data->index);
360 const OpenTypeTable &table = ot_face.get_table_by_tag (tag);
362 hb_blob_t *blob = hb_blob_create_sub_blob (data->blob, table.offset, table.length);
364 hb_blob_unlock (data->blob);
370 hb_face_create_for_data (hb_blob_t *blob,
373 if (unlikely (!blob || !hb_blob_get_length (blob)))
374 return &_hb_face_nil;
376 hb_face_for_data_closure_t *closure = _hb_face_for_data_closure_create (Sanitizer<OpenTypeFontFile>::sanitize (hb_blob_reference (blob)), index);
378 if (unlikely (!closure))
379 return &_hb_face_nil;
381 return hb_face_create_for_tables (_hb_face_for_data_get_table,
383 (hb_destroy_func_t) _hb_face_for_data_closure_destroy);
388 hb_face_reference (hb_face_t *face)
390 return hb_object_reference (face);
394 hb_face_destroy (hb_face_t *face)
396 if (!hb_object_destroy (face)) return;
398 _hb_ot_layout_destroy (face->ot_layout);
401 face->destroy (face->user_data);
407 hb_face_set_user_data (hb_face_t *face,
408 hb_user_data_key_t *key,
410 hb_destroy_func_t destroy)
412 return hb_object_set_user_data (face, key, data, destroy);
416 hb_face_get_user_data (hb_face_t *face,
417 hb_user_data_key_t *key)
419 return hb_object_get_user_data (face, key);
424 hb_face_reference_table (hb_face_t *face,
429 if (unlikely (!face || !face->get_table))
430 return hb_blob_get_empty ();
432 blob = face->get_table (tag, face->user_data);
433 if (unlikely (!blob))
434 return hb_blob_get_empty ();
440 hb_face_get_upem (hb_face_t *face)
442 return _hb_ot_layout_get_upem (face);
450 static hb_font_t _hb_font_nil = {
451 HB_OBJECT_HEADER_STATIC,
462 NULL, /* user_data */
467 hb_font_create (hb_face_t *face)
471 if (unlikely (!face))
472 face = &_hb_face_nil;
473 if (unlikely (hb_object_is_inert (face)))
474 return &_hb_font_nil;
475 if (!(font = hb_object_create<hb_font_t> ()))
476 return &_hb_font_nil;
478 font->face = hb_face_reference (face);
479 font->klass = &_hb_font_funcs_nil;
485 hb_font_reference (hb_font_t *font)
487 return hb_object_reference (font);
491 hb_font_destroy (hb_font_t *font)
493 if (!hb_object_destroy (font)) return;
495 hb_face_destroy (font->face);
496 hb_font_funcs_destroy (font->klass);
498 font->destroy (font->user_data);
504 hb_font_set_user_data (hb_font_t *font,
505 hb_user_data_key_t *key,
507 hb_destroy_func_t destroy)
509 return hb_object_set_user_data (font, key, data, destroy);
513 hb_font_get_user_data (hb_font_t *font,
514 hb_user_data_key_t *key)
516 return hb_object_get_user_data (font, key);
521 hb_font_get_face (hb_font_t *font)
528 hb_font_set_funcs (hb_font_t *font,
529 hb_font_funcs_t *klass,
531 hb_destroy_func_t destroy)
533 if (hb_object_is_inert (font))
537 font->destroy (font->user_data);
540 klass = &_hb_font_funcs_nil;
542 hb_font_funcs_reference (klass);
543 hb_font_funcs_destroy (font->klass);
545 font->user_data = user_data;
546 font->destroy = destroy;
550 hb_font_unset_funcs (hb_font_t *font,
551 hb_font_funcs_t **klass,
553 hb_destroy_func_t *destroy)
555 /* None of the input arguments can be NULL. */
557 *klass = font->klass;
558 *user_data = font->user_data;
559 *destroy = font->destroy;
561 if (hb_object_is_inert (font))
565 font->user_data = NULL;
566 font->destroy = NULL;
570 hb_font_set_scale (hb_font_t *font,
574 if (hb_object_is_inert (font))
577 font->x_scale = x_scale;
578 font->y_scale = y_scale;
582 hb_font_get_scale (hb_font_t *font,
586 if (x_scale) *x_scale = font->x_scale;
587 if (y_scale) *y_scale = font->y_scale;
591 hb_font_set_ppem (hb_font_t *font,
595 if (hb_object_is_inert (font))
598 font->x_ppem = x_ppem;
599 font->y_ppem = y_ppem;
603 hb_font_get_ppem (hb_font_t *font,
604 unsigned int *x_ppem,
605 unsigned int *y_ppem)
607 if (x_ppem) *x_ppem = font->x_ppem;
608 if (y_ppem) *y_ppem = font->y_ppem;