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