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"
34 #include "hb-ot-head-table.hh"
36 #include "hb-cache-private.hh"
47 hb_font_get_glyph_nil (hb_font_t *font,
48 void *font_data HB_UNUSED,
49 hb_codepoint_t unicode,
50 hb_codepoint_t variation_selector,
51 hb_codepoint_t *glyph,
52 void *user_data HB_UNUSED)
55 return hb_font_get_glyph (font->parent, unicode, variation_selector, glyph);
62 hb_font_get_glyph_h_advance_nil (hb_font_t *font,
63 void *font_data HB_UNUSED,
65 void *user_data HB_UNUSED)
68 return font->parent_scale_x_distance (hb_font_get_glyph_h_advance (font->parent, glyph));
74 hb_font_get_glyph_v_advance_nil (hb_font_t *font,
75 void *font_data HB_UNUSED,
77 void *user_data HB_UNUSED)
80 return font->parent_scale_y_distance (hb_font_get_glyph_v_advance (font->parent, glyph));
86 hb_font_get_glyph_h_origin_nil (hb_font_t *font,
87 void *font_data HB_UNUSED,
91 void *user_data HB_UNUSED)
94 hb_bool_t ret = hb_font_get_glyph_h_origin (font->parent, glyph, x, y);
96 font->parent_scale_position (x, y);
105 hb_font_get_glyph_v_origin_nil (hb_font_t *font,
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, glyph, x, y);
115 font->parent_scale_position (x, y);
124 hb_font_get_glyph_h_kerning_nil (hb_font_t *font,
125 void *font_data HB_UNUSED,
126 hb_codepoint_t left_glyph,
127 hb_codepoint_t right_glyph,
128 void *user_data HB_UNUSED)
131 return font->parent_scale_x_distance (hb_font_get_glyph_h_kerning (font->parent, left_glyph, right_glyph));
137 hb_font_get_glyph_v_kerning_nil (hb_font_t *font,
138 void *font_data HB_UNUSED,
139 hb_codepoint_t top_glyph,
140 hb_codepoint_t bottom_glyph,
141 void *user_data HB_UNUSED)
144 return font->parent_scale_y_distance (hb_font_get_glyph_v_kerning (font->parent, top_glyph, bottom_glyph));
150 hb_font_get_glyph_extents_nil (hb_font_t *font,
151 void *font_data HB_UNUSED,
152 hb_codepoint_t glyph,
153 hb_glyph_extents_t *extents,
154 void *user_data HB_UNUSED)
157 hb_bool_t ret = hb_font_get_glyph_extents (font->parent,
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 = hb_font_get_glyph_contour_point (font->parent, 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 hb_font_get_glyph_name (font->parent, glyph, name, size);
201 snprintf (name, size, "gid%u", glyph);
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 hb_font_get_glyph_from_name (font->parent, 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 (void)
236 hb_font_funcs_t *ffuncs;
238 if (!(ffuncs = hb_object_create<hb_font_funcs_t> ()))
239 return hb_font_funcs_get_empty ();
241 ffuncs->get = _hb_font_funcs_nil.get;
247 hb_font_funcs_get_empty (void)
249 return const_cast<hb_font_funcs_t *> (&_hb_font_funcs_nil);
253 hb_font_funcs_reference (hb_font_funcs_t *ffuncs)
255 return hb_object_reference (ffuncs);
259 hb_font_funcs_destroy (hb_font_funcs_t *ffuncs)
261 if (!hb_object_destroy (ffuncs)) return;
263 #define HB_FONT_FUNC_IMPLEMENT(name) if (ffuncs->destroy.name) \
264 ffuncs->destroy.name (ffuncs->user_data.name);
265 HB_FONT_FUNCS_IMPLEMENT_CALLBACKS
266 #undef HB_FONT_FUNC_IMPLEMENT
272 hb_font_funcs_set_user_data (hb_font_funcs_t *ffuncs,
273 hb_user_data_key_t *key,
275 hb_destroy_func_t destroy,
278 return hb_object_set_user_data (ffuncs, key, data, destroy, replace);
282 hb_font_funcs_get_user_data (hb_font_funcs_t *ffuncs,
283 hb_user_data_key_t *key)
285 return hb_object_get_user_data (ffuncs, key);
290 hb_font_funcs_make_immutable (hb_font_funcs_t *ffuncs)
292 if (hb_object_is_inert (ffuncs))
295 ffuncs->immutable = true;
299 hb_font_funcs_is_immutable (hb_font_funcs_t *ffuncs)
301 return ffuncs->immutable;
305 #define HB_FONT_FUNC_IMPLEMENT(name) \
308 hb_font_funcs_set_##name##_func (hb_font_funcs_t *ffuncs, \
309 hb_font_get_##name##_func_t func, \
311 hb_destroy_func_t destroy) \
313 if (ffuncs->immutable) { \
315 destroy (user_data); \
319 if (ffuncs->destroy.name) \
320 ffuncs->destroy.name (ffuncs->user_data.name); \
323 ffuncs->get.name = func; \
324 ffuncs->user_data.name = user_data; \
325 ffuncs->destroy.name = destroy; \
327 ffuncs->get.name = hb_font_get_##name##_nil; \
328 ffuncs->user_data.name = NULL; \
329 ffuncs->destroy.name = NULL; \
333 HB_FONT_FUNCS_IMPLEMENT_CALLBACKS
334 #undef HB_FONT_FUNC_IMPLEMENT
338 hb_font_get_glyph (hb_font_t *font,
339 hb_codepoint_t unicode, hb_codepoint_t variation_selector,
340 hb_codepoint_t *glyph)
343 return font->klass->get.glyph (font, font->user_data,
344 unicode, variation_selector, glyph,
345 font->klass->user_data.glyph);
349 hb_font_get_glyph_h_advance (hb_font_t *font,
350 hb_codepoint_t glyph)
352 return font->klass->get.glyph_h_advance (font, font->user_data,
354 font->klass->user_data.glyph_h_advance);
358 hb_font_get_glyph_v_advance (hb_font_t *font,
359 hb_codepoint_t glyph)
361 return font->klass->get.glyph_v_advance (font, font->user_data,
363 font->klass->user_data.glyph_v_advance);
367 hb_font_get_glyph_h_origin (hb_font_t *font,
368 hb_codepoint_t glyph,
369 hb_position_t *x, hb_position_t *y)
372 return font->klass->get.glyph_h_origin (font, font->user_data,
374 font->klass->user_data.glyph_h_origin);
378 hb_font_get_glyph_v_origin (hb_font_t *font,
379 hb_codepoint_t glyph,
380 hb_position_t *x, hb_position_t *y)
383 return font->klass->get.glyph_v_origin (font, font->user_data,
385 font->klass->user_data.glyph_v_origin);
389 hb_font_get_glyph_h_kerning (hb_font_t *font,
390 hb_codepoint_t left_glyph, hb_codepoint_t right_glyph)
392 return font->klass->get.glyph_h_kerning (font, font->user_data,
393 left_glyph, right_glyph,
394 font->klass->user_data.glyph_h_kerning);
398 hb_font_get_glyph_v_kerning (hb_font_t *font,
399 hb_codepoint_t left_glyph, hb_codepoint_t right_glyph)
401 return font->klass->get.glyph_v_kerning (font, font->user_data,
402 left_glyph, right_glyph,
403 font->klass->user_data.glyph_v_kerning);
407 hb_font_get_glyph_extents (hb_font_t *font,
408 hb_codepoint_t glyph,
409 hb_glyph_extents_t *extents)
411 memset (extents, 0, sizeof (*extents));
412 return font->klass->get.glyph_extents (font, font->user_data,
415 font->klass->user_data.glyph_extents);
419 hb_font_get_glyph_contour_point (hb_font_t *font,
420 hb_codepoint_t glyph, unsigned int point_index,
421 hb_position_t *x, hb_position_t *y)
424 return font->klass->get.glyph_contour_point (font, font->user_data,
427 font->klass->user_data.glyph_contour_point);
431 hb_font_get_glyph_name (hb_font_t *font,
432 hb_codepoint_t glyph,
433 char *name, unsigned int size)
435 return font->klass->get.glyph_name (font, font->user_data,
438 font->klass->user_data.glyph_name);
442 hb_font_get_glyph_from_name (hb_font_t *font,
443 const char *name, int len, /* -1 means nul-terminated */
444 hb_codepoint_t *glyph)
446 return font->klass->get.glyph_from_name (font, font->user_data,
449 font->klass->user_data.glyph_from_name);
453 /* A bit higher-level, and with fallback */
456 hb_font_get_glyph_advance_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 if (likely (HB_DIRECTION_IS_HORIZONTAL (direction))) {
462 *x = hb_font_get_glyph_h_advance (font, glyph);
466 *y = hb_font_get_glyph_v_advance (font, glyph);
471 guess_v_origin_minus_h_origin (hb_font_t *font,
472 hb_codepoint_t glyph,
473 hb_position_t *x, hb_position_t *y)
475 *x = hb_font_get_glyph_h_advance (font, glyph) / 2;
477 /* TODO use font_metics.ascent */
483 hb_font_get_glyph_origin_for_direction (hb_font_t *font,
484 hb_codepoint_t glyph,
485 hb_direction_t direction,
486 hb_position_t *x, hb_position_t *y)
488 if (likely (HB_DIRECTION_IS_HORIZONTAL (direction))) {
489 hb_bool_t ret = hb_font_get_glyph_h_origin (font, glyph, x, y);
490 if (!ret && (ret = hb_font_get_glyph_v_origin (font, glyph, x, y))) {
491 hb_position_t dx, dy;
492 guess_v_origin_minus_h_origin (font, glyph, &dx, &dy);
496 hb_bool_t ret = hb_font_get_glyph_v_origin (font, glyph, x, y);
497 if (!ret && (ret = hb_font_get_glyph_h_origin (font, glyph, x, y))) {
498 hb_position_t dx, dy;
499 guess_v_origin_minus_h_origin (font, glyph, &dx, &dy);
506 hb_font_add_glyph_origin_for_direction (hb_font_t *font,
507 hb_codepoint_t glyph,
508 hb_direction_t direction,
509 hb_position_t *x, hb_position_t *y)
511 hb_position_t origin_x, origin_y;
513 hb_font_get_glyph_origin_for_direction (font, glyph, direction, &origin_x, &origin_y);
520 hb_font_subtract_glyph_origin_for_direction (hb_font_t *font,
521 hb_codepoint_t glyph,
522 hb_direction_t direction,
523 hb_position_t *x, hb_position_t *y)
525 hb_position_t origin_x, origin_y;
527 hb_font_get_glyph_origin_for_direction (font, glyph, direction, &origin_x, &origin_y);
534 hb_font_get_glyph_kerning_for_direction (hb_font_t *font,
535 hb_codepoint_t first_glyph, hb_codepoint_t second_glyph,
536 hb_direction_t direction,
537 hb_position_t *x, hb_position_t *y)
539 if (likely (HB_DIRECTION_IS_HORIZONTAL (direction))) {
540 *x = hb_font_get_glyph_h_kerning (font, first_glyph, second_glyph);
544 *y = hb_font_get_glyph_v_kerning (font, first_glyph, second_glyph);
549 hb_font_get_glyph_extents_for_origin (hb_font_t *font,
550 hb_codepoint_t glyph,
551 hb_direction_t direction,
552 hb_glyph_extents_t *extents)
554 hb_bool_t ret = hb_font_get_glyph_extents (font, glyph, extents);
557 hb_font_subtract_glyph_origin_for_direction (font, glyph, direction, &extents->x_bearing, &extents->y_bearing);
563 hb_font_get_glyph_contour_point_for_origin (hb_font_t *font,
564 hb_codepoint_t glyph, unsigned int point_index,
565 hb_direction_t direction,
566 hb_position_t *x, hb_position_t *y)
568 hb_bool_t ret = hb_font_get_glyph_contour_point (font, glyph, point_index, x, y);
571 hb_font_subtract_glyph_origin_for_direction (font, glyph, direction, x, y);
581 static const hb_face_t _hb_face_nil = {
582 HB_OBJECT_HEADER_STATIC,
584 true, /* immutable */
586 NULL, /* reference_table */
587 NULL, /* user_data */
590 NULL, /* ot_layout */
598 hb_face_create_for_tables (hb_reference_table_func_t reference_table,
600 hb_destroy_func_t destroy)
604 if (!reference_table || !(face = hb_object_create<hb_face_t> ())) {
607 return hb_face_get_empty ();
610 face->reference_table = reference_table;
611 face->user_data = user_data;
612 face->destroy = destroy;
614 face->ot_layout = _hb_ot_layout_create (face);
622 typedef struct _hb_face_for_data_closure_t {
625 } hb_face_for_data_closure_t;
627 static hb_face_for_data_closure_t *
628 _hb_face_for_data_closure_create (hb_blob_t *blob, unsigned int index)
630 hb_face_for_data_closure_t *closure;
632 closure = (hb_face_for_data_closure_t *) malloc (sizeof (hb_face_for_data_closure_t));
633 if (unlikely (!closure))
636 closure->blob = blob;
637 closure->index = index;
643 _hb_face_for_data_closure_destroy (hb_face_for_data_closure_t *closure)
645 hb_blob_destroy (closure->blob);
650 _hb_face_for_data_reference_table (hb_face_t *face HB_UNUSED, hb_tag_t tag, void *user_data)
652 hb_face_for_data_closure_t *data = (hb_face_for_data_closure_t *) user_data;
654 if (tag == HB_TAG_NONE)
655 return hb_blob_reference (data->blob);
657 const OpenTypeFontFile &ot_file = *Sanitizer<OpenTypeFontFile>::lock_instance (data->blob);
658 const OpenTypeFontFace &ot_face = ot_file.get_face (data->index);
660 const OpenTypeTable &table = ot_face.get_table_by_tag (tag);
662 hb_blob_t *blob = hb_blob_create_sub_blob (data->blob, table.offset, table.length);
668 hb_face_create (hb_blob_t *blob,
673 if (unlikely (!blob || !hb_blob_get_length (blob)))
674 return hb_face_get_empty ();
676 hb_face_for_data_closure_t *closure = _hb_face_for_data_closure_create (Sanitizer<OpenTypeFontFile>::sanitize (hb_blob_reference (blob)), index);
678 if (unlikely (!closure))
679 return hb_face_get_empty ();
681 face = hb_face_create_for_tables (_hb_face_for_data_reference_table,
683 (hb_destroy_func_t) _hb_face_for_data_closure_destroy);
685 hb_face_set_index (face, index);
691 hb_face_get_empty (void)
693 return const_cast<hb_face_t *> (&_hb_face_nil);
698 hb_face_reference (hb_face_t *face)
700 return hb_object_reference (face);
704 hb_face_destroy (hb_face_t *face)
706 if (!hb_object_destroy (face)) return;
708 _hb_ot_layout_destroy (face->ot_layout);
711 face->destroy (face->user_data);
717 hb_face_set_user_data (hb_face_t *face,
718 hb_user_data_key_t *key,
720 hb_destroy_func_t destroy,
723 return hb_object_set_user_data (face, key, data, destroy, replace);
727 hb_face_get_user_data (hb_face_t *face,
728 hb_user_data_key_t *key)
730 return hb_object_get_user_data (face, key);
734 hb_face_make_immutable (hb_face_t *face)
736 if (hb_object_is_inert (face))
739 face->immutable = true;
743 hb_face_is_immutable (hb_face_t *face)
745 return face->immutable;
750 hb_face_reference_table (hb_face_t *face,
755 if (unlikely (!face || !face->reference_table))
756 return hb_blob_get_empty ();
758 blob = face->reference_table (face, tag, face->user_data);
759 if (unlikely (!blob))
760 return hb_blob_get_empty ();
766 hb_face_reference_blob (hb_face_t *face)
768 return hb_face_reference_table (face, HB_TAG_NONE);
772 hb_face_set_index (hb_face_t *face,
775 if (hb_object_is_inert (face))
782 hb_face_get_index (hb_face_t *face)
788 hb_face_set_upem (hb_face_t *face,
791 if (hb_object_is_inert (face))
798 hb_face_get_upem (hb_face_t *face)
800 if (unlikely (!face->upem)) {
801 hb_blob_t *head_blob = Sanitizer<head>::sanitize (hb_face_reference_table (face, HB_OT_TAG_head));
802 const head *head_table = Sanitizer<head>::lock_instance (head_blob);
803 face->upem = head_table->get_upem ();
804 hb_blob_destroy (head_blob);
815 hb_font_create (hb_face_t *face)
819 if (unlikely (!face))
820 face = hb_face_get_empty ();
821 if (unlikely (hb_object_is_inert (face)))
822 return hb_font_get_empty ();
823 if (!(font = hb_object_create<hb_font_t> ()))
824 return hb_font_get_empty ();
826 hb_face_make_immutable (face);
827 font->face = hb_face_reference (face);
828 font->klass = hb_font_funcs_get_empty ();
834 hb_font_create_sub_font (hb_font_t *parent)
836 if (unlikely (!parent))
837 return hb_font_get_empty ();
839 hb_font_t *font = hb_font_create (parent->face);
841 if (unlikely (hb_object_is_inert (font)))
844 hb_font_make_immutable (parent);
845 font->parent = hb_font_reference (parent);
847 font->x_scale = parent->x_scale;
848 font->y_scale = parent->y_scale;
849 font->x_ppem = parent->x_ppem;
850 font->y_ppem = parent->y_ppem;
856 hb_font_get_empty (void)
858 static const hb_font_t _hb_font_nil = {
859 HB_OBJECT_HEADER_STATIC,
861 true, /* immutable */
864 const_cast<hb_face_t *> (&_hb_face_nil),
872 const_cast<hb_font_funcs_t *> (&_hb_font_funcs_nil), /* klass */
873 NULL, /* user_data */
877 return const_cast<hb_font_t *> (&_hb_font_nil);
881 hb_font_reference (hb_font_t *font)
883 return hb_object_reference (font);
887 hb_font_destroy (hb_font_t *font)
889 if (!hb_object_destroy (font)) return;
891 hb_font_destroy (font->parent);
892 hb_face_destroy (font->face);
893 hb_font_funcs_destroy (font->klass);
895 font->destroy (font->user_data);
901 hb_font_set_user_data (hb_font_t *font,
902 hb_user_data_key_t *key,
904 hb_destroy_func_t destroy,
907 return hb_object_set_user_data (font, key, data, destroy, replace);
911 hb_font_get_user_data (hb_font_t *font,
912 hb_user_data_key_t *key)
914 return hb_object_get_user_data (font, key);
918 hb_font_make_immutable (hb_font_t *font)
920 if (hb_object_is_inert (font))
923 font->immutable = true;
927 hb_font_is_immutable (hb_font_t *font)
929 return font->immutable;
933 hb_font_get_parent (hb_font_t *font)
939 hb_font_get_face (hb_font_t *font)
946 hb_font_set_funcs (hb_font_t *font,
947 hb_font_funcs_t *klass,
949 hb_destroy_func_t destroy)
951 if (font->immutable) {
958 font->destroy (font->user_data);
961 klass = hb_font_funcs_get_empty ();
963 hb_font_funcs_reference (klass);
964 hb_font_funcs_destroy (font->klass);
966 font->user_data = user_data;
967 font->destroy = destroy;
971 hb_font_set_funcs_data (hb_font_t *font,
973 hb_destroy_func_t destroy)
975 /* Destroy user_data? */
976 if (font->immutable) {
983 font->destroy (font->user_data);
985 font->user_data = user_data;
986 font->destroy = destroy;
991 hb_font_set_scale (hb_font_t *font,
998 font->x_scale = x_scale;
999 font->y_scale = y_scale;
1003 hb_font_get_scale (hb_font_t *font,
1007 if (x_scale) *x_scale = font->x_scale;
1008 if (y_scale) *y_scale = font->y_scale;
1012 hb_font_set_ppem (hb_font_t *font,
1013 unsigned int x_ppem,
1014 unsigned int y_ppem)
1016 if (font->immutable)
1019 font->x_ppem = x_ppem;
1020 font->y_ppem = y_ppem;
1024 hb_font_get_ppem (hb_font_t *font,
1025 unsigned int *x_ppem,
1026 unsigned int *y_ppem)
1028 if (x_ppem) *x_ppem = font->x_ppem;
1029 if (y_ppem) *y_ppem = font->y_ppem;