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
32 #include "hb-machinery.hh"
40 * @short_description: Font objects
43 * Font objects represent a font face at a certain size and other
44 * parameters (pixels per EM, points per EM, variation settings.)
45 * Fonts are created from font faces, and are used as input to
46 * hb_shape() among other things.
55 hb_font_get_font_h_extents_nil (hb_font_t *font HB_UNUSED,
56 void *font_data HB_UNUSED,
57 hb_font_extents_t *extents,
58 void *user_data HB_UNUSED)
60 memset (extents, 0, sizeof (*extents));
64 hb_font_get_font_h_extents_default (hb_font_t *font,
65 void *font_data HB_UNUSED,
66 hb_font_extents_t *extents,
67 void *user_data HB_UNUSED)
69 hb_bool_t ret = font->parent->get_font_h_extents (extents);
71 extents->ascender = font->parent_scale_y_distance (extents->ascender);
72 extents->descender = font->parent_scale_y_distance (extents->descender);
73 extents->line_gap = font->parent_scale_y_distance (extents->line_gap);
79 hb_font_get_font_v_extents_nil (hb_font_t *font HB_UNUSED,
80 void *font_data HB_UNUSED,
81 hb_font_extents_t *extents,
82 void *user_data HB_UNUSED)
84 memset (extents, 0, sizeof (*extents));
88 hb_font_get_font_v_extents_default (hb_font_t *font,
89 void *font_data HB_UNUSED,
90 hb_font_extents_t *extents,
91 void *user_data HB_UNUSED)
93 hb_bool_t ret = font->parent->get_font_v_extents (extents);
95 extents->ascender = font->parent_scale_x_distance (extents->ascender);
96 extents->descender = font->parent_scale_x_distance (extents->descender);
97 extents->line_gap = font->parent_scale_x_distance (extents->line_gap);
103 hb_font_get_nominal_glyph_nil (hb_font_t *font HB_UNUSED,
104 void *font_data HB_UNUSED,
105 hb_codepoint_t unicode HB_UNUSED,
106 hb_codepoint_t *glyph,
107 void *user_data HB_UNUSED)
113 hb_font_get_nominal_glyph_default (hb_font_t *font,
114 void *font_data HB_UNUSED,
115 hb_codepoint_t unicode,
116 hb_codepoint_t *glyph,
117 void *user_data HB_UNUSED)
119 if (font->has_nominal_glyphs_func_set ())
121 return font->get_nominal_glyphs (1, &unicode, 0, glyph, 0);
123 return font->parent->get_nominal_glyph (unicode, glyph);
126 #define hb_font_get_nominal_glyphs_nil hb_font_get_nominal_glyphs_default
128 hb_font_get_nominal_glyphs_default (hb_font_t *font,
129 void *font_data HB_UNUSED,
131 const hb_codepoint_t *first_unicode,
132 unsigned int unicode_stride,
133 hb_codepoint_t *first_glyph,
134 unsigned int glyph_stride,
135 void *user_data HB_UNUSED)
137 if (font->has_nominal_glyph_func_set ())
139 for (unsigned int i = 0; i < count; i++)
141 if (!font->get_nominal_glyph (*first_unicode, first_glyph))
144 first_unicode = &StructAtOffsetUnaligned<hb_codepoint_t> (first_unicode, unicode_stride);
145 first_glyph = &StructAtOffsetUnaligned<hb_codepoint_t> (first_glyph, glyph_stride);
150 return font->parent->get_nominal_glyphs (count,
151 first_unicode, unicode_stride,
152 first_glyph, glyph_stride);
156 hb_font_get_variation_glyph_nil (hb_font_t *font HB_UNUSED,
157 void *font_data HB_UNUSED,
158 hb_codepoint_t unicode HB_UNUSED,
159 hb_codepoint_t variation_selector HB_UNUSED,
160 hb_codepoint_t *glyph,
161 void *user_data HB_UNUSED)
167 hb_font_get_variation_glyph_default (hb_font_t *font,
168 void *font_data HB_UNUSED,
169 hb_codepoint_t unicode,
170 hb_codepoint_t variation_selector,
171 hb_codepoint_t *glyph,
172 void *user_data HB_UNUSED)
174 return font->parent->get_variation_glyph (unicode, variation_selector, glyph);
179 hb_font_get_glyph_h_advance_nil (hb_font_t *font,
180 void *font_data HB_UNUSED,
181 hb_codepoint_t glyph HB_UNUSED,
182 void *user_data HB_UNUSED)
184 return font->x_scale;
187 hb_font_get_glyph_h_advance_default (hb_font_t *font,
188 void *font_data HB_UNUSED,
189 hb_codepoint_t glyph,
190 void *user_data HB_UNUSED)
192 if (font->has_glyph_h_advances_func_set ())
195 font->get_glyph_h_advances (1, &glyph, 0, &ret, 0);
198 return font->parent_scale_x_distance (font->parent->get_glyph_h_advance (glyph));
202 hb_font_get_glyph_v_advance_nil (hb_font_t *font,
203 void *font_data HB_UNUSED,
204 hb_codepoint_t glyph HB_UNUSED,
205 void *user_data HB_UNUSED)
207 /* TODO use font_extents.ascender+descender */
208 return font->y_scale;
211 hb_font_get_glyph_v_advance_default (hb_font_t *font,
212 void *font_data HB_UNUSED,
213 hb_codepoint_t glyph,
214 void *user_data HB_UNUSED)
216 if (font->has_glyph_v_advances_func_set ())
219 font->get_glyph_v_advances (1, &glyph, 0, &ret, 0);
222 return font->parent_scale_y_distance (font->parent->get_glyph_v_advance (glyph));
225 #define hb_font_get_glyph_h_advances_nil hb_font_get_glyph_h_advances_default
227 hb_font_get_glyph_h_advances_default (hb_font_t* font,
228 void* font_data HB_UNUSED,
230 const hb_codepoint_t *first_glyph,
231 unsigned int glyph_stride,
232 hb_position_t *first_advance,
233 unsigned int advance_stride,
234 void *user_data HB_UNUSED)
236 if (font->has_glyph_h_advance_func_set ())
238 for (unsigned int i = 0; i < count; i++)
240 *first_advance = font->get_glyph_h_advance (*first_glyph);
241 first_glyph = &StructAtOffsetUnaligned<hb_codepoint_t> (first_glyph, glyph_stride);
242 first_advance = &StructAtOffsetUnaligned<hb_position_t> (first_advance, advance_stride);
247 font->parent->get_glyph_h_advances (count,
248 first_glyph, glyph_stride,
249 first_advance, advance_stride);
250 for (unsigned int i = 0; i < count; i++)
252 *first_advance = font->parent_scale_x_distance (*first_advance);
253 first_advance = &StructAtOffsetUnaligned<hb_position_t> (first_advance, advance_stride);
257 #define hb_font_get_glyph_v_advances_nil hb_font_get_glyph_v_advances_default
259 hb_font_get_glyph_v_advances_default (hb_font_t* font,
260 void* font_data HB_UNUSED,
262 const hb_codepoint_t *first_glyph,
263 unsigned int glyph_stride,
264 hb_position_t *first_advance,
265 unsigned int advance_stride,
266 void *user_data HB_UNUSED)
268 if (font->has_glyph_v_advance_func_set ())
270 for (unsigned int i = 0; i < count; i++)
272 *first_advance = font->get_glyph_v_advance (*first_glyph);
273 first_glyph = &StructAtOffsetUnaligned<hb_codepoint_t> (first_glyph, glyph_stride);
274 first_advance = &StructAtOffsetUnaligned<hb_position_t> (first_advance, advance_stride);
279 font->parent->get_glyph_v_advances (count,
280 first_glyph, glyph_stride,
281 first_advance, advance_stride);
282 for (unsigned int i = 0; i < count; i++)
284 *first_advance = font->parent_scale_y_distance (*first_advance);
285 first_advance = &StructAtOffsetUnaligned<hb_position_t> (first_advance, advance_stride);
290 hb_font_get_glyph_h_origin_nil (hb_font_t *font HB_UNUSED,
291 void *font_data HB_UNUSED,
292 hb_codepoint_t glyph HB_UNUSED,
295 void *user_data HB_UNUSED)
301 hb_font_get_glyph_h_origin_default (hb_font_t *font,
302 void *font_data HB_UNUSED,
303 hb_codepoint_t glyph,
306 void *user_data HB_UNUSED)
308 hb_bool_t ret = font->parent->get_glyph_h_origin (glyph, x, y);
310 font->parent_scale_position (x, y);
315 hb_font_get_glyph_v_origin_nil (hb_font_t *font HB_UNUSED,
316 void *font_data HB_UNUSED,
317 hb_codepoint_t glyph HB_UNUSED,
320 void *user_data HB_UNUSED)
326 hb_font_get_glyph_v_origin_default (hb_font_t *font,
327 void *font_data HB_UNUSED,
328 hb_codepoint_t glyph,
331 void *user_data HB_UNUSED)
333 hb_bool_t ret = font->parent->get_glyph_v_origin (glyph, x, y);
335 font->parent_scale_position (x, y);
340 hb_font_get_glyph_h_kerning_nil (hb_font_t *font HB_UNUSED,
341 void *font_data HB_UNUSED,
342 hb_codepoint_t left_glyph HB_UNUSED,
343 hb_codepoint_t right_glyph HB_UNUSED,
344 void *user_data HB_UNUSED)
349 hb_font_get_glyph_h_kerning_default (hb_font_t *font,
350 void *font_data HB_UNUSED,
351 hb_codepoint_t left_glyph,
352 hb_codepoint_t right_glyph,
353 void *user_data HB_UNUSED)
355 return font->parent_scale_x_distance (font->parent->get_glyph_h_kerning (left_glyph, right_glyph));
359 hb_font_get_glyph_v_kerning_nil (hb_font_t *font HB_UNUSED,
360 void *font_data HB_UNUSED,
361 hb_codepoint_t top_glyph HB_UNUSED,
362 hb_codepoint_t bottom_glyph HB_UNUSED,
363 void *user_data HB_UNUSED)
368 hb_font_get_glyph_v_kerning_default (hb_font_t *font,
369 void *font_data HB_UNUSED,
370 hb_codepoint_t top_glyph,
371 hb_codepoint_t bottom_glyph,
372 void *user_data HB_UNUSED)
374 return font->parent_scale_y_distance (font->parent->get_glyph_v_kerning (top_glyph, bottom_glyph));
378 hb_font_get_glyph_extents_nil (hb_font_t *font HB_UNUSED,
379 void *font_data HB_UNUSED,
380 hb_codepoint_t glyph HB_UNUSED,
381 hb_glyph_extents_t *extents,
382 void *user_data HB_UNUSED)
384 memset (extents, 0, sizeof (*extents));
388 hb_font_get_glyph_extents_default (hb_font_t *font,
389 void *font_data HB_UNUSED,
390 hb_codepoint_t glyph,
391 hb_glyph_extents_t *extents,
392 void *user_data HB_UNUSED)
394 hb_bool_t ret = font->parent->get_glyph_extents (glyph, extents);
396 font->parent_scale_position (&extents->x_bearing, &extents->y_bearing);
397 font->parent_scale_distance (&extents->width, &extents->height);
403 hb_font_get_glyph_contour_point_nil (hb_font_t *font HB_UNUSED,
404 void *font_data HB_UNUSED,
405 hb_codepoint_t glyph HB_UNUSED,
406 unsigned int point_index HB_UNUSED,
409 void *user_data HB_UNUSED)
415 hb_font_get_glyph_contour_point_default (hb_font_t *font,
416 void *font_data HB_UNUSED,
417 hb_codepoint_t glyph,
418 unsigned int point_index,
421 void *user_data HB_UNUSED)
423 hb_bool_t ret = font->parent->get_glyph_contour_point (glyph, point_index, x, y);
425 font->parent_scale_position (x, y);
430 hb_font_get_glyph_name_nil (hb_font_t *font HB_UNUSED,
431 void *font_data HB_UNUSED,
432 hb_codepoint_t glyph HB_UNUSED,
433 char *name, unsigned int size,
434 void *user_data HB_UNUSED)
436 if (size) *name = '\0';
440 hb_font_get_glyph_name_default (hb_font_t *font,
441 void *font_data HB_UNUSED,
442 hb_codepoint_t glyph,
443 char *name, unsigned int size,
444 void *user_data HB_UNUSED)
446 return font->parent->get_glyph_name (glyph, name, size);
450 hb_font_get_glyph_from_name_nil (hb_font_t *font HB_UNUSED,
451 void *font_data HB_UNUSED,
452 const char *name HB_UNUSED,
453 int len HB_UNUSED, /* -1 means nul-terminated */
454 hb_codepoint_t *glyph,
455 void *user_data HB_UNUSED)
461 hb_font_get_glyph_from_name_default (hb_font_t *font,
462 void *font_data HB_UNUSED,
463 const char *name, int len, /* -1 means nul-terminated */
464 hb_codepoint_t *glyph,
465 void *user_data HB_UNUSED)
467 return font->parent->get_glyph_from_name (name, len, glyph);
470 DEFINE_NULL_INSTANCE (hb_font_funcs_t) =
472 HB_OBJECT_HEADER_STATIC,
475 #define HB_FONT_FUNC_IMPLEMENT(name) nullptr,
476 HB_FONT_FUNCS_IMPLEMENT_CALLBACKS
477 #undef HB_FONT_FUNC_IMPLEMENT
480 #define HB_FONT_FUNC_IMPLEMENT(name) nullptr,
481 HB_FONT_FUNCS_IMPLEMENT_CALLBACKS
482 #undef HB_FONT_FUNC_IMPLEMENT
486 #define HB_FONT_FUNC_IMPLEMENT(name) hb_font_get_##name##_nil,
487 HB_FONT_FUNCS_IMPLEMENT_CALLBACKS
488 #undef HB_FONT_FUNC_IMPLEMENT
493 static const hb_font_funcs_t _hb_font_funcs_default = {
494 HB_OBJECT_HEADER_STATIC,
497 #define HB_FONT_FUNC_IMPLEMENT(name) nullptr,
498 HB_FONT_FUNCS_IMPLEMENT_CALLBACKS
499 #undef HB_FONT_FUNC_IMPLEMENT
502 #define HB_FONT_FUNC_IMPLEMENT(name) nullptr,
503 HB_FONT_FUNCS_IMPLEMENT_CALLBACKS
504 #undef HB_FONT_FUNC_IMPLEMENT
508 #define HB_FONT_FUNC_IMPLEMENT(name) hb_font_get_##name##_default,
509 HB_FONT_FUNCS_IMPLEMENT_CALLBACKS
510 #undef HB_FONT_FUNC_IMPLEMENT
517 * hb_font_funcs_create: (Xconstructor)
521 * Return value: (transfer full):
526 hb_font_funcs_create ()
528 hb_font_funcs_t *ffuncs;
530 if (!(ffuncs = hb_object_create<hb_font_funcs_t> ()))
531 return hb_font_funcs_get_empty ();
533 ffuncs->get = _hb_font_funcs_default.get;
539 * hb_font_funcs_get_empty:
543 * Return value: (transfer full):
548 hb_font_funcs_get_empty ()
550 return const_cast<hb_font_funcs_t *> (&_hb_font_funcs_default);
554 * hb_font_funcs_reference: (skip)
555 * @ffuncs: font functions.
564 hb_font_funcs_reference (hb_font_funcs_t *ffuncs)
566 return hb_object_reference (ffuncs);
570 * hb_font_funcs_destroy: (skip)
571 * @ffuncs: font functions.
578 hb_font_funcs_destroy (hb_font_funcs_t *ffuncs)
580 if (!hb_object_destroy (ffuncs)) return;
582 #define HB_FONT_FUNC_IMPLEMENT(name) if (ffuncs->destroy.name) \
583 ffuncs->destroy.name (ffuncs->user_data.name);
584 HB_FONT_FUNCS_IMPLEMENT_CALLBACKS
585 #undef HB_FONT_FUNC_IMPLEMENT
591 * hb_font_funcs_set_user_data: (skip)
592 * @ffuncs: font functions.
605 hb_font_funcs_set_user_data (hb_font_funcs_t *ffuncs,
606 hb_user_data_key_t *key,
608 hb_destroy_func_t destroy,
611 return hb_object_set_user_data (ffuncs, key, data, destroy, replace);
615 * hb_font_funcs_get_user_data: (skip)
616 * @ffuncs: font functions.
621 * Return value: (transfer none):
626 hb_font_funcs_get_user_data (hb_font_funcs_t *ffuncs,
627 hb_user_data_key_t *key)
629 return hb_object_get_user_data (ffuncs, key);
634 * hb_font_funcs_make_immutable:
635 * @ffuncs: font functions.
642 hb_font_funcs_make_immutable (hb_font_funcs_t *ffuncs)
644 if (hb_object_is_immutable (ffuncs))
647 hb_object_make_immutable (ffuncs);
651 * hb_font_funcs_is_immutable:
652 * @ffuncs: font functions.
661 hb_font_funcs_is_immutable (hb_font_funcs_t *ffuncs)
663 return hb_object_is_immutable (ffuncs);
667 #define HB_FONT_FUNC_IMPLEMENT(name) \
670 hb_font_funcs_set_##name##_func (hb_font_funcs_t *ffuncs, \
671 hb_font_get_##name##_func_t func, \
673 hb_destroy_func_t destroy) \
675 if (hb_object_is_immutable (ffuncs)) { \
677 destroy (user_data); \
681 if (ffuncs->destroy.name) \
682 ffuncs->destroy.name (ffuncs->user_data.name); \
685 ffuncs->get.f.name = func; \
686 ffuncs->user_data.name = user_data; \
687 ffuncs->destroy.name = destroy; \
689 ffuncs->get.f.name = hb_font_get_##name##_default; \
690 ffuncs->user_data.name = nullptr; \
691 ffuncs->destroy.name = nullptr; \
695 HB_FONT_FUNCS_IMPLEMENT_CALLBACKS
696 #undef HB_FONT_FUNC_IMPLEMENT
699 hb_font_t::has_func_set (unsigned int i)
701 return this->klass->get.array[i] != _hb_font_funcs_default.get.array[i];
705 hb_font_t::has_func (unsigned int i)
707 return has_func_set (i) ||
708 (parent && parent != &_hb_Null_hb_font_t && parent->has_func (i));
714 * hb_font_get_h_extents:
725 hb_font_get_h_extents (hb_font_t *font,
726 hb_font_extents_t *extents)
728 return font->get_font_h_extents (extents);
732 * hb_font_get_v_extents:
743 hb_font_get_v_extents (hb_font_t *font,
744 hb_font_extents_t *extents)
746 return font->get_font_v_extents (extents);
753 * @variation_selector:
763 hb_font_get_glyph (hb_font_t *font,
764 hb_codepoint_t unicode, hb_codepoint_t variation_selector,
765 hb_codepoint_t *glyph)
767 if (unlikely (variation_selector))
768 return font->get_variation_glyph (unicode, variation_selector, glyph);
769 return font->get_nominal_glyph (unicode, glyph);
773 * hb_font_get_nominal_glyph:
785 hb_font_get_nominal_glyph (hb_font_t *font,
786 hb_codepoint_t unicode,
787 hb_codepoint_t *glyph)
789 return font->get_nominal_glyph (unicode, glyph);
793 * hb_font_get_variation_glyph:
796 * @variation_selector:
806 hb_font_get_variation_glyph (hb_font_t *font,
807 hb_codepoint_t unicode, hb_codepoint_t variation_selector,
808 hb_codepoint_t *glyph)
810 return font->get_variation_glyph (unicode, variation_selector, glyph);
814 * hb_font_get_glyph_h_advance:
825 hb_font_get_glyph_h_advance (hb_font_t *font,
826 hb_codepoint_t glyph)
828 return font->get_glyph_h_advance (glyph);
832 * hb_font_get_glyph_v_advance:
843 hb_font_get_glyph_v_advance (hb_font_t *font,
844 hb_codepoint_t glyph)
846 return font->get_glyph_v_advance (glyph);
850 * hb_font_get_glyph_h_advances:
858 hb_font_get_glyph_h_advances (hb_font_t* font,
860 const hb_codepoint_t *first_glyph,
861 unsigned glyph_stride,
862 hb_position_t *first_advance,
863 unsigned advance_stride)
865 font->get_glyph_h_advances (count, first_glyph, glyph_stride, first_advance, advance_stride);
868 * hb_font_get_glyph_v_advances:
876 hb_font_get_glyph_v_advances (hb_font_t* font,
878 const hb_codepoint_t *first_glyph,
879 unsigned glyph_stride,
880 hb_position_t *first_advance,
881 unsigned advance_stride)
883 font->get_glyph_v_advances (count, first_glyph, glyph_stride, first_advance, advance_stride);
887 * hb_font_get_glyph_h_origin:
900 hb_font_get_glyph_h_origin (hb_font_t *font,
901 hb_codepoint_t glyph,
902 hb_position_t *x, hb_position_t *y)
904 return font->get_glyph_h_origin (glyph, x, y);
908 * hb_font_get_glyph_v_origin:
921 hb_font_get_glyph_v_origin (hb_font_t *font,
922 hb_codepoint_t glyph,
923 hb_position_t *x, hb_position_t *y)
925 return font->get_glyph_v_origin (glyph, x, y);
929 * hb_font_get_glyph_h_kerning:
942 hb_font_get_glyph_h_kerning (hb_font_t *font,
943 hb_codepoint_t left_glyph, hb_codepoint_t right_glyph)
945 return font->get_glyph_h_kerning (left_glyph, right_glyph);
949 * hb_font_get_glyph_v_kerning:
962 hb_font_get_glyph_v_kerning (hb_font_t *font,
963 hb_codepoint_t top_glyph, hb_codepoint_t bottom_glyph)
965 return font->get_glyph_v_kerning (top_glyph, bottom_glyph);
969 * hb_font_get_glyph_extents:
981 hb_font_get_glyph_extents (hb_font_t *font,
982 hb_codepoint_t glyph,
983 hb_glyph_extents_t *extents)
985 return font->get_glyph_extents (glyph, extents);
989 * hb_font_get_glyph_contour_point:
1003 hb_font_get_glyph_contour_point (hb_font_t *font,
1004 hb_codepoint_t glyph, unsigned int point_index,
1005 hb_position_t *x, hb_position_t *y)
1007 return font->get_glyph_contour_point (glyph, point_index, x, y);
1011 * hb_font_get_glyph_name:
1014 * @name: (array length=size):
1024 hb_font_get_glyph_name (hb_font_t *font,
1025 hb_codepoint_t glyph,
1026 char *name, unsigned int size)
1028 return font->get_glyph_name (glyph, name, size);
1032 * hb_font_get_glyph_from_name:
1034 * @name: (array length=len):
1045 hb_font_get_glyph_from_name (hb_font_t *font,
1046 const char *name, int len, /* -1 means nul-terminated */
1047 hb_codepoint_t *glyph)
1049 return font->get_glyph_from_name (name, len, glyph);
1053 /* A bit higher-level, and with fallback */
1056 * hb_font_get_extents_for_direction:
1066 hb_font_get_extents_for_direction (hb_font_t *font,
1067 hb_direction_t direction,
1068 hb_font_extents_t *extents)
1070 return font->get_extents_for_direction (direction, extents);
1073 * hb_font_get_glyph_advance_for_direction:
1085 hb_font_get_glyph_advance_for_direction (hb_font_t *font,
1086 hb_codepoint_t glyph,
1087 hb_direction_t direction,
1088 hb_position_t *x, hb_position_t *y)
1090 return font->get_glyph_advance_for_direction (glyph, direction, x, y);
1093 * hb_font_get_glyph_advances_for_direction:
1102 hb_font_get_glyph_advances_for_direction (hb_font_t* font,
1103 hb_direction_t direction,
1105 const hb_codepoint_t *first_glyph,
1106 unsigned glyph_stride,
1107 hb_position_t *first_advance,
1108 unsigned advance_stride)
1110 font->get_glyph_advances_for_direction (direction, count, first_glyph, glyph_stride, first_advance, advance_stride);
1114 * hb_font_get_glyph_origin_for_direction:
1126 hb_font_get_glyph_origin_for_direction (hb_font_t *font,
1127 hb_codepoint_t glyph,
1128 hb_direction_t direction,
1129 hb_position_t *x, hb_position_t *y)
1131 return font->get_glyph_origin_for_direction (glyph, direction, x, y);
1135 * hb_font_add_glyph_origin_for_direction:
1147 hb_font_add_glyph_origin_for_direction (hb_font_t *font,
1148 hb_codepoint_t glyph,
1149 hb_direction_t direction,
1150 hb_position_t *x, hb_position_t *y)
1152 return font->add_glyph_origin_for_direction (glyph, direction, x, y);
1156 * hb_font_subtract_glyph_origin_for_direction:
1168 hb_font_subtract_glyph_origin_for_direction (hb_font_t *font,
1169 hb_codepoint_t glyph,
1170 hb_direction_t direction,
1171 hb_position_t *x, hb_position_t *y)
1173 return font->subtract_glyph_origin_for_direction (glyph, direction, x, y);
1177 * hb_font_get_glyph_kerning_for_direction:
1191 hb_font_get_glyph_kerning_for_direction (hb_font_t *font,
1192 hb_codepoint_t first_glyph, hb_codepoint_t second_glyph,
1193 hb_direction_t direction,
1194 hb_position_t *x, hb_position_t *y)
1196 return font->get_glyph_kerning_for_direction (first_glyph, second_glyph, direction, x, y);
1200 * hb_font_get_glyph_extents_for_origin:
1213 hb_font_get_glyph_extents_for_origin (hb_font_t *font,
1214 hb_codepoint_t glyph,
1215 hb_direction_t direction,
1216 hb_glyph_extents_t *extents)
1218 return font->get_glyph_extents_for_origin (glyph, direction, extents);
1222 * hb_font_get_glyph_contour_point_for_origin:
1237 hb_font_get_glyph_contour_point_for_origin (hb_font_t *font,
1238 hb_codepoint_t glyph, unsigned int point_index,
1239 hb_direction_t direction,
1240 hb_position_t *x, hb_position_t *y)
1242 return font->get_glyph_contour_point_for_origin (glyph, point_index, direction, x, y);
1245 /* Generates gidDDD if glyph has no name. */
1247 * hb_font_glyph_to_string:
1250 * @s: (array length=size):
1258 hb_font_glyph_to_string (hb_font_t *font,
1259 hb_codepoint_t glyph,
1260 char *s, unsigned int size)
1262 font->glyph_to_string (glyph, s, size);
1265 /* Parses gidDDD and uniUUUU strings automatically. */
1267 * hb_font_glyph_from_string:
1269 * @s: (array length=len) (element-type uint8_t):
1280 hb_font_glyph_from_string (hb_font_t *font,
1281 const char *s, int len, /* -1 means nul-terminated */
1282 hb_codepoint_t *glyph)
1284 return font->glyph_from_string (s, len, glyph);
1292 DEFINE_NULL_INSTANCE (hb_font_t) =
1294 HB_OBJECT_HEADER_STATIC,
1296 nullptr, /* parent */
1297 const_cast<hb_face_t *> (&_hb_Null_hb_face_t),
1307 nullptr, /* coords */
1309 const_cast<hb_font_funcs_t *> (&_hb_Null_hb_font_funcs_t),
1311 /* Zero for the rest is fine. */
1316 _hb_font_create (hb_face_t *face)
1320 if (unlikely (!face))
1321 face = hb_face_get_empty ();
1322 if (!(font = hb_object_create<hb_font_t> ()))
1323 return hb_font_get_empty ();
1325 hb_face_make_immutable (face);
1326 font->parent = hb_font_get_empty ();
1327 font->face = hb_face_reference (face);
1328 font->klass = hb_font_funcs_get_empty ();
1329 font->data.init0 (font);
1330 font->x_scale = font->y_scale = hb_face_get_upem (face);
1336 * hb_font_create: (Xconstructor)
1341 * Return value: (transfer full):
1346 hb_font_create (hb_face_t *face)
1348 hb_font_t *font = _hb_font_create (face);
1350 /* Install our in-house, very lightweight, funcs. */
1351 hb_ot_font_set_funcs (font);
1357 * hb_font_create_sub_font:
1358 * @parent: parent font.
1362 * Return value: (transfer full):
1367 hb_font_create_sub_font (hb_font_t *parent)
1369 if (unlikely (!parent))
1370 parent = hb_font_get_empty ();
1372 hb_font_t *font = _hb_font_create (parent->face);
1374 if (unlikely (hb_object_is_immutable (font)))
1377 font->parent = hb_font_reference (parent);
1379 font->x_scale = parent->x_scale;
1380 font->y_scale = parent->y_scale;
1381 font->x_ppem = parent->x_ppem;
1382 font->y_ppem = parent->y_ppem;
1383 font->ptem = parent->ptem;
1385 font->num_coords = parent->num_coords;
1386 if (!font->num_coords)
1387 font->coords = nullptr;
1390 unsigned int size = parent->num_coords * sizeof (parent->coords[0]);
1391 font->coords = (int *) malloc (size);
1392 if (unlikely (!font->coords))
1393 font->num_coords = 0;
1395 memcpy (font->coords, parent->coords, size);
1402 * hb_font_get_empty:
1406 * Return value: (transfer full)
1411 hb_font_get_empty ()
1413 return const_cast<hb_font_t *> (&Null(hb_font_t));
1417 * hb_font_reference: (skip)
1422 * Return value: (transfer full):
1427 hb_font_reference (hb_font_t *font)
1429 return hb_object_reference (font);
1433 * hb_font_destroy: (skip)
1441 hb_font_destroy (hb_font_t *font)
1443 if (!hb_object_destroy (font)) return;
1448 font->destroy (font->user_data);
1450 hb_font_destroy (font->parent);
1451 hb_face_destroy (font->face);
1452 hb_font_funcs_destroy (font->klass);
1454 free (font->coords);
1460 * hb_font_set_user_data: (skip)
1474 hb_font_set_user_data (hb_font_t *font,
1475 hb_user_data_key_t *key,
1477 hb_destroy_func_t destroy,
1480 return hb_object_set_user_data (font, key, data, destroy, replace);
1484 * hb_font_get_user_data: (skip)
1490 * Return value: (transfer none):
1495 hb_font_get_user_data (hb_font_t *font,
1496 hb_user_data_key_t *key)
1498 return hb_object_get_user_data (font, key);
1502 * hb_font_make_immutable:
1510 hb_font_make_immutable (hb_font_t *font)
1512 if (hb_object_is_immutable (font))
1516 hb_font_make_immutable (font->parent);
1518 hb_object_make_immutable (font);
1522 * hb_font_is_immutable:
1532 hb_font_is_immutable (hb_font_t *font)
1534 return hb_object_is_immutable (font);
1538 * hb_font_set_parent:
1540 * @parent: new parent.
1542 * Sets parent font of @font.
1547 hb_font_set_parent (hb_font_t *font,
1550 if (hb_object_is_immutable (font))
1554 parent = hb_font_get_empty ();
1556 hb_font_t *old = font->parent;
1558 font->parent = hb_font_reference (parent);
1560 hb_font_destroy (old);
1564 * hb_font_get_parent:
1569 * Return value: (transfer none):
1574 hb_font_get_parent (hb_font_t *font)
1576 return font->parent;
1584 * Sets font-face of @font.
1589 hb_font_set_face (hb_font_t *font,
1592 if (hb_object_is_immutable (font))
1595 if (unlikely (!face))
1596 face = hb_face_get_empty ();
1598 hb_face_t *old = font->face;
1600 font->face = hb_face_reference (face);
1602 hb_face_destroy (old);
1611 * Return value: (transfer none):
1616 hb_font_get_face (hb_font_t *font)
1623 * hb_font_set_funcs:
1625 * @klass: (closure font_data) (destroy destroy) (scope notified):
1634 hb_font_set_funcs (hb_font_t *font,
1635 hb_font_funcs_t *klass,
1637 hb_destroy_func_t destroy)
1639 if (hb_object_is_immutable (font))
1642 destroy (font_data);
1647 font->destroy (font->user_data);
1650 klass = hb_font_funcs_get_empty ();
1652 hb_font_funcs_reference (klass);
1653 hb_font_funcs_destroy (font->klass);
1654 font->klass = klass;
1655 font->user_data = font_data;
1656 font->destroy = destroy;
1660 * hb_font_set_funcs_data:
1662 * @font_data: (destroy destroy) (scope notified):
1670 hb_font_set_funcs_data (hb_font_t *font,
1672 hb_destroy_func_t destroy)
1674 /* Destroy user_data? */
1675 if (hb_object_is_immutable (font))
1678 destroy (font_data);
1683 font->destroy (font->user_data);
1685 font->user_data = font_data;
1686 font->destroy = destroy;
1691 * hb_font_set_scale:
1701 hb_font_set_scale (hb_font_t *font,
1705 if (hb_object_is_immutable (font))
1708 font->x_scale = x_scale;
1709 font->y_scale = y_scale;
1713 * hb_font_get_scale:
1723 hb_font_get_scale (hb_font_t *font,
1727 if (x_scale) *x_scale = font->x_scale;
1728 if (y_scale) *y_scale = font->y_scale;
1742 hb_font_set_ppem (hb_font_t *font,
1743 unsigned int x_ppem,
1744 unsigned int y_ppem)
1746 if (hb_object_is_immutable (font))
1749 font->x_ppem = x_ppem;
1750 font->y_ppem = y_ppem;
1764 hb_font_get_ppem (hb_font_t *font,
1765 unsigned int *x_ppem,
1766 unsigned int *y_ppem)
1768 if (x_ppem) *x_ppem = font->x_ppem;
1769 if (y_ppem) *y_ppem = font->y_ppem;
1775 * @ptem: font size in points.
1777 * Sets "point size" of the font. Set to 0 to unset.
1779 * There are 72 points in an inch.
1784 hb_font_set_ptem (hb_font_t *font, float ptem)
1786 if (hb_object_is_immutable (font))
1796 * Gets the "point size" of the font. A value of 0 means unset.
1798 * Return value: Point size.
1803 hb_font_get_ptem (hb_font_t *font)
1813 _hb_font_adopt_var_coords_normalized (hb_font_t *font,
1814 int *coords, /* 2.14 normalized */
1815 unsigned int coords_length)
1817 free (font->coords);
1819 font->coords = coords;
1820 font->num_coords = coords_length;
1824 * hb_font_set_variations:
1829 hb_font_set_variations (hb_font_t *font,
1830 const hb_variation_t *variations,
1831 unsigned int variations_length)
1833 if (hb_object_is_immutable (font))
1836 if (!variations_length)
1838 hb_font_set_var_coords_normalized (font, nullptr, 0);
1842 unsigned int coords_length = hb_ot_var_get_axis_count (font->face);
1844 int *normalized = coords_length ? (int *) calloc (coords_length, sizeof (int)) : nullptr;
1845 if (unlikely (coords_length && !normalized))
1848 hb_ot_var_normalize_variations (font->face,
1849 variations, variations_length,
1850 normalized, coords_length);
1851 _hb_font_adopt_var_coords_normalized (font, normalized, coords_length);
1855 * hb_font_set_var_coords_design:
1860 hb_font_set_var_coords_design (hb_font_t *font,
1861 const float *coords,
1862 unsigned int coords_length)
1864 if (hb_object_is_immutable (font))
1867 int *normalized = coords_length ? (int *) calloc (coords_length, sizeof (int)) : nullptr;
1868 if (unlikely (coords_length && !normalized))
1871 hb_ot_var_normalize_coords (font->face, coords_length, coords, normalized);
1872 _hb_font_adopt_var_coords_normalized (font, normalized, coords_length);
1876 * hb_font_set_var_coords_normalized:
1881 hb_font_set_var_coords_normalized (hb_font_t *font,
1882 const int *coords, /* 2.14 normalized */
1883 unsigned int coords_length)
1885 if (hb_object_is_immutable (font))
1888 int *copy = coords_length ? (int *) calloc (coords_length, sizeof (coords[0])) : nullptr;
1889 if (unlikely (coords_length && !copy))
1893 memcpy (copy, coords, coords_length * sizeof (coords[0]));
1895 _hb_font_adopt_var_coords_normalized (font, copy, coords_length);
1899 * hb_font_get_var_coords_normalized:
1901 * Return value is valid as long as variation coordinates of the font
1907 hb_font_get_var_coords_normalized (hb_font_t *font,
1908 unsigned int *length)
1911 *length = font->num_coords;
1913 return font->coords;
1918 * Deprecated get_glyph_func():
1921 struct hb_trampoline_closure_t
1924 hb_destroy_func_t destroy;
1925 unsigned int ref_count;
1928 template <typename FuncType>
1929 struct hb_trampoline_t
1931 hb_trampoline_closure_t closure; /* Must be first. */
1935 template <typename FuncType>
1936 static hb_trampoline_t<FuncType> *
1937 trampoline_create (FuncType func,
1939 hb_destroy_func_t destroy)
1941 typedef hb_trampoline_t<FuncType> trampoline_t;
1943 trampoline_t *trampoline = (trampoline_t *) calloc (1, sizeof (trampoline_t));
1945 if (unlikely (!trampoline))
1948 trampoline->closure.user_data = user_data;
1949 trampoline->closure.destroy = destroy;
1950 trampoline->closure.ref_count = 1;
1951 trampoline->func = func;
1957 trampoline_reference (hb_trampoline_closure_t *closure)
1959 closure->ref_count++;
1963 trampoline_destroy (void *user_data)
1965 hb_trampoline_closure_t *closure = (hb_trampoline_closure_t *) user_data;
1967 if (--closure->ref_count)
1970 if (closure->destroy)
1971 closure->destroy (closure->user_data);
1975 typedef hb_trampoline_t<hb_font_get_glyph_func_t> hb_font_get_glyph_trampoline_t;
1978 hb_font_get_nominal_glyph_trampoline (hb_font_t *font,
1980 hb_codepoint_t unicode,
1981 hb_codepoint_t *glyph,
1984 hb_font_get_glyph_trampoline_t *trampoline = (hb_font_get_glyph_trampoline_t *) user_data;
1985 return trampoline->func (font, font_data, unicode, 0, glyph, trampoline->closure.user_data);
1989 hb_font_get_variation_glyph_trampoline (hb_font_t *font,
1991 hb_codepoint_t unicode,
1992 hb_codepoint_t variation_selector,
1993 hb_codepoint_t *glyph,
1996 hb_font_get_glyph_trampoline_t *trampoline = (hb_font_get_glyph_trampoline_t *) user_data;
1997 return trampoline->func (font, font_data, unicode, variation_selector, glyph, trampoline->closure.user_data);
2001 * hb_font_funcs_set_glyph_func:
2002 * @ffuncs: font functions.
2003 * @func: (closure user_data) (destroy destroy) (scope notified): callback function.
2004 * @user_data: data to pass to @func.
2005 * @destroy: function to call when @user_data is not needed anymore.
2007 * Deprecated. Use hb_font_funcs_set_nominal_glyph_func() and
2008 * hb_font_funcs_set_variation_glyph_func() instead.
2014 hb_font_funcs_set_glyph_func (hb_font_funcs_t *ffuncs,
2015 hb_font_get_glyph_func_t func,
2016 void *user_data, hb_destroy_func_t destroy)
2018 hb_font_get_glyph_trampoline_t *trampoline;
2020 trampoline = trampoline_create (func, user_data, destroy);
2021 if (unlikely (!trampoline))
2024 destroy (user_data);
2028 hb_font_funcs_set_nominal_glyph_func (ffuncs,
2029 hb_font_get_nominal_glyph_trampoline,
2031 trampoline_destroy);
2033 trampoline_reference (&trampoline->closure);
2034 hb_font_funcs_set_variation_glyph_func (ffuncs,
2035 hb_font_get_variation_glyph_trampoline,
2037 trampoline_destroy);