2 * Copyright © 2009 Red Hat, Inc.
3 * Copyright © 2012 Google, Inc.
5 * This is part of HarfBuzz, a text shaping library.
7 * Permission is hereby granted, without written agreement and without
8 * license or royalty fees, to use, copy, modify, and distribute this
9 * software and its documentation for any purpose, provided that the
10 * above copyright notice and the following two paragraphs appear in
11 * all copies of this software.
13 * IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE TO ANY PARTY FOR
14 * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
15 * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN
16 * IF THE COPYRIGHT HOLDER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH
19 * THE COPYRIGHT HOLDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING,
20 * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
21 * FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS
22 * ON AN "AS IS" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO
23 * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
25 * Red Hat Author(s): Behdad Esfahbod
26 * Google Author(s): Behdad Esfahbod
29 #include "hb-private.hh"
31 #include "hb-ot-layout-private.hh"
33 #include "hb-font-private.hh"
34 #include "hb-open-file-private.hh"
35 #include "hb-ot-head-table.hh"
36 #include "hb-ot-maxp-table.hh"
38 #include "hb-cache-private.hh"
48 hb_font_get_glyph_nil (hb_font_t
*font
,
49 void *font_data HB_UNUSED
,
50 hb_codepoint_t unicode
,
51 hb_codepoint_t variation_selector
,
52 hb_codepoint_t
*glyph
,
53 void *user_data HB_UNUSED
)
56 return font
->parent
->get_glyph (unicode
, variation_selector
, glyph
);
63 hb_font_get_glyph_h_advance_nil (hb_font_t
*font
,
64 void *font_data HB_UNUSED
,
66 void *user_data HB_UNUSED
)
69 return font
->parent_scale_x_distance (font
->parent
->get_glyph_h_advance (glyph
));
75 hb_font_get_glyph_v_advance_nil (hb_font_t
*font
,
76 void *font_data HB_UNUSED
,
78 void *user_data HB_UNUSED
)
81 return font
->parent_scale_y_distance (font
->parent
->get_glyph_v_advance (glyph
));
87 hb_font_get_glyph_h_origin_nil (hb_font_t
*font
,
88 void *font_data HB_UNUSED
,
92 void *user_data HB_UNUSED
)
95 hb_bool_t ret
= font
->parent
->get_glyph_h_origin (glyph
, x
, y
);
97 font
->parent_scale_position (x
, y
);
106 hb_font_get_glyph_v_origin_nil (hb_font_t
*font
,
107 void *font_data HB_UNUSED
,
108 hb_codepoint_t glyph
,
111 void *user_data HB_UNUSED
)
114 hb_bool_t ret
= font
->parent
->get_glyph_v_origin (glyph
, x
, y
);
116 font
->parent_scale_position (x
, y
);
125 hb_font_get_glyph_h_kerning_nil (hb_font_t
*font
,
126 void *font_data HB_UNUSED
,
127 hb_codepoint_t left_glyph
,
128 hb_codepoint_t right_glyph
,
129 void *user_data HB_UNUSED
)
132 return font
->parent_scale_x_distance (font
->parent
->get_glyph_h_kerning (left_glyph
, right_glyph
));
138 hb_font_get_glyph_v_kerning_nil (hb_font_t
*font
,
139 void *font_data HB_UNUSED
,
140 hb_codepoint_t top_glyph
,
141 hb_codepoint_t bottom_glyph
,
142 void *user_data HB_UNUSED
)
145 return font
->parent_scale_y_distance (font
->parent
->get_glyph_v_kerning (top_glyph
, bottom_glyph
));
151 hb_font_get_glyph_extents_nil (hb_font_t
*font
,
152 void *font_data HB_UNUSED
,
153 hb_codepoint_t glyph
,
154 hb_glyph_extents_t
*extents
,
155 void *user_data HB_UNUSED
)
158 hb_bool_t ret
= font
->parent
->get_glyph_extents (glyph
, extents
);
160 font
->parent_scale_position (&extents
->x_bearing
, &extents
->y_bearing
);
161 font
->parent_scale_distance (&extents
->width
, &extents
->height
);
166 memset (extents
, 0, sizeof (*extents
));
171 hb_font_get_glyph_contour_point_nil (hb_font_t
*font
,
172 void *font_data HB_UNUSED
,
173 hb_codepoint_t glyph
,
174 unsigned int point_index
,
177 void *user_data HB_UNUSED
)
180 hb_bool_t ret
= font
->parent
->get_glyph_contour_point (glyph
, point_index
, x
, y
);
182 font
->parent_scale_position (x
, y
);
191 hb_font_get_glyph_name_nil (hb_font_t
*font
,
192 void *font_data HB_UNUSED
,
193 hb_codepoint_t glyph
,
194 char *name
, unsigned int size
,
195 void *user_data HB_UNUSED
)
198 return font
->parent
->get_glyph_name (glyph
, name
, size
);
200 if (size
) *name
= '\0';
205 hb_font_get_glyph_from_name_nil (hb_font_t
*font
,
206 void *font_data HB_UNUSED
,
207 const char *name
, int len
, /* -1 means nul-terminated */
208 hb_codepoint_t
*glyph
,
209 void *user_data HB_UNUSED
)
212 return font
->parent
->get_glyph_from_name (name
, len
, glyph
);
219 static const hb_font_funcs_t _hb_font_funcs_nil
= {
220 HB_OBJECT_HEADER_STATIC
,
222 true, /* immutable */
225 #define HB_FONT_FUNC_IMPLEMENT(name) hb_font_get_##name##_nil,
226 HB_FONT_FUNCS_IMPLEMENT_CALLBACKS
227 #undef HB_FONT_FUNC_IMPLEMENT
233 * hb_font_funcs_create: (Xconstructor)
237 * Return value: (transfer full):
242 hb_font_funcs_create (void)
244 hb_font_funcs_t
*ffuncs
;
246 if (!(ffuncs
= hb_object_create
<hb_font_funcs_t
> ()))
247 return hb_font_funcs_get_empty ();
249 ffuncs
->get
= _hb_font_funcs_nil
.get
;
255 * hb_font_funcs_get_empty:
259 * Return value: (transfer full):
264 hb_font_funcs_get_empty (void)
266 return const_cast<hb_font_funcs_t
*> (&_hb_font_funcs_nil
);
270 * hb_font_funcs_reference: (skip)
271 * @ffuncs: font functions.
280 hb_font_funcs_reference (hb_font_funcs_t
*ffuncs
)
282 return hb_object_reference (ffuncs
);
286 * hb_font_funcs_destroy: (skip)
287 * @ffuncs: font functions.
294 hb_font_funcs_destroy (hb_font_funcs_t
*ffuncs
)
296 if (!hb_object_destroy (ffuncs
)) return;
298 #define HB_FONT_FUNC_IMPLEMENT(name) if (ffuncs->destroy.name) \
299 ffuncs->destroy.name (ffuncs->user_data.name);
300 HB_FONT_FUNCS_IMPLEMENT_CALLBACKS
301 #undef HB_FONT_FUNC_IMPLEMENT
307 * hb_font_funcs_set_user_data: (skip)
308 * @ffuncs: font functions.
321 hb_font_funcs_set_user_data (hb_font_funcs_t
*ffuncs
,
322 hb_user_data_key_t
*key
,
324 hb_destroy_func_t destroy
,
327 return hb_object_set_user_data (ffuncs
, key
, data
, destroy
, replace
);
331 * hb_font_funcs_get_user_data: (skip)
332 * @ffuncs: font functions.
337 * Return value: (transfer none):
342 hb_font_funcs_get_user_data (hb_font_funcs_t
*ffuncs
,
343 hb_user_data_key_t
*key
)
345 return hb_object_get_user_data (ffuncs
, key
);
350 * hb_font_funcs_make_immutable:
351 * @ffuncs: font functions.
358 hb_font_funcs_make_immutable (hb_font_funcs_t
*ffuncs
)
360 if (unlikely (hb_object_is_inert (ffuncs
)))
363 ffuncs
->immutable
= true;
367 * hb_font_funcs_is_immutable:
368 * @ffuncs: font functions.
377 hb_font_funcs_is_immutable (hb_font_funcs_t
*ffuncs
)
379 return ffuncs
->immutable
;
383 #define HB_FONT_FUNC_IMPLEMENT(name) \
386 hb_font_funcs_set_##name##_func (hb_font_funcs_t *ffuncs, \
387 hb_font_get_##name##_func_t func, \
389 hb_destroy_func_t destroy) \
391 if (ffuncs->immutable) { \
393 destroy (user_data); \
397 if (ffuncs->destroy.name) \
398 ffuncs->destroy.name (ffuncs->user_data.name); \
401 ffuncs->get.name = func; \
402 ffuncs->user_data.name = user_data; \
403 ffuncs->destroy.name = destroy; \
405 ffuncs->get.name = hb_font_get_##name##_nil; \
406 ffuncs->user_data.name = NULL; \
407 ffuncs->destroy.name = NULL; \
411 HB_FONT_FUNCS_IMPLEMENT_CALLBACKS
412 #undef HB_FONT_FUNC_IMPLEMENT
421 * @variation_selector:
431 hb_font_get_glyph (hb_font_t
*font
,
432 hb_codepoint_t unicode
, hb_codepoint_t variation_selector
,
433 hb_codepoint_t
*glyph
)
435 return font
->get_glyph (unicode
, variation_selector
, glyph
);
439 * hb_font_get_glyph_h_advance:
450 hb_font_get_glyph_h_advance (hb_font_t
*font
,
451 hb_codepoint_t glyph
)
453 return font
->get_glyph_h_advance (glyph
);
457 * hb_font_get_glyph_v_advance:
468 hb_font_get_glyph_v_advance (hb_font_t
*font
,
469 hb_codepoint_t glyph
)
471 return font
->get_glyph_v_advance (glyph
);
475 * hb_font_get_glyph_h_origin:
488 hb_font_get_glyph_h_origin (hb_font_t
*font
,
489 hb_codepoint_t glyph
,
490 hb_position_t
*x
, hb_position_t
*y
)
492 return font
->get_glyph_h_origin (glyph
, x
, y
);
496 * hb_font_get_glyph_v_origin:
509 hb_font_get_glyph_v_origin (hb_font_t
*font
,
510 hb_codepoint_t glyph
,
511 hb_position_t
*x
, hb_position_t
*y
)
513 return font
->get_glyph_v_origin (glyph
, x
, y
);
517 * hb_font_get_glyph_h_kerning:
529 hb_font_get_glyph_h_kerning (hb_font_t
*font
,
530 hb_codepoint_t left_glyph
, hb_codepoint_t right_glyph
)
532 return font
->get_glyph_h_kerning (left_glyph
, right_glyph
);
536 * hb_font_get_glyph_v_kerning:
548 hb_font_get_glyph_v_kerning (hb_font_t
*font
,
549 hb_codepoint_t top_glyph
, hb_codepoint_t bottom_glyph
)
551 return font
->get_glyph_v_kerning (top_glyph
, bottom_glyph
);
555 * hb_font_get_glyph_extents:
567 hb_font_get_glyph_extents (hb_font_t
*font
,
568 hb_codepoint_t glyph
,
569 hb_glyph_extents_t
*extents
)
571 return font
->get_glyph_extents (glyph
, extents
);
575 * hb_font_get_glyph_contour_point:
589 hb_font_get_glyph_contour_point (hb_font_t
*font
,
590 hb_codepoint_t glyph
, unsigned int point_index
,
591 hb_position_t
*x
, hb_position_t
*y
)
593 return font
->get_glyph_contour_point (glyph
, point_index
, x
, y
);
597 * hb_font_get_glyph_name:
600 * @name: (array length=size):
610 hb_font_get_glyph_name (hb_font_t
*font
,
611 hb_codepoint_t glyph
,
612 char *name
, unsigned int size
)
614 return font
->get_glyph_name (glyph
, name
, size
);
618 * hb_font_get_glyph_from_name:
620 * @name: (array length=len):
631 hb_font_get_glyph_from_name (hb_font_t
*font
,
632 const char *name
, int len
, /* -1 means nul-terminated */
633 hb_codepoint_t
*glyph
)
635 return font
->get_glyph_from_name (name
, len
, glyph
);
639 /* A bit higher-level, and with fallback */
642 * hb_font_get_glyph_advance_for_direction:
654 hb_font_get_glyph_advance_for_direction (hb_font_t
*font
,
655 hb_codepoint_t glyph
,
656 hb_direction_t direction
,
657 hb_position_t
*x
, hb_position_t
*y
)
659 return font
->get_glyph_advance_for_direction (glyph
, direction
, x
, y
);
663 * hb_font_get_glyph_origin_for_direction:
675 hb_font_get_glyph_origin_for_direction (hb_font_t
*font
,
676 hb_codepoint_t glyph
,
677 hb_direction_t direction
,
678 hb_position_t
*x
, hb_position_t
*y
)
680 return font
->get_glyph_origin_for_direction (glyph
, direction
, x
, y
);
684 * hb_font_add_glyph_origin_for_direction:
696 hb_font_add_glyph_origin_for_direction (hb_font_t
*font
,
697 hb_codepoint_t glyph
,
698 hb_direction_t direction
,
699 hb_position_t
*x
, hb_position_t
*y
)
701 return font
->add_glyph_origin_for_direction (glyph
, direction
, x
, y
);
705 * hb_font_subtract_glyph_origin_for_direction:
717 hb_font_subtract_glyph_origin_for_direction (hb_font_t
*font
,
718 hb_codepoint_t glyph
,
719 hb_direction_t direction
,
720 hb_position_t
*x
, hb_position_t
*y
)
722 return font
->subtract_glyph_origin_for_direction (glyph
, direction
, x
, y
);
726 * hb_font_get_glyph_kerning_for_direction:
739 hb_font_get_glyph_kerning_for_direction (hb_font_t
*font
,
740 hb_codepoint_t first_glyph
, hb_codepoint_t second_glyph
,
741 hb_direction_t direction
,
742 hb_position_t
*x
, hb_position_t
*y
)
744 return font
->get_glyph_kerning_for_direction (first_glyph
, second_glyph
, direction
, x
, y
);
748 * hb_font_get_glyph_extents_for_origin:
761 hb_font_get_glyph_extents_for_origin (hb_font_t
*font
,
762 hb_codepoint_t glyph
,
763 hb_direction_t direction
,
764 hb_glyph_extents_t
*extents
)
766 return font
->get_glyph_extents_for_origin (glyph
, direction
, extents
);
770 * hb_font_get_glyph_contour_point_for_origin:
785 hb_font_get_glyph_contour_point_for_origin (hb_font_t
*font
,
786 hb_codepoint_t glyph
, unsigned int point_index
,
787 hb_direction_t direction
,
788 hb_position_t
*x
, hb_position_t
*y
)
790 return font
->get_glyph_contour_point_for_origin (glyph
, point_index
, direction
, x
, y
);
793 /* Generates gidDDD if glyph has no name. */
795 * hb_font_glyph_to_string:
798 * @s: (array length=size):
806 hb_font_glyph_to_string (hb_font_t
*font
,
807 hb_codepoint_t glyph
,
808 char *s
, unsigned int size
)
810 font
->glyph_to_string (glyph
, s
, size
);
813 /* Parses gidDDD and uniUUUU strings automatically. */
815 * hb_font_glyph_from_string:
817 * @s: (array length=len) (element-type uint8_t):
828 hb_font_glyph_from_string (hb_font_t
*font
,
829 const char *s
, int len
, /* -1 means nul-terminated */
830 hb_codepoint_t
*glyph
)
832 return font
->glyph_from_string (s
, len
, glyph
);
841 * hb_font_create: (Xconstructor)
846 * Return value: (transfer full):
851 hb_font_create (hb_face_t
*face
)
855 if (unlikely (!face
))
856 face
= hb_face_get_empty ();
857 if (!(font
= hb_object_create
<hb_font_t
> ()))
858 return hb_font_get_empty ();
860 hb_face_make_immutable (face
);
861 font
->face
= hb_face_reference (face
);
862 font
->klass
= hb_font_funcs_get_empty ();
868 * hb_font_create_sub_font:
869 * @parent: parent font.
873 * Return value: (transfer full):
878 hb_font_create_sub_font (hb_font_t
*parent
)
880 if (unlikely (!parent
))
881 parent
= hb_font_get_empty ();
883 hb_font_t
*font
= hb_font_create (parent
->face
);
885 if (unlikely (hb_object_is_inert (font
)))
888 hb_font_make_immutable (parent
);
889 font
->parent
= hb_font_reference (parent
);
891 font
->x_scale
= parent
->x_scale
;
892 font
->y_scale
= parent
->y_scale
;
893 font
->x_ppem
= parent
->x_ppem
;
894 font
->y_ppem
= parent
->y_ppem
;
904 * Return value: (transfer full)
909 hb_font_get_empty (void)
911 static const hb_font_t _hb_font_nil
= {
912 HB_OBJECT_HEADER_STATIC
,
914 true, /* immutable */
917 const_cast<hb_face_t
*> (&_hb_face_nil
),
925 const_cast<hb_font_funcs_t
*> (&_hb_font_funcs_nil
), /* klass */
926 NULL
, /* user_data */
930 #define HB_SHAPER_IMPLEMENT(shaper) HB_SHAPER_DATA_INVALID,
931 #include "hb-shaper-list.hh"
932 #undef HB_SHAPER_IMPLEMENT
936 return const_cast<hb_font_t
*> (&_hb_font_nil
);
940 * hb_font_reference: (skip)
945 * Return value: (transfer full):
950 hb_font_reference (hb_font_t
*font
)
952 return hb_object_reference (font
);
956 * hb_font_destroy: (skip)
964 hb_font_destroy (hb_font_t
*font
)
966 if (!hb_object_destroy (font
)) return;
968 #define HB_SHAPER_IMPLEMENT(shaper) HB_SHAPER_DATA_DESTROY(shaper, font);
969 #include "hb-shaper-list.hh"
970 #undef HB_SHAPER_IMPLEMENT
973 font
->destroy (font
->user_data
);
975 hb_font_destroy (font
->parent
);
976 hb_face_destroy (font
->face
);
977 hb_font_funcs_destroy (font
->klass
);
983 * hb_font_set_user_data: (skip)
997 hb_font_set_user_data (hb_font_t
*font
,
998 hb_user_data_key_t
*key
,
1000 hb_destroy_func_t destroy
,
1003 return hb_object_set_user_data (font
, key
, data
, destroy
, replace
);
1007 * hb_font_get_user_data: (skip)
1013 * Return value: (transfer none):
1018 hb_font_get_user_data (hb_font_t
*font
,
1019 hb_user_data_key_t
*key
)
1021 return hb_object_get_user_data (font
, key
);
1025 * hb_font_make_immutable:
1033 hb_font_make_immutable (hb_font_t
*font
)
1035 if (unlikely (hb_object_is_inert (font
)))
1038 font
->immutable
= true;
1042 * hb_font_is_immutable:
1052 hb_font_is_immutable (hb_font_t
*font
)
1054 return font
->immutable
;
1058 * hb_font_get_parent:
1063 * Return value: (transfer none):
1068 hb_font_get_parent (hb_font_t
*font
)
1070 return font
->parent
;
1079 * Return value: (transfer none):
1084 hb_font_get_face (hb_font_t
*font
)
1091 * hb_font_set_funcs:
1093 * @klass: (closure font_data) (destroy destroy) (scope notified):
1102 hb_font_set_funcs (hb_font_t
*font
,
1103 hb_font_funcs_t
*klass
,
1105 hb_destroy_func_t destroy
)
1107 if (font
->immutable
) {
1109 destroy (font_data
);
1114 font
->destroy (font
->user_data
);
1117 klass
= hb_font_funcs_get_empty ();
1119 hb_font_funcs_reference (klass
);
1120 hb_font_funcs_destroy (font
->klass
);
1121 font
->klass
= klass
;
1122 font
->user_data
= font_data
;
1123 font
->destroy
= destroy
;
1127 * hb_font_set_funcs_data:
1129 * @font_data: (destroy destroy) (scope notified):
1137 hb_font_set_funcs_data (hb_font_t
*font
,
1139 hb_destroy_func_t destroy
)
1141 /* Destroy user_data? */
1142 if (font
->immutable
) {
1144 destroy (font_data
);
1149 font
->destroy (font
->user_data
);
1151 font
->user_data
= font_data
;
1152 font
->destroy
= destroy
;
1157 * hb_font_set_scale:
1167 hb_font_set_scale (hb_font_t
*font
,
1171 if (font
->immutable
)
1174 font
->x_scale
= x_scale
;
1175 font
->y_scale
= y_scale
;
1179 * hb_font_get_scale:
1189 hb_font_get_scale (hb_font_t
*font
,
1193 if (x_scale
) *x_scale
= font
->x_scale
;
1194 if (y_scale
) *y_scale
= font
->y_scale
;
1208 hb_font_set_ppem (hb_font_t
*font
,
1209 unsigned int x_ppem
,
1210 unsigned int y_ppem
)
1212 if (font
->immutable
)
1215 font
->x_ppem
= x_ppem
;
1216 font
->y_ppem
= y_ppem
;
1230 hb_font_get_ppem (hb_font_t
*font
,
1231 unsigned int *x_ppem
,
1232 unsigned int *y_ppem
)
1234 if (x_ppem
) *x_ppem
= font
->x_ppem
;
1235 if (y_ppem
) *y_ppem
= font
->y_ppem
;