user-font-proxy.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. /*
  2. * Copyright © 2006, 2008 Red Hat, Inc.
  3. *
  4. * Permission to use, copy, modify, distribute, and sell this software
  5. * and its documentation for any purpose is hereby granted without
  6. * fee, provided that the above copyright notice appear in all copies
  7. * and that both that copyright notice and this permission notice
  8. * appear in supporting documentation, and that the name of
  9. * Red Hat, Inc. not be used in advertising or publicity pertaining to
  10. * distribution of the software without specific, written prior
  11. * permission. Red Hat, Inc. makes no representations about the
  12. * suitability of this software for any purpose. It is provided "as
  13. * is" without express or implied warranty.
  14. *
  15. * RED HAT, INC. DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS
  16. * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
  17. * FITNESS, IN NO EVENT SHALL RED HAT, INC. BE LIABLE FOR ANY SPECIAL,
  18. * INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
  19. * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
  20. * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR
  21. * IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  22. *
  23. * Contributor(s):
  24. * Kristian Høgsberg <krh@redhat.com>
  25. * Behdad Esfahbod <behdad@behdad.org>
  26. */
  27. #include "cairo-test.h"
  28. #include <stdlib.h>
  29. #include <stdio.h>
  30. /*#define ROTATED 1*/
  31. #define BORDER 10
  32. #define TEXT_SIZE 64
  33. #define WIDTH (TEXT_SIZE * 12 + 2*BORDER)
  34. #ifndef ROTATED
  35. #define HEIGHT ((TEXT_SIZE + 2*BORDER)*2)
  36. #else
  37. #define HEIGHT WIDTH
  38. #endif
  39. #define TEXT "geez... cairo user-font"
  40. static cairo_user_data_key_t fallback_font_key;
  41. static cairo_status_t
  42. test_scaled_font_init (cairo_scaled_font_t *scaled_font,
  43. cairo_t *cr,
  44. cairo_font_extents_t *extents)
  45. {
  46. cairo_status_t status;
  47. cairo_set_font_face (cr,
  48. cairo_font_face_get_user_data (cairo_scaled_font_get_font_face (scaled_font),
  49. &fallback_font_key));
  50. status = cairo_scaled_font_set_user_data (scaled_font,
  51. &fallback_font_key,
  52. cairo_scaled_font_reference (cairo_get_scaled_font (cr)),
  53. (cairo_destroy_func_t) cairo_scaled_font_destroy);
  54. if (unlikely (status)) {
  55. cairo_scaled_font_destroy (cairo_get_scaled_font (cr));
  56. return status;
  57. }
  58. cairo_font_extents (cr, extents);
  59. return CAIRO_STATUS_SUCCESS;
  60. }
  61. static cairo_status_t
  62. test_scaled_font_render_glyph (cairo_scaled_font_t *scaled_font,
  63. unsigned long glyph,
  64. cairo_t *cr,
  65. cairo_text_extents_t *extents)
  66. {
  67. cairo_glyph_t cairo_glyph;
  68. cairo_glyph.index = glyph;
  69. cairo_glyph.x = 0;
  70. cairo_glyph.y = 0;
  71. cairo_set_font_face (cr,
  72. cairo_font_face_get_user_data (cairo_scaled_font_get_font_face (scaled_font),
  73. &fallback_font_key));
  74. cairo_show_glyphs (cr, &cairo_glyph, 1);
  75. cairo_glyph_extents (cr, &cairo_glyph, 1, extents);
  76. return CAIRO_STATUS_SUCCESS;
  77. }
  78. static cairo_status_t
  79. test_scaled_font_text_to_glyphs (cairo_scaled_font_t *scaled_font,
  80. const char *utf8,
  81. int utf8_len,
  82. cairo_glyph_t **glyphs,
  83. int *num_glyphs,
  84. cairo_text_cluster_t **clusters,
  85. int *num_clusters,
  86. cairo_text_cluster_flags_t *cluster_flags)
  87. {
  88. cairo_scaled_font_t *fallback_scaled_font;
  89. fallback_scaled_font = cairo_scaled_font_get_user_data (scaled_font,
  90. &fallback_font_key);
  91. return cairo_scaled_font_text_to_glyphs (fallback_scaled_font, 0, 0,
  92. utf8, utf8_len,
  93. glyphs, num_glyphs,
  94. clusters, num_clusters, cluster_flags);
  95. }
  96. static cairo_status_t
  97. _user_font_face_create (cairo_font_face_t **out)
  98. {
  99. cairo_font_face_t *user_font_face;
  100. cairo_font_face_t *fallback_font_face;
  101. cairo_status_t status;
  102. user_font_face = cairo_user_font_face_create ();
  103. cairo_user_font_face_set_init_func (user_font_face, test_scaled_font_init);
  104. cairo_user_font_face_set_render_glyph_func (user_font_face, test_scaled_font_render_glyph);
  105. cairo_user_font_face_set_text_to_glyphs_func (user_font_face, test_scaled_font_text_to_glyphs);
  106. /* This also happens to be default font face on cairo_t, so does
  107. * not make much sense here. For demonstration only.
  108. */
  109. fallback_font_face = cairo_toy_font_face_create ("",
  110. CAIRO_FONT_SLANT_NORMAL,
  111. CAIRO_FONT_WEIGHT_NORMAL);
  112. status = cairo_font_face_set_user_data (user_font_face,
  113. &fallback_font_key,
  114. fallback_font_face,
  115. (cairo_destroy_func_t) cairo_font_face_destroy);
  116. if (status) {
  117. cairo_font_face_destroy (fallback_font_face);
  118. cairo_font_face_destroy (user_font_face);
  119. return status;
  120. }
  121. *out = user_font_face;
  122. return CAIRO_STATUS_SUCCESS;
  123. }
  124. static cairo_test_status_t
  125. draw (cairo_t *cr, int width, int height)
  126. {
  127. const char text[] = TEXT;
  128. cairo_font_extents_t font_extents;
  129. cairo_text_extents_t extents;
  130. cairo_font_face_t *font_face;
  131. cairo_status_t status;
  132. cairo_set_source_rgb (cr, 1, 1, 1);
  133. cairo_paint (cr);
  134. #ifdef ROTATED
  135. cairo_translate (cr, TEXT_SIZE, 0);
  136. cairo_rotate (cr, .6);
  137. #endif
  138. status = _user_font_face_create (&font_face);
  139. if (status) {
  140. return cairo_test_status_from_status (cairo_test_get_context (cr),
  141. status);
  142. }
  143. cairo_set_font_face (cr, font_face);
  144. cairo_font_face_destroy (font_face);
  145. cairo_set_font_size (cr, TEXT_SIZE);
  146. cairo_font_extents (cr, &font_extents);
  147. cairo_text_extents (cr, text, &extents);
  148. /* logical boundaries in red */
  149. cairo_move_to (cr, 0, BORDER);
  150. cairo_rel_line_to (cr, WIDTH, 0);
  151. cairo_move_to (cr, 0, BORDER + font_extents.ascent);
  152. cairo_rel_line_to (cr, WIDTH, 0);
  153. cairo_move_to (cr, 0, BORDER + font_extents.ascent + font_extents.descent);
  154. cairo_rel_line_to (cr, WIDTH, 0);
  155. cairo_move_to (cr, BORDER, 0);
  156. cairo_rel_line_to (cr, 0, 2*BORDER + TEXT_SIZE);
  157. cairo_move_to (cr, BORDER + extents.x_advance, 0);
  158. cairo_rel_line_to (cr, 0, 2*BORDER + TEXT_SIZE);
  159. cairo_set_source_rgb (cr, 1, 0, 0);
  160. cairo_set_line_width (cr, 2);
  161. cairo_stroke (cr);
  162. /* ink boundaries in green */
  163. cairo_rectangle (cr,
  164. BORDER + extents.x_bearing, BORDER + font_extents.ascent + extents.y_bearing,
  165. extents.width, extents.height);
  166. cairo_set_source_rgb (cr, 0, 1, 0);
  167. cairo_set_line_width (cr, 2);
  168. cairo_stroke (cr);
  169. /* text in gray */
  170. cairo_set_source_rgb (cr, 0, 0, 0);
  171. cairo_move_to (cr, BORDER, BORDER + font_extents.ascent);
  172. cairo_show_text (cr, text);
  173. /* filled version of text in light blue */
  174. cairo_set_source_rgb (cr, 0, 0, 1);
  175. cairo_move_to (cr, BORDER, BORDER + font_extents.height + BORDER + font_extents.ascent);
  176. cairo_text_path (cr, text);
  177. cairo_fill (cr);
  178. return CAIRO_TEST_SUCCESS;
  179. }
  180. CAIRO_TEST (user_font_proxy,
  181. "Tests a user-font using a native font in its render_glyph",
  182. "font, user-font", /* keywords */
  183. "cairo >= 1.7.4", /* requirements */
  184. WIDTH, HEIGHT,
  185. NULL, draw)