2 * Copyright (C) 2009 Red Hat, Inc.
4 * This is part of HarfBuzz, an OpenType Layout engine 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-open-file-private.hh"
33 #include "hb-ot-layout-private.h"
43 hb_font_get_glyph_nil (hb_font_t *font, hb_face_t *face, const void *user_data,
44 hb_codepoint_t unicode, hb_codepoint_t variation_selector)
48 hb_font_get_contour_point_nil (hb_font_t *font, hb_face_t *face, const void *user_data,
49 unsigned int point_index,
50 hb_codepoint_t glyph, hb_position_t *x, hb_position_t *y)
54 hb_font_get_glyph_metrics_nil (hb_font_t *font, hb_face_t *face, const void *user_data,
55 hb_codepoint_t glyph, hb_glyph_metrics_t *metrics)
56 { memset (metrics, 0, sizeof (*metrics)); }
59 hb_font_get_kerning_nil (hb_font_t *font, hb_face_t *face, const void *user_data,
60 hb_codepoint_t first_glyph, hb_codepoint_t second_glyph)
63 hb_font_funcs_t _hb_font_funcs_nil = {
64 HB_REFERENCE_COUNT_INVALID, /* ref_count */
68 hb_font_get_glyph_nil,
69 hb_font_get_contour_point_nil,
70 hb_font_get_glyph_metrics_nil,
71 hb_font_get_kerning_nil
75 hb_font_funcs_create (void)
77 hb_font_funcs_t *ffuncs;
79 if (!HB_OBJECT_DO_CREATE (hb_font_funcs_t, ffuncs))
80 return &_hb_font_funcs_nil;
86 hb_font_funcs_reference (hb_font_funcs_t *ffuncs)
88 HB_OBJECT_DO_REFERENCE (ffuncs);
92 hb_font_funcs_get_reference_count (hb_font_funcs_t *ffuncs)
94 HB_OBJECT_DO_GET_REFERENCE_COUNT (ffuncs);
98 hb_font_funcs_destroy (hb_font_funcs_t *ffuncs)
100 HB_OBJECT_DO_DESTROY (ffuncs);
106 hb_font_funcs_copy (hb_font_funcs_t *other_ffuncs)
108 hb_font_funcs_t *ffuncs;
110 if (!HB_OBJECT_DO_CREATE (hb_font_funcs_t, ffuncs))
111 return &_hb_font_funcs_nil;
113 *ffuncs = *other_ffuncs;
115 /* re-init refcount */
116 HB_OBJECT_DO_INIT (ffuncs);
117 ffuncs->immutable = FALSE;
123 hb_font_funcs_make_immutable (hb_font_funcs_t *ffuncs)
125 if (HB_OBJECT_IS_INERT (ffuncs))
128 ffuncs->immutable = TRUE;
133 hb_font_funcs_set_glyph_func (hb_font_funcs_t *ffuncs,
134 hb_font_get_glyph_func_t glyph_func)
136 if (ffuncs->immutable)
139 ffuncs->get_glyph = glyph_func ? glyph_func : hb_font_get_glyph_nil;
143 hb_font_funcs_set_contour_point_func (hb_font_funcs_t *ffuncs,
144 hb_font_get_contour_point_func_t contour_point_func)
146 if (ffuncs->immutable)
149 ffuncs->get_contour_point = contour_point_func ? contour_point_func : hb_font_get_contour_point_nil;
153 hb_font_funcs_set_glyph_metrics_func (hb_font_funcs_t *ffuncs,
154 hb_font_get_glyph_metrics_func_t glyph_metrics_func)
156 if (ffuncs->immutable)
159 ffuncs->get_glyph_metrics = glyph_metrics_func ? glyph_metrics_func : hb_font_get_glyph_metrics_nil;
163 hb_font_funcs_set_kerning_func (hb_font_funcs_t *ffuncs,
164 hb_font_get_kerning_func_t kerning_func)
166 if (ffuncs->immutable)
169 ffuncs->get_kerning = kerning_func ? kerning_func : hb_font_get_kerning_nil;
174 hb_font_get_glyph (hb_font_t *font, hb_face_t *face,
175 hb_codepoint_t unicode, hb_codepoint_t variation_selector)
177 return font->klass->get_glyph (font, face, font->user_data,
178 unicode, variation_selector);
182 hb_font_get_contour_point (hb_font_t *font, hb_face_t *face,
183 unsigned int point_index,
184 hb_codepoint_t glyph, hb_position_t *x, hb_position_t *y)
186 return font->klass->get_contour_point (font, face, font->user_data,
192 hb_font_get_glyph_metrics (hb_font_t *font, hb_face_t *face,
193 hb_codepoint_t glyph, hb_glyph_metrics_t *metrics)
195 /* TODO Zero metrics here? */
196 return font->klass->get_glyph_metrics (font, face, font->user_data,
201 hb_font_get_kerning (hb_font_t *font, hb_face_t *face,
202 hb_codepoint_t first_glyph, hb_codepoint_t second_glyph)
204 return font->klass->get_kerning (font, face, font->user_data,
205 first_glyph, second_glyph);
213 _hb_face_get_table_from_blob (hb_tag_t tag, void *user_data)
215 hb_face_t *face = (hb_face_t *) user_data;
217 const OpenTypeFontFile &ot_file = Sanitizer<OpenTypeFontFile>::lock_instance (face->blob);
218 const OpenTypeFontFace &ot_face = ot_file.get_face (face->index);
220 const OpenTypeTable &table = ot_face.get_table_by_tag (tag);
222 hb_blob_t *blob = hb_blob_create_sub_blob (face->blob, table.offset, table.length);
224 hb_blob_unlock (face->blob);
229 static hb_face_t _hb_face_nil = {
230 HB_REFERENCE_COUNT_INVALID, /* ref_count */
235 NULL, /* get_table */
237 NULL, /* user_data */
243 hb_face_create_for_tables (hb_get_table_func_t get_table,
244 hb_destroy_func_t destroy,
249 if (!HB_OBJECT_DO_CREATE (hb_face_t, face)) {
252 return &_hb_face_nil;
255 face->get_table = get_table;
256 face->destroy = destroy;
257 face->user_data = user_data;
259 _hb_ot_layout_init (face);
265 hb_face_create_for_data (hb_blob_t *blob,
270 if (!HB_OBJECT_DO_CREATE (hb_face_t, face))
271 return &_hb_face_nil;
273 face->blob = Sanitizer<OpenTypeFontFile>::sanitize (hb_blob_reference (blob));
275 face->get_table = _hb_face_get_table_from_blob;
276 face->user_data = face;
278 _hb_ot_layout_init (face);
284 hb_face_reference (hb_face_t *face)
286 HB_OBJECT_DO_REFERENCE (face);
290 hb_face_get_reference_count (hb_face_t *face)
292 HB_OBJECT_DO_GET_REFERENCE_COUNT (face);
296 hb_face_destroy (hb_face_t *face)
298 HB_OBJECT_DO_DESTROY (face);
300 _hb_ot_layout_fini (face);
302 hb_blob_destroy (face->blob);
305 face->destroy (face->user_data);
311 hb_face_get_table (hb_face_t *face,
314 if (HB_UNLIKELY (!face || !face->get_table))
315 return hb_blob_create_empty ();
317 return face->get_table (tag, face->user_data);
325 static hb_font_t _hb_font_nil = {
326 HB_REFERENCE_COUNT_INVALID, /* ref_count */
340 hb_font_create (void)
344 if (!HB_OBJECT_DO_CREATE (hb_font_t, font))
345 return &_hb_font_nil;
351 hb_font_reference (hb_font_t *font)
353 HB_OBJECT_DO_REFERENCE (font);
357 hb_font_get_reference_count (hb_font_t *font)
359 HB_OBJECT_DO_GET_REFERENCE_COUNT (font);
363 hb_font_destroy (hb_font_t *font)
365 HB_OBJECT_DO_DESTROY (font);
367 hb_font_funcs_destroy (font->klass);
369 font->destroy (font->user_data);
375 hb_font_set_funcs (hb_font_t *font,
376 hb_font_funcs_t *klass,
377 hb_destroy_func_t destroy,
380 if (HB_OBJECT_IS_INERT (font))
384 font->destroy (font->user_data);
386 hb_font_funcs_reference (klass);
387 hb_font_funcs_destroy (font->klass);
389 font->destroy = destroy;
390 font->user_data = user_data;
394 hb_font_set_scale (hb_font_t *font,
395 hb_16dot16_t x_scale,
396 hb_16dot16_t y_scale)
398 if (HB_OBJECT_IS_INERT (font))
401 font->x_scale = x_scale;
402 font->y_scale = y_scale;
406 hb_font_set_ppem (hb_font_t *font,
410 if (HB_OBJECT_IS_INERT (font))
413 font->x_ppem = x_ppem;
414 font->y_ppem = y_ppem;