clear-source.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. /*
  2. * Copyright 2009 Benjamin Otte
  3. *
  4. * Permission is hereby granted, free of charge, to any person
  5. * obtaining a copy of this software and associated documentation
  6. * files (the "Software"), to deal in the Software without
  7. * restriction, including without limitation the rights to use, copy,
  8. * modify, merge, publish, distribute, sublicense, and/or sell copies
  9. * of the Software, and to permit persons to whom the Software is
  10. * furnished to do so, subject to the following conditions:
  11. *
  12. * The above copyright notice and this permission notice shall be
  13. * included in all copies or substantial portions of the Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  16. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  17. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  18. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  19. * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  20. * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  21. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  22. * SOFTWARE.
  23. *
  24. * Author: Benjamin Otte <otte@gnome.org>
  25. */
  26. #include "cairo-test.h"
  27. typedef enum {
  28. CLEAR,
  29. CLEARED,
  30. PAINTED
  31. } surface_type_t;
  32. #define SIZE 10
  33. #define SPACE 5
  34. static cairo_surface_t *
  35. create_surface (cairo_t *target, cairo_content_t content, surface_type_t type)
  36. {
  37. cairo_surface_t *surface;
  38. cairo_t *cr;
  39. surface = cairo_surface_create_similar (cairo_get_target (target),
  40. content,
  41. SIZE, SIZE);
  42. if (type == CLEAR)
  43. return surface;
  44. cr = cairo_create (surface);
  45. cairo_surface_destroy (surface);
  46. cairo_set_source_rgb (cr, 0.75, 0, 0);
  47. cairo_paint (cr);
  48. if (type == PAINTED)
  49. goto DONE;
  50. cairo_set_operator (cr, CAIRO_OPERATOR_CLEAR);
  51. cairo_paint (cr);
  52. DONE:
  53. surface = cairo_surface_reference (cairo_get_target (cr));
  54. cairo_destroy (cr);
  55. return surface;
  56. }
  57. static void
  58. paint (cairo_t *cr, cairo_surface_t *surface)
  59. {
  60. cairo_set_source_surface (cr, surface, 0, 0);
  61. cairo_paint (cr);
  62. }
  63. static void
  64. fill (cairo_t *cr, cairo_surface_t *surface)
  65. {
  66. cairo_set_source_surface (cr, surface, 0, 0);
  67. cairo_rectangle (cr, -SPACE, -SPACE, SIZE + 2 * SPACE, SIZE + 2 * SPACE);
  68. cairo_fill (cr);
  69. }
  70. static void
  71. stroke (cairo_t *cr, cairo_surface_t *surface)
  72. {
  73. cairo_set_source_surface (cr, surface, 0, 0);
  74. cairo_set_line_width (cr, 2.0);
  75. cairo_rectangle (cr, 1, 1, SIZE - 2, SIZE - 2);
  76. cairo_stroke (cr);
  77. }
  78. static void
  79. mask (cairo_t *cr, cairo_surface_t *surface)
  80. {
  81. cairo_set_source_rgb (cr, 0, 0, 0.75);
  82. cairo_mask_surface (cr, surface, 0, 0);
  83. }
  84. static void
  85. mask_self (cairo_t *cr, cairo_surface_t *surface)
  86. {
  87. cairo_set_source_surface (cr, surface, 0, 0);
  88. cairo_mask_surface (cr, surface, 0, 0);
  89. }
  90. static void
  91. glyphs (cairo_t *cr, cairo_surface_t *surface)
  92. {
  93. cairo_set_source_surface (cr, surface, 0, 0);
  94. cairo_select_font_face (cr,
  95. "@cairo:",
  96. CAIRO_FONT_SLANT_NORMAL,
  97. CAIRO_FONT_WEIGHT_NORMAL);
  98. cairo_set_font_size (cr, 16);
  99. cairo_translate (cr, 0, SIZE);
  100. cairo_show_text (cr, "C");
  101. }
  102. typedef void (* operation_t) (cairo_t *cr, cairo_surface_t *surface);
  103. static operation_t operations[] = {
  104. paint,
  105. fill,
  106. stroke,
  107. mask,
  108. mask_self,
  109. glyphs
  110. };
  111. static cairo_test_status_t
  112. draw (cairo_t *cr, int width, int height)
  113. {
  114. cairo_content_t contents[] = { CAIRO_CONTENT_COLOR_ALPHA, CAIRO_CONTENT_COLOR, CAIRO_CONTENT_ALPHA };
  115. unsigned int content, type, ops;
  116. cairo_set_source_rgb (cr, 0.5, 0.5, 0.5);
  117. cairo_paint (cr);
  118. cairo_translate (cr, SPACE, SPACE);
  119. for (type = 0; type <= PAINTED; type++) {
  120. for (content = 0; content < ARRAY_LENGTH (contents); content++) {
  121. cairo_surface_t *surface;
  122. surface = create_surface (cr, contents[content], type);
  123. cairo_save (cr);
  124. for (ops = 0; ops < ARRAY_LENGTH (operations); ops++) {
  125. cairo_save (cr);
  126. operations[ops] (cr, surface);
  127. cairo_restore (cr);
  128. cairo_translate (cr, 0, SIZE + SPACE);
  129. }
  130. cairo_restore (cr);
  131. cairo_translate (cr, SIZE + SPACE, 0);
  132. cairo_surface_destroy (surface);
  133. }
  134. }
  135. return CAIRO_TEST_SUCCESS;
  136. }
  137. CAIRO_TEST (clear_source,
  138. "Check painting with cleared surfaces works as expected",
  139. NULL, /* keywords */
  140. NULL, /* requirements */
  141. (SIZE + SPACE) * 9 + SPACE, ARRAY_LENGTH (operations) * (SIZE + SPACE) + SPACE,
  142. NULL, draw)