e257cd87d4480b088845b4a286777dfaa1f4e67a
[profile/ivi/org.tizen.video-player.git] / src / hb-font.cc
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.hh"
34
35 #include <string.h>
36
37 HB_BEGIN_DECLS
38
39
40 /*
41  * hb_font_funcs_t
42  */
43
44 static hb_codepoint_t
45 hb_font_get_glyph_nil (hb_font_t *font HB_UNUSED,
46                        hb_face_t *face HB_UNUSED,
47                        const void *user_data HB_UNUSED,
48                        hb_codepoint_t unicode HB_UNUSED,
49                        hb_codepoint_t variation_selector HB_UNUSED)
50 { return 0; }
51
52 static hb_bool_t
53 hb_font_get_contour_point_nil (hb_font_t *font HB_UNUSED,
54                                hb_face_t *face HB_UNUSED,
55                                const void *user_data HB_UNUSED,
56                                unsigned int point_index HB_UNUSED,
57                                hb_codepoint_t glyph HB_UNUSED,
58                                hb_position_t *x HB_UNUSED,
59                                hb_position_t *y HB_UNUSED)
60 { return false; }
61
62 static void
63 hb_font_get_glyph_metrics_nil (hb_font_t *font HB_UNUSED,
64                                hb_face_t *face HB_UNUSED,
65                                const void *user_data HB_UNUSED,
66                                hb_codepoint_t glyph HB_UNUSED,
67                                hb_glyph_metrics_t *metrics HB_UNUSED)
68 { }
69
70 static hb_position_t
71 hb_font_get_kerning_nil (hb_font_t *font HB_UNUSED,
72                          hb_face_t *face HB_UNUSED,
73                          const void *user_data HB_UNUSED,
74                          hb_codepoint_t first_glyph HB_UNUSED,
75                          hb_codepoint_t second_glyph HB_UNUSED)
76 { return 0; }
77
78 hb_font_funcs_t _hb_font_funcs_nil = {
79   HB_REFERENCE_COUNT_INVALID, /* ref_count */
80   TRUE,  /* immutable */
81   {
82     hb_font_get_glyph_nil,
83     hb_font_get_contour_point_nil,
84     hb_font_get_glyph_metrics_nil,
85     hb_font_get_kerning_nil
86   }
87 };
88
89 hb_font_funcs_t *
90 hb_font_funcs_create (void)
91 {
92   hb_font_funcs_t *ffuncs;
93
94   if (!HB_OBJECT_DO_CREATE (hb_font_funcs_t, ffuncs))
95     return &_hb_font_funcs_nil;
96
97   ffuncs->v = _hb_font_funcs_nil.v;
98
99   return ffuncs;
100 }
101
102 hb_font_funcs_t *
103 hb_font_funcs_reference (hb_font_funcs_t *ffuncs)
104 {
105   HB_OBJECT_DO_REFERENCE (ffuncs);
106 }
107
108 unsigned int
109 hb_font_funcs_get_reference_count (hb_font_funcs_t *ffuncs)
110 {
111   HB_OBJECT_DO_GET_REFERENCE_COUNT (ffuncs);
112 }
113
114 void
115 hb_font_funcs_destroy (hb_font_funcs_t *ffuncs)
116 {
117   HB_OBJECT_DO_DESTROY (ffuncs);
118
119   free (ffuncs);
120 }
121
122 hb_font_funcs_t *
123 hb_font_funcs_copy (hb_font_funcs_t *other_ffuncs)
124 {
125   hb_font_funcs_t *ffuncs;
126
127   if (!HB_OBJECT_DO_CREATE (hb_font_funcs_t, ffuncs))
128     return &_hb_font_funcs_nil;
129
130   ffuncs->v = other_ffuncs->v;
131
132   return ffuncs;
133 }
134
135 void
136 hb_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
145 void
146 hb_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->v.get_glyph = glyph_func ? glyph_func : hb_font_get_glyph_nil;
153 }
154
155 void
156 hb_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->v.get_contour_point = contour_point_func ? contour_point_func : hb_font_get_contour_point_nil;
163 }
164
165 void
166 hb_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->v.get_glyph_metrics = glyph_metrics_func ? glyph_metrics_func : hb_font_get_glyph_metrics_nil;
173 }
174
175 void
176 hb_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->v.get_kerning = kerning_func ? kerning_func : hb_font_get_kerning_nil;
183 }
184
185
186 hb_font_get_glyph_func_t
187 hb_font_funcs_get_glyph_func (hb_font_funcs_t *ffuncs)
188 {
189   return ffuncs->v.get_glyph;
190 }
191
192 hb_font_get_contour_point_func_t
193 hb_font_funcs_get_contour_point_func (hb_font_funcs_t *ffuncs)
194 {
195   return ffuncs->v.get_contour_point;
196 }
197
198 hb_font_get_glyph_metrics_func_t
199 hb_font_funcs_get_glyph_metrics_func (hb_font_funcs_t *ffuncs)
200 {
201   return ffuncs->v.get_glyph_metrics;
202 }
203
204 hb_font_get_kerning_func_t
205 hb_font_funcs_get_kerning_func (hb_font_funcs_t *ffuncs)
206 {
207   return ffuncs->v.get_kerning;
208 }
209
210
211
212 hb_codepoint_t
213 hb_font_get_glyph (hb_font_t *font, hb_face_t *face,
214                    hb_codepoint_t unicode, hb_codepoint_t variation_selector)
215 {
216   return font->klass->v.get_glyph (font, face, font->user_data,
217                                    unicode, variation_selector);
218 }
219
220 hb_bool_t
221 hb_font_get_contour_point (hb_font_t *font, hb_face_t *face,
222                            unsigned int point_index,
223                            hb_codepoint_t glyph, hb_position_t *x, hb_position_t *y)
224 {
225   *x = 0; *y = 0;
226   return font->klass->v.get_contour_point (font, face, font->user_data,
227                                            point_index,
228                                            glyph, x, y);
229 }
230
231 void
232 hb_font_get_glyph_metrics (hb_font_t *font, hb_face_t *face,
233                            hb_codepoint_t glyph, hb_glyph_metrics_t *metrics)
234 {
235   memset (metrics, 0, sizeof (*metrics));
236   return font->klass->v.get_glyph_metrics (font, face, font->user_data,
237                                            glyph, metrics);
238 }
239
240 hb_position_t
241 hb_font_get_kerning (hb_font_t *font, hb_face_t *face,
242                      hb_codepoint_t first_glyph, hb_codepoint_t second_glyph)
243 {
244   return font->klass->v.get_kerning (font, face, font->user_data,
245                                      first_glyph, second_glyph);
246 }
247
248
249 /*
250  * hb_face_t
251  */
252
253 static hb_face_t _hb_face_nil = {
254   HB_REFERENCE_COUNT_INVALID, /* ref_count */
255
256   NULL, /* get_table */
257   NULL, /* destroy */
258   NULL, /* user_data */
259
260   NULL, /* head_blob */
261   NULL, /* head_table */
262
263   NULL  /* ot_layout */
264 };
265
266
267 hb_face_t *
268 hb_face_create_for_tables (hb_get_table_func_t  get_table,
269                            hb_destroy_func_t    destroy,
270                            void                *user_data)
271 {
272   hb_face_t *face;
273
274   if (!HB_OBJECT_DO_CREATE (hb_face_t, face)) {
275     if (destroy)
276       destroy (user_data);
277     return &_hb_face_nil;
278   }
279
280   face->get_table = get_table;
281   face->destroy = destroy;
282   face->user_data = user_data;
283
284   face->ot_layout = _hb_ot_layout_new (face);
285
286   face->head_blob = Sanitizer<head>::sanitize (hb_face_get_table (face, HB_OT_TAG_head));
287   face->head_table = Sanitizer<head>::lock_instance (face->head_blob);
288
289   return face;
290 }
291
292
293 typedef struct _hb_face_for_data_closure_t {
294   hb_blob_t *blob;
295   unsigned int  index;
296 } hb_face_for_data_closure_t;
297
298 static hb_face_for_data_closure_t *
299 _hb_face_for_data_closure_create (hb_blob_t *blob, unsigned int index)
300 {
301   hb_face_for_data_closure_t *closure;
302
303   closure = (hb_face_for_data_closure_t *) malloc (sizeof (hb_face_for_data_closure_t));
304   if (unlikely (!closure))
305     return NULL;
306
307   closure->blob = hb_blob_reference (blob);
308   closure->index = index;
309
310   return closure;
311 }
312
313 static void
314 _hb_face_for_data_closure_destroy (hb_face_for_data_closure_t *closure)
315 {
316   hb_blob_destroy (closure->blob);
317   free (closure);
318 }
319
320 static hb_blob_t *
321 _hb_face_for_data_get_table (hb_tag_t tag, void *user_data)
322 {
323   hb_face_for_data_closure_t *data = (hb_face_for_data_closure_t *) user_data;
324
325   const OpenTypeFontFile &ot_file = *Sanitizer<OpenTypeFontFile>::lock_instance (data->blob);
326   const OpenTypeFontFace &ot_face = ot_file.get_face (data->index);
327
328   const OpenTypeTable &table = ot_face.get_table_by_tag (tag);
329
330   hb_blob_t *blob = hb_blob_create_sub_blob (data->blob, table.offset, table.length);
331
332   hb_blob_unlock (data->blob);
333
334   return blob;
335 }
336
337 hb_face_t *
338 hb_face_create_for_data (hb_blob_t    *blob,
339                          unsigned int  index)
340 {
341   hb_face_for_data_closure_t *closure = _hb_face_for_data_closure_create (Sanitizer<OpenTypeFontFile>::sanitize (hb_blob_reference (blob)), index);
342
343   if (unlikely (!closure))
344     return &_hb_face_nil;
345
346   return hb_face_create_for_tables (_hb_face_for_data_get_table,
347                                     (hb_destroy_func_t) _hb_face_for_data_closure_destroy,
348                                     closure);
349 }
350
351
352 hb_face_t *
353 hb_face_reference (hb_face_t *face)
354 {
355   HB_OBJECT_DO_REFERENCE (face);
356 }
357
358 unsigned int
359 hb_face_get_reference_count (hb_face_t *face)
360 {
361   HB_OBJECT_DO_GET_REFERENCE_COUNT (face);
362 }
363
364 void
365 hb_face_destroy (hb_face_t *face)
366 {
367   HB_OBJECT_DO_DESTROY (face);
368
369   _hb_ot_layout_free (face->ot_layout);
370
371   hb_blob_unlock (face->head_blob);
372   hb_blob_destroy (face->head_blob);
373
374   if (face->destroy)
375     face->destroy (face->user_data);
376
377   free (face);
378 }
379
380 hb_blob_t *
381 hb_face_get_table (hb_face_t *face,
382                    hb_tag_t   tag)
383 {
384   hb_blob_t *blob;
385
386   if (unlikely (!face || !face->get_table))
387     return &_hb_blob_nil;
388
389   blob = face->get_table (tag, face->user_data);
390
391   return blob;
392 }
393
394
395 /*
396  * hb_font_t
397  */
398
399 static hb_font_t _hb_font_nil = {
400   HB_REFERENCE_COUNT_INVALID, /* ref_count */
401
402   0, /* x_scale */
403   0, /* y_scale */
404
405   0, /* x_ppem */
406   0, /* y_ppem */
407
408   NULL, /* klass */
409   NULL, /* destroy */
410   NULL  /* user_data */
411 };
412
413 hb_font_t *
414 hb_font_create (void)
415 {
416   hb_font_t *font;
417
418   if (!HB_OBJECT_DO_CREATE (hb_font_t, font))
419     return &_hb_font_nil;
420
421   font->klass = &_hb_font_funcs_nil;
422
423   return font;
424 }
425
426 hb_font_t *
427 hb_font_reference (hb_font_t *font)
428 {
429   HB_OBJECT_DO_REFERENCE (font);
430 }
431
432 unsigned int
433 hb_font_get_reference_count (hb_font_t *font)
434 {
435   HB_OBJECT_DO_GET_REFERENCE_COUNT (font);
436 }
437
438 void
439 hb_font_destroy (hb_font_t *font)
440 {
441   HB_OBJECT_DO_DESTROY (font);
442
443   hb_font_funcs_destroy (font->klass);
444   if (font->destroy)
445     font->destroy (font->user_data);
446
447   free (font);
448 }
449
450 void
451 hb_font_set_funcs (hb_font_t         *font,
452                    hb_font_funcs_t   *klass,
453                    hb_destroy_func_t  destroy,
454                    void              *user_data)
455 {
456   if (HB_OBJECT_IS_INERT (font))
457     return;
458
459   if (font->destroy)
460     font->destroy (font->user_data);
461
462   if (!klass)
463     klass = &_hb_font_funcs_nil;
464
465   hb_font_funcs_reference (klass);
466   hb_font_funcs_destroy (font->klass);
467   font->klass = klass;
468   font->destroy = destroy;
469   font->user_data = user_data;
470 }
471
472 void
473 hb_font_unset_funcs (hb_font_t          *font,
474                      hb_font_funcs_t   **klass,
475                      hb_destroy_func_t  *destroy,
476                      void              **user_data)
477 {
478   /* None of the input arguments can be NULL. */
479
480   *klass = font->klass;
481   *destroy = font->destroy;
482   *user_data = font->user_data;
483
484   if (HB_OBJECT_IS_INERT (font))
485     return;
486
487   font->klass = NULL;
488   font->destroy = NULL;
489   font->user_data = NULL;
490 }
491
492 void
493 hb_font_set_scale (hb_font_t *font,
494                    unsigned int x_scale,
495                    unsigned int y_scale)
496 {
497   if (HB_OBJECT_IS_INERT (font))
498     return;
499
500   font->x_scale = x_scale;
501   font->y_scale = y_scale;
502 }
503
504 void
505 hb_font_get_scale (hb_font_t *font,
506                    unsigned int *x_scale,
507                    unsigned int *y_scale)
508 {
509   if (x_scale) *x_scale = font->x_scale;
510   if (y_scale) *y_scale = font->y_scale;
511 }
512
513 void
514 hb_font_set_ppem (hb_font_t *font,
515                   unsigned int x_ppem,
516                   unsigned int y_ppem)
517 {
518   if (HB_OBJECT_IS_INERT (font))
519     return;
520
521   font->x_ppem = x_ppem;
522   font->y_ppem = y_ppem;
523 }
524
525 void
526 hb_font_get_ppem (hb_font_t *font,
527                   unsigned int *x_ppem,
528                   unsigned int *y_ppem)
529 {
530   if (x_ppem) *x_ppem = font->x_ppem;
531   if (y_ppem) *y_ppem = font->y_ppem;
532 }
533
534
535 HB_END_DECLS