2 * Copyright (C) 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.h"
29 #include "hb-font-private.h"
30 #include "hb-blob-private.h"
31 #include "hb-open-file-private.hh"
33 #include "hb-ot-layout-private.hh"
45 hb_font_get_glyph_nil (hb_font_t *font HB_UNUSED,
46 hb_face_t *face HB_UNUSED,
47 const void *user_data HB_UNUSED,
48 hb_codepoint_t unicode HB_UNUSED,
49 hb_codepoint_t variation_selector HB_UNUSED)
53 hb_font_get_contour_point_nil (hb_font_t *font HB_UNUSED,
54 hb_face_t *face HB_UNUSED,
55 const void *user_data HB_UNUSED,
56 unsigned int point_index HB_UNUSED,
57 hb_codepoint_t glyph HB_UNUSED,
58 hb_position_t *x HB_UNUSED,
59 hb_position_t *y HB_UNUSED)
63 hb_font_get_glyph_metrics_nil (hb_font_t *font HB_UNUSED,
64 hb_face_t *face HB_UNUSED,
65 const void *user_data HB_UNUSED,
66 hb_codepoint_t glyph HB_UNUSED,
67 hb_glyph_metrics_t *metrics HB_UNUSED)
71 hb_font_get_kerning_nil (hb_font_t *font HB_UNUSED,
72 hb_face_t *face HB_UNUSED,
73 const void *user_data HB_UNUSED,
74 hb_codepoint_t first_glyph HB_UNUSED,
75 hb_codepoint_t second_glyph HB_UNUSED)
78 hb_font_funcs_t _hb_font_funcs_nil = {
79 HB_REFERENCE_COUNT_INVALID, /* ref_count */
82 hb_font_get_glyph_nil,
83 hb_font_get_contour_point_nil,
84 hb_font_get_glyph_metrics_nil,
85 hb_font_get_kerning_nil
90 hb_font_funcs_create (void)
92 hb_font_funcs_t *ffuncs;
94 if (!HB_OBJECT_DO_CREATE (hb_font_funcs_t, ffuncs))
95 return &_hb_font_funcs_nil;
97 ffuncs->v = _hb_font_funcs_nil.v;
103 hb_font_funcs_reference (hb_font_funcs_t *ffuncs)
105 HB_OBJECT_DO_REFERENCE (ffuncs);
109 hb_font_funcs_get_reference_count (hb_font_funcs_t *ffuncs)
111 HB_OBJECT_DO_GET_REFERENCE_COUNT (ffuncs);
115 hb_font_funcs_destroy (hb_font_funcs_t *ffuncs)
117 HB_OBJECT_DO_DESTROY (ffuncs);
123 hb_font_funcs_copy (hb_font_funcs_t *other_ffuncs)
125 hb_font_funcs_t *ffuncs;
127 if (!HB_OBJECT_DO_CREATE (hb_font_funcs_t, ffuncs))
128 return &_hb_font_funcs_nil;
130 ffuncs->v = other_ffuncs->v;
136 hb_font_funcs_make_immutable (hb_font_funcs_t *ffuncs)
138 if (HB_OBJECT_IS_INERT (ffuncs))
141 ffuncs->immutable = TRUE;
145 hb_font_funcs_is_immutable (hb_font_funcs_t *ffuncs)
147 return ffuncs->immutable = TRUE;
152 hb_font_funcs_set_glyph_func (hb_font_funcs_t *ffuncs,
153 hb_font_get_glyph_func_t glyph_func)
155 if (ffuncs->immutable)
158 ffuncs->v.get_glyph = glyph_func ? glyph_func : hb_font_get_glyph_nil;
162 hb_font_funcs_set_contour_point_func (hb_font_funcs_t *ffuncs,
163 hb_font_get_contour_point_func_t contour_point_func)
165 if (ffuncs->immutable)
168 ffuncs->v.get_contour_point = contour_point_func ? contour_point_func : hb_font_get_contour_point_nil;
172 hb_font_funcs_set_glyph_metrics_func (hb_font_funcs_t *ffuncs,
173 hb_font_get_glyph_metrics_func_t glyph_metrics_func)
175 if (ffuncs->immutable)
178 ffuncs->v.get_glyph_metrics = glyph_metrics_func ? glyph_metrics_func : hb_font_get_glyph_metrics_nil;
182 hb_font_funcs_set_kerning_func (hb_font_funcs_t *ffuncs,
183 hb_font_get_kerning_func_t kerning_func)
185 if (ffuncs->immutable)
188 ffuncs->v.get_kerning = kerning_func ? kerning_func : hb_font_get_kerning_nil;
192 hb_font_get_glyph_func_t
193 hb_font_funcs_get_glyph_func (hb_font_funcs_t *ffuncs)
195 return ffuncs->v.get_glyph;
198 hb_font_get_contour_point_func_t
199 hb_font_funcs_get_contour_point_func (hb_font_funcs_t *ffuncs)
201 return ffuncs->v.get_contour_point;
204 hb_font_get_glyph_metrics_func_t
205 hb_font_funcs_get_glyph_metrics_func (hb_font_funcs_t *ffuncs)
207 return ffuncs->v.get_glyph_metrics;
210 hb_font_get_kerning_func_t
211 hb_font_funcs_get_kerning_func (hb_font_funcs_t *ffuncs)
213 return ffuncs->v.get_kerning;
219 hb_font_get_glyph (hb_font_t *font, hb_face_t *face,
220 hb_codepoint_t unicode, hb_codepoint_t variation_selector)
222 return font->klass->v.get_glyph (font, face, font->user_data,
223 unicode, variation_selector);
227 hb_font_get_contour_point (hb_font_t *font, hb_face_t *face,
228 unsigned int point_index,
229 hb_codepoint_t glyph, hb_position_t *x, hb_position_t *y)
232 return font->klass->v.get_contour_point (font, face, font->user_data,
238 hb_font_get_glyph_metrics (hb_font_t *font, hb_face_t *face,
239 hb_codepoint_t glyph, hb_glyph_metrics_t *metrics)
241 memset (metrics, 0, sizeof (*metrics));
242 return font->klass->v.get_glyph_metrics (font, face, font->user_data,
247 hb_font_get_kerning (hb_font_t *font, hb_face_t *face,
248 hb_codepoint_t first_glyph, hb_codepoint_t second_glyph)
250 return font->klass->v.get_kerning (font, face, font->user_data,
251 first_glyph, second_glyph);
259 static hb_face_t _hb_face_nil = {
260 HB_REFERENCE_COUNT_INVALID, /* ref_count */
262 NULL, /* get_table */
264 NULL, /* user_data */
266 NULL, /* head_blob */
267 NULL, /* head_table */
274 hb_face_create_for_tables (hb_get_table_func_t get_table,
275 hb_destroy_func_t destroy,
280 if (!HB_OBJECT_DO_CREATE (hb_face_t, face)) {
283 return &_hb_face_nil;
286 face->get_table = get_table;
287 face->destroy = destroy;
288 face->user_data = user_data;
290 face->ot_layout = _hb_ot_layout_new (face);
292 face->head_blob = Sanitizer<head>::sanitize (hb_face_get_table (face, HB_OT_TAG_head));
293 face->head_table = Sanitizer<head>::lock_instance (face->head_blob);
299 typedef struct _hb_face_for_data_closure_t {
302 } hb_face_for_data_closure_t;
304 static hb_face_for_data_closure_t *
305 _hb_face_for_data_closure_create (hb_blob_t *blob, unsigned int index)
307 hb_face_for_data_closure_t *closure;
309 closure = (hb_face_for_data_closure_t *) malloc (sizeof (hb_face_for_data_closure_t));
310 if (unlikely (!closure))
313 closure->blob = hb_blob_reference (blob);
314 closure->index = index;
320 _hb_face_for_data_closure_destroy (hb_face_for_data_closure_t *closure)
322 hb_blob_destroy (closure->blob);
327 _hb_face_for_data_get_table (hb_tag_t tag, void *user_data)
329 hb_face_for_data_closure_t *data = (hb_face_for_data_closure_t *) user_data;
331 const OpenTypeFontFile &ot_file = *Sanitizer<OpenTypeFontFile>::lock_instance (data->blob);
332 const OpenTypeFontFace &ot_face = ot_file.get_face (data->index);
334 const OpenTypeTable &table = ot_face.get_table_by_tag (tag);
336 hb_blob_t *blob = hb_blob_create_sub_blob (data->blob, table.offset, table.length);
338 hb_blob_unlock (data->blob);
344 hb_face_create_for_data (hb_blob_t *blob,
347 hb_face_for_data_closure_t *closure = _hb_face_for_data_closure_create (Sanitizer<OpenTypeFontFile>::sanitize (hb_blob_reference (blob)), index);
349 if (unlikely (!closure))
350 return &_hb_face_nil;
352 return hb_face_create_for_tables (_hb_face_for_data_get_table,
353 (hb_destroy_func_t) _hb_face_for_data_closure_destroy,
359 hb_face_reference (hb_face_t *face)
361 HB_OBJECT_DO_REFERENCE (face);
365 hb_face_get_reference_count (hb_face_t *face)
367 HB_OBJECT_DO_GET_REFERENCE_COUNT (face);
371 hb_face_destroy (hb_face_t *face)
373 HB_OBJECT_DO_DESTROY (face);
375 _hb_ot_layout_free (face->ot_layout);
377 hb_blob_unlock (face->head_blob);
378 hb_blob_destroy (face->head_blob);
381 face->destroy (face->user_data);
387 hb_face_get_table (hb_face_t *face,
392 if (unlikely (!face || !face->get_table))
393 return &_hb_blob_nil;
395 blob = face->get_table (tag, face->user_data);
401 hb_face_get_upem (hb_face_t *face)
403 return (face->head_table ? face->head_table : &Null(head))->get_upem ();
411 static hb_font_t _hb_font_nil = {
412 HB_REFERENCE_COUNT_INVALID, /* ref_count */
426 hb_font_create (void)
430 if (!HB_OBJECT_DO_CREATE (hb_font_t, font))
431 return &_hb_font_nil;
433 font->klass = &_hb_font_funcs_nil;
439 hb_font_reference (hb_font_t *font)
441 HB_OBJECT_DO_REFERENCE (font);
445 hb_font_get_reference_count (hb_font_t *font)
447 HB_OBJECT_DO_GET_REFERENCE_COUNT (font);
451 hb_font_destroy (hb_font_t *font)
453 HB_OBJECT_DO_DESTROY (font);
455 hb_font_funcs_destroy (font->klass);
457 font->destroy (font->user_data);
463 hb_font_set_funcs (hb_font_t *font,
464 hb_font_funcs_t *klass,
465 hb_destroy_func_t destroy,
468 if (HB_OBJECT_IS_INERT (font))
472 font->destroy (font->user_data);
475 klass = &_hb_font_funcs_nil;
477 hb_font_funcs_reference (klass);
478 hb_font_funcs_destroy (font->klass);
480 font->destroy = destroy;
481 font->user_data = user_data;
485 hb_font_unset_funcs (hb_font_t *font,
486 hb_font_funcs_t **klass,
487 hb_destroy_func_t *destroy,
490 /* None of the input arguments can be NULL. */
492 *klass = font->klass;
493 *destroy = font->destroy;
494 *user_data = font->user_data;
496 if (HB_OBJECT_IS_INERT (font))
500 font->destroy = NULL;
501 font->user_data = NULL;
505 hb_font_set_scale (hb_font_t *font,
506 unsigned int x_scale,
507 unsigned int y_scale)
509 if (HB_OBJECT_IS_INERT (font))
512 font->x_scale = x_scale;
513 font->y_scale = y_scale;
517 hb_font_get_scale (hb_font_t *font,
518 unsigned int *x_scale,
519 unsigned int *y_scale)
521 if (x_scale) *x_scale = font->x_scale;
522 if (y_scale) *y_scale = font->y_scale;
526 hb_font_set_ppem (hb_font_t *font,
530 if (HB_OBJECT_IS_INERT (font))
533 font->x_ppem = x_ppem;
534 font->y_ppem = y_ppem;
538 hb_font_get_ppem (hb_font_t *font,
539 unsigned int *x_ppem,
540 unsigned int *y_ppem)
542 if (x_ppem) *x_ppem = font->x_ppem;
543 if (y_ppem) *y_ppem = font->y_ppem;