2 * Copyright © 2009 Red Hat, Inc.
3 * Copyright © 2009 Keith Stribley
4 * Copyright © 2015 Google, Inc.
6 * This is part of HarfBuzz, a text shaping library.
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.
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
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.
26 * Red Hat Author(s): Behdad Esfahbod
27 * Google Author(s): Behdad Esfahbod
36 #include "hb-cache.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"
44 #include FT_ADVANCES_H
45 #include FT_MULTIPLE_MASTERS_H
47 #include FT_TRUETYPE_TABLES_H
48 #include FT_SYNTHESIS_H
49 #if (FREETYPE_MAJOR*10000 + FREETYPE_MINOR*100 + FREETYPE_PATCH) >= 21300
57 * @short_description: FreeType integration
60 * Functions for using HarfBuzz with the FreeType library.
62 * HarfBuzz supports using FreeType to provide face and
65 * <note>Note that FreeType is not thread-safe, therefore these
66 * functions are not thread-safe either.</note>
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:
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
82 * - We don't handle / allow for emboldening / obliqueing.
84 * - In the future, we should add constructors to create fonts in font space?
88 using hb_ft_advance_cache_t = hb_cache_t<16, 24, 8, false>;
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. */
97 mutable hb_mutex_t lock; /* Protects members below. */
99 mutable unsigned cached_serial;
100 mutable hb_ft_advance_cache_t advance_cache;
103 static hb_ft_font_t *
104 _hb_ft_font_create (FT_Face ft_face, bool symbol, bool unref)
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;
109 ft_font->lock.init ();
110 ft_font->ft_face = ft_face;
111 ft_font->symbol = symbol;
112 ft_font->unref = unref;
114 ft_font->load_flags = FT_LOAD_DEFAULT | FT_LOAD_NO_HINTING;
116 ft_font->cached_serial = (unsigned) -1;
117 new (&ft_font->advance_cache) hb_ft_advance_cache_t;
123 _hb_ft_face_destroy (void *data)
125 FT_Done_Face ((FT_Face) data);
129 _hb_ft_font_destroy (void *data)
131 hb_ft_font_t *ft_font = (hb_ft_font_t *) data;
134 _hb_ft_face_destroy (ft_font->ft_face);
136 ft_font->lock.fini ();
142 /* hb_font changed, update FT_Face. */
143 static void _hb_ft_hb_font_changed (hb_font_t *font, FT_Face ft_face)
145 hb_ft_font_t *ft_font = (hb_ft_font_t *) font->user_data;
147 float x_mult = 1.f, y_mult = 1.f;
149 if (font->x_scale < 0) x_mult = -x_mult;
150 if (font->y_scale < 0) y_mult = -y_mult;
152 if (FT_Set_Char_Size (ft_face,
153 abs (font->x_scale), abs (font->y_scale),
156 font->x_ppem * 72 * 64 / font->x_scale,
157 font->y_ppem * 72 * 64 / font->y_scale
159 ) && ft_face->num_fixed_sizes)
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,
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;
179 if (x_mult != 1.f || y_mult != 1.f)
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;
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);
192 FT_Fixed *ft_coords = (FT_Fixed *) hb_calloc (num_coords, sizeof (FT_Fixed));
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);
204 /* Check if hb_font changed, update FT_Face. */
206 _hb_ft_hb_font_check_changed (hb_font_t *font,
207 const hb_ft_font_t *ft_font)
209 if (font->serial != ft_font->cached_serial)
211 _hb_ft_hb_font_changed (font, ft_font->ft_face);
212 ft_font->advance_cache.clear ();
213 ft_font->cached_serial = font->serial;
221 * hb_ft_font_set_load_flags:
222 * @font: #hb_font_t to work upon
223 * @load_flags: The FreeType load flags to set
225 * Sets the FT_Load_Glyph load flags for the specified #hb_font_t.
227 * For more information, see
228 * https://www.freetype.org/freetype2/docs/reference/ft2-base_interface.html#ft_load_xxx
230 * This function works with #hb_font_t objects created by
231 * hb_ft_font_create() or hb_ft_font_create_referenced().
236 hb_ft_font_set_load_flags (hb_font_t *font, int load_flags)
238 if (hb_object_is_immutable (font))
241 if (unlikely (font->destroy != (hb_destroy_func_t) _hb_ft_font_destroy))
244 hb_ft_font_t *ft_font = (hb_ft_font_t *) font->user_data;
246 ft_font->load_flags = load_flags;
250 * hb_ft_font_get_load_flags:
251 * @font: #hb_font_t to work upon
253 * Fetches the FT_Load_Glyph load flags of the specified #hb_font_t.
255 * For more information, see
256 * https://www.freetype.org/freetype2/docs/reference/ft2-base_interface.html#ft_load_xxx
258 * This function works with #hb_font_t objects created by
259 * hb_ft_font_create() or hb_ft_font_create_referenced().
261 * Return value: FT_Load_Glyph flags found, or 0
266 hb_ft_font_get_load_flags (hb_font_t *font)
268 if (unlikely (font->destroy != (hb_destroy_func_t) _hb_ft_font_destroy))
271 const hb_ft_font_t *ft_font = (const hb_ft_font_t *) font->user_data;
273 return ft_font->load_flags;
277 * hb_ft_font_get_face: (skip)
278 * @font: #hb_font_t to work upon
280 * Fetches the FT_Face associated with the specified #hb_font_t
283 * This function works with #hb_font_t objects created by
284 * hb_ft_font_create() or hb_ft_font_create_referenced().
286 * Return value: (nullable): the FT_Face found or `NULL`
291 hb_ft_font_get_face (hb_font_t *font)
293 if (unlikely (font->destroy != (hb_destroy_func_t) _hb_ft_font_destroy))
296 const hb_ft_font_t *ft_font = (const hb_ft_font_t *) font->user_data;
298 return ft_font->ft_face;
302 * hb_ft_font_lock_face: (skip)
303 * @font: #hb_font_t to work upon
305 * Gets the FT_Face associated with @font.
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().
310 * This function works with #hb_font_t objects created by
311 * hb_ft_font_create() or hb_ft_font_create_referenced().
313 * Return value: (nullable) (transfer none): the FT_Face associated with @font or `NULL`
317 hb_ft_font_lock_face (hb_font_t *font)
319 if (unlikely (font->destroy != (hb_destroy_func_t) _hb_ft_font_destroy))
322 const hb_ft_font_t *ft_font = (const hb_ft_font_t *) font->user_data;
324 ft_font->lock.lock ();
326 return ft_font->ft_face;
330 * hb_ft_font_unlock_face: (skip)
331 * @font: #hb_font_t to work upon
333 * Releases an FT_Face previously obtained with hb_ft_font_lock_face().
338 hb_ft_font_unlock_face (hb_font_t *font)
340 if (unlikely (font->destroy != (hb_destroy_func_t) _hb_ft_font_destroy))
343 const hb_ft_font_t *ft_font = (const hb_ft_font_t *) font->user_data;
345 ft_font->lock.unlock ();
350 hb_ft_get_nominal_glyph (hb_font_t *font,
352 hb_codepoint_t unicode,
353 hb_codepoint_t *glyph,
354 void *user_data HB_UNUSED)
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);
362 if (unlikely (ft_font->symbol))
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);
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));
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));
397 hb_ft_get_nominal_glyphs (hb_font_t *font HB_UNUSED,
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)
406 const hb_ft_font_t *ft_font = (const hb_ft_font_t *) font_data;
407 hb_lock_t lock (ft_font->lock);
410 done < count && (*first_glyph = FT_Get_Char_Index (ft_font->ft_face, *first_unicode));
413 first_unicode = &StructAtOffsetUnaligned<hb_codepoint_t> (first_unicode, unicode_stride);
414 first_glyph = &StructAtOffsetUnaligned<hb_codepoint_t> (first_glyph, glyph_stride);
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. */
423 hb_ft_get_variation_glyph (hb_font_t *font HB_UNUSED,
425 hb_codepoint_t unicode,
426 hb_codepoint_t variation_selector,
427 hb_codepoint_t *glyph,
428 void *user_data HB_UNUSED)
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);
442 hb_ft_get_glyph_h_advances (hb_font_t* font, void* font_data,
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)
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;
456 #ifdef HAVE_FT_GET_TRANSFORM
457 if (ft_font->transform)
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;
467 x_mult = font->x_scale < 0 ? -1 : +1;
470 for (unsigned int i = 0; i < count; i++)
473 hb_codepoint_t glyph = *first_glyph;
476 if (ft_font->advance_cache.get (glyph, &cv))
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! */
484 v = (int) (v * x_mult + (1<<9)) >> 10;
485 ft_font->advance_cache.set (glyph, v);
489 first_glyph = &StructAtOffsetUnaligned<hb_codepoint_t> (first_glyph, glyph_stride);
490 first_advance = &StructAtOffsetUnaligned<hb_position_t> (first_advance, advance_stride);
493 if (font->x_strength && !font->embolden_in_place)
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++)
500 *first_advance += *first_advance ? x_strength : 0;
501 first_advance = &StructAtOffsetUnaligned<hb_position_t> (first_advance, advance_stride);
506 #ifndef HB_NO_VERTICAL
508 hb_ft_get_glyph_v_advance (hb_font_t *font,
510 hb_codepoint_t glyph,
511 void *user_data HB_UNUSED)
513 const hb_ft_font_t *ft_font = (const hb_ft_font_t *) font_data;
514 hb_lock_t lock (ft_font->lock);
517 #ifdef HAVE_FT_GET_TRANSFORM
518 if (ft_font->transform)
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;
528 y_mult = font->y_scale < 0 ? -1 : +1;
531 if (unlikely (FT_Get_Advance (ft_font->ft_face, glyph, ft_font->load_flags | FT_LOAD_VERTICAL_LAYOUT, &v)))
534 v = (int) (y_mult * v);
536 /* Note: FreeType's vertical metrics grows downward while other FreeType coordinates
537 * have a Y growing upward. Hence the extra negation. */
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);
544 #ifndef HB_NO_VERTICAL
546 hb_ft_get_glyph_v_origin (hb_font_t *font,
548 hb_codepoint_t glyph,
551 void *user_data HB_UNUSED)
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)
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;
570 x_mult = font->x_scale < 0 ? -1 : +1;
571 y_mult = font->y_scale < 0 ? -1 : +1;
574 if (unlikely (FT_Load_Glyph (ft_face, glyph, ft_font->load_flags)))
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);
582 *x = (hb_position_t) (x_mult * *x);
583 *y = (hb_position_t) (y_mult * *y);
589 #ifndef HB_NO_OT_SHAPE_FALLBACK
591 hb_ft_get_glyph_h_kerning (hb_font_t *font,
593 hb_codepoint_t left_glyph,
594 hb_codepoint_t right_glyph,
595 void *user_data HB_UNUSED)
597 const hb_ft_font_t *ft_font = (const hb_ft_font_t *) font_data;
598 hb_lock_t lock (ft_font->lock);
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))
610 hb_ft_get_glyph_extents (hb_font_t *font,
612 hb_codepoint_t glyph,
613 hb_glyph_extents_t *extents,
614 void *user_data HB_UNUSED)
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)
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;
634 x_mult = font->x_scale < 0 ? -1 : +1;
635 y_mult = font->y_scale < 0 ? -1 : +1;
638 if (unlikely (FT_Load_Glyph (ft_face, glyph, ft_font->load_flags)))
641 /* Copied from hb_font_t::scale_glyph_extents. */
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;
651 x1 += hb_min (y1 * slant_xy, y2 * slant_xy);
652 x2 += hb_max (y1 * slant_xy, y2 * slant_xy);
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;
660 if (font->x_strength || font->y_strength)
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;
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;
680 hb_ft_get_glyph_contour_point (hb_font_t *font HB_UNUSED,
682 hb_codepoint_t glyph,
683 unsigned int point_index,
686 void *user_data HB_UNUSED)
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;
692 if (unlikely (FT_Load_Glyph (ft_face, glyph, ft_font->load_flags)))
695 if (unlikely (ft_face->glyph->format != FT_GLYPH_FORMAT_OUTLINE))
698 if (unlikely (point_index >= (unsigned int) ft_face->glyph->outline.n_points))
701 *x = ft_face->glyph->outline.points[point_index].x;
702 *y = ft_face->glyph->outline.points[point_index].y;
708 hb_ft_get_glyph_name (hb_font_t *font HB_UNUSED,
710 hb_codepoint_t glyph,
711 char *name, unsigned int size,
712 void *user_data HB_UNUSED)
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;
718 hb_bool_t ret = !FT_Get_Glyph_Name (ft_face, glyph, name, size);
719 if (ret && (size && !*name))
726 hb_ft_get_glyph_from_name (hb_font_t *font HB_UNUSED,
728 const char *name, int len, /* -1 means nul-terminated */
729 hb_codepoint_t *glyph,
730 void *user_data HB_UNUSED)
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;
737 *glyph = FT_Get_Name_Index (ft_face, (FT_String *) name);
739 /* Make a nul-terminated version. */
741 len = hb_min (len, (int) sizeof (buf) - 1);
742 strncpy (buf, name, len);
744 *glyph = FT_Get_Name_Index (ft_face, buf);
749 /* Check whether the given name was actually the name of glyph 0. */
751 if (!FT_Get_Glyph_Name(ft_face, 0, buf, sizeof (buf)) &&
752 len < 0 ? !strcmp (buf, name) : !strncmp (buf, name, len))
760 hb_ft_get_font_h_extents (hb_font_t *font HB_UNUSED,
762 hb_font_extents_t *metrics,
763 void *user_data HB_UNUSED)
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;
769 #ifdef HAVE_FT_GET_TRANSFORM
770 if (ft_font->transform)
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;
780 y_mult = font->y_scale < 0 ? -1 : +1;
783 if (ft_face->units_per_EM != 0)
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);
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);
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);
807 _hb_ft_move_to (const FT_Vector *to,
810 hb_draw_session_t *drawing = (hb_draw_session_t *) arg;
811 drawing->move_to (to->x, to->y);
816 _hb_ft_line_to (const FT_Vector *to,
819 hb_draw_session_t *drawing = (hb_draw_session_t *) arg;
820 drawing->line_to (to->x, to->y);
825 _hb_ft_conic_to (const FT_Vector *control,
829 hb_draw_session_t *drawing = (hb_draw_session_t *) arg;
830 drawing->quadratic_to (control->x, control->y,
836 _hb_ft_cubic_to (const FT_Vector *control1,
837 const FT_Vector *control2,
841 hb_draw_session_t *drawing = (hb_draw_session_t *) arg;
842 drawing->cubic_to (control1->x, control1->y,
843 control2->x, control2->y,
849 hb_ft_draw_glyph (hb_font_t *font,
851 hb_codepoint_t glyph,
852 hb_draw_funcs_t *draw_funcs, void *draw_data,
853 void *user_data HB_UNUSED)
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;
859 if (unlikely (FT_Load_Glyph (ft_face, glyph,
860 FT_LOAD_NO_BITMAP | ft_font->load_flags)))
863 if (ft_face->glyph->format != FT_GLYPH_FORMAT_OUTLINE)
866 const FT_Outline_Funcs outline_funcs = {
875 hb_draw_session_t draw_session (draw_funcs, draw_data, font->slant_xy);
878 if (font->x_strength || font->y_strength)
880 FT_Outline_EmboldenXY (&ft_face->glyph->outline, font->x_strength, font->y_strength);
884 if (font->embolden_in_place)
886 /* Undo the FreeType shift. */
887 x_shift = -font->x_strength / 2;
889 if (font->y_scale < 0) y_shift = -font->y_strength;
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;
897 if (x_shift || y_shift)
899 auto &outline = ft_face->glyph->outline;
900 for (auto &point : hb_iter (outline.points, outline.contours[outline.n_contours - 1] + 1))
909 FT_Outline_Decompose (&ft_face->glyph->outline,
916 #if (FREETYPE_MAJOR*10000 + FREETYPE_MINOR*100 + FREETYPE_PATCH) >= 21300
918 #include "hb-ft-colr.hh"
921 hb_ft_paint_glyph (hb_font_t *font,
924 hb_paint_funcs_t *paint_funcs, void *paint_data,
925 unsigned int palette_index,
926 hb_color_t foreground,
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;
933 /* We release the lock before calling into glyph callbacks, such that
934 * eg. draw API can call back into the face.*/
936 if (unlikely (FT_Load_Glyph (ft_face, gid,
937 ft_font->load_flags | FT_LOAD_COLOR)))
940 if (ft_face->glyph->format == FT_GLYPH_FORMAT_OUTLINE)
942 if (hb_ft_paint_glyph_colr (font, font_data, gid,
943 paint_funcs, paint_data,
944 palette_index, foreground,
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);
958 auto *glyph = ft_face->glyph;
959 if (glyph->format == FT_GLYPH_FORMAT_BITMAP)
961 auto &bitmap = glyph->bitmap;
962 if (bitmap.pixel_mode == FT_PIXEL_MODE_BGRA)
964 if (bitmap.pitch != (signed) bitmap.width * 4)
967 ft_font->lock.unlock ();
969 hb_blob_t *blob = hb_blob_create ((const char *) bitmap.buffer,
970 bitmap.pitch * bitmap.rows,
971 HB_MEMORY_MODE_DUPLICATE,
974 hb_glyph_extents_t extents;
975 if (!hb_font_get_glyph_extents (font, gid, &extents))
978 if (!paint_funcs->image (paint_data,
982 HB_PAINT_IMAGE_FORMAT_BGRA,
986 /* TODO Try a forced outline load and paint? */
990 hb_blob_destroy (blob);
991 ft_font->lock.lock ();
1001 static inline void free_static_ft_funcs ();
1003 static struct hb_ft_font_funcs_lazy_loader_t : hb_font_funcs_lazy_loader_t<hb_ft_font_funcs_lazy_loader_t>
1005 static hb_font_funcs_t *create ()
1007 hb_font_funcs_t *funcs = hb_font_funcs_create ();
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);
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);
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);
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);
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);
1033 hb_font_funcs_set_draw_glyph_func (funcs, hb_ft_draw_glyph, nullptr, nullptr);
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);
1042 hb_font_funcs_make_immutable (funcs);
1044 hb_atexit (free_static_ft_funcs);
1051 void free_static_ft_funcs ()
1053 static_ft_funcs.free_instance ();
1056 static hb_font_funcs_t *
1057 _hb_ft_get_font_funcs ()
1059 return static_ft_funcs.get_unconst ();
1063 _hb_ft_font_set_funcs (hb_font_t *font, FT_Face ft_face, bool unref)
1065 bool symbol = ft_face->charmap && ft_face->charmap->encoding == FT_ENCODING_MS_SYMBOL;
1067 hb_ft_font_t *ft_font = _hb_ft_font_create (ft_face, symbol, unref);
1068 if (unlikely (!ft_font)) return;
1070 hb_font_set_funcs (font,
1071 _hb_ft_get_font_funcs (),
1073 _hb_ft_font_destroy);
1078 _hb_ft_reference_table (hb_face_t *face HB_UNUSED, hb_tag_t tag, void *user_data)
1080 FT_Face ft_face = (FT_Face) user_data;
1082 FT_ULong length = 0;
1085 /* Note: FreeType like HarfBuzz uses the NONE tag for fetching the entire blob */
1087 error = FT_Load_Sfnt_Table (ft_face, tag, 0, nullptr, &length);
1091 buffer = (FT_Byte *) hb_malloc (length);
1095 error = FT_Load_Sfnt_Table (ft_face, tag, 0, buffer, &length);
1102 return hb_blob_create ((const char *) buffer, length,
1103 HB_MEMORY_MODE_WRITABLE,
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
1112 * Creates an #hb_face_t face object from the specified FT_Face.
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.
1118 * This variant of the function does not provide any life-cycle management.
1120 * Most client programs should use hb_ft_face_create_referenced()
1121 * (or, perhaps, hb_ft_face_create_cached()) instead.
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.
1127 * Return value: (transfer full): the new #hb_face_t face object
1132 hb_ft_face_create (FT_Face ft_face,
1133 hb_destroy_func_t destroy)
1137 if (!ft_face->stream->read) {
1140 blob = hb_blob_create ((const char *) ft_face->stream->base,
1141 (unsigned int) ft_face->stream->size,
1142 HB_MEMORY_MODE_READONLY,
1144 face = hb_face_create (blob, ft_face->face_index);
1145 hb_blob_destroy (blob);
1147 face = hb_face_create_for_tables (_hb_ft_reference_table, ft_face, destroy);
1150 hb_face_set_index (face, ft_face->face_index);
1151 hb_face_set_upem (face, ft_face->units_per_EM);
1157 * hb_ft_face_create_referenced:
1158 * @ft_face: FT_Face to work upon
1160 * Creates an #hb_face_t face object from the specified FT_Face.
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.
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.
1172 * Use this version unless you know you have good reasons not to.
1174 * Return value: (transfer full): the new #hb_face_t face object
1179 hb_ft_face_create_referenced (FT_Face ft_face)
1181 FT_Reference_Face (ft_face);
1182 return hb_ft_face_create (ft_face, _hb_ft_face_destroy);
1186 hb_ft_face_finalize (void *arg)
1188 FT_Face ft_face = (FT_Face) arg;
1189 hb_face_destroy ((hb_face_t *) ft_face->generic.data);
1193 * hb_ft_face_create_cached:
1194 * @ft_face: FT_Face to work upon
1196 * Creates an #hb_face_t face object from the specified FT_Face.
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.
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.
1208 * However, client programs are still responsible for destroying
1209 * @ft_face after the last #hb_face_t face object has been destroyed.
1211 * Return value: (transfer full): the new #hb_face_t face object
1216 hb_ft_face_create_cached (FT_Face ft_face)
1218 if (unlikely (!ft_face->generic.data || ft_face->generic.finalizer != (FT_Generic_Finalizer) hb_ft_face_finalize))
1220 if (ft_face->generic.finalizer)
1221 ft_face->generic.finalizer (ft_face);
1223 ft_face->generic.data = hb_ft_face_create (ft_face, nullptr);
1224 ft_face->generic.finalizer = hb_ft_face_finalize;
1227 return hb_face_reference ((hb_face_t *) ft_face->generic.data);
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
1235 * Creates an #hb_font_t font object from the specified FT_Face.
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>
1241 * This variant of the function does not provide any life-cycle management.
1243 * Most client programs should use hb_ft_font_create_referenced()
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.
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.
1256 * Return value: (transfer full): the new #hb_font_t font object
1261 hb_ft_font_create (FT_Face ft_face,
1262 hb_destroy_func_t destroy)
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);
1276 * hb_ft_font_changed:
1277 * @font: #hb_font_t to work upon
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.
1286 hb_ft_font_changed (hb_font_t *font)
1288 if (font->destroy != (hb_destroy_func_t) _hb_ft_font_destroy)
1291 hb_ft_font_t *ft_font = (hb_ft_font_t *) font->user_data;
1293 FT_Face ft_face = ft_font->ft_face;
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);
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))
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)
1312 if (!FT_Get_Var_Blend_Coordinates (ft_face, mm_var->num_axis, ft_coords))
1314 bool nonzero = false;
1316 for (unsigned int i = 0; i < mm_var->num_axis; ++i)
1318 coords[i] = ft_coords[i] >>= 2;
1319 nonzero = nonzero || coords[i];
1323 hb_font_set_var_coords_normalized (font, coords, mm_var->num_axis);
1325 hb_font_set_var_coords_normalized (font, nullptr, 0);
1329 hb_free (ft_coords);
1330 #ifdef HAVE_FT_DONE_MM_VAR
1331 FT_Done_MM_Var (ft_face->glyph->library, mm_var);
1338 ft_font->advance_cache.clear ();
1339 ft_font->cached_serial = font->serial;
1343 * hb_ft_hb_font_changed:
1344 * @font: #hb_font_t to work upon
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.
1352 * Return value: true if changed, false otherwise
1357 hb_ft_hb_font_changed (hb_font_t *font)
1359 if (font->destroy != (hb_destroy_func_t) _hb_ft_font_destroy)
1362 hb_ft_font_t *ft_font = (hb_ft_font_t *) font->user_data;
1364 return _hb_ft_hb_font_check_changed (font, ft_font);
1368 * hb_ft_font_create_referenced:
1369 * @ft_face: FT_Face to work upon
1371 * Creates an #hb_font_t font object from the specified FT_Face.
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>
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.
1382 * Use this version unless you know you have good reasons not to.
1384 * Return value: (transfer full): the new #hb_font_t font object
1389 hb_ft_font_create_referenced (FT_Face ft_face)
1391 FT_Reference_Face (ft_face);
1392 return hb_ft_font_create (ft_face, _hb_ft_face_destroy);
1395 static inline void free_static_ft_library ();
1397 static struct hb_ft_library_lazy_loader_t : hb_lazy_loader_t<hb_remove_pointer<FT_Library>,
1398 hb_ft_library_lazy_loader_t>
1400 static FT_Library create ()
1403 if (FT_Init_FreeType (&l))
1406 hb_atexit (free_static_ft_library);
1410 static void destroy (FT_Library l)
1412 FT_Done_FreeType (l);
1414 static FT_Library get_null ()
1418 } static_ft_library;
1421 void free_static_ft_library ()
1423 static_ft_library.free_instance ();
1429 return static_ft_library.get_unconst ();
1433 _release_blob (void *arg)
1435 FT_Face ft_face = (FT_Face) arg;
1436 hb_blob_destroy ((hb_blob_t *) ft_face->generic.data);
1440 * hb_ft_font_set_funcs:
1441 * @font: #hb_font_t to work upon
1443 * Configures the font-functions structure of the specified
1444 * #hb_font_t font object to use FreeType font functions.
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.
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.
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.
1460 * <note>Note: Internally, this function creates an FT_Face.
1466 hb_ft_font_set_funcs (hb_font_t *font)
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");
1474 FT_Face ft_face = nullptr;
1475 FT_Error err = FT_New_Memory_Face (get_ft_library (),
1476 (const FT_Byte *) blob_data,
1478 hb_face_get_index (font->face),
1481 if (unlikely (err)) {
1482 hb_blob_destroy (blob);
1483 DEBUG_MSG (FT, font, "Font face FT_New_Memory_Face() failed");
1487 if (FT_Select_Charmap (ft_face, FT_ENCODING_MS_SYMBOL))
1488 FT_Select_Charmap (ft_face, FT_ENCODING_UNICODE);
1491 ft_face->generic.data = blob;
1492 ft_face->generic.finalizer = _release_blob;
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);
1497 _hb_ft_hb_font_changed (font, ft_face);