operator-source.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. /*
  2. * Copyright © 2005 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. * Authors: Kristian Høgsberg <krh@redhat.com>
  24. * Owen Taylor <otaylor@redhat.com>
  25. * Uli Schlachter <psychon@znc.in>
  26. */
  27. #include "cairo-test.h"
  28. #include <math.h>
  29. #include <stdio.h>
  30. #define WIDTH 16
  31. #define HEIGHT 16
  32. #define PAD 2
  33. static void
  34. set_solid_pattern (cairo_t *cr, int x, int y)
  35. {
  36. cairo_set_source_rgb (cr, 1.0, 0, 0.0);
  37. }
  38. static void
  39. set_translucent_pattern (cairo_t *cr, int x, int y)
  40. {
  41. cairo_set_source_rgba (cr, 1, 0, 0, 0.5);
  42. }
  43. static void
  44. set_gradient_pattern (cairo_t *cr, int x, int y)
  45. {
  46. cairo_pattern_t *pattern;
  47. pattern = cairo_pattern_create_linear (x, y, x + WIDTH, y + HEIGHT);
  48. cairo_pattern_add_color_stop_rgba (pattern, 0.2, 1, 0, 0, 1);
  49. cairo_pattern_add_color_stop_rgba (pattern, 0.8, 1, 0, 0, 0.0);
  50. cairo_set_source (cr, pattern);
  51. cairo_pattern_destroy (pattern);
  52. }
  53. static void
  54. set_surface_pattern (cairo_t *cr, int x, int y)
  55. {
  56. cairo_surface_t *source_surface;
  57. cairo_t *cr2;
  58. double width = (int)(0.6 * WIDTH);
  59. double height = (int)(0.6 * HEIGHT);
  60. x += 0.2 * WIDTH;
  61. y += 0.2 * HEIGHT;
  62. source_surface = cairo_surface_create_similar (cairo_get_group_target (cr),
  63. CAIRO_CONTENT_COLOR_ALPHA,
  64. width, height);
  65. cr2 = cairo_create (source_surface);
  66. cairo_surface_destroy (source_surface);
  67. cairo_set_source_rgb (cr2, 1, 0, 0); /* red */
  68. cairo_paint (cr2);
  69. cairo_set_source_rgb (cr2, 1, 1, 1); /* white */
  70. cairo_arc (cr2, 0.5 * width, 0.5 * height, 0.5 * height, 0, 2 * M_PI);
  71. cairo_fill (cr2);
  72. cairo_set_source_surface (cr, cairo_get_target (cr2), x, y);
  73. cairo_destroy (cr2);
  74. }
  75. static void
  76. draw_mask (cairo_t *cr, int x, int y)
  77. {
  78. cairo_surface_t *mask_surface;
  79. cairo_t *cr2;
  80. double width = (int)(0.9 * WIDTH);
  81. double height = (int)(0.9 * HEIGHT);
  82. x += 0.05 * WIDTH;
  83. y += 0.05 * HEIGHT;
  84. mask_surface = cairo_surface_create_similar (cairo_get_group_target (cr),
  85. CAIRO_CONTENT_ALPHA,
  86. width, height);
  87. cr2 = cairo_create (mask_surface);
  88. cairo_surface_destroy (mask_surface);
  89. cairo_set_source_rgb (cr2, 1, 1, 1); /* white */
  90. cairo_arc (cr2, 0.5 * width, 0.5 * height, 0.45 * height, 0, 2 * M_PI);
  91. cairo_fill (cr2);
  92. cairo_mask_surface (cr, cairo_get_target (cr2), x, y);
  93. cairo_destroy (cr2);
  94. }
  95. static void
  96. draw_glyphs (cairo_t *cr, int x, int y)
  97. {
  98. cairo_text_extents_t extents;
  99. cairo_set_font_size (cr, 0.8 * HEIGHT);
  100. cairo_text_extents (cr, "FG", &extents);
  101. cairo_move_to (cr,
  102. x + floor ((WIDTH - extents.width) / 2 + 0.5) - extents.x_bearing,
  103. y + floor ((HEIGHT - extents.height) / 2 + 0.5) - extents.y_bearing);
  104. cairo_show_text (cr, "FG");
  105. }
  106. static void
  107. draw_polygon (cairo_t *cr, int x, int y)
  108. {
  109. double width = (int)(0.9 * WIDTH);
  110. double height = (int)(0.9 * HEIGHT);
  111. x += 0.05 * WIDTH;
  112. y += 0.05 * HEIGHT;
  113. cairo_new_path (cr);
  114. cairo_move_to (cr, x, y);
  115. cairo_line_to (cr, x, y + height);
  116. cairo_line_to (cr, x + width / 2, y + 3 * height / 4);
  117. cairo_line_to (cr, x + width, y + height);
  118. cairo_line_to (cr, x + width, y);
  119. cairo_line_to (cr, x + width / 2, y + height / 4);
  120. cairo_close_path (cr);
  121. cairo_fill (cr);
  122. }
  123. static void
  124. draw_rects (cairo_t *cr, int x, int y, double offset)
  125. {
  126. double block_width = (int)(0.33 * WIDTH + 0.5) - offset/3;
  127. double block_height = (int)(0.33 * HEIGHT + 0.5) - offset/3;
  128. int i, j;
  129. x += offset/2;
  130. y += offset/2;
  131. for (i = 0; i < 3; i++)
  132. for (j = 0; j < 3; j++)
  133. if ((i + j) % 2 == 0)
  134. cairo_rectangle (cr,
  135. x + block_width * i, y + block_height * j,
  136. block_width, block_height);
  137. cairo_fill (cr);
  138. }
  139. static void
  140. draw_aligned_rects (cairo_t *cr, int x, int y)
  141. {
  142. draw_rects (cr, x, y, 0);
  143. }
  144. static void
  145. draw_unaligned_rects (cairo_t *cr, int x, int y)
  146. {
  147. draw_rects (cr, x, y, 2.1);
  148. }
  149. static void (* const pattern_funcs[])(cairo_t *cr, int x, int y) = {
  150. set_solid_pattern,
  151. set_translucent_pattern,
  152. set_gradient_pattern,
  153. set_surface_pattern,
  154. };
  155. static void (* const draw_funcs[])(cairo_t *cr, int x, int y) = {
  156. draw_mask,
  157. draw_glyphs,
  158. draw_polygon,
  159. draw_aligned_rects,
  160. draw_unaligned_rects
  161. };
  162. #define IMAGE_WIDTH (ARRAY_LENGTH (pattern_funcs) * (WIDTH + PAD) + PAD)
  163. #define IMAGE_HEIGHT (ARRAY_LENGTH (draw_funcs) * (HEIGHT + PAD) + PAD)
  164. static cairo_test_status_t
  165. draw (cairo_t *cr, int width, int height)
  166. {
  167. const cairo_test_context_t *ctx = cairo_test_get_context (cr);
  168. size_t i, j, x, y;
  169. cairo_pattern_t *pattern;
  170. cairo_select_font_face (cr, CAIRO_TEST_FONT_FAMILY " Sans",
  171. CAIRO_FONT_SLANT_NORMAL,
  172. CAIRO_FONT_WEIGHT_NORMAL);
  173. for (j = 0; j < ARRAY_LENGTH (draw_funcs); j++) {
  174. for (i = 0; i < ARRAY_LENGTH (pattern_funcs); i++) {
  175. x = i * (WIDTH + PAD) + PAD;
  176. y = j * (HEIGHT + PAD) + PAD;
  177. cairo_save (cr);
  178. pattern = cairo_pattern_create_linear (x + WIDTH, y,
  179. x, y + HEIGHT);
  180. cairo_pattern_add_color_stop_rgba (pattern, 0.2,
  181. 0.0, 0.0, 1.0, 1.0); /* Solid blue */
  182. cairo_pattern_add_color_stop_rgba (pattern, 0.8,
  183. 0.0, 0.0, 1.0, 0.0); /* Transparent blue */
  184. cairo_set_source (cr, pattern);
  185. cairo_pattern_destroy (pattern);
  186. cairo_rectangle (cr, x, y, WIDTH, HEIGHT);
  187. cairo_fill_preserve (cr);
  188. cairo_clip (cr);
  189. cairo_set_operator (cr, CAIRO_OPERATOR_SOURCE);
  190. pattern_funcs[i] (cr, x, y);
  191. draw_funcs[j] (cr, x, y);
  192. if (cairo_status (cr))
  193. cairo_test_log (ctx, "%d %d HERE!\n", (int)i, (int)j);
  194. cairo_restore (cr);
  195. }
  196. }
  197. if (cairo_status (cr) != CAIRO_STATUS_SUCCESS)
  198. cairo_test_log (ctx, "%d %d .HERE!\n", (int)i, (int)j);
  199. return CAIRO_TEST_SUCCESS;
  200. }
  201. CAIRO_TEST (operator_source,
  202. "Test of CAIRO_OPERATOR_SOURCE",
  203. "operator", /* keywords */
  204. NULL, /* requirements */
  205. IMAGE_WIDTH, IMAGE_HEIGHT,
  206. NULL, draw)