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 void *font_data HB_UNUSED,
47 hb_codepoint_t unicode,
48 hb_codepoint_t variation_selector,
49 hb_codepoint_t *glyph,
50 void *user_data HB_UNUSED)
53 return hb_font_get_glyph (font->parent, unicode, variation_selector, glyph);
60 hb_font_get_glyph_h_advance_nil (hb_font_t *font HB_UNUSED,
61 void *font_data HB_UNUSED,
63 void *user_data HB_UNUSED)
66 return font->parent_scale_x_distance (hb_font_get_glyph_h_advance (font->parent, glyph));
72 hb_font_get_glyph_v_advance_nil (hb_font_t *font HB_UNUSED,
73 void *font_data HB_UNUSED,
75 void *user_data HB_UNUSED)
78 return font->parent_scale_y_distance (hb_font_get_glyph_v_advance (font->parent, glyph));
84 hb_font_get_glyph_h_origin_nil (hb_font_t *font HB_UNUSED,
85 void *font_data HB_UNUSED,
89 void *user_data HB_UNUSED)
92 hb_bool_t ret = hb_font_get_glyph_h_origin (font->parent,
96 font->parent_scale_position (x, y);
105 hb_font_get_glyph_v_origin_nil (hb_font_t *font HB_UNUSED,
106 void *font_data HB_UNUSED,
107 hb_codepoint_t glyph,
110 void *user_data HB_UNUSED)
113 hb_bool_t ret = hb_font_get_glyph_v_origin (font->parent,
117 font->parent_scale_position (x, y);
126 hb_font_get_glyph_h_kerning_nil (hb_font_t *font HB_UNUSED,
127 void *font_data HB_UNUSED,
128 hb_codepoint_t left_glyph,
129 hb_codepoint_t right_glyph,
130 void *user_data HB_UNUSED)
133 return font->parent_scale_x_distance (hb_font_get_glyph_h_kerning (font->parent, left_glyph, right_glyph));
139 hb_font_get_glyph_v_kerning_nil (hb_font_t *font HB_UNUSED,
140 void *font_data HB_UNUSED,
141 hb_codepoint_t top_glyph,
142 hb_codepoint_t bottom_glyph,
143 void *user_data HB_UNUSED)
146 return font->parent_scale_y_distance (hb_font_get_glyph_v_kerning (font->parent, top_glyph, bottom_glyph));
152 hb_font_get_glyph_extents_nil (hb_font_t *font HB_UNUSED,
153 void *font_data HB_UNUSED,
154 hb_codepoint_t glyph,
155 hb_glyph_extents_t *extents,
156 void *user_data HB_UNUSED)
159 hb_bool_t ret = hb_font_get_glyph_extents (font->parent,
163 font->parent_scale_position (&extents->x_bearing, &extents->y_bearing);
164 font->parent_scale_distance (&extents->width, &extents->height);
169 memset (extents, 0, sizeof (*extents));
174 hb_font_get_glyph_contour_point_nil (hb_font_t *font HB_UNUSED,
175 void *font_data HB_UNUSED,
176 hb_codepoint_t glyph,
177 unsigned int point_index,
180 void *user_data HB_UNUSED)
183 hb_bool_t ret = hb_font_get_glyph_contour_point (font->parent,
187 font->parent_scale_position (x, y);
196 static hb_font_funcs_t _hb_font_funcs_nil = {
197 HB_OBJECT_HEADER_STATIC,
199 TRUE, /* immutable */
202 #define HB_FONT_FUNC_IMPLEMENT(name) hb_font_get_##name##_nil,
203 HB_FONT_FUNCS_IMPLEMENT_CALLBACKS
204 #undef HB_FONT_FUNC_IMPLEMENT
210 hb_font_funcs_create (void)
212 hb_font_funcs_t *ffuncs;
214 if (!(ffuncs = hb_object_create<hb_font_funcs_t> ()))
215 return &_hb_font_funcs_nil;
217 ffuncs->get = _hb_font_funcs_nil.get;
223 hb_font_funcs_get_empty (void)
225 return &_hb_font_funcs_nil;
229 hb_font_funcs_reference (hb_font_funcs_t *ffuncs)
231 return hb_object_reference (ffuncs);
235 hb_font_funcs_destroy (hb_font_funcs_t *ffuncs)
237 if (!hb_object_destroy (ffuncs)) return;
239 #define HB_FONT_FUNC_IMPLEMENT(name) if (ffuncs->destroy.name) \
240 ffuncs->destroy.name (ffuncs->user_data.name);
241 HB_FONT_FUNCS_IMPLEMENT_CALLBACKS
242 #undef HB_FONT_FUNC_IMPLEMENT
248 hb_font_funcs_set_user_data (hb_font_funcs_t *ffuncs,
249 hb_user_data_key_t *key,
251 hb_destroy_func_t destroy)
253 return hb_object_set_user_data (ffuncs, key, data, destroy);
257 hb_font_funcs_get_user_data (hb_font_funcs_t *ffuncs,
258 hb_user_data_key_t *key)
260 return hb_object_get_user_data (ffuncs, key);
265 hb_font_funcs_make_immutable (hb_font_funcs_t *ffuncs)
267 if (hb_object_is_inert (ffuncs))
270 ffuncs->immutable = TRUE;
274 hb_font_funcs_is_immutable (hb_font_funcs_t *ffuncs)
276 return ffuncs->immutable;
280 #define HB_FONT_FUNC_IMPLEMENT(name) \
283 hb_font_funcs_set_##name##_func (hb_font_funcs_t *ffuncs, \
284 hb_font_get_##name##_func_t func, \
286 hb_destroy_func_t destroy) \
288 if (ffuncs->immutable) \
291 if (ffuncs->destroy.name) \
292 ffuncs->destroy.name (ffuncs->user_data.name); \
295 ffuncs->get.name = func; \
296 ffuncs->user_data.name = user_data; \
297 ffuncs->destroy.name = destroy; \
299 ffuncs->get.name = hb_font_get_##name##_nil; \
300 ffuncs->user_data.name = NULL; \
301 ffuncs->destroy.name = NULL; \
305 HB_FONT_FUNCS_IMPLEMENT_CALLBACKS
306 #undef HB_FONT_FUNC_IMPLEMENT
310 hb_font_get_glyph (hb_font_t *font,
311 hb_codepoint_t unicode, hb_codepoint_t variation_selector,
312 hb_codepoint_t *glyph)
315 return font->klass->get.glyph (font, font->user_data,
316 unicode, variation_selector, glyph,
317 font->klass->user_data.glyph);
321 hb_font_get_glyph_h_advance (hb_font_t *font,
322 hb_codepoint_t glyph)
324 return font->klass->get.glyph_h_advance (font, font->user_data,
326 font->klass->user_data.glyph_h_advance);
330 hb_font_get_glyph_v_advance (hb_font_t *font,
331 hb_codepoint_t glyph)
333 return font->klass->get.glyph_v_advance (font, font->user_data,
335 font->klass->user_data.glyph_v_advance);
339 hb_font_get_glyph_h_origin (hb_font_t *font,
340 hb_codepoint_t glyph,
341 hb_position_t *x, hb_position_t *y)
344 return font->klass->get.glyph_h_origin (font, font->user_data,
346 font->klass->user_data.glyph_h_origin);
350 hb_font_get_glyph_v_origin (hb_font_t *font,
351 hb_codepoint_t glyph,
352 hb_position_t *x, hb_position_t *y)
355 return font->klass->get.glyph_v_origin (font, font->user_data,
357 font->klass->user_data.glyph_v_origin);
361 hb_font_get_glyph_h_kerning (hb_font_t *font,
362 hb_codepoint_t left_glyph, hb_codepoint_t right_glyph)
364 return font->klass->get.glyph_h_kerning (font, font->user_data,
365 left_glyph, right_glyph,
366 font->klass->user_data.glyph_h_kerning);
370 hb_font_get_glyph_v_kerning (hb_font_t *font,
371 hb_codepoint_t left_glyph, hb_codepoint_t right_glyph)
373 return font->klass->get.glyph_v_kerning (font, font->user_data,
374 left_glyph, right_glyph,
375 font->klass->user_data.glyph_v_kerning);
379 hb_font_get_glyph_extents (hb_font_t *font,
380 hb_codepoint_t glyph,
381 hb_glyph_extents_t *extents)
383 memset (extents, 0, sizeof (*extents));
384 return font->klass->get.glyph_extents (font, font->user_data,
387 font->klass->user_data.glyph_extents);
391 hb_font_get_glyph_contour_point (hb_font_t *font,
392 hb_codepoint_t glyph, unsigned int point_index,
393 hb_position_t *x, hb_position_t *y)
396 return font->klass->get.glyph_contour_point (font, font->user_data,
399 font->klass->user_data.glyph_contour_point);
403 /* A bit higher-level, and with fallback */
406 hb_font_get_glyph_advance_for_direction (hb_font_t *font,
407 hb_codepoint_t glyph,
408 hb_direction_t direction,
409 hb_position_t *x, hb_position_t *y)
411 if (likely (HB_DIRECTION_IS_HORIZONTAL (direction))) {
412 *x = hb_font_get_glyph_h_advance (font, glyph);
416 *y = hb_font_get_glyph_v_advance (font, glyph);
421 guess_v_origin_minus_h_origin (hb_font_t *font,
422 hb_codepoint_t glyph,
423 hb_position_t *x, hb_position_t *y)
425 *x = hb_font_get_glyph_h_advance (font, glyph) / 2;
427 /* TODO use font_metics.ascent */
433 hb_font_get_glyph_origin_for_direction (hb_font_t *font,
434 hb_codepoint_t glyph,
435 hb_direction_t direction,
436 hb_position_t *x, hb_position_t *y)
438 if (likely (HB_DIRECTION_IS_HORIZONTAL (direction))) {
439 hb_bool_t ret = hb_font_get_glyph_h_origin (font, glyph, x, y);
440 if (!ret && (ret = hb_font_get_glyph_v_origin (font, glyph, x, y))) {
441 hb_position_t dx, dy;
442 guess_v_origin_minus_h_origin (font, glyph, &dx, &dy);
446 hb_bool_t ret = hb_font_get_glyph_v_origin (font, glyph, x, y);
447 if (!ret && (ret = hb_font_get_glyph_h_origin (font, glyph, x, y))) {
448 hb_position_t dx, dy;
449 guess_v_origin_minus_h_origin (font, glyph, &dx, &dy);
456 hb_font_add_glyph_origin_for_direction (hb_font_t *font,
457 hb_codepoint_t glyph,
458 hb_direction_t direction,
459 hb_position_t *x, hb_position_t *y)
461 hb_position_t origin_x, origin_y;
463 hb_font_get_glyph_origin_for_direction (font, glyph, direction, &origin_x, &origin_y);
470 hb_font_subtract_glyph_origin_for_direction (hb_font_t *font,
471 hb_codepoint_t glyph,
472 hb_direction_t direction,
473 hb_position_t *x, hb_position_t *y)
475 hb_position_t origin_x, origin_y;
477 hb_font_get_glyph_origin_for_direction (font, glyph, direction, &origin_x, &origin_y);
484 hb_font_get_glyph_kerning_for_direction (hb_font_t *font,
485 hb_codepoint_t first_glyph, hb_codepoint_t second_glyph,
486 hb_direction_t direction,
487 hb_position_t *x, hb_position_t *y)
489 if (likely (HB_DIRECTION_IS_HORIZONTAL (direction))) {
490 *x = hb_font_get_glyph_h_kerning (font, first_glyph, second_glyph);
494 *y = hb_font_get_glyph_v_kerning (font, first_glyph, second_glyph);
499 hb_font_get_glyph_extents_for_origin (hb_font_t *font,
500 hb_codepoint_t glyph,
501 hb_direction_t direction,
502 hb_glyph_extents_t *extents)
504 hb_bool_t ret = hb_font_get_glyph_extents (font, glyph, extents);
507 hb_font_subtract_glyph_origin_for_direction (font, glyph, direction, &extents->x_bearing, &extents->y_bearing);
513 hb_font_get_glyph_contour_point_for_origin (hb_font_t *font,
514 hb_codepoint_t glyph, unsigned int point_index,
515 hb_direction_t direction,
516 hb_position_t *x, hb_position_t *y)
518 hb_bool_t ret = hb_font_get_glyph_contour_point (font, glyph, point_index, x, y);
521 hb_font_subtract_glyph_origin_for_direction (font, glyph, direction, x, y);
531 static hb_face_t _hb_face_nil = {
532 HB_OBJECT_HEADER_STATIC,
534 TRUE, /* immutable */
536 NULL, /* get_table */
537 NULL, /* user_data */
540 NULL, /* ot_layout */
547 hb_face_create_for_tables (hb_get_table_func_t get_table,
549 hb_destroy_func_t destroy)
553 if (!get_table || !(face = hb_object_create<hb_face_t> ())) {
556 return &_hb_face_nil;
559 face->get_table = get_table;
560 face->user_data = user_data;
561 face->destroy = destroy;
563 face->ot_layout = _hb_ot_layout_create (face);
565 face->upem = _hb_ot_layout_get_upem (face);
571 typedef struct _hb_face_for_data_closure_t {
574 } hb_face_for_data_closure_t;
576 static hb_face_for_data_closure_t *
577 _hb_face_for_data_closure_create (hb_blob_t *blob, unsigned int index)
579 hb_face_for_data_closure_t *closure;
581 closure = (hb_face_for_data_closure_t *) malloc (sizeof (hb_face_for_data_closure_t));
582 if (unlikely (!closure))
585 closure->blob = blob;
586 closure->index = index;
592 _hb_face_for_data_closure_destroy (hb_face_for_data_closure_t *closure)
594 hb_blob_destroy (closure->blob);
599 _hb_face_for_data_get_table (hb_face_t *face HB_UNUSED, hb_tag_t tag, void *user_data)
601 hb_face_for_data_closure_t *data = (hb_face_for_data_closure_t *) user_data;
603 const OpenTypeFontFile &ot_file = *Sanitizer<OpenTypeFontFile>::lock_instance (data->blob);
604 const OpenTypeFontFace &ot_face = ot_file.get_face (data->index);
606 const OpenTypeTable &table = ot_face.get_table_by_tag (tag);
608 hb_blob_t *blob = hb_blob_create_sub_blob (data->blob, table.offset, table.length);
614 hb_face_create (hb_blob_t *blob,
617 if (unlikely (!blob || !hb_blob_get_length (blob)))
618 return &_hb_face_nil;
620 hb_face_for_data_closure_t *closure = _hb_face_for_data_closure_create (Sanitizer<OpenTypeFontFile>::sanitize (hb_blob_reference (blob)), index);
622 if (unlikely (!closure))
623 return &_hb_face_nil;
625 return hb_face_create_for_tables (_hb_face_for_data_get_table,
627 (hb_destroy_func_t) _hb_face_for_data_closure_destroy);
631 hb_face_get_empty (void)
633 return &_hb_face_nil;
638 hb_face_reference (hb_face_t *face)
640 return hb_object_reference (face);
644 hb_face_destroy (hb_face_t *face)
646 if (!hb_object_destroy (face)) return;
648 _hb_ot_layout_destroy (face->ot_layout);
651 face->destroy (face->user_data);
657 hb_face_set_user_data (hb_face_t *face,
658 hb_user_data_key_t *key,
660 hb_destroy_func_t destroy)
662 return hb_object_set_user_data (face, key, data, destroy);
666 hb_face_get_user_data (hb_face_t *face,
667 hb_user_data_key_t *key)
669 return hb_object_get_user_data (face, key);
673 hb_face_make_immutable (hb_face_t *face)
675 if (hb_object_is_inert (face))
678 face->immutable = true;
682 hb_face_is_immutable (hb_face_t *face)
684 return face->immutable;
689 hb_face_reference_table (hb_face_t *face,
694 if (unlikely (!face || !face->get_table))
695 return hb_blob_get_empty ();
697 blob = face->get_table (face, tag, face->user_data);
698 if (unlikely (!blob))
699 return hb_blob_get_empty ();
705 hb_face_get_upem (hb_face_t *face)
707 return _hb_ot_layout_get_upem (face);
715 static hb_font_t _hb_font_nil = {
716 HB_OBJECT_HEADER_STATIC,
718 TRUE, /* immutable */
729 &_hb_font_funcs_nil, /* klass */
730 NULL, /* user_data */
735 hb_font_create (hb_face_t *face)
739 if (unlikely (!face))
740 face = &_hb_face_nil;
741 if (unlikely (hb_object_is_inert (face)))
742 return &_hb_font_nil;
743 if (!(font = hb_object_create<hb_font_t> ()))
744 return &_hb_font_nil;
746 hb_face_make_immutable (face);
747 font->face = hb_face_reference (face);
748 font->klass = &_hb_font_funcs_nil;
754 hb_font_create_sub_font (hb_font_t *parent)
756 if (unlikely (!parent))
757 return &_hb_font_nil;
759 hb_font_t *font = hb_font_create (parent->face);
761 if (unlikely (hb_object_is_inert (font)))
764 hb_font_make_immutable (parent);
765 font->parent = hb_font_reference (parent);
767 font->x_scale = parent->x_scale;
768 font->y_scale = parent->y_scale;
769 font->x_ppem = parent->x_ppem;
770 font->y_ppem = parent->y_ppem;
772 font->klass = &_hb_font_funcs_nil;
778 hb_font_get_empty (void)
780 return &_hb_font_nil;
784 hb_font_reference (hb_font_t *font)
786 return hb_object_reference (font);
790 hb_font_destroy (hb_font_t *font)
792 if (!hb_object_destroy (font)) return;
794 hb_font_destroy (font->parent);
795 hb_face_destroy (font->face);
796 hb_font_funcs_destroy (font->klass);
798 font->destroy (font->user_data);
804 hb_font_set_user_data (hb_font_t *font,
805 hb_user_data_key_t *key,
807 hb_destroy_func_t destroy)
809 return hb_object_set_user_data (font, key, data, destroy);
813 hb_font_get_user_data (hb_font_t *font,
814 hb_user_data_key_t *key)
816 return hb_object_get_user_data (font, key);
820 hb_font_make_immutable (hb_font_t *font)
822 if (hb_object_is_inert (font))
825 font->immutable = true;
829 hb_font_is_immutable (hb_font_t *font)
831 return font->immutable;
835 hb_font_get_parent (hb_font_t *font)
841 hb_font_get_face (hb_font_t *font)
848 hb_font_set_funcs (hb_font_t *font,
849 hb_font_funcs_t *klass,
851 hb_destroy_func_t destroy)
857 font->destroy (font->user_data);
860 klass = &_hb_font_funcs_nil;
862 hb_font_funcs_reference (klass);
863 hb_font_funcs_destroy (font->klass);
865 font->user_data = user_data;
866 font->destroy = destroy;
871 hb_font_set_scale (hb_font_t *font,
878 font->x_scale = x_scale;
879 font->y_scale = y_scale;
883 hb_font_get_scale (hb_font_t *font,
887 if (x_scale) *x_scale = font->x_scale;
888 if (y_scale) *y_scale = font->y_scale;
892 hb_font_set_ppem (hb_font_t *font,
899 font->x_ppem = x_ppem;
900 font->y_ppem = y_ppem;
904 hb_font_get_ppem (hb_font_t *font,
905 unsigned int *x_ppem,
906 unsigned int *y_ppem)
908 if (x_ppem) *x_ppem = font->x_ppem;
909 if (y_ppem) *y_ppem = font->y_ppem;