eaa1311d445fb2d033267efbe2800d0395d9f4d6
[platform/upstream/harfbuzz.git] / src / hb-ft.cc
1 /*
2  * Copyright © 2009  Red Hat, Inc.
3  * Copyright © 2009  Keith Stribley
4  * Copyright © 2015  Google, Inc.
5  *
6  *  This is part of HarfBuzz, a text shaping library.
7  *
8  * Permission is hereby granted, without written agreement and without
9  * license or royalty fees, to use, copy, modify, and distribute this
10  * software and its documentation for any purpose, provided that the
11  * above copyright notice and the following two paragraphs appear in
12  * all copies of this software.
13  *
14  * IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE TO ANY PARTY FOR
15  * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
16  * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN
17  * IF THE COPYRIGHT HOLDER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH
18  * DAMAGE.
19  *
20  * THE COPYRIGHT HOLDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING,
21  * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
22  * FITNESS FOR A PARTICULAR PURPOSE.  THE SOFTWARE PROVIDED HEREUNDER IS
23  * ON AN "AS IS" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO
24  * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
25  *
26  * Red Hat Author(s): Behdad Esfahbod
27  * Google Author(s): Behdad Esfahbod
28  */
29
30 #include "hb-private.hh"
31
32 #include "hb-ft.h"
33
34 #include "hb-font-private.hh"
35
36 #include FT_ADVANCES_H
37 #include FT_TRUETYPE_TABLES_H
38
39
40
41 #ifndef HB_DEBUG_FT
42 #define HB_DEBUG_FT (HB_DEBUG+0)
43 #endif
44
45
46 /* TODO:
47  *
48  * In general, this file does a fine job of what it's supposed to do.
49  * There are, however, things that need more work:
50  *
51  *   - I remember seeing FT_Get_Advance() without the NO_HINTING flag to be buggy.
52  *     Have not investigated.
53  *
54  *   - FreeType works in 26.6 mode.  Clients can decide to use that mode, and everything
55  *     would work fine.  However, we also abuse this API for performing in font-space,
56  *     but don't pass the correct flags to FreeType.  We just abuse the no-hinting mode
57  *     for that, such that no rounding etc happens.  As such, we don't set ppem, and
58  *     pass NO_HINTING as load_flags.  Would be much better to use NO_SCALE, and scale
59  *     ourselves, like we do in uniscribe, etc.
60  *
61  *   - We don't handle / allow for emboldening / obliqueing.
62  *
63  *   - In the future, we should add constructors to create fonts in font space?
64  *
65  *   - FT_Load_Glyph() is exteremely costly.  Do something about it?
66  */
67
68
69 struct hb_ft_font_t
70 {
71   FT_Face ft_face;
72   int load_flags;
73   bool symbol; /* Whether selected cmap is symbol cmap. */
74   bool unref; /* Whether to destroy ft_face when done. */
75 };
76
77 static hb_ft_font_t *
78 _hb_ft_font_create (FT_Face ft_face, bool symbol, bool unref)
79 {
80   hb_ft_font_t *ft_font = (hb_ft_font_t *) calloc (1, sizeof (hb_ft_font_t));
81
82   if (unlikely (!ft_font))
83     return NULL;
84
85   ft_font->ft_face = ft_face;
86   ft_font->symbol = symbol;
87   ft_font->unref = unref;
88
89   ft_font->load_flags = FT_LOAD_DEFAULT | FT_LOAD_NO_HINTING;
90
91   return ft_font;
92 }
93
94 static void
95 _hb_ft_face_destroy (FT_Face ft_face)
96 {
97   FT_Done_Face (ft_face);
98 }
99
100 static void
101 _hb_ft_font_destroy (hb_ft_font_t *ft_font)
102 {
103   if (ft_font->unref)
104     _hb_ft_face_destroy (ft_font->ft_face);
105
106   free (ft_font);
107 }
108
109 /**
110  * hb_ft_font_set_load_flags:
111  * @font:
112  * @load_flags:
113  *
114  * 
115  *
116  * Since: 1.0.5
117  **/
118 void
119 hb_ft_font_set_load_flags (hb_font_t *font, int load_flags)
120 {
121   if (font->immutable)
122     return;
123
124   if (font->destroy != (hb_destroy_func_t) _hb_ft_font_destroy)
125     return;
126
127   hb_ft_font_t *ft_font = (hb_ft_font_t *) font->user_data;
128
129   ft_font->load_flags = load_flags;
130 }
131
132 /**
133  * hb_ft_font_get_load_flags:
134  * @font:
135  *
136  * 
137  *
138  * Return value:
139  * Since: 1.0.5
140  **/
141 int
142 hb_ft_font_get_load_flags (hb_font_t *font)
143 {
144   if (font->destroy != (hb_destroy_func_t) _hb_ft_font_destroy)
145     return 0;
146
147   const hb_ft_font_t *ft_font = (const hb_ft_font_t *) font->user_data;
148
149   return ft_font->load_flags;
150 }
151
152 FT_Face
153 hb_ft_font_get_face (hb_font_t *font)
154 {
155   if (font->destroy != (hb_destroy_func_t) _hb_ft_font_destroy)
156     return NULL;
157
158   const hb_ft_font_t *ft_font = (const hb_ft_font_t *) font->user_data;
159
160   return ft_font->ft_face;
161 }
162
163
164
165 static hb_bool_t
166 hb_ft_get_nominal_glyph (hb_font_t *font HB_UNUSED,
167                          void *font_data,
168                          hb_codepoint_t unicode,
169                          hb_codepoint_t *glyph,
170                          void *user_data HB_UNUSED)
171 {
172   const hb_ft_font_t *ft_font = (const hb_ft_font_t *) font_data;
173   unsigned int g = FT_Get_Char_Index (ft_font->ft_face, unicode);
174
175   if (unlikely (!g))
176   {
177     if (unlikely (ft_font->symbol) && unicode <= 0x00FFu)
178     {
179       /* For symbol-encoded OpenType fonts, we duplicate the
180        * U+F000..F0FF range at U+0000..U+00FF.  That's what
181        * Windows seems to do, and that's hinted about at:
182        * http://www.microsoft.com/typography/otspec/recom.htm
183        * under "Non-Standard (Symbol) Fonts". */
184       g = FT_Get_Char_Index (ft_font->ft_face, 0xF000u + unicode);
185       if (!g)
186         return false;
187     }
188     else
189       return false;
190   }
191
192   *glyph = g;
193   return true;
194 }
195
196 static hb_bool_t
197 hb_ft_get_variation_glyph (hb_font_t *font HB_UNUSED,
198                            void *font_data,
199                            hb_codepoint_t unicode,
200                            hb_codepoint_t variation_selector,
201                            hb_codepoint_t *glyph,
202                            void *user_data HB_UNUSED)
203 {
204   const hb_ft_font_t *ft_font = (const hb_ft_font_t *) font_data;
205   unsigned int g = FT_Face_GetCharVariantIndex (ft_font->ft_face, unicode, variation_selector);
206
207   if (unlikely (!g))
208     return false;
209
210   *glyph = g;
211   return true;
212 }
213
214 static hb_position_t
215 hb_ft_get_glyph_h_advance (hb_font_t *font HB_UNUSED,
216                            void *font_data,
217                            hb_codepoint_t glyph,
218                            void *user_data HB_UNUSED)
219 {
220   const hb_ft_font_t *ft_font = (const hb_ft_font_t *) font_data;
221   FT_Fixed v;
222
223   if (unlikely (FT_Get_Advance (ft_font->ft_face, glyph, ft_font->load_flags, &v)))
224     return 0;
225
226   if (font->x_scale < 0)
227     v = -v;
228
229   return (v + (1<<9)) >> 10;
230 }
231
232 static hb_position_t
233 hb_ft_get_glyph_v_advance (hb_font_t *font HB_UNUSED,
234                            void *font_data,
235                            hb_codepoint_t glyph,
236                            void *user_data HB_UNUSED)
237 {
238   const hb_ft_font_t *ft_font = (const hb_ft_font_t *) font_data;
239   FT_Fixed v;
240
241   if (unlikely (FT_Get_Advance (ft_font->ft_face, glyph, ft_font->load_flags | FT_LOAD_VERTICAL_LAYOUT, &v)))
242     return 0;
243
244   if (font->y_scale < 0)
245     v = -v;
246
247   /* Note: FreeType's vertical metrics grows downward while other FreeType coordinates
248    * have a Y growing upward.  Hence the extra negation. */
249   return (-v + (1<<9)) >> 10;
250 }
251
252 static hb_bool_t
253 hb_ft_get_glyph_v_origin (hb_font_t *font HB_UNUSED,
254                           void *font_data,
255                           hb_codepoint_t glyph,
256                           hb_position_t *x,
257                           hb_position_t *y,
258                           void *user_data HB_UNUSED)
259 {
260   const hb_ft_font_t *ft_font = (const hb_ft_font_t *) font_data;
261   FT_Face ft_face = ft_font->ft_face;
262
263   if (unlikely (FT_Load_Glyph (ft_face, glyph, ft_font->load_flags)))
264     return false;
265
266   /* Note: FreeType's vertical metrics grows downward while other FreeType coordinates
267    * have a Y growing upward.  Hence the extra negation. */
268   *x = ft_face->glyph->metrics.horiBearingX -   ft_face->glyph->metrics.vertBearingX;
269   *y = ft_face->glyph->metrics.horiBearingY - (-ft_face->glyph->metrics.vertBearingY);
270
271   if (font->x_scale < 0)
272     *x = -*x;
273   if (font->y_scale < 0)
274     *y = -*y;
275
276   return true;
277 }
278
279 static hb_position_t
280 hb_ft_get_glyph_h_kerning (hb_font_t *font,
281                            void *font_data,
282                            hb_codepoint_t left_glyph,
283                            hb_codepoint_t right_glyph,
284                            void *user_data HB_UNUSED)
285 {
286   const hb_ft_font_t *ft_font = (const hb_ft_font_t *) font_data;
287   FT_Vector kerningv;
288
289   FT_Kerning_Mode mode = font->x_ppem ? FT_KERNING_DEFAULT : FT_KERNING_UNFITTED;
290   if (FT_Get_Kerning (ft_font->ft_face, left_glyph, right_glyph, mode, &kerningv))
291     return 0;
292
293   return kerningv.x;
294 }
295
296 static hb_bool_t
297 hb_ft_get_glyph_extents (hb_font_t *font HB_UNUSED,
298                          void *font_data,
299                          hb_codepoint_t glyph,
300                          hb_glyph_extents_t *extents,
301                          void *user_data HB_UNUSED)
302 {
303   const hb_ft_font_t *ft_font = (const hb_ft_font_t *) font_data;
304   FT_Face ft_face = ft_font->ft_face;
305
306   if (unlikely (FT_Load_Glyph (ft_face, glyph, ft_font->load_flags)))
307     return false;
308
309   extents->x_bearing = ft_face->glyph->metrics.horiBearingX;
310   extents->y_bearing = ft_face->glyph->metrics.horiBearingY;
311   extents->width = ft_face->glyph->metrics.width;
312   extents->height = -ft_face->glyph->metrics.height;
313   if (font->x_scale < 0)
314   {
315     extents->x_bearing = -extents->x_bearing;
316     extents->width = -extents->width;
317   }
318   if (font->y_scale < 0)
319   {
320     extents->y_bearing = -extents->y_bearing;
321     extents->height = -extents->height;
322   }
323   return true;
324 }
325
326 static hb_bool_t
327 hb_ft_get_glyph_contour_point (hb_font_t *font HB_UNUSED,
328                                void *font_data,
329                                hb_codepoint_t glyph,
330                                unsigned int point_index,
331                                hb_position_t *x,
332                                hb_position_t *y,
333                                void *user_data HB_UNUSED)
334 {
335   const hb_ft_font_t *ft_font = (const hb_ft_font_t *) font_data;
336   FT_Face ft_face = ft_font->ft_face;
337
338   if (unlikely (FT_Load_Glyph (ft_face, glyph, ft_font->load_flags)))
339       return false;
340
341   if (unlikely (ft_face->glyph->format != FT_GLYPH_FORMAT_OUTLINE))
342       return false;
343
344   if (unlikely (point_index >= (unsigned int) ft_face->glyph->outline.n_points))
345       return false;
346
347   *x = ft_face->glyph->outline.points[point_index].x;
348   *y = ft_face->glyph->outline.points[point_index].y;
349
350   return true;
351 }
352
353 static hb_bool_t
354 hb_ft_get_glyph_name (hb_font_t *font HB_UNUSED,
355                       void *font_data,
356                       hb_codepoint_t glyph,
357                       char *name, unsigned int size,
358                       void *user_data HB_UNUSED)
359 {
360   const hb_ft_font_t *ft_font = (const hb_ft_font_t *) font_data;
361
362   hb_bool_t ret = !FT_Get_Glyph_Name (ft_font->ft_face, glyph, name, size);
363   if (ret && (size && !*name))
364     ret = false;
365
366   return ret;
367 }
368
369 static hb_bool_t
370 hb_ft_get_glyph_from_name (hb_font_t *font HB_UNUSED,
371                            void *font_data,
372                            const char *name, int len, /* -1 means nul-terminated */
373                            hb_codepoint_t *glyph,
374                            void *user_data HB_UNUSED)
375 {
376   const hb_ft_font_t *ft_font = (const hb_ft_font_t *) font_data;
377   FT_Face ft_face = ft_font->ft_face;
378
379   if (len < 0)
380     *glyph = FT_Get_Name_Index (ft_face, (FT_String *) name);
381   else {
382     /* Make a nul-terminated version. */
383     char buf[128];
384     len = MIN (len, (int) sizeof (buf) - 1);
385     strncpy (buf, name, len);
386     buf[len] = '\0';
387     *glyph = FT_Get_Name_Index (ft_face, buf);
388   }
389
390   if (*glyph == 0)
391   {
392     /* Check whether the given name was actually the name of glyph 0. */
393     char buf[128];
394     if (!FT_Get_Glyph_Name(ft_face, 0, buf, sizeof (buf)) &&
395         len < 0 ? !strcmp (buf, name) : !strncmp (buf, name, len))
396       return true;
397   }
398
399   return *glyph != 0;
400 }
401
402 static hb_bool_t
403 hb_ft_get_font_h_extents (hb_font_t *font HB_UNUSED,
404                           void *font_data,
405                           hb_font_extents_t *metrics,
406                           void *user_data HB_UNUSED)
407 {
408   const hb_ft_font_t *ft_font = (const hb_ft_font_t *) font_data;
409   FT_Face ft_face = ft_font->ft_face;
410   metrics->ascender = ft_face->size->metrics.ascender;
411   metrics->descender = ft_face->size->metrics.descender;
412   metrics->line_gap = ft_face->size->metrics.height - (ft_face->size->metrics.ascender - ft_face->size->metrics.descender);
413   if (font->y_scale < 0)
414   {
415     metrics->ascender = -metrics->ascender;
416     metrics->descender = -metrics->descender;
417     metrics->line_gap = -metrics->line_gap;
418   }
419   return true;
420 }
421
422 static hb_font_funcs_t *static_ft_funcs = NULL;
423
424 #ifdef HB_USE_ATEXIT
425 static
426 void free_static_ft_funcs (void)
427 {
428   hb_font_funcs_destroy (static_ft_funcs);
429 }
430 #endif
431
432 static void
433 _hb_ft_font_set_funcs (hb_font_t *font, FT_Face ft_face, bool unref)
434 {
435 retry:
436   hb_font_funcs_t *funcs = (hb_font_funcs_t *) hb_atomic_ptr_get (&static_ft_funcs);
437
438   if (unlikely (!funcs))
439   {
440     funcs = hb_font_funcs_create ();
441
442     hb_font_funcs_set_font_h_extents_func (funcs, hb_ft_get_font_h_extents, NULL, NULL);
443     //hb_font_funcs_set_font_v_extents_func (funcs, hb_ft_get_font_v_extents, NULL, NULL);
444     hb_font_funcs_set_nominal_glyph_func (funcs, hb_ft_get_nominal_glyph, NULL, NULL);
445     hb_font_funcs_set_variation_glyph_func (funcs, hb_ft_get_variation_glyph, NULL, NULL);
446     hb_font_funcs_set_glyph_h_advance_func (funcs, hb_ft_get_glyph_h_advance, NULL, NULL);
447     hb_font_funcs_set_glyph_v_advance_func (funcs, hb_ft_get_glyph_v_advance, NULL, NULL);
448     //hb_font_funcs_set_glyph_h_origin_func (funcs, hb_ft_get_glyph_h_origin, NULL, NULL);
449     hb_font_funcs_set_glyph_v_origin_func (funcs, hb_ft_get_glyph_v_origin, NULL, NULL);
450     hb_font_funcs_set_glyph_h_kerning_func (funcs, hb_ft_get_glyph_h_kerning, NULL, NULL);
451     //hb_font_funcs_set_glyph_v_kerning_func (funcs, hb_ft_get_glyph_v_kerning, NULL, NULL);
452     hb_font_funcs_set_glyph_extents_func (funcs, hb_ft_get_glyph_extents, NULL, NULL);
453     hb_font_funcs_set_glyph_contour_point_func (funcs, hb_ft_get_glyph_contour_point, NULL, NULL);
454     hb_font_funcs_set_glyph_name_func (funcs, hb_ft_get_glyph_name, NULL, NULL);
455     hb_font_funcs_set_glyph_from_name_func (funcs, hb_ft_get_glyph_from_name, NULL, NULL);
456
457     hb_font_funcs_make_immutable (funcs);
458
459     if (!hb_atomic_ptr_cmpexch (&static_ft_funcs, NULL, funcs)) {
460       hb_font_funcs_destroy (funcs);
461       goto retry;
462     }
463
464 #ifdef HB_USE_ATEXIT
465     atexit (free_static_ft_funcs); /* First person registers atexit() callback. */
466 #endif
467   };
468
469   bool symbol = ft_face->charmap && ft_face->charmap->encoding == FT_ENCODING_MS_SYMBOL;
470
471   hb_font_set_funcs (font,
472                      funcs,
473                      _hb_ft_font_create (ft_face, symbol, unref),
474                      (hb_destroy_func_t) _hb_ft_font_destroy);
475 }
476
477
478 static hb_blob_t *
479 reference_table  (hb_face_t *face HB_UNUSED, hb_tag_t tag, void *user_data)
480 {
481   FT_Face ft_face = (FT_Face) user_data;
482   FT_Byte *buffer;
483   FT_ULong  length = 0;
484   FT_Error error;
485
486   /* Note: FreeType like HarfBuzz uses the NONE tag for fetching the entire blob */
487
488   error = FT_Load_Sfnt_Table (ft_face, tag, 0, NULL, &length);
489   if (error)
490     return NULL;
491
492   buffer = (FT_Byte *) malloc (length);
493   if (buffer == NULL)
494     return NULL;
495
496   error = FT_Load_Sfnt_Table (ft_face, tag, 0, buffer, &length);
497   if (error)
498     return NULL;
499
500   return hb_blob_create ((const char *) buffer, length,
501                          HB_MEMORY_MODE_WRITABLE,
502                          buffer, free);
503 }
504
505 /**
506  * hb_ft_face_create:
507  * @ft_face: (destroy destroy) (scope notified): 
508  * @destroy:
509  *
510  * 
511  *
512  * Return value: (transfer full): 
513  * Since: 0.9.2
514  **/
515 hb_face_t *
516 hb_ft_face_create (FT_Face           ft_face,
517                    hb_destroy_func_t destroy)
518 {
519   hb_face_t *face;
520
521   if (ft_face->stream->read == NULL) {
522     hb_blob_t *blob;
523
524     blob = hb_blob_create ((const char *) ft_face->stream->base,
525                            (unsigned int) ft_face->stream->size,
526                            HB_MEMORY_MODE_READONLY,
527                            ft_face, destroy);
528     face = hb_face_create (blob, ft_face->face_index);
529     hb_blob_destroy (blob);
530   } else {
531     face = hb_face_create_for_tables (reference_table, ft_face, destroy);
532   }
533
534   hb_face_set_index (face, ft_face->face_index);
535   hb_face_set_upem (face, ft_face->units_per_EM);
536
537   return face;
538 }
539
540 /**
541  * hb_ft_face_create_referenced:
542  * @ft_face:
543  *
544  * 
545  *
546  * Return value: (transfer full): 
547  * Since: 0.9.38
548  **/
549 hb_face_t *
550 hb_ft_face_create_referenced (FT_Face ft_face)
551 {
552   FT_Reference_Face (ft_face);
553   return hb_ft_face_create (ft_face, (hb_destroy_func_t) _hb_ft_face_destroy);
554 }
555
556 static void
557 hb_ft_face_finalize (FT_Face ft_face)
558 {
559   hb_face_destroy ((hb_face_t *) ft_face->generic.data);
560 }
561
562 /**
563  * hb_ft_face_create_cached:
564  * @ft_face: 
565  *
566  * 
567  *
568  * Return value: (transfer full): 
569  * Since: 0.9.2
570  **/
571 hb_face_t *
572 hb_ft_face_create_cached (FT_Face ft_face)
573 {
574   if (unlikely (!ft_face->generic.data || ft_face->generic.finalizer != (FT_Generic_Finalizer) hb_ft_face_finalize))
575   {
576     if (ft_face->generic.finalizer)
577       ft_face->generic.finalizer (ft_face);
578
579     ft_face->generic.data = hb_ft_face_create (ft_face, NULL);
580     ft_face->generic.finalizer = (FT_Generic_Finalizer) hb_ft_face_finalize;
581   }
582
583   return hb_face_reference ((hb_face_t *) ft_face->generic.data);
584 }
585
586
587 /**
588  * hb_ft_font_create:
589  * @ft_face: (destroy destroy) (scope notified): 
590  * @destroy:
591  *
592  * 
593  *
594  * Return value: (transfer full): 
595  * Since: 0.9.2
596  **/
597 hb_font_t *
598 hb_ft_font_create (FT_Face           ft_face,
599                    hb_destroy_func_t destroy)
600 {
601   hb_font_t *font;
602   hb_face_t *face;
603
604   face = hb_ft_face_create (ft_face, destroy);
605   font = hb_font_create (face);
606   hb_face_destroy (face);
607   _hb_ft_font_set_funcs (font, ft_face, false);
608   hb_font_set_scale (font,
609                      (int) (((uint64_t) ft_face->size->metrics.x_scale * (uint64_t) ft_face->units_per_EM + (1<<15)) >> 16),
610                      (int) (((uint64_t) ft_face->size->metrics.y_scale * (uint64_t) ft_face->units_per_EM + (1<<15)) >> 16));
611 #if 0 /* hb-ft works in no-hinting model */
612   hb_font_set_ppem (font,
613                     ft_face->size->metrics.x_ppem,
614                     ft_face->size->metrics.y_ppem);
615 #endif
616
617   return font;
618 }
619
620 /**
621  * hb_ft_font_create_referenced:
622  * @ft_face:
623  *
624  * 
625  *
626  * Return value: (transfer full): 
627  * Since: 0.9.38
628  **/
629 hb_font_t *
630 hb_ft_font_create_referenced (FT_Face ft_face)
631 {
632   FT_Reference_Face (ft_face);
633   return hb_ft_font_create (ft_face, (hb_destroy_func_t) _hb_ft_face_destroy);
634 }
635
636
637 /* Thread-safe, lock-free, FT_Library */
638
639 static FT_Library ft_library;
640
641 #ifdef HB_USE_ATEXIT
642 static
643 void free_ft_library (void)
644 {
645   FT_Done_FreeType (ft_library);
646 }
647 #endif
648
649 static FT_Library
650 get_ft_library (void)
651 {
652 retry:
653   FT_Library library = (FT_Library) hb_atomic_ptr_get (&ft_library);
654
655   if (unlikely (!library))
656   {
657     /* Not found; allocate one. */
658     if (FT_Init_FreeType (&library))
659       return NULL;
660
661     if (!hb_atomic_ptr_cmpexch (&ft_library, NULL, library)) {
662       FT_Done_FreeType (library);
663       goto retry;
664     }
665
666 #ifdef HB_USE_ATEXIT
667     atexit (free_ft_library); /* First person registers atexit() callback. */
668 #endif
669   }
670
671   return library;
672 }
673
674 static void
675 _release_blob (FT_Face ft_face)
676 {
677   hb_blob_destroy ((hb_blob_t *) ft_face->generic.data);
678 }
679
680 void
681 hb_ft_font_set_funcs (hb_font_t *font)
682 {
683   hb_blob_t *blob = hb_face_reference_blob (font->face);
684   unsigned int blob_length;
685   const char *blob_data = hb_blob_get_data (blob, &blob_length);
686   if (unlikely (!blob_length))
687     DEBUG_MSG (FT, font, "Font face has empty blob");
688
689   FT_Face ft_face = NULL;
690   FT_Error err = FT_New_Memory_Face (get_ft_library (),
691                                      (const FT_Byte *) blob_data,
692                                      blob_length,
693                                      hb_face_get_index (font->face),
694                                      &ft_face);
695
696   if (unlikely (err)) {
697     hb_blob_destroy (blob);
698     DEBUG_MSG (FT, font, "Font face FT_New_Memory_Face() failed");
699     return;
700   }
701
702   if (FT_Select_Charmap (ft_face, FT_ENCODING_UNICODE))
703     FT_Select_Charmap (ft_face, FT_ENCODING_MS_SYMBOL);
704
705   FT_Set_Char_Size (ft_face,
706                     abs (font->x_scale), abs (font->y_scale),
707                     0, 0);
708 #if 0
709                     font->x_ppem * 72 * 64 / font->x_scale,
710                     font->y_ppem * 72 * 64 / font->y_scale);
711 #endif
712   if (font->x_scale < 0 || font->y_scale < 0)
713   {
714     FT_Matrix matrix = { font->x_scale < 0 ? -1 : +1, 0,
715                           0, font->y_scale < 0 ? -1 : +1};
716     FT_Set_Transform (ft_face, &matrix, NULL);
717   }
718
719   ft_face->generic.data = blob;
720   ft_face->generic.finalizer = (FT_Generic_Finalizer) _release_blob;
721
722   _hb_ft_font_set_funcs (font, ft_face, true);
723   hb_ft_font_set_load_flags (font, FT_LOAD_DEFAULT | FT_LOAD_NO_HINTING);
724 }