Imported Upstream version 2.6.4
[platform/upstream/harfbuzz.git] / src / hb-font.cc
1 /*
2  * Copyright © 2009  Red Hat, Inc.
3  * Copyright © 2012  Google, Inc.
4  *
5  *  This is part of HarfBuzz, a text shaping library.
6  *
7  * Permission is hereby granted, without written agreement and without
8  * license or royalty fees, to use, copy, modify, and distribute this
9  * software and its documentation for any purpose, provided that the
10  * above copyright notice and the following two paragraphs appear in
11  * all copies of this software.
12  *
13  * IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE TO ANY PARTY FOR
14  * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
15  * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN
16  * IF THE COPYRIGHT HOLDER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH
17  * DAMAGE.
18  *
19  * THE COPYRIGHT HOLDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING,
20  * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
21  * FITNESS FOR A PARTICULAR PURPOSE.  THE SOFTWARE PROVIDED HEREUNDER IS
22  * ON AN "AS IS" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO
23  * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
24  *
25  * Red Hat Author(s): Behdad Esfahbod
26  * Google Author(s): Behdad Esfahbod
27  */
28
29 #include "hb.hh"
30
31 #include "hb-font.hh"
32 #include "hb-machinery.hh"
33
34 #include "hb-ot.h"
35
36
37 /**
38  * SECTION:hb-font
39  * @title: hb-font
40  * @short_description: Font objects
41  * @include: hb.h
42  *
43  * Font objects represent a font face at a certain size and other
44  * parameters (pixels per EM, points per EM, variation settings.)
45  * Fonts are created from font faces, and are used as input to
46  * hb_shape() among other things.
47  **/
48
49
50 /*
51  * hb_font_funcs_t
52  */
53
54 static hb_bool_t
55 hb_font_get_font_h_extents_nil (hb_font_t *font HB_UNUSED,
56                                 void *font_data HB_UNUSED,
57                                 hb_font_extents_t *extents,
58                                 void *user_data HB_UNUSED)
59 {
60   memset (extents, 0, sizeof (*extents));
61   return false;
62 }
63 static hb_bool_t
64 hb_font_get_font_h_extents_default (hb_font_t *font,
65                                     void *font_data HB_UNUSED,
66                                     hb_font_extents_t *extents,
67                                     void *user_data HB_UNUSED)
68 {
69   hb_bool_t ret = font->parent->get_font_h_extents (extents);
70   if (ret) {
71     extents->ascender = font->parent_scale_y_distance (extents->ascender);
72     extents->descender = font->parent_scale_y_distance (extents->descender);
73     extents->line_gap = font->parent_scale_y_distance (extents->line_gap);
74   }
75   return ret;
76 }
77
78 static hb_bool_t
79 hb_font_get_font_v_extents_nil (hb_font_t *font HB_UNUSED,
80                                 void *font_data HB_UNUSED,
81                                 hb_font_extents_t *extents,
82                                 void *user_data HB_UNUSED)
83 {
84   memset (extents, 0, sizeof (*extents));
85   return false;
86 }
87 static hb_bool_t
88 hb_font_get_font_v_extents_default (hb_font_t *font,
89                                     void *font_data HB_UNUSED,
90                                     hb_font_extents_t *extents,
91                                     void *user_data HB_UNUSED)
92 {
93   hb_bool_t ret = font->parent->get_font_v_extents (extents);
94   if (ret) {
95     extents->ascender = font->parent_scale_x_distance (extents->ascender);
96     extents->descender = font->parent_scale_x_distance (extents->descender);
97     extents->line_gap = font->parent_scale_x_distance (extents->line_gap);
98   }
99   return ret;
100 }
101
102 static hb_bool_t
103 hb_font_get_nominal_glyph_nil (hb_font_t *font HB_UNUSED,
104                                void *font_data HB_UNUSED,
105                                hb_codepoint_t unicode HB_UNUSED,
106                                hb_codepoint_t *glyph,
107                                void *user_data HB_UNUSED)
108 {
109   *glyph = 0;
110   return false;
111 }
112 static hb_bool_t
113 hb_font_get_nominal_glyph_default (hb_font_t *font,
114                                    void *font_data HB_UNUSED,
115                                    hb_codepoint_t unicode,
116                                    hb_codepoint_t *glyph,
117                                    void *user_data HB_UNUSED)
118 {
119   if (font->has_nominal_glyphs_func_set ())
120   {
121     return font->get_nominal_glyphs (1, &unicode, 0, glyph, 0);
122   }
123   return font->parent->get_nominal_glyph (unicode, glyph);
124 }
125
126 #define hb_font_get_nominal_glyphs_nil hb_font_get_nominal_glyphs_default
127 static unsigned int
128 hb_font_get_nominal_glyphs_default (hb_font_t *font,
129                                     void *font_data HB_UNUSED,
130                                     unsigned int count,
131                                     const hb_codepoint_t *first_unicode,
132                                     unsigned int unicode_stride,
133                                     hb_codepoint_t *first_glyph,
134                                     unsigned int glyph_stride,
135                                     void *user_data HB_UNUSED)
136 {
137   if (font->has_nominal_glyph_func_set ())
138   {
139     for (unsigned int i = 0; i < count; i++)
140     {
141       if (!font->get_nominal_glyph (*first_unicode, first_glyph))
142         return i;
143
144       first_unicode = &StructAtOffsetUnaligned<hb_codepoint_t> (first_unicode, unicode_stride);
145       first_glyph = &StructAtOffsetUnaligned<hb_codepoint_t> (first_glyph, glyph_stride);
146     }
147     return count;
148   }
149
150   return font->parent->get_nominal_glyphs (count,
151                                            first_unicode, unicode_stride,
152                                            first_glyph, glyph_stride);
153 }
154
155 static hb_bool_t
156 hb_font_get_variation_glyph_nil (hb_font_t *font HB_UNUSED,
157                                  void *font_data HB_UNUSED,
158                                  hb_codepoint_t unicode HB_UNUSED,
159                                  hb_codepoint_t variation_selector HB_UNUSED,
160                                  hb_codepoint_t *glyph,
161                                  void *user_data HB_UNUSED)
162 {
163   *glyph = 0;
164   return false;
165 }
166 static hb_bool_t
167 hb_font_get_variation_glyph_default (hb_font_t *font,
168                                      void *font_data HB_UNUSED,
169                                      hb_codepoint_t unicode,
170                                      hb_codepoint_t variation_selector,
171                                      hb_codepoint_t *glyph,
172                                      void *user_data HB_UNUSED)
173 {
174   return font->parent->get_variation_glyph (unicode, variation_selector, glyph);
175 }
176
177
178 static hb_position_t
179 hb_font_get_glyph_h_advance_nil (hb_font_t *font,
180                                  void *font_data HB_UNUSED,
181                                  hb_codepoint_t glyph HB_UNUSED,
182                                  void *user_data HB_UNUSED)
183 {
184   return font->x_scale;
185 }
186 static hb_position_t
187 hb_font_get_glyph_h_advance_default (hb_font_t *font,
188                                      void *font_data HB_UNUSED,
189                                      hb_codepoint_t glyph,
190                                      void *user_data HB_UNUSED)
191 {
192   if (font->has_glyph_h_advances_func_set ())
193   {
194     hb_position_t ret;
195     font->get_glyph_h_advances (1, &glyph, 0, &ret, 0);
196     return ret;
197   }
198   return font->parent_scale_x_distance (font->parent->get_glyph_h_advance (glyph));
199 }
200
201 static hb_position_t
202 hb_font_get_glyph_v_advance_nil (hb_font_t *font,
203                                  void *font_data HB_UNUSED,
204                                  hb_codepoint_t glyph HB_UNUSED,
205                                  void *user_data HB_UNUSED)
206 {
207   /* TODO use font_extents.ascender+descender */
208   return font->y_scale;
209 }
210 static hb_position_t
211 hb_font_get_glyph_v_advance_default (hb_font_t *font,
212                                      void *font_data HB_UNUSED,
213                                      hb_codepoint_t glyph,
214                                      void *user_data HB_UNUSED)
215 {
216   if (font->has_glyph_v_advances_func_set ())
217   {
218     hb_position_t ret;
219     font->get_glyph_v_advances (1, &glyph, 0, &ret, 0);
220     return ret;
221   }
222   return font->parent_scale_y_distance (font->parent->get_glyph_v_advance (glyph));
223 }
224
225 #define hb_font_get_glyph_h_advances_nil hb_font_get_glyph_h_advances_default
226 static void
227 hb_font_get_glyph_h_advances_default (hb_font_t* font,
228                                       void* font_data HB_UNUSED,
229                                       unsigned int count,
230                                       const hb_codepoint_t *first_glyph,
231                                       unsigned int glyph_stride,
232                                       hb_position_t *first_advance,
233                                       unsigned int advance_stride,
234                                       void *user_data HB_UNUSED)
235 {
236   if (font->has_glyph_h_advance_func_set ())
237   {
238     for (unsigned int i = 0; i < count; i++)
239     {
240       *first_advance = font->get_glyph_h_advance (*first_glyph);
241       first_glyph = &StructAtOffsetUnaligned<hb_codepoint_t> (first_glyph, glyph_stride);
242       first_advance = &StructAtOffsetUnaligned<hb_position_t> (first_advance, advance_stride);
243     }
244     return;
245   }
246
247   font->parent->get_glyph_h_advances (count,
248                                       first_glyph, glyph_stride,
249                                       first_advance, advance_stride);
250   for (unsigned int i = 0; i < count; i++)
251   {
252     *first_advance = font->parent_scale_x_distance (*first_advance);
253     first_advance = &StructAtOffsetUnaligned<hb_position_t> (first_advance, advance_stride);
254   }
255 }
256
257 #define hb_font_get_glyph_v_advances_nil hb_font_get_glyph_v_advances_default
258 static void
259 hb_font_get_glyph_v_advances_default (hb_font_t* font,
260                                       void* font_data HB_UNUSED,
261                                       unsigned int count,
262                                       const hb_codepoint_t *first_glyph,
263                                       unsigned int glyph_stride,
264                                       hb_position_t *first_advance,
265                                       unsigned int advance_stride,
266                                       void *user_data HB_UNUSED)
267 {
268   if (font->has_glyph_v_advance_func_set ())
269   {
270     for (unsigned int i = 0; i < count; i++)
271     {
272       *first_advance = font->get_glyph_v_advance (*first_glyph);
273       first_glyph = &StructAtOffsetUnaligned<hb_codepoint_t> (first_glyph, glyph_stride);
274       first_advance = &StructAtOffsetUnaligned<hb_position_t> (first_advance, advance_stride);
275     }
276     return;
277   }
278
279   font->parent->get_glyph_v_advances (count,
280                                       first_glyph, glyph_stride,
281                                       first_advance, advance_stride);
282   for (unsigned int i = 0; i < count; i++)
283   {
284     *first_advance = font->parent_scale_y_distance (*first_advance);
285     first_advance = &StructAtOffsetUnaligned<hb_position_t> (first_advance, advance_stride);
286   }
287 }
288
289 static hb_bool_t
290 hb_font_get_glyph_h_origin_nil (hb_font_t *font HB_UNUSED,
291                                 void *font_data HB_UNUSED,
292                                 hb_codepoint_t glyph HB_UNUSED,
293                                 hb_position_t *x,
294                                 hb_position_t *y,
295                                 void *user_data HB_UNUSED)
296 {
297   *x = *y = 0;
298   return true;
299 }
300 static hb_bool_t
301 hb_font_get_glyph_h_origin_default (hb_font_t *font,
302                                     void *font_data HB_UNUSED,
303                                     hb_codepoint_t glyph,
304                                     hb_position_t *x,
305                                     hb_position_t *y,
306                                     void *user_data HB_UNUSED)
307 {
308   hb_bool_t ret = font->parent->get_glyph_h_origin (glyph, x, y);
309   if (ret)
310     font->parent_scale_position (x, y);
311   return ret;
312 }
313
314 static hb_bool_t
315 hb_font_get_glyph_v_origin_nil (hb_font_t *font HB_UNUSED,
316                                 void *font_data HB_UNUSED,
317                                 hb_codepoint_t glyph HB_UNUSED,
318                                 hb_position_t *x,
319                                 hb_position_t *y,
320                                 void *user_data HB_UNUSED)
321 {
322   *x = *y = 0;
323   return false;
324 }
325 static hb_bool_t
326 hb_font_get_glyph_v_origin_default (hb_font_t *font,
327                                     void *font_data HB_UNUSED,
328                                     hb_codepoint_t glyph,
329                                     hb_position_t *x,
330                                     hb_position_t *y,
331                                     void *user_data HB_UNUSED)
332 {
333   hb_bool_t ret = font->parent->get_glyph_v_origin (glyph, x, y);
334   if (ret)
335     font->parent_scale_position (x, y);
336   return ret;
337 }
338
339 static hb_position_t
340 hb_font_get_glyph_h_kerning_nil (hb_font_t *font HB_UNUSED,
341                                  void *font_data HB_UNUSED,
342                                  hb_codepoint_t left_glyph HB_UNUSED,
343                                  hb_codepoint_t right_glyph HB_UNUSED,
344                                  void *user_data HB_UNUSED)
345 {
346   return 0;
347 }
348 static hb_position_t
349 hb_font_get_glyph_h_kerning_default (hb_font_t *font,
350                                      void *font_data HB_UNUSED,
351                                      hb_codepoint_t left_glyph,
352                                      hb_codepoint_t right_glyph,
353                                      void *user_data HB_UNUSED)
354 {
355   return font->parent_scale_x_distance (font->parent->get_glyph_h_kerning (left_glyph, right_glyph));
356 }
357
358 #ifndef HB_DISABLE_DEPRECATED
359 static hb_position_t
360 hb_font_get_glyph_v_kerning_nil (hb_font_t *font HB_UNUSED,
361                                  void *font_data HB_UNUSED,
362                                  hb_codepoint_t top_glyph HB_UNUSED,
363                                  hb_codepoint_t bottom_glyph HB_UNUSED,
364                                  void *user_data HB_UNUSED)
365 {
366   return 0;
367 }
368 static hb_position_t
369 hb_font_get_glyph_v_kerning_default (hb_font_t *font,
370                                      void *font_data HB_UNUSED,
371                                      hb_codepoint_t top_glyph,
372                                      hb_codepoint_t bottom_glyph,
373                                      void *user_data HB_UNUSED)
374 {
375   return font->parent_scale_y_distance (font->parent->get_glyph_v_kerning (top_glyph, bottom_glyph));
376 }
377 #endif
378
379 static hb_bool_t
380 hb_font_get_glyph_extents_nil (hb_font_t *font HB_UNUSED,
381                                void *font_data HB_UNUSED,
382                                hb_codepoint_t glyph HB_UNUSED,
383                                hb_glyph_extents_t *extents,
384                                void *user_data HB_UNUSED)
385 {
386   memset (extents, 0, sizeof (*extents));
387   return false;
388 }
389 static hb_bool_t
390 hb_font_get_glyph_extents_default (hb_font_t *font,
391                                    void *font_data HB_UNUSED,
392                                    hb_codepoint_t glyph,
393                                    hb_glyph_extents_t *extents,
394                                    void *user_data HB_UNUSED)
395 {
396   hb_bool_t ret = font->parent->get_glyph_extents (glyph, extents);
397   if (ret) {
398     font->parent_scale_position (&extents->x_bearing, &extents->y_bearing);
399     font->parent_scale_distance (&extents->width, &extents->height);
400   }
401   return ret;
402 }
403
404 static hb_bool_t
405 hb_font_get_glyph_contour_point_nil (hb_font_t *font HB_UNUSED,
406                                      void *font_data HB_UNUSED,
407                                      hb_codepoint_t glyph HB_UNUSED,
408                                      unsigned int point_index HB_UNUSED,
409                                      hb_position_t *x,
410                                      hb_position_t *y,
411                                      void *user_data HB_UNUSED)
412 {
413   *x = *y = 0;
414   return false;
415 }
416 static hb_bool_t
417 hb_font_get_glyph_contour_point_default (hb_font_t *font,
418                                          void *font_data HB_UNUSED,
419                                          hb_codepoint_t glyph,
420                                          unsigned int point_index,
421                                          hb_position_t *x,
422                                          hb_position_t *y,
423                                          void *user_data HB_UNUSED)
424 {
425   hb_bool_t ret = font->parent->get_glyph_contour_point (glyph, point_index, x, y);
426   if (ret)
427     font->parent_scale_position (x, y);
428   return ret;
429 }
430
431 static hb_bool_t
432 hb_font_get_glyph_name_nil (hb_font_t *font HB_UNUSED,
433                             void *font_data HB_UNUSED,
434                             hb_codepoint_t glyph HB_UNUSED,
435                             char *name, unsigned int size,
436                             void *user_data HB_UNUSED)
437 {
438   if (size) *name = '\0';
439   return false;
440 }
441 static hb_bool_t
442 hb_font_get_glyph_name_default (hb_font_t *font,
443                                 void *font_data HB_UNUSED,
444                                 hb_codepoint_t glyph,
445                                 char *name, unsigned int size,
446                                 void *user_data HB_UNUSED)
447 {
448   return font->parent->get_glyph_name (glyph, name, size);
449 }
450
451 static hb_bool_t
452 hb_font_get_glyph_from_name_nil (hb_font_t *font HB_UNUSED,
453                                  void *font_data HB_UNUSED,
454                                  const char *name HB_UNUSED,
455                                  int len HB_UNUSED, /* -1 means nul-terminated */
456                                  hb_codepoint_t *glyph,
457                                  void *user_data HB_UNUSED)
458 {
459   *glyph = 0;
460   return false;
461 }
462 static hb_bool_t
463 hb_font_get_glyph_from_name_default (hb_font_t *font,
464                                      void *font_data HB_UNUSED,
465                                      const char *name, int len, /* -1 means nul-terminated */
466                                      hb_codepoint_t *glyph,
467                                      void *user_data HB_UNUSED)
468 {
469   return font->parent->get_glyph_from_name (name, len, glyph);
470 }
471
472 DEFINE_NULL_INSTANCE (hb_font_funcs_t) =
473 {
474   HB_OBJECT_HEADER_STATIC,
475
476   {
477 #define HB_FONT_FUNC_IMPLEMENT(name) nullptr,
478     HB_FONT_FUNCS_IMPLEMENT_CALLBACKS
479 #undef HB_FONT_FUNC_IMPLEMENT
480   },
481   {
482 #define HB_FONT_FUNC_IMPLEMENT(name) nullptr,
483     HB_FONT_FUNCS_IMPLEMENT_CALLBACKS
484 #undef HB_FONT_FUNC_IMPLEMENT
485   },
486   {
487     {
488 #define HB_FONT_FUNC_IMPLEMENT(name) hb_font_get_##name##_nil,
489       HB_FONT_FUNCS_IMPLEMENT_CALLBACKS
490 #undef HB_FONT_FUNC_IMPLEMENT
491     }
492   }
493 };
494
495 static const hb_font_funcs_t _hb_font_funcs_default = {
496   HB_OBJECT_HEADER_STATIC,
497
498   {
499 #define HB_FONT_FUNC_IMPLEMENT(name) nullptr,
500     HB_FONT_FUNCS_IMPLEMENT_CALLBACKS
501 #undef HB_FONT_FUNC_IMPLEMENT
502   },
503   {
504 #define HB_FONT_FUNC_IMPLEMENT(name) nullptr,
505     HB_FONT_FUNCS_IMPLEMENT_CALLBACKS
506 #undef HB_FONT_FUNC_IMPLEMENT
507   },
508   {
509     {
510 #define HB_FONT_FUNC_IMPLEMENT(name) hb_font_get_##name##_default,
511       HB_FONT_FUNCS_IMPLEMENT_CALLBACKS
512 #undef HB_FONT_FUNC_IMPLEMENT
513     }
514   }
515 };
516
517
518 /**
519  * hb_font_funcs_create: (Xconstructor)
520  *
521  *
522  *
523  * Return value: (transfer full):
524  *
525  * Since: 0.9.2
526  **/
527 hb_font_funcs_t *
528 hb_font_funcs_create ()
529 {
530   hb_font_funcs_t *ffuncs;
531
532   if (!(ffuncs = hb_object_create<hb_font_funcs_t> ()))
533     return hb_font_funcs_get_empty ();
534
535   ffuncs->get = _hb_font_funcs_default.get;
536
537   return ffuncs;
538 }
539
540 /**
541  * hb_font_funcs_get_empty:
542  *
543  *
544  *
545  * Return value: (transfer full):
546  *
547  * Since: 0.9.2
548  **/
549 hb_font_funcs_t *
550 hb_font_funcs_get_empty ()
551 {
552   return const_cast<hb_font_funcs_t *> (&_hb_font_funcs_default);
553 }
554
555 /**
556  * hb_font_funcs_reference: (skip)
557  * @ffuncs: font functions.
558  *
559  *
560  *
561  * Return value:
562  *
563  * Since: 0.9.2
564  **/
565 hb_font_funcs_t *
566 hb_font_funcs_reference (hb_font_funcs_t *ffuncs)
567 {
568   return hb_object_reference (ffuncs);
569 }
570
571 /**
572  * hb_font_funcs_destroy: (skip)
573  * @ffuncs: font functions.
574  *
575  *
576  *
577  * Since: 0.9.2
578  **/
579 void
580 hb_font_funcs_destroy (hb_font_funcs_t *ffuncs)
581 {
582   if (!hb_object_destroy (ffuncs)) return;
583
584 #define HB_FONT_FUNC_IMPLEMENT(name) if (ffuncs->destroy.name) \
585   ffuncs->destroy.name (ffuncs->user_data.name);
586   HB_FONT_FUNCS_IMPLEMENT_CALLBACKS
587 #undef HB_FONT_FUNC_IMPLEMENT
588
589   free (ffuncs);
590 }
591
592 /**
593  * hb_font_funcs_set_user_data: (skip)
594  * @ffuncs: font functions.
595  * @key:
596  * @data:
597  * @destroy:
598  * @replace:
599  *
600  *
601  *
602  * Return value:
603  *
604  * Since: 0.9.2
605  **/
606 hb_bool_t
607 hb_font_funcs_set_user_data (hb_font_funcs_t    *ffuncs,
608                              hb_user_data_key_t *key,
609                              void *              data,
610                              hb_destroy_func_t   destroy,
611                              hb_bool_t           replace)
612 {
613   return hb_object_set_user_data (ffuncs, key, data, destroy, replace);
614 }
615
616 /**
617  * hb_font_funcs_get_user_data: (skip)
618  * @ffuncs: font functions.
619  * @key:
620  *
621  *
622  *
623  * Return value: (transfer none):
624  *
625  * Since: 0.9.2
626  **/
627 void *
628 hb_font_funcs_get_user_data (hb_font_funcs_t    *ffuncs,
629                              hb_user_data_key_t *key)
630 {
631   return hb_object_get_user_data (ffuncs, key);
632 }
633
634
635 /**
636  * hb_font_funcs_make_immutable:
637  * @ffuncs: font functions.
638  *
639  *
640  *
641  * Since: 0.9.2
642  **/
643 void
644 hb_font_funcs_make_immutable (hb_font_funcs_t *ffuncs)
645 {
646   if (hb_object_is_immutable (ffuncs))
647     return;
648
649   hb_object_make_immutable (ffuncs);
650 }
651
652 /**
653  * hb_font_funcs_is_immutable:
654  * @ffuncs: font functions.
655  *
656  *
657  *
658  * Return value:
659  *
660  * Since: 0.9.2
661  **/
662 hb_bool_t
663 hb_font_funcs_is_immutable (hb_font_funcs_t *ffuncs)
664 {
665   return hb_object_is_immutable (ffuncs);
666 }
667
668
669 #define HB_FONT_FUNC_IMPLEMENT(name) \
670                                                                          \
671 void                                                                     \
672 hb_font_funcs_set_##name##_func (hb_font_funcs_t             *ffuncs,    \
673                                  hb_font_get_##name##_func_t  func,      \
674                                  void                        *user_data, \
675                                  hb_destroy_func_t            destroy)   \
676 {                                                                        \
677   if (hb_object_is_immutable (ffuncs)) {                                 \
678     if (destroy)                                                         \
679       destroy (user_data);                                               \
680     return;                                                              \
681   }                                                                      \
682                                                                          \
683   if (ffuncs->destroy.name)                                              \
684     ffuncs->destroy.name (ffuncs->user_data.name);                       \
685                                                                          \
686   if (func) {                                                            \
687     ffuncs->get.f.name = func;                                           \
688     ffuncs->user_data.name = user_data;                                  \
689     ffuncs->destroy.name = destroy;                                      \
690   } else {                                                               \
691     ffuncs->get.f.name = hb_font_get_##name##_default;                   \
692     ffuncs->user_data.name = nullptr;                                    \
693     ffuncs->destroy.name = nullptr;                                      \
694   }                                                                      \
695 }
696
697 HB_FONT_FUNCS_IMPLEMENT_CALLBACKS
698 #undef HB_FONT_FUNC_IMPLEMENT
699
700 bool
701 hb_font_t::has_func_set (unsigned int i)
702 {
703   return this->klass->get.array[i] != _hb_font_funcs_default.get.array[i];
704 }
705
706 bool
707 hb_font_t::has_func (unsigned int i)
708 {
709   return has_func_set (i) ||
710          (parent && parent != &_hb_Null_hb_font_t && parent->has_func (i));
711 }
712
713 /* Public getters */
714
715 /**
716  * hb_font_get_h_extents:
717  * @font: a font.
718  * @extents: (out):
719  *
720  *
721  *
722  * Return value:
723  *
724  * Since: 1.1.3
725  **/
726 hb_bool_t
727 hb_font_get_h_extents (hb_font_t *font,
728                        hb_font_extents_t *extents)
729 {
730   return font->get_font_h_extents (extents);
731 }
732
733 /**
734  * hb_font_get_v_extents:
735  * @font: a font.
736  * @extents: (out):
737  *
738  *
739  *
740  * Return value:
741  *
742  * Since: 1.1.3
743  **/
744 hb_bool_t
745 hb_font_get_v_extents (hb_font_t *font,
746                        hb_font_extents_t *extents)
747 {
748   return font->get_font_v_extents (extents);
749 }
750
751 /**
752  * hb_font_get_glyph:
753  * @font: a font.
754  * @unicode:
755  * @variation_selector:
756  * @glyph: (out):
757  *
758  *
759  *
760  * Return value:
761  *
762  * Since: 0.9.2
763  **/
764 hb_bool_t
765 hb_font_get_glyph (hb_font_t *font,
766                    hb_codepoint_t unicode, hb_codepoint_t variation_selector,
767                    hb_codepoint_t *glyph)
768 {
769   if (unlikely (variation_selector))
770     return font->get_variation_glyph (unicode, variation_selector, glyph);
771   return font->get_nominal_glyph (unicode, glyph);
772 }
773
774 /**
775  * hb_font_get_nominal_glyph:
776  * @font: a font.
777  * @unicode:
778  * @glyph: (out):
779  *
780  *
781  *
782  * Return value:
783  *
784  * Since: 1.2.3
785  **/
786 hb_bool_t
787 hb_font_get_nominal_glyph (hb_font_t *font,
788                            hb_codepoint_t unicode,
789                            hb_codepoint_t *glyph)
790 {
791   return font->get_nominal_glyph (unicode, glyph);
792 }
793
794 /**
795  * hb_font_get_nominal_glyphs:
796  * @font: a font.
797  *
798  *
799  *
800  * Return value:
801  *
802  * Since: 2.6.3
803  **/
804 unsigned int
805 hb_font_get_nominal_glyphs (hb_font_t *font,
806                             unsigned int count,
807                             const hb_codepoint_t *first_unicode,
808                             unsigned int unicode_stride,
809                             hb_codepoint_t *first_glyph,
810                             unsigned int glyph_stride)
811 {
812   return font->get_nominal_glyphs (count,
813                                    first_unicode, unicode_stride,
814                                    first_glyph, glyph_stride);
815 }
816
817 /**
818  * hb_font_get_variation_glyph:
819  * @font: a font.
820  * @unicode:
821  * @variation_selector:
822  * @glyph: (out):
823  *
824  *
825  *
826  * Return value:
827  *
828  * Since: 1.2.3
829  **/
830 hb_bool_t
831 hb_font_get_variation_glyph (hb_font_t *font,
832                              hb_codepoint_t unicode, hb_codepoint_t variation_selector,
833                              hb_codepoint_t *glyph)
834 {
835   return font->get_variation_glyph (unicode, variation_selector, glyph);
836 }
837
838 /**
839  * hb_font_get_glyph_h_advance:
840  * @font: a font.
841  * @glyph:
842  *
843  *
844  *
845  * Return value:
846  *
847  * Since: 0.9.2
848  **/
849 hb_position_t
850 hb_font_get_glyph_h_advance (hb_font_t *font,
851                              hb_codepoint_t glyph)
852 {
853   return font->get_glyph_h_advance (glyph);
854 }
855
856 /**
857  * hb_font_get_glyph_v_advance:
858  * @font: a font.
859  * @glyph:
860  *
861  *
862  *
863  * Return value:
864  *
865  * Since: 0.9.2
866  **/
867 hb_position_t
868 hb_font_get_glyph_v_advance (hb_font_t *font,
869                              hb_codepoint_t glyph)
870 {
871   return font->get_glyph_v_advance (glyph);
872 }
873
874 /**
875  * hb_font_get_glyph_h_advances:
876  * @font: a font.
877  *
878  *
879  *
880  * Since: 1.8.6
881  **/
882 void
883 hb_font_get_glyph_h_advances (hb_font_t* font,
884                               unsigned int count,
885                               const hb_codepoint_t *first_glyph,
886                               unsigned glyph_stride,
887                               hb_position_t *first_advance,
888                               unsigned advance_stride)
889 {
890   font->get_glyph_h_advances (count, first_glyph, glyph_stride, first_advance, advance_stride);
891 }
892 /**
893  * hb_font_get_glyph_v_advances:
894  * @font: a font.
895  *
896  *
897  *
898  * Since: 1.8.6
899  **/
900 void
901 hb_font_get_glyph_v_advances (hb_font_t* font,
902                               unsigned int count,
903                               const hb_codepoint_t *first_glyph,
904                               unsigned glyph_stride,
905                               hb_position_t *first_advance,
906                               unsigned advance_stride)
907 {
908   font->get_glyph_v_advances (count, first_glyph, glyph_stride, first_advance, advance_stride);
909 }
910
911 /**
912  * hb_font_get_glyph_h_origin:
913  * @font: a font.
914  * @glyph:
915  * @x: (out):
916  * @y: (out):
917  *
918  *
919  *
920  * Return value:
921  *
922  * Since: 0.9.2
923  **/
924 hb_bool_t
925 hb_font_get_glyph_h_origin (hb_font_t *font,
926                             hb_codepoint_t glyph,
927                             hb_position_t *x, hb_position_t *y)
928 {
929   return font->get_glyph_h_origin (glyph, x, y);
930 }
931
932 /**
933  * hb_font_get_glyph_v_origin:
934  * @font: a font.
935  * @glyph:
936  * @x: (out):
937  * @y: (out):
938  *
939  *
940  *
941  * Return value:
942  *
943  * Since: 0.9.2
944  **/
945 hb_bool_t
946 hb_font_get_glyph_v_origin (hb_font_t *font,
947                             hb_codepoint_t glyph,
948                             hb_position_t *x, hb_position_t *y)
949 {
950   return font->get_glyph_v_origin (glyph, x, y);
951 }
952
953 /**
954  * hb_font_get_glyph_h_kerning:
955  * @font: a font.
956  * @left_glyph:
957  * @right_glyph:
958  *
959  *
960  *
961  * Return value:
962  *
963  * Since: 0.9.2
964  **/
965 hb_position_t
966 hb_font_get_glyph_h_kerning (hb_font_t *font,
967                              hb_codepoint_t left_glyph, hb_codepoint_t right_glyph)
968 {
969   return font->get_glyph_h_kerning (left_glyph, right_glyph);
970 }
971
972 #ifndef HB_DISABLE_DEPRECATED
973 /**
974  * hb_font_get_glyph_v_kerning:
975  * @font: a font.
976  * @top_glyph:
977  * @bottom_glyph:
978  *
979  *
980  *
981  * Return value:
982  *
983  * Since: 0.9.2
984  * Deprecated: 2.0.0
985  **/
986 hb_position_t
987 hb_font_get_glyph_v_kerning (hb_font_t *font,
988                              hb_codepoint_t top_glyph, hb_codepoint_t bottom_glyph)
989 {
990   return font->get_glyph_v_kerning (top_glyph, bottom_glyph);
991 }
992 #endif
993
994 /**
995  * hb_font_get_glyph_extents:
996  * @font: a font.
997  * @glyph:
998  * @extents: (out):
999  *
1000  *
1001  *
1002  * Return value:
1003  *
1004  * Since: 0.9.2
1005  **/
1006 hb_bool_t
1007 hb_font_get_glyph_extents (hb_font_t *font,
1008                            hb_codepoint_t glyph,
1009                            hb_glyph_extents_t *extents)
1010 {
1011   return font->get_glyph_extents (glyph, extents);
1012 }
1013
1014 /**
1015  * hb_font_get_glyph_contour_point:
1016  * @font: a font.
1017  * @glyph:
1018  * @point_index:
1019  * @x: (out):
1020  * @y: (out):
1021  *
1022  *
1023  *
1024  * Return value:
1025  *
1026  * Since: 0.9.2
1027  **/
1028 hb_bool_t
1029 hb_font_get_glyph_contour_point (hb_font_t *font,
1030                                  hb_codepoint_t glyph, unsigned int point_index,
1031                                  hb_position_t *x, hb_position_t *y)
1032 {
1033   return font->get_glyph_contour_point (glyph, point_index, x, y);
1034 }
1035
1036 /**
1037  * hb_font_get_glyph_name:
1038  * @font: a font.
1039  * @glyph:
1040  * @name: (array length=size):
1041  * @size:
1042  *
1043  *
1044  *
1045  * Return value:
1046  *
1047  * Since: 0.9.2
1048  **/
1049 hb_bool_t
1050 hb_font_get_glyph_name (hb_font_t *font,
1051                         hb_codepoint_t glyph,
1052                         char *name, unsigned int size)
1053 {
1054   return font->get_glyph_name (glyph, name, size);
1055 }
1056
1057 /**
1058  * hb_font_get_glyph_from_name:
1059  * @font: a font.
1060  * @name: (array length=len):
1061  * @len:
1062  * @glyph: (out):
1063  *
1064  *
1065  *
1066  * Return value:
1067  *
1068  * Since: 0.9.2
1069  **/
1070 hb_bool_t
1071 hb_font_get_glyph_from_name (hb_font_t *font,
1072                              const char *name, int len, /* -1 means nul-terminated */
1073                              hb_codepoint_t *glyph)
1074 {
1075   return font->get_glyph_from_name (name, len, glyph);
1076 }
1077
1078
1079 /* A bit higher-level, and with fallback */
1080
1081 /**
1082  * hb_font_get_extents_for_direction:
1083  * @font: a font.
1084  * @direction:
1085  * @extents: (out):
1086  *
1087  *
1088  *
1089  * Since: 1.1.3
1090  **/
1091 void
1092 hb_font_get_extents_for_direction (hb_font_t *font,
1093                                    hb_direction_t direction,
1094                                    hb_font_extents_t *extents)
1095 {
1096   return font->get_extents_for_direction (direction, extents);
1097 }
1098 /**
1099  * hb_font_get_glyph_advance_for_direction:
1100  * @font: a font.
1101  * @glyph:
1102  * @direction:
1103  * @x: (out):
1104  * @y: (out):
1105  *
1106  *
1107  *
1108  * Since: 0.9.2
1109  **/
1110 void
1111 hb_font_get_glyph_advance_for_direction (hb_font_t *font,
1112                                          hb_codepoint_t glyph,
1113                                          hb_direction_t direction,
1114                                          hb_position_t *x, hb_position_t *y)
1115 {
1116   return font->get_glyph_advance_for_direction (glyph, direction, x, y);
1117 }
1118 /**
1119  * hb_font_get_glyph_advances_for_direction:
1120  * @font: a font.
1121  * @direction:
1122  *
1123  *
1124  *
1125  * Since: 1.8.6
1126  **/
1127 HB_EXTERN void
1128 hb_font_get_glyph_advances_for_direction (hb_font_t* font,
1129                                           hb_direction_t direction,
1130                                           unsigned int count,
1131                                           const hb_codepoint_t *first_glyph,
1132                                           unsigned glyph_stride,
1133                                           hb_position_t *first_advance,
1134                                           unsigned advance_stride)
1135 {
1136   font->get_glyph_advances_for_direction (direction, count, first_glyph, glyph_stride, first_advance, advance_stride);
1137 }
1138
1139 /**
1140  * hb_font_get_glyph_origin_for_direction:
1141  * @font: a font.
1142  * @glyph:
1143  * @direction:
1144  * @x: (out):
1145  * @y: (out):
1146  *
1147  *
1148  *
1149  * Since: 0.9.2
1150  **/
1151 void
1152 hb_font_get_glyph_origin_for_direction (hb_font_t *font,
1153                                         hb_codepoint_t glyph,
1154                                         hb_direction_t direction,
1155                                         hb_position_t *x, hb_position_t *y)
1156 {
1157   return font->get_glyph_origin_for_direction (glyph, direction, x, y);
1158 }
1159
1160 /**
1161  * hb_font_add_glyph_origin_for_direction:
1162  * @font: a font.
1163  * @glyph:
1164  * @direction:
1165  * @x: (out):
1166  * @y: (out):
1167  *
1168  *
1169  *
1170  * Since: 0.9.2
1171  **/
1172 void
1173 hb_font_add_glyph_origin_for_direction (hb_font_t *font,
1174                                         hb_codepoint_t glyph,
1175                                         hb_direction_t direction,
1176                                         hb_position_t *x, hb_position_t *y)
1177 {
1178   return font->add_glyph_origin_for_direction (glyph, direction, x, y);
1179 }
1180
1181 /**
1182  * hb_font_subtract_glyph_origin_for_direction:
1183  * @font: a font.
1184  * @glyph:
1185  * @direction:
1186  * @x: (out):
1187  * @y: (out):
1188  *
1189  *
1190  *
1191  * Since: 0.9.2
1192  **/
1193 void
1194 hb_font_subtract_glyph_origin_for_direction (hb_font_t *font,
1195                                              hb_codepoint_t glyph,
1196                                              hb_direction_t direction,
1197                                              hb_position_t *x, hb_position_t *y)
1198 {
1199   return font->subtract_glyph_origin_for_direction (glyph, direction, x, y);
1200 }
1201
1202 /**
1203  * hb_font_get_glyph_kerning_for_direction:
1204  * @font: a font.
1205  * @first_glyph:
1206  * @second_glyph:
1207  * @direction:
1208  * @x: (out):
1209  * @y: (out):
1210  *
1211  *
1212  *
1213  * Since: 0.9.2
1214  **/
1215 void
1216 hb_font_get_glyph_kerning_for_direction (hb_font_t *font,
1217                                          hb_codepoint_t first_glyph, hb_codepoint_t second_glyph,
1218                                          hb_direction_t direction,
1219                                          hb_position_t *x, hb_position_t *y)
1220 {
1221   return font->get_glyph_kerning_for_direction (first_glyph, second_glyph, direction, x, y);
1222 }
1223
1224 /**
1225  * hb_font_get_glyph_extents_for_origin:
1226  * @font: a font.
1227  * @glyph:
1228  * @direction:
1229  * @extents: (out):
1230  *
1231  *
1232  *
1233  * Return value:
1234  *
1235  * Since: 0.9.2
1236  **/
1237 hb_bool_t
1238 hb_font_get_glyph_extents_for_origin (hb_font_t *font,
1239                                       hb_codepoint_t glyph,
1240                                       hb_direction_t direction,
1241                                       hb_glyph_extents_t *extents)
1242 {
1243   return font->get_glyph_extents_for_origin (glyph, direction, extents);
1244 }
1245
1246 /**
1247  * hb_font_get_glyph_contour_point_for_origin:
1248  * @font: a font.
1249  * @glyph:
1250  * @point_index:
1251  * @direction:
1252  * @x: (out):
1253  * @y: (out):
1254  *
1255  *
1256  *
1257  * Return value:
1258  *
1259  * Since: 0.9.2
1260  **/
1261 hb_bool_t
1262 hb_font_get_glyph_contour_point_for_origin (hb_font_t *font,
1263                                             hb_codepoint_t glyph, unsigned int point_index,
1264                                             hb_direction_t direction,
1265                                             hb_position_t *x, hb_position_t *y)
1266 {
1267   return font->get_glyph_contour_point_for_origin (glyph, point_index, direction, x, y);
1268 }
1269
1270 /* Generates gidDDD if glyph has no name. */
1271 /**
1272  * hb_font_glyph_to_string:
1273  * @font: a font.
1274  * @glyph:
1275  * @s: (array length=size):
1276  * @size:
1277  *
1278  *
1279  *
1280  * Since: 0.9.2
1281  **/
1282 void
1283 hb_font_glyph_to_string (hb_font_t *font,
1284                          hb_codepoint_t glyph,
1285                          char *s, unsigned int size)
1286 {
1287   font->glyph_to_string (glyph, s, size);
1288 }
1289
1290 /* Parses gidDDD and uniUUUU strings automatically. */
1291 /**
1292  * hb_font_glyph_from_string:
1293  * @font: a font.
1294  * @s: (array length=len) (element-type uint8_t):
1295  * @len:
1296  * @glyph: (out):
1297  *
1298  *
1299  *
1300  * Return value:
1301  *
1302  * Since: 0.9.2
1303  **/
1304 hb_bool_t
1305 hb_font_glyph_from_string (hb_font_t *font,
1306                            const char *s, int len, /* -1 means nul-terminated */
1307                            hb_codepoint_t *glyph)
1308 {
1309   return font->glyph_from_string (s, len, glyph);
1310 }
1311
1312
1313 /*
1314  * hb_font_t
1315  */
1316
1317 DEFINE_NULL_INSTANCE (hb_font_t) =
1318 {
1319   HB_OBJECT_HEADER_STATIC,
1320
1321   nullptr, /* parent */
1322   const_cast<hb_face_t *> (&_hb_Null_hb_face_t),
1323
1324   1000, /* x_scale */
1325   1000, /* y_scale */
1326   1<<16, /* x_mult */
1327   1<<16, /* y_mult */
1328
1329   0, /* x_ppem */
1330   0, /* y_ppem */
1331   0, /* ptem */
1332
1333   0, /* num_coords */
1334   nullptr, /* coords */
1335
1336   const_cast<hb_font_funcs_t *> (&_hb_Null_hb_font_funcs_t),
1337
1338   /* Zero for the rest is fine. */
1339 };
1340
1341
1342 static hb_font_t *
1343 _hb_font_create (hb_face_t *face)
1344 {
1345   hb_font_t *font;
1346
1347   if (unlikely (!face))
1348     face = hb_face_get_empty ();
1349   if (!(font = hb_object_create<hb_font_t> ()))
1350     return hb_font_get_empty ();
1351
1352   hb_face_make_immutable (face);
1353   font->parent = hb_font_get_empty ();
1354   font->face = hb_face_reference (face);
1355   font->klass = hb_font_funcs_get_empty ();
1356   font->data.init0 (font);
1357   font->x_scale = font->y_scale = hb_face_get_upem (face);
1358   font->x_mult = font->y_mult = 1 << 16;
1359
1360   return font;
1361 }
1362
1363 /**
1364  * hb_font_create: (Xconstructor)
1365  * @face: a face.
1366  *
1367  *
1368  *
1369  * Return value: (transfer full):
1370  *
1371  * Since: 0.9.2
1372  **/
1373 hb_font_t *
1374 hb_font_create (hb_face_t *face)
1375 {
1376   hb_font_t *font = _hb_font_create (face);
1377
1378 #ifndef HB_NO_OT_FONT
1379   /* Install our in-house, very lightweight, funcs. */
1380   hb_ot_font_set_funcs (font);
1381 #endif
1382
1383   return font;
1384 }
1385
1386 /**
1387  * hb_font_create_sub_font:
1388  * @parent: parent font.
1389  *
1390  *
1391  *
1392  * Return value: (transfer full):
1393  *
1394  * Since: 0.9.2
1395  **/
1396 hb_font_t *
1397 hb_font_create_sub_font (hb_font_t *parent)
1398 {
1399   if (unlikely (!parent))
1400     parent = hb_font_get_empty ();
1401
1402   hb_font_t *font = _hb_font_create (parent->face);
1403
1404   if (unlikely (hb_object_is_immutable (font)))
1405     return font;
1406
1407   font->parent = hb_font_reference (parent);
1408
1409   font->x_scale = parent->x_scale;
1410   font->y_scale = parent->y_scale;
1411   font->mults_changed ();
1412   font->x_ppem = parent->x_ppem;
1413   font->y_ppem = parent->y_ppem;
1414   font->ptem = parent->ptem;
1415
1416   font->num_coords = parent->num_coords;
1417   if (font->num_coords)
1418   {
1419     unsigned int size = parent->num_coords * sizeof (parent->coords[0]);
1420     font->coords = (int *) malloc (size);
1421     if (unlikely (!font->coords))
1422       font->num_coords = 0;
1423     else
1424       memcpy (font->coords, parent->coords, size);
1425   }
1426
1427   return font;
1428 }
1429
1430 /**
1431  * hb_font_get_empty:
1432  *
1433  *
1434  *
1435  * Return value: (transfer full)
1436  *
1437  * Since: 0.9.2
1438  **/
1439 hb_font_t *
1440 hb_font_get_empty ()
1441 {
1442   return const_cast<hb_font_t *> (&Null(hb_font_t));
1443 }
1444
1445 /**
1446  * hb_font_reference: (skip)
1447  * @font: a font.
1448  *
1449  *
1450  *
1451  * Return value: (transfer full):
1452  *
1453  * Since: 0.9.2
1454  **/
1455 hb_font_t *
1456 hb_font_reference (hb_font_t *font)
1457 {
1458   return hb_object_reference (font);
1459 }
1460
1461 /**
1462  * hb_font_destroy: (skip)
1463  * @font: a font.
1464  *
1465  *
1466  *
1467  * Since: 0.9.2
1468  **/
1469 void
1470 hb_font_destroy (hb_font_t *font)
1471 {
1472   if (!hb_object_destroy (font)) return;
1473
1474   font->data.fini ();
1475
1476   if (font->destroy)
1477     font->destroy (font->user_data);
1478
1479   hb_font_destroy (font->parent);
1480   hb_face_destroy (font->face);
1481   hb_font_funcs_destroy (font->klass);
1482
1483   free (font->coords);
1484
1485   free (font);
1486 }
1487
1488 /**
1489  * hb_font_set_user_data: (skip)
1490  * @font: a font.
1491  * @key:
1492  * @data:
1493  * @destroy:
1494  * @replace:
1495  *
1496  *
1497  *
1498  * Return value:
1499  *
1500  * Since: 0.9.2
1501  **/
1502 hb_bool_t
1503 hb_font_set_user_data (hb_font_t          *font,
1504                        hb_user_data_key_t *key,
1505                        void *              data,
1506                        hb_destroy_func_t   destroy,
1507                        hb_bool_t           replace)
1508 {
1509   return hb_object_set_user_data (font, key, data, destroy, replace);
1510 }
1511
1512 /**
1513  * hb_font_get_user_data: (skip)
1514  * @font: a font.
1515  * @key:
1516  *
1517  *
1518  *
1519  * Return value: (transfer none):
1520  *
1521  * Since: 0.9.2
1522  **/
1523 void *
1524 hb_font_get_user_data (hb_font_t          *font,
1525                        hb_user_data_key_t *key)
1526 {
1527   return hb_object_get_user_data (font, key);
1528 }
1529
1530 /**
1531  * hb_font_make_immutable:
1532  * @font: a font.
1533  *
1534  *
1535  *
1536  * Since: 0.9.2
1537  **/
1538 void
1539 hb_font_make_immutable (hb_font_t *font)
1540 {
1541   if (hb_object_is_immutable (font))
1542     return;
1543
1544   if (font->parent)
1545     hb_font_make_immutable (font->parent);
1546
1547   hb_object_make_immutable (font);
1548 }
1549
1550 /**
1551  * hb_font_is_immutable:
1552  * @font: a font.
1553  *
1554  *
1555  *
1556  * Return value:
1557  *
1558  * Since: 0.9.2
1559  **/
1560 hb_bool_t
1561 hb_font_is_immutable (hb_font_t *font)
1562 {
1563   return hb_object_is_immutable (font);
1564 }
1565
1566 /**
1567  * hb_font_set_parent:
1568  * @font: a font.
1569  * @parent: new parent.
1570  *
1571  * Sets parent font of @font.
1572  *
1573  * Since: 1.0.5
1574  **/
1575 void
1576 hb_font_set_parent (hb_font_t *font,
1577                     hb_font_t *parent)
1578 {
1579   if (hb_object_is_immutable (font))
1580     return;
1581
1582   if (!parent)
1583     parent = hb_font_get_empty ();
1584
1585   hb_font_t *old = font->parent;
1586
1587   font->parent = hb_font_reference (parent);
1588
1589   hb_font_destroy (old);
1590 }
1591
1592 /**
1593  * hb_font_get_parent:
1594  * @font: a font.
1595  *
1596  *
1597  *
1598  * Return value: (transfer none):
1599  *
1600  * Since: 0.9.2
1601  **/
1602 hb_font_t *
1603 hb_font_get_parent (hb_font_t *font)
1604 {
1605   return font->parent;
1606 }
1607
1608 /**
1609  * hb_font_set_face:
1610  * @font: a font.
1611  * @face: new face.
1612  *
1613  * Sets font-face of @font.
1614  *
1615  * Since: 1.4.3
1616  **/
1617 void
1618 hb_font_set_face (hb_font_t *font,
1619                   hb_face_t *face)
1620 {
1621   if (hb_object_is_immutable (font))
1622     return;
1623
1624   if (unlikely (!face))
1625     face = hb_face_get_empty ();
1626
1627   hb_face_t *old = font->face;
1628
1629   hb_face_make_immutable (face);
1630   font->face = hb_face_reference (face);
1631   font->mults_changed ();
1632
1633   hb_face_destroy (old);
1634 }
1635
1636 /**
1637  * hb_font_get_face:
1638  * @font: a font.
1639  *
1640  *
1641  *
1642  * Return value: (transfer none):
1643  *
1644  * Since: 0.9.2
1645  **/
1646 hb_face_t *
1647 hb_font_get_face (hb_font_t *font)
1648 {
1649   return font->face;
1650 }
1651
1652
1653 /**
1654  * hb_font_set_funcs:
1655  * @font: a font.
1656  * @klass: (closure font_data) (destroy destroy) (scope notified):
1657  * @font_data:
1658  * @destroy:
1659  *
1660  *
1661  *
1662  * Since: 0.9.2
1663  **/
1664 void
1665 hb_font_set_funcs (hb_font_t         *font,
1666                    hb_font_funcs_t   *klass,
1667                    void              *font_data,
1668                    hb_destroy_func_t  destroy)
1669 {
1670   if (hb_object_is_immutable (font))
1671   {
1672     if (destroy)
1673       destroy (font_data);
1674     return;
1675   }
1676
1677   if (font->destroy)
1678     font->destroy (font->user_data);
1679
1680   if (!klass)
1681     klass = hb_font_funcs_get_empty ();
1682
1683   hb_font_funcs_reference (klass);
1684   hb_font_funcs_destroy (font->klass);
1685   font->klass = klass;
1686   font->user_data = font_data;
1687   font->destroy = destroy;
1688 }
1689
1690 /**
1691  * hb_font_set_funcs_data:
1692  * @font: a font.
1693  * @font_data: (destroy destroy) (scope notified):
1694  * @destroy:
1695  *
1696  *
1697  *
1698  * Since: 0.9.2
1699  **/
1700 void
1701 hb_font_set_funcs_data (hb_font_t         *font,
1702                         void              *font_data,
1703                         hb_destroy_func_t  destroy)
1704 {
1705   /* Destroy user_data? */
1706   if (hb_object_is_immutable (font))
1707   {
1708     if (destroy)
1709       destroy (font_data);
1710     return;
1711   }
1712
1713   if (font->destroy)
1714     font->destroy (font->user_data);
1715
1716   font->user_data = font_data;
1717   font->destroy = destroy;
1718 }
1719
1720
1721 /**
1722  * hb_font_set_scale:
1723  * @font: a font.
1724  * @x_scale:
1725  * @y_scale:
1726  *
1727  *
1728  *
1729  * Since: 0.9.2
1730  **/
1731 void
1732 hb_font_set_scale (hb_font_t *font,
1733                    int x_scale,
1734                    int y_scale)
1735 {
1736   if (hb_object_is_immutable (font))
1737     return;
1738
1739   font->x_scale = x_scale;
1740   font->y_scale = y_scale;
1741   font->mults_changed ();
1742 }
1743
1744 /**
1745  * hb_font_get_scale:
1746  * @font: a font.
1747  * @x_scale: (out):
1748  * @y_scale: (out):
1749  *
1750  *
1751  *
1752  * Since: 0.9.2
1753  **/
1754 void
1755 hb_font_get_scale (hb_font_t *font,
1756                    int *x_scale,
1757                    int *y_scale)
1758 {
1759   if (x_scale) *x_scale = font->x_scale;
1760   if (y_scale) *y_scale = font->y_scale;
1761 }
1762
1763 /**
1764  * hb_font_set_ppem:
1765  * @font: a font.
1766  * @x_ppem:
1767  * @y_ppem:
1768  *
1769  *
1770  *
1771  * Since: 0.9.2
1772  **/
1773 void
1774 hb_font_set_ppem (hb_font_t *font,
1775                   unsigned int x_ppem,
1776                   unsigned int y_ppem)
1777 {
1778   if (hb_object_is_immutable (font))
1779     return;
1780
1781   font->x_ppem = x_ppem;
1782   font->y_ppem = y_ppem;
1783 }
1784
1785 /**
1786  * hb_font_get_ppem:
1787  * @font: a font.
1788  * @x_ppem: (out):
1789  * @y_ppem: (out):
1790  *
1791  *
1792  *
1793  * Since: 0.9.2
1794  **/
1795 void
1796 hb_font_get_ppem (hb_font_t *font,
1797                   unsigned int *x_ppem,
1798                   unsigned int *y_ppem)
1799 {
1800   if (x_ppem) *x_ppem = font->x_ppem;
1801   if (y_ppem) *y_ppem = font->y_ppem;
1802 }
1803
1804 /**
1805  * hb_font_set_ptem:
1806  * @font: a font.
1807  * @ptem: font size in points.
1808  *
1809  * Sets "point size" of the font.  Set to 0 to unset.
1810  *
1811  * There are 72 points in an inch.
1812  *
1813  * Since: 1.6.0
1814  **/
1815 void
1816 hb_font_set_ptem (hb_font_t *font, float ptem)
1817 {
1818   if (hb_object_is_immutable (font))
1819     return;
1820
1821   font->ptem = ptem;
1822 }
1823
1824 /**
1825  * hb_font_get_ptem:
1826  * @font: a font.
1827  *
1828  * Gets the "point size" of the font.  A value of 0 means unset.
1829  *
1830  * Return value: Point size.
1831  *
1832  * Since: 0.9.2
1833  **/
1834 float
1835 hb_font_get_ptem (hb_font_t *font)
1836 {
1837   return font->ptem;
1838 }
1839
1840 #ifndef HB_NO_VAR
1841 /*
1842  * Variations
1843  */
1844
1845 static void
1846 _hb_font_adopt_var_coords_normalized (hb_font_t *font,
1847                                       int *coords, /* 2.14 normalized */
1848                                       unsigned int coords_length)
1849 {
1850   free (font->coords);
1851
1852   font->coords = coords;
1853   font->num_coords = coords_length;
1854 }
1855
1856 /**
1857  * hb_font_set_variations:
1858  *
1859  * Since: 1.4.2
1860  */
1861 void
1862 hb_font_set_variations (hb_font_t *font,
1863                         const hb_variation_t *variations,
1864                         unsigned int variations_length)
1865 {
1866   if (hb_object_is_immutable (font))
1867     return;
1868
1869   if (!variations_length)
1870   {
1871     hb_font_set_var_coords_normalized (font, nullptr, 0);
1872     return;
1873   }
1874
1875   unsigned int coords_length = hb_ot_var_get_axis_count (font->face);
1876
1877   int *normalized = coords_length ? (int *) calloc (coords_length, sizeof (int)) : nullptr;
1878   if (unlikely (coords_length && !normalized))
1879     return;
1880
1881   hb_ot_var_normalize_variations (font->face,
1882                                   variations, variations_length,
1883                                   normalized, coords_length);
1884   _hb_font_adopt_var_coords_normalized (font, normalized, coords_length);
1885 }
1886
1887 /**
1888  * hb_font_set_var_coords_design:
1889  *
1890  * Since: 1.4.2
1891  */
1892 void
1893 hb_font_set_var_coords_design (hb_font_t *font,
1894                                const float *coords,
1895                                unsigned int coords_length)
1896 {
1897   if (hb_object_is_immutable (font))
1898     return;
1899
1900   int *normalized = coords_length ? (int *) calloc (coords_length, sizeof (int)) : nullptr;
1901   if (unlikely (coords_length && !normalized))
1902     return;
1903
1904   hb_ot_var_normalize_coords (font->face, coords_length, coords, normalized);
1905   _hb_font_adopt_var_coords_normalized (font, normalized, coords_length);
1906 }
1907
1908 /**
1909  * hb_font_set_var_named_instance:
1910  * @font: a font.
1911  * @instance_index: named instance index.
1912  *
1913  * Sets design coords of a font from a named instance index.
1914  *
1915  * Since: 2.6.0
1916  */
1917 void
1918 hb_font_set_var_named_instance (hb_font_t *font,
1919                                 unsigned instance_index)
1920 {
1921   if (hb_object_is_immutable (font))
1922     return;
1923
1924   unsigned int coords_length = hb_ot_var_named_instance_get_design_coords (font->face, instance_index, nullptr, nullptr);
1925
1926   float *coords = coords_length ? (float *) calloc (coords_length, sizeof (float)) : nullptr;
1927   if (unlikely (coords_length && !coords))
1928     return;
1929
1930   hb_ot_var_named_instance_get_design_coords (font->face, instance_index, &coords_length, coords);
1931   hb_font_set_var_coords_design (font, coords, coords_length);
1932   free (coords);
1933 }
1934
1935 /**
1936  * hb_font_set_var_coords_normalized:
1937  *
1938  * Since: 1.4.2
1939  */
1940 void
1941 hb_font_set_var_coords_normalized (hb_font_t *font,
1942                                    const int *coords, /* 2.14 normalized */
1943                                    unsigned int coords_length)
1944 {
1945   if (hb_object_is_immutable (font))
1946     return;
1947
1948   int *copy = coords_length ? (int *) calloc (coords_length, sizeof (coords[0])) : nullptr;
1949   if (unlikely (coords_length && !copy))
1950     return;
1951
1952   if (coords_length)
1953     memcpy (copy, coords, coords_length * sizeof (coords[0]));
1954
1955   _hb_font_adopt_var_coords_normalized (font, copy, coords_length);
1956 }
1957
1958 /**
1959  * hb_font_get_var_coords_normalized:
1960  *
1961  * Return value is valid as long as variation coordinates of the font
1962  * are not modified.
1963  *
1964  * Since: 1.4.2
1965  */
1966 const int *
1967 hb_font_get_var_coords_normalized (hb_font_t *font,
1968                                    unsigned int *length)
1969 {
1970   if (length)
1971     *length = font->num_coords;
1972
1973   return font->coords;
1974 }
1975 #endif
1976
1977 #ifndef HB_DISABLE_DEPRECATED
1978 /*
1979  * Deprecated get_glyph_func():
1980  */
1981
1982 struct hb_trampoline_closure_t
1983 {
1984   void *user_data;
1985   hb_destroy_func_t destroy;
1986   unsigned int ref_count;
1987 };
1988
1989 template <typename FuncType>
1990 struct hb_trampoline_t
1991 {
1992   hb_trampoline_closure_t closure; /* Must be first. */
1993   FuncType func;
1994 };
1995
1996 template <typename FuncType>
1997 static hb_trampoline_t<FuncType> *
1998 trampoline_create (FuncType           func,
1999                    void              *user_data,
2000                    hb_destroy_func_t  destroy)
2001 {
2002   typedef hb_trampoline_t<FuncType> trampoline_t;
2003
2004   trampoline_t *trampoline = (trampoline_t *) calloc (1, sizeof (trampoline_t));
2005
2006   if (unlikely (!trampoline))
2007     return nullptr;
2008
2009   trampoline->closure.user_data = user_data;
2010   trampoline->closure.destroy = destroy;
2011   trampoline->closure.ref_count = 1;
2012   trampoline->func = func;
2013
2014   return trampoline;
2015 }
2016
2017 static void
2018 trampoline_reference (hb_trampoline_closure_t *closure)
2019 {
2020   closure->ref_count++;
2021 }
2022
2023 static void
2024 trampoline_destroy (void *user_data)
2025 {
2026   hb_trampoline_closure_t *closure = (hb_trampoline_closure_t *) user_data;
2027
2028   if (--closure->ref_count)
2029     return;
2030
2031   if (closure->destroy)
2032     closure->destroy (closure->user_data);
2033   free (closure);
2034 }
2035
2036 typedef hb_trampoline_t<hb_font_get_glyph_func_t> hb_font_get_glyph_trampoline_t;
2037
2038 static hb_bool_t
2039 hb_font_get_nominal_glyph_trampoline (hb_font_t *font,
2040                                       void *font_data,
2041                                       hb_codepoint_t unicode,
2042                                       hb_codepoint_t *glyph,
2043                                       void *user_data)
2044 {
2045   hb_font_get_glyph_trampoline_t *trampoline = (hb_font_get_glyph_trampoline_t *) user_data;
2046   return trampoline->func (font, font_data, unicode, 0, glyph, trampoline->closure.user_data);
2047 }
2048
2049 static hb_bool_t
2050 hb_font_get_variation_glyph_trampoline (hb_font_t *font,
2051                                         void *font_data,
2052                                         hb_codepoint_t unicode,
2053                                         hb_codepoint_t variation_selector,
2054                                         hb_codepoint_t *glyph,
2055                                         void *user_data)
2056 {
2057   hb_font_get_glyph_trampoline_t *trampoline = (hb_font_get_glyph_trampoline_t *) user_data;
2058   return trampoline->func (font, font_data, unicode, variation_selector, glyph, trampoline->closure.user_data);
2059 }
2060
2061 /**
2062  * hb_font_funcs_set_glyph_func:
2063  * @ffuncs: font functions.
2064  * @func: (closure user_data) (destroy destroy) (scope notified): callback function.
2065  * @user_data: data to pass to @func.
2066  * @destroy: function to call when @user_data is not needed anymore.
2067  *
2068  * Deprecated.  Use hb_font_funcs_set_nominal_glyph_func() and
2069  * hb_font_funcs_set_variation_glyph_func() instead.
2070  *
2071  * Since: 0.9.2
2072  * Deprecated: 1.2.3
2073  **/
2074 void
2075 hb_font_funcs_set_glyph_func (hb_font_funcs_t *ffuncs,
2076                               hb_font_get_glyph_func_t func,
2077                               void *user_data, hb_destroy_func_t destroy)
2078 {
2079   hb_font_get_glyph_trampoline_t *trampoline;
2080
2081   trampoline = trampoline_create (func, user_data, destroy);
2082   if (unlikely (!trampoline))
2083   {
2084     if (destroy)
2085       destroy (user_data);
2086     return;
2087   }
2088
2089   hb_font_funcs_set_nominal_glyph_func (ffuncs,
2090                                         hb_font_get_nominal_glyph_trampoline,
2091                                         trampoline,
2092                                         trampoline_destroy);
2093
2094   trampoline_reference (&trampoline->closure);
2095   hb_font_funcs_set_variation_glyph_func (ffuncs,
2096                                           hb_font_get_variation_glyph_trampoline,
2097                                           trampoline,
2098                                           trampoline_destroy);
2099 }
2100 #endif