hb-font.cc revision f97bf4f81257c97a46dff51be31b2ec214d6c270
1/*
2 * Copyright (C) 2009  Red Hat, Inc.
3 *
4 *  This is part of HarfBuzz, a text shaping 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.hh"
30#include "hb-blob-private.h"
31#include "hb-open-file-private.hh"
32
33#include "hb-ot-layout-private.hh"
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_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)
48{ return 0; }
49
50static hb_bool_t
51hb_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)
58{ return false; }
59
60static void
61hb_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)
66{ }
67
68static hb_position_t
69hb_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)
74{ return 0; }
75
76hb_font_funcs_t _hb_font_funcs_nil = {
77  HB_REFERENCE_COUNT_INVALID, /* ref_count */
78  TRUE,  /* immutable */
79  {
80    hb_font_get_glyph_nil,
81    hb_font_get_contour_point_nil,
82    hb_font_get_glyph_metrics_nil,
83    hb_font_get_kerning_nil
84  }
85};
86
87hb_font_funcs_t *
88hb_font_funcs_create (void)
89{
90  hb_font_funcs_t *ffuncs;
91
92  if (!HB_OBJECT_DO_CREATE (hb_font_funcs_t, ffuncs))
93    return &_hb_font_funcs_nil;
94
95  ffuncs->v = _hb_font_funcs_nil.v;
96
97  return ffuncs;
98}
99
100hb_font_funcs_t *
101hb_font_funcs_reference (hb_font_funcs_t *ffuncs)
102{
103  HB_OBJECT_DO_REFERENCE (ffuncs);
104}
105
106unsigned int
107hb_font_funcs_get_reference_count (hb_font_funcs_t *ffuncs)
108{
109  HB_OBJECT_DO_GET_REFERENCE_COUNT (ffuncs);
110}
111
112void
113hb_font_funcs_destroy (hb_font_funcs_t *ffuncs)
114{
115  HB_OBJECT_DO_DESTROY (ffuncs);
116
117  free (ffuncs);
118}
119
120hb_font_funcs_t *
121hb_font_funcs_copy (hb_font_funcs_t *other_ffuncs)
122{
123  hb_font_funcs_t *ffuncs;
124
125  if (!HB_OBJECT_DO_CREATE (hb_font_funcs_t, ffuncs))
126    return &_hb_font_funcs_nil;
127
128  ffuncs->v = other_ffuncs->v;
129
130  return ffuncs;
131}
132
133void
134hb_font_funcs_make_immutable (hb_font_funcs_t *ffuncs)
135{
136  if (HB_OBJECT_IS_INERT (ffuncs))
137    return;
138
139  ffuncs->immutable = TRUE;
140}
141
142
143void
144hb_font_funcs_set_glyph_func (hb_font_funcs_t *ffuncs,
145			      hb_font_get_glyph_func_t glyph_func)
146{
147  if (ffuncs->immutable)
148    return;
149
150  ffuncs->v.get_glyph = glyph_func ? glyph_func : hb_font_get_glyph_nil;
151}
152
153void
154hb_font_funcs_set_contour_point_func (hb_font_funcs_t *ffuncs,
155				      hb_font_get_contour_point_func_t contour_point_func)
156{
157  if (ffuncs->immutable)
158    return;
159
160  ffuncs->v.get_contour_point = contour_point_func ? contour_point_func : hb_font_get_contour_point_nil;
161}
162
163void
164hb_font_funcs_set_glyph_metrics_func (hb_font_funcs_t *ffuncs,
165				      hb_font_get_glyph_metrics_func_t glyph_metrics_func)
166{
167  if (ffuncs->immutable)
168    return;
169
170  ffuncs->v.get_glyph_metrics = glyph_metrics_func ? glyph_metrics_func : hb_font_get_glyph_metrics_nil;
171}
172
173void
174hb_font_funcs_set_kerning_func (hb_font_funcs_t *ffuncs,
175				hb_font_get_kerning_func_t kerning_func)
176{
177  if (ffuncs->immutable)
178    return;
179
180  ffuncs->v.get_kerning = kerning_func ? kerning_func : hb_font_get_kerning_nil;
181}
182
183
184hb_codepoint_t
185hb_font_get_glyph (hb_font_t *font, hb_face_t *face,
186		   hb_codepoint_t unicode, hb_codepoint_t variation_selector)
187{
188  return font->klass->v.get_glyph (font, face, font->user_data,
189				   unicode, variation_selector);
190}
191
192hb_bool_t
193hb_font_get_contour_point (hb_font_t *font, hb_face_t *face,
194			   unsigned int point_index,
195			   hb_codepoint_t glyph, hb_position_t *x, hb_position_t *y)
196{
197  *x = 0; *y = 0;
198  return font->klass->v.get_contour_point (font, face, font->user_data,
199					   point_index,
200					   glyph, x, y);
201}
202
203void
204hb_font_get_glyph_metrics (hb_font_t *font, hb_face_t *face,
205			   hb_codepoint_t glyph, hb_glyph_metrics_t *metrics)
206{
207  memset (metrics, 0, sizeof (*metrics));
208  return font->klass->v.get_glyph_metrics (font, face, font->user_data,
209					   glyph, metrics);
210}
211
212hb_position_t
213hb_font_get_kerning (hb_font_t *font, hb_face_t *face,
214		     hb_codepoint_t first_glyph, hb_codepoint_t second_glyph)
215{
216  return font->klass->v.get_kerning (font, face, font->user_data,
217				     first_glyph, second_glyph);
218}
219
220
221/*
222 * hb_face_t
223 */
224
225static hb_face_t _hb_face_nil = {
226  HB_REFERENCE_COUNT_INVALID, /* ref_count */
227
228  NULL, /* get_table */
229  NULL, /* destroy */
230  NULL, /* user_data */
231
232  NULL, /* head_blob */
233  NULL, /* head_table */
234
235  NULL  /* ot_layout */
236};
237
238
239hb_face_t *
240hb_face_create_for_tables (hb_get_table_func_t  get_table,
241			   hb_destroy_func_t    destroy,
242			   void                *user_data)
243{
244  hb_face_t *face;
245
246  if (!HB_OBJECT_DO_CREATE (hb_face_t, face)) {
247    if (destroy)
248      destroy (user_data);
249    return &_hb_face_nil;
250  }
251
252  face->get_table = get_table;
253  face->destroy = destroy;
254  face->user_data = user_data;
255
256  face->ot_layout = _hb_ot_layout_new (face);
257
258  face->head_blob = Sanitizer<head>::sanitize (hb_face_get_table (face, HB_OT_TAG_head));
259  face->head_table = Sanitizer<head>::lock_instance (face->head_blob);
260
261  return face;
262}
263
264
265typedef struct _hb_face_for_data_closure_t {
266  hb_blob_t *blob;
267  unsigned int  index;
268} hb_face_for_data_closure_t;
269
270static hb_face_for_data_closure_t *
271_hb_face_for_data_closure_create (hb_blob_t *blob, unsigned int index)
272{
273  hb_face_for_data_closure_t *closure;
274
275  closure = (hb_face_for_data_closure_t *) malloc (sizeof (hb_face_for_data_closure_t));
276  if (unlikely (!closure))
277    return NULL;
278
279  closure->blob = hb_blob_reference (blob);
280  closure->index = index;
281
282  return closure;
283}
284
285static void
286_hb_face_for_data_closure_destroy (hb_face_for_data_closure_t *closure)
287{
288  hb_blob_destroy (closure->blob);
289  free (closure);
290}
291
292static hb_blob_t *
293_hb_face_for_data_get_table (hb_tag_t tag, void *user_data)
294{
295  hb_face_for_data_closure_t *data = (hb_face_for_data_closure_t *) user_data;
296
297  const OpenTypeFontFile &ot_file = *Sanitizer<OpenTypeFontFile>::lock_instance (data->blob);
298  const OpenTypeFontFace &ot_face = ot_file.get_face (data->index);
299
300  const OpenTypeTable &table = ot_face.get_table_by_tag (tag);
301
302  hb_blob_t *blob = hb_blob_create_sub_blob (data->blob, table.offset, table.length);
303
304  hb_blob_unlock (data->blob);
305
306  return blob;
307}
308
309hb_face_t *
310hb_face_create_for_data (hb_blob_t    *blob,
311			 unsigned int  index)
312{
313  hb_blob_reference (blob);
314  hb_face_for_data_closure_t *closure = _hb_face_for_data_closure_create (Sanitizer<OpenTypeFontFile>::sanitize (blob), index);
315  hb_blob_destroy (blob);
316
317  if (unlikely (!closure))
318    return &_hb_face_nil;
319
320  return hb_face_create_for_tables (_hb_face_for_data_get_table,
321				    (hb_destroy_func_t) _hb_face_for_data_closure_destroy,
322				    closure);
323}
324
325
326hb_face_t *
327hb_face_reference (hb_face_t *face)
328{
329  HB_OBJECT_DO_REFERENCE (face);
330}
331
332unsigned int
333hb_face_get_reference_count (hb_face_t *face)
334{
335  HB_OBJECT_DO_GET_REFERENCE_COUNT (face);
336}
337
338void
339hb_face_destroy (hb_face_t *face)
340{
341  HB_OBJECT_DO_DESTROY (face);
342
343  _hb_ot_layout_free (face->ot_layout);
344
345  hb_blob_unlock (face->head_blob);
346  hb_blob_destroy (face->head_blob);
347
348  if (face->destroy)
349    face->destroy (face->user_data);
350
351  free (face);
352}
353
354hb_blob_t *
355hb_face_get_table (hb_face_t *face,
356		   hb_tag_t   tag)
357{
358  hb_blob_t *blob;
359
360  if (unlikely (!face || !face->get_table))
361    return &_hb_blob_nil;
362
363  blob = face->get_table (tag, face->user_data);
364
365  return blob;
366}
367
368
369/*
370 * hb_font_t
371 */
372
373static hb_font_t _hb_font_nil = {
374  HB_REFERENCE_COUNT_INVALID, /* ref_count */
375
376  0, /* x_scale */
377  0, /* y_scale */
378
379  0, /* x_ppem */
380  0, /* y_ppem */
381
382  NULL, /* klass */
383  NULL, /* destroy */
384  NULL  /* user_data */
385};
386
387hb_font_t *
388hb_font_create (void)
389{
390  hb_font_t *font;
391
392  if (!HB_OBJECT_DO_CREATE (hb_font_t, font))
393    return &_hb_font_nil;
394
395  font->klass = &_hb_font_funcs_nil;
396
397  return font;
398}
399
400hb_font_t *
401hb_font_reference (hb_font_t *font)
402{
403  HB_OBJECT_DO_REFERENCE (font);
404}
405
406unsigned int
407hb_font_get_reference_count (hb_font_t *font)
408{
409  HB_OBJECT_DO_GET_REFERENCE_COUNT (font);
410}
411
412void
413hb_font_destroy (hb_font_t *font)
414{
415  HB_OBJECT_DO_DESTROY (font);
416
417  hb_font_funcs_destroy (font->klass);
418  if (font->destroy)
419    font->destroy (font->user_data);
420
421  free (font);
422}
423
424void
425hb_font_set_funcs (hb_font_t         *font,
426		   hb_font_funcs_t   *klass,
427		   hb_destroy_func_t  destroy,
428		   void              *user_data)
429{
430  if (HB_OBJECT_IS_INERT (font))
431    return;
432
433  if (font->destroy)
434    font->destroy (font->user_data);
435
436  if (!klass)
437    klass = &_hb_font_funcs_nil;
438
439  hb_font_funcs_reference (klass);
440  hb_font_funcs_destroy (font->klass);
441  font->klass = klass;
442  font->destroy = destroy;
443  font->user_data = user_data;
444}
445
446void
447hb_font_set_scale (hb_font_t *font,
448		   unsigned int x_scale,
449		   unsigned int y_scale)
450{
451  if (HB_OBJECT_IS_INERT (font))
452    return;
453
454  font->x_scale = x_scale;
455  font->y_scale = y_scale;
456}
457
458void
459hb_font_set_ppem (hb_font_t *font,
460		  unsigned int x_ppem,
461		  unsigned int y_ppem)
462{
463  if (HB_OBJECT_IS_INERT (font))
464    return;
465
466  font->x_ppem = x_ppem;
467  font->y_ppem = y_ppem;
468}
469
470