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-cache.hh"
37#include "hb-draw.hh"
38#include "hb-font.hh"
39#include "hb-machinery.hh"
40#include "hb-ot-os2-table.hh"
41#include "hb-ot-shaper-arabic-pua.hh"
42#include "hb-paint.hh"
43
44#include FT_ADVANCES_H
45#include FT_MULTIPLE_MASTERS_H
46#include FT_OUTLINE_H
47#include FT_TRUETYPE_TABLES_H
48#include FT_SYNTHESIS_H
49#if (FREETYPE_MAJOR*10000 + FREETYPE_MINOR*100 + FREETYPE_PATCH) >= 21300
50#include FT_COLOR_H
51#endif
52
53
54/**
55 * SECTION:hb-ft
56 * @title: hb-ft
57 * @short_description: FreeType integration
58 * @include: hb-ft.h
59 *
60 * Functions for using HarfBuzz with the FreeType library.
61 *
62 * HarfBuzz supports using FreeType to provide face and
63 * font data.
64 *
65 * <note>Note that FreeType is not thread-safe, therefore these
66 * functions are not thread-safe either.</note>
67 **/
68
69
70/* TODO:
71 *
72 * In general, this file does a fine job of what it's supposed to do.
73 * There are, however, things that need more work:
74 *
75 * - FreeType works in 26.6 mode. Clients can decide to use that mode, and everything
76 * would work fine. However, we also abuse this API for performing in font-space,
77 * but don't pass the correct flags to FreeType. We just abuse the no-hinting mode
78 * for that, such that no rounding etc happens. As such, we don't set ppem, and
79 * pass NO_HINTING as load_flags. Would be much better to use NO_SCALE, and scale
80 * ourselves.
81 *
82 * - We don't handle / allow for emboldening / obliqueing.
83 *
84 * - In the future, we should add constructors to create fonts in font space?
85 */
86
87
88using hb_ft_advance_cache_t = hb_cache_t<16, 24, 8, false>;
89
90struct hb_ft_font_t
91{
92 int load_flags;
93 bool symbol; /* Whether selected cmap is symbol cmap. */
94 bool unref; /* Whether to destroy ft_face when done. */
95 bool transform; /* Whether to apply FT_Face's transform. */
96
97 mutable hb_mutex_t lock; /* Protects members below. */
98 FT_Face ft_face;
99 mutable unsigned cached_serial;
100 mutable hb_ft_advance_cache_t advance_cache;
101};
102
103static hb_ft_font_t *
104_hb_ft_font_create (FT_Face ft_face, bool symbol, bool unref)
105{
106 hb_ft_font_t *ft_font = (hb_ft_font_t *) hb_calloc (1, sizeof (hb_ft_font_t));
107 if (unlikely (!ft_font)) return nullptr;
108
109 ft_font->lock.init ();
110 ft_font->ft_face = ft_face;
111 ft_font->symbol = symbol;
112 ft_font->unref = unref;
113
114 ft_font->load_flags = FT_LOAD_DEFAULT | FT_LOAD_NO_HINTING;
115
116 ft_font->cached_serial = (unsigned) -1;
117 new (&ft_font->advance_cache) hb_ft_advance_cache_t;
118
119 return ft_font;
120}
121
122static void
123_hb_ft_face_destroy (void *data)
124{
125 FT_Done_Face ((FT_Face) data);
126}
127
128static void
129_hb_ft_font_destroy (void *data)
130{
131 hb_ft_font_t *ft_font = (hb_ft_font_t *) data;
132
133 if (ft_font->unref)
134 _hb_ft_face_destroy (ft_font->ft_face);
135
136 ft_font->lock.fini ();
137
138 hb_free (ft_font);
139}
140
141
142/* hb_font changed, update FT_Face. */
143static void _hb_ft_hb_font_changed (hb_font_t *font, FT_Face ft_face)
144{
145 hb_ft_font_t *ft_font = (hb_ft_font_t *) font->user_data;
146
147 float x_mult = 1.f, y_mult = 1.f;
148
149 if (font->x_scale < 0) x_mult = -x_mult;
150 if (font->y_scale < 0) y_mult = -y_mult;
151
152 if (FT_Set_Char_Size (ft_face,
153 abs (font->x_scale), abs (font->y_scale),
154 0, 0
155#if 0
156 font->x_ppem * 72 * 64 / font->x_scale,
157 font->y_ppem * 72 * 64 / font->y_scale
158#endif
159 ) && ft_face->num_fixed_sizes)
160 {
161#ifdef HAVE_FT_GET_TRANSFORM
162 /* Bitmap font, eg. bitmap color emoji. */
163 /* Pick largest size? */
164 int x_scale = ft_face->available_sizes[ft_face->num_fixed_sizes - 1].x_ppem;
165 int y_scale = ft_face->available_sizes[ft_face->num_fixed_sizes - 1].y_ppem;
166 FT_Set_Char_Size (ft_face,
167 x_scale, y_scale,
168 0, 0);
169
170 /* This contains the sign that was previously in x_mult/y_mult. */
171 x_mult = (float) font->x_scale / x_scale;
172 y_mult = (float) font->y_scale / y_scale;
173#endif
174 }
175 else
176 { /* Shrug */ }
177
178
179 if (x_mult != 1.f || y_mult != 1.f)
180 {
181 FT_Matrix matrix = { (int) roundf (x_mult * (1<<16)), 0,
182 0, (int) roundf (y_mult * (1<<16))};
183 FT_Set_Transform (ft_face, &matrix, nullptr);
184 ft_font->transform = true;
185 }
186
187#if defined(HAVE_FT_GET_VAR_BLEND_COORDINATES) && !defined(HB_NO_VAR)
188 unsigned int num_coords;
189 const float *coords = hb_font_get_var_coords_design (font, &num_coords);
190 if (num_coords)
191 {
192 FT_Fixed *ft_coords = (FT_Fixed *) hb_calloc (num_coords, sizeof (FT_Fixed));
193 if (ft_coords)
194 {
195 for (unsigned int i = 0; i < num_coords; i++)
196 ft_coords[i] = coords[i] * 65536.f;
197 FT_Set_Var_Design_Coordinates (ft_face, num_coords, ft_coords);
198 hb_free (ft_coords);
199 }
200 }
201#endif
202}
203
204/* Check if hb_font changed, update FT_Face. */
205static inline bool
206_hb_ft_hb_font_check_changed (hb_font_t *font,
207 const hb_ft_font_t *ft_font)
208{
209 if (font->serial != ft_font->cached_serial)
210 {
211 _hb_ft_hb_font_changed (font, ft_font->ft_face);
212 ft_font->advance_cache.clear ();
213 ft_font->cached_serial = font->serial;
214 return true;
215 }
216 return false;
217}
218
219
220/**
221 * hb_ft_font_set_load_flags:
222 * @font: #hb_font_t to work upon
223 * @load_flags: The FreeType load flags to set
224 *
225 * Sets the FT_Load_Glyph load flags for the specified #hb_font_t.
226 *
227 * For more information, see
228 * https://www.freetype.org/freetype2/docs/reference/ft2-base_interface.html#ft_load_xxx
229 *
230 * This function works with #hb_font_t objects created by
231 * hb_ft_font_create() or hb_ft_font_create_referenced().
232 *
233 * Since: 1.0.5
234 **/
235void
236hb_ft_font_set_load_flags (hb_font_t *font, int load_flags)
237{
238 if (hb_object_is_immutable (font))
239 return;
240
241 if (unlikely (font->destroy != (hb_destroy_func_t) _hb_ft_font_destroy))
242 return;
243
244 hb_ft_font_t *ft_font = (hb_ft_font_t *) font->user_data;
245
246 ft_font->load_flags = load_flags;
247}
248
249/**
250 * hb_ft_font_get_load_flags:
251 * @font: #hb_font_t to work upon
252 *
253 * Fetches the FT_Load_Glyph load flags of the specified #hb_font_t.
254 *
255 * For more information, see
256 * https://www.freetype.org/freetype2/docs/reference/ft2-base_interface.html#ft_load_xxx
257 *
258 * This function works with #hb_font_t objects created by
259 * hb_ft_font_create() or hb_ft_font_create_referenced().
260 *
261 * Return value: FT_Load_Glyph flags found, or 0
262 *
263 * Since: 1.0.5
264 **/
265int
266hb_ft_font_get_load_flags (hb_font_t *font)
267{
268 if (unlikely (font->destroy != (hb_destroy_func_t) _hb_ft_font_destroy))
269 return 0;
270
271 const hb_ft_font_t *ft_font = (const hb_ft_font_t *) font->user_data;
272
273 return ft_font->load_flags;
274}
275
276/**
277 * hb_ft_font_get_face: (skip)
278 * @font: #hb_font_t to work upon
279 *
280 * Fetches the FT_Face associated with the specified #hb_font_t
281 * font object.
282 *
283 * This function works with #hb_font_t objects created by
284 * hb_ft_font_create() or hb_ft_font_create_referenced().
285 *
286 * Return value: (nullable): the FT_Face found or `NULL`
287 *
288 * Since: 0.9.2
289 **/
290FT_Face
291hb_ft_font_get_face (hb_font_t *font)
292{
293 if (unlikely (font->destroy != (hb_destroy_func_t) _hb_ft_font_destroy))
294 return nullptr;
295
296 const hb_ft_font_t *ft_font = (const hb_ft_font_t *) font->user_data;
297
298 return ft_font->ft_face;
299}
300
301/**
302 * hb_ft_font_lock_face: (skip)
303 * @font: #hb_font_t to work upon
304 *
305 * Gets the FT_Face associated with @font.
306 *
307 * This face will be kept around and access to the FT_Face object
308 * from other HarfBuzz API wil be blocked until you call hb_ft_font_unlock_face().
309 *
310 * This function works with #hb_font_t objects created by
311 * hb_ft_font_create() or hb_ft_font_create_referenced().
312 *
313 * Return value: (nullable) (transfer none): the FT_Face associated with @font or `NULL`
314 * Since: 2.6.5
315 **/
316FT_Face
317hb_ft_font_lock_face (hb_font_t *font)
318{
319 if (unlikely (font->destroy != (hb_destroy_func_t) _hb_ft_font_destroy))
320 return nullptr;
321
322 const hb_ft_font_t *ft_font = (const hb_ft_font_t *) font->user_data;
323
324 ft_font->lock.lock ();
325
326 return ft_font->ft_face;
327}
328
329/**
330 * hb_ft_font_unlock_face: (skip)
331 * @font: #hb_font_t to work upon
332 *
333 * Releases an FT_Face previously obtained with hb_ft_font_lock_face().
334 *
335 * Since: 2.6.5
336 **/
337void
338hb_ft_font_unlock_face (hb_font_t *font)
339{
340 if (unlikely (font->destroy != (hb_destroy_func_t) _hb_ft_font_destroy))
341 return;
342
343 const hb_ft_font_t *ft_font = (const hb_ft_font_t *) font->user_data;
344
345 ft_font->lock.unlock ();
346}
347
348
349static hb_bool_t
350hb_ft_get_nominal_glyph (hb_font_t *font,
351 void *font_data,
352 hb_codepoint_t unicode,
353 hb_codepoint_t *glyph,
354 void *user_data HB_UNUSED)
355{
356 const hb_ft_font_t *ft_font = (const hb_ft_font_t *) font_data;
357 hb_lock_t lock (ft_font->lock);
358 unsigned int g = FT_Get_Char_Index (ft_font->ft_face, unicode);
359
360 if (unlikely (!g))
361 {
362 if (unlikely (ft_font->symbol))
363 {
364 switch ((unsigned) font->face->table.OS2->get_font_page ()) {
365 case OT::OS2::font_page_t::FONT_PAGE_NONE:
366 if (unicode <= 0x00FFu)
367 /* For symbol-encoded OpenType fonts, we duplicate the
368 * U+F000..F0FF range at U+0000..U+00FF. That's what
369 * Windows seems to do, and that's hinted about at:
370 * https://docs.microsoft.com/en-us/typography/opentype/spec/recom
371 * under "Non-Standard (Symbol) Fonts". */
372 g = FT_Get_Char_Index (ft_font->ft_face, 0xF000u + unicode);
373 break;
374#ifndef HB_NO_OT_SHAPER_ARABIC_FALLBACK
375 case OT::OS2::font_page_t::FONT_PAGE_SIMP_ARABIC:
376 g = FT_Get_Char_Index (ft_font->ft_face, _hb_arabic_pua_simp_map (unicode));
377 break;
378 case OT::OS2::font_page_t::FONT_PAGE_TRAD_ARABIC:
379 g = FT_Get_Char_Index (ft_font->ft_face, _hb_arabic_pua_trad_map (unicode));
380 break;
381#endif
382 default:
383 break;
384 }
385 if (!g)
386 return false;
387 }
388 else
389 return false;
390 }
391
392 *glyph = g;
393 return true;
394}
395
396static unsigned int
397hb_ft_get_nominal_glyphs (hb_font_t *font HB_UNUSED,
398 void *font_data,
399 unsigned int count,
400 const hb_codepoint_t *first_unicode,
401 unsigned int unicode_stride,
402 hb_codepoint_t *first_glyph,
403 unsigned int glyph_stride,
404 void *user_data HB_UNUSED)
405{
406 const hb_ft_font_t *ft_font = (const hb_ft_font_t *) font_data;
407 hb_lock_t lock (ft_font->lock);
408 unsigned int done;
409 for (done = 0;
410 done < count && (*first_glyph = FT_Get_Char_Index (ft_font->ft_face, *first_unicode));
411 done++)
412 {
413 first_unicode = &StructAtOffsetUnaligned<hb_codepoint_t> (first_unicode, unicode_stride);
414 first_glyph = &StructAtOffsetUnaligned<hb_codepoint_t> (first_glyph, glyph_stride);
415 }
416 /* We don't need to do ft_font->symbol dance here, since HB calls the singular
417 * nominal_glyph() for what we don't handle here. */
418 return done;
419}
420
421
422static hb_bool_t
423hb_ft_get_variation_glyph (hb_font_t *font HB_UNUSED,
424 void *font_data,
425 hb_codepoint_t unicode,
426 hb_codepoint_t variation_selector,
427 hb_codepoint_t *glyph,
428 void *user_data HB_UNUSED)
429{
430 const hb_ft_font_t *ft_font = (const hb_ft_font_t *) font_data;
431 hb_lock_t lock (ft_font->lock);
432 unsigned int g = FT_Face_GetCharVariantIndex (ft_font->ft_face, unicode, variation_selector);
433
434 if (unlikely (!g))
435 return false;
436
437 *glyph = g;
438 return true;
439}
440
441static void
442hb_ft_get_glyph_h_advances (hb_font_t* font, void* font_data,
443 unsigned count,
444 const hb_codepoint_t *first_glyph,
445 unsigned glyph_stride,
446 hb_position_t *first_advance,
447 unsigned advance_stride,
448 void *user_data HB_UNUSED)
449{
450 const hb_ft_font_t *ft_font = (const hb_ft_font_t *) font_data;
451 hb_position_t *orig_first_advance = first_advance;
452 hb_lock_t lock (ft_font->lock);
453 FT_Face ft_face = ft_font->ft_face;
454 int load_flags = ft_font->load_flags;
455 float x_mult;
456#ifdef HAVE_FT_GET_TRANSFORM
457 if (ft_font->transform)
458 {
459 FT_Matrix matrix;
460 FT_Get_Transform (ft_face, &matrix, nullptr);
461 x_mult = sqrtf ((float)matrix.xx * matrix.xx + (float)matrix.xy * matrix.xy) / 65536.f;
462 x_mult *= font->x_scale < 0 ? -1 : +1;
463 }
464 else
465#endif
466 {
467 x_mult = font->x_scale < 0 ? -1 : +1;
468 }
469
470 for (unsigned int i = 0; i < count; i++)
471 {
472 FT_Fixed v = 0;
473 hb_codepoint_t glyph = *first_glyph;
474
475 unsigned int cv;
476 if (ft_font->advance_cache.get (glyph, &cv))
477 v = cv;
478 else
479 {
480 FT_Get_Advance (ft_face, glyph, load_flags, &v);
481 /* Work around bug that FreeType seems to return negative advance
482 * for variable-set fonts if x_scale is negative! */
483 v = abs (v);
484 v = (int) (v * x_mult + (1<<9)) >> 10;
485 ft_font->advance_cache.set (glyph, v);
486 }
487
488 *first_advance = v;
489 first_glyph = &StructAtOffsetUnaligned<hb_codepoint_t> (first_glyph, glyph_stride);
490 first_advance = &StructAtOffsetUnaligned<hb_position_t> (first_advance, advance_stride);
491 }
492
493 if (font->x_strength && !font->embolden_in_place)
494 {
495 /* Emboldening. */
496 hb_position_t x_strength = font->x_scale >= 0 ? font->x_strength : -font->x_strength;
497 first_advance = orig_first_advance;
498 for (unsigned int i = 0; i < count; i++)
499 {
500 *first_advance += *first_advance ? x_strength : 0;
501 first_advance = &StructAtOffsetUnaligned<hb_position_t> (first_advance, advance_stride);
502 }
503 }
504}
505
506#ifndef HB_NO_VERTICAL
507static hb_position_t
508hb_ft_get_glyph_v_advance (hb_font_t *font,
509 void *font_data,
510 hb_codepoint_t glyph,
511 void *user_data HB_UNUSED)
512{
513 const hb_ft_font_t *ft_font = (const hb_ft_font_t *) font_data;
514 hb_lock_t lock (ft_font->lock);
515 FT_Fixed v;
516 float y_mult;
517#ifdef HAVE_FT_GET_TRANSFORM
518 if (ft_font->transform)
519 {
520 FT_Matrix matrix;
521 FT_Get_Transform (ft_font->ft_face, &matrix, nullptr);
522 y_mult = sqrtf ((float)matrix.yx * matrix.yx + (float)matrix.yy * matrix.yy) / 65536.f;
523 y_mult *= font->y_scale < 0 ? -1 : +1;
524 }
525 else
526#endif
527 {
528 y_mult = font->y_scale < 0 ? -1 : +1;
529 }
530
531 if (unlikely (FT_Get_Advance (ft_font->ft_face, glyph, ft_font->load_flags | FT_LOAD_VERTICAL_LAYOUT, &v)))
532 return 0;
533
534 v = (int) (y_mult * v);
535
536 /* Note: FreeType's vertical metrics grows downward while other FreeType coordinates
537 * have a Y growing upward. Hence the extra negation. */
538
539 hb_position_t y_strength = font->y_scale >= 0 ? font->y_strength : -font->y_strength;
540 return ((-v + (1<<9)) >> 10) + (font->embolden_in_place ? 0 : y_strength);
541}
542#endif
543
544#ifndef HB_NO_VERTICAL
545static hb_bool_t
546hb_ft_get_glyph_v_origin (hb_font_t *font,
547 void *font_data,
548 hb_codepoint_t glyph,
549 hb_position_t *x,
550 hb_position_t *y,
551 void *user_data HB_UNUSED)
552{
553 const hb_ft_font_t *ft_font = (const hb_ft_font_t *) font_data;
554 hb_lock_t lock (ft_font->lock);
555 FT_Face ft_face = ft_font->ft_face;
556 float x_mult, y_mult;
557#ifdef HAVE_FT_GET_TRANSFORM
558 if (ft_font->transform)
559 {
560 FT_Matrix matrix;
561 FT_Get_Transform (ft_face, &matrix, nullptr);
562 x_mult = sqrtf ((float)matrix.xx * matrix.xx + (float)matrix.xy * matrix.xy) / 65536.f;
563 x_mult *= font->x_scale < 0 ? -1 : +1;
564 y_mult = sqrtf ((float)matrix.yx * matrix.yx + (float)matrix.yy * matrix.yy) / 65536.f;
565 y_mult *= font->y_scale < 0 ? -1 : +1;
566 }
567 else
568#endif
569 {
570 x_mult = font->x_scale < 0 ? -1 : +1;
571 y_mult = font->y_scale < 0 ? -1 : +1;
572 }
573
574 if (unlikely (FT_Load_Glyph (ft_face, glyph, ft_font->load_flags)))
575 return false;
576
577 /* Note: FreeType's vertical metrics grows downward while other FreeType coordinates
578 * have a Y growing upward. Hence the extra negation. */
579 *x = ft_face->glyph->metrics.horiBearingX - ft_face->glyph->metrics.vertBearingX;
580 *y = ft_face->glyph->metrics.horiBearingY - (-ft_face->glyph->metrics.vertBearingY);
581
582 *x = (hb_position_t) (x_mult * *x);
583 *y = (hb_position_t) (y_mult * *y);
584
585 return true;
586}
587#endif
588
589#ifndef HB_NO_OT_SHAPE_FALLBACK
590static hb_position_t
591hb_ft_get_glyph_h_kerning (hb_font_t *font,
592 void *font_data,
593 hb_codepoint_t left_glyph,
594 hb_codepoint_t right_glyph,
595 void *user_data HB_UNUSED)
596{
597 const hb_ft_font_t *ft_font = (const hb_ft_font_t *) font_data;
598 hb_lock_t lock (ft_font->lock);
599 FT_Vector kerningv;
600
601 FT_Kerning_Mode mode = font->x_ppem ? FT_KERNING_DEFAULT : FT_KERNING_UNFITTED;
602 if (FT_Get_Kerning (ft_font->ft_face, left_glyph, right_glyph, mode, &kerningv))
603 return 0;
604
605 return kerningv.x;
606}
607#endif
608
609static hb_bool_t
610hb_ft_get_glyph_extents (hb_font_t *font,
611 void *font_data,
612 hb_codepoint_t glyph,
613 hb_glyph_extents_t *extents,
614 void *user_data HB_UNUSED)
615{
616 const hb_ft_font_t *ft_font = (const hb_ft_font_t *) font_data;
617 hb_lock_t lock (ft_font->lock);
618 FT_Face ft_face = ft_font->ft_face;
619 float x_mult, y_mult;
620 float slant_xy = font->slant_xy;
621#ifdef HAVE_FT_GET_TRANSFORM
622 if (ft_font->transform)
623 {
624 FT_Matrix matrix;
625 FT_Get_Transform (ft_face, &matrix, nullptr);
626 x_mult = sqrtf ((float)matrix.xx * matrix.xx + (float)matrix.xy * matrix.xy) / 65536.f;
627 x_mult *= font->x_scale < 0 ? -1 : +1;
628 y_mult = sqrtf ((float)matrix.yx * matrix.yx + (float)matrix.yy * matrix.yy) / 65536.f;
629 y_mult *= font->y_scale < 0 ? -1 : +1;
630 }
631 else
632#endif
633 {
634 x_mult = font->x_scale < 0 ? -1 : +1;
635 y_mult = font->y_scale < 0 ? -1 : +1;
636 }
637
638 if (unlikely (FT_Load_Glyph (ft_face, glyph, ft_font->load_flags)))
639 return false;
640
641 /* Copied from hb_font_t::scale_glyph_extents. */
642
643 float x1 = x_mult * ft_face->glyph->metrics.horiBearingX;
644 float y1 = y_mult * ft_face->glyph->metrics.horiBearingY;
645 float x2 = x1 + x_mult * ft_face->glyph->metrics.width;
646 float y2 = y1 + y_mult * -ft_face->glyph->metrics.height;
647
648 /* Apply slant. */
649 if (slant_xy)
650 {
651 x1 += hb_min (y1 * slant_xy, y2 * slant_xy);
652 x2 += hb_max (y1 * slant_xy, y2 * slant_xy);
653 }
654
655 extents->x_bearing = floorf (x1);
656 extents->y_bearing = floorf (y1);
657 extents->width = ceilf (x2) - extents->x_bearing;
658 extents->height = ceilf (y2) - extents->y_bearing;
659
660 if (font->x_strength || font->y_strength)
661 {
662 /* Y */
663 int y_shift = font->y_strength;
664 if (font->y_scale < 0) y_shift = -y_shift;
665 extents->y_bearing += y_shift;
666 extents->height -= y_shift;
667
668 /* X */
669 int x_shift = font->x_strength;
670 if (font->x_scale < 0) x_shift = -x_shift;
671 if (font->embolden_in_place)
672 extents->x_bearing -= x_shift / 2;
673 extents->width += x_shift;
674 }
675
676 return true;
677}
678
679static hb_bool_t
680hb_ft_get_glyph_contour_point (hb_font_t *font HB_UNUSED,
681 void *font_data,
682 hb_codepoint_t glyph,
683 unsigned int point_index,
684 hb_position_t *x,
685 hb_position_t *y,
686 void *user_data HB_UNUSED)
687{
688 const hb_ft_font_t *ft_font = (const hb_ft_font_t *) font_data;
689 hb_lock_t lock (ft_font->lock);
690 FT_Face ft_face = ft_font->ft_face;
691
692 if (unlikely (FT_Load_Glyph (ft_face, glyph, ft_font->load_flags)))
693 return false;
694
695 if (unlikely (ft_face->glyph->format != FT_GLYPH_FORMAT_OUTLINE))
696 return false;
697
698 if (unlikely (point_index >= (unsigned int) ft_face->glyph->outline.n_points))
699 return false;
700
701 *x = ft_face->glyph->outline.points[point_index].x;
702 *y = ft_face->glyph->outline.points[point_index].y;
703
704 return true;
705}
706
707static hb_bool_t
708hb_ft_get_glyph_name (hb_font_t *font HB_UNUSED,
709 void *font_data,
710 hb_codepoint_t glyph,
711 char *name, unsigned int size,
712 void *user_data HB_UNUSED)
713{
714 const hb_ft_font_t *ft_font = (const hb_ft_font_t *) font_data;
715 hb_lock_t lock (ft_font->lock);
716 FT_Face ft_face = ft_font->ft_face;
717
718 hb_bool_t ret = !FT_Get_Glyph_Name (ft_face, glyph, name, size);
719 if (ret && (size && !*name))
720 ret = false;
721
722 return ret;
723}
724
725static hb_bool_t
726hb_ft_get_glyph_from_name (hb_font_t *font HB_UNUSED,
727 void *font_data,
728 const char *name, int len, /* -1 means nul-terminated */
729 hb_codepoint_t *glyph,
730 void *user_data HB_UNUSED)
731{
732 const hb_ft_font_t *ft_font = (const hb_ft_font_t *) font_data;
733 hb_lock_t lock (ft_font->lock);
734 FT_Face ft_face = ft_font->ft_face;
735
736 if (len < 0)
737 *glyph = FT_Get_Name_Index (ft_face, (FT_String *) name);
738 else {
739 /* Make a nul-terminated version. */
740 char buf[128];
741 len = hb_min (len, (int) sizeof (buf) - 1);
742 strncpy (buf, name, len);
743 buf[len] = '\0';
744 *glyph = FT_Get_Name_Index (ft_face, buf);
745 }
746
747 if (*glyph == 0)
748 {
749 /* Check whether the given name was actually the name of glyph 0. */
750 char buf[128];
751 if (!FT_Get_Glyph_Name(ft_face, 0, buf, sizeof (buf)) &&
752 len < 0 ? !strcmp (buf, name) : !strncmp (buf, name, len))
753 return true;
754 }
755
756 return *glyph != 0;
757}
758
759static hb_bool_t
760hb_ft_get_font_h_extents (hb_font_t *font HB_UNUSED,
761 void *font_data,
762 hb_font_extents_t *metrics,
763 void *user_data HB_UNUSED)
764{
765 const hb_ft_font_t *ft_font = (const hb_ft_font_t *) font_data;
766 hb_lock_t lock (ft_font->lock);
767 FT_Face ft_face = ft_font->ft_face;
768 float y_mult;
769#ifdef HAVE_FT_GET_TRANSFORM
770 if (ft_font->transform)
771 {
772 FT_Matrix matrix;
773 FT_Get_Transform (ft_face, &matrix, nullptr);
774 y_mult = sqrtf ((float)matrix.yx * matrix.yx + (float)matrix.yy * matrix.yy) / 65536.f;
775 y_mult *= font->y_scale < 0 ? -1 : +1;
776 }
777 else
778#endif
779 {
780 y_mult = font->y_scale < 0 ? -1 : +1;
781 }
782
783 if (ft_face->units_per_EM != 0)
784 {
785 metrics->ascender = FT_MulFix(ft_face->ascender, ft_face->size->metrics.y_scale);
786 metrics->descender = FT_MulFix(ft_face->descender, ft_face->size->metrics.y_scale);
787 metrics->line_gap = FT_MulFix( ft_face->height, ft_face->size->metrics.y_scale ) - (metrics->ascender - metrics->descender);
788 }
789 else
790 {
791 /* Bitmap-only font, eg. color bitmap font. */
792 metrics->ascender = ft_face->size->metrics.ascender;
793 metrics->descender = ft_face->size->metrics.descender;
794 metrics->line_gap = ft_face->size->metrics.height - (metrics->ascender - metrics->descender);
795 }
796
797 metrics->ascender = (hb_position_t) (y_mult * (metrics->ascender + font->y_strength));
798 metrics->descender = (hb_position_t) (y_mult * metrics->descender);
799 metrics->line_gap = (hb_position_t) (y_mult * metrics->line_gap);
800
801 return true;
802}
803
804#ifndef HB_NO_DRAW
805
806static int
807_hb_ft_move_to (const FT_Vector *to,
808 void *arg)
809{
810 hb_draw_session_t *drawing = (hb_draw_session_t *) arg;
811 drawing->move_to (to->x, to->y);
812 return FT_Err_Ok;
813}
814
815static int
816_hb_ft_line_to (const FT_Vector *to,
817 void *arg)
818{
819 hb_draw_session_t *drawing = (hb_draw_session_t *) arg;
820 drawing->line_to (to->x, to->y);
821 return FT_Err_Ok;
822}
823
824static int
825_hb_ft_conic_to (const FT_Vector *control,
826 const FT_Vector *to,
827 void *arg)
828{
829 hb_draw_session_t *drawing = (hb_draw_session_t *) arg;
830 drawing->quadratic_to (control->x, control->y,
831 to->x, to->y);
832 return FT_Err_Ok;
833}
834
835static int
836_hb_ft_cubic_to (const FT_Vector *control1,
837 const FT_Vector *control2,
838 const FT_Vector *to,
839 void *arg)
840{
841 hb_draw_session_t *drawing = (hb_draw_session_t *) arg;
842 drawing->cubic_to (control1->x, control1->y,
843 control2->x, control2->y,
844 to->x, to->y);
845 return FT_Err_Ok;
846}
847
848static void
849hb_ft_draw_glyph (hb_font_t *font,
850 void *font_data,
851 hb_codepoint_t glyph,
852 hb_draw_funcs_t *draw_funcs, void *draw_data,
853 void *user_data HB_UNUSED)
854{
855 const hb_ft_font_t *ft_font = (const hb_ft_font_t *) font_data;
856 hb_lock_t lock (ft_font->lock);
857 FT_Face ft_face = ft_font->ft_face;
858
859 if (unlikely (FT_Load_Glyph (ft_face, glyph,
860 FT_LOAD_NO_BITMAP | ft_font->load_flags)))
861 return;
862
863 if (ft_face->glyph->format != FT_GLYPH_FORMAT_OUTLINE)
864 return;
865
866 const FT_Outline_Funcs outline_funcs = {
867 _hb_ft_move_to,
868 _hb_ft_line_to,
869 _hb_ft_conic_to,
870 _hb_ft_cubic_to,
871 0, /* shift */
872 0, /* delta */
873 };
874
875 hb_draw_session_t draw_session (draw_funcs, draw_data, font->slant_xy);
876
877 /* Embolden */
878 if (font->x_strength || font->y_strength)
879 {
880 FT_Outline_EmboldenXY (&ft_face->glyph->outline, font->x_strength, font->y_strength);
881
882 int x_shift = 0;
883 int y_shift = 0;
884 if (font->embolden_in_place)
885 {
886 /* Undo the FreeType shift. */
887 x_shift = -font->x_strength / 2;
888 y_shift = 0;
889 if (font->y_scale < 0) y_shift = -font->y_strength;
890 }
891 else
892 {
893 /* FreeType applied things in the wrong direction for negative scale; fix up. */
894 if (font->x_scale < 0) x_shift = -font->x_strength;
895 if (font->y_scale < 0) y_shift = -font->y_strength;
896 }
897 if (x_shift || y_shift)
898 {
899 auto &outline = ft_face->glyph->outline;
900 for (auto &point : hb_iter (outline.points, outline.contours[outline.n_contours - 1] + 1))
901 {
902 point.x += x_shift;
903 point.y += y_shift;
904 }
905 }
906 }
907
908
909 FT_Outline_Decompose (&ft_face->glyph->outline,
910 &outline_funcs,
911 &draw_session);
912}
913#endif
914
915#ifndef HB_NO_PAINT
916#if (FREETYPE_MAJOR*10000 + FREETYPE_MINOR*100 + FREETYPE_PATCH) >= 21300
917
918#include "hb-ft-colr.hh"
919
920static void
921hb_ft_paint_glyph (hb_font_t *font,
922 void *font_data,
923 hb_codepoint_t gid,
924 hb_paint_funcs_t *paint_funcs, void *paint_data,
925 unsigned int palette_index,
926 hb_color_t foreground,
927 void *user_data)
928{
929 const hb_ft_font_t *ft_font = (const hb_ft_font_t *) font_data;
930 hb_lock_t lock (ft_font->lock);
931 FT_Face ft_face = ft_font->ft_face;
932
933 /* We release the lock before calling into glyph callbacks, such that
934 * eg. draw API can call back into the face.*/
935
936 if (unlikely (FT_Load_Glyph (ft_face, gid,
937 ft_font->load_flags | FT_LOAD_COLOR)))
938 return;
939
940 if (ft_face->glyph->format == FT_GLYPH_FORMAT_OUTLINE)
941 {
942 if (hb_ft_paint_glyph_colr (font, font_data, gid,
943 paint_funcs, paint_data,
944 palette_index, foreground,
945 user_data))
946 return;
947
948 /* Simple outline. */
949 ft_font->lock.unlock ();
950 paint_funcs->push_clip_glyph (paint_data, gid, font);
951 ft_font->lock.lock ();
952 paint_funcs->color (paint_data, true, foreground);
953 paint_funcs->pop_clip (paint_data);
954
955 return;
956 }
957
958 auto *glyph = ft_face->glyph;
959 if (glyph->format == FT_GLYPH_FORMAT_BITMAP)
960 {
961 auto &bitmap = glyph->bitmap;
962 if (bitmap.pixel_mode == FT_PIXEL_MODE_BGRA)
963 {
964 if (bitmap.pitch != (signed) bitmap.width * 4)
965 return;
966
967 ft_font->lock.unlock ();
968
969 hb_blob_t *blob = hb_blob_create ((const char *) bitmap.buffer,
970 bitmap.pitch * bitmap.rows,
971 HB_MEMORY_MODE_DUPLICATE,
972 nullptr, nullptr);
973
974 hb_glyph_extents_t extents;
975 if (!hb_font_get_glyph_extents (font, gid, &extents))
976 goto out;
977
978 if (!paint_funcs->image (paint_data,
979 blob,
980 bitmap.width,
981 bitmap.rows,
982 HB_PAINT_IMAGE_FORMAT_BGRA,
983 font->slant_xy,
984 &extents))
985 {
986 /* TODO Try a forced outline load and paint? */
987 }
988
989 out:
990 hb_blob_destroy (blob);
991 ft_font->lock.lock ();
992 }
993
994 return;
995 }
996}
997#endif
998#endif
999
1000
1001static inline void free_static_ft_funcs ();
1002
1003static struct hb_ft_font_funcs_lazy_loader_t : hb_font_funcs_lazy_loader_t<hb_ft_font_funcs_lazy_loader_t>
1004{
1005 static hb_font_funcs_t *create ()
1006 {
1007 hb_font_funcs_t *funcs = hb_font_funcs_create ();
1008
1009 hb_font_funcs_set_nominal_glyph_func (funcs, hb_ft_get_nominal_glyph, nullptr, nullptr);
1010 hb_font_funcs_set_nominal_glyphs_func (funcs, hb_ft_get_nominal_glyphs, nullptr, nullptr);
1011 hb_font_funcs_set_variation_glyph_func (funcs, hb_ft_get_variation_glyph, nullptr, nullptr);
1012
1013 hb_font_funcs_set_font_h_extents_func (funcs, hb_ft_get_font_h_extents, nullptr, nullptr);
1014 hb_font_funcs_set_glyph_h_advances_func (funcs, hb_ft_get_glyph_h_advances, nullptr, nullptr);
1015 //hb_font_funcs_set_glyph_h_origin_func (funcs, hb_ft_get_glyph_h_origin, nullptr, nullptr);
1016
1017#ifndef HB_NO_VERTICAL
1018 //hb_font_funcs_set_font_v_extents_func (funcs, hb_ft_get_font_v_extents, nullptr, nullptr);
1019 hb_font_funcs_set_glyph_v_advance_func (funcs, hb_ft_get_glyph_v_advance, nullptr, nullptr);
1020 hb_font_funcs_set_glyph_v_origin_func (funcs, hb_ft_get_glyph_v_origin, nullptr, nullptr);
1021#endif
1022
1023#ifndef HB_NO_OT_SHAPE_FALLBACK
1024 hb_font_funcs_set_glyph_h_kerning_func (funcs, hb_ft_get_glyph_h_kerning, nullptr, nullptr);
1025#endif
1026 //hb_font_funcs_set_glyph_v_kerning_func (funcs, hb_ft_get_glyph_v_kerning, nullptr, nullptr);
1027 hb_font_funcs_set_glyph_extents_func (funcs, hb_ft_get_glyph_extents, nullptr, nullptr);
1028 hb_font_funcs_set_glyph_contour_point_func (funcs, hb_ft_get_glyph_contour_point, nullptr, nullptr);
1029 hb_font_funcs_set_glyph_name_func (funcs, hb_ft_get_glyph_name, nullptr, nullptr);
1030 hb_font_funcs_set_glyph_from_name_func (funcs, hb_ft_get_glyph_from_name, nullptr, nullptr);
1031
1032#ifndef HB_NO_DRAW
1033 hb_font_funcs_set_draw_glyph_func (funcs, hb_ft_draw_glyph, nullptr, nullptr);
1034#endif
1035
1036#ifndef HB_NO_PAINT
1037#if (FREETYPE_MAJOR*10000 + FREETYPE_MINOR*100 + FREETYPE_PATCH) >= 21300
1038 hb_font_funcs_set_paint_glyph_func (funcs, hb_ft_paint_glyph, nullptr, nullptr);
1039#endif
1040#endif
1041
1042 hb_font_funcs_make_immutable (funcs);
1043
1044 hb_atexit (free_static_ft_funcs);
1045
1046 return funcs;
1047 }
1048} static_ft_funcs;
1049
1050static inline
1051void free_static_ft_funcs ()
1052{
1053 static_ft_funcs.free_instance ();
1054}
1055
1056static hb_font_funcs_t *
1057_hb_ft_get_font_funcs ()
1058{
1059 return static_ft_funcs.get_unconst ();
1060}
1061
1062static void
1063_hb_ft_font_set_funcs (hb_font_t *font, FT_Face ft_face, bool unref)
1064{
1065 bool symbol = ft_face->charmap && ft_face->charmap->encoding == FT_ENCODING_MS_SYMBOL;
1066
1067 hb_ft_font_t *ft_font = _hb_ft_font_create (ft_face, symbol, unref);
1068 if (unlikely (!ft_font)) return;
1069
1070 hb_font_set_funcs (font,
1071 _hb_ft_get_font_funcs (),
1072 ft_font,
1073 _hb_ft_font_destroy);
1074}
1075
1076
1077static hb_blob_t *
1078_hb_ft_reference_table (hb_face_t *face HB_UNUSED, hb_tag_t tag, void *user_data)
1079{
1080 FT_Face ft_face = (FT_Face) user_data;
1081 FT_Byte *buffer;
1082 FT_ULong length = 0;
1083 FT_Error error;
1084
1085 /* Note: FreeType like HarfBuzz uses the NONE tag for fetching the entire blob */
1086
1087 error = FT_Load_Sfnt_Table (ft_face, tag, 0, nullptr, &length);
1088 if (error)
1089 return nullptr;
1090
1091 buffer = (FT_Byte *) hb_malloc (length);
1092 if (!buffer)
1093 return nullptr;
1094
1095 error = FT_Load_Sfnt_Table (ft_face, tag, 0, buffer, &length);
1096 if (error)
1097 {
1098 hb_free (buffer);
1099 return nullptr;
1100 }
1101
1102 return hb_blob_create ((const char *) buffer, length,
1103 HB_MEMORY_MODE_WRITABLE,
1104 buffer, hb_free);
1105}
1106
1107/**
1108 * hb_ft_face_create:
1109 * @ft_face: (destroy destroy) (scope notified): FT_Face to work upon
1110 * @destroy: (nullable): A callback to call when the face object is not needed anymore
1111 *
1112 * Creates an #hb_face_t face object from the specified FT_Face.
1113 *
1114 * Note that this is using the FT_Face object just to get at the underlying
1115 * font data, and fonts created from the returned #hb_face_t will use the native
1116 * HarfBuzz font implementation, unless you call hb_ft_font_set_funcs() on them.
1117 *
1118 * This variant of the function does not provide any life-cycle management.
1119 *
1120 * Most client programs should use hb_ft_face_create_referenced()
1121 * (or, perhaps, hb_ft_face_create_cached()) instead.
1122 *
1123 * If you know you have valid reasons not to use hb_ft_face_create_referenced(),
1124 * then it is the client program's responsibility to destroy @ft_face
1125 * after the #hb_face_t face object has been destroyed.
1126 *
1127 * Return value: (transfer full): the new #hb_face_t face object
1128 *
1129 * Since: 0.9.2
1130 **/
1131hb_face_t *
1132hb_ft_face_create (FT_Face ft_face,
1133 hb_destroy_func_t destroy)
1134{
1135 hb_face_t *face;
1136
1137 if (!ft_face->stream->read) {
1138 hb_blob_t *blob;
1139
1140 blob = hb_blob_create ((const char *) ft_face->stream->base,
1141 (unsigned int) ft_face->stream->size,
1142 HB_MEMORY_MODE_READONLY,
1143 ft_face, destroy);
1144 face = hb_face_create (blob, ft_face->face_index);
1145 hb_blob_destroy (blob);
1146 } else {
1147 face = hb_face_create_for_tables (_hb_ft_reference_table, ft_face, destroy);
1148 }
1149
1150 hb_face_set_index (face, ft_face->face_index);
1151 hb_face_set_upem (face, ft_face->units_per_EM);
1152
1153 return face;
1154}
1155
1156/**
1157 * hb_ft_face_create_referenced:
1158 * @ft_face: FT_Face to work upon
1159 *
1160 * Creates an #hb_face_t face object from the specified FT_Face.
1161 *
1162 * Note that this is using the FT_Face object just to get at the underlying
1163 * font data, and fonts created from the returned #hb_face_t will use the native
1164 * HarfBuzz font implementation, unless you call hb_ft_font_set_funcs() on them.
1165 *
1166 * This is the preferred variant of the hb_ft_face_create*
1167 * function family, because it calls FT_Reference_Face() on @ft_face,
1168 * ensuring that @ft_face remains alive as long as the resulting
1169 * #hb_face_t face object remains alive. Also calls FT_Done_Face()
1170 * when the #hb_face_t face object is destroyed.
1171 *
1172 * Use this version unless you know you have good reasons not to.
1173 *
1174 * Return value: (transfer full): the new #hb_face_t face object
1175 *
1176 * Since: 0.9.38
1177 **/
1178hb_face_t *
1179hb_ft_face_create_referenced (FT_Face ft_face)
1180{
1181 FT_Reference_Face (ft_face);
1182 return hb_ft_face_create (ft_face, _hb_ft_face_destroy);
1183}
1184
1185static void
1186hb_ft_face_finalize (void *arg)
1187{
1188 FT_Face ft_face = (FT_Face) arg;
1189 hb_face_destroy ((hb_face_t *) ft_face->generic.data);
1190}
1191
1192/**
1193 * hb_ft_face_create_cached:
1194 * @ft_face: FT_Face to work upon
1195 *
1196 * Creates an #hb_face_t face object from the specified FT_Face.
1197 *
1198 * Note that this is using the FT_Face object just to get at the underlying
1199 * font data, and fonts created from the returned #hb_face_t will use the native
1200 * HarfBuzz font implementation, unless you call hb_ft_font_set_funcs() on them.
1201 *
1202 * This variant of the function caches the newly created #hb_face_t
1203 * face object, using the @generic pointer of @ft_face. Subsequent function
1204 * calls that are passed the same @ft_face parameter will have the same
1205 * #hb_face_t returned to them, and that #hb_face_t will be correctly
1206 * reference counted.
1207 *
1208 * However, client programs are still responsible for destroying
1209 * @ft_face after the last #hb_face_t face object has been destroyed.
1210 *
1211 * Return value: (transfer full): the new #hb_face_t face object
1212 *
1213 * Since: 0.9.2
1214 **/
1215hb_face_t *
1216hb_ft_face_create_cached (FT_Face ft_face)
1217{
1218 if (unlikely (!ft_face->generic.data || ft_face->generic.finalizer != (FT_Generic_Finalizer) hb_ft_face_finalize))
1219 {
1220 if (ft_face->generic.finalizer)
1221 ft_face->generic.finalizer (ft_face);
1222
1223 ft_face->generic.data = hb_ft_face_create (ft_face, nullptr);
1224 ft_face->generic.finalizer = hb_ft_face_finalize;
1225 }
1226
1227 return hb_face_reference ((hb_face_t *) ft_face->generic.data);
1228}
1229
1230/**
1231 * hb_ft_font_create:
1232 * @ft_face: (destroy destroy) (scope notified): FT_Face to work upon
1233 * @destroy: (nullable): A callback to call when the font object is not needed anymore
1234 *
1235 * Creates an #hb_font_t font object from the specified FT_Face.
1236 *
1237 * <note>Note: You must set the face size on @ft_face before calling
1238 * hb_ft_font_create() on it. HarfBuzz assumes size is always set and will
1239 * access `size` member of FT_Face unconditionally.</note>
1240 *
1241 * This variant of the function does not provide any life-cycle management.
1242 *
1243 * Most client programs should use hb_ft_font_create_referenced()
1244 * instead.
1245 *
1246 * If you know you have valid reasons not to use hb_ft_font_create_referenced(),
1247 * then it is the client program's responsibility to destroy @ft_face
1248 * after the #hb_font_t font object has been destroyed.
1249 *
1250 * HarfBuzz will use the @destroy callback on the #hb_font_t font object
1251 * if it is supplied when you use this function. However, even if @destroy
1252 * is provided, it is the client program's responsibility to destroy @ft_face,
1253 * and it is the client program's responsibility to ensure that @ft_face is
1254 * destroyed only after the #hb_font_t font object has been destroyed.
1255 *
1256 * Return value: (transfer full): the new #hb_font_t font object
1257 *
1258 * Since: 0.9.2
1259 **/
1260hb_font_t *
1261hb_ft_font_create (FT_Face ft_face,
1262 hb_destroy_func_t destroy)
1263{
1264 hb_font_t *font;
1265 hb_face_t *face;
1266
1267 face = hb_ft_face_create (ft_face, destroy);
1268 font = hb_font_create (face);
1269 hb_face_destroy (face);
1270 _hb_ft_font_set_funcs (font, ft_face, false);
1271 hb_ft_font_changed (font);
1272 return font;
1273}
1274
1275/**
1276 * hb_ft_font_changed:
1277 * @font: #hb_font_t to work upon
1278 *
1279 * Refreshes the state of @font when the underlying FT_Face has changed.
1280 * This function should be called after changing the size or
1281 * variation-axis settings on the FT_Face.
1282 *
1283 * Since: 1.0.5
1284 **/
1285void
1286hb_ft_font_changed (hb_font_t *font)
1287{
1288 if (font->destroy != (hb_destroy_func_t) _hb_ft_font_destroy)
1289 return;
1290
1291 hb_ft_font_t *ft_font = (hb_ft_font_t *) font->user_data;
1292
1293 FT_Face ft_face = ft_font->ft_face;
1294
1295 hb_font_set_scale (font,
1296 (int) (((uint64_t) ft_face->size->metrics.x_scale * (uint64_t) ft_face->units_per_EM + (1u<<15)) >> 16),
1297 (int) (((uint64_t) ft_face->size->metrics.y_scale * (uint64_t) ft_face->units_per_EM + (1u<<15)) >> 16));
1298#if 0 /* hb-ft works in no-hinting model */
1299 hb_font_set_ppem (font,
1300 ft_face->size->metrics.x_ppem,
1301 ft_face->size->metrics.y_ppem);
1302#endif
1303
1304#if defined(HAVE_FT_GET_VAR_BLEND_COORDINATES) && !defined(HB_NO_VAR)
1305 FT_MM_Var *mm_var = nullptr;
1306 if (!FT_Get_MM_Var (ft_face, &mm_var))
1307 {
1308 FT_Fixed *ft_coords = (FT_Fixed *) hb_calloc (mm_var->num_axis, sizeof (FT_Fixed));
1309 int *coords = (int *) hb_calloc (mm_var->num_axis, sizeof (int));
1310 if (coords && ft_coords)
1311 {
1312 if (!FT_Get_Var_Blend_Coordinates (ft_face, mm_var->num_axis, ft_coords))
1313 {
1314 bool nonzero = false;
1315
1316 for (unsigned int i = 0; i < mm_var->num_axis; ++i)
1317 {
1318 coords[i] = ft_coords[i] >>= 2;
1319 nonzero = nonzero || coords[i];
1320 }
1321
1322 if (nonzero)
1323 hb_font_set_var_coords_normalized (font, coords, mm_var->num_axis);
1324 else
1325 hb_font_set_var_coords_normalized (font, nullptr, 0);
1326 }
1327 }
1328 hb_free (coords);
1329 hb_free (ft_coords);
1330#ifdef HAVE_FT_DONE_MM_VAR
1331 FT_Done_MM_Var (ft_face->glyph->library, mm_var);
1332#else
1333 hb_free (mm_var);
1334#endif
1335 }
1336#endif
1337
1338 ft_font->advance_cache.clear ();
1339 ft_font->cached_serial = font->serial;
1340}
1341
1342/**
1343 * hb_ft_hb_font_changed:
1344 * @font: #hb_font_t to work upon
1345 *
1346 * Refreshes the state of the underlying FT_Face of @font when the hb_font_t
1347 * @font has changed.
1348 * This function should be called after changing the size or
1349 * variation-axis settings on the @font.
1350 * This call is fast if nothing has changed on @font.
1351 *
1352 * Return value: true if changed, false otherwise
1353 *
1354 * Since: 4.4.0
1355 **/
1356hb_bool_t
1357hb_ft_hb_font_changed (hb_font_t *font)
1358{
1359 if (font->destroy != (hb_destroy_func_t) _hb_ft_font_destroy)
1360 return false;
1361
1362 hb_ft_font_t *ft_font = (hb_ft_font_t *) font->user_data;
1363
1364 return _hb_ft_hb_font_check_changed (font, ft_font);
1365}
1366
1367/**
1368 * hb_ft_font_create_referenced:
1369 * @ft_face: FT_Face to work upon
1370 *
1371 * Creates an #hb_font_t font object from the specified FT_Face.
1372 *
1373 * <note>Note: You must set the face size on @ft_face before calling
1374 * hb_ft_font_create_referenced() on it. HarfBuzz assumes size is always set
1375 * and will access `size` member of FT_Face unconditionally.</note>
1376 *
1377 * This is the preferred variant of the hb_ft_font_create*
1378 * function family, because it calls FT_Reference_Face() on @ft_face,
1379 * ensuring that @ft_face remains alive as long as the resulting
1380 * #hb_font_t font object remains alive.
1381 *
1382 * Use this version unless you know you have good reasons not to.
1383 *
1384 * Return value: (transfer full): the new #hb_font_t font object
1385 *
1386 * Since: 0.9.38
1387 **/
1388hb_font_t *
1389hb_ft_font_create_referenced (FT_Face ft_face)
1390{
1391 FT_Reference_Face (ft_face);
1392 return hb_ft_font_create (ft_face, _hb_ft_face_destroy);
1393}
1394
1395static inline void free_static_ft_library ();
1396
1397static struct hb_ft_library_lazy_loader_t : hb_lazy_loader_t<hb_remove_pointer<FT_Library>,
1398 hb_ft_library_lazy_loader_t>
1399{
1400 static FT_Library create ()
1401 {
1402 FT_Library l;
1403 if (FT_Init_FreeType (&l))
1404 return nullptr;
1405
1406 hb_atexit (free_static_ft_library);
1407
1408 return l;
1409 }
1410 static void destroy (FT_Library l)
1411 {
1412 FT_Done_FreeType (l);
1413 }
1414 static FT_Library get_null ()
1415 {
1416 return nullptr;
1417 }
1418} static_ft_library;
1419
1420static inline
1421void free_static_ft_library ()
1422{
1423 static_ft_library.free_instance ();
1424}
1425
1426static FT_Library
1427get_ft_library ()
1428{
1429 return static_ft_library.get_unconst ();
1430}
1431
1432static void
1433_release_blob (void *arg)
1434{
1435 FT_Face ft_face = (FT_Face) arg;
1436 hb_blob_destroy ((hb_blob_t *) ft_face->generic.data);
1437}
1438
1439/**
1440 * hb_ft_font_set_funcs:
1441 * @font: #hb_font_t to work upon
1442 *
1443 * Configures the font-functions structure of the specified
1444 * #hb_font_t font object to use FreeType font functions.
1445 *
1446 * In particular, you can use this function to configure an
1447 * existing #hb_face_t face object for use with FreeType font
1448 * functions even if that #hb_face_t face object was initially
1449 * created with hb_face_create(), and therefore was not
1450 * initially configured to use FreeType font functions.
1451 *
1452 * An #hb_font_t object created with hb_ft_font_create()
1453 * is preconfigured for FreeType font functions and does not
1454 * require this function to be used.
1455 *
1456 * Note that if you modify the underlying #hb_font_t after
1457 * calling this function, you need to call hb_ft_hb_font_changed()
1458 * to update the underlying FT_Face.
1459 *
1460 * <note>Note: Internally, this function creates an FT_Face.
1461* </note>
1462 *
1463 * Since: 1.0.5
1464 **/
1465void
1466hb_ft_font_set_funcs (hb_font_t *font)
1467{
1468 hb_blob_t *blob = hb_face_reference_blob (font->face);
1469 unsigned int blob_length;
1470 const char *blob_data = hb_blob_get_data (blob, &blob_length);
1471 if (unlikely (!blob_length))
1472 DEBUG_MSG (FT, font, "Font face has empty blob");
1473
1474 FT_Face ft_face = nullptr;
1475 FT_Error err = FT_New_Memory_Face (get_ft_library (),
1476 (const FT_Byte *) blob_data,
1477 blob_length,
1478 hb_face_get_index (font->face),
1479 &ft_face);
1480
1481 if (unlikely (err)) {
1482 hb_blob_destroy (blob);
1483 DEBUG_MSG (FT, font, "Font face FT_New_Memory_Face() failed");
1484 return;
1485 }
1486
1487 if (FT_Select_Charmap (ft_face, FT_ENCODING_MS_SYMBOL))
1488 FT_Select_Charmap (ft_face, FT_ENCODING_UNICODE);
1489
1490
1491 ft_face->generic.data = blob;
1492 ft_face->generic.finalizer = _release_blob;
1493
1494 _hb_ft_font_set_funcs (font, ft_face, true);
1495 hb_ft_font_set_load_flags (font, FT_LOAD_DEFAULT | FT_LOAD_NO_HINTING);
1496
1497 _hb_ft_hb_font_changed (font, ft_face);
1498}
1499
1500#endif
1501