hb-font.cc revision 8d70312c7b899131c3aafa7a43527ef3ced33bfe
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-unicode-private.h"
31#include "hb-open-file-private.hh"
32#include "hb-blob.h"
33
34#include "hb-ot-layout-private.h"
35
36/*
37 * hb_font_funcs_t
38 */
39
40hb_font_funcs_t _hb_font_funcs_nil = {
41  HB_REFERENCE_COUNT_INVALID, /* ref_count */
42
43  TRUE  /* immutable */
44
45  /*
46  hb_font_get_glyph_func_t glyph_func;
47  hb_font_get_contour_point_func_t contour_point_func;
48  hb_font_get_glyph_metrics_func_t glyph_metrics_func;
49  hb_font_get_kerning_func_t kerning_func;
50  */
51};
52
53hb_font_funcs_t *
54hb_font_funcs_create (void)
55{
56  hb_font_funcs_t *ffuncs;
57
58  if (!HB_OBJECT_DO_CREATE (hb_font_funcs_t, ffuncs))
59    return &_hb_font_funcs_nil;
60
61  return ffuncs;
62}
63
64hb_font_funcs_t *
65hb_font_funcs_reference (hb_font_funcs_t *ffuncs)
66{
67  HB_OBJECT_DO_REFERENCE (ffuncs);
68}
69
70unsigned int
71hb_font_funcs_get_reference_count (hb_font_funcs_t *ffuncs)
72{
73  HB_OBJECT_DO_GET_REFERENCE_COUNT (ffuncs);
74}
75
76void
77hb_font_funcs_destroy (hb_font_funcs_t *ffuncs)
78{
79  HB_OBJECT_DO_DESTROY (ffuncs);
80
81  free (ffuncs);
82}
83
84hb_font_funcs_t *
85hb_font_funcs_copy (hb_font_funcs_t *other_ffuncs)
86{
87  hb_font_funcs_t *ffuncs;
88
89  if (!HB_OBJECT_DO_CREATE (hb_font_funcs_t, ffuncs))
90    return &_hb_font_funcs_nil;
91
92  *ffuncs = *other_ffuncs;
93
94  /* re-init refcount */
95  HB_OBJECT_DO_INIT (ffuncs);
96  ffuncs->immutable = FALSE;
97
98  return ffuncs;
99}
100
101void
102hb_font_funcs_make_immutable (hb_font_funcs_t *ffuncs)
103{
104  if (HB_OBJECT_IS_INERT (ffuncs))
105    return;
106
107  ffuncs->immutable = TRUE;
108}
109
110
111
112/*
113 * hb_face_t
114 */
115
116static hb_blob_t *
117_hb_face_get_table_from_blob (hb_tag_t tag, void *user_data)
118{
119  hb_face_t *face = (hb_face_t *) user_data;
120
121  const OpenTypeFontFile &ot_file = Sanitizer<OpenTypeFontFile>::lock_instance (face->blob);
122  const OpenTypeFontFace &ot_face = ot_file.get_face (face->index);
123
124  const OpenTypeTable &table = ot_face.get_table_by_tag (tag);
125
126  hb_blob_t *blob = hb_blob_create_sub_blob (face->blob, table.offset, table.length);
127
128  hb_blob_unlock (face->blob);
129
130  return blob;
131}
132
133static hb_face_t _hb_face_nil = {
134  HB_REFERENCE_COUNT_INVALID, /* ref_count */
135
136  NULL, /* blob */
137  0, /* index */
138
139  NULL, /* get_table */
140  NULL, /* destroy */
141  NULL, /* user_data */
142
143  &_hb_unicode_funcs_nil,  /* unicode */
144
145  {} /* ot_layout */
146};
147
148hb_face_t *
149hb_face_create_for_tables (hb_get_table_func_t  get_table,
150			   hb_destroy_func_t    destroy,
151			   void                *user_data)
152{
153  hb_face_t *face;
154
155  if (!HB_OBJECT_DO_CREATE (hb_face_t, face)) {
156    if (destroy)
157      destroy (user_data);
158    return &_hb_face_nil;
159  }
160
161  face->get_table = get_table;
162  face->destroy = destroy;
163  face->user_data = user_data;
164
165  _hb_ot_layout_init (face);
166
167  return face;
168}
169
170hb_face_t *
171hb_face_create_for_data (hb_blob_t    *blob,
172			 unsigned int  index)
173{
174  hb_face_t *face;
175
176  if (!HB_OBJECT_DO_CREATE (hb_face_t, face))
177    return &_hb_face_nil;
178
179  face->blob = Sanitizer<OpenTypeFontFile>::sanitize (hb_blob_reference (blob));
180  face->index = index;
181  face->get_table = _hb_face_get_table_from_blob;
182  face->user_data = face;
183
184  _hb_ot_layout_init (face);
185
186  return face;
187}
188
189hb_face_t *
190hb_face_reference (hb_face_t *face)
191{
192  HB_OBJECT_DO_REFERENCE (face);
193}
194
195unsigned int
196hb_face_get_reference_count (hb_face_t *face)
197{
198  HB_OBJECT_DO_GET_REFERENCE_COUNT (face);
199}
200
201void
202hb_face_destroy (hb_face_t *face)
203{
204  HB_OBJECT_DO_DESTROY (face);
205
206  _hb_ot_layout_fini (face);
207
208  hb_blob_destroy (face->blob);
209
210  if (face->destroy)
211    face->destroy (face->user_data);
212
213  hb_unicode_funcs_destroy (face->unicode);
214
215  free (face);
216}
217
218void
219hb_face_set_unicode_funcs (hb_face_t *face,
220			       hb_unicode_funcs_t *unicode)
221{
222  if (HB_OBJECT_IS_INERT (face))
223    return;
224
225  hb_unicode_funcs_reference (unicode);
226  hb_unicode_funcs_destroy (face->unicode);
227  face->unicode = unicode;
228}
229
230hb_blob_t *
231hb_face_get_table (hb_face_t *face,
232		   hb_tag_t   tag)
233{
234  if (HB_UNLIKELY (!face || !face->get_table))
235    return hb_blob_create_empty ();
236
237  return face->get_table (tag, face->user_data);
238}
239
240
241/*
242 * hb_font_t
243 */
244
245static hb_font_t _hb_font_nil = {
246  HB_REFERENCE_COUNT_INVALID, /* ref_count */
247
248  0, /* x_scale */
249  0, /* y_scale */
250
251  0, /* x_ppem */
252  0, /* y_ppem */
253
254  NULL /* klass */
255};
256
257hb_font_t *
258hb_font_create (void)
259{
260  hb_font_t *font;
261
262  if (!HB_OBJECT_DO_CREATE (hb_font_t, font))
263    return &_hb_font_nil;
264
265  return font;
266}
267
268hb_font_t *
269hb_font_reference (hb_font_t *font)
270{
271  HB_OBJECT_DO_REFERENCE (font);
272}
273
274unsigned int
275hb_font_get_reference_count (hb_font_t *font)
276{
277  HB_OBJECT_DO_GET_REFERENCE_COUNT (font);
278}
279
280void
281hb_font_destroy (hb_font_t *font)
282{
283  HB_OBJECT_DO_DESTROY (font);
284
285  hb_font_funcs_destroy (font->klass);
286
287  free (font);
288}
289
290void
291hb_font_set_funcs (hb_font_t       *font,
292		   hb_font_funcs_t *klass)
293{
294  if (HB_OBJECT_IS_INERT (font))
295    return;
296
297  hb_font_funcs_reference (klass);
298  hb_font_funcs_destroy (font->klass);
299  font->klass = klass;
300}
301
302void
303hb_font_set_scale (hb_font_t *font,
304		   hb_16dot16_t x_scale,
305		   hb_16dot16_t y_scale)
306{
307  if (HB_OBJECT_IS_INERT (font))
308    return;
309
310  font->x_scale = x_scale;
311  font->y_scale = y_scale;
312}
313
314void
315hb_font_set_ppem (hb_font_t *font,
316		  unsigned int x_ppem,
317		  unsigned int y_ppem)
318{
319  if (HB_OBJECT_IS_INERT (font))
320    return;
321
322  font->x_ppem = x_ppem;
323  font->y_ppem = y_ppem;
324}
325
326