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"
45 hb_font_get_glyph_nil (hb_font_t *font HB_UNUSED,
46 void *font_data HB_UNUSED,
47 hb_codepoint_t unicode,
48 hb_codepoint_t variation_selector,
49 hb_codepoint_t *glyph,
50 void *user_data HB_UNUSED)
53 return hb_font_get_glyph (font->parent, unicode, variation_selector, glyph);
60 hb_font_get_glyph_h_advance_nil (hb_font_t *font HB_UNUSED,
61 void *font_data HB_UNUSED,
63 void *user_data HB_UNUSED)
66 return font->parent_scale_x_distance (hb_font_get_glyph_h_advance (font->parent, glyph));
72 hb_font_get_glyph_v_advance_nil (hb_font_t *font HB_UNUSED,
73 void *font_data HB_UNUSED,
75 void *user_data HB_UNUSED)
78 return font->parent_scale_y_distance (hb_font_get_glyph_v_advance (font->parent, glyph));
84 hb_font_get_glyph_h_origin_nil (hb_font_t *font HB_UNUSED,
85 void *font_data HB_UNUSED,
89 void *user_data HB_UNUSED)
92 hb_bool_t ret = hb_font_get_glyph_h_origin (font->parent,
96 font->parent_scale_position (x, y);
105 hb_font_get_glyph_v_origin_nil (hb_font_t *font HB_UNUSED,
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,
117 font->parent_scale_position (x, y);
126 hb_font_get_glyph_h_kerning_nil (hb_font_t *font HB_UNUSED,
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 (hb_font_get_glyph_h_kerning (font->parent, left_glyph, right_glyph));
139 hb_font_get_glyph_v_kerning_nil (hb_font_t *font HB_UNUSED,
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 (hb_font_get_glyph_v_kerning (font->parent, top_glyph, bottom_glyph));
152 hb_font_get_glyph_extents_nil (hb_font_t *font HB_UNUSED,
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 = hb_font_get_glyph_extents (font->parent,
163 font->parent_scale_position (&extents->x_bearing, &extents->y_bearing);
164 font->parent_scale_distance (&extents->width, &extents->height);
169 memset (extents, 0, sizeof (*extents));
174 hb_font_get_glyph_contour_point_nil (hb_font_t *font HB_UNUSED,
175 void *font_data HB_UNUSED,
176 hb_codepoint_t glyph,
177 unsigned int point_index,
180 void *user_data HB_UNUSED)
183 hb_bool_t ret = hb_font_get_glyph_contour_point (font->parent,
187 font->parent_scale_position (x, y);
196 static hb_font_funcs_t _hb_font_funcs_nil = {
197 HB_OBJECT_HEADER_STATIC,
199 TRUE, /* immutable */
202 #define HB_FONT_FUNC_IMPLEMENT(name) hb_font_get_##name##_nil,
203 HB_FONT_FUNCS_IMPLEMENT_CALLBACKS
204 #undef HB_FONT_FUNC_IMPLEMENT
210 hb_font_funcs_create (void)
212 hb_font_funcs_t *ffuncs;
214 if (!(ffuncs = hb_object_create<hb_font_funcs_t> ()))
215 return &_hb_font_funcs_nil;
217 ffuncs->get = _hb_font_funcs_nil.get;
223 hb_font_funcs_get_empty (void)
225 return &_hb_font_funcs_nil;
229 hb_font_funcs_reference (hb_font_funcs_t *ffuncs)
231 return hb_object_reference (ffuncs);
235 hb_font_funcs_destroy (hb_font_funcs_t *ffuncs)
237 if (!hb_object_destroy (ffuncs)) return;
239 #define HB_FONT_FUNC_IMPLEMENT(name) if (ffuncs->destroy.name) ffuncs->destroy.name (ffuncs->user_data.name);
240 HB_FONT_FUNCS_IMPLEMENT_CALLBACKS
241 #undef HB_FONT_FUNC_IMPLEMENT
247 hb_font_funcs_set_user_data (hb_font_funcs_t *ffuncs,
248 hb_user_data_key_t *key,
250 hb_destroy_func_t destroy)
252 return hb_object_set_user_data (ffuncs, key, data, destroy);
256 hb_font_funcs_get_user_data (hb_font_funcs_t *ffuncs,
257 hb_user_data_key_t *key)
259 return hb_object_get_user_data (ffuncs, key);
264 hb_font_funcs_make_immutable (hb_font_funcs_t *ffuncs)
266 if (hb_object_is_inert (ffuncs))
269 ffuncs->immutable = TRUE;
273 hb_font_funcs_is_immutable (hb_font_funcs_t *ffuncs)
275 return ffuncs->immutable;
279 #define HB_FONT_FUNC_IMPLEMENT(name) \
282 hb_font_funcs_set_##name##_func (hb_font_funcs_t *ffuncs, \
283 hb_font_get_##name##_func_t func, \
285 hb_destroy_func_t destroy) \
287 if (ffuncs->immutable) \
290 if (ffuncs->destroy.name) \
291 ffuncs->destroy.name (ffuncs->user_data.name); \
294 ffuncs->get.name = func; \
295 ffuncs->user_data.name = user_data; \
296 ffuncs->destroy.name = destroy; \
298 ffuncs->get.name = hb_font_get_##name##_nil; \
299 ffuncs->user_data.name = NULL; \
300 ffuncs->destroy.name = NULL; \
304 HB_FONT_FUNCS_IMPLEMENT_CALLBACKS
305 #undef HB_FONT_FUNC_IMPLEMENT
309 hb_font_get_glyph (hb_font_t *font,
310 hb_codepoint_t unicode, hb_codepoint_t variation_selector,
311 hb_codepoint_t *glyph)
314 return font->klass->get.glyph (font, font->user_data,
315 unicode, variation_selector, glyph,
316 font->klass->user_data.glyph);
320 hb_font_get_glyph_h_advance (hb_font_t *font,
321 hb_codepoint_t glyph)
323 return font->klass->get.glyph_h_advance (font, font->user_data,
325 font->klass->user_data.glyph_h_advance);
329 hb_font_get_glyph_v_advance (hb_font_t *font,
330 hb_codepoint_t glyph)
332 return font->klass->get.glyph_v_advance (font, font->user_data,
334 font->klass->user_data.glyph_v_advance);
338 hb_font_get_glyph_h_origin (hb_font_t *font,
339 hb_codepoint_t glyph,
340 hb_position_t *x, hb_position_t *y)
343 return font->klass->get.glyph_h_origin (font, font->user_data,
345 font->klass->user_data.glyph_h_origin);
349 hb_font_get_glyph_v_origin (hb_font_t *font,
350 hb_codepoint_t glyph,
351 hb_position_t *x, hb_position_t *y)
354 return font->klass->get.glyph_v_origin (font, font->user_data,
356 font->klass->user_data.glyph_v_origin);
360 hb_font_get_glyph_h_kerning (hb_font_t *font,
361 hb_codepoint_t left_glyph, hb_codepoint_t right_glyph)
363 return font->klass->get.glyph_h_kerning (font, font->user_data,
364 left_glyph, right_glyph,
365 font->klass->user_data.glyph_h_kerning);
369 hb_font_get_glyph_v_kerning (hb_font_t *font,
370 hb_codepoint_t left_glyph, hb_codepoint_t right_glyph)
372 return font->klass->get.glyph_v_kerning (font, font->user_data,
373 left_glyph, right_glyph,
374 font->klass->user_data.glyph_v_kerning);
378 hb_font_get_glyph_extents (hb_font_t *font,
379 hb_codepoint_t glyph,
380 hb_glyph_extents_t *extents)
382 memset (extents, 0, sizeof (*extents));
383 return font->klass->get.glyph_extents (font, font->user_data,
386 font->klass->user_data.glyph_extents);
390 hb_font_get_glyph_contour_point (hb_font_t *font,
391 hb_codepoint_t glyph, unsigned int point_index,
392 hb_position_t *x, hb_position_t *y)
395 return font->klass->get.glyph_contour_point (font, font->user_data,
398 font->klass->user_data.glyph_contour_point);
402 /* A bit higher-level, and with fallback */
405 hb_font_get_glyph_advance_for_direction (hb_font_t *font,
406 hb_codepoint_t glyph,
407 hb_direction_t direction,
408 hb_position_t *x, hb_position_t *y)
410 if (likely (HB_DIRECTION_IS_HORIZONTAL (direction))) {
411 *x = hb_font_get_glyph_h_advance (font, glyph);
415 *y = hb_font_get_glyph_v_advance (font, glyph);
420 guess_v_origin_minus_h_origin (hb_font_t *font,
421 hb_codepoint_t glyph,
422 hb_position_t *x, hb_position_t *y)
424 *x = hb_font_get_glyph_h_advance (font, glyph) / 2;
426 /* TODO use font_metics.ascent */
432 hb_font_get_glyph_origin_for_direction (hb_font_t *font,
433 hb_codepoint_t glyph,
434 hb_direction_t direction,
435 hb_position_t *x, hb_position_t *y)
437 if (likely (HB_DIRECTION_IS_HORIZONTAL (direction))) {
438 hb_bool_t ret = hb_font_get_glyph_h_origin (font, glyph, x, y);
439 if (!ret && (ret = hb_font_get_glyph_v_origin (font, glyph, x, y))) {
440 hb_position_t dx, dy;
441 guess_v_origin_minus_h_origin (font, glyph, &dx, &dy);
445 hb_bool_t ret = hb_font_get_glyph_v_origin (font, glyph, x, y);
446 if (!ret && (ret = hb_font_get_glyph_h_origin (font, glyph, x, y))) {
447 hb_position_t dx, dy;
448 guess_v_origin_minus_h_origin (font, glyph, &dx, &dy);
455 hb_font_add_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 hb_position_t origin_x, origin_y;
462 hb_font_get_glyph_origin_for_direction (font, glyph, direction, &origin_x, &origin_y);
469 hb_font_subtract_glyph_origin_for_direction (hb_font_t *font,
470 hb_codepoint_t glyph,
471 hb_direction_t direction,
472 hb_position_t *x, hb_position_t *y)
474 hb_position_t origin_x, origin_y;
476 hb_font_get_glyph_origin_for_direction (font, glyph, direction, &origin_x, &origin_y);
483 hb_font_get_glyph_kerning_for_direction (hb_font_t *font,
484 hb_codepoint_t first_glyph, hb_codepoint_t second_glyph,
485 hb_direction_t direction,
486 hb_position_t *x, hb_position_t *y)
488 if (likely (HB_DIRECTION_IS_HORIZONTAL (direction))) {
489 *x = hb_font_get_glyph_h_kerning (font, first_glyph, second_glyph);
493 *y = hb_font_get_glyph_v_kerning (font, first_glyph, second_glyph);
498 hb_font_get_glyph_extents_for_origin (hb_font_t *font,
499 hb_codepoint_t glyph,
500 hb_direction_t direction,
501 hb_glyph_extents_t *extents)
503 hb_bool_t ret = hb_font_get_glyph_extents (font, glyph, extents);
506 hb_font_subtract_glyph_origin_for_direction (font, glyph, direction, &extents->x_bearing, &extents->y_bearing);
512 hb_font_get_glyph_contour_point_for_origin (hb_font_t *font,
513 hb_codepoint_t glyph, unsigned int point_index,
514 hb_direction_t direction,
515 hb_position_t *x, hb_position_t *y)
517 hb_bool_t ret = hb_font_get_glyph_contour_point (font, glyph, point_index, x, y);
520 hb_font_subtract_glyph_origin_for_direction (font, glyph, direction, x, y);
530 static hb_face_t _hb_face_nil = {
531 HB_OBJECT_HEADER_STATIC,
533 TRUE, /* immutable */
535 NULL, /* get_table */
536 NULL, /* user_data */
539 NULL, /* ot_layout */
546 hb_face_create_for_tables (hb_get_table_func_t get_table,
548 hb_destroy_func_t destroy)
552 if (!get_table || !(face = hb_object_create<hb_face_t> ())) {
555 return &_hb_face_nil;
558 face->get_table = get_table;
559 face->user_data = user_data;
560 face->destroy = destroy;
562 face->ot_layout = _hb_ot_layout_create (face);
564 face->upem = _hb_ot_layout_get_upem (face);
570 typedef struct _hb_face_for_data_closure_t {
573 } hb_face_for_data_closure_t;
575 static hb_face_for_data_closure_t *
576 _hb_face_for_data_closure_create (hb_blob_t *blob, unsigned int index)
578 hb_face_for_data_closure_t *closure;
580 closure = (hb_face_for_data_closure_t *) malloc (sizeof (hb_face_for_data_closure_t));
581 if (unlikely (!closure))
584 closure->blob = blob;
585 closure->index = index;
591 _hb_face_for_data_closure_destroy (hb_face_for_data_closure_t *closure)
593 hb_blob_destroy (closure->blob);
598 _hb_face_for_data_get_table (hb_face_t *face HB_UNUSED, hb_tag_t tag, void *user_data)
600 hb_face_for_data_closure_t *data = (hb_face_for_data_closure_t *) user_data;
602 const OpenTypeFontFile &ot_file = *Sanitizer<OpenTypeFontFile>::lock_instance (data->blob);
603 const OpenTypeFontFace &ot_face = ot_file.get_face (data->index);
605 const OpenTypeTable &table = ot_face.get_table_by_tag (tag);
607 hb_blob_t *blob = hb_blob_create_sub_blob (data->blob, table.offset, table.length);
613 hb_face_create (hb_blob_t *blob,
616 if (unlikely (!blob || !hb_blob_get_length (blob)))
617 return &_hb_face_nil;
619 hb_face_for_data_closure_t *closure = _hb_face_for_data_closure_create (Sanitizer<OpenTypeFontFile>::sanitize (hb_blob_reference (blob)), index);
621 if (unlikely (!closure))
622 return &_hb_face_nil;
624 return hb_face_create_for_tables (_hb_face_for_data_get_table,
626 (hb_destroy_func_t) _hb_face_for_data_closure_destroy);
630 hb_face_get_empty (void)
632 return &_hb_face_nil;
637 hb_face_reference (hb_face_t *face)
639 return hb_object_reference (face);
643 hb_face_destroy (hb_face_t *face)
645 if (!hb_object_destroy (face)) return;
647 _hb_ot_layout_destroy (face->ot_layout);
650 face->destroy (face->user_data);
656 hb_face_set_user_data (hb_face_t *face,
657 hb_user_data_key_t *key,
659 hb_destroy_func_t destroy)
661 return hb_object_set_user_data (face, key, data, destroy);
665 hb_face_get_user_data (hb_face_t *face,
666 hb_user_data_key_t *key)
668 return hb_object_get_user_data (face, key);
672 hb_face_make_immutable (hb_face_t *face)
674 if (hb_object_is_inert (face))
677 face->immutable = true;
681 hb_face_is_immutable (hb_face_t *face)
683 return face->immutable;
688 hb_face_reference_table (hb_face_t *face,
693 if (unlikely (!face || !face->get_table))
694 return hb_blob_get_empty ();
696 blob = face->get_table (face, tag, face->user_data);
697 if (unlikely (!blob))
698 return hb_blob_get_empty ();
704 hb_face_get_upem (hb_face_t *face)
706 return _hb_ot_layout_get_upem (face);
714 static hb_font_t _hb_font_nil = {
715 HB_OBJECT_HEADER_STATIC,
717 TRUE, /* immutable */
728 &_hb_font_funcs_nil, /* klass */
729 NULL, /* user_data */
734 hb_font_create (hb_face_t *face)
738 if (unlikely (!face))
739 face = &_hb_face_nil;
740 if (unlikely (hb_object_is_inert (face)))
741 return &_hb_font_nil;
742 if (!(font = hb_object_create<hb_font_t> ()))
743 return &_hb_font_nil;
745 hb_face_make_immutable (face);
746 font->face = hb_face_reference (face);
747 font->klass = &_hb_font_funcs_nil;
753 hb_font_create_sub_font (hb_font_t *parent)
755 if (unlikely (!parent))
756 return &_hb_font_nil;
758 hb_font_t *font = hb_font_create (parent->face);
760 if (unlikely (hb_object_is_inert (font)))
763 hb_font_make_immutable (parent);
764 font->parent = hb_font_reference (parent);
766 font->x_scale = parent->x_scale;
767 font->y_scale = parent->y_scale;
768 font->x_ppem = parent->x_ppem;
769 font->y_ppem = parent->y_ppem;
771 font->klass = &_hb_font_funcs_nil;
777 hb_font_get_empty (void)
779 return &_hb_font_nil;
783 hb_font_reference (hb_font_t *font)
785 return hb_object_reference (font);
789 hb_font_destroy (hb_font_t *font)
791 if (!hb_object_destroy (font)) return;
793 hb_font_destroy (font->parent);
794 hb_face_destroy (font->face);
795 hb_font_funcs_destroy (font->klass);
797 font->destroy (font->user_data);
803 hb_font_set_user_data (hb_font_t *font,
804 hb_user_data_key_t *key,
806 hb_destroy_func_t destroy)
808 return hb_object_set_user_data (font, key, data, destroy);
812 hb_font_get_user_data (hb_font_t *font,
813 hb_user_data_key_t *key)
815 return hb_object_get_user_data (font, key);
819 hb_font_make_immutable (hb_font_t *font)
821 if (hb_object_is_inert (font))
824 font->immutable = true;
828 hb_font_is_immutable (hb_font_t *font)
830 return font->immutable;
834 hb_font_get_parent (hb_font_t *font)
840 hb_font_get_face (hb_font_t *font)
847 hb_font_set_funcs (hb_font_t *font,
848 hb_font_funcs_t *klass,
850 hb_destroy_func_t destroy)
856 font->destroy (font->user_data);
859 klass = &_hb_font_funcs_nil;
861 hb_font_funcs_reference (klass);
862 hb_font_funcs_destroy (font->klass);
864 font->user_data = user_data;
865 font->destroy = destroy;
870 hb_font_set_scale (hb_font_t *font,
877 font->x_scale = x_scale;
878 font->y_scale = y_scale;
882 hb_font_get_scale (hb_font_t *font,
886 if (x_scale) *x_scale = font->x_scale;
887 if (y_scale) *y_scale = font->y_scale;
891 hb_font_set_ppem (hb_font_t *font,
898 font->x_ppem = x_ppem;
899 font->y_ppem = y_ppem;
903 hb_font_get_ppem (hb_font_t *font,
904 unsigned int *x_ppem,
905 unsigned int *y_ppem)
907 if (x_ppem) *x_ppem = font->x_ppem;
908 if (y_ppem) *y_ppem = font->y_ppem;