helper-cairo-ft.hh 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. /*
  2. * Copyright © 2022 Google, Inc.
  3. *
  4. * This is part of HarfBuzz, a text shaping library.
  5. *
  6. * Permission is hereby granted, without written agreement and without
  7. * license or royalty fees, to use, copy, modify, and distribute this
  8. * software and its documentation for any purpose, provided that the
  9. * above copyright notice and the following two paragraphs appear in
  10. * all copies of this software.
  11. *
  12. * IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE TO ANY PARTY FOR
  13. * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
  14. * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN
  15. * IF THE COPYRIGHT HOLDER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH
  16. * DAMAGE.
  17. *
  18. * THE COPYRIGHT HOLDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING,
  19. * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
  20. * FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS
  21. * ON AN "AS IS" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO
  22. * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
  23. *
  24. * Google Author(s): Behdad Esfahbod
  25. */
  26. #ifndef HELPER_CAIRO_FT_HH
  27. #define HELPER_CAIRO_FT_HH
  28. #include "font-options.hh"
  29. #include <cairo-ft.h>
  30. #include <hb-ft.h>
  31. #include FT_MULTIPLE_MASTERS_H
  32. static FT_Library ft_library;
  33. #ifdef HAVE_ATEXIT
  34. static inline
  35. void free_ft_library ()
  36. {
  37. FT_Done_FreeType (ft_library);
  38. }
  39. #endif
  40. static inline cairo_font_face_t *
  41. helper_cairo_create_ft_font_face (const font_options_t *font_opts)
  42. {
  43. cairo_font_face_t *cairo_face;
  44. /* We cannot use the FT_Face from hb_font_t, as doing so will confuse hb_font_t because
  45. * cairo will reset the face size. As such, create new face...
  46. * TODO Perhaps add API to hb-ft to encapsulate this code. */
  47. FT_Face ft_face = nullptr;//hb_ft_font_get_face (font);
  48. if (!ft_face)
  49. {
  50. if (!ft_library)
  51. {
  52. FT_Init_FreeType (&ft_library);
  53. #ifdef HAVE_ATEXIT
  54. atexit (free_ft_library);
  55. #endif
  56. }
  57. unsigned int blob_length;
  58. const char *blob_data = hb_blob_get_data (font_opts->blob, &blob_length);
  59. if (FT_New_Memory_Face (ft_library,
  60. (const FT_Byte *) blob_data,
  61. blob_length,
  62. font_opts->face_index,
  63. &ft_face))
  64. fail (false, "FT_New_Memory_Face fail");
  65. }
  66. if (!ft_face)
  67. {
  68. /* This allows us to get some boxes at least... */
  69. cairo_face = cairo_toy_font_face_create ("@cairo:sans",
  70. CAIRO_FONT_SLANT_NORMAL,
  71. CAIRO_FONT_WEIGHT_NORMAL);
  72. }
  73. else
  74. {
  75. #if !defined(HB_NO_VAR) && defined(HAVE_FT_SET_VAR_BLEND_COORDINATES)
  76. unsigned int num_coords;
  77. const float *coords = hb_font_get_var_coords_design (font_opts->font, &num_coords);
  78. if (num_coords)
  79. {
  80. FT_Fixed *ft_coords = (FT_Fixed *) calloc (num_coords, sizeof (FT_Fixed));
  81. if (ft_coords)
  82. {
  83. for (unsigned int i = 0; i < num_coords; i++)
  84. ft_coords[i] = coords[i] * 65536.f;
  85. FT_Set_Var_Design_Coordinates (ft_face, num_coords, ft_coords);
  86. free (ft_coords);
  87. }
  88. }
  89. #endif
  90. cairo_face = cairo_ft_font_face_create_for_ft_face (ft_face, font_opts->ft_load_flags);
  91. }
  92. return cairo_face;
  93. }
  94. static inline bool
  95. helper_cairo_ft_scaled_font_has_color (cairo_scaled_font_t *scaled_font)
  96. {
  97. bool ret = false;
  98. #ifdef FT_HAS_COLOR
  99. FT_Face ft_face = cairo_ft_scaled_font_lock_face (scaled_font);
  100. if (ft_face)
  101. {
  102. if (FT_HAS_COLOR (ft_face))
  103. ret = true;
  104. cairo_ft_scaled_font_unlock_face (scaled_font);
  105. }
  106. #endif
  107. return ret;
  108. }
  109. #endif