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"
35 #include "hb-open-file-private.hh"
36 #include "hb-ot-head-table.hh"
37 #include "hb-ot-maxp-table.hh"
39 #include "hb-cache-private.hh"
49 hb_font_get_glyph_nil (hb_font_t
*font
,
50 void *font_data HB_UNUSED
,
51 hb_codepoint_t unicode
,
52 hb_codepoint_t variation_selector
,
53 hb_codepoint_t
*glyph
,
54 void *user_data HB_UNUSED
)
57 return font
->parent
->get_glyph (unicode
, variation_selector
, glyph
);
64 hb_font_get_glyph_h_advance_nil (hb_font_t
*font
,
65 void *font_data HB_UNUSED
,
67 void *user_data HB_UNUSED
)
70 return font
->parent_scale_x_distance (font
->parent
->get_glyph_h_advance (glyph
));
76 hb_font_get_glyph_v_advance_nil (hb_font_t
*font
,
77 void *font_data HB_UNUSED
,
79 void *user_data HB_UNUSED
)
82 return font
->parent_scale_y_distance (font
->parent
->get_glyph_v_advance (glyph
));
88 hb_font_get_glyph_h_origin_nil (hb_font_t
*font
,
89 void *font_data HB_UNUSED
,
93 void *user_data HB_UNUSED
)
96 hb_bool_t ret
= font
->parent
->get_glyph_h_origin (glyph
, x
, y
);
98 font
->parent_scale_position (x
, y
);
107 hb_font_get_glyph_v_origin_nil (hb_font_t
*font
,
108 void *font_data HB_UNUSED
,
109 hb_codepoint_t glyph
,
112 void *user_data HB_UNUSED
)
115 hb_bool_t ret
= font
->parent
->get_glyph_v_origin (glyph
, x
, y
);
117 font
->parent_scale_position (x
, y
);
126 hb_font_get_glyph_h_kerning_nil (hb_font_t
*font
,
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 (font
->parent
->get_glyph_h_kerning (left_glyph
, right_glyph
));
139 hb_font_get_glyph_v_kerning_nil (hb_font_t
*font
,
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 (font
->parent
->get_glyph_v_kerning (top_glyph
, bottom_glyph
));
152 hb_font_get_glyph_extents_nil (hb_font_t
*font
,
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
= font
->parent
->get_glyph_extents (glyph
, extents
);
161 font
->parent_scale_position (&extents
->x_bearing
, &extents
->y_bearing
);
162 font
->parent_scale_distance (&extents
->width
, &extents
->height
);
167 memset (extents
, 0, sizeof (*extents
));
172 hb_font_get_glyph_contour_point_nil (hb_font_t
*font
,
173 void *font_data HB_UNUSED
,
174 hb_codepoint_t glyph
,
175 unsigned int point_index
,
178 void *user_data HB_UNUSED
)
181 hb_bool_t ret
= font
->parent
->get_glyph_contour_point (glyph
, point_index
, x
, y
);
183 font
->parent_scale_position (x
, y
);
192 hb_font_get_glyph_name_nil (hb_font_t
*font
,
193 void *font_data HB_UNUSED
,
194 hb_codepoint_t glyph
,
195 char *name
, unsigned int size
,
196 void *user_data HB_UNUSED
)
199 return font
->parent
->get_glyph_name (glyph
, name
, size
);
201 if (size
) *name
= '\0';
206 hb_font_get_glyph_from_name_nil (hb_font_t
*font
,
207 void *font_data HB_UNUSED
,
208 const char *name
, int len
, /* -1 means nul-terminated */
209 hb_codepoint_t
*glyph
,
210 void *user_data HB_UNUSED
)
213 return font
->parent
->get_glyph_from_name (name
, len
, glyph
);
220 static const hb_font_funcs_t _hb_font_funcs_nil
= {
221 HB_OBJECT_HEADER_STATIC
,
223 true, /* immutable */
226 #define HB_FONT_FUNC_IMPLEMENT(name) hb_font_get_##name##_nil,
227 HB_FONT_FUNCS_IMPLEMENT_CALLBACKS
228 #undef HB_FONT_FUNC_IMPLEMENT
234 * hb_font_funcs_create: (Xconstructor)
238 * Return value: (transfer full):
243 hb_font_funcs_create (void)
245 hb_font_funcs_t
*ffuncs
;
247 if (!(ffuncs
= hb_object_create
<hb_font_funcs_t
> ()))
248 return hb_font_funcs_get_empty ();
250 ffuncs
->get
= _hb_font_funcs_nil
.get
;
256 * hb_font_funcs_get_empty:
260 * Return value: (transfer full):
265 hb_font_funcs_get_empty (void)
267 return const_cast<hb_font_funcs_t
*> (&_hb_font_funcs_nil
);
271 * hb_font_funcs_reference: (skip)
272 * @ffuncs: font functions.
281 hb_font_funcs_reference (hb_font_funcs_t
*ffuncs
)
283 return hb_object_reference (ffuncs
);
287 * hb_font_funcs_destroy: (skip)
288 * @ffuncs: font functions.
295 hb_font_funcs_destroy (hb_font_funcs_t
*ffuncs
)
297 if (!hb_object_destroy (ffuncs
)) return;
299 #define HB_FONT_FUNC_IMPLEMENT(name) if (ffuncs->destroy.name) \
300 ffuncs->destroy.name (ffuncs->user_data.name);
301 HB_FONT_FUNCS_IMPLEMENT_CALLBACKS
302 #undef HB_FONT_FUNC_IMPLEMENT
308 * hb_font_funcs_set_user_data: (skip)
309 * @ffuncs: font functions.
322 hb_font_funcs_set_user_data (hb_font_funcs_t
*ffuncs
,
323 hb_user_data_key_t
*key
,
325 hb_destroy_func_t destroy
,
328 return hb_object_set_user_data (ffuncs
, key
, data
, destroy
, replace
);
332 * hb_font_funcs_get_user_data: (skip)
333 * @ffuncs: font functions.
338 * Return value: (transfer none):
343 hb_font_funcs_get_user_data (hb_font_funcs_t
*ffuncs
,
344 hb_user_data_key_t
*key
)
346 return hb_object_get_user_data (ffuncs
, key
);
351 * hb_font_funcs_make_immutable:
352 * @ffuncs: font functions.
359 hb_font_funcs_make_immutable (hb_font_funcs_t
*ffuncs
)
361 if (hb_object_is_inert (ffuncs
))
364 ffuncs
->immutable
= true;
368 * hb_font_funcs_is_immutable:
369 * @ffuncs: font functions.
378 hb_font_funcs_is_immutable (hb_font_funcs_t
*ffuncs
)
380 return ffuncs
->immutable
;
384 #define HB_FONT_FUNC_IMPLEMENT(name) \
387 hb_font_funcs_set_##name##_func (hb_font_funcs_t *ffuncs, \
388 hb_font_get_##name##_func_t func, \
390 hb_destroy_func_t destroy) \
392 if (ffuncs->immutable) { \
394 destroy (user_data); \
398 if (ffuncs->destroy.name) \
399 ffuncs->destroy.name (ffuncs->user_data.name); \
402 ffuncs->get.name = func; \
403 ffuncs->user_data.name = user_data; \
404 ffuncs->destroy.name = destroy; \
406 ffuncs->get.name = hb_font_get_##name##_nil; \
407 ffuncs->user_data.name = NULL; \
408 ffuncs->destroy.name = NULL; \
412 HB_FONT_FUNCS_IMPLEMENT_CALLBACKS
413 #undef HB_FONT_FUNC_IMPLEMENT
422 * @variation_selector:
432 hb_font_get_glyph (hb_font_t
*font
,
433 hb_codepoint_t unicode
, hb_codepoint_t variation_selector
,
434 hb_codepoint_t
*glyph
)
436 return font
->get_glyph (unicode
, variation_selector
, glyph
);
440 * hb_font_get_glyph_h_advance:
451 hb_font_get_glyph_h_advance (hb_font_t
*font
,
452 hb_codepoint_t glyph
)
454 return font
->get_glyph_h_advance (glyph
);
458 * hb_font_get_glyph_v_advance:
469 hb_font_get_glyph_v_advance (hb_font_t
*font
,
470 hb_codepoint_t glyph
)
472 return font
->get_glyph_v_advance (glyph
);
476 * hb_font_get_glyph_h_origin:
489 hb_font_get_glyph_h_origin (hb_font_t
*font
,
490 hb_codepoint_t glyph
,
491 hb_position_t
*x
, hb_position_t
*y
)
493 return font
->get_glyph_h_origin (glyph
, x
, y
);
497 * hb_font_get_glyph_v_origin:
510 hb_font_get_glyph_v_origin (hb_font_t
*font
,
511 hb_codepoint_t glyph
,
512 hb_position_t
*x
, hb_position_t
*y
)
514 return font
->get_glyph_v_origin (glyph
, x
, y
);
518 * hb_font_get_glyph_h_kerning:
530 hb_font_get_glyph_h_kerning (hb_font_t
*font
,
531 hb_codepoint_t left_glyph
, hb_codepoint_t right_glyph
)
533 return font
->get_glyph_h_kerning (left_glyph
, right_glyph
);
537 * hb_font_get_glyph_v_kerning:
549 hb_font_get_glyph_v_kerning (hb_font_t
*font
,
550 hb_codepoint_t top_glyph
, hb_codepoint_t bottom_glyph
)
552 return font
->get_glyph_v_kerning (top_glyph
, bottom_glyph
);
556 * hb_font_get_glyph_extents:
568 hb_font_get_glyph_extents (hb_font_t
*font
,
569 hb_codepoint_t glyph
,
570 hb_glyph_extents_t
*extents
)
572 return font
->get_glyph_extents (glyph
, extents
);
576 * hb_font_get_glyph_contour_point:
590 hb_font_get_glyph_contour_point (hb_font_t
*font
,
591 hb_codepoint_t glyph
, unsigned int point_index
,
592 hb_position_t
*x
, hb_position_t
*y
)
594 return font
->get_glyph_contour_point (glyph
, point_index
, x
, y
);
598 * hb_font_get_glyph_name:
601 * @name: (array length=size):
611 hb_font_get_glyph_name (hb_font_t
*font
,
612 hb_codepoint_t glyph
,
613 char *name
, unsigned int size
)
615 return font
->get_glyph_name (glyph
, name
, size
);
619 * hb_font_get_glyph_from_name:
621 * @name: (array length=len):
632 hb_font_get_glyph_from_name (hb_font_t
*font
,
633 const char *name
, int len
, /* -1 means nul-terminated */
634 hb_codepoint_t
*glyph
)
636 return font
->get_glyph_from_name (name
, len
, glyph
);
640 /* A bit higher-level, and with fallback */
643 * hb_font_get_glyph_advance_for_direction:
655 hb_font_get_glyph_advance_for_direction (hb_font_t
*font
,
656 hb_codepoint_t glyph
,
657 hb_direction_t direction
,
658 hb_position_t
*x
, hb_position_t
*y
)
660 return font
->get_glyph_advance_for_direction (glyph
, direction
, x
, y
);
664 * hb_font_get_glyph_origin_for_direction:
676 hb_font_get_glyph_origin_for_direction (hb_font_t
*font
,
677 hb_codepoint_t glyph
,
678 hb_direction_t direction
,
679 hb_position_t
*x
, hb_position_t
*y
)
681 return font
->get_glyph_origin_for_direction (glyph
, direction
, x
, y
);
685 * hb_font_add_glyph_origin_for_direction:
697 hb_font_add_glyph_origin_for_direction (hb_font_t
*font
,
698 hb_codepoint_t glyph
,
699 hb_direction_t direction
,
700 hb_position_t
*x
, hb_position_t
*y
)
702 return font
->add_glyph_origin_for_direction (glyph
, direction
, x
, y
);
706 * hb_font_subtract_glyph_origin_for_direction:
718 hb_font_subtract_glyph_origin_for_direction (hb_font_t
*font
,
719 hb_codepoint_t glyph
,
720 hb_direction_t direction
,
721 hb_position_t
*x
, hb_position_t
*y
)
723 return font
->subtract_glyph_origin_for_direction (glyph
, direction
, x
, y
);
727 * hb_font_get_glyph_kerning_for_direction:
740 hb_font_get_glyph_kerning_for_direction (hb_font_t
*font
,
741 hb_codepoint_t first_glyph
, hb_codepoint_t second_glyph
,
742 hb_direction_t direction
,
743 hb_position_t
*x
, hb_position_t
*y
)
745 return font
->get_glyph_kerning_for_direction (first_glyph
, second_glyph
, direction
, x
, y
);
749 * hb_font_get_glyph_extents_for_origin:
762 hb_font_get_glyph_extents_for_origin (hb_font_t
*font
,
763 hb_codepoint_t glyph
,
764 hb_direction_t direction
,
765 hb_glyph_extents_t
*extents
)
767 return font
->get_glyph_extents_for_origin (glyph
, direction
, extents
);
771 * hb_font_get_glyph_contour_point_for_origin:
786 hb_font_get_glyph_contour_point_for_origin (hb_font_t
*font
,
787 hb_codepoint_t glyph
, unsigned int point_index
,
788 hb_direction_t direction
,
789 hb_position_t
*x
, hb_position_t
*y
)
791 return font
->get_glyph_contour_point_for_origin (glyph
, point_index
, direction
, x
, y
);
794 /* Generates gidDDD if glyph has no name. */
796 * hb_font_glyph_to_string:
799 * @s: (array length=size):
807 hb_font_glyph_to_string (hb_font_t
*font
,
808 hb_codepoint_t glyph
,
809 char *s
, unsigned int size
)
811 font
->glyph_to_string (glyph
, s
, size
);
814 /* Parses gidDDD and uniUUUU strings automatically. */
816 * hb_font_glyph_from_string:
818 * @s: (array length=len):
829 hb_font_glyph_from_string (hb_font_t
*font
,
830 const char *s
, int len
, /* -1 means nul-terminated */
831 hb_codepoint_t
*glyph
)
833 return font
->glyph_from_string (s
, len
, glyph
);
842 * hb_font_create: (Xconstructor)
847 * Return value: (transfer full):
852 hb_font_create (hb_face_t
*face
)
856 if (unlikely (!face
))
857 face
= hb_face_get_empty ();
858 if (unlikely (hb_object_is_inert (face
)))
859 return hb_font_get_empty ();
860 if (!(font
= hb_object_create
<hb_font_t
> ()))
861 return hb_font_get_empty ();
863 hb_face_make_immutable (face
);
864 font
->face
= hb_face_reference (face
);
865 font
->klass
= hb_font_funcs_get_empty ();
871 * hb_font_create_sub_font:
872 * @parent: parent font.
876 * Return value: (transfer full):
881 hb_font_create_sub_font (hb_font_t
*parent
)
883 if (unlikely (!parent
))
884 return hb_font_get_empty ();
886 hb_font_t
*font
= hb_font_create (parent
->face
);
888 if (unlikely (hb_object_is_inert (font
)))
891 hb_font_make_immutable (parent
);
892 font
->parent
= hb_font_reference (parent
);
894 font
->x_scale
= parent
->x_scale
;
895 font
->y_scale
= parent
->y_scale
;
896 font
->x_ppem
= parent
->x_ppem
;
897 font
->y_ppem
= parent
->y_ppem
;
907 * Return value: (transfer full)
912 hb_font_get_empty (void)
914 static const hb_font_t _hb_font_nil
= {
915 HB_OBJECT_HEADER_STATIC
,
917 true, /* immutable */
920 const_cast<hb_face_t
*> (&_hb_face_nil
),
928 const_cast<hb_font_funcs_t
*> (&_hb_font_funcs_nil
), /* klass */
929 NULL
, /* user_data */
933 #define HB_SHAPER_IMPLEMENT(shaper) HB_SHAPER_DATA_INVALID,
934 #include "hb-shaper-list.hh"
935 #undef HB_SHAPER_IMPLEMENT
939 return const_cast<hb_font_t
*> (&_hb_font_nil
);
943 * hb_font_reference: (skip)
948 * Return value: (transfer full):
953 hb_font_reference (hb_font_t
*font
)
955 return hb_object_reference (font
);
959 * hb_font_destroy: (skip)
967 hb_font_destroy (hb_font_t
*font
)
969 if (!hb_object_destroy (font
)) return;
971 #define HB_SHAPER_IMPLEMENT(shaper) HB_SHAPER_DATA_DESTROY(shaper, font);
972 #include "hb-shaper-list.hh"
973 #undef HB_SHAPER_IMPLEMENT
976 font
->destroy (font
->user_data
);
978 hb_font_destroy (font
->parent
);
979 hb_face_destroy (font
->face
);
980 hb_font_funcs_destroy (font
->klass
);
986 * hb_font_set_user_data: (skip)
1000 hb_font_set_user_data (hb_font_t
*font
,
1001 hb_user_data_key_t
*key
,
1003 hb_destroy_func_t destroy
,
1006 return hb_object_set_user_data (font
, key
, data
, destroy
, replace
);
1010 * hb_font_get_user_data: (skip)
1016 * Return value: (transfer none):
1021 hb_font_get_user_data (hb_font_t
*font
,
1022 hb_user_data_key_t
*key
)
1024 return hb_object_get_user_data (font
, key
);
1028 * hb_font_make_immutable:
1036 hb_font_make_immutable (hb_font_t
*font
)
1038 if (hb_object_is_inert (font
))
1041 font
->immutable
= true;
1045 * hb_font_is_immutable:
1055 hb_font_is_immutable (hb_font_t
*font
)
1057 return font
->immutable
;
1061 * hb_font_get_parent:
1066 * Return value: (transfer none):
1071 hb_font_get_parent (hb_font_t
*font
)
1073 return font
->parent
;
1082 * Return value: (transfer none):
1087 hb_font_get_face (hb_font_t
*font
)
1094 * hb_font_set_funcs:
1096 * @klass: (closure font_data) (destroy destroy) (scope notified):
1105 hb_font_set_funcs (hb_font_t
*font
,
1106 hb_font_funcs_t
*klass
,
1108 hb_destroy_func_t destroy
)
1110 if (font
->immutable
) {
1112 destroy (font_data
);
1117 font
->destroy (font
->user_data
);
1120 klass
= hb_font_funcs_get_empty ();
1122 hb_font_funcs_reference (klass
);
1123 hb_font_funcs_destroy (font
->klass
);
1124 font
->klass
= klass
;
1125 font
->user_data
= font_data
;
1126 font
->destroy
= destroy
;
1130 * hb_font_set_funcs_data:
1132 * @font_data: (destroy destroy) (scope notified):
1140 hb_font_set_funcs_data (hb_font_t
*font
,
1142 hb_destroy_func_t destroy
)
1144 /* Destroy user_data? */
1145 if (font
->immutable
) {
1147 destroy (font_data
);
1152 font
->destroy (font
->user_data
);
1154 font
->user_data
= font_data
;
1155 font
->destroy
= destroy
;
1160 * hb_font_set_scale:
1170 hb_font_set_scale (hb_font_t
*font
,
1174 if (font
->immutable
)
1177 font
->x_scale
= x_scale
;
1178 font
->y_scale
= y_scale
;
1182 * hb_font_get_scale:
1192 hb_font_get_scale (hb_font_t
*font
,
1196 if (x_scale
) *x_scale
= font
->x_scale
;
1197 if (y_scale
) *y_scale
= font
->y_scale
;
1211 hb_font_set_ppem (hb_font_t
*font
,
1212 unsigned int x_ppem
,
1213 unsigned int y_ppem
)
1215 if (font
->immutable
)
1218 font
->x_ppem
= x_ppem
;
1219 font
->y_ppem
= y_ppem
;
1233 hb_font_get_ppem (hb_font_t
*font
,
1234 unsigned int *x_ppem
,
1235 unsigned int *y_ppem
)
1237 if (x_ppem
) *x_ppem
= font
->x_ppem
;
1238 if (y_ppem
) *y_ppem
= font
->y_ppem
;