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):
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 (unlikely (hb_object_is_inert (face
)))
858 return hb_font_get_empty ();
859 if (!(font
= hb_object_create
<hb_font_t
> ()))
860 return hb_font_get_empty ();
862 hb_face_make_immutable (face
);
863 font
->face
= hb_face_reference (face
);
864 font
->klass
= hb_font_funcs_get_empty ();
870 * hb_font_create_sub_font:
871 * @parent: parent font.
875 * Return value: (transfer full):
880 hb_font_create_sub_font (hb_font_t
*parent
)
882 if (unlikely (!parent
))
883 return hb_font_get_empty ();
885 hb_font_t
*font
= hb_font_create (parent
->face
);
887 if (unlikely (hb_object_is_inert (font
)))
890 hb_font_make_immutable (parent
);
891 font
->parent
= hb_font_reference (parent
);
893 font
->x_scale
= parent
->x_scale
;
894 font
->y_scale
= parent
->y_scale
;
895 font
->x_ppem
= parent
->x_ppem
;
896 font
->y_ppem
= parent
->y_ppem
;
906 * Return value: (transfer full)
911 hb_font_get_empty (void)
913 static const hb_font_t _hb_font_nil
= {
914 HB_OBJECT_HEADER_STATIC
,
916 true, /* immutable */
919 const_cast<hb_face_t
*> (&_hb_face_nil
),
927 const_cast<hb_font_funcs_t
*> (&_hb_font_funcs_nil
), /* klass */
928 NULL
, /* user_data */
932 #define HB_SHAPER_IMPLEMENT(shaper) HB_SHAPER_DATA_INVALID,
933 #include "hb-shaper-list.hh"
934 #undef HB_SHAPER_IMPLEMENT
938 return const_cast<hb_font_t
*> (&_hb_font_nil
);
942 * hb_font_reference: (skip)
947 * Return value: (transfer full):
952 hb_font_reference (hb_font_t
*font
)
954 return hb_object_reference (font
);
958 * hb_font_destroy: (skip)
966 hb_font_destroy (hb_font_t
*font
)
968 if (!hb_object_destroy (font
)) return;
970 #define HB_SHAPER_IMPLEMENT(shaper) HB_SHAPER_DATA_DESTROY(shaper, font);
971 #include "hb-shaper-list.hh"
972 #undef HB_SHAPER_IMPLEMENT
975 font
->destroy (font
->user_data
);
977 hb_font_destroy (font
->parent
);
978 hb_face_destroy (font
->face
);
979 hb_font_funcs_destroy (font
->klass
);
985 * hb_font_set_user_data: (skip)
999 hb_font_set_user_data (hb_font_t
*font
,
1000 hb_user_data_key_t
*key
,
1002 hb_destroy_func_t destroy
,
1005 return hb_object_set_user_data (font
, key
, data
, destroy
, replace
);
1009 * hb_font_get_user_data: (skip)
1015 * Return value: (transfer none):
1020 hb_font_get_user_data (hb_font_t
*font
,
1021 hb_user_data_key_t
*key
)
1023 return hb_object_get_user_data (font
, key
);
1027 * hb_font_make_immutable:
1035 hb_font_make_immutable (hb_font_t
*font
)
1037 if (unlikely (hb_object_is_inert (font
)))
1040 font
->immutable
= true;
1044 * hb_font_is_immutable:
1054 hb_font_is_immutable (hb_font_t
*font
)
1056 return font
->immutable
;
1060 * hb_font_get_parent:
1065 * Return value: (transfer none):
1070 hb_font_get_parent (hb_font_t
*font
)
1072 return font
->parent
;
1081 * Return value: (transfer none):
1086 hb_font_get_face (hb_font_t
*font
)
1093 * hb_font_set_funcs:
1095 * @klass: (closure font_data) (destroy destroy) (scope notified):
1104 hb_font_set_funcs (hb_font_t
*font
,
1105 hb_font_funcs_t
*klass
,
1107 hb_destroy_func_t destroy
)
1109 if (font
->immutable
) {
1111 destroy (font_data
);
1116 font
->destroy (font
->user_data
);
1119 klass
= hb_font_funcs_get_empty ();
1121 hb_font_funcs_reference (klass
);
1122 hb_font_funcs_destroy (font
->klass
);
1123 font
->klass
= klass
;
1124 font
->user_data
= font_data
;
1125 font
->destroy
= destroy
;
1129 * hb_font_set_funcs_data:
1131 * @font_data: (destroy destroy) (scope notified):
1139 hb_font_set_funcs_data (hb_font_t
*font
,
1141 hb_destroy_func_t destroy
)
1143 /* Destroy user_data? */
1144 if (font
->immutable
) {
1146 destroy (font_data
);
1151 font
->destroy (font
->user_data
);
1153 font
->user_data
= font_data
;
1154 font
->destroy
= destroy
;
1159 * hb_font_set_scale:
1169 hb_font_set_scale (hb_font_t
*font
,
1173 if (font
->immutable
)
1176 font
->x_scale
= x_scale
;
1177 font
->y_scale
= y_scale
;
1181 * hb_font_get_scale:
1191 hb_font_get_scale (hb_font_t
*font
,
1195 if (x_scale
) *x_scale
= font
->x_scale
;
1196 if (y_scale
) *y_scale
= font
->y_scale
;
1210 hb_font_set_ppem (hb_font_t
*font
,
1211 unsigned int x_ppem
,
1212 unsigned int y_ppem
)
1214 if (font
->immutable
)
1217 font
->x_ppem
= x_ppem
;
1218 font
->y_ppem
= y_ppem
;
1232 hb_font_get_ppem (hb_font_t
*font
,
1233 unsigned int *x_ppem
,
1234 unsigned int *y_ppem
)
1236 if (x_ppem
) *x_ppem
= font
->x_ppem
;
1237 if (y_ppem
) *y_ppem
= font
->y_ppem
;