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.hh"
30 #include "hb-blob-private.h"
31 #include "hb-open-file-private.hh"
33 #include "hb-ot-layout-private.hh"
43 hb_font_get_glyph_nil (hb_font_t *font HB_UNUSED,
44 hb_face_t *face HB_UNUSED,
45 const void *user_data HB_UNUSED,
46 hb_codepoint_t unicode HB_UNUSED,
47 hb_codepoint_t variation_selector HB_UNUSED)
51 hb_font_get_contour_point_nil (hb_font_t *font HB_UNUSED,
52 hb_face_t *face HB_UNUSED,
53 const void *user_data HB_UNUSED,
54 unsigned int point_index HB_UNUSED,
55 hb_codepoint_t glyph HB_UNUSED,
56 hb_position_t *x HB_UNUSED,
57 hb_position_t *y HB_UNUSED)
61 hb_font_get_glyph_metrics_nil (hb_font_t *font HB_UNUSED,
62 hb_face_t *face HB_UNUSED,
63 const void *user_data HB_UNUSED,
64 hb_codepoint_t glyph HB_UNUSED,
65 hb_glyph_metrics_t *metrics HB_UNUSED)
69 hb_font_get_kerning_nil (hb_font_t *font HB_UNUSED,
70 hb_face_t *face HB_UNUSED,
71 const void *user_data HB_UNUSED,
72 hb_codepoint_t first_glyph HB_UNUSED,
73 hb_codepoint_t second_glyph HB_UNUSED)
76 hb_font_funcs_t _hb_font_funcs_nil = {
77 HB_REFERENCE_COUNT_INVALID, /* ref_count */
81 hb_font_get_glyph_nil,
82 hb_font_get_contour_point_nil,
83 hb_font_get_glyph_metrics_nil,
84 hb_font_get_kerning_nil
88 hb_font_funcs_create (void)
90 hb_font_funcs_t *ffuncs;
92 if (!HB_OBJECT_DO_CREATE (hb_font_funcs_t, ffuncs))
93 return &_hb_font_funcs_nil;
99 hb_font_funcs_reference (hb_font_funcs_t *ffuncs)
101 HB_OBJECT_DO_REFERENCE (ffuncs);
105 hb_font_funcs_get_reference_count (hb_font_funcs_t *ffuncs)
107 HB_OBJECT_DO_GET_REFERENCE_COUNT (ffuncs);
111 hb_font_funcs_destroy (hb_font_funcs_t *ffuncs)
113 HB_OBJECT_DO_DESTROY (ffuncs);
119 hb_font_funcs_copy (hb_font_funcs_t *other_ffuncs)
121 hb_font_funcs_t *ffuncs;
123 if (!HB_OBJECT_DO_CREATE (hb_font_funcs_t, ffuncs))
124 return &_hb_font_funcs_nil;
126 *ffuncs = *other_ffuncs;
128 /* re-init refcount */
129 HB_OBJECT_DO_INIT (ffuncs);
130 ffuncs->immutable = FALSE;
136 hb_font_funcs_make_immutable (hb_font_funcs_t *ffuncs)
138 if (HB_OBJECT_IS_INERT (ffuncs))
141 ffuncs->immutable = TRUE;
146 hb_font_funcs_set_glyph_func (hb_font_funcs_t *ffuncs,
147 hb_font_get_glyph_func_t glyph_func)
149 if (ffuncs->immutable)
152 ffuncs->get_glyph = glyph_func ? glyph_func : hb_font_get_glyph_nil;
156 hb_font_funcs_set_contour_point_func (hb_font_funcs_t *ffuncs,
157 hb_font_get_contour_point_func_t contour_point_func)
159 if (ffuncs->immutable)
162 ffuncs->get_contour_point = contour_point_func ? contour_point_func : hb_font_get_contour_point_nil;
166 hb_font_funcs_set_glyph_metrics_func (hb_font_funcs_t *ffuncs,
167 hb_font_get_glyph_metrics_func_t glyph_metrics_func)
169 if (ffuncs->immutable)
172 ffuncs->get_glyph_metrics = glyph_metrics_func ? glyph_metrics_func : hb_font_get_glyph_metrics_nil;
176 hb_font_funcs_set_kerning_func (hb_font_funcs_t *ffuncs,
177 hb_font_get_kerning_func_t kerning_func)
179 if (ffuncs->immutable)
182 ffuncs->get_kerning = kerning_func ? kerning_func : hb_font_get_kerning_nil;
187 hb_font_get_glyph (hb_font_t *font, hb_face_t *face,
188 hb_codepoint_t unicode, hb_codepoint_t variation_selector)
190 return font->klass->get_glyph (font, face, font->user_data,
191 unicode, variation_selector);
195 hb_font_get_contour_point (hb_font_t *font, hb_face_t *face,
196 unsigned int point_index,
197 hb_codepoint_t glyph, hb_position_t *x, hb_position_t *y)
200 return font->klass->get_contour_point (font, face, font->user_data,
206 hb_font_get_glyph_metrics (hb_font_t *font, hb_face_t *face,
207 hb_codepoint_t glyph, hb_glyph_metrics_t *metrics)
209 memset (metrics, 0, sizeof (*metrics));
210 return font->klass->get_glyph_metrics (font, face, font->user_data,
215 hb_font_get_kerning (hb_font_t *font, hb_face_t *face,
216 hb_codepoint_t first_glyph, hb_codepoint_t second_glyph)
218 return font->klass->get_kerning (font, face, font->user_data,
219 first_glyph, second_glyph);
227 static hb_face_t _hb_face_nil = {
228 HB_REFERENCE_COUNT_INVALID, /* ref_count */
230 NULL, /* get_table */
232 NULL, /* user_data */
234 NULL, /* head_blob */
235 NULL, /* head_table */
242 hb_face_create_for_tables (hb_get_table_func_t get_table,
243 hb_destroy_func_t destroy,
248 if (!HB_OBJECT_DO_CREATE (hb_face_t, face)) {
251 return &_hb_face_nil;
254 face->get_table = get_table;
255 face->destroy = destroy;
256 face->user_data = user_data;
258 _hb_ot_layout_init (face);
264 typedef struct _hb_face_for_data_closure_t {
267 } hb_face_for_data_closure_t;
269 static hb_face_for_data_closure_t _hb_face_for_data_closure_nil = {
274 static hb_face_for_data_closure_t *
275 _hb_face_for_data_closure_create (hb_blob_t *blob, unsigned int index)
277 hb_face_for_data_closure_t *closure;
279 closure = (hb_face_for_data_closure_t *) malloc (sizeof (hb_face_for_data_closure_t));
280 if (unlikely (!closure))
281 return &_hb_face_for_data_closure_nil;
283 closure->blob = hb_blob_reference (blob);
284 closure->index = index;
290 _hb_face_for_data_closure_destroy (hb_face_for_data_closure_t *closure)
292 if (likely (closure != &_hb_face_for_data_closure_nil)) {
293 hb_blob_destroy (closure->blob);
299 _hb_face_for_data_get_table (hb_tag_t tag, void *user_data)
301 hb_face_for_data_closure_t *data = (hb_face_for_data_closure_t *) user_data;
303 const OpenTypeFontFile &ot_file = *Sanitizer<OpenTypeFontFile>::lock_instance (data->blob);
304 const OpenTypeFontFace &ot_face = ot_file.get_face (data->index);
306 const OpenTypeTable &table = ot_face.get_table_by_tag (tag);
308 hb_blob_t *blob = hb_blob_create_sub_blob (data->blob, table.offset, table.length);
310 hb_blob_unlock (data->blob);
316 hb_face_create_for_data (hb_blob_t *blob,
321 if (!HB_OBJECT_DO_CREATE (hb_face_t, face))
322 return &_hb_face_nil;
324 face->get_table = _hb_face_for_data_get_table;
325 face->destroy = (hb_destroy_func_t) _hb_face_for_data_closure_destroy;
326 hb_blob_reference (blob);
327 face->user_data = _hb_face_for_data_closure_create (Sanitizer<OpenTypeFontFile>::sanitize (blob), index);
328 hb_blob_destroy (blob);
330 face->head_blob = Sanitizer<head>::sanitize (hb_face_get_table (face, HB_OT_TAG_head));
331 face->head_table = Sanitizer<head>::lock_instance (face->head_blob);
333 _hb_ot_layout_init (face);
340 hb_face_reference (hb_face_t *face)
342 HB_OBJECT_DO_REFERENCE (face);
346 hb_face_get_reference_count (hb_face_t *face)
348 HB_OBJECT_DO_GET_REFERENCE_COUNT (face);
352 hb_face_destroy (hb_face_t *face)
354 HB_OBJECT_DO_DESTROY (face);
356 _hb_ot_layout_fini (face);
358 hb_blob_unlock (face->head_blob);
359 hb_blob_destroy (face->head_blob);
362 face->destroy (face->user_data);
368 hb_face_get_table (hb_face_t *face,
373 if (unlikely (!face || !face->get_table))
374 return &_hb_blob_nil;
376 blob = face->get_table (tag, face->user_data);
378 return blob? blob : &_hb_blob_nil;
386 static hb_font_t _hb_font_nil = {
387 HB_REFERENCE_COUNT_INVALID, /* ref_count */
401 hb_font_create (void)
405 if (!HB_OBJECT_DO_CREATE (hb_font_t, font))
406 return &_hb_font_nil;
408 font->klass = &_hb_font_funcs_nil;
414 hb_font_reference (hb_font_t *font)
416 HB_OBJECT_DO_REFERENCE (font);
420 hb_font_get_reference_count (hb_font_t *font)
422 HB_OBJECT_DO_GET_REFERENCE_COUNT (font);
426 hb_font_destroy (hb_font_t *font)
428 HB_OBJECT_DO_DESTROY (font);
430 hb_font_funcs_destroy (font->klass);
432 font->destroy (font->user_data);
438 hb_font_set_funcs (hb_font_t *font,
439 hb_font_funcs_t *klass,
440 hb_destroy_func_t destroy,
443 if (HB_OBJECT_IS_INERT (font))
447 font->destroy (font->user_data);
450 klass = &_hb_font_funcs_nil;
452 hb_font_funcs_reference (klass);
453 hb_font_funcs_destroy (font->klass);
455 font->destroy = destroy;
456 font->user_data = user_data;
460 hb_font_set_scale (hb_font_t *font,
461 hb_16dot16_t x_scale,
462 hb_16dot16_t y_scale)
464 if (HB_OBJECT_IS_INERT (font))
467 font->x_scale = x_scale;
468 font->y_scale = y_scale;
472 hb_font_set_ppem (hb_font_t *font,
476 if (HB_OBJECT_IS_INERT (font))
479 font->x_ppem = x_ppem;
480 font->y_ppem = y_ppem;