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_contour_point_nil (hb_font_t *font HB_UNUSED,
46 void *font_data HB_UNUSED,
48 unsigned int point_index,
51 void *user_data HB_UNUSED)
55 ret = hb_font_get_contour_point (font->parent,
58 font->parent_scale_position (x, y);
68 hb_font_get_glyph_advance_nil (hb_font_t *font HB_UNUSED,
69 void *font_data HB_UNUSED,
71 hb_position_t *x_advance,
72 hb_position_t *y_advance,
73 void *user_data HB_UNUSED)
76 hb_font_get_glyph_advance (font->parent, glyph, x_advance, y_advance);
77 font->parent_scale_distance (x_advance, y_advance);
81 *x_advance = *y_advance = 0;
85 hb_font_get_glyph_extents_nil (hb_font_t *font HB_UNUSED,
86 void *font_data HB_UNUSED,
88 hb_glyph_extents_t *extents,
89 void *user_data HB_UNUSED)
92 hb_font_get_glyph_extents (font->parent, glyph, extents);
93 font->parent_scale_position (&extents->x_bearing, &extents->y_bearing);
94 font->parent_scale_distance (&extents->width, &extents->height);
98 extents->x_bearing = extents->y_bearing = 0;
99 extents->width = extents->height = 0;
103 hb_font_get_glyph_nil (hb_font_t *font HB_UNUSED,
104 void *font_data HB_UNUSED,
105 hb_codepoint_t unicode,
106 hb_codepoint_t variation_selector,
107 hb_codepoint_t *glyph,
108 void *user_data HB_UNUSED)
111 return hb_font_get_glyph (font->parent, unicode, variation_selector, glyph);
118 hb_font_get_kerning_nil (hb_font_t *font HB_UNUSED,
119 void *font_data HB_UNUSED,
120 hb_codepoint_t left_glyph,
121 hb_codepoint_t right_glyph,
122 hb_position_t *x_kern,
123 hb_position_t *y_kern,
124 void *user_data HB_UNUSED)
127 hb_font_get_kerning (font->parent, left_glyph, right_glyph, x_kern, y_kern);
128 font->parent_scale_distance (x_kern, y_kern);
132 *x_kern = *y_kern = 0;
136 static hb_font_funcs_t _hb_font_funcs_nil = {
137 HB_OBJECT_HEADER_STATIC,
139 TRUE, /* immutable */
142 hb_font_get_contour_point_nil,
143 hb_font_get_glyph_advance_nil,
144 hb_font_get_glyph_extents_nil,
145 hb_font_get_glyph_nil,
146 hb_font_get_kerning_nil
152 hb_font_funcs_create (void)
154 hb_font_funcs_t *ffuncs;
156 if (!(ffuncs = hb_object_create<hb_font_funcs_t> ()))
157 return &_hb_font_funcs_nil;
159 ffuncs->get = _hb_font_funcs_nil.get;
165 hb_font_funcs_get_empty (void)
167 return &_hb_font_funcs_nil;
171 hb_font_funcs_reference (hb_font_funcs_t *ffuncs)
173 return hb_object_reference (ffuncs);
177 hb_font_funcs_destroy (hb_font_funcs_t *ffuncs)
179 if (!hb_object_destroy (ffuncs)) return;
181 #define DESTROY(name) if (ffuncs->destroy.name) ffuncs->destroy.name (ffuncs->user_data.name)
182 DESTROY (contour_point);
183 DESTROY (glyph_advance);
184 DESTROY (glyph_extents);
193 hb_font_funcs_set_user_data (hb_font_funcs_t *ffuncs,
194 hb_user_data_key_t *key,
196 hb_destroy_func_t destroy)
198 return hb_object_set_user_data (ffuncs, key, data, destroy);
202 hb_font_funcs_get_user_data (hb_font_funcs_t *ffuncs,
203 hb_user_data_key_t *key)
205 return hb_object_get_user_data (ffuncs, key);
210 hb_font_funcs_make_immutable (hb_font_funcs_t *ffuncs)
212 if (hb_object_is_inert (ffuncs))
215 ffuncs->immutable = TRUE;
219 hb_font_funcs_is_immutable (hb_font_funcs_t *ffuncs)
221 return ffuncs->immutable;
225 #define IMPLEMENT(name) \
228 hb_font_funcs_set_##name##_func (hb_font_funcs_t *ffuncs, \
229 hb_font_get_##name##_func_t func, \
231 hb_destroy_func_t destroy) \
233 if (ffuncs->immutable) \
236 if (ffuncs->destroy.name) \
237 ffuncs->destroy.name (ffuncs->user_data.name); \
240 ffuncs->get.name = func; \
241 ffuncs->user_data.name = user_data; \
242 ffuncs->destroy.name = destroy; \
244 ffuncs->get.name = hb_font_get_##name##_nil; \
245 ffuncs->user_data.name = NULL; \
246 ffuncs->destroy.name = NULL; \
250 IMPLEMENT (contour_point);
251 IMPLEMENT (glyph_advance);
252 IMPLEMENT (glyph_extents);
260 hb_font_get_contour_point (hb_font_t *font,
261 hb_codepoint_t glyph, unsigned int point_index,
262 hb_position_t *x, hb_position_t *y)
265 return font->klass->get.contour_point (font, font->user_data,
268 font->klass->user_data.contour_point);
272 hb_font_get_glyph_advance (hb_font_t *font,
273 hb_codepoint_t glyph,
274 hb_position_t *x_advance, hb_position_t *y_advance)
276 *x_advance = *y_advance = 0;
277 return font->klass->get.glyph_advance (font, font->user_data,
278 glyph, x_advance, y_advance,
279 font->klass->user_data.glyph_advance);
283 hb_font_get_glyph_extents (hb_font_t *font,
284 hb_codepoint_t glyph, hb_glyph_extents_t *extents)
286 memset (extents, 0, sizeof (*extents));
287 return font->klass->get.glyph_extents (font, font->user_data,
289 font->klass->user_data.glyph_extents);
293 hb_font_get_glyph (hb_font_t *font,
294 hb_codepoint_t unicode, hb_codepoint_t variation_selector,
295 hb_codepoint_t *glyph)
298 return font->klass->get.glyph (font, font->user_data,
299 unicode, variation_selector, glyph,
300 font->klass->user_data.glyph);
304 hb_font_get_kerning (hb_font_t *font,
305 hb_codepoint_t left_glyph, hb_codepoint_t right_glyph,
306 hb_position_t *x_kern, hb_position_t *y_kern)
308 *x_kern = *y_kern = 0;
309 return font->klass->get.kerning (font, font->user_data,
310 left_glyph, right_glyph,
312 font->klass->user_data.kerning);
320 static hb_face_t _hb_face_nil = {
321 HB_OBJECT_HEADER_STATIC,
323 TRUE, /* immutable */
325 NULL, /* get_table */
326 NULL, /* user_data */
329 NULL, /* ot_layout */
336 hb_face_create_for_tables (hb_get_table_func_t get_table,
338 hb_destroy_func_t destroy)
342 if (!get_table || !(face = hb_object_create<hb_face_t> ())) {
345 return &_hb_face_nil;
348 face->get_table = get_table;
349 face->user_data = user_data;
350 face->destroy = destroy;
352 face->ot_layout = _hb_ot_layout_create (face);
354 face->upem = _hb_ot_layout_get_upem (face);
360 typedef struct _hb_face_for_data_closure_t {
363 } hb_face_for_data_closure_t;
365 static hb_face_for_data_closure_t *
366 _hb_face_for_data_closure_create (hb_blob_t *blob, unsigned int index)
368 hb_face_for_data_closure_t *closure;
370 closure = (hb_face_for_data_closure_t *) malloc (sizeof (hb_face_for_data_closure_t));
371 if (unlikely (!closure))
374 closure->blob = blob;
375 closure->index = index;
381 _hb_face_for_data_closure_destroy (hb_face_for_data_closure_t *closure)
383 hb_blob_destroy (closure->blob);
388 _hb_face_for_data_get_table (hb_face_t *face HB_UNUSED, hb_tag_t tag, void *user_data)
390 hb_face_for_data_closure_t *data = (hb_face_for_data_closure_t *) user_data;
392 const OpenTypeFontFile &ot_file = *Sanitizer<OpenTypeFontFile>::lock_instance (data->blob);
393 const OpenTypeFontFace &ot_face = ot_file.get_face (data->index);
395 const OpenTypeTable &table = ot_face.get_table_by_tag (tag);
397 hb_blob_t *blob = hb_blob_create_sub_blob (data->blob, table.offset, table.length);
403 hb_face_create (hb_blob_t *blob,
406 if (unlikely (!blob || !hb_blob_get_length (blob)))
407 return &_hb_face_nil;
409 hb_face_for_data_closure_t *closure = _hb_face_for_data_closure_create (Sanitizer<OpenTypeFontFile>::sanitize (hb_blob_reference (blob)), index);
411 if (unlikely (!closure))
412 return &_hb_face_nil;
414 return hb_face_create_for_tables (_hb_face_for_data_get_table,
416 (hb_destroy_func_t) _hb_face_for_data_closure_destroy);
420 hb_face_get_empty (void)
422 return &_hb_face_nil;
427 hb_face_reference (hb_face_t *face)
429 return hb_object_reference (face);
433 hb_face_destroy (hb_face_t *face)
435 if (!hb_object_destroy (face)) return;
437 _hb_ot_layout_destroy (face->ot_layout);
440 face->destroy (face->user_data);
446 hb_face_set_user_data (hb_face_t *face,
447 hb_user_data_key_t *key,
449 hb_destroy_func_t destroy)
451 return hb_object_set_user_data (face, key, data, destroy);
455 hb_face_get_user_data (hb_face_t *face,
456 hb_user_data_key_t *key)
458 return hb_object_get_user_data (face, key);
462 hb_face_make_immutable (hb_face_t *face)
464 if (hb_object_is_inert (face))
467 face->immutable = true;
471 hb_face_is_immutable (hb_face_t *face)
473 return face->immutable;
478 hb_face_reference_table (hb_face_t *face,
483 if (unlikely (!face || !face->get_table))
484 return hb_blob_get_empty ();
486 blob = face->get_table (face, tag, face->user_data);
487 if (unlikely (!blob))
488 return hb_blob_get_empty ();
494 hb_face_get_upem (hb_face_t *face)
496 return _hb_ot_layout_get_upem (face);
504 static hb_font_t _hb_font_nil = {
505 HB_OBJECT_HEADER_STATIC,
507 TRUE, /* immutable */
518 &_hb_font_funcs_nil, /* klass */
519 NULL, /* user_data */
524 hb_font_create (hb_face_t *face)
528 if (unlikely (!face))
529 face = &_hb_face_nil;
530 if (unlikely (hb_object_is_inert (face)))
531 return &_hb_font_nil;
532 if (!(font = hb_object_create<hb_font_t> ()))
533 return &_hb_font_nil;
535 hb_face_make_immutable (face);
536 font->face = hb_face_reference (face);
537 font->klass = &_hb_font_funcs_nil;
543 hb_font_create_sub_font (hb_font_t *parent)
545 if (unlikely (!parent))
546 return &_hb_font_nil;
548 hb_font_t *font = hb_font_create (parent->face);
550 if (unlikely (hb_object_is_inert (font)))
553 hb_font_make_immutable (parent);
554 font->parent = hb_font_reference (parent);
556 font->x_scale = parent->x_scale;
557 font->y_scale = parent->y_scale;
558 font->x_ppem = parent->x_ppem;
559 font->y_ppem = parent->y_ppem;
561 font->klass = &_hb_font_funcs_nil;
567 hb_font_get_empty (void)
569 return &_hb_font_nil;
573 hb_font_reference (hb_font_t *font)
575 return hb_object_reference (font);
579 hb_font_destroy (hb_font_t *font)
581 if (!hb_object_destroy (font)) return;
583 hb_font_destroy (font->parent);
584 hb_face_destroy (font->face);
585 hb_font_funcs_destroy (font->klass);
587 font->destroy (font->user_data);
593 hb_font_set_user_data (hb_font_t *font,
594 hb_user_data_key_t *key,
596 hb_destroy_func_t destroy)
598 return hb_object_set_user_data (font, key, data, destroy);
602 hb_font_get_user_data (hb_font_t *font,
603 hb_user_data_key_t *key)
605 return hb_object_get_user_data (font, key);
609 hb_font_make_immutable (hb_font_t *font)
611 if (hb_object_is_inert (font))
614 font->immutable = true;
618 hb_font_is_immutable (hb_font_t *font)
620 return font->immutable;
624 hb_font_get_parent (hb_font_t *font)
630 hb_font_get_face (hb_font_t *font)
637 hb_font_set_funcs (hb_font_t *font,
638 hb_font_funcs_t *klass,
640 hb_destroy_func_t destroy)
646 font->destroy (font->user_data);
649 klass = &_hb_font_funcs_nil;
651 hb_font_funcs_reference (klass);
652 hb_font_funcs_destroy (font->klass);
654 font->user_data = user_data;
655 font->destroy = destroy;
660 hb_font_set_scale (hb_font_t *font,
667 font->x_scale = x_scale;
668 font->y_scale = y_scale;
672 hb_font_get_scale (hb_font_t *font,
676 if (x_scale) *x_scale = font->x_scale;
677 if (y_scale) *y_scale = font->y_scale;
681 hb_font_set_ppem (hb_font_t *font,
688 font->x_ppem = x_ppem;
689 font->y_ppem = y_ppem;
693 hb_font_get_ppem (hb_font_t *font,
694 unsigned int *x_ppem,
695 unsigned int *y_ppem)
697 if (x_ppem) *x_ppem = font->x_ppem;
698 if (y_ppem) *y_ppem = font->y_ppem;