hb-font.cc revision 3b59306b858d31d97be0ea8c64461de1d0d03572
1/*
2 * Copyright (C) 2009  Red Hat, Inc.
3 *
4 *  This is part of HarfBuzz, an OpenType Layout engine library.
5 *
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.
11 *
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
16 * DAMAGE.
17 *
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.
23 *
24 * Red Hat Author(s): Behdad Esfahbod
25 */
26
27#include "hb-private.h"
28
29#include "hb-font-private.h"
30#include "hb-open-file-private.hh"
31#include "hb-blob.h"
32
33#include "hb-ot-layout-private.h"
34
35#include <string.h>
36
37
38/*
39 * hb_font_funcs_t
40 */
41
42static hb_codepoint_t
43hb_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)
45{ return unicode; }
46
47static hb_bool_t
48hb_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)
51{ return false; }
52
53static void
54hb_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)); }
57
58static hb_position_t
59hb_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)
61{ return 0; }
62
63hb_font_funcs_t _hb_font_funcs_nil = {
64  HB_REFERENCE_COUNT_INVALID, /* ref_count */
65
66  TRUE,  /* immutable */
67
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
72};
73
74hb_font_funcs_t *
75hb_font_funcs_create (void)
76{
77  hb_font_funcs_t *ffuncs;
78
79  if (!HB_OBJECT_DO_CREATE (hb_font_funcs_t, ffuncs))
80    return &_hb_font_funcs_nil;
81
82  return ffuncs;
83}
84
85hb_font_funcs_t *
86hb_font_funcs_reference (hb_font_funcs_t *ffuncs)
87{
88  HB_OBJECT_DO_REFERENCE (ffuncs);
89}
90
91unsigned int
92hb_font_funcs_get_reference_count (hb_font_funcs_t *ffuncs)
93{
94  HB_OBJECT_DO_GET_REFERENCE_COUNT (ffuncs);
95}
96
97void
98hb_font_funcs_destroy (hb_font_funcs_t *ffuncs)
99{
100  HB_OBJECT_DO_DESTROY (ffuncs);
101
102  free (ffuncs);
103}
104
105hb_font_funcs_t *
106hb_font_funcs_copy (hb_font_funcs_t *other_ffuncs)
107{
108  hb_font_funcs_t *ffuncs;
109
110  if (!HB_OBJECT_DO_CREATE (hb_font_funcs_t, ffuncs))
111    return &_hb_font_funcs_nil;
112
113  *ffuncs = *other_ffuncs;
114
115  /* re-init refcount */
116  HB_OBJECT_DO_INIT (ffuncs);
117  ffuncs->immutable = FALSE;
118
119  return ffuncs;
120}
121
122void
123hb_font_funcs_make_immutable (hb_font_funcs_t *ffuncs)
124{
125  if (HB_OBJECT_IS_INERT (ffuncs))
126    return;
127
128  ffuncs->immutable = TRUE;
129}
130
131
132void
133hb_font_funcs_set_glyph_func (hb_font_funcs_t *ffuncs,
134			      hb_font_get_glyph_func_t glyph_func)
135{
136  if (ffuncs->immutable)
137    return;
138
139  ffuncs->get_glyph = glyph_func ? glyph_func : hb_font_get_glyph_nil;
140}
141
142void
143hb_font_funcs_set_contour_point_func (hb_font_funcs_t *ffuncs,
144				      hb_font_get_contour_point_func_t contour_point_func)
145{
146  if (ffuncs->immutable)
147    return;
148
149  ffuncs->get_contour_point = contour_point_func ? contour_point_func : hb_font_get_contour_point_nil;
150}
151
152void
153hb_font_funcs_set_glyph_metrics_func (hb_font_funcs_t *ffuncs,
154				      hb_font_get_glyph_metrics_func_t glyph_metrics_func)
155{
156  if (ffuncs->immutable)
157    return;
158
159  ffuncs->get_glyph_metrics = glyph_metrics_func ? glyph_metrics_func : hb_font_get_glyph_metrics_nil;
160}
161
162void
163hb_font_funcs_set_kerning_func (hb_font_funcs_t *ffuncs,
164				hb_font_get_kerning_func_t kerning_func)
165{
166  if (ffuncs->immutable)
167    return;
168
169  ffuncs->get_kerning = kerning_func ? kerning_func : hb_font_get_kerning_nil;
170}
171
172
173/*
174 * hb_face_t
175 */
176
177static hb_blob_t *
178_hb_face_get_table_from_blob (hb_tag_t tag, void *user_data)
179{
180  hb_face_t *face = (hb_face_t *) user_data;
181
182  const OpenTypeFontFile &ot_file = Sanitizer<OpenTypeFontFile>::lock_instance (face->blob);
183  const OpenTypeFontFace &ot_face = ot_file.get_face (face->index);
184
185  const OpenTypeTable &table = ot_face.get_table_by_tag (tag);
186
187  hb_blob_t *blob = hb_blob_create_sub_blob (face->blob, table.offset, table.length);
188
189  hb_blob_unlock (face->blob);
190
191  return blob;
192}
193
194static hb_face_t _hb_face_nil = {
195  HB_REFERENCE_COUNT_INVALID, /* ref_count */
196
197  NULL, /* blob */
198  0, /* index */
199
200  NULL, /* get_table */
201  NULL, /* destroy */
202  NULL, /* user_data */
203
204  {} /* ot_layout */
205};
206
207hb_face_t *
208hb_face_create_for_tables (hb_get_table_func_t  get_table,
209			   hb_destroy_func_t    destroy,
210			   void                *user_data)
211{
212  hb_face_t *face;
213
214  if (!HB_OBJECT_DO_CREATE (hb_face_t, face)) {
215    if (destroy)
216      destroy (user_data);
217    return &_hb_face_nil;
218  }
219
220  face->get_table = get_table;
221  face->destroy = destroy;
222  face->user_data = user_data;
223
224  _hb_ot_layout_init (face);
225
226  return face;
227}
228
229hb_face_t *
230hb_face_create_for_data (hb_blob_t    *blob,
231			 unsigned int  index)
232{
233  hb_face_t *face;
234
235  if (!HB_OBJECT_DO_CREATE (hb_face_t, face))
236    return &_hb_face_nil;
237
238  face->blob = Sanitizer<OpenTypeFontFile>::sanitize (hb_blob_reference (blob));
239  face->index = index;
240  face->get_table = _hb_face_get_table_from_blob;
241  face->user_data = face;
242
243  _hb_ot_layout_init (face);
244
245  return face;
246}
247
248hb_face_t *
249hb_face_reference (hb_face_t *face)
250{
251  HB_OBJECT_DO_REFERENCE (face);
252}
253
254unsigned int
255hb_face_get_reference_count (hb_face_t *face)
256{
257  HB_OBJECT_DO_GET_REFERENCE_COUNT (face);
258}
259
260void
261hb_face_destroy (hb_face_t *face)
262{
263  HB_OBJECT_DO_DESTROY (face);
264
265  _hb_ot_layout_fini (face);
266
267  hb_blob_destroy (face->blob);
268
269  if (face->destroy)
270    face->destroy (face->user_data);
271
272  free (face);
273}
274
275hb_blob_t *
276hb_face_get_table (hb_face_t *face,
277		   hb_tag_t   tag)
278{
279  if (HB_UNLIKELY (!face || !face->get_table))
280    return hb_blob_create_empty ();
281
282  return face->get_table (tag, face->user_data);
283}
284
285
286/*
287 * hb_font_t
288 */
289
290static hb_font_t _hb_font_nil = {
291  HB_REFERENCE_COUNT_INVALID, /* ref_count */
292
293  0, /* x_scale */
294  0, /* y_scale */
295
296  0, /* x_ppem */
297  0, /* y_ppem */
298
299  NULL, /* klass */
300  NULL, /* destroy */
301  NULL  /* user_data */
302};
303
304hb_font_t *
305hb_font_create (void)
306{
307  hb_font_t *font;
308
309  if (!HB_OBJECT_DO_CREATE (hb_font_t, font))
310    return &_hb_font_nil;
311
312  return font;
313}
314
315hb_font_t *
316hb_font_reference (hb_font_t *font)
317{
318  HB_OBJECT_DO_REFERENCE (font);
319}
320
321unsigned int
322hb_font_get_reference_count (hb_font_t *font)
323{
324  HB_OBJECT_DO_GET_REFERENCE_COUNT (font);
325}
326
327void
328hb_font_destroy (hb_font_t *font)
329{
330  HB_OBJECT_DO_DESTROY (font);
331
332  hb_font_funcs_destroy (font->klass);
333  if (font->destroy)
334    font->destroy (font->user_data);
335
336  free (font);
337}
338
339void
340hb_font_set_funcs (hb_font_t         *font,
341		   hb_font_funcs_t   *klass,
342		   hb_destroy_func_t  destroy,
343		   void              *user_data)
344{
345  if (HB_OBJECT_IS_INERT (font))
346    return;
347
348  if (font->destroy)
349    font->destroy (font->user_data);
350
351  hb_font_funcs_reference (klass);
352  hb_font_funcs_destroy (font->klass);
353  font->klass = klass;
354  font->destroy = destroy;
355  font->user_data = user_data;
356}
357
358void
359hb_font_set_scale (hb_font_t *font,
360		   hb_16dot16_t x_scale,
361		   hb_16dot16_t y_scale)
362{
363  if (HB_OBJECT_IS_INERT (font))
364    return;
365
366  font->x_scale = x_scale;
367  font->y_scale = y_scale;
368}
369
370void
371hb_font_set_ppem (hb_font_t *font,
372		  unsigned int x_ppem,
373		  unsigned int y_ppem)
374{
375  if (HB_OBJECT_IS_INERT (font))
376    return;
377
378  font->x_ppem = x_ppem;
379  font->y_ppem = y_ppem;
380}
381
382