Add initial implementation of user_data to objects
[apps/home/video-player.git] / src / hb-font.cc
1 /*
2  * Copyright © 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.hh"
28
29 #include "hb-font-private.hh"
30 #include "hb-blob-private.hh"
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 void
53 hb_font_get_glyph_advance_nil (hb_font_t *font HB_UNUSED,
54                                hb_face_t *face HB_UNUSED,
55                                const void *user_data HB_UNUSED,
56                                hb_codepoint_t glyph HB_UNUSED,
57                                hb_position_t *x_advance HB_UNUSED,
58                                hb_position_t *y_advance HB_UNUSED)
59 { }
60
61 static void
62 hb_font_get_glyph_extents_nil (hb_font_t *font HB_UNUSED,
63                                hb_face_t *face HB_UNUSED,
64                                const void *user_data HB_UNUSED,
65                                hb_codepoint_t glyph HB_UNUSED,
66                                hb_glyph_extents_t *extents HB_UNUSED)
67 { }
68
69 static hb_bool_t
70 hb_font_get_contour_point_nil (hb_font_t *font HB_UNUSED,
71                                hb_face_t *face HB_UNUSED,
72                                const void *user_data HB_UNUSED,
73                                unsigned int point_index HB_UNUSED,
74                                hb_codepoint_t glyph HB_UNUSED,
75                                hb_position_t *x HB_UNUSED,
76                                hb_position_t *y HB_UNUSED)
77 { return false; }
78
79 static hb_position_t
80 hb_font_get_kerning_nil (hb_font_t *font HB_UNUSED,
81                          hb_face_t *face HB_UNUSED,
82                          const void *user_data HB_UNUSED,
83                          hb_codepoint_t first_glyph HB_UNUSED,
84                          hb_codepoint_t second_glyph HB_UNUSED)
85 { return 0; }
86
87 hb_font_funcs_t _hb_font_funcs_nil = {
88   HB_OBJECT_HEADER_STATIC,
89
90   TRUE,  /* immutable */
91   {
92     hb_font_get_glyph_nil,
93     hb_font_get_glyph_advance_nil,
94     hb_font_get_glyph_extents_nil,
95     hb_font_get_contour_point_nil,
96     hb_font_get_kerning_nil
97   }
98 };
99
100 hb_font_funcs_t *
101 hb_font_funcs_create (void)
102 {
103   hb_font_funcs_t *ffuncs;
104
105   if (!(ffuncs = hb_object_create<hb_font_funcs_t> ()))
106     return &_hb_font_funcs_nil;
107
108   ffuncs->v = _hb_font_funcs_nil.v;
109
110   return ffuncs;
111 }
112
113 hb_font_funcs_t *
114 hb_font_funcs_reference (hb_font_funcs_t *ffuncs)
115 {
116   return hb_object_reference (ffuncs);
117 }
118
119 void
120 hb_font_funcs_destroy (hb_font_funcs_t *ffuncs)
121 {
122   if (!hb_object_destroy (ffuncs)) return;
123
124   free (ffuncs);
125 }
126
127 hb_font_funcs_t *
128 hb_font_funcs_copy (hb_font_funcs_t *other_ffuncs)
129 {
130   hb_font_funcs_t *ffuncs;
131
132   if (!(ffuncs = hb_object_create<hb_font_funcs_t> ()))
133     return &_hb_font_funcs_nil;
134
135   ffuncs->v = other_ffuncs->v;
136
137   return ffuncs;
138 }
139
140 void
141 hb_font_funcs_make_immutable (hb_font_funcs_t *ffuncs)
142 {
143   if (hb_object_is_inert (ffuncs))
144     return;
145
146   ffuncs->immutable = TRUE;
147 }
148
149 hb_bool_t
150 hb_font_funcs_is_immutable (hb_font_funcs_t *ffuncs)
151 {
152   return ffuncs->immutable;
153 }
154
155
156 void
157 hb_font_funcs_set_glyph_func (hb_font_funcs_t *ffuncs,
158                               hb_font_get_glyph_func_t glyph_func)
159 {
160   if (ffuncs->immutable)
161     return;
162
163   ffuncs->v.get_glyph = glyph_func ? glyph_func : hb_font_get_glyph_nil;
164 }
165
166 void
167 hb_font_funcs_set_glyph_advance_func (hb_font_funcs_t *ffuncs,
168                                       hb_font_get_glyph_advance_func_t glyph_advance_func)
169 {
170   if (ffuncs->immutable)
171     return;
172
173   ffuncs->v.get_glyph_advance = glyph_advance_func ? glyph_advance_func : hb_font_get_glyph_advance_nil;
174 }
175
176 void
177 hb_font_funcs_set_glyph_extents_func (hb_font_funcs_t *ffuncs,
178                                       hb_font_get_glyph_extents_func_t glyph_extents_func)
179 {
180   if (ffuncs->immutable)
181     return;
182
183   ffuncs->v.get_glyph_extents = glyph_extents_func ? glyph_extents_func : hb_font_get_glyph_extents_nil;
184 }
185
186 void
187 hb_font_funcs_set_contour_point_func (hb_font_funcs_t *ffuncs,
188                                       hb_font_get_contour_point_func_t contour_point_func)
189 {
190   if (ffuncs->immutable)
191     return;
192
193   ffuncs->v.get_contour_point = contour_point_func ? contour_point_func : hb_font_get_contour_point_nil;
194 }
195
196 void
197 hb_font_funcs_set_kerning_func (hb_font_funcs_t *ffuncs,
198                                 hb_font_get_kerning_func_t kerning_func)
199 {
200   if (ffuncs->immutable)
201     return;
202
203   ffuncs->v.get_kerning = kerning_func ? kerning_func : hb_font_get_kerning_nil;
204 }
205
206
207 hb_font_get_glyph_func_t
208 hb_font_funcs_get_glyph_func (hb_font_funcs_t *ffuncs)
209 {
210   return ffuncs->v.get_glyph;
211 }
212
213 hb_font_get_glyph_advance_func_t
214 hb_font_funcs_get_glyph_advance_func (hb_font_funcs_t *ffuncs)
215 {
216   return ffuncs->v.get_glyph_advance;
217 }
218
219 hb_font_get_glyph_extents_func_t
220 hb_font_funcs_get_glyph_extents_func (hb_font_funcs_t *ffuncs)
221 {
222   return ffuncs->v.get_glyph_extents;
223 }
224
225 hb_font_get_contour_point_func_t
226 hb_font_funcs_get_contour_point_func (hb_font_funcs_t *ffuncs)
227 {
228   return ffuncs->v.get_contour_point;
229 }
230
231 hb_font_get_kerning_func_t
232 hb_font_funcs_get_kerning_func (hb_font_funcs_t *ffuncs)
233 {
234   return ffuncs->v.get_kerning;
235 }
236
237
238
239 hb_codepoint_t
240 hb_font_get_glyph (hb_font_t *font, hb_face_t *face,
241                    hb_codepoint_t unicode, hb_codepoint_t variation_selector)
242 {
243   return font->klass->v.get_glyph (font, face, font->user_data,
244                                    unicode, variation_selector);
245 }
246
247 void
248 hb_font_get_glyph_advance (hb_font_t *font, hb_face_t *face,
249                            hb_codepoint_t glyph,
250                            hb_position_t *x_advance, hb_position_t *y_advance)
251 {
252   *x_advance = *y_advance = 0;
253   return font->klass->v.get_glyph_advance (font, face, font->user_data,
254                                            glyph, x_advance, y_advance);
255 }
256
257 void
258 hb_font_get_glyph_extents (hb_font_t *font, hb_face_t *face,
259                            hb_codepoint_t glyph, hb_glyph_extents_t *extents)
260 {
261   memset (extents, 0, sizeof (*extents));
262   return font->klass->v.get_glyph_extents (font, face, font->user_data,
263                                            glyph, extents);
264 }
265
266 hb_bool_t
267 hb_font_get_contour_point (hb_font_t *font, hb_face_t *face,
268                            unsigned int point_index,
269                            hb_codepoint_t glyph, hb_position_t *x, hb_position_t *y)
270 {
271   *x = 0; *y = 0;
272   return font->klass->v.get_contour_point (font, face, font->user_data,
273                                            point_index,
274                                            glyph, x, y);
275 }
276
277 hb_position_t
278 hb_font_get_kerning (hb_font_t *font, hb_face_t *face,
279                      hb_codepoint_t first_glyph, hb_codepoint_t second_glyph)
280 {
281   return font->klass->v.get_kerning (font, face, font->user_data,
282                                      first_glyph, second_glyph);
283 }
284
285
286 /*
287  * hb_face_t
288  */
289
290 static hb_face_t _hb_face_nil = {
291   HB_OBJECT_HEADER_STATIC,
292
293   NULL, /* get_table */
294   NULL, /* user_data */
295   NULL, /* destroy */
296
297   NULL, /* head_blob */
298   NULL, /* head_table */
299
300   NULL  /* ot_layout */
301 };
302
303
304 hb_face_t *
305 hb_face_create_for_tables (hb_get_table_func_t  get_table,
306                            void                *user_data,
307                            hb_destroy_func_t    destroy)
308 {
309   hb_face_t *face;
310
311   if (!(face = hb_object_create<hb_face_t> ())) {
312     if (destroy)
313       destroy (user_data);
314     return &_hb_face_nil;
315   }
316
317   face->get_table = get_table;
318   face->user_data = user_data;
319   face->destroy = destroy;
320
321   face->ot_layout = _hb_ot_layout_new (face);
322
323   face->head_blob = Sanitizer<head>::sanitize (hb_face_reference_table (face, HB_OT_TAG_head));
324   face->head_table = Sanitizer<head>::lock_instance (face->head_blob);
325
326   return face;
327 }
328
329
330 typedef struct _hb_face_for_data_closure_t {
331   hb_blob_t *blob;
332   unsigned int  index;
333 } hb_face_for_data_closure_t;
334
335 static hb_face_for_data_closure_t *
336 _hb_face_for_data_closure_create (hb_blob_t *blob, unsigned int index)
337 {
338   hb_face_for_data_closure_t *closure;
339
340   closure = (hb_face_for_data_closure_t *) malloc (sizeof (hb_face_for_data_closure_t));
341   if (unlikely (!closure))
342     return NULL;
343
344   closure->blob = blob;
345   closure->index = index;
346
347   return closure;
348 }
349
350 static void
351 _hb_face_for_data_closure_destroy (hb_face_for_data_closure_t *closure)
352 {
353   hb_blob_destroy (closure->blob);
354   free (closure);
355 }
356
357 static hb_blob_t *
358 _hb_face_for_data_get_table (hb_tag_t tag, void *user_data)
359 {
360   hb_face_for_data_closure_t *data = (hb_face_for_data_closure_t *) user_data;
361
362   const OpenTypeFontFile &ot_file = *Sanitizer<OpenTypeFontFile>::lock_instance (data->blob);
363   const OpenTypeFontFace &ot_face = ot_file.get_face (data->index);
364
365   const OpenTypeTable &table = ot_face.get_table_by_tag (tag);
366
367   hb_blob_t *blob = hb_blob_create_sub_blob (data->blob, table.offset, table.length);
368
369   hb_blob_unlock (data->blob);
370
371   return blob;
372 }
373
374 hb_face_t *
375 hb_face_create_for_data (hb_blob_t    *blob,
376                          unsigned int  index)
377 {
378   hb_face_for_data_closure_t *closure = _hb_face_for_data_closure_create (Sanitizer<OpenTypeFontFile>::sanitize (hb_blob_reference (blob)), index);
379
380   if (unlikely (!closure))
381     return &_hb_face_nil;
382
383   return hb_face_create_for_tables (_hb_face_for_data_get_table,
384                                     closure,
385                                     (hb_destroy_func_t) _hb_face_for_data_closure_destroy);
386 }
387
388
389 hb_face_t *
390 hb_face_reference (hb_face_t *face)
391 {
392   return hb_object_reference (face);
393 }
394
395 void
396 hb_face_destroy (hb_face_t *face)
397 {
398   if (!hb_object_destroy (face)) return;
399
400   _hb_ot_layout_free (face->ot_layout);
401
402   hb_blob_unlock (face->head_blob);
403   hb_blob_destroy (face->head_blob);
404
405   if (face->destroy)
406     face->destroy (face->user_data);
407
408   free (face);
409 }
410
411 hb_blob_t *
412 hb_face_reference_table (hb_face_t *face,
413                    hb_tag_t   tag)
414 {
415   hb_blob_t *blob;
416
417   if (unlikely (!face || !face->get_table))
418     return &_hb_blob_nil;
419
420   blob = face->get_table (tag, face->user_data);
421   if (unlikely (!blob))
422     blob = hb_blob_create_empty();
423
424   return blob;
425 }
426
427 unsigned int
428 hb_face_get_upem (hb_face_t *face)
429 {
430   return (face->head_table ? face->head_table : &Null(head))->get_upem ();
431 }
432
433
434 /*
435  * hb_font_t
436  */
437
438 static hb_font_t _hb_font_nil = {
439   HB_OBJECT_HEADER_STATIC,
440
441   0, /* x_scale */
442   0, /* y_scale */
443
444   0, /* x_ppem */
445   0, /* y_ppem */
446
447   NULL, /* klass */
448   NULL, /* user_data */
449   NULL  /* destroy */
450 };
451
452 hb_font_t *
453 hb_font_create (void)
454 {
455   hb_font_t *font;
456
457   if (!(font = hb_object_create<hb_font_t> ()))
458     return &_hb_font_nil;
459
460   font->klass = &_hb_font_funcs_nil;
461
462   return font;
463 }
464
465 hb_font_t *
466 hb_font_reference (hb_font_t *font)
467 {
468   return hb_object_reference (font);
469 }
470
471 void
472 hb_font_destroy (hb_font_t *font)
473 {
474   if (!hb_object_destroy (font)) return;
475
476   hb_font_funcs_destroy (font->klass);
477   if (font->destroy)
478     font->destroy (font->user_data);
479
480   free (font);
481 }
482
483 void
484 hb_font_set_funcs (hb_font_t         *font,
485                    hb_font_funcs_t   *klass,
486                    void              *user_data,
487                    hb_destroy_func_t  destroy)
488 {
489   if (hb_object_is_inert (font))
490     return;
491
492   if (font->destroy)
493     font->destroy (font->user_data);
494
495   if (!klass)
496     klass = &_hb_font_funcs_nil;
497
498   hb_font_funcs_reference (klass);
499   hb_font_funcs_destroy (font->klass);
500   font->klass = klass;
501   font->user_data = user_data;
502   font->destroy = destroy;
503 }
504
505 void
506 hb_font_unset_funcs (hb_font_t          *font,
507                      hb_font_funcs_t   **klass,
508                      void              **user_data,
509                      hb_destroy_func_t  *destroy)
510 {
511   /* None of the input arguments can be NULL. */
512
513   *klass = font->klass;
514   *user_data = font->user_data;
515   *destroy = font->destroy;
516
517   if (hb_object_is_inert (font))
518     return;
519
520   font->klass = NULL;
521   font->user_data = NULL;
522   font->destroy = NULL;
523 }
524
525 void
526 hb_font_set_scale (hb_font_t *font,
527                    int x_scale,
528                    int y_scale)
529 {
530   if (hb_object_is_inert (font))
531     return;
532
533   font->x_scale = x_scale;
534   font->y_scale = y_scale;
535 }
536
537 void
538 hb_font_get_scale (hb_font_t *font,
539                    int *x_scale,
540                    int *y_scale)
541 {
542   if (x_scale) *x_scale = font->x_scale;
543   if (y_scale) *y_scale = font->y_scale;
544 }
545
546 void
547 hb_font_set_ppem (hb_font_t *font,
548                   unsigned int x_ppem,
549                   unsigned int y_ppem)
550 {
551   if (hb_object_is_inert (font))
552     return;
553
554   font->x_ppem = x_ppem;
555   font->y_ppem = y_ppem;
556 }
557
558 void
559 hb_font_get_ppem (hb_font_t *font,
560                   unsigned int *x_ppem,
561                   unsigned int *y_ppem)
562 {
563   if (x_ppem) *x_ppem = font->x_ppem;
564   if (y_ppem) *y_ppem = font->y_ppem;
565 }
566
567
568 HB_END_DECLS