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.hh"
31
32 #ifdef HAVE_FREETYPE
33
34 #include "hb-ft.h"
35
36 #include "hb-font.hh"
37 #include "hb-machinery.hh"
38 #include "hb-cache.hh"
39
40 #include FT_ADVANCES_H
41 #include FT_MULTIPLE_MASTERS_H
42 #include FT_TRUETYPE_TABLES_H
43
44
45 /**
46 * SECTION:hb-ft
47 * @title: hb-ft
48 * @short_description: FreeType integration
49 * @include: hb-ft.h
50 *
51 * Functions for using HarfBuzz with the FreeType library.
52 *
53 * HarfBuzz supports using FreeType to provide face and
54 * font data.
55 *
56 * <note>Note that FreeType is not thread-safe, therefore these
57 * functions are not thread-safe either.</note>
58 **/
59
60
61 /* TODO:
62 *
63 * In general, this file does a fine job of what it's supposed to do.
64 * There are, however, things that need more work:
65 *
66 * - FreeType works in 26.6 mode. Clients can decide to use that mode, and everything
67 * would work fine. However, we also abuse this API for performing in font-space,
68 * but don't pass the correct flags to FreeType. We just abuse the no-hinting mode
69 * for that, such that no rounding etc happens. As such, we don't set ppem, and
70 * pass NO_HINTING as load_flags. Would be much better to use NO_SCALE, and scale
71 * ourselves.
72 *
73 * - We don't handle / allow for emboldening / obliqueing.
74 *
75 * - In the future, we should add constructors to create fonts in font space?
76 */
77
78
79 struct hb_ft_font_t
80 {
81 mutable hb_mutex_t lock;
82 FT_Face ft_face;
83 int load_flags;
84 bool symbol; /* Whether selected cmap is symbol cmap. */
85 bool unref; /* Whether to destroy ft_face when done. */
86
87 mutable int cached_x_scale;
88 mutable hb_advance_cache_t advance_cache;
89 };
90
91 static hb_ft_font_t *
_hb_ft_font_create(FT_Face ft_face,bool symbol,bool unref)92 _hb_ft_font_create (FT_Face ft_face, bool symbol, bool unref)
93 {
94 hb_ft_font_t *ft_font = (hb_ft_font_t *) hb_calloc (1, sizeof (hb_ft_font_t));
95 if (unlikely (!ft_font)) return nullptr;
96
97 ft_font->lock.init ();
98 ft_font->ft_face = ft_face;
99 ft_font->symbol = symbol;
100 ft_font->unref = unref;
101
102 ft_font->load_flags = FT_LOAD_DEFAULT | FT_LOAD_NO_HINTING;
103
104 ft_font->cached_x_scale = 0;
105 ft_font->advance_cache.init ();
106
107 return ft_font;
108 }
109
110 static void
_hb_ft_face_destroy(void * data)111 _hb_ft_face_destroy (void *data)
112 {
113 FT_Done_Face ((FT_Face) data);
114 }
115
116 static void
_hb_ft_font_destroy(void * data)117 _hb_ft_font_destroy (void *data)
118 {
119 hb_ft_font_t *ft_font = (hb_ft_font_t *) data;
120
121 ft_font->advance_cache.fini ();
122
123 if (ft_font->unref)
124 _hb_ft_face_destroy (ft_font->ft_face);
125
126 ft_font->lock.fini ();
127
128 hb_free (ft_font);
129 }
130
131 /**
132 * hb_ft_font_set_load_flags:
133 * @font: #hb_font_t to work upon
134 * @load_flags: The FreeType load flags to set
135 *
136 * Sets the FT_Load_Glyph load flags for the specified #hb_font_t.
137 *
138 * For more information, see
139 * https://www.freetype.org/freetype2/docs/reference/ft2-base_interface.html#ft_load_xxx
140 *
141 * Since: 1.0.5
142 **/
143 void
hb_ft_font_set_load_flags(hb_font_t * font,int load_flags)144 hb_ft_font_set_load_flags (hb_font_t *font, int load_flags)
145 {
146 if (hb_object_is_immutable (font))
147 return;
148
149 if (unlikely (font->destroy != (hb_destroy_func_t) _hb_ft_font_destroy))
150 return;
151
152 hb_ft_font_t *ft_font = (hb_ft_font_t *) font->user_data;
153
154 ft_font->load_flags = load_flags;
155 }
156
157 /**
158 * hb_ft_font_get_load_flags:
159 * @font: #hb_font_t to work upon
160 *
161 * Fetches the FT_Load_Glyph load flags of the specified #hb_font_t.
162 *
163 * For more information, see
164 * https://www.freetype.org/freetype2/docs/reference/ft2-base_interface.html#ft_load_xxx
165 *
166 * Return value: FT_Load_Glyph flags found
167 *
168 * Since: 1.0.5
169 **/
170 int
hb_ft_font_get_load_flags(hb_font_t * font)171 hb_ft_font_get_load_flags (hb_font_t *font)
172 {
173 if (unlikely (font->destroy != (hb_destroy_func_t) _hb_ft_font_destroy))
174 return 0;
175
176 const hb_ft_font_t *ft_font = (const hb_ft_font_t *) font->user_data;
177
178 return ft_font->load_flags;
179 }
180
181 /**
182 * hb_ft_font_get_face:
183 * @font: #hb_font_t to work upon
184 *
185 * Fetches the FT_Face associated with the specified #hb_font_t
186 * font object.
187 *
188 * Return value: (nullable): the FT_Face found or %NULL
189 *
190 * Since: 0.9.2
191 **/
192 FT_Face
hb_ft_font_get_face(hb_font_t * font)193 hb_ft_font_get_face (hb_font_t *font)
194 {
195 if (unlikely (font->destroy != (hb_destroy_func_t) _hb_ft_font_destroy))
196 return nullptr;
197
198 const hb_ft_font_t *ft_font = (const hb_ft_font_t *) font->user_data;
199
200 return ft_font->ft_face;
201 }
202
203 /**
204 * hb_ft_font_lock_face:
205 * @font: #hb_font_t to work upon
206 *
207 * Gets the FT_Face associated with @font, This face will be kept around until
208 * you call hb_ft_font_unlock_face().
209 *
210 * Return value: (nullable): the FT_Face associated with @font or %NULL
211 * Since: 2.6.5
212 **/
213 FT_Face
hb_ft_font_lock_face(hb_font_t * font)214 hb_ft_font_lock_face (hb_font_t *font)
215 {
216 if (unlikely (font->destroy != (hb_destroy_func_t) _hb_ft_font_destroy))
217 return nullptr;
218
219 const hb_ft_font_t *ft_font = (const hb_ft_font_t *) font->user_data;
220
221 ft_font->lock.lock ();
222
223 return ft_font->ft_face;
224 }
225
226 /**
227 * hb_ft_font_unlock_face:
228 * @font: #hb_font_t to work upon
229 *
230 * Releases an FT_Face previously obtained with hb_ft_font_lock_face().
231 *
232 * Since: 2.6.5
233 **/
234 void
hb_ft_font_unlock_face(hb_font_t * font)235 hb_ft_font_unlock_face (hb_font_t *font)
236 {
237 if (unlikely (font->destroy != (hb_destroy_func_t) _hb_ft_font_destroy))
238 return;
239
240 const hb_ft_font_t *ft_font = (const hb_ft_font_t *) font->user_data;
241
242 ft_font->lock.unlock ();
243 }
244
245
246 static hb_bool_t
hb_ft_get_nominal_glyph(hb_font_t * font HB_UNUSED,void * font_data,hb_codepoint_t unicode,hb_codepoint_t * glyph,void * user_data HB_UNUSED)247 hb_ft_get_nominal_glyph (hb_font_t *font HB_UNUSED,
248 void *font_data,
249 hb_codepoint_t unicode,
250 hb_codepoint_t *glyph,
251 void *user_data HB_UNUSED)
252 {
253 const hb_ft_font_t *ft_font = (const hb_ft_font_t *) font_data;
254 hb_lock_t lock (ft_font->lock);
255 unsigned int g = FT_Get_Char_Index (ft_font->ft_face, unicode);
256
257 if (unlikely (!g))
258 {
259 if (unlikely (ft_font->symbol) && unicode <= 0x00FFu)
260 {
261 /* For symbol-encoded OpenType fonts, we duplicate the
262 * U+F000..F0FF range at U+0000..U+00FF. That's what
263 * Windows seems to do, and that's hinted about at:
264 * https://docs.microsoft.com/en-us/typography/opentype/spec/recom
265 * under "Non-Standard (Symbol) Fonts". */
266 g = FT_Get_Char_Index (ft_font->ft_face, 0xF000u + unicode);
267 if (!g)
268 return false;
269 }
270 else
271 return false;
272 }
273
274 *glyph = g;
275 return true;
276 }
277
278 static unsigned int
hb_ft_get_nominal_glyphs(hb_font_t * font HB_UNUSED,void * font_data,unsigned int count,const hb_codepoint_t * first_unicode,unsigned int unicode_stride,hb_codepoint_t * first_glyph,unsigned int glyph_stride,void * user_data HB_UNUSED)279 hb_ft_get_nominal_glyphs (hb_font_t *font HB_UNUSED,
280 void *font_data,
281 unsigned int count,
282 const hb_codepoint_t *first_unicode,
283 unsigned int unicode_stride,
284 hb_codepoint_t *first_glyph,
285 unsigned int glyph_stride,
286 void *user_data HB_UNUSED)
287 {
288 const hb_ft_font_t *ft_font = (const hb_ft_font_t *) font_data;
289 hb_lock_t lock (ft_font->lock);
290 unsigned int done;
291 for (done = 0;
292 done < count && (*first_glyph = FT_Get_Char_Index (ft_font->ft_face, *first_unicode));
293 done++)
294 {
295 first_unicode = &StructAtOffsetUnaligned<hb_codepoint_t> (first_unicode, unicode_stride);
296 first_glyph = &StructAtOffsetUnaligned<hb_codepoint_t> (first_glyph, glyph_stride);
297 }
298 /* We don't need to do ft_font->symbol dance here, since HB calls the singular
299 * nominal_glyph() for what we don't handle here. */
300 return done;
301 }
302
303
304 static hb_bool_t
hb_ft_get_variation_glyph(hb_font_t * font HB_UNUSED,void * font_data,hb_codepoint_t unicode,hb_codepoint_t variation_selector,hb_codepoint_t * glyph,void * user_data HB_UNUSED)305 hb_ft_get_variation_glyph (hb_font_t *font HB_UNUSED,
306 void *font_data,
307 hb_codepoint_t unicode,
308 hb_codepoint_t variation_selector,
309 hb_codepoint_t *glyph,
310 void *user_data HB_UNUSED)
311 {
312 const hb_ft_font_t *ft_font = (const hb_ft_font_t *) font_data;
313 hb_lock_t lock (ft_font->lock);
314 unsigned int g = FT_Face_GetCharVariantIndex (ft_font->ft_face, unicode, variation_selector);
315
316 if (unlikely (!g))
317 return false;
318
319 *glyph = g;
320 return true;
321 }
322
323 static void
hb_ft_get_glyph_h_advances(hb_font_t * font,void * font_data,unsigned count,const hb_codepoint_t * first_glyph,unsigned glyph_stride,hb_position_t * first_advance,unsigned advance_stride,void * user_data HB_UNUSED)324 hb_ft_get_glyph_h_advances (hb_font_t* font, void* font_data,
325 unsigned count,
326 const hb_codepoint_t *first_glyph,
327 unsigned glyph_stride,
328 hb_position_t *first_advance,
329 unsigned advance_stride,
330 void *user_data HB_UNUSED)
331 {
332 const hb_ft_font_t *ft_font = (const hb_ft_font_t *) font_data;
333 hb_lock_t lock (ft_font->lock);
334 FT_Face ft_face = ft_font->ft_face;
335 int load_flags = ft_font->load_flags;
336 int mult = font->x_scale < 0 ? -1 : +1;
337
338 if (font->x_scale != ft_font->cached_x_scale)
339 {
340 ft_font->advance_cache.clear ();
341 ft_font->cached_x_scale = font->x_scale;
342 }
343
344 for (unsigned int i = 0; i < count; i++)
345 {
346 FT_Fixed v = 0;
347 hb_codepoint_t glyph = *first_glyph;
348
349 unsigned int cv;
350 if (ft_font->advance_cache.get (glyph, &cv))
351 v = cv;
352 else
353 {
354 FT_Get_Advance (ft_face, glyph, load_flags, &v);
355 ft_font->advance_cache.set (glyph, v);
356 }
357
358 *first_advance = (v * mult + (1<<9)) >> 10;
359 first_glyph = &StructAtOffsetUnaligned<hb_codepoint_t> (first_glyph, glyph_stride);
360 first_advance = &StructAtOffsetUnaligned<hb_position_t> (first_advance, advance_stride);
361 }
362 }
363
364 static hb_position_t
hb_ft_get_glyph_v_advance(hb_font_t * font,void * font_data,hb_codepoint_t glyph,void * user_data HB_UNUSED)365 hb_ft_get_glyph_v_advance (hb_font_t *font,
366 void *font_data,
367 hb_codepoint_t glyph,
368 void *user_data HB_UNUSED)
369 {
370 const hb_ft_font_t *ft_font = (const hb_ft_font_t *) font_data;
371 hb_lock_t lock (ft_font->lock);
372 FT_Fixed v;
373
374 if (unlikely (FT_Get_Advance (ft_font->ft_face, glyph, ft_font->load_flags | FT_LOAD_VERTICAL_LAYOUT, &v)))
375 return 0;
376
377 if (font->y_scale < 0)
378 v = -v;
379
380 /* Note: FreeType's vertical metrics grows downward while other FreeType coordinates
381 * have a Y growing upward. Hence the extra negation. */
382 return (-v + (1<<9)) >> 10;
383 }
384
385 static hb_bool_t
hb_ft_get_glyph_v_origin(hb_font_t * font,void * font_data,hb_codepoint_t glyph,hb_position_t * x,hb_position_t * y,void * user_data HB_UNUSED)386 hb_ft_get_glyph_v_origin (hb_font_t *font,
387 void *font_data,
388 hb_codepoint_t glyph,
389 hb_position_t *x,
390 hb_position_t *y,
391 void *user_data HB_UNUSED)
392 {
393 const hb_ft_font_t *ft_font = (const hb_ft_font_t *) font_data;
394 hb_lock_t lock (ft_font->lock);
395 FT_Face ft_face = ft_font->ft_face;
396
397 if (unlikely (FT_Load_Glyph (ft_face, glyph, ft_font->load_flags)))
398 return false;
399
400 /* Note: FreeType's vertical metrics grows downward while other FreeType coordinates
401 * have a Y growing upward. Hence the extra negation. */
402 *x = ft_face->glyph->metrics.horiBearingX - ft_face->glyph->metrics.vertBearingX;
403 *y = ft_face->glyph->metrics.horiBearingY - (-ft_face->glyph->metrics.vertBearingY);
404
405 if (font->x_scale < 0)
406 *x = -*x;
407 if (font->y_scale < 0)
408 *y = -*y;
409
410 return true;
411 }
412
413 #ifndef HB_NO_OT_SHAPE_FALLBACK
414 static hb_position_t
hb_ft_get_glyph_h_kerning(hb_font_t * font,void * font_data,hb_codepoint_t left_glyph,hb_codepoint_t right_glyph,void * user_data HB_UNUSED)415 hb_ft_get_glyph_h_kerning (hb_font_t *font,
416 void *font_data,
417 hb_codepoint_t left_glyph,
418 hb_codepoint_t right_glyph,
419 void *user_data HB_UNUSED)
420 {
421 const hb_ft_font_t *ft_font = (const hb_ft_font_t *) font_data;
422 FT_Vector kerningv;
423
424 FT_Kerning_Mode mode = font->x_ppem ? FT_KERNING_DEFAULT : FT_KERNING_UNFITTED;
425 if (FT_Get_Kerning (ft_font->ft_face, left_glyph, right_glyph, mode, &kerningv))
426 return 0;
427
428 return kerningv.x;
429 }
430 #endif
431
432 static hb_bool_t
hb_ft_get_glyph_extents(hb_font_t * font,void * font_data,hb_codepoint_t glyph,hb_glyph_extents_t * extents,void * user_data HB_UNUSED)433 hb_ft_get_glyph_extents (hb_font_t *font,
434 void *font_data,
435 hb_codepoint_t glyph,
436 hb_glyph_extents_t *extents,
437 void *user_data HB_UNUSED)
438 {
439 const hb_ft_font_t *ft_font = (const hb_ft_font_t *) font_data;
440 hb_lock_t lock (ft_font->lock);
441 FT_Face ft_face = ft_font->ft_face;
442
443 if (unlikely (FT_Load_Glyph (ft_face, glyph, ft_font->load_flags)))
444 return false;
445
446 extents->x_bearing = ft_face->glyph->metrics.horiBearingX;
447 extents->y_bearing = ft_face->glyph->metrics.horiBearingY;
448 extents->width = ft_face->glyph->metrics.width;
449 extents->height = -ft_face->glyph->metrics.height;
450 if (font->x_scale < 0)
451 {
452 extents->x_bearing = -extents->x_bearing;
453 extents->width = -extents->width;
454 }
455 if (font->y_scale < 0)
456 {
457 extents->y_bearing = -extents->y_bearing;
458 extents->height = -extents->height;
459 }
460 return true;
461 }
462
463 static hb_bool_t
hb_ft_get_glyph_contour_point(hb_font_t * font HB_UNUSED,void * font_data,hb_codepoint_t glyph,unsigned int point_index,hb_position_t * x,hb_position_t * y,void * user_data HB_UNUSED)464 hb_ft_get_glyph_contour_point (hb_font_t *font HB_UNUSED,
465 void *font_data,
466 hb_codepoint_t glyph,
467 unsigned int point_index,
468 hb_position_t *x,
469 hb_position_t *y,
470 void *user_data HB_UNUSED)
471 {
472 const hb_ft_font_t *ft_font = (const hb_ft_font_t *) font_data;
473 hb_lock_t lock (ft_font->lock);
474 FT_Face ft_face = ft_font->ft_face;
475
476 if (unlikely (FT_Load_Glyph (ft_face, glyph, ft_font->load_flags)))
477 return false;
478
479 if (unlikely (ft_face->glyph->format != FT_GLYPH_FORMAT_OUTLINE))
480 return false;
481
482 if (unlikely (point_index >= (unsigned int) ft_face->glyph->outline.n_points))
483 return false;
484
485 *x = ft_face->glyph->outline.points[point_index].x;
486 *y = ft_face->glyph->outline.points[point_index].y;
487
488 return true;
489 }
490
491 static hb_bool_t
hb_ft_get_glyph_name(hb_font_t * font HB_UNUSED,void * font_data,hb_codepoint_t glyph,char * name,unsigned int size,void * user_data HB_UNUSED)492 hb_ft_get_glyph_name (hb_font_t *font HB_UNUSED,
493 void *font_data,
494 hb_codepoint_t glyph,
495 char *name, unsigned int size,
496 void *user_data HB_UNUSED)
497 {
498 const hb_ft_font_t *ft_font = (const hb_ft_font_t *) font_data;
499 hb_lock_t lock (ft_font->lock);
500 FT_Face ft_face = ft_font->ft_face;
501
502 hb_bool_t ret = !FT_Get_Glyph_Name (ft_face, glyph, name, size);
503 if (ret && (size && !*name))
504 ret = false;
505
506 return ret;
507 }
508
509 static hb_bool_t
hb_ft_get_glyph_from_name(hb_font_t * font HB_UNUSED,void * font_data,const char * name,int len,hb_codepoint_t * glyph,void * user_data HB_UNUSED)510 hb_ft_get_glyph_from_name (hb_font_t *font HB_UNUSED,
511 void *font_data,
512 const char *name, int len, /* -1 means nul-terminated */
513 hb_codepoint_t *glyph,
514 void *user_data HB_UNUSED)
515 {
516 const hb_ft_font_t *ft_font = (const hb_ft_font_t *) font_data;
517 hb_lock_t lock (ft_font->lock);
518 FT_Face ft_face = ft_font->ft_face;
519
520 if (len < 0)
521 *glyph = FT_Get_Name_Index (ft_face, (FT_String *) name);
522 else {
523 /* Make a nul-terminated version. */
524 char buf[128];
525 len = hb_min (len, (int) sizeof (buf) - 1);
526 strncpy (buf, name, len);
527 buf[len] = '\0';
528 *glyph = FT_Get_Name_Index (ft_face, buf);
529 }
530
531 if (*glyph == 0)
532 {
533 /* Check whether the given name was actually the name of glyph 0. */
534 char buf[128];
535 if (!FT_Get_Glyph_Name(ft_face, 0, buf, sizeof (buf)) &&
536 len < 0 ? !strcmp (buf, name) : !strncmp (buf, name, len))
537 return true;
538 }
539
540 return *glyph != 0;
541 }
542
543 static hb_bool_t
hb_ft_get_font_h_extents(hb_font_t * font HB_UNUSED,void * font_data,hb_font_extents_t * metrics,void * user_data HB_UNUSED)544 hb_ft_get_font_h_extents (hb_font_t *font HB_UNUSED,
545 void *font_data,
546 hb_font_extents_t *metrics,
547 void *user_data HB_UNUSED)
548 {
549 const hb_ft_font_t *ft_font = (const hb_ft_font_t *) font_data;
550 hb_lock_t lock (ft_font->lock);
551 FT_Face ft_face = ft_font->ft_face;
552 metrics->ascender = FT_MulFix(ft_face->ascender, ft_face->size->metrics.y_scale);
553 metrics->descender = FT_MulFix(ft_face->descender, ft_face->size->metrics.y_scale);
554 metrics->line_gap = FT_MulFix( ft_face->height, ft_face->size->metrics.y_scale ) - (metrics->ascender - metrics->descender);
555 if (font->y_scale < 0)
556 {
557 metrics->ascender = -metrics->ascender;
558 metrics->descender = -metrics->descender;
559 metrics->line_gap = -metrics->line_gap;
560 }
561 return true;
562 }
563
564 static inline void free_static_ft_funcs ();
565
566 static struct hb_ft_font_funcs_lazy_loader_t : hb_font_funcs_lazy_loader_t<hb_ft_font_funcs_lazy_loader_t>
567 {
createhb_ft_font_funcs_lazy_loader_t568 static hb_font_funcs_t *create ()
569 {
570 hb_font_funcs_t *funcs = hb_font_funcs_create ();
571
572 hb_font_funcs_set_font_h_extents_func (funcs, hb_ft_get_font_h_extents, nullptr, nullptr);
573 //hb_font_funcs_set_font_v_extents_func (funcs, hb_ft_get_font_v_extents, nullptr, nullptr);
574 hb_font_funcs_set_nominal_glyph_func (funcs, hb_ft_get_nominal_glyph, nullptr, nullptr);
575 hb_font_funcs_set_nominal_glyphs_func (funcs, hb_ft_get_nominal_glyphs, nullptr, nullptr);
576 hb_font_funcs_set_variation_glyph_func (funcs, hb_ft_get_variation_glyph, nullptr, nullptr);
577 hb_font_funcs_set_glyph_h_advances_func (funcs, hb_ft_get_glyph_h_advances, nullptr, nullptr);
578 hb_font_funcs_set_glyph_v_advance_func (funcs, hb_ft_get_glyph_v_advance, nullptr, nullptr);
579 //hb_font_funcs_set_glyph_h_origin_func (funcs, hb_ft_get_glyph_h_origin, nullptr, nullptr);
580 hb_font_funcs_set_glyph_v_origin_func (funcs, hb_ft_get_glyph_v_origin, nullptr, nullptr);
581 #ifndef HB_NO_OT_SHAPE_FALLBACK
582 hb_font_funcs_set_glyph_h_kerning_func (funcs, hb_ft_get_glyph_h_kerning, nullptr, nullptr);
583 #endif
584 //hb_font_funcs_set_glyph_v_kerning_func (funcs, hb_ft_get_glyph_v_kerning, nullptr, nullptr);
585 hb_font_funcs_set_glyph_extents_func (funcs, hb_ft_get_glyph_extents, nullptr, nullptr);
586 hb_font_funcs_set_glyph_contour_point_func (funcs, hb_ft_get_glyph_contour_point, nullptr, nullptr);
587 hb_font_funcs_set_glyph_name_func (funcs, hb_ft_get_glyph_name, nullptr, nullptr);
588 hb_font_funcs_set_glyph_from_name_func (funcs, hb_ft_get_glyph_from_name, nullptr, nullptr);
589
590 hb_font_funcs_make_immutable (funcs);
591
592 hb_atexit (free_static_ft_funcs);
593
594 return funcs;
595 }
596 } static_ft_funcs;
597
598 static inline
free_static_ft_funcs()599 void free_static_ft_funcs ()
600 {
601 static_ft_funcs.free_instance ();
602 }
603
604 static hb_font_funcs_t *
_hb_ft_get_font_funcs()605 _hb_ft_get_font_funcs ()
606 {
607 return static_ft_funcs.get_unconst ();
608 }
609
610 static void
_hb_ft_font_set_funcs(hb_font_t * font,FT_Face ft_face,bool unref)611 _hb_ft_font_set_funcs (hb_font_t *font, FT_Face ft_face, bool unref)
612 {
613 bool symbol = ft_face->charmap && ft_face->charmap->encoding == FT_ENCODING_MS_SYMBOL;
614
615 hb_ft_font_t *ft_font = _hb_ft_font_create (ft_face, symbol, unref);
616 if (unlikely (!ft_font)) return;
617
618 hb_font_set_funcs (font,
619 _hb_ft_get_font_funcs (),
620 ft_font,
621 _hb_ft_font_destroy);
622 }
623
624
625 static hb_blob_t *
_hb_ft_reference_table(hb_face_t * face HB_UNUSED,hb_tag_t tag,void * user_data)626 _hb_ft_reference_table (hb_face_t *face HB_UNUSED, hb_tag_t tag, void *user_data)
627 {
628 FT_Face ft_face = (FT_Face) user_data;
629 FT_Byte *buffer;
630 FT_ULong length = 0;
631 FT_Error error;
632
633 /* Note: FreeType like HarfBuzz uses the NONE tag for fetching the entire blob */
634
635 error = FT_Load_Sfnt_Table (ft_face, tag, 0, nullptr, &length);
636 if (error)
637 return nullptr;
638
639 buffer = (FT_Byte *) hb_malloc (length);
640 if (!buffer)
641 return nullptr;
642
643 error = FT_Load_Sfnt_Table (ft_face, tag, 0, buffer, &length);
644 if (error)
645 {
646 hb_free (buffer);
647 return nullptr;
648 }
649
650 return hb_blob_create ((const char *) buffer, length,
651 HB_MEMORY_MODE_WRITABLE,
652 buffer, hb_free);
653 }
654
655 /**
656 * hb_ft_face_create:
657 * @ft_face: (destroy destroy) (scope notified): FT_Face to work upon
658 * @destroy: (nullable): A callback to call when the face object is not needed anymore
659 *
660 * Creates an #hb_face_t face object from the specified FT_Face.
661 *
662 * This variant of the function does not provide any life-cycle management.
663 *
664 * Most client programs should use hb_ft_face_create_referenced()
665 * (or, perhaps, hb_ft_face_create_cached()) instead.
666 *
667 * If you know you have valid reasons not to use hb_ft_face_create_referenced(),
668 * then it is the client program's responsibility to destroy @ft_face
669 * after the #hb_face_t face object has been destroyed.
670 *
671 * Return value: (transfer full): the new #hb_face_t face object
672 *
673 * Since: 0.9.2
674 **/
675 hb_face_t *
hb_ft_face_create(FT_Face ft_face,hb_destroy_func_t destroy)676 hb_ft_face_create (FT_Face ft_face,
677 hb_destroy_func_t destroy)
678 {
679 hb_face_t *face;
680
681 if (!ft_face->stream->read) {
682 hb_blob_t *blob;
683
684 blob = hb_blob_create ((const char *) ft_face->stream->base,
685 (unsigned int) ft_face->stream->size,
686 HB_MEMORY_MODE_READONLY,
687 ft_face, destroy);
688 face = hb_face_create (blob, ft_face->face_index);
689 hb_blob_destroy (blob);
690 } else {
691 face = hb_face_create_for_tables (_hb_ft_reference_table, ft_face, destroy);
692 }
693
694 hb_face_set_index (face, ft_face->face_index);
695 hb_face_set_upem (face, ft_face->units_per_EM);
696
697 return face;
698 }
699
700 /**
701 * hb_ft_face_create_referenced:
702 * @ft_face: FT_Face to work upon
703 *
704 * Creates an #hb_face_t face object from the specified FT_Face.
705 *
706 * This is the preferred variant of the hb_ft_face_create*
707 * function family, because it calls FT_Reference_Face() on @ft_face,
708 * ensuring that @ft_face remains alive as long as the resulting
709 * #hb_face_t face object remains alive. Also calls FT_Done_Face()
710 * when the #hb_face_t face object is destroyed.
711 *
712 * Use this version unless you know you have good reasons not to.
713 *
714 * Return value: (transfer full): the new #hb_face_t face object
715 *
716 * Since: 0.9.38
717 **/
718 hb_face_t *
hb_ft_face_create_referenced(FT_Face ft_face)719 hb_ft_face_create_referenced (FT_Face ft_face)
720 {
721 FT_Reference_Face (ft_face);
722 return hb_ft_face_create (ft_face, _hb_ft_face_destroy);
723 }
724
725 static void
hb_ft_face_finalize(FT_Face ft_face)726 hb_ft_face_finalize (FT_Face ft_face)
727 {
728 hb_face_destroy ((hb_face_t *) ft_face->generic.data);
729 }
730
731 /**
732 * hb_ft_face_create_cached:
733 * @ft_face: FT_Face to work upon
734 *
735 * Creates an #hb_face_t face object from the specified FT_Face.
736 *
737 * This variant of the function caches the newly created #hb_face_t
738 * face object, using the @generic pointer of @ft_face. Subsequent function
739 * calls that are passed the same @ft_face parameter will have the same
740 * #hb_face_t returned to them, and that #hb_face_t will be correctly
741 * reference counted.
742 *
743 * However, client programs are still responsible for destroying
744 * @ft_face after the last #hb_face_t face object has been destroyed.
745 *
746 * Return value: (transfer full): the new #hb_face_t face object
747 *
748 * Since: 0.9.2
749 **/
750 hb_face_t *
hb_ft_face_create_cached(FT_Face ft_face)751 hb_ft_face_create_cached (FT_Face ft_face)
752 {
753 if (unlikely (!ft_face->generic.data || ft_face->generic.finalizer != (FT_Generic_Finalizer) hb_ft_face_finalize))
754 {
755 if (ft_face->generic.finalizer)
756 ft_face->generic.finalizer (ft_face);
757
758 ft_face->generic.data = hb_ft_face_create (ft_face, nullptr);
759 ft_face->generic.finalizer = (FT_Generic_Finalizer) hb_ft_face_finalize;
760 }
761
762 return hb_face_reference ((hb_face_t *) ft_face->generic.data);
763 }
764
765 /**
766 * hb_ft_font_create:
767 * @ft_face: (destroy destroy) (scope notified): FT_Face to work upon
768 * @destroy: (nullable): A callback to call when the font object is not needed anymore
769 *
770 * Creates an #hb_font_t font object from the specified FT_Face.
771 *
772 * <note>Note: You must set the face size on @ft_face before calling
773 * hb_ft_font_create() on it. HarfBuzz assumes size is always set and will
774 * access `size` member of FT_Face unconditionally.</note>
775 *
776 * This variant of the function does not provide any life-cycle management.
777 *
778 * Most client programs should use hb_ft_font_create_referenced()
779 * instead.
780 *
781 * If you know you have valid reasons not to use hb_ft_font_create_referenced(),
782 * then it is the client program's responsibility to destroy @ft_face
783 * after the #hb_font_t font object has been destroyed.
784 *
785 * HarfBuzz will use the @destroy callback on the #hb_font_t font object
786 * if it is supplied when you use this function. However, even if @destroy
787 * is provided, it is the client program's responsibility to destroy @ft_face,
788 * and it is the client program's responsibility to ensure that @ft_face is
789 * destroyed only after the #hb_font_t font object has been destroyed.
790 *
791 * Return value: (transfer full): the new #hb_font_t font object
792 *
793 * Since: 0.9.2
794 **/
795 hb_font_t *
hb_ft_font_create(FT_Face ft_face,hb_destroy_func_t destroy)796 hb_ft_font_create (FT_Face ft_face,
797 hb_destroy_func_t destroy)
798 {
799 hb_font_t *font;
800 hb_face_t *face;
801
802 face = hb_ft_face_create (ft_face, destroy);
803 font = hb_font_create (face);
804 hb_face_destroy (face);
805 _hb_ft_font_set_funcs (font, ft_face, false);
806 hb_ft_font_changed (font);
807 return font;
808 }
809
810 /**
811 * hb_ft_font_changed:
812 * @font: #hb_font_t to work upon
813 *
814 * Refreshes the state of @font when the underlying FT_Face has changed.
815 * This function should be called after changing the size or
816 * variation-axis settings on the FT_Face.
817 *
818 * Since: 1.0.5
819 **/
820 void
hb_ft_font_changed(hb_font_t * font)821 hb_ft_font_changed (hb_font_t *font)
822 {
823 if (font->destroy != (hb_destroy_func_t) _hb_ft_font_destroy)
824 return;
825
826 hb_ft_font_t *ft_font = (hb_ft_font_t *) font->user_data;
827
828 FT_Face ft_face = ft_font->ft_face;
829
830 hb_font_set_scale (font,
831 (int) (((uint64_t) ft_face->size->metrics.x_scale * (uint64_t) ft_face->units_per_EM + (1u<<15)) >> 16),
832 (int) (((uint64_t) ft_face->size->metrics.y_scale * (uint64_t) ft_face->units_per_EM + (1u<<15)) >> 16));
833 #if 0 /* hb-ft works in no-hinting model */
834 hb_font_set_ppem (font,
835 ft_face->size->metrics.x_ppem,
836 ft_face->size->metrics.y_ppem);
837 #endif
838
839 #if defined(HAVE_FT_GET_VAR_BLEND_COORDINATES) && !defined(HB_NO_VAR)
840 FT_MM_Var *mm_var = nullptr;
841 if (!FT_Get_MM_Var (ft_face, &mm_var))
842 {
843 FT_Fixed *ft_coords = (FT_Fixed *) hb_calloc (mm_var->num_axis, sizeof (FT_Fixed));
844 int *coords = (int *) hb_calloc (mm_var->num_axis, sizeof (int));
845 if (coords && ft_coords)
846 {
847 if (!FT_Get_Var_Blend_Coordinates (ft_face, mm_var->num_axis, ft_coords))
848 {
849 bool nonzero = false;
850
851 for (unsigned int i = 0; i < mm_var->num_axis; ++i)
852 {
853 coords[i] = ft_coords[i] >>= 2;
854 nonzero = nonzero || coords[i];
855 }
856
857 if (nonzero)
858 hb_font_set_var_coords_normalized (font, coords, mm_var->num_axis);
859 else
860 hb_font_set_var_coords_normalized (font, nullptr, 0);
861 }
862 }
863 hb_free (coords);
864 hb_free (ft_coords);
865 #ifdef HAVE_FT_DONE_MM_VAR
866 FT_Done_MM_Var (ft_face->glyph->library, mm_var);
867 #else
868 hb_free (mm_var);
869 #endif
870 }
871 #endif
872 }
873
874 /**
875 * hb_ft_font_create_referenced:
876 * @ft_face: FT_Face to work upon
877 *
878 * Creates an #hb_font_t font object from the specified FT_Face.
879 *
880 * <note>Note: You must set the face size on @ft_face before calling
881 * hb_ft_font_create_referenced() on it. HarfBuzz assumes size is always set
882 * and will access `size` member of FT_Face unconditionally.</note>
883 *
884 * This is the preferred variant of the hb_ft_font_create*
885 * function family, because it calls FT_Reference_Face() on @ft_face,
886 * ensuring that @ft_face remains alive as long as the resulting
887 * #hb_font_t font object remains alive.
888 *
889 * Use this version unless you know you have good reasons not to.
890 *
891 * Return value: (transfer full): the new #hb_font_t font object
892 *
893 * Since: 0.9.38
894 **/
895 hb_font_t *
hb_ft_font_create_referenced(FT_Face ft_face)896 hb_ft_font_create_referenced (FT_Face ft_face)
897 {
898 FT_Reference_Face (ft_face);
899 return hb_ft_font_create (ft_face, _hb_ft_face_destroy);
900 }
901
902 static inline void free_static_ft_library ();
903
904 static struct hb_ft_library_lazy_loader_t : hb_lazy_loader_t<hb_remove_pointer<FT_Library>,
905 hb_ft_library_lazy_loader_t>
906 {
createhb_ft_library_lazy_loader_t907 static FT_Library create ()
908 {
909 FT_Library l;
910 if (FT_Init_FreeType (&l))
911 return nullptr;
912
913 hb_atexit (free_static_ft_library);
914
915 return l;
916 }
destroyhb_ft_library_lazy_loader_t917 static void destroy (FT_Library l)
918 {
919 FT_Done_FreeType (l);
920 }
get_nullhb_ft_library_lazy_loader_t921 static FT_Library get_null ()
922 {
923 return nullptr;
924 }
925 } static_ft_library;
926
927 static inline
free_static_ft_library()928 void free_static_ft_library ()
929 {
930 static_ft_library.free_instance ();
931 }
932
933 static FT_Library
get_ft_library()934 get_ft_library ()
935 {
936 return static_ft_library.get_unconst ();
937 }
938
939 static void
_release_blob(FT_Face ft_face)940 _release_blob (FT_Face ft_face)
941 {
942 hb_blob_destroy ((hb_blob_t *) ft_face->generic.data);
943 }
944
945 /**
946 * hb_ft_font_set_funcs:
947 * @font: #hb_font_t to work upon
948 *
949 * Configures the font-functions structure of the specified
950 * #hb_font_t font object to use FreeType font functions.
951 *
952 * In particular, you can use this function to configure an
953 * existing #hb_face_t face object for use with FreeType font
954 * functions even if that #hb_face_t face object was initially
955 * created with hb_face_create(), and therefore was not
956 * initially configured to use FreeType font functions.
957 *
958 * An #hb_face_t face object created with hb_ft_face_create()
959 * is preconfigured for FreeType font functions and does not
960 * require this function to be used.
961 *
962 * <note>Note: Internally, this function creates an FT_Face.
963 * </note>
964 *
965 * Since: 1.0.5
966 **/
967 void
hb_ft_font_set_funcs(hb_font_t * font)968 hb_ft_font_set_funcs (hb_font_t *font)
969 {
970 hb_blob_t *blob = hb_face_reference_blob (font->face);
971 unsigned int blob_length;
972 const char *blob_data = hb_blob_get_data (blob, &blob_length);
973 if (unlikely (!blob_length))
974 DEBUG_MSG (FT, font, "Font face has empty blob");
975
976 FT_Face ft_face = nullptr;
977 FT_Error err = FT_New_Memory_Face (get_ft_library (),
978 (const FT_Byte *) blob_data,
979 blob_length,
980 hb_face_get_index (font->face),
981 &ft_face);
982
983 if (unlikely (err)) {
984 hb_blob_destroy (blob);
985 DEBUG_MSG (FT, font, "Font face FT_New_Memory_Face() failed");
986 return;
987 }
988
989 if (FT_Select_Charmap (ft_face, FT_ENCODING_MS_SYMBOL))
990 FT_Select_Charmap (ft_face, FT_ENCODING_UNICODE);
991
992 FT_Set_Char_Size (ft_face,
993 abs (font->x_scale), abs (font->y_scale),
994 0, 0);
995 #if 0
996 font->x_ppem * 72 * 64 / font->x_scale,
997 font->y_ppem * 72 * 64 / font->y_scale);
998 #endif
999 if (font->x_scale < 0 || font->y_scale < 0)
1000 {
1001 FT_Matrix matrix = { font->x_scale < 0 ? -1 : +1, 0,
1002 0, font->y_scale < 0 ? -1 : +1};
1003 FT_Set_Transform (ft_face, &matrix, nullptr);
1004 }
1005
1006 #if defined(HAVE_FT_GET_VAR_BLEND_COORDINATES) && !defined(HB_NO_VAR)
1007 unsigned int num_coords;
1008 const int *coords = hb_font_get_var_coords_normalized (font, &num_coords);
1009 if (num_coords)
1010 {
1011 FT_Fixed *ft_coords = (FT_Fixed *) hb_calloc (num_coords, sizeof (FT_Fixed));
1012 if (ft_coords)
1013 {
1014 for (unsigned int i = 0; i < num_coords; i++)
1015 ft_coords[i] = coords[i] * 4;
1016 FT_Set_Var_Blend_Coordinates (ft_face, num_coords, ft_coords);
1017 hb_free (ft_coords);
1018 }
1019 }
1020 #endif
1021
1022 ft_face->generic.data = blob;
1023 ft_face->generic.finalizer = (FT_Generic_Finalizer) _release_blob;
1024
1025 _hb_ft_font_set_funcs (font, ft_face, true);
1026 hb_ft_font_set_load_flags (font, FT_LOAD_DEFAULT | FT_LOAD_NO_HINTING);
1027 }
1028
1029
1030 #endif
1031