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 hb_position_t *x_advance,
64 hb_position_t *y_advance,
65 void *user_data HB_UNUSED)
68 hb_bool_t ret = hb_font_get_glyph_h_advance (font->parent,
70 x_advance, y_advance);
71 font->parent_scale_distance (x_advance, y_advance);
75 *x_advance = *y_advance = 0;
80 hb_font_get_glyph_v_advance_nil (hb_font_t *font HB_UNUSED,
81 void *font_data HB_UNUSED,
83 hb_position_t *x_advance,
84 hb_position_t *y_advance,
85 void *user_data HB_UNUSED)
88 hb_bool_t ret = hb_font_get_glyph_v_advance (font->parent,
90 x_advance, y_advance);
91 font->parent_scale_distance (x_advance, y_advance);
95 *x_advance = *y_advance = 0;
100 hb_font_get_glyph_v_origin_nil (hb_font_t *font HB_UNUSED,
101 void *font_data HB_UNUSED,
102 hb_codepoint_t glyph,
103 hb_position_t *x_origin,
104 hb_position_t *y_origin,
105 void *user_data HB_UNUSED)
108 hb_bool_t ret = hb_font_get_glyph_v_origin (font->parent,
111 font->parent_scale_distance (x_origin, y_origin);
115 *x_origin = *y_origin = 0;
120 hb_font_get_h_kerning_nil (hb_font_t *font HB_UNUSED,
121 void *font_data HB_UNUSED,
122 hb_codepoint_t left_glyph,
123 hb_codepoint_t right_glyph,
124 hb_position_t *x_kern,
125 hb_position_t *y_kern,
126 void *user_data HB_UNUSED)
129 hb_bool_t ret = hb_font_get_h_kerning (font->parent,
130 left_glyph, right_glyph,
132 font->parent_scale_distance (x_kern, y_kern);
136 *x_kern = *y_kern = 0;
141 hb_font_get_v_kerning_nil (hb_font_t *font HB_UNUSED,
142 void *font_data HB_UNUSED,
143 hb_codepoint_t top_glyph,
144 hb_codepoint_t bottom_glyph,
145 hb_position_t *x_kern,
146 hb_position_t *y_kern,
147 void *user_data HB_UNUSED)
150 hb_bool_t ret = hb_font_get_v_kerning (font->parent,
151 top_glyph, bottom_glyph,
153 font->parent_scale_distance (x_kern, y_kern);
157 *x_kern = *y_kern = 0;
162 hb_font_get_glyph_extents_nil (hb_font_t *font HB_UNUSED,
163 void *font_data HB_UNUSED,
164 hb_codepoint_t glyph,
166 hb_glyph_extents_t *extents,
167 void *user_data HB_UNUSED)
170 hb_bool_t ret = hb_font_get_glyph_extents (font->parent,
174 font->parent_scale_position (&extents->x_bearing, &extents->y_bearing);
175 font->parent_scale_distance (&extents->width, &extents->height);
179 extents->x_bearing = extents->y_bearing = 0;
180 extents->width = extents->height = 0;
185 hb_font_get_contour_point_nil (hb_font_t *font HB_UNUSED,
186 void *font_data HB_UNUSED,
187 hb_codepoint_t glyph,
188 unsigned int point_index,
192 void *user_data HB_UNUSED)
195 hb_bool_t ret = hb_font_get_contour_point (font->parent,
199 font->parent_scale_position (x, y);
208 static hb_font_funcs_t _hb_font_funcs_nil = {
209 HB_OBJECT_HEADER_STATIC,
211 TRUE, /* immutable */
214 #define HB_FONT_FUNC_IMPLEMENT(name) hb_font_get_##name##_nil,
215 HB_FONT_FUNCS_IMPLEMENT_CALLBACKS
216 #undef HB_FONT_FUNC_IMPLEMENT
222 hb_font_funcs_create (void)
224 hb_font_funcs_t *ffuncs;
226 if (!(ffuncs = hb_object_create<hb_font_funcs_t> ()))
227 return &_hb_font_funcs_nil;
229 ffuncs->get = _hb_font_funcs_nil.get;
235 hb_font_funcs_get_empty (void)
237 return &_hb_font_funcs_nil;
241 hb_font_funcs_reference (hb_font_funcs_t *ffuncs)
243 return hb_object_reference (ffuncs);
247 hb_font_funcs_destroy (hb_font_funcs_t *ffuncs)
249 if (!hb_object_destroy (ffuncs)) return;
251 #define HB_FONT_FUNC_IMPLEMENT(name) if (ffuncs->destroy.name) ffuncs->destroy.name (ffuncs->user_data.name);
252 HB_FONT_FUNCS_IMPLEMENT_CALLBACKS
253 #undef HB_FONT_FUNC_IMPLEMENT
259 hb_font_funcs_set_user_data (hb_font_funcs_t *ffuncs,
260 hb_user_data_key_t *key,
262 hb_destroy_func_t destroy)
264 return hb_object_set_user_data (ffuncs, key, data, destroy);
268 hb_font_funcs_get_user_data (hb_font_funcs_t *ffuncs,
269 hb_user_data_key_t *key)
271 return hb_object_get_user_data (ffuncs, key);
276 hb_font_funcs_make_immutable (hb_font_funcs_t *ffuncs)
278 if (hb_object_is_inert (ffuncs))
281 ffuncs->immutable = TRUE;
285 hb_font_funcs_is_immutable (hb_font_funcs_t *ffuncs)
287 return ffuncs->immutable;
291 #define HB_FONT_FUNC_IMPLEMENT(name) \
294 hb_font_funcs_set_##name##_func (hb_font_funcs_t *ffuncs, \
295 hb_font_get_##name##_func_t func, \
297 hb_destroy_func_t destroy) \
299 if (ffuncs->immutable) \
302 if (ffuncs->destroy.name) \
303 ffuncs->destroy.name (ffuncs->user_data.name); \
306 ffuncs->get.name = func; \
307 ffuncs->user_data.name = user_data; \
308 ffuncs->destroy.name = destroy; \
310 ffuncs->get.name = hb_font_get_##name##_nil; \
311 ffuncs->user_data.name = NULL; \
312 ffuncs->destroy.name = NULL; \
316 HB_FONT_FUNCS_IMPLEMENT_CALLBACKS
317 #undef HB_FONT_FUNC_IMPLEMENT
321 hb_font_get_glyph (hb_font_t *font,
322 hb_codepoint_t unicode, hb_codepoint_t variation_selector,
323 hb_codepoint_t *glyph)
326 return font->klass->get.glyph (font, font->user_data,
327 unicode, variation_selector, glyph,
328 font->klass->user_data.glyph);
332 hb_font_get_glyph_h_advance (hb_font_t *font,
333 hb_codepoint_t glyph,
334 hb_position_t *x_advance, hb_position_t *y_advance)
336 *x_advance = *y_advance = 0;
337 return font->klass->get.glyph_h_advance (font, font->user_data,
338 glyph, x_advance, y_advance,
339 font->klass->user_data.glyph_h_advance);
343 hb_font_get_glyph_v_advance (hb_font_t *font,
344 hb_codepoint_t glyph,
345 hb_position_t *x_advance, hb_position_t *y_advance)
347 *x_advance = *y_advance = 0;
348 return font->klass->get.glyph_v_advance (font, font->user_data,
349 glyph, x_advance, y_advance,
350 font->klass->user_data.glyph_v_advance);
354 hb_font_get_glyph_v_origin (hb_font_t *font,
355 hb_codepoint_t glyph,
356 hb_position_t *x_origin, hb_position_t *y_origin)
358 *x_origin = *y_origin = 0;
359 return font->klass->get.glyph_v_origin (font, font->user_data,
360 glyph, x_origin, y_origin,
361 font->klass->user_data.glyph_v_origin);
365 hb_font_get_h_kerning (hb_font_t *font,
366 hb_codepoint_t left_glyph, hb_codepoint_t right_glyph,
367 hb_position_t *x_kern, hb_position_t *y_kern)
369 *x_kern = *y_kern = 0;
370 return font->klass->get.h_kerning (font, font->user_data,
371 left_glyph, right_glyph,
373 font->klass->user_data.h_kerning);
377 hb_font_get_v_kerning (hb_font_t *font,
378 hb_codepoint_t left_glyph, hb_codepoint_t right_glyph,
379 hb_position_t *x_kern, hb_position_t *y_kern)
381 *x_kern = *y_kern = 0;
382 return font->klass->get.v_kerning (font, font->user_data,
383 left_glyph, right_glyph,
385 font->klass->user_data.v_kerning);
389 hb_font_get_glyph_extents (hb_font_t *font,
390 hb_codepoint_t glyph,
392 hb_glyph_extents_t *extents)
394 memset (extents, 0, sizeof (*extents));
395 return font->klass->get.glyph_extents (font, font->user_data,
399 font->klass->user_data.glyph_extents);
403 hb_font_get_contour_point (hb_font_t *font,
404 hb_codepoint_t glyph, unsigned int point_index,
406 hb_position_t *x, hb_position_t *y)
409 return font->klass->get.contour_point (font, font->user_data,
413 font->klass->user_data.contour_point);
417 /* A bit higher-level, and with fallback */
420 hb_font_get_glyph_advance_for_direction (hb_font_t *font,
421 hb_codepoint_t glyph,
422 hb_direction_t direction,
423 hb_position_t *x_advance, hb_position_t *y_advance)
425 if (HB_DIRECTION_IS_VERTICAL (direction)) {
426 hb_bool_t ret = hb_font_get_glyph_v_advance (font, glyph, x_advance, y_advance);
428 /* TODO Simulate using h_advance and font_extents */
431 hb_font_get_glyph_h_advance (font, glyph, x_advance, y_advance);
436 hb_font_get_kerning_for_direction (hb_font_t *font,
437 hb_codepoint_t first_glyph, hb_codepoint_t second_glyph,
438 hb_direction_t direction,
439 hb_position_t *x_kern, hb_position_t *y_kern)
442 case HB_DIRECTION_LTR:
443 case HB_DIRECTION_RTL:
444 hb_font_get_h_kerning (font, first_glyph, second_glyph, x_kern, y_kern);
447 case HB_DIRECTION_TTB:
448 case HB_DIRECTION_BTT:
449 hb_font_get_v_kerning (font, first_glyph, second_glyph, x_kern, y_kern);
452 case HB_DIRECTION_INVALID:
459 hb_font_get_glyph_extents_for_direction (hb_font_t *font,
460 hb_codepoint_t glyph,
461 hb_direction_t direction,
462 hb_glyph_extents_t *extents)
464 hb_bool_t vertical = HB_DIRECTION_IS_VERTICAL (direction);
465 hb_bool_t ret = hb_font_get_glyph_extents (font, glyph, &vertical, extents);
468 if (vertical != HB_DIRECTION_IS_VERTICAL (direction)) {
469 /* XXX Adjust origin */
472 /* TODO Simulate using get_h_advance and font_extents? */
477 hb_font_get_contour_point_for_direction (hb_font_t *font,
478 hb_codepoint_t glyph, unsigned int point_index,
479 hb_direction_t direction,
480 hb_position_t *x, hb_position_t *y)
482 hb_bool_t vertical = HB_DIRECTION_IS_VERTICAL (direction);
483 hb_bool_t ret = hb_font_get_contour_point (font, glyph, point_index, &vertical, x, y);
485 if (ret && vertical != HB_DIRECTION_IS_VERTICAL (direction)) {
486 /* XXX Adjust origin */
497 static hb_face_t _hb_face_nil = {
498 HB_OBJECT_HEADER_STATIC,
500 TRUE, /* immutable */
502 NULL, /* get_table */
503 NULL, /* user_data */
506 NULL, /* ot_layout */
513 hb_face_create_for_tables (hb_get_table_func_t get_table,
515 hb_destroy_func_t destroy)
519 if (!get_table || !(face = hb_object_create<hb_face_t> ())) {
522 return &_hb_face_nil;
525 face->get_table = get_table;
526 face->user_data = user_data;
527 face->destroy = destroy;
529 face->ot_layout = _hb_ot_layout_create (face);
531 face->upem = _hb_ot_layout_get_upem (face);
537 typedef struct _hb_face_for_data_closure_t {
540 } hb_face_for_data_closure_t;
542 static hb_face_for_data_closure_t *
543 _hb_face_for_data_closure_create (hb_blob_t *blob, unsigned int index)
545 hb_face_for_data_closure_t *closure;
547 closure = (hb_face_for_data_closure_t *) malloc (sizeof (hb_face_for_data_closure_t));
548 if (unlikely (!closure))
551 closure->blob = blob;
552 closure->index = index;
558 _hb_face_for_data_closure_destroy (hb_face_for_data_closure_t *closure)
560 hb_blob_destroy (closure->blob);
565 _hb_face_for_data_get_table (hb_face_t *face HB_UNUSED, hb_tag_t tag, void *user_data)
567 hb_face_for_data_closure_t *data = (hb_face_for_data_closure_t *) user_data;
569 const OpenTypeFontFile &ot_file = *Sanitizer<OpenTypeFontFile>::lock_instance (data->blob);
570 const OpenTypeFontFace &ot_face = ot_file.get_face (data->index);
572 const OpenTypeTable &table = ot_face.get_table_by_tag (tag);
574 hb_blob_t *blob = hb_blob_create_sub_blob (data->blob, table.offset, table.length);
580 hb_face_create (hb_blob_t *blob,
583 if (unlikely (!blob || !hb_blob_get_length (blob)))
584 return &_hb_face_nil;
586 hb_face_for_data_closure_t *closure = _hb_face_for_data_closure_create (Sanitizer<OpenTypeFontFile>::sanitize (hb_blob_reference (blob)), index);
588 if (unlikely (!closure))
589 return &_hb_face_nil;
591 return hb_face_create_for_tables (_hb_face_for_data_get_table,
593 (hb_destroy_func_t) _hb_face_for_data_closure_destroy);
597 hb_face_get_empty (void)
599 return &_hb_face_nil;
604 hb_face_reference (hb_face_t *face)
606 return hb_object_reference (face);
610 hb_face_destroy (hb_face_t *face)
612 if (!hb_object_destroy (face)) return;
614 _hb_ot_layout_destroy (face->ot_layout);
617 face->destroy (face->user_data);
623 hb_face_set_user_data (hb_face_t *face,
624 hb_user_data_key_t *key,
626 hb_destroy_func_t destroy)
628 return hb_object_set_user_data (face, key, data, destroy);
632 hb_face_get_user_data (hb_face_t *face,
633 hb_user_data_key_t *key)
635 return hb_object_get_user_data (face, key);
639 hb_face_make_immutable (hb_face_t *face)
641 if (hb_object_is_inert (face))
644 face->immutable = true;
648 hb_face_is_immutable (hb_face_t *face)
650 return face->immutable;
655 hb_face_reference_table (hb_face_t *face,
660 if (unlikely (!face || !face->get_table))
661 return hb_blob_get_empty ();
663 blob = face->get_table (face, tag, face->user_data);
664 if (unlikely (!blob))
665 return hb_blob_get_empty ();
671 hb_face_get_upem (hb_face_t *face)
673 return _hb_ot_layout_get_upem (face);
681 static hb_font_t _hb_font_nil = {
682 HB_OBJECT_HEADER_STATIC,
684 TRUE, /* immutable */
695 &_hb_font_funcs_nil, /* klass */
696 NULL, /* user_data */
701 hb_font_create (hb_face_t *face)
705 if (unlikely (!face))
706 face = &_hb_face_nil;
707 if (unlikely (hb_object_is_inert (face)))
708 return &_hb_font_nil;
709 if (!(font = hb_object_create<hb_font_t> ()))
710 return &_hb_font_nil;
712 hb_face_make_immutable (face);
713 font->face = hb_face_reference (face);
714 font->klass = &_hb_font_funcs_nil;
720 hb_font_create_sub_font (hb_font_t *parent)
722 if (unlikely (!parent))
723 return &_hb_font_nil;
725 hb_font_t *font = hb_font_create (parent->face);
727 if (unlikely (hb_object_is_inert (font)))
730 hb_font_make_immutable (parent);
731 font->parent = hb_font_reference (parent);
733 font->x_scale = parent->x_scale;
734 font->y_scale = parent->y_scale;
735 font->x_ppem = parent->x_ppem;
736 font->y_ppem = parent->y_ppem;
738 font->klass = &_hb_font_funcs_nil;
744 hb_font_get_empty (void)
746 return &_hb_font_nil;
750 hb_font_reference (hb_font_t *font)
752 return hb_object_reference (font);
756 hb_font_destroy (hb_font_t *font)
758 if (!hb_object_destroy (font)) return;
760 hb_font_destroy (font->parent);
761 hb_face_destroy (font->face);
762 hb_font_funcs_destroy (font->klass);
764 font->destroy (font->user_data);
770 hb_font_set_user_data (hb_font_t *font,
771 hb_user_data_key_t *key,
773 hb_destroy_func_t destroy)
775 return hb_object_set_user_data (font, key, data, destroy);
779 hb_font_get_user_data (hb_font_t *font,
780 hb_user_data_key_t *key)
782 return hb_object_get_user_data (font, key);
786 hb_font_make_immutable (hb_font_t *font)
788 if (hb_object_is_inert (font))
791 font->immutable = true;
795 hb_font_is_immutable (hb_font_t *font)
797 return font->immutable;
801 hb_font_get_parent (hb_font_t *font)
807 hb_font_get_face (hb_font_t *font)
814 hb_font_set_funcs (hb_font_t *font,
815 hb_font_funcs_t *klass,
817 hb_destroy_func_t destroy)
823 font->destroy (font->user_data);
826 klass = &_hb_font_funcs_nil;
828 hb_font_funcs_reference (klass);
829 hb_font_funcs_destroy (font->klass);
831 font->user_data = user_data;
832 font->destroy = destroy;
837 hb_font_set_scale (hb_font_t *font,
844 font->x_scale = x_scale;
845 font->y_scale = y_scale;
849 hb_font_get_scale (hb_font_t *font,
853 if (x_scale) *x_scale = font->x_scale;
854 if (y_scale) *y_scale = font->y_scale;
858 hb_font_set_ppem (hb_font_t *font,
865 font->x_ppem = x_ppem;
866 font->y_ppem = y_ppem;
870 hb_font_get_ppem (hb_font_t *font,
871 unsigned int *x_ppem,
872 unsigned int *y_ppem)
874 if (x_ppem) *x_ppem = font->x_ppem;
875 if (y_ppem) *y_ppem = font->y_ppem;