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"
50 hb_font_get_glyph_nil (hb_font_t *font,
51 void *font_data HB_UNUSED,
52 hb_codepoint_t unicode,
53 hb_codepoint_t variation_selector,
54 hb_codepoint_t *glyph,
55 void *user_data HB_UNUSED)
58 return font->parent->get_glyph (unicode, variation_selector, glyph);
65 hb_font_get_glyph_h_advance_nil (hb_font_t *font,
66 void *font_data HB_UNUSED,
68 void *user_data HB_UNUSED)
71 return font->parent_scale_x_distance (font->parent->get_glyph_h_advance (glyph));
77 hb_font_get_glyph_v_advance_nil (hb_font_t *font,
78 void *font_data HB_UNUSED,
80 void *user_data HB_UNUSED)
83 return font->parent_scale_y_distance (font->parent->get_glyph_v_advance (glyph));
89 hb_font_get_glyph_h_origin_nil (hb_font_t *font,
90 void *font_data HB_UNUSED,
94 void *user_data HB_UNUSED)
97 hb_bool_t ret = font->parent->get_glyph_h_origin (glyph, x, y);
99 font->parent_scale_position (x, y);
108 hb_font_get_glyph_v_origin_nil (hb_font_t *font,
109 void *font_data HB_UNUSED,
110 hb_codepoint_t glyph,
113 void *user_data HB_UNUSED)
116 hb_bool_t ret = font->parent->get_glyph_v_origin (glyph, x, y);
118 font->parent_scale_position (x, y);
127 hb_font_get_glyph_h_kerning_nil (hb_font_t *font,
128 void *font_data HB_UNUSED,
129 hb_codepoint_t left_glyph,
130 hb_codepoint_t right_glyph,
131 void *user_data HB_UNUSED)
134 return font->parent_scale_x_distance (font->parent->get_glyph_h_kerning (left_glyph, right_glyph));
140 hb_font_get_glyph_v_kerning_nil (hb_font_t *font,
141 void *font_data HB_UNUSED,
142 hb_codepoint_t top_glyph,
143 hb_codepoint_t bottom_glyph,
144 void *user_data HB_UNUSED)
147 return font->parent_scale_y_distance (font->parent->get_glyph_v_kerning (top_glyph, bottom_glyph));
153 hb_font_get_glyph_extents_nil (hb_font_t *font,
154 void *font_data HB_UNUSED,
155 hb_codepoint_t glyph,
156 hb_glyph_extents_t *extents,
157 void *user_data HB_UNUSED)
160 hb_bool_t ret = font->parent->get_glyph_extents (glyph, extents);
162 font->parent_scale_position (&extents->x_bearing, &extents->y_bearing);
163 font->parent_scale_distance (&extents->width, &extents->height);
168 memset (extents, 0, sizeof (*extents));
173 hb_font_get_glyph_contour_point_nil (hb_font_t *font,
174 void *font_data HB_UNUSED,
175 hb_codepoint_t glyph,
176 unsigned int point_index,
179 void *user_data HB_UNUSED)
182 hb_bool_t ret = font->parent->get_glyph_contour_point (glyph, point_index, x, y);
184 font->parent_scale_position (x, y);
193 hb_font_get_glyph_name_nil (hb_font_t *font,
194 void *font_data HB_UNUSED,
195 hb_codepoint_t glyph,
196 char *name, unsigned int size,
197 void *user_data HB_UNUSED)
200 return font->parent->get_glyph_name (glyph, name, size);
202 if (size) *name = '\0';
207 hb_font_get_glyph_from_name_nil (hb_font_t *font,
208 void *font_data HB_UNUSED,
209 const char *name, int len, /* -1 means nul-terminated */
210 hb_codepoint_t *glyph,
211 void *user_data HB_UNUSED)
214 return font->parent->get_glyph_from_name (name, len, glyph);
221 static const hb_font_funcs_t _hb_font_funcs_nil = {
222 HB_OBJECT_HEADER_STATIC,
224 true, /* immutable */
227 #define HB_FONT_FUNC_IMPLEMENT(name) hb_font_get_##name##_nil,
228 HB_FONT_FUNCS_IMPLEMENT_CALLBACKS
229 #undef HB_FONT_FUNC_IMPLEMENT
235 hb_font_funcs_create (void)
237 hb_font_funcs_t *ffuncs;
239 if (!(ffuncs = hb_object_create<hb_font_funcs_t> ()))
240 return hb_font_funcs_get_empty ();
242 ffuncs->get = _hb_font_funcs_nil.get;
248 hb_font_funcs_get_empty (void)
250 return const_cast<hb_font_funcs_t *> (&_hb_font_funcs_nil);
254 hb_font_funcs_reference (hb_font_funcs_t *ffuncs)
256 return hb_object_reference (ffuncs);
260 hb_font_funcs_destroy (hb_font_funcs_t *ffuncs)
262 if (!hb_object_destroy (ffuncs)) return;
264 #define HB_FONT_FUNC_IMPLEMENT(name) if (ffuncs->destroy.name) \
265 ffuncs->destroy.name (ffuncs->user_data.name);
266 HB_FONT_FUNCS_IMPLEMENT_CALLBACKS
267 #undef HB_FONT_FUNC_IMPLEMENT
273 hb_font_funcs_set_user_data (hb_font_funcs_t *ffuncs,
274 hb_user_data_key_t *key,
276 hb_destroy_func_t destroy,
279 return hb_object_set_user_data (ffuncs, key, data, destroy, replace);
283 hb_font_funcs_get_user_data (hb_font_funcs_t *ffuncs,
284 hb_user_data_key_t *key)
286 return hb_object_get_user_data (ffuncs, key);
291 hb_font_funcs_make_immutable (hb_font_funcs_t *ffuncs)
293 if (hb_object_is_inert (ffuncs))
296 ffuncs->immutable = true;
300 hb_font_funcs_is_immutable (hb_font_funcs_t *ffuncs)
302 return ffuncs->immutable;
306 #define HB_FONT_FUNC_IMPLEMENT(name) \
309 hb_font_funcs_set_##name##_func (hb_font_funcs_t *ffuncs, \
310 hb_font_get_##name##_func_t func, \
312 hb_destroy_func_t destroy) \
314 if (ffuncs->immutable) { \
316 destroy (user_data); \
320 if (ffuncs->destroy.name) \
321 ffuncs->destroy.name (ffuncs->user_data.name); \
324 ffuncs->get.name = func; \
325 ffuncs->user_data.name = user_data; \
326 ffuncs->destroy.name = destroy; \
328 ffuncs->get.name = hb_font_get_##name##_nil; \
329 ffuncs->user_data.name = NULL; \
330 ffuncs->destroy.name = NULL; \
334 HB_FONT_FUNCS_IMPLEMENT_CALLBACKS
335 #undef HB_FONT_FUNC_IMPLEMENT
341 hb_font_get_glyph (hb_font_t *font,
342 hb_codepoint_t unicode, hb_codepoint_t variation_selector,
343 hb_codepoint_t *glyph)
345 return font->get_glyph (unicode, variation_selector, glyph);
349 hb_font_get_glyph_h_advance (hb_font_t *font,
350 hb_codepoint_t glyph)
352 return font->get_glyph_h_advance (glyph);
356 hb_font_get_glyph_v_advance (hb_font_t *font,
357 hb_codepoint_t glyph)
359 return font->get_glyph_v_advance (glyph);
363 hb_font_get_glyph_h_origin (hb_font_t *font,
364 hb_codepoint_t glyph,
365 hb_position_t *x, hb_position_t *y)
367 return font->get_glyph_h_origin (glyph, x, y);
371 hb_font_get_glyph_v_origin (hb_font_t *font,
372 hb_codepoint_t glyph,
373 hb_position_t *x, hb_position_t *y)
375 return font->get_glyph_v_origin (glyph, x, y);
379 hb_font_get_glyph_h_kerning (hb_font_t *font,
380 hb_codepoint_t left_glyph, hb_codepoint_t right_glyph)
382 return font->get_glyph_h_kerning (left_glyph, right_glyph);
386 hb_font_get_glyph_v_kerning (hb_font_t *font,
387 hb_codepoint_t left_glyph, hb_codepoint_t right_glyph)
389 return font->get_glyph_v_kerning (left_glyph, right_glyph);
393 hb_font_get_glyph_extents (hb_font_t *font,
394 hb_codepoint_t glyph,
395 hb_glyph_extents_t *extents)
397 return font->get_glyph_extents (glyph, extents);
401 hb_font_get_glyph_contour_point (hb_font_t *font,
402 hb_codepoint_t glyph, unsigned int point_index,
403 hb_position_t *x, hb_position_t *y)
405 return font->get_glyph_contour_point (glyph, point_index, x, y);
409 hb_font_get_glyph_name (hb_font_t *font,
410 hb_codepoint_t glyph,
411 char *name, unsigned int size)
413 return font->get_glyph_name (glyph, name, size);
417 hb_font_get_glyph_from_name (hb_font_t *font,
418 const char *name, int len, /* -1 means nul-terminated */
419 hb_codepoint_t *glyph)
421 return font->get_glyph_from_name (name, len, glyph);
425 /* A bit higher-level, and with fallback */
428 hb_font_get_glyph_advance_for_direction (hb_font_t *font,
429 hb_codepoint_t glyph,
430 hb_direction_t direction,
431 hb_position_t *x, hb_position_t *y)
433 return font->get_glyph_advance_for_direction (glyph, direction, x, y);
437 hb_font_get_glyph_origin_for_direction (hb_font_t *font,
438 hb_codepoint_t glyph,
439 hb_direction_t direction,
440 hb_position_t *x, hb_position_t *y)
442 return font->get_glyph_origin_for_direction (glyph, direction, x, y);
446 hb_font_add_glyph_origin_for_direction (hb_font_t *font,
447 hb_codepoint_t glyph,
448 hb_direction_t direction,
449 hb_position_t *x, hb_position_t *y)
451 return font->add_glyph_origin_for_direction (glyph, direction, x, y);
455 hb_font_subtract_glyph_origin_for_direction (hb_font_t *font,
456 hb_codepoint_t glyph,
457 hb_direction_t direction,
458 hb_position_t *x, hb_position_t *y)
460 return font->subtract_glyph_origin_for_direction (glyph, direction, x, y);
464 hb_font_get_glyph_kerning_for_direction (hb_font_t *font,
465 hb_codepoint_t first_glyph, hb_codepoint_t second_glyph,
466 hb_direction_t direction,
467 hb_position_t *x, hb_position_t *y)
469 return font->get_glyph_kerning_for_direction (first_glyph, second_glyph, direction, x, y);
473 hb_font_get_glyph_extents_for_origin (hb_font_t *font,
474 hb_codepoint_t glyph,
475 hb_direction_t direction,
476 hb_glyph_extents_t *extents)
478 return font->get_glyph_extents_for_origin (glyph, direction, extents);
482 hb_font_get_glyph_contour_point_for_origin (hb_font_t *font,
483 hb_codepoint_t glyph, unsigned int point_index,
484 hb_direction_t direction,
485 hb_position_t *x, hb_position_t *y)
487 return font->get_glyph_contour_point_for_origin (glyph, point_index, direction, x, y);
490 /* Generates gidDDD if glyph has no name. */
492 hb_font_glyph_to_string (hb_font_t *font,
493 hb_codepoint_t glyph,
494 char *s, unsigned int size)
496 font->glyph_to_string (glyph, s, size);
499 /* Parses gidDDD and uniUUUU strings automatically. */
501 hb_font_glyph_from_string (hb_font_t *font,
502 const char *s, int len, /* -1 means nul-terminated */
503 hb_codepoint_t *glyph)
505 return font->glyph_from_string (s, len, glyph);
513 static const hb_face_t _hb_face_nil = {
514 HB_OBJECT_HEADER_STATIC,
516 true, /* immutable */
518 NULL, /* reference_table_func */
519 NULL, /* user_data */
527 #define HB_SHAPER_IMPLEMENT(shaper) HB_SHAPER_DATA_INVALID,
528 #include "hb-shaper-list.hh"
529 #undef HB_SHAPER_IMPLEMENT
532 NULL, /* shape_plans */
537 hb_face_create_for_tables (hb_reference_table_func_t reference_table_func,
539 hb_destroy_func_t destroy)
543 if (!reference_table_func || !(face = hb_object_create<hb_face_t> ())) {
546 return hb_face_get_empty ();
549 face->reference_table_func = reference_table_func;
550 face->user_data = user_data;
551 face->destroy = destroy;
554 face->num_glyphs = (unsigned int) -1;
560 typedef struct hb_face_for_data_closure_t {
563 } hb_face_for_data_closure_t;
565 static hb_face_for_data_closure_t *
566 _hb_face_for_data_closure_create (hb_blob_t *blob, unsigned int index)
568 hb_face_for_data_closure_t *closure;
570 closure = (hb_face_for_data_closure_t *) malloc (sizeof (hb_face_for_data_closure_t));
571 if (unlikely (!closure))
574 closure->blob = blob;
575 closure->index = index;
581 _hb_face_for_data_closure_destroy (hb_face_for_data_closure_t *closure)
583 hb_blob_destroy (closure->blob);
588 _hb_face_for_data_reference_table (hb_face_t *face HB_UNUSED, hb_tag_t tag, void *user_data)
590 hb_face_for_data_closure_t *data = (hb_face_for_data_closure_t *) user_data;
592 if (tag == HB_TAG_NONE)
593 return hb_blob_reference (data->blob);
595 const OT::OpenTypeFontFile &ot_file = *OT::Sanitizer<OT::OpenTypeFontFile>::lock_instance (data->blob);
596 const OT::OpenTypeFontFace &ot_face = ot_file.get_face (data->index);
598 const OT::OpenTypeTable &table = ot_face.get_table_by_tag (tag);
600 hb_blob_t *blob = hb_blob_create_sub_blob (data->blob, table.offset, table.length);
606 hb_face_create (hb_blob_t *blob,
611 if (unlikely (!blob || !hb_blob_get_length (blob)))
612 return hb_face_get_empty ();
614 hb_face_for_data_closure_t *closure = _hb_face_for_data_closure_create (OT::Sanitizer<OT::OpenTypeFontFile>::sanitize (hb_blob_reference (blob)), index);
616 if (unlikely (!closure))
617 return hb_face_get_empty ();
619 face = hb_face_create_for_tables (_hb_face_for_data_reference_table,
621 (hb_destroy_func_t) _hb_face_for_data_closure_destroy);
623 hb_face_set_index (face, index);
629 hb_face_get_empty (void)
631 return const_cast<hb_face_t *> (&_hb_face_nil);
636 hb_face_reference (hb_face_t *face)
638 return hb_object_reference (face);
642 hb_face_destroy (hb_face_t *face)
644 if (!hb_object_destroy (face)) return;
646 for (hb_face_t::plan_node_t *node = face->shape_plans; node; )
648 hb_face_t::plan_node_t *next = node->next;
649 hb_shape_plan_destroy (node->shape_plan);
654 #define HB_SHAPER_IMPLEMENT(shaper) HB_SHAPER_DATA_DESTROY(shaper, face);
655 #include "hb-shaper-list.hh"
656 #undef HB_SHAPER_IMPLEMENT
659 face->destroy (face->user_data);
665 hb_face_set_user_data (hb_face_t *face,
666 hb_user_data_key_t *key,
668 hb_destroy_func_t destroy,
671 return hb_object_set_user_data (face, key, data, destroy, replace);
675 hb_face_get_user_data (hb_face_t *face,
676 hb_user_data_key_t *key)
678 return hb_object_get_user_data (face, key);
682 hb_face_make_immutable (hb_face_t *face)
684 if (hb_object_is_inert (face))
687 face->immutable = true;
691 hb_face_is_immutable (hb_face_t *face)
693 return face->immutable;
698 hb_face_reference_table (hb_face_t *face,
701 return face->reference_table (tag);
705 hb_face_reference_blob (hb_face_t *face)
707 return face->reference_table (HB_TAG_NONE);
711 hb_face_set_index (hb_face_t *face,
714 if (hb_object_is_inert (face))
721 hb_face_get_index (hb_face_t *face)
727 hb_face_set_upem (hb_face_t *face,
730 if (hb_object_is_inert (face))
737 hb_face_get_upem (hb_face_t *face)
739 return face->get_upem ();
743 hb_face_t::load_upem (void) const
745 hb_blob_t *head_blob = OT::Sanitizer<OT::head>::sanitize (reference_table (HB_OT_TAG_head));
746 const OT::head *head_table = OT::Sanitizer<OT::head>::lock_instance (head_blob);
747 upem = head_table->get_upem ();
748 hb_blob_destroy (head_blob);
752 hb_face_set_glyph_count (hb_face_t *face,
753 unsigned int glyph_count)
755 if (hb_object_is_inert (face))
758 face->num_glyphs = glyph_count;
762 hb_face_get_glyph_count (hb_face_t *face)
764 return face->get_num_glyphs ();
768 hb_face_t::load_num_glyphs (void) const
770 hb_blob_t *maxp_blob = OT::Sanitizer<OT::maxp>::sanitize (reference_table (HB_OT_TAG_maxp));
771 const OT::maxp *maxp_table = OT::Sanitizer<OT::maxp>::lock_instance (maxp_blob);
772 num_glyphs = maxp_table->get_num_glyphs ();
773 hb_blob_destroy (maxp_blob);
782 hb_font_create (hb_face_t *face)
786 if (unlikely (!face))
787 face = hb_face_get_empty ();
788 if (unlikely (hb_object_is_inert (face)))
789 return hb_font_get_empty ();
790 if (!(font = hb_object_create<hb_font_t> ()))
791 return hb_font_get_empty ();
793 hb_face_make_immutable (face);
794 font->face = hb_face_reference (face);
795 font->klass = hb_font_funcs_get_empty ();
801 hb_font_create_sub_font (hb_font_t *parent)
803 if (unlikely (!parent))
804 return hb_font_get_empty ();
806 hb_font_t *font = hb_font_create (parent->face);
808 if (unlikely (hb_object_is_inert (font)))
811 hb_font_make_immutable (parent);
812 font->parent = hb_font_reference (parent);
814 font->x_scale = parent->x_scale;
815 font->y_scale = parent->y_scale;
816 font->x_ppem = parent->x_ppem;
817 font->y_ppem = parent->y_ppem;
823 hb_font_get_empty (void)
825 static const hb_font_t _hb_font_nil = {
826 HB_OBJECT_HEADER_STATIC,
828 true, /* immutable */
831 const_cast<hb_face_t *> (&_hb_face_nil),
839 const_cast<hb_font_funcs_t *> (&_hb_font_funcs_nil), /* klass */
840 NULL, /* user_data */
844 #define HB_SHAPER_IMPLEMENT(shaper) HB_SHAPER_DATA_INVALID,
845 #include "hb-shaper-list.hh"
846 #undef HB_SHAPER_IMPLEMENT
850 return const_cast<hb_font_t *> (&_hb_font_nil);
854 hb_font_reference (hb_font_t *font)
856 return hb_object_reference (font);
860 hb_font_destroy (hb_font_t *font)
862 if (!hb_object_destroy (font)) return;
864 #define HB_SHAPER_IMPLEMENT(shaper) HB_SHAPER_DATA_DESTROY(shaper, font);
865 #include "hb-shaper-list.hh"
866 #undef HB_SHAPER_IMPLEMENT
869 font->destroy (font->user_data);
871 hb_font_destroy (font->parent);
872 hb_face_destroy (font->face);
873 hb_font_funcs_destroy (font->klass);
879 hb_font_set_user_data (hb_font_t *font,
880 hb_user_data_key_t *key,
882 hb_destroy_func_t destroy,
885 return hb_object_set_user_data (font, key, data, destroy, replace);
889 hb_font_get_user_data (hb_font_t *font,
890 hb_user_data_key_t *key)
892 return hb_object_get_user_data (font, key);
896 hb_font_make_immutable (hb_font_t *font)
898 if (hb_object_is_inert (font))
901 font->immutable = true;
905 hb_font_is_immutable (hb_font_t *font)
907 return font->immutable;
911 hb_font_get_parent (hb_font_t *font)
917 hb_font_get_face (hb_font_t *font)
924 hb_font_set_funcs (hb_font_t *font,
925 hb_font_funcs_t *klass,
927 hb_destroy_func_t destroy)
929 if (font->immutable) {
936 font->destroy (font->user_data);
939 klass = hb_font_funcs_get_empty ();
941 hb_font_funcs_reference (klass);
942 hb_font_funcs_destroy (font->klass);
944 font->user_data = user_data;
945 font->destroy = destroy;
949 hb_font_set_funcs_data (hb_font_t *font,
951 hb_destroy_func_t destroy)
953 /* Destroy user_data? */
954 if (font->immutable) {
961 font->destroy (font->user_data);
963 font->user_data = user_data;
964 font->destroy = destroy;
969 hb_font_set_scale (hb_font_t *font,
976 font->x_scale = x_scale;
977 font->y_scale = y_scale;
981 hb_font_get_scale (hb_font_t *font,
985 if (x_scale) *x_scale = font->x_scale;
986 if (y_scale) *y_scale = font->y_scale;
990 hb_font_set_ppem (hb_font_t *font,
997 font->x_ppem = x_ppem;
998 font->y_ppem = y_ppem;
1002 hb_font_get_ppem (hb_font_t *font,
1003 unsigned int *x_ppem,
1004 unsigned int *y_ppem)
1006 if (x_ppem) *x_ppem = font->x_ppem;
1007 if (y_ppem) *y_ppem = font->y_ppem;