QtBase  v6.3.1
hb-ft.cc
Go to the documentation of this file.
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-draw.hh"
37 #include "hb-font.hh"
38 #include "hb-machinery.hh"
39 #include "hb-cache.hh"
40 
41 #include FT_ADVANCES_H
42 #include FT_MULTIPLE_MASTERS_H
43 #include FT_OUTLINE_H
44 #include FT_TRUETYPE_TABLES_H
45 
46 
63 /* TODO:
64  *
65  * In general, this file does a fine job of what it's supposed to do.
66  * There are, however, things that need more work:
67  *
68  * - FreeType works in 26.6 mode. Clients can decide to use that mode, and everything
69  * would work fine. However, we also abuse this API for performing in font-space,
70  * but don't pass the correct flags to FreeType. We just abuse the no-hinting mode
71  * for that, such that no rounding etc happens. As such, we don't set ppem, and
72  * pass NO_HINTING as load_flags. Would be much better to use NO_SCALE, and scale
73  * ourselves.
74  *
75  * - We don't handle / allow for emboldening / obliqueing.
76  *
77  * - In the future, we should add constructors to create fonts in font space?
78  */
79 
80 
81 struct hb_ft_font_t
82 {
83  mutable hb_mutex_t lock;
84  FT_Face ft_face;
85  int load_flags;
86  bool symbol; /* Whether selected cmap is symbol cmap. */
87  bool unref; /* Whether to destroy ft_face when done. */
88 
89  mutable int cached_x_scale;
90  mutable hb_advance_cache_t advance_cache;
91 };
92 
93 static hb_ft_font_t *
94 _hb_ft_font_create (FT_Face ft_face, bool symbol, bool unref)
95 {
96  hb_ft_font_t *ft_font = (hb_ft_font_t *) hb_calloc (1, sizeof (hb_ft_font_t));
97  if (unlikely (!ft_font)) return nullptr;
98 
99  ft_font->lock.init ();
100  ft_font->ft_face = ft_face;
101  ft_font->symbol = symbol;
102  ft_font->unref = unref;
103 
104  ft_font->load_flags = FT_LOAD_DEFAULT | FT_LOAD_NO_HINTING;
105 
106  ft_font->cached_x_scale = 0;
107  ft_font->advance_cache.init ();
108 
109  return ft_font;
110 }
111 
112 static void
113 _hb_ft_face_destroy (void *data)
114 {
116 }
117 
118 static void
119 _hb_ft_font_destroy (void *data)
120 {
121  hb_ft_font_t *ft_font = (hb_ft_font_t *) data;
122 
123  ft_font->advance_cache.fini ();
124 
125  if (ft_font->unref)
126  _hb_ft_face_destroy (ft_font->ft_face);
127 
128  ft_font->lock.fini ();
129 
130  hb_free (ft_font);
131 }
132 
145 void
146 hb_ft_font_set_load_flags (hb_font_t *font, int load_flags)
147 {
148  if (hb_object_is_immutable (font))
149  return;
150 
151  if (unlikely (font->destroy != (hb_destroy_func_t) _hb_ft_font_destroy))
152  return;
153 
154  hb_ft_font_t *ft_font = (hb_ft_font_t *) font->user_data;
155 
156  ft_font->load_flags = load_flags;
157 }
158 
172 int
174 {
175  if (unlikely (font->destroy != (hb_destroy_func_t) _hb_ft_font_destroy))
176  return 0;
177 
178  const hb_ft_font_t *ft_font = (const hb_ft_font_t *) font->user_data;
179 
180  return ft_font->load_flags;
181 }
182 
194 FT_Face
196 {
197  if (unlikely (font->destroy != (hb_destroy_func_t) _hb_ft_font_destroy))
198  return nullptr;
199 
200  const hb_ft_font_t *ft_font = (const hb_ft_font_t *) font->user_data;
201 
202  return ft_font->ft_face;
203 }
204 
215 FT_Face
217 {
218  if (unlikely (font->destroy != (hb_destroy_func_t) _hb_ft_font_destroy))
219  return nullptr;
220 
221  const hb_ft_font_t *ft_font = (const hb_ft_font_t *) font->user_data;
222 
223  ft_font->lock.lock ();
224 
225  return ft_font->ft_face;
226 }
227 
236 void
238 {
239  if (unlikely (font->destroy != (hb_destroy_func_t) _hb_ft_font_destroy))
240  return;
241 
242  const hb_ft_font_t *ft_font = (const hb_ft_font_t *) font->user_data;
243 
244  ft_font->lock.unlock ();
245 }
246 
247 
248 static hb_bool_t
249 hb_ft_get_nominal_glyph (hb_font_t *font HB_UNUSED,
250  void *font_data,
251  hb_codepoint_t unicode,
252  hb_codepoint_t *glyph,
253  void *user_data HB_UNUSED)
254 {
255  const hb_ft_font_t *ft_font = (const hb_ft_font_t *) font_data;
256  hb_lock_t lock (ft_font->lock);
257  unsigned int g = FT_Get_Char_Index (ft_font->ft_face, unicode);
258 
259  if (unlikely (!g))
260  {
261  if (unlikely (ft_font->symbol) && unicode <= 0x00FFu)
262  {
263  /* For symbol-encoded OpenType fonts, we duplicate the
264  * U+F000..F0FF range at U+0000..U+00FF. That's what
265  * Windows seems to do, and that's hinted about at:
266  * https://docs.microsoft.com/en-us/typography/opentype/spec/recom
267  * under "Non-Standard (Symbol) Fonts". */
268  g = FT_Get_Char_Index (ft_font->ft_face, 0xF000u + unicode);
269  if (!g)
270  return false;
271  }
272  else
273  return false;
274  }
275 
276  *glyph = g;
277  return true;
278 }
279 
280 static unsigned int
281 hb_ft_get_nominal_glyphs (hb_font_t *font HB_UNUSED,
282  void *font_data,
283  unsigned int count,
284  const hb_codepoint_t *first_unicode,
285  unsigned int unicode_stride,
286  hb_codepoint_t *first_glyph,
287  unsigned int glyph_stride,
288  void *user_data HB_UNUSED)
289 {
290  const hb_ft_font_t *ft_font = (const hb_ft_font_t *) font_data;
291  hb_lock_t lock (ft_font->lock);
292  unsigned int done;
293  for (done = 0;
294  done < count && (*first_glyph = FT_Get_Char_Index (ft_font->ft_face, *first_unicode));
295  done++)
296  {
297  first_unicode = &StructAtOffsetUnaligned<hb_codepoint_t> (first_unicode, unicode_stride);
298  first_glyph = &StructAtOffsetUnaligned<hb_codepoint_t> (first_glyph, glyph_stride);
299  }
300  /* We don't need to do ft_font->symbol dance here, since HB calls the singular
301  * nominal_glyph() for what we don't handle here. */
302  return done;
303 }
304 
305 
306 static hb_bool_t
307 hb_ft_get_variation_glyph (hb_font_t *font HB_UNUSED,
308  void *font_data,
309  hb_codepoint_t unicode,
310  hb_codepoint_t variation_selector,
311  hb_codepoint_t *glyph,
312  void *user_data HB_UNUSED)
313 {
314  const hb_ft_font_t *ft_font = (const hb_ft_font_t *) font_data;
315  hb_lock_t lock (ft_font->lock);
316  unsigned int g = FT_Face_GetCharVariantIndex (ft_font->ft_face, unicode, variation_selector);
317 
318  if (unlikely (!g))
319  return false;
320 
321  *glyph = g;
322  return true;
323 }
324 
325 static void
326 hb_ft_get_glyph_h_advances (hb_font_t* font, void* font_data,
327  unsigned count,
328  const hb_codepoint_t *first_glyph,
329  unsigned glyph_stride,
330  hb_position_t *first_advance,
331  unsigned advance_stride,
332  void *user_data HB_UNUSED)
333 {
334  const hb_ft_font_t *ft_font = (const hb_ft_font_t *) font_data;
335  hb_lock_t lock (ft_font->lock);
336  FT_Face ft_face = ft_font->ft_face;
337  int load_flags = ft_font->load_flags;
338  int mult = font->x_scale < 0 ? -1 : +1;
339 
340  if (font->x_scale != ft_font->cached_x_scale)
341  {
342  ft_font->advance_cache.clear ();
343  ft_font->cached_x_scale = font->x_scale;
344  }
345 
346  for (unsigned int i = 0; i < count; i++)
347  {
348  FT_Fixed v = 0;
349  hb_codepoint_t glyph = *first_glyph;
350 
351  unsigned int cv;
352  if (ft_font->advance_cache.get (glyph, &cv))
353  v = cv;
354  else
355  {
356  FT_Get_Advance (ft_face, glyph, load_flags, &v);
357  ft_font->advance_cache.set (glyph, v);
358  }
359 
360  *first_advance = (v * mult + (1<<9)) >> 10;
361  first_glyph = &StructAtOffsetUnaligned<hb_codepoint_t> (first_glyph, glyph_stride);
362  first_advance = &StructAtOffsetUnaligned<hb_position_t> (first_advance, advance_stride);
363  }
364 }
365 
366 #ifndef HB_NO_VERTICAL
367 static hb_position_t
368 hb_ft_get_glyph_v_advance (hb_font_t *font,
369  void *font_data,
370  hb_codepoint_t glyph,
371  void *user_data HB_UNUSED)
372 {
373  const hb_ft_font_t *ft_font = (const hb_ft_font_t *) font_data;
374  hb_lock_t lock (ft_font->lock);
375  FT_Fixed v;
376 
377  if (unlikely (FT_Get_Advance (ft_font->ft_face, glyph, ft_font->load_flags | FT_LOAD_VERTICAL_LAYOUT, &v)))
378  return 0;
379 
380  if (font->y_scale < 0)
381  v = -v;
382 
383  /* Note: FreeType's vertical metrics grows downward while other FreeType coordinates
384  * have a Y growing upward. Hence the extra negation. */
385 
386  return (-v + (1<<9)) >> 10;
387 }
388 #endif
389 
390 #ifndef HB_NO_VERTICAL
391 static hb_bool_t
392 hb_ft_get_glyph_v_origin (hb_font_t *font,
393  void *font_data,
394  hb_codepoint_t glyph,
395  hb_position_t *x,
396  hb_position_t *y,
397  void *user_data HB_UNUSED)
398 {
399  const hb_ft_font_t *ft_font = (const hb_ft_font_t *) font_data;
400  hb_lock_t lock (ft_font->lock);
401  FT_Face ft_face = ft_font->ft_face;
402 
403  if (unlikely (FT_Load_Glyph (ft_face, glyph, ft_font->load_flags)))
404  return false;
405 
406  /* Note: FreeType's vertical metrics grows downward while other FreeType coordinates
407  * have a Y growing upward. Hence the extra negation. */
408  *x = ft_face->glyph->metrics.horiBearingX - ft_face->glyph->metrics.vertBearingX;
409  *y = ft_face->glyph->metrics.horiBearingY - (-ft_face->glyph->metrics.vertBearingY);
410 
411  if (font->x_scale < 0)
412  *x = -*x;
413  if (font->y_scale < 0)
414  *y = -*y;
415 
416  return true;
417 }
418 #endif
419 
420 #ifndef HB_NO_OT_SHAPE_FALLBACK
421 static hb_position_t
422 hb_ft_get_glyph_h_kerning (hb_font_t *font,
423  void *font_data,
426  void *user_data HB_UNUSED)
427 {
428  const hb_ft_font_t *ft_font = (const hb_ft_font_t *) font_data;
429  FT_Vector kerningv;
430 
432  if (FT_Get_Kerning (ft_font->ft_face, left_glyph, right_glyph, mode, &kerningv))
433  return 0;
434 
435  return kerningv.x;
436 }
437 #endif
438 
439 static hb_bool_t
440 hb_ft_get_glyph_extents (hb_font_t *font,
441  void *font_data,
442  hb_codepoint_t glyph,
443  hb_glyph_extents_t *extents,
444  void *user_data HB_UNUSED)
445 {
446  const hb_ft_font_t *ft_font = (const hb_ft_font_t *) font_data;
447  hb_lock_t lock (ft_font->lock);
448  FT_Face ft_face = ft_font->ft_face;
449 
450  if (unlikely (FT_Load_Glyph (ft_face, glyph, ft_font->load_flags)))
451  return false;
452 
453  extents->x_bearing = ft_face->glyph->metrics.horiBearingX;
454  extents->y_bearing = ft_face->glyph->metrics.horiBearingY;
455  extents->width = ft_face->glyph->metrics.width;
456  extents->height = -ft_face->glyph->metrics.height;
457  if (font->x_scale < 0)
458  {
459  extents->x_bearing = -extents->x_bearing;
460  extents->width = -extents->width;
461  }
462  if (font->y_scale < 0)
463  {
464  extents->y_bearing = -extents->y_bearing;
465  extents->height = -extents->height;
466  }
467  return true;
468 }
469 
470 static hb_bool_t
471 hb_ft_get_glyph_contour_point (hb_font_t *font HB_UNUSED,
472  void *font_data,
473  hb_codepoint_t glyph,
474  unsigned int point_index,
475  hb_position_t *x,
476  hb_position_t *y,
477  void *user_data HB_UNUSED)
478 {
479  const hb_ft_font_t *ft_font = (const hb_ft_font_t *) font_data;
480  hb_lock_t lock (ft_font->lock);
481  FT_Face ft_face = ft_font->ft_face;
482 
483  if (unlikely (FT_Load_Glyph (ft_face, glyph, ft_font->load_flags)))
484  return false;
485 
486  if (unlikely (ft_face->glyph->format != FT_GLYPH_FORMAT_OUTLINE))
487  return false;
488 
489  if (unlikely (point_index >= (unsigned int) ft_face->glyph->outline.n_points))
490  return false;
491 
492  *x = ft_face->glyph->outline.points[point_index].x;
493  *y = ft_face->glyph->outline.points[point_index].y;
494 
495  return true;
496 }
497 
498 static hb_bool_t
499 hb_ft_get_glyph_name (hb_font_t *font HB_UNUSED,
500  void *font_data,
501  hb_codepoint_t glyph,
502  char *name, unsigned int size,
503  void *user_data HB_UNUSED)
504 {
505  const hb_ft_font_t *ft_font = (const hb_ft_font_t *) font_data;
506  hb_lock_t lock (ft_font->lock);
507  FT_Face ft_face = ft_font->ft_face;
508 
509  hb_bool_t ret = !FT_Get_Glyph_Name (ft_face, glyph, name, size);
510  if (ret && (size && !*name))
511  ret = false;
512 
513  return ret;
514 }
515 
516 static hb_bool_t
517 hb_ft_get_glyph_from_name (hb_font_t *font HB_UNUSED,
518  void *font_data,
519  const char *name, int len, /* -1 means nul-terminated */
520  hb_codepoint_t *glyph,
521  void *user_data HB_UNUSED)
522 {
523  const hb_ft_font_t *ft_font = (const hb_ft_font_t *) font_data;
524  hb_lock_t lock (ft_font->lock);
525  FT_Face ft_face = ft_font->ft_face;
526 
527  if (len < 0)
528  *glyph = FT_Get_Name_Index (ft_face, (FT_String *) name);
529  else {
530  /* Make a nul-terminated version. */
531  char buf[128];
532  len = hb_min (len, (int) sizeof (buf) - 1);
533  strncpy (buf, name, len);
534  buf[len] = '\0';
535  *glyph = FT_Get_Name_Index (ft_face, buf);
536  }
537 
538  if (*glyph == 0)
539  {
540  /* Check whether the given name was actually the name of glyph 0. */
541  char buf[128];
542  if (!FT_Get_Glyph_Name(ft_face, 0, buf, sizeof (buf)) &&
543  len < 0 ? !strcmp (buf, name) : !strncmp (buf, name, len))
544  return true;
545  }
546 
547  return *glyph != 0;
548 }
549 
550 static hb_bool_t
551 hb_ft_get_font_h_extents (hb_font_t *font HB_UNUSED,
552  void *font_data,
554  void *user_data HB_UNUSED)
555 {
556  const hb_ft_font_t *ft_font = (const hb_ft_font_t *) font_data;
557  hb_lock_t lock (ft_font->lock);
558  FT_Face ft_face = ft_font->ft_face;
559  metrics->ascender = FT_MulFix(ft_face->ascender, ft_face->size->metrics.y_scale);
560  metrics->descender = FT_MulFix(ft_face->descender, ft_face->size->metrics.y_scale);
561  metrics->line_gap = FT_MulFix( ft_face->height, ft_face->size->metrics.y_scale ) - (metrics->ascender - metrics->descender);
562  if (font->y_scale < 0)
563  {
564  metrics->ascender = -metrics->ascender;
565  metrics->descender = -metrics->descender;
566  metrics->line_gap = -metrics->line_gap;
567  }
568  return true;
569 }
570 
571 #ifndef HB_NO_DRAW
572 
573 static int
574 _hb_ft_move_to (const FT_Vector *to,
575  hb_draw_session_t *drawing)
576 {
577  drawing->move_to (to->x, to->y);
578  return FT_Err_Ok;
579 }
580 
581 static int
582 _hb_ft_line_to (const FT_Vector *to,
583  hb_draw_session_t *drawing)
584 {
585  drawing->line_to (to->x, to->y);
586  return FT_Err_Ok;
587 }
588 
589 static int
590 _hb_ft_conic_to (const FT_Vector *control,
591  const FT_Vector *to,
592  hb_draw_session_t *drawing)
593 {
594  drawing->quadratic_to (control->x, control->y,
595  to->x, to->y);
596  return FT_Err_Ok;
597 }
598 
599 static int
600 _hb_ft_cubic_to (const FT_Vector *control1,
601  const FT_Vector *control2,
602  const FT_Vector *to,
603  hb_draw_session_t *drawing)
604 {
605  drawing->cubic_to (control1->x, control1->y,
606  control2->x, control2->y,
607  to->x, to->y);
608  return FT_Err_Ok;
609 }
610 
611 static void
612 hb_ft_get_glyph_shape (hb_font_t *font HB_UNUSED,
613  void *font_data,
614  hb_codepoint_t glyph,
615  hb_draw_funcs_t *draw_funcs, void *draw_data,
616  void *user_data HB_UNUSED)
617 {
618  const hb_ft_font_t *ft_font = (const hb_ft_font_t *) font_data;
619  hb_lock_t lock (ft_font->lock);
620  FT_Face ft_face = ft_font->ft_face;
621 
622  if (unlikely (FT_Load_Glyph (ft_face, glyph,
623  FT_LOAD_NO_BITMAP | ft_font->load_flags)))
624  return;
625 
626  if (ft_face->glyph->format != FT_GLYPH_FORMAT_OUTLINE)
627  return;
628 
629  const FT_Outline_Funcs outline_funcs = {
630  (FT_Outline_MoveToFunc) _hb_ft_move_to,
631  (FT_Outline_LineToFunc) _hb_ft_line_to,
632  (FT_Outline_ConicToFunc) _hb_ft_conic_to,
633  (FT_Outline_CubicToFunc) _hb_ft_cubic_to,
634  0, /* shift */
635  0, /* delta */
636  };
637 
638  hb_draw_session_t draw_session (draw_funcs, draw_data, font->slant_xy);
639 
640  FT_Outline_Decompose (&ft_face->glyph->outline,
641  &outline_funcs,
642  &draw_session);
643 }
644 #endif
645 
646 
647 static inline void free_static_ft_funcs ();
648 
649 static struct hb_ft_font_funcs_lazy_loader_t : hb_font_funcs_lazy_loader_t<hb_ft_font_funcs_lazy_loader_t>
650 {
651  static hb_font_funcs_t *create ()
652  {
654 
655  hb_font_funcs_set_nominal_glyph_func (funcs, hb_ft_get_nominal_glyph, nullptr, nullptr);
656  hb_font_funcs_set_nominal_glyphs_func (funcs, hb_ft_get_nominal_glyphs, nullptr, nullptr);
657  hb_font_funcs_set_variation_glyph_func (funcs, hb_ft_get_variation_glyph, nullptr, nullptr);
658 
659  hb_font_funcs_set_font_h_extents_func (funcs, hb_ft_get_font_h_extents, nullptr, nullptr);
660  hb_font_funcs_set_glyph_h_advances_func (funcs, hb_ft_get_glyph_h_advances, nullptr, nullptr);
661  //hb_font_funcs_set_glyph_h_origin_func (funcs, hb_ft_get_glyph_h_origin, nullptr, nullptr);
662 
663 #ifndef HB_NO_VERTICAL
664  //hb_font_funcs_set_font_v_extents_func (funcs, hb_ft_get_font_v_extents, nullptr, nullptr);
665  hb_font_funcs_set_glyph_v_advance_func (funcs, hb_ft_get_glyph_v_advance, nullptr, nullptr);
666  hb_font_funcs_set_glyph_v_origin_func (funcs, hb_ft_get_glyph_v_origin, nullptr, nullptr);
667 #endif
668 
669 #ifndef HB_NO_OT_SHAPE_FALLBACK
670  hb_font_funcs_set_glyph_h_kerning_func (funcs, hb_ft_get_glyph_h_kerning, nullptr, nullptr);
671 #endif
672  //hb_font_funcs_set_glyph_v_kerning_func (funcs, hb_ft_get_glyph_v_kerning, nullptr, nullptr);
673  hb_font_funcs_set_glyph_extents_func (funcs, hb_ft_get_glyph_extents, nullptr, nullptr);
674  hb_font_funcs_set_glyph_contour_point_func (funcs, hb_ft_get_glyph_contour_point, nullptr, nullptr);
675  hb_font_funcs_set_glyph_name_func (funcs, hb_ft_get_glyph_name, nullptr, nullptr);
676  hb_font_funcs_set_glyph_from_name_func (funcs, hb_ft_get_glyph_from_name, nullptr, nullptr);
677 
678 #ifndef HB_NO_DRAW
679  hb_font_funcs_set_glyph_shape_func (funcs, hb_ft_get_glyph_shape, nullptr, nullptr);
680 #endif
681 
683 
684  hb_atexit (free_static_ft_funcs);
685 
686  return funcs;
687  }
688 } static_ft_funcs;
689 
690 static inline
691 void free_static_ft_funcs ()
692 {
693  static_ft_funcs.free_instance ();
694 }
695 
696 static hb_font_funcs_t *
697 _hb_ft_get_font_funcs ()
698 {
699  return static_ft_funcs.get_unconst ();
700 }
701 
702 static void
703 _hb_ft_font_set_funcs (hb_font_t *font, FT_Face ft_face, bool unref)
704 {
705  bool symbol = ft_face->charmap && ft_face->charmap->encoding == FT_ENCODING_MS_SYMBOL;
706 
707  hb_ft_font_t *ft_font = _hb_ft_font_create (ft_face, symbol, unref);
708  if (unlikely (!ft_font)) return;
709 
711  _hb_ft_get_font_funcs (),
712  ft_font,
713  _hb_ft_font_destroy);
714 }
715 
716 
717 static hb_blob_t *
718 _hb_ft_reference_table (hb_face_t *face HB_UNUSED, hb_tag_t tag, void *user_data)
719 {
720  FT_Face ft_face = (FT_Face) user_data;
721  FT_Byte *buffer;
722  FT_ULong length = 0;
723  FT_Error error;
724 
725  /* Note: FreeType like HarfBuzz uses the NONE tag for fetching the entire blob */
726 
727  error = FT_Load_Sfnt_Table (ft_face, tag, 0, nullptr, &length);
728  if (error)
729  return nullptr;
730 
731  buffer = (FT_Byte *) hb_malloc (length);
732  if (!buffer)
733  return nullptr;
734 
735  error = FT_Load_Sfnt_Table (ft_face, tag, 0, buffer, &length);
736  if (error)
737  {
738  hb_free (buffer);
739  return nullptr;
740  }
741 
742  return hb_blob_create ((const char *) buffer, length,
744  buffer, hb_free);
745 }
746 
767 hb_face_t *
768 hb_ft_face_create (FT_Face ft_face,
770 {
771  hb_face_t *face;
772 
773  if (!ft_face->stream->read) {
774  hb_blob_t *blob;
775 
776  blob = hb_blob_create ((const char *) ft_face->stream->base,
777  (unsigned int) ft_face->stream->size,
779  ft_face, destroy);
780  face = hb_face_create (blob, ft_face->face_index);
781  hb_blob_destroy (blob);
782  } else {
783  face = hb_face_create_for_tables (_hb_ft_reference_table, ft_face, destroy);
784  }
785 
786  hb_face_set_index (face, ft_face->face_index);
787  hb_face_set_upem (face, ft_face->units_per_EM);
788 
789  return face;
790 }
791 
810 hb_face_t *
812 {
813  FT_Reference_Face (ft_face);
814  return hb_ft_face_create (ft_face, _hb_ft_face_destroy);
815 }
816 
817 static void
818 hb_ft_face_finalize (FT_Face ft_face)
819 {
820  hb_face_destroy ((hb_face_t *) ft_face->generic.data);
821 }
822 
842 hb_face_t *
844 {
845  if (unlikely (!ft_face->generic.data || ft_face->generic.finalizer != (FT_Generic_Finalizer) hb_ft_face_finalize))
846  {
847  if (ft_face->generic.finalizer)
848  ft_face->generic.finalizer (ft_face);
849 
850  ft_face->generic.data = hb_ft_face_create (ft_face, nullptr);
851  ft_face->generic.finalizer = (FT_Generic_Finalizer) hb_ft_face_finalize;
852  }
853 
854  return hb_face_reference ((hb_face_t *) ft_face->generic.data);
855 }
856 
887 hb_font_t *
888 hb_ft_font_create (FT_Face ft_face,
890 {
891  hb_font_t *font;
892  hb_face_t *face;
893 
894  face = hb_ft_face_create (ft_face, destroy);
897  _hb_ft_font_set_funcs (font, ft_face, false);
899  return font;
900 }
901 
912 void
914 {
915  if (font->destroy != (hb_destroy_func_t) _hb_ft_font_destroy)
916  return;
917 
918  hb_ft_font_t *ft_font = (hb_ft_font_t *) font->user_data;
919 
920  FT_Face ft_face = ft_font->ft_face;
921 
923  (int) (((uint64_t) ft_face->size->metrics.x_scale * (uint64_t) ft_face->units_per_EM + (1u<<15)) >> 16),
924  (int) (((uint64_t) ft_face->size->metrics.y_scale * (uint64_t) ft_face->units_per_EM + (1u<<15)) >> 16));
925 #if 0 /* hb-ft works in no-hinting model */
927  ft_face->size->metrics.x_ppem,
928  ft_face->size->metrics.y_ppem);
929 #endif
930 
931 #if defined(HAVE_FT_GET_VAR_BLEND_COORDINATES) && !defined(HB_NO_VAR)
932  FT_MM_Var *mm_var = nullptr;
933  if (!FT_Get_MM_Var (ft_face, &mm_var))
934  {
935  FT_Fixed *ft_coords = (FT_Fixed *) hb_calloc (mm_var->num_axis, sizeof (FT_Fixed));
936  int *coords = (int *) hb_calloc (mm_var->num_axis, sizeof (int));
937  if (coords && ft_coords)
938  {
939  if (!FT_Get_Var_Blend_Coordinates (ft_face, mm_var->num_axis, ft_coords))
940  {
941  bool nonzero = false;
942 
943  for (unsigned int i = 0; i < mm_var->num_axis; ++i)
944  {
945  coords[i] = ft_coords[i] >>= 2;
946  nonzero = nonzero || coords[i];
947  }
948 
949  if (nonzero)
951  else
953  }
954  }
955  hb_free (coords);
956  hb_free (ft_coords);
957 #ifdef HAVE_FT_DONE_MM_VAR
958  FT_Done_MM_Var (ft_face->glyph->library, mm_var);
959 #else
960  hb_free (mm_var);
961 #endif
962  }
963 #endif
964 }
965 
987 hb_font_t *
989 {
990  FT_Reference_Face (ft_face);
991  return hb_ft_font_create (ft_face, _hb_ft_face_destroy);
992 }
993 
994 static inline void free_static_ft_library ();
995 
996 static struct hb_ft_library_lazy_loader_t : hb_lazy_loader_t<hb_remove_pointer<FT_Library>,
997  hb_ft_library_lazy_loader_t>
998 {
999  static FT_Library create ()
1000  {
1001  FT_Library l;
1002  if (FT_Init_FreeType (&l))
1003  return nullptr;
1004 
1005  hb_atexit (free_static_ft_library);
1006 
1007  return l;
1008  }
1009  static void destroy (FT_Library l)
1010  {
1011  FT_Done_FreeType (l);
1012  }
1013  static FT_Library get_null ()
1014  {
1015  return nullptr;
1016  }
1017 } static_ft_library;
1018 
1019 static inline
1020 void free_static_ft_library ()
1021 {
1022  static_ft_library.free_instance ();
1023 }
1024 
1025 static FT_Library
1026 get_ft_library ()
1027 {
1028  return static_ft_library.get_unconst ();
1029 }
1030 
1031 static void
1032 _release_blob (FT_Face ft_face)
1033 {
1034  hb_blob_destroy ((hb_blob_t *) ft_face->generic.data);
1035 }
1036 
1059 void
1061 {
1062  hb_blob_t *blob = hb_face_reference_blob (font->face);
1063  unsigned int blob_length;
1064  const char *blob_data = hb_blob_get_data (blob, &blob_length);
1065  if (unlikely (!blob_length))
1066  DEBUG_MSG (FT, font, "Font face has empty blob");
1067 
1068  FT_Face ft_face = nullptr;
1069  FT_Error err = FT_New_Memory_Face (get_ft_library (),
1070  (const FT_Byte *) blob_data,
1071  blob_length,
1072  hb_face_get_index (font->face),
1073  &ft_face);
1074 
1075  if (unlikely (err)) {
1076  hb_blob_destroy (blob);
1077  DEBUG_MSG (FT, font, "Font face FT_New_Memory_Face() failed");
1078  return;
1079  }
1080 
1081  if (FT_Select_Charmap (ft_face, FT_ENCODING_MS_SYMBOL))
1082  FT_Select_Charmap (ft_face, FT_ENCODING_UNICODE);
1083 
1084  FT_Set_Char_Size (ft_face,
1085  abs (font->x_scale), abs (font->y_scale),
1086  0, 0);
1087 #if 0
1088  font->x_ppem * 72 * 64 / font->x_scale,
1089  font->y_ppem * 72 * 64 / font->y_scale);
1090 #endif
1091  if (font->x_scale < 0 || font->y_scale < 0)
1092  {
1093  FT_Matrix matrix = { font->x_scale < 0 ? -1 : +1, 0,
1094  0, font->y_scale < 0 ? -1 : +1};
1095  FT_Set_Transform (ft_face, &matrix, nullptr);
1096  }
1097 
1098 #if defined(HAVE_FT_GET_VAR_BLEND_COORDINATES) && !defined(HB_NO_VAR)
1099  unsigned int num_coords;
1100  const int *coords = hb_font_get_var_coords_normalized (font, &num_coords);
1101  if (num_coords)
1102  {
1103  FT_Fixed *ft_coords = (FT_Fixed *) hb_calloc (num_coords, sizeof (FT_Fixed));
1104  if (ft_coords)
1105  {
1106  for (unsigned int i = 0; i < num_coords; i++)
1107  ft_coords[i] = coords[i] * 4;
1108  FT_Set_Var_Blend_Coordinates (ft_face, num_coords, ft_coords);
1109  hb_free (ft_coords);
1110  }
1111  }
1112 #endif
1113 
1114  ft_face->generic.data = blob;
1115  ft_face->generic.finalizer = (FT_Generic_Finalizer) _release_blob;
1116 
1117  _hb_ft_font_set_funcs (font, ft_face, true);
1119 }
1120 
1121 
1122 #endif
small capitals from c petite p scientific i
[1]
Definition: afcover.h:80
sizeof(AF_ModuleRec)
FT_Error error
Definition: cffdrivr.c:657
FT_Load_Glyph(FT_Face face, FT_UInt glyph_index, FT_Int32 load_flags)
Definition: ftobjs.c:796
#define FT_LOAD_VERTICAL_LAYOUT
Definition: freetype.h:3026
#define FT_LOAD_NO_BITMAP
Definition: freetype.h:3025
FT_Set_Transform(FT_Face face, FT_Matrix *matrix, FT_Vector *delta)
Definition: ftobjs.c:689
FT_Done_Face(FT_Face face)
Definition: ftobjs.c:2783
#define FT_LOAD_NO_HINTING
Definition: freetype.h:3023
FT_Get_Char_Index(FT_Face face, FT_ULong charcode)
Definition: ftobjs.c:3731
FT_Select_Charmap(FT_Face face, FT_Encoding encoding)
Definition: ftobjs.c:3521
#define FT_LOAD_DEFAULT
Definition: freetype.h:3021
struct FT_FaceRec_ * FT_Face
Definition: freetype.h:483
FT_Face_GetCharVariantIndex(FT_Face face, FT_ULong charcode, FT_ULong variantSelector)
Definition: ftobjs.c:3897
enum FT_Kerning_Mode_ FT_Kerning_Mode
FT_Get_Glyph_Name(FT_Face face, FT_UInt glyph_index, FT_Pointer buffer, FT_UInt buffer_max)
Definition: ftobjs.c:4110
FT_Get_Name_Index(FT_Face face, const FT_String *glyph_name)
Definition: ftobjs.c:4082
FT_Done_FreeType(FT_Library library)
Definition: ftinit.c:235
FT_Get_Kerning(FT_Face face, FT_UInt left_glyph, FT_UInt right_glyph, FT_UInt kern_mode, FT_Vector *akerning)
Definition: ftobjs.c:3411
@ FT_KERNING_UNFITTED
Definition: freetype.h:3407
@ FT_KERNING_DEFAULT
Definition: freetype.h:3406
FT_Set_Char_Size(FT_Face face, FT_F26Dot6 char_width, FT_F26Dot6 char_height, FT_UInt horz_resolution, FT_UInt vert_resolution)
Definition: ftobjs.c:3331
FT_Reference_Face(FT_Face face)
Definition: ftobjs.c:2769
FT_New_Memory_Face(FT_Library library, const FT_Byte *file_base, FT_Long file_size, FT_Long face_index, FT_Face *aface)
Definition: ftobjs.c:1493
FT_MulFix(FT_Long a, FT_Long b)
Definition: ftcalc.c:508
FT_Init_FreeType(FT_Library *alibrary)
Definition: ftinit.c:199
FT_Get_Advance(FT_Face face, FT_UInt gindex, FT_Int32 load_flags, FT_Fixed *padvance)
Definition: ftadvanc.c:74
return FT_Err_Ok
Definition: ftbbox.c:526
int(* FT_Outline_ConicToFunc)(const FT_Vector *control, const FT_Vector *to, void *user)
Definition: ftimage.h:571
int(* FT_Outline_LineToFunc)(const FT_Vector *to, void *user)
Definition: ftimage.h:537
int(* FT_Outline_CubicToFunc)(const FT_Vector *control1, const FT_Vector *control2, const FT_Vector *to, void *user)
Definition: ftimage.h:607
int(* FT_Outline_MoveToFunc)(const FT_Vector *to, void *user)
Definition: ftimage.h:508
FT_Set_Var_Blend_Coordinates(FT_Face face, FT_UInt num_coords, FT_Fixed *coords)
Definition: ftmm.c:392
FT_Get_Var_Blend_Coordinates(FT_Face face, FT_UInt num_coords, FT_Fixed *coords)
Definition: ftmm.c:471
FT_Done_MM_Var(FT_Library library, FT_MM_Var *amaster)
Definition: ftmm.c:150
FT_Get_MM_Var(FT_Face face, FT_MM_Var **amaster)
Definition: ftmm.c:123
FT_BEGIN_HEADER FT_Outline_Decompose(FT_Outline *outline, const FT_Outline_Funcs *func_interface, void *user)
Definition: ftoutln.c:43
unsigned long FT_ULong
Definition: fttypes.h:253
unsigned char FT_Byte
Definition: fttypes.h:154
signed long FT_Fixed
Definition: fttypes.h:287
int FT_Error
Definition: fttypes.h:299
char FT_String
Definition: fttypes.h:187
void(* FT_Generic_Finalizer)(void *object)
Definition: fttypes.h:435
const char * hb_blob_get_data(hb_blob_t *blob, unsigned int *length)
Definition: hb-blob.cc:377
hb_blob_t * hb_blob_create(const char *data, unsigned int length, hb_memory_mode_t mode, void *user_data, hb_destroy_func_t destroy)
Definition: hb-blob.cc:69
void hb_blob_destroy(hb_blob_t *blob)
Definition: hb-blob.cc:262
#define DEBUG_MSG(WHAT, OBJ,...)
void const void *obj HB_UNUSED
Definition: hb-debug.hh:180
void hb_face_set_index(hb_face_t *face, unsigned int index)
Definition: hb-face.cc:439
void hb_face_destroy(hb_face_t *face)
Definition: hb-face.cc:287
void hb_face_set_upem(hb_face_t *face, unsigned int upem)
Definition: hb-face.cc:476
hb_face_t * hb_face_create(hb_blob_t *blob, unsigned int index)
Definition: hb-face.cc:217
hb_face_t * hb_face_create_for_tables(hb_reference_table_func_t reference_table_func, void *user_data, hb_destroy_func_t destroy)
Definition: hb-face.cc:119
hb_face_t * hb_face_reference(hb_face_t *face)
Definition: hb-face.cc:271
unsigned int hb_face_get_index(const hb_face_t *face)
Definition: hb-face.cc:461
hb_blob_t * hb_face_reference_blob(hb_face_t *face)
Definition: hb-face.cc:420
hb_font_t * hb_font_create(hb_face_t *face)
Definition: hb-font.cc:1689
void hb_font_set_var_coords_normalized(hb_font_t *font, const int *coords, unsigned int coords_length)
Definition: hb-font.cc:2387
const int * hb_font_get_var_coords_normalized(hb_font_t *font, unsigned int *length)
Definition: hb-font.cc:2440
void hb_font_set_funcs(hb_font_t *font, hb_font_funcs_t *klass, void *font_data, hb_destroy_func_t destroy)
Definition: hb-font.cc:2013
void hb_font_set_ppem(hb_font_t *font, unsigned int x_ppem, unsigned int y_ppem)
Definition: hb-font.cc:2123
void hb_font_set_scale(hb_font_t *font, int x_scale, int y_scale)
Definition: hb-font.cc:2081
hb_font_funcs_t * hb_font_funcs_create()
Definition: hb-font.cc:691
void hb_font_funcs_make_immutable(hb_font_funcs_t *ffuncs)
Definition: hb-font.cc:810
HB_EXTERN hb_face_t * hb_ft_face_create_referenced(FT_Face ft_face)
HB_EXTERN hb_font_t * hb_ft_font_create(FT_Face ft_face, hb_destroy_func_t destroy)
HB_EXTERN hb_font_t * hb_ft_font_create_referenced(FT_Face ft_face)
HB_EXTERN void hb_ft_font_unlock_face(hb_font_t *font)
HB_EXTERN FT_Face hb_ft_font_get_face(hb_font_t *font)
HB_EXTERN hb_face_t * hb_ft_face_create_cached(FT_Face ft_face)
HB_EXTERN void hb_ft_font_set_funcs(hb_font_t *font)
HB_EXTERN int hb_ft_font_get_load_flags(hb_font_t *font)
HB_EXTERN void hb_ft_font_changed(hb_font_t *font)
HB_BEGIN_DECLS HB_EXTERN hb_face_t * hb_ft_face_create(FT_Face ft_face, hb_destroy_func_t destroy)
HB_EXTERN void hb_ft_font_set_load_flags(hb_font_t *font, int load_flags)
HB_EXTERN FT_Face hb_ft_font_lock_face(hb_font_t *font)
#define hb_calloc
Definition: hb.hh:236
#define hb_malloc
Definition: hb.hh:235
#define hb_atexit(_)
Definition: hb.hh:452
#define unlikely(expr)
Definition: hb.hh:251
#define hb_free
Definition: hb.hh:238
int PRIV() strcmp(PCRE2_SPTR str1, PCRE2_SPTR str2)
int PRIV() strncmp(PCRE2_SPTR str1, PCRE2_SPTR str2, size_t len)
QT_BEGIN_NAMESPACE bool done
GLenum GLuint GLenum GLsizei length
Definition: qopengl.h:270
GLsizei const GLfloat * v
[13]
GLint GLint GLint GLint GLint x
[0]
GLenum mode
GLenum GLuint GLintptr GLsizeiptr size
[1]
GLsizei const GLubyte GLsizei GLenum const void * coords
GLenum GLenum GLsizei count
GLenum face
GLsizei GLenum const void GLuint GLsizei GLfloat * metrics
GLenum GLuint buffer
GLenum GLuint GLenum GLsizei const GLchar * buf
GLint GLsizei GLsizei GLenum GLenum GLsizei void * data
GLboolean GLboolean g
GLuint name
GLint y
GLenum GLsizei len
Definition: qopenglext.h:3292
GLuint GLenum matrix
Definition: qopenglext.h:11564
#define FT(t)
@ HB_MEMORY_MODE_WRITABLE
Definition: hb-blob.h:72
@ HB_MEMORY_MODE_READONLY
Definition: hb-blob.h:71
void(* hb_destroy_func_t)(void *user_data)
Definition: hb-common.h:771
HB_BEGIN_DECLS typedef int hb_bool_t
Definition: hb-common.h:97
uint32_t hb_codepoint_t
Definition: hb-common.h:106
int32_t hb_position_t
Definition: hb-common.h:115
uint32_t hb_tag_t
Definition: hb-common.h:157
HB_EXTERN hb_font_get_glyph_func_t void * user_data
HB_EXTERN hb_font_get_glyph_func_t void hb_destroy_func_t destroy
HB_EXTERN void hb_font_funcs_set_glyph_name_func(hb_font_funcs_t *ffuncs, hb_font_get_glyph_name_func_t func, void *user_data, hb_destroy_func_t destroy)
HB_EXTERN void hb_font_funcs_set_glyph_contour_point_func(hb_font_funcs_t *ffuncs, hb_font_get_glyph_contour_point_func_t func, void *user_data, hb_destroy_func_t destroy)
HB_EXTERN void hb_font_funcs_set_glyph_extents_func(hb_font_funcs_t *ffuncs, hb_font_get_glyph_extents_func_t func, void *user_data, hb_destroy_func_t destroy)
HB_EXTERN void hb_font_funcs_set_glyph_h_kerning_func(hb_font_funcs_t *ffuncs, hb_font_get_glyph_h_kerning_func_t func, void *user_data, hb_destroy_func_t destroy)
HB_EXTERN void hb_font_funcs_set_nominal_glyphs_func(hb_font_funcs_t *ffuncs, hb_font_get_nominal_glyphs_func_t func, void *user_data, hb_destroy_func_t destroy)
HB_EXTERN void hb_font_funcs_set_variation_glyph_func(hb_font_funcs_t *ffuncs, hb_font_get_variation_glyph_func_t func, void *user_data, hb_destroy_func_t destroy)
HB_EXTERN void hb_font_funcs_set_font_h_extents_func(hb_font_funcs_t *ffuncs, hb_font_get_font_h_extents_func_t func, void *user_data, hb_destroy_func_t destroy)
HB_EXTERN void hb_font_funcs_set_glyph_v_origin_func(hb_font_funcs_t *ffuncs, hb_font_get_glyph_v_origin_func_t func, void *user_data, hb_destroy_func_t destroy)
HB_EXTERN void hb_font_funcs_set_glyph_v_advance_func(hb_font_funcs_t *ffuncs, hb_font_get_glyph_v_advance_func_t func, void *user_data, hb_destroy_func_t destroy)
HB_EXTERN void hb_font_funcs_set_glyph_shape_func(hb_font_funcs_t *ffuncs, hb_font_get_glyph_shape_func_t func, void *user_data, hb_destroy_func_t destroy)
HB_EXTERN void hb_font_funcs_set_nominal_glyph_func(hb_font_funcs_t *ffuncs, hb_font_get_nominal_glyph_func_t func, void *user_data, hb_destroy_func_t destroy)
HB_EXTERN void hb_font_funcs_set_glyph_h_advances_func(hb_font_funcs_t *ffuncs, hb_font_get_glyph_h_advances_func_t func, void *user_data, hb_destroy_func_t destroy)
HB_EXTERN void hb_font_funcs_set_glyph_from_name_func(hb_font_funcs_t *ffuncs, hb_font_get_glyph_from_name_func_t func, void *user_data, hb_destroy_func_t destroy)
QReadWriteLock lock
[0]
FT_Encoding encoding
Definition: freetype.h:820
FT_Size size
Definition: freetype.h:1066
FT_Short height
Definition: freetype.h:1057
FT_Short descender
Definition: freetype.h:1056
FT_Stream stream
Definition: freetype.h:1073
FT_UShort units_per_EM
Definition: freetype.h:1054
FT_Generic generic
Definition: freetype.h:1047
FT_Long face_index
Definition: freetype.h:1031
FT_CharMap charmap
Definition: freetype.h:1067
FT_GlyphSlot glyph
Definition: freetype.h:1065
FT_Short ascender
Definition: freetype.h:1055
FT_Generic_Finalizer finalizer
Definition: fttypes.h:470
void * data
Definition: fttypes.h:469
FT_Library library
Definition: freetype.h:1873
FT_Outline outline
Definition: freetype.h:1890
FT_Glyph_Metrics metrics
Definition: freetype.h:1879
FT_Glyph_Format format
Definition: freetype.h:1884
FT_UInt num_axis
Definition: ftmm.h:244
FT_Vector * points
Definition: ftimage.h:341
short n_points
Definition: ftimage.h:339
FT_Fixed y_scale
Definition: freetype.h:1601
FT_Fixed x_scale
Definition: freetype.h:1600
FT_UShort x_ppem
Definition: freetype.h:1597
FT_UShort y_ppem
Definition: freetype.h:1598
FT_Size_Metrics metrics
Definition: freetype.h:1636
unsigned char * base
Definition: ftsystem.h:329
unsigned long size
Definition: ftsystem.h:330
FT_Stream_IoFunc read
Definition: ftsystem.h:335
FT_Pos x
Definition: ftimage.h:77
FT_Pos y
Definition: ftimage.h:78
void line_to(float to_x, float to_y)
Definition: hb-draw.hh:180
void move_to(float to_x, float to_y)
Definition: hb-draw.hh:171
void cubic_to(float control1_x, float control1_y, float control2_x, float control2_y, float to_x, float to_y)
Definition: hb-draw.hh:203
void quadratic_to(float control_x, float control_y, float to_x, float to_y)
Definition: hb-draw.hh:190
hb_position_t x_bearing
Definition: hb-font.h:141
hb_position_t y_bearing
Definition: hb-font.h:142
hb_position_t height
Definition: hb-font.h:144
hb_position_t width
Definition: hb-font.h:143
static void destroy(Stored *p)
static const Stored * get_null()
std::condition_variable cv
FT_UInt left_glyph
Definition: ttdriver.c:203
FT_UInt FT_UInt right_glyph
Definition: ttdriver.c:204
FT_Load_Sfnt_Table(FT_Face face, FT_ULong tag, FT_Long offset, FT_Byte *buffer, FT_ULong *length)
Definition: ftobjs.c:4197
XmlOutput::xml_output tag(const QString &name)
Definition: xmloutput.h:154