helper-cairo-user.hh 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  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_USER_HH
  27. #define HELPER_CAIRO_USER_HH
  28. #include "font-options.hh"
  29. #include <cairo.h>
  30. #include <hb.h>
  31. #include "hb-blob.hh"
  32. static void
  33. move_to (hb_draw_funcs_t *dfuncs,
  34. cairo_t *cr,
  35. hb_draw_state_t *st,
  36. float to_x, float to_y,
  37. void *)
  38. {
  39. cairo_move_to (cr,
  40. (double) to_x, (double) to_y);
  41. }
  42. static void
  43. line_to (hb_draw_funcs_t *dfuncs,
  44. cairo_t *cr,
  45. hb_draw_state_t *st,
  46. float to_x, float to_y,
  47. void *)
  48. {
  49. cairo_line_to (cr,
  50. (double) to_x, (double) to_y);
  51. }
  52. static void
  53. cubic_to (hb_draw_funcs_t *dfuncs,
  54. cairo_t *cr,
  55. hb_draw_state_t *st,
  56. float control1_x, float control1_y,
  57. float control2_x, float control2_y,
  58. float to_x, float to_y,
  59. void *)
  60. {
  61. cairo_curve_to (cr,
  62. (double) control1_x, (double) control1_y,
  63. (double) control2_x, (double) control2_y,
  64. (double) to_x, (double) to_y);
  65. }
  66. static void
  67. close_path (hb_draw_funcs_t *dfuncs,
  68. cairo_t *cr,
  69. hb_draw_state_t *st,
  70. void *)
  71. {
  72. cairo_close_path (cr);
  73. }
  74. static hb_draw_funcs_t *
  75. get_cairo_draw_funcs ()
  76. {
  77. static hb_draw_funcs_t *funcs;
  78. if (!funcs)
  79. {
  80. funcs = hb_draw_funcs_create ();
  81. hb_draw_funcs_set_move_to_func (funcs, (hb_draw_move_to_func_t) move_to, nullptr, nullptr);
  82. hb_draw_funcs_set_line_to_func (funcs, (hb_draw_line_to_func_t) line_to, nullptr, nullptr);
  83. hb_draw_funcs_set_cubic_to_func (funcs, (hb_draw_cubic_to_func_t) cubic_to, nullptr, nullptr);
  84. hb_draw_funcs_set_close_path_func (funcs, (hb_draw_close_path_func_t) close_path, nullptr, nullptr);
  85. }
  86. return funcs;
  87. }
  88. static const cairo_user_data_key_t _hb_font_cairo_user_data_key = {0};
  89. static cairo_status_t
  90. render_glyph (cairo_scaled_font_t *scaled_font,
  91. unsigned long glyph,
  92. cairo_t *cr,
  93. cairo_text_extents_t *extents)
  94. {
  95. hb_font_t *font = (hb_font_t *) (cairo_font_face_get_user_data (cairo_scaled_font_get_font_face (scaled_font),
  96. &_hb_font_cairo_user_data_key));
  97. hb_position_t x_scale, y_scale;
  98. hb_font_get_scale (font, &x_scale, &y_scale);
  99. cairo_scale (cr, +1./x_scale, -1./y_scale);
  100. hb_font_get_glyph_shape (font, glyph, get_cairo_draw_funcs (), cr);
  101. cairo_fill (cr);
  102. return CAIRO_STATUS_SUCCESS;
  103. }
  104. #ifdef HAVE_CAIRO_USER_FONT_FACE_SET_RENDER_COLOR_GLYPH_FUNC
  105. #ifdef CAIRO_HAS_PNG_FUNCTIONS
  106. static inline cairo_status_t
  107. _hb_bytes_read_func (hb_bytes_t *src,
  108. char *data,
  109. unsigned length)
  110. {
  111. if (unlikely (src->length < length))
  112. return CAIRO_STATUS_READ_ERROR;
  113. memcpy (data, src->arrayZ, length);
  114. *src += length;
  115. return CAIRO_STATUS_SUCCESS;
  116. }
  117. static cairo_status_t
  118. render_color_glyph_png (cairo_scaled_font_t *scaled_font,
  119. unsigned long glyph,
  120. cairo_t *cr,
  121. cairo_text_extents_t *extents)
  122. {
  123. hb_font_t *font = (hb_font_t *) (cairo_font_face_get_user_data (cairo_scaled_font_get_font_face (scaled_font),
  124. &_hb_font_cairo_user_data_key));
  125. hb_blob_t *blob = hb_ot_color_glyph_reference_png (font, glyph);
  126. if (blob == hb_blob_get_empty ())
  127. return CAIRO_STATUS_USER_FONT_NOT_IMPLEMENTED;
  128. hb_position_t x_scale, y_scale;
  129. hb_font_get_scale (font, &x_scale, &y_scale);
  130. cairo_scale (cr, +1./x_scale, -1./y_scale);
  131. /* Draw PNG. */
  132. hb_bytes_t bytes = blob->as_bytes ();
  133. cairo_surface_t *surface = cairo_image_surface_create_from_png_stream ((cairo_read_func_t) _hb_bytes_read_func,
  134. std::addressof (bytes));
  135. hb_blob_destroy (blob);
  136. if (unlikely (cairo_surface_status (surface)) != CAIRO_STATUS_SUCCESS)
  137. {
  138. cairo_surface_destroy (surface);
  139. return CAIRO_STATUS_USER_FONT_NOT_IMPLEMENTED;
  140. }
  141. int width = cairo_image_surface_get_width (surface);
  142. int height = cairo_image_surface_get_width (surface);
  143. hb_glyph_extents_t hb_extents;
  144. if (unlikely (!hb_font_get_glyph_extents (font, glyph, &hb_extents)))
  145. {
  146. cairo_surface_destroy (surface);
  147. return CAIRO_STATUS_USER_FONT_NOT_IMPLEMENTED;
  148. }
  149. cairo_pattern_t *pattern = cairo_pattern_create_for_surface (surface);
  150. cairo_pattern_set_extend (pattern, CAIRO_EXTEND_PAD);
  151. cairo_matrix_t matrix = {(double) width, 0, 0, (double) height, 0, 0};
  152. cairo_pattern_set_matrix (pattern, &matrix);
  153. cairo_translate (cr, hb_extents.x_bearing, hb_extents.y_bearing);
  154. cairo_scale (cr, hb_extents.width, hb_extents.height);
  155. cairo_set_source (cr, pattern);
  156. cairo_rectangle (cr, 0, 0, 1, 1);
  157. cairo_fill (cr);
  158. cairo_pattern_destroy (pattern);
  159. cairo_surface_destroy (surface);
  160. return CAIRO_STATUS_SUCCESS;
  161. }
  162. #endif
  163. static cairo_status_t
  164. render_color_glyph_layers (cairo_scaled_font_t *scaled_font,
  165. unsigned long glyph,
  166. cairo_t *cr,
  167. cairo_text_extents_t *extents)
  168. {
  169. hb_font_t *font = (hb_font_t *) (cairo_font_face_get_user_data (cairo_scaled_font_get_font_face (scaled_font),
  170. &_hb_font_cairo_user_data_key));
  171. hb_face_t *face = hb_font_get_face (font);
  172. unsigned count = hb_ot_color_glyph_get_layers (face, glyph, 0, nullptr, nullptr);
  173. if (!count)
  174. return CAIRO_STATUS_USER_FONT_NOT_IMPLEMENTED;
  175. hb_ot_color_layer_t layers[16];
  176. unsigned offset = 0, len;
  177. do {
  178. len = ARRAY_LENGTH (layers);
  179. hb_ot_color_glyph_get_layers (face, glyph,
  180. offset,
  181. &len,
  182. layers);
  183. for (unsigned i = 0; i < len; i++)
  184. {
  185. hb_color_t color;
  186. unsigned clen = 1;
  187. unsigned color_index = layers[i].color_index;
  188. bool is_foreground = color_index == 65535;
  189. if (!is_foreground)
  190. {
  191. hb_ot_color_palette_get_colors (face,
  192. 0/*palette_index*/,
  193. color_index/*start_offset*/,
  194. &clen/*color_count*/,
  195. &color);
  196. if (clen < 1)
  197. continue;
  198. }
  199. cairo_save (cr);
  200. {
  201. if (!is_foreground)
  202. cairo_set_source_rgba (cr,
  203. hb_color_get_red (color) / 255.,
  204. hb_color_get_green (color) / 255.,
  205. hb_color_get_blue (color) / 255.,
  206. hb_color_get_alpha (color) / 255.);
  207. cairo_status_t ret = render_glyph (scaled_font, layers[i].glyph, cr, extents);
  208. if (ret != CAIRO_STATUS_SUCCESS)
  209. return ret;
  210. }
  211. cairo_restore (cr);
  212. }
  213. }
  214. while (len == ARRAY_LENGTH (layers));
  215. return CAIRO_STATUS_SUCCESS;
  216. }
  217. static cairo_status_t
  218. render_color_glyph (cairo_scaled_font_t *scaled_font,
  219. unsigned long glyph,
  220. cairo_t *cr,
  221. cairo_text_extents_t *extents)
  222. {
  223. cairo_status_t ret = CAIRO_STATUS_USER_FONT_NOT_IMPLEMENTED;
  224. #ifdef CAIRO_HAS_PNG_FUNCTIONS
  225. ret = render_color_glyph_png (scaled_font, glyph, cr, extents);
  226. if (ret != CAIRO_STATUS_USER_FONT_NOT_IMPLEMENTED)
  227. return ret;
  228. #endif
  229. ret = render_color_glyph_layers (scaled_font, glyph, cr, extents);
  230. if (ret != CAIRO_STATUS_USER_FONT_NOT_IMPLEMENTED)
  231. return ret;
  232. return render_glyph (scaled_font, glyph, cr, extents);
  233. }
  234. #endif
  235. static inline cairo_font_face_t *
  236. helper_cairo_create_user_font_face (const font_options_t *font_opts)
  237. {
  238. cairo_font_face_t *cairo_face = cairo_user_font_face_create ();
  239. cairo_font_face_set_user_data (cairo_face,
  240. &_hb_font_cairo_user_data_key,
  241. hb_font_reference (font_opts->font),
  242. (cairo_destroy_func_t) hb_font_destroy);
  243. cairo_user_font_face_set_render_glyph_func (cairo_face, render_glyph);
  244. #ifdef HAVE_CAIRO_USER_FONT_FACE_SET_RENDER_COLOR_GLYPH_FUNC
  245. hb_face_t *face = hb_font_get_face (font_opts->font);
  246. if (hb_ot_color_has_png (face) || hb_ot_color_has_layers (face))
  247. cairo_user_font_face_set_render_color_glyph_func (cairo_face, render_color_glyph);
  248. #endif
  249. return cairo_face;
  250. }
  251. static inline bool
  252. helper_cairo_user_font_face_has_data (cairo_font_face_t *font_face)
  253. {
  254. return cairo_font_face_get_user_data (font_face, &_hb_font_cairo_user_data_key);
  255. }
  256. static inline bool
  257. helper_cairo_user_scaled_font_has_color (cairo_scaled_font_t *scaled_font)
  258. {
  259. /* Ignoring SVG for now, since we cannot render it. */
  260. hb_font_t *font = (hb_font_t *) (cairo_font_face_get_user_data (cairo_scaled_font_get_font_face (scaled_font),
  261. &_hb_font_cairo_user_data_key));
  262. hb_face_t *face = hb_font_get_face (font);
  263. return hb_ot_color_has_png (face) || hb_ot_color_has_layers (face);
  264. }
  265. #endif