[API] Add support for vertical text
[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-ot-layout-private.hh"
30
31 #include "hb-font-private.hh"
32 #include "hb-blob.h"
33 #include "hb-open-file-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_bool_t
45 hb_font_get_glyph_nil (hb_font_t *font HB_UNUSED,
46                        void *font_data HB_UNUSED,
47                        hb_codepoint_t unicode,
48                        hb_codepoint_t variation_selector,
49                        hb_codepoint_t *glyph,
50                        void *user_data HB_UNUSED)
51 {
52   if (font->parent)
53     return hb_font_get_glyph (font->parent, unicode, variation_selector, glyph);
54
55   *glyph = 0;
56   return FALSE;
57 }
58
59 static hb_bool_t
60 hb_font_get_glyph_h_advance_nil (hb_font_t *font HB_UNUSED,
61                                  void *font_data HB_UNUSED,
62                                  hb_codepoint_t glyph,
63                                  hb_position_t *x_advance,
64                                  hb_position_t *y_advance,
65                                  void *user_data HB_UNUSED)
66 {
67   if (font->parent) {
68     hb_bool_t ret = hb_font_get_glyph_h_advance (font->parent,
69                                                  glyph,
70                                                  x_advance, y_advance);
71     font->parent_scale_distance (x_advance, y_advance);
72     return ret;
73   }
74
75   *x_advance = *y_advance = 0;
76   return FALSE;
77 }
78
79 static hb_bool_t
80 hb_font_get_glyph_v_advance_nil (hb_font_t *font HB_UNUSED,
81                                  void *font_data HB_UNUSED,
82                                  hb_codepoint_t glyph,
83                                  hb_position_t *x_advance,
84                                  hb_position_t *y_advance,
85                                  void *user_data HB_UNUSED)
86 {
87   if (font->parent) {
88     hb_bool_t ret = hb_font_get_glyph_v_advance (font->parent,
89                                                  glyph,
90                                                  x_advance, y_advance);
91     font->parent_scale_distance (x_advance, y_advance);
92     return ret;
93   }
94
95   *x_advance = *y_advance = 0;
96   return FALSE;
97 }
98
99 static hb_bool_t
100 hb_font_get_glyph_v_origin_nil (hb_font_t *font HB_UNUSED,
101                                 void *font_data HB_UNUSED,
102                                 hb_codepoint_t glyph,
103                                 hb_position_t *x_origin,
104                                 hb_position_t *y_origin,
105                                 void *user_data HB_UNUSED)
106 {
107   if (font->parent) {
108     hb_bool_t ret = hb_font_get_glyph_v_origin (font->parent,
109                                                 glyph,
110                                                 x_origin, y_origin);
111     font->parent_scale_distance (x_origin, y_origin);
112     return ret;
113   }
114
115   *x_origin = *y_origin = 0;
116   return FALSE;
117 }
118
119 static hb_bool_t
120 hb_font_get_h_kerning_nil (hb_font_t *font HB_UNUSED,
121                            void *font_data HB_UNUSED,
122                            hb_codepoint_t left_glyph,
123                            hb_codepoint_t right_glyph,
124                            hb_position_t *x_kern,
125                            hb_position_t *y_kern,
126                            void *user_data HB_UNUSED)
127 {
128   if (font->parent) {
129     hb_bool_t ret = hb_font_get_h_kerning (font->parent,
130                                            left_glyph, right_glyph,
131                                            x_kern, y_kern);
132     font->parent_scale_distance (x_kern, y_kern);
133     return ret;
134   }
135
136   *x_kern = *y_kern = 0;
137   return FALSE;
138 }
139
140 static hb_bool_t
141 hb_font_get_v_kerning_nil (hb_font_t *font HB_UNUSED,
142                            void *font_data HB_UNUSED,
143                            hb_codepoint_t top_glyph,
144                            hb_codepoint_t bottom_glyph,
145                            hb_position_t *x_kern,
146                            hb_position_t *y_kern,
147                            void *user_data HB_UNUSED)
148 {
149   if (font->parent) {
150     hb_bool_t ret = hb_font_get_v_kerning (font->parent,
151                                            top_glyph, bottom_glyph,
152                                            x_kern, y_kern);
153     font->parent_scale_distance (x_kern, y_kern);
154     return ret;
155   }
156
157   *x_kern = *y_kern = 0;
158   return FALSE;
159 }
160
161 static hb_bool_t
162 hb_font_get_glyph_extents_nil (hb_font_t *font HB_UNUSED,
163                                void *font_data HB_UNUSED,
164                                hb_codepoint_t glyph,
165                                hb_bool_t *vertical,
166                                hb_glyph_extents_t *extents,
167                                void *user_data HB_UNUSED)
168 {
169   if (font->parent) {
170     hb_bool_t ret = hb_font_get_glyph_extents (font->parent,
171                                                glyph,
172                                                vertical,
173                                                extents);
174     font->parent_scale_position (&extents->x_bearing, &extents->y_bearing);
175     font->parent_scale_distance (&extents->width, &extents->height);
176     return ret;
177   }
178
179   extents->x_bearing = extents->y_bearing = 0;
180   extents->width = extents->height = 0;
181   return FALSE;
182 }
183
184 static hb_bool_t
185 hb_font_get_contour_point_nil (hb_font_t *font HB_UNUSED,
186                                void *font_data HB_UNUSED,
187                                hb_codepoint_t glyph,
188                                unsigned int point_index,
189                                hb_bool_t *vertical,
190                                hb_position_t *x,
191                                hb_position_t *y,
192                                void *user_data HB_UNUSED)
193 {
194   if (font->parent) {
195     hb_bool_t ret = hb_font_get_contour_point (font->parent,
196                                                glyph, point_index,
197                                                vertical,
198                                                x, y);
199     font->parent_scale_position (x, y);
200     return ret;
201   }
202
203   *x = *y = 0;
204   return FALSE;
205 }
206
207
208 static hb_font_funcs_t _hb_font_funcs_nil = {
209   HB_OBJECT_HEADER_STATIC,
210
211   TRUE, /* immutable */
212
213   {
214 #define HB_FONT_FUNC_IMPLEMENT(name) hb_font_get_##name##_nil,
215     HB_FONT_FUNCS_IMPLEMENT_CALLBACKS
216 #undef HB_FONT_FUNC_IMPLEMENT
217   }
218 };
219
220
221 hb_font_funcs_t *
222 hb_font_funcs_create (void)
223 {
224   hb_font_funcs_t *ffuncs;
225
226   if (!(ffuncs = hb_object_create<hb_font_funcs_t> ()))
227     return &_hb_font_funcs_nil;
228
229   ffuncs->get = _hb_font_funcs_nil.get;
230
231   return ffuncs;
232 }
233
234 hb_font_funcs_t *
235 hb_font_funcs_get_empty (void)
236 {
237   return &_hb_font_funcs_nil;
238 }
239
240 hb_font_funcs_t *
241 hb_font_funcs_reference (hb_font_funcs_t *ffuncs)
242 {
243   return hb_object_reference (ffuncs);
244 }
245
246 void
247 hb_font_funcs_destroy (hb_font_funcs_t *ffuncs)
248 {
249   if (!hb_object_destroy (ffuncs)) return;
250
251 #define HB_FONT_FUNC_IMPLEMENT(name) if (ffuncs->destroy.name) ffuncs->destroy.name (ffuncs->user_data.name);
252   HB_FONT_FUNCS_IMPLEMENT_CALLBACKS
253 #undef HB_FONT_FUNC_IMPLEMENT
254
255   free (ffuncs);
256 }
257
258 hb_bool_t
259 hb_font_funcs_set_user_data (hb_font_funcs_t    *ffuncs,
260                              hb_user_data_key_t *key,
261                              void *              data,
262                              hb_destroy_func_t   destroy)
263 {
264   return hb_object_set_user_data (ffuncs, key, data, destroy);
265 }
266
267 void *
268 hb_font_funcs_get_user_data (hb_font_funcs_t    *ffuncs,
269                              hb_user_data_key_t *key)
270 {
271   return hb_object_get_user_data (ffuncs, key);
272 }
273
274
275 void
276 hb_font_funcs_make_immutable (hb_font_funcs_t *ffuncs)
277 {
278   if (hb_object_is_inert (ffuncs))
279     return;
280
281   ffuncs->immutable = TRUE;
282 }
283
284 hb_bool_t
285 hb_font_funcs_is_immutable (hb_font_funcs_t *ffuncs)
286 {
287   return ffuncs->immutable;
288 }
289
290
291 #define HB_FONT_FUNC_IMPLEMENT(name) \
292                                                                          \
293 void                                                                     \
294 hb_font_funcs_set_##name##_func (hb_font_funcs_t             *ffuncs,    \
295                                  hb_font_get_##name##_func_t  func,      \
296                                  void                        *user_data, \
297                                  hb_destroy_func_t            destroy)   \
298 {                                                                        \
299   if (ffuncs->immutable)                                                 \
300     return;                                                              \
301                                                                          \
302   if (ffuncs->destroy.name)                                              \
303     ffuncs->destroy.name (ffuncs->user_data.name);                       \
304                                                                          \
305   if (func) {                                                            \
306     ffuncs->get.name = func;                                             \
307     ffuncs->user_data.name = user_data;                                  \
308     ffuncs->destroy.name = destroy;                                      \
309   } else {                                                               \
310     ffuncs->get.name = hb_font_get_##name##_nil;                         \
311     ffuncs->user_data.name = NULL;                                       \
312     ffuncs->destroy.name = NULL;                                         \
313   }                                                                      \
314 }
315
316 HB_FONT_FUNCS_IMPLEMENT_CALLBACKS
317 #undef HB_FONT_FUNC_IMPLEMENT
318
319
320 hb_bool_t
321 hb_font_get_glyph (hb_font_t *font,
322                    hb_codepoint_t unicode, hb_codepoint_t variation_selector,
323                    hb_codepoint_t *glyph)
324 {
325   *glyph = 0;
326   return font->klass->get.glyph (font, font->user_data,
327                                  unicode, variation_selector, glyph,
328                                  font->klass->user_data.glyph);
329 }
330
331 hb_bool_t
332 hb_font_get_glyph_h_advance (hb_font_t *font,
333                              hb_codepoint_t glyph,
334                              hb_position_t *x_advance, hb_position_t *y_advance)
335 {
336   *x_advance = *y_advance = 0;
337   return font->klass->get.glyph_h_advance (font, font->user_data,
338                                            glyph, x_advance, y_advance,
339                                            font->klass->user_data.glyph_h_advance);
340 }
341
342 hb_bool_t
343 hb_font_get_glyph_v_advance (hb_font_t *font,
344                              hb_codepoint_t glyph,
345                              hb_position_t *x_advance, hb_position_t *y_advance)
346 {
347   *x_advance = *y_advance = 0;
348   return font->klass->get.glyph_v_advance (font, font->user_data,
349                                            glyph, x_advance, y_advance,
350                                            font->klass->user_data.glyph_v_advance);
351 }
352
353 hb_bool_t
354 hb_font_get_glyph_v_origin (hb_font_t *font,
355                             hb_codepoint_t glyph,
356                             hb_position_t *x_origin, hb_position_t *y_origin)
357 {
358   *x_origin = *y_origin = 0;
359   return font->klass->get.glyph_v_origin (font, font->user_data,
360                                            glyph, x_origin, y_origin,
361                                            font->klass->user_data.glyph_v_origin);
362 }
363
364 hb_bool_t
365 hb_font_get_h_kerning (hb_font_t *font,
366                        hb_codepoint_t left_glyph, hb_codepoint_t right_glyph,
367                        hb_position_t *x_kern, hb_position_t *y_kern)
368 {
369   *x_kern = *y_kern = 0;
370   return font->klass->get.h_kerning (font, font->user_data,
371                                      left_glyph, right_glyph,
372                                      x_kern, y_kern,
373                                      font->klass->user_data.h_kerning);
374 }
375
376 hb_bool_t
377 hb_font_get_v_kerning (hb_font_t *font,
378                        hb_codepoint_t left_glyph, hb_codepoint_t right_glyph,
379                        hb_position_t *x_kern, hb_position_t *y_kern)
380 {
381   *x_kern = *y_kern = 0;
382   return font->klass->get.v_kerning (font, font->user_data,
383                                      left_glyph, right_glyph,
384                                      x_kern, y_kern,
385                                      font->klass->user_data.v_kerning);
386 }
387
388 hb_bool_t
389 hb_font_get_glyph_extents (hb_font_t *font,
390                            hb_codepoint_t glyph,
391                            hb_bool_t *vertical,
392                            hb_glyph_extents_t *extents)
393 {
394   memset (extents, 0, sizeof (*extents));
395   return font->klass->get.glyph_extents (font, font->user_data,
396                                          glyph,
397                                          vertical,
398                                          extents,
399                                          font->klass->user_data.glyph_extents);
400 }
401
402 hb_bool_t
403 hb_font_get_contour_point (hb_font_t *font,
404                            hb_codepoint_t glyph, unsigned int point_index,
405                            hb_bool_t *vertical,
406                            hb_position_t *x, hb_position_t *y)
407 {
408   *x = *y = 0;
409   return font->klass->get.contour_point (font, font->user_data,
410                                          glyph, point_index,
411                                          vertical,
412                                          x, y,
413                                          font->klass->user_data.contour_point);
414 }
415
416
417 /* A bit higher-level, and with fallback */
418
419 void
420 hb_font_get_glyph_advance_for_direction (hb_font_t *font,
421                                          hb_codepoint_t glyph,
422                                          hb_direction_t direction,
423                                          hb_position_t *x_advance, hb_position_t *y_advance)
424 {
425   if (HB_DIRECTION_IS_VERTICAL (direction)) {
426     hb_bool_t ret = hb_font_get_glyph_v_advance (font, glyph, x_advance, y_advance);
427     if (!ret) {
428       /* TODO Simulate using h_advance and font_extents */
429     }
430   } else {
431     hb_font_get_glyph_h_advance (font, glyph, x_advance, y_advance);
432   }
433 }
434
435 void
436 hb_font_get_kerning_for_direction (hb_font_t *font,
437                                    hb_codepoint_t first_glyph, hb_codepoint_t second_glyph,
438                                    hb_direction_t direction,
439                                    hb_position_t *x_kern, hb_position_t *y_kern)
440 {
441   switch (direction) {
442     case HB_DIRECTION_LTR:
443     case HB_DIRECTION_RTL:
444       hb_font_get_h_kerning (font, first_glyph, second_glyph, x_kern, y_kern);
445       break;
446
447     case HB_DIRECTION_TTB:
448     case HB_DIRECTION_BTT:
449       hb_font_get_v_kerning (font, first_glyph, second_glyph, x_kern, y_kern);
450       break;
451
452     case HB_DIRECTION_INVALID:
453     default:
454       break;
455   }
456 }
457
458 void
459 hb_font_get_glyph_extents_for_direction (hb_font_t *font,
460                                          hb_codepoint_t glyph,
461                                          hb_direction_t direction,
462                                          hb_glyph_extents_t *extents)
463 {
464   hb_bool_t vertical = HB_DIRECTION_IS_VERTICAL (direction);
465   hb_bool_t ret = hb_font_get_glyph_extents (font, glyph, &vertical, extents);
466
467   if (ret) {
468     if (vertical != HB_DIRECTION_IS_VERTICAL (direction)) {
469       /* XXX Adjust origin */
470     }
471   } else {
472     /* TODO Simulate using get_h_advance and font_extents? */
473   }
474 }
475
476 hb_bool_t
477 hb_font_get_contour_point_for_direction (hb_font_t *font,
478                                          hb_codepoint_t glyph, unsigned int point_index,
479                                          hb_direction_t direction,
480                                          hb_position_t *x, hb_position_t *y)
481 {
482   hb_bool_t vertical = HB_DIRECTION_IS_VERTICAL (direction);
483   hb_bool_t ret = hb_font_get_contour_point (font, glyph, point_index, &vertical, x, y);
484
485   if (ret && vertical != HB_DIRECTION_IS_VERTICAL (direction)) {
486     /* XXX Adjust origin */
487   }
488
489   return ret;
490 }
491
492
493 /*
494  * hb_face_t
495  */
496
497 static hb_face_t _hb_face_nil = {
498   HB_OBJECT_HEADER_STATIC,
499
500   TRUE, /* immutable */
501
502   NULL, /* get_table */
503   NULL, /* user_data */
504   NULL, /* destroy */
505
506   NULL, /* ot_layout */
507
508   1000
509 };
510
511
512 hb_face_t *
513 hb_face_create_for_tables (hb_get_table_func_t  get_table,
514                            void                *user_data,
515                            hb_destroy_func_t    destroy)
516 {
517   hb_face_t *face;
518
519   if (!get_table || !(face = hb_object_create<hb_face_t> ())) {
520     if (destroy)
521       destroy (user_data);
522     return &_hb_face_nil;
523   }
524
525   face->get_table = get_table;
526   face->user_data = user_data;
527   face->destroy = destroy;
528
529   face->ot_layout = _hb_ot_layout_create (face);
530
531   face->upem = _hb_ot_layout_get_upem (face);
532
533   return face;
534 }
535
536
537 typedef struct _hb_face_for_data_closure_t {
538   hb_blob_t *blob;
539   unsigned int  index;
540 } hb_face_for_data_closure_t;
541
542 static hb_face_for_data_closure_t *
543 _hb_face_for_data_closure_create (hb_blob_t *blob, unsigned int index)
544 {
545   hb_face_for_data_closure_t *closure;
546
547   closure = (hb_face_for_data_closure_t *) malloc (sizeof (hb_face_for_data_closure_t));
548   if (unlikely (!closure))
549     return NULL;
550
551   closure->blob = blob;
552   closure->index = index;
553
554   return closure;
555 }
556
557 static void
558 _hb_face_for_data_closure_destroy (hb_face_for_data_closure_t *closure)
559 {
560   hb_blob_destroy (closure->blob);
561   free (closure);
562 }
563
564 static hb_blob_t *
565 _hb_face_for_data_get_table (hb_face_t *face HB_UNUSED, hb_tag_t tag, void *user_data)
566 {
567   hb_face_for_data_closure_t *data = (hb_face_for_data_closure_t *) user_data;
568
569   const OpenTypeFontFile &ot_file = *Sanitizer<OpenTypeFontFile>::lock_instance (data->blob);
570   const OpenTypeFontFace &ot_face = ot_file.get_face (data->index);
571
572   const OpenTypeTable &table = ot_face.get_table_by_tag (tag);
573
574   hb_blob_t *blob = hb_blob_create_sub_blob (data->blob, table.offset, table.length);
575
576   return blob;
577 }
578
579 hb_face_t *
580 hb_face_create (hb_blob_t    *blob,
581                 unsigned int  index)
582 {
583   if (unlikely (!blob || !hb_blob_get_length (blob)))
584     return &_hb_face_nil;
585
586   hb_face_for_data_closure_t *closure = _hb_face_for_data_closure_create (Sanitizer<OpenTypeFontFile>::sanitize (hb_blob_reference (blob)), index);
587
588   if (unlikely (!closure))
589     return &_hb_face_nil;
590
591   return hb_face_create_for_tables (_hb_face_for_data_get_table,
592                                     closure,
593                                     (hb_destroy_func_t) _hb_face_for_data_closure_destroy);
594 }
595
596 hb_face_t *
597 hb_face_get_empty (void)
598 {
599   return &_hb_face_nil;
600 }
601
602
603 hb_face_t *
604 hb_face_reference (hb_face_t *face)
605 {
606   return hb_object_reference (face);
607 }
608
609 void
610 hb_face_destroy (hb_face_t *face)
611 {
612   if (!hb_object_destroy (face)) return;
613
614   _hb_ot_layout_destroy (face->ot_layout);
615
616   if (face->destroy)
617     face->destroy (face->user_data);
618
619   free (face);
620 }
621
622 hb_bool_t
623 hb_face_set_user_data (hb_face_t          *face,
624                        hb_user_data_key_t *key,
625                        void *              data,
626                        hb_destroy_func_t   destroy)
627 {
628   return hb_object_set_user_data (face, key, data, destroy);
629 }
630
631 void *
632 hb_face_get_user_data (hb_face_t          *face,
633                        hb_user_data_key_t *key)
634 {
635   return hb_object_get_user_data (face, key);
636 }
637
638 void
639 hb_face_make_immutable (hb_face_t *face)
640 {
641   if (hb_object_is_inert (face))
642     return;
643
644   face->immutable = true;
645 }
646
647 hb_bool_t
648 hb_face_is_immutable (hb_face_t *face)
649 {
650   return face->immutable;
651 }
652
653
654 hb_blob_t *
655 hb_face_reference_table (hb_face_t *face,
656                          hb_tag_t   tag)
657 {
658   hb_blob_t *blob;
659
660   if (unlikely (!face || !face->get_table))
661     return hb_blob_get_empty ();
662
663   blob = face->get_table (face, tag, face->user_data);
664   if (unlikely (!blob))
665     return hb_blob_get_empty ();
666
667   return blob;
668 }
669
670 unsigned int
671 hb_face_get_upem (hb_face_t *face)
672 {
673   return _hb_ot_layout_get_upem (face);
674 }
675
676
677 /*
678  * hb_font_t
679  */
680
681 static hb_font_t _hb_font_nil = {
682   HB_OBJECT_HEADER_STATIC,
683
684   TRUE, /* immutable */
685
686   NULL, /* parent */
687   &_hb_face_nil,
688
689   0, /* x_scale */
690   0, /* y_scale */
691
692   0, /* x_ppem */
693   0, /* y_ppem */
694
695   &_hb_font_funcs_nil, /* klass */
696   NULL, /* user_data */
697   NULL  /* destroy */
698 };
699
700 hb_font_t *
701 hb_font_create (hb_face_t *face)
702 {
703   hb_font_t *font;
704
705   if (unlikely (!face))
706     face = &_hb_face_nil;
707   if (unlikely (hb_object_is_inert (face)))
708     return &_hb_font_nil;
709   if (!(font = hb_object_create<hb_font_t> ()))
710     return &_hb_font_nil;
711
712   hb_face_make_immutable (face);
713   font->face = hb_face_reference (face);
714   font->klass = &_hb_font_funcs_nil;
715
716   return font;
717 }
718
719 hb_font_t *
720 hb_font_create_sub_font (hb_font_t *parent)
721 {
722   if (unlikely (!parent))
723     return &_hb_font_nil;
724
725   hb_font_t *font = hb_font_create (parent->face);
726
727   if (unlikely (hb_object_is_inert (font)))
728     return font;
729
730   hb_font_make_immutable (parent);
731   font->parent = hb_font_reference (parent);
732
733   font->x_scale = parent->x_scale;
734   font->y_scale = parent->y_scale;
735   font->x_ppem = parent->x_ppem;
736   font->y_ppem = parent->y_ppem;
737
738   font->klass = &_hb_font_funcs_nil;
739
740   return font;
741 }
742
743 hb_font_t *
744 hb_font_get_empty (void)
745 {
746   return &_hb_font_nil;
747 }
748
749 hb_font_t *
750 hb_font_reference (hb_font_t *font)
751 {
752   return hb_object_reference (font);
753 }
754
755 void
756 hb_font_destroy (hb_font_t *font)
757 {
758   if (!hb_object_destroy (font)) return;
759
760   hb_font_destroy (font->parent);
761   hb_face_destroy (font->face);
762   hb_font_funcs_destroy (font->klass);
763   if (font->destroy)
764     font->destroy (font->user_data);
765
766   free (font);
767 }
768
769 hb_bool_t
770 hb_font_set_user_data (hb_font_t          *font,
771                        hb_user_data_key_t *key,
772                        void *              data,
773                        hb_destroy_func_t   destroy)
774 {
775   return hb_object_set_user_data (font, key, data, destroy);
776 }
777
778 void *
779 hb_font_get_user_data (hb_font_t          *font,
780                        hb_user_data_key_t *key)
781 {
782   return hb_object_get_user_data (font, key);
783 }
784
785 void
786 hb_font_make_immutable (hb_font_t *font)
787 {
788   if (hb_object_is_inert (font))
789     return;
790
791   font->immutable = true;
792 }
793
794 hb_bool_t
795 hb_font_is_immutable (hb_font_t *font)
796 {
797   return font->immutable;
798 }
799
800 hb_font_t *
801 hb_font_get_parent (hb_font_t *font)
802 {
803   return font->parent;
804 }
805
806 hb_face_t *
807 hb_font_get_face (hb_font_t *font)
808 {
809   return font->face;
810 }
811
812
813 void
814 hb_font_set_funcs (hb_font_t         *font,
815                    hb_font_funcs_t   *klass,
816                    void              *user_data,
817                    hb_destroy_func_t  destroy)
818 {
819   if (font->immutable)
820     return;
821
822   if (font->destroy)
823     font->destroy (font->user_data);
824
825   if (!klass)
826     klass = &_hb_font_funcs_nil;
827
828   hb_font_funcs_reference (klass);
829   hb_font_funcs_destroy (font->klass);
830   font->klass = klass;
831   font->user_data = user_data;
832   font->destroy = destroy;
833 }
834
835
836 void
837 hb_font_set_scale (hb_font_t *font,
838                    int x_scale,
839                    int y_scale)
840 {
841   if (font->immutable)
842     return;
843
844   font->x_scale = x_scale;
845   font->y_scale = y_scale;
846 }
847
848 void
849 hb_font_get_scale (hb_font_t *font,
850                    int *x_scale,
851                    int *y_scale)
852 {
853   if (x_scale) *x_scale = font->x_scale;
854   if (y_scale) *y_scale = font->y_scale;
855 }
856
857 void
858 hb_font_set_ppem (hb_font_t *font,
859                   unsigned int x_ppem,
860                   unsigned int y_ppem)
861 {
862   if (font->immutable)
863     return;
864
865   font->x_ppem = x_ppem;
866   font->y_ppem = y_ppem;
867 }
868
869 void
870 hb_font_get_ppem (hb_font_t *font,
871                   unsigned int *x_ppem,
872                   unsigned int *y_ppem)
873 {
874   if (x_ppem) *x_ppem = font->x_ppem;
875   if (y_ppem) *y_ppem = font->y_ppem;
876 }
877
878
879 HB_END_DECLS