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"
38 #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 (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
340 hb_font_get_glyph (hb_font_t *font,
341 hb_codepoint_t unicode, hb_codepoint_t variation_selector,
342 hb_codepoint_t *glyph)
344 return font->get_glyph (unicode, variation_selector, glyph);
348 hb_font_get_glyph_h_advance (hb_font_t *font,
349 hb_codepoint_t glyph)
351 return font->get_glyph_h_advance (glyph);
355 hb_font_get_glyph_v_advance (hb_font_t *font,
356 hb_codepoint_t glyph)
358 return font->get_glyph_v_advance (glyph);
362 hb_font_get_glyph_h_origin (hb_font_t *font,
363 hb_codepoint_t glyph,
364 hb_position_t *x, hb_position_t *y)
366 return font->get_glyph_h_origin (glyph, x, y);
370 hb_font_get_glyph_v_origin (hb_font_t *font,
371 hb_codepoint_t glyph,
372 hb_position_t *x, hb_position_t *y)
374 return font->get_glyph_v_origin (glyph, x, y);
378 hb_font_get_glyph_h_kerning (hb_font_t *font,
379 hb_codepoint_t left_glyph, hb_codepoint_t right_glyph)
381 return font->get_glyph_h_kerning (left_glyph, right_glyph);
385 hb_font_get_glyph_v_kerning (hb_font_t *font,
386 hb_codepoint_t left_glyph, hb_codepoint_t right_glyph)
388 return font->get_glyph_v_kerning (left_glyph, right_glyph);
392 hb_font_get_glyph_extents (hb_font_t *font,
393 hb_codepoint_t glyph,
394 hb_glyph_extents_t *extents)
396 return font->get_glyph_extents (glyph, extents);
400 hb_font_get_glyph_contour_point (hb_font_t *font,
401 hb_codepoint_t glyph, unsigned int point_index,
402 hb_position_t *x, hb_position_t *y)
404 return font->get_glyph_contour_point (glyph, point_index, x, y);
408 hb_font_get_glyph_name (hb_font_t *font,
409 hb_codepoint_t glyph,
410 char *name, unsigned int size)
412 return font->get_glyph_name (glyph, name, size);
416 hb_font_get_glyph_from_name (hb_font_t *font,
417 const char *name, int len, /* -1 means nul-terminated */
418 hb_codepoint_t *glyph)
420 return font->get_glyph_from_name (name, len, glyph);
424 /* A bit higher-level, and with fallback */
427 hb_font_get_glyph_advance_for_direction (hb_font_t *font,
428 hb_codepoint_t glyph,
429 hb_direction_t direction,
430 hb_position_t *x, hb_position_t *y)
432 return font->get_glyph_advance_for_direction (glyph, direction, x, y);
436 hb_font_get_glyph_origin_for_direction (hb_font_t *font,
437 hb_codepoint_t glyph,
438 hb_direction_t direction,
439 hb_position_t *x, hb_position_t *y)
441 return font->get_glyph_origin_for_direction (glyph, direction, x, y);
445 hb_font_add_glyph_origin_for_direction (hb_font_t *font,
446 hb_codepoint_t glyph,
447 hb_direction_t direction,
448 hb_position_t *x, hb_position_t *y)
450 return font->add_glyph_origin_for_direction (glyph, direction, x, y);
454 hb_font_subtract_glyph_origin_for_direction (hb_font_t *font,
455 hb_codepoint_t glyph,
456 hb_direction_t direction,
457 hb_position_t *x, hb_position_t *y)
459 return font->subtract_glyph_origin_for_direction (glyph, direction, x, y);
463 hb_font_get_glyph_kerning_for_direction (hb_font_t *font,
464 hb_codepoint_t first_glyph, hb_codepoint_t second_glyph,
465 hb_direction_t direction,
466 hb_position_t *x, hb_position_t *y)
468 return font->get_glyph_kerning_for_direction (first_glyph, second_glyph, direction, x, y);
472 hb_font_get_glyph_extents_for_origin (hb_font_t *font,
473 hb_codepoint_t glyph,
474 hb_direction_t direction,
475 hb_glyph_extents_t *extents)
477 return font->get_glyph_extents_for_origin (glyph, direction, extents);
481 hb_font_get_glyph_contour_point_for_origin (hb_font_t *font,
482 hb_codepoint_t glyph, unsigned int point_index,
483 hb_direction_t direction,
484 hb_position_t *x, hb_position_t *y)
486 return font->get_glyph_contour_point_for_origin (glyph, point_index, direction, x, y);
489 /* Generates gidDDD if glyph has no name. */
491 hb_font_glyph_to_string (hb_font_t *font,
492 hb_codepoint_t glyph,
493 char *s, unsigned int size)
495 font->glyph_to_string (glyph, s, size);
498 /* Parses gidDDD and uniUUUU strings automatically. */
500 hb_font_glyph_from_string (hb_font_t *font,
501 const char *s, int len, /* -1 means nul-terminated */
502 hb_codepoint_t *glyph)
504 return font->glyph_from_string (s, len, glyph);
512 static const hb_face_t _hb_face_nil = {
513 HB_OBJECT_HEADER_STATIC,
515 true, /* immutable */
517 NULL, /* reference_table_func */
518 NULL, /* user_data */
525 #define HB_SHAPER_IMPLEMENT(shaper) HB_SHAPER_DATA_INVALID,
526 #include "hb-shaper-list.hh"
527 #undef HB_SHAPER_IMPLEMENT
530 NULL, /* shape_plans */
535 hb_face_create_for_tables (hb_reference_table_func_t reference_table_func,
537 hb_destroy_func_t destroy)
541 if (!reference_table_func || !(face = hb_object_create<hb_face_t> ())) {
544 return hb_face_get_empty ();
547 face->reference_table_func = reference_table_func;
548 face->user_data = user_data;
549 face->destroy = destroy;
557 typedef struct hb_face_for_data_closure_t {
560 } hb_face_for_data_closure_t;
562 static hb_face_for_data_closure_t *
563 _hb_face_for_data_closure_create (hb_blob_t *blob, unsigned int index)
565 hb_face_for_data_closure_t *closure;
567 closure = (hb_face_for_data_closure_t *) malloc (sizeof (hb_face_for_data_closure_t));
568 if (unlikely (!closure))
571 closure->blob = blob;
572 closure->index = index;
578 _hb_face_for_data_closure_destroy (hb_face_for_data_closure_t *closure)
580 hb_blob_destroy (closure->blob);
585 _hb_face_for_data_reference_table (hb_face_t *face HB_UNUSED, hb_tag_t tag, void *user_data)
587 hb_face_for_data_closure_t *data = (hb_face_for_data_closure_t *) user_data;
589 if (tag == HB_TAG_NONE)
590 return hb_blob_reference (data->blob);
592 const OpenTypeFontFile &ot_file = *Sanitizer<OpenTypeFontFile>::lock_instance (data->blob);
593 const OpenTypeFontFace &ot_face = ot_file.get_face (data->index);
595 const OpenTypeTable &table = ot_face.get_table_by_tag (tag);
597 hb_blob_t *blob = hb_blob_create_sub_blob (data->blob, table.offset, table.length);
603 hb_face_create (hb_blob_t *blob,
608 if (unlikely (!blob || !hb_blob_get_length (blob)))
609 return hb_face_get_empty ();
611 hb_face_for_data_closure_t *closure = _hb_face_for_data_closure_create (Sanitizer<OpenTypeFontFile>::sanitize (hb_blob_reference (blob)), index);
613 if (unlikely (!closure))
614 return hb_face_get_empty ();
616 face = hb_face_create_for_tables (_hb_face_for_data_reference_table,
618 (hb_destroy_func_t) _hb_face_for_data_closure_destroy);
620 hb_face_set_index (face, index);
626 hb_face_get_empty (void)
628 return const_cast<hb_face_t *> (&_hb_face_nil);
633 hb_face_reference (hb_face_t *face)
635 return hb_object_reference (face);
639 hb_face_destroy (hb_face_t *face)
641 if (!hb_object_destroy (face)) return;
643 for (hb_face_t::plan_node_t *node = face->shape_plans; node; )
645 hb_face_t::plan_node_t *next = node->next;
646 hb_shape_plan_destroy (node->shape_plan);
651 #define HB_SHAPER_IMPLEMENT(shaper) HB_SHAPER_DATA_DESTROY(shaper, face);
652 #include "hb-shaper-list.hh"
653 #undef HB_SHAPER_IMPLEMENT
656 face->destroy (face->user_data);
662 hb_face_set_user_data (hb_face_t *face,
663 hb_user_data_key_t *key,
665 hb_destroy_func_t destroy,
668 return hb_object_set_user_data (face, key, data, destroy, replace);
672 hb_face_get_user_data (hb_face_t *face,
673 hb_user_data_key_t *key)
675 return hb_object_get_user_data (face, key);
679 hb_face_make_immutable (hb_face_t *face)
681 if (hb_object_is_inert (face))
684 face->immutable = true;
688 hb_face_is_immutable (hb_face_t *face)
690 return face->immutable;
695 hb_face_reference_table (hb_face_t *face,
698 return face->reference_table (tag);
702 hb_face_reference_blob (hb_face_t *face)
704 return face->reference_table (HB_TAG_NONE);
708 hb_face_set_index (hb_face_t *face,
711 if (hb_object_is_inert (face))
718 hb_face_get_index (hb_face_t *face)
724 hb_face_set_upem (hb_face_t *face,
727 if (hb_object_is_inert (face))
734 hb_face_get_upem (hb_face_t *face)
736 return face->get_upem ();
741 hb_face_t::load_upem (void) const
743 hb_blob_t *head_blob = Sanitizer<head>::sanitize (reference_table (HB_OT_TAG_head));
744 const head *head_table = Sanitizer<head>::lock_instance (head_blob);
745 upem = head_table->get_upem ();
746 hb_blob_destroy (head_blob);
755 hb_font_create (hb_face_t *face)
759 if (unlikely (!face))
760 face = hb_face_get_empty ();
761 if (unlikely (hb_object_is_inert (face)))
762 return hb_font_get_empty ();
763 if (!(font = hb_object_create<hb_font_t> ()))
764 return hb_font_get_empty ();
766 hb_face_make_immutable (face);
767 font->face = hb_face_reference (face);
768 font->klass = hb_font_funcs_get_empty ();
774 hb_font_create_sub_font (hb_font_t *parent)
776 if (unlikely (!parent))
777 return hb_font_get_empty ();
779 hb_font_t *font = hb_font_create (parent->face);
781 if (unlikely (hb_object_is_inert (font)))
784 hb_font_make_immutable (parent);
785 font->parent = hb_font_reference (parent);
787 font->x_scale = parent->x_scale;
788 font->y_scale = parent->y_scale;
789 font->x_ppem = parent->x_ppem;
790 font->y_ppem = parent->y_ppem;
796 hb_font_get_empty (void)
798 static const hb_font_t _hb_font_nil = {
799 HB_OBJECT_HEADER_STATIC,
801 true, /* immutable */
804 const_cast<hb_face_t *> (&_hb_face_nil),
812 const_cast<hb_font_funcs_t *> (&_hb_font_funcs_nil), /* klass */
813 NULL, /* user_data */
817 #define HB_SHAPER_IMPLEMENT(shaper) HB_SHAPER_DATA_INVALID,
818 #include "hb-shaper-list.hh"
819 #undef HB_SHAPER_IMPLEMENT
823 return const_cast<hb_font_t *> (&_hb_font_nil);
827 hb_font_reference (hb_font_t *font)
829 return hb_object_reference (font);
833 hb_font_destroy (hb_font_t *font)
835 if (!hb_object_destroy (font)) return;
837 #define HB_SHAPER_IMPLEMENT(shaper) HB_SHAPER_DATA_DESTROY(shaper, font);
838 #include "hb-shaper-list.hh"
839 #undef HB_SHAPER_IMPLEMENT
842 font->destroy (font->user_data);
844 hb_font_destroy (font->parent);
845 hb_face_destroy (font->face);
846 hb_font_funcs_destroy (font->klass);
852 hb_font_set_user_data (hb_font_t *font,
853 hb_user_data_key_t *key,
855 hb_destroy_func_t destroy,
858 return hb_object_set_user_data (font, key, data, destroy, replace);
862 hb_font_get_user_data (hb_font_t *font,
863 hb_user_data_key_t *key)
865 return hb_object_get_user_data (font, key);
869 hb_font_make_immutable (hb_font_t *font)
871 if (hb_object_is_inert (font))
874 font->immutable = true;
878 hb_font_is_immutable (hb_font_t *font)
880 return font->immutable;
884 hb_font_get_parent (hb_font_t *font)
890 hb_font_get_face (hb_font_t *font)
897 hb_font_set_funcs (hb_font_t *font,
898 hb_font_funcs_t *klass,
900 hb_destroy_func_t destroy)
902 if (font->immutable) {
909 font->destroy (font->user_data);
912 klass = hb_font_funcs_get_empty ();
914 hb_font_funcs_reference (klass);
915 hb_font_funcs_destroy (font->klass);
917 font->user_data = user_data;
918 font->destroy = destroy;
922 hb_font_set_funcs_data (hb_font_t *font,
924 hb_destroy_func_t destroy)
926 /* Destroy user_data? */
927 if (font->immutable) {
934 font->destroy (font->user_data);
936 font->user_data = user_data;
937 font->destroy = destroy;
942 hb_font_set_scale (hb_font_t *font,
949 font->x_scale = x_scale;
950 font->y_scale = y_scale;
954 hb_font_get_scale (hb_font_t *font,
958 if (x_scale) *x_scale = font->x_scale;
959 if (y_scale) *y_scale = font->y_scale;
963 hb_font_set_ppem (hb_font_t *font,
970 font->x_ppem = x_ppem;
971 font->y_ppem = y_ppem;
975 hb_font_get_ppem (hb_font_t *font,
976 unsigned int *x_ppem,
977 unsigned int *y_ppem)
979 if (x_ppem) *x_ppem = font->x_ppem;
980 if (y_ppem) *y_ppem = font->y_ppem;