recording-surface-pattern.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. /*
  2. * Copyright © 2007 Adrian Johnson
  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: Adrian Johnson <ajohnson@redneon.com>
  25. */
  26. #include "cairo-test.h"
  27. #define PAT_WIDTH 120
  28. #define PAT_HEIGHT 120
  29. #define SIZE (PAT_WIDTH*2)
  30. #define PAD 2
  31. #define WIDTH (PAD + SIZE + PAD)
  32. #define HEIGHT WIDTH
  33. /* This test is designed to test painting a recording surface pattern with
  34. * CAIRO_EXTEND_NONE and a non identity pattern matrix.
  35. */
  36. static cairo_pattern_t *create_pattern (cairo_t *target)
  37. {
  38. cairo_surface_t *surface;
  39. cairo_pattern_t *pattern;
  40. cairo_t *cr;
  41. surface = cairo_surface_create_similar (cairo_get_group_target (target),
  42. CAIRO_CONTENT_COLOR_ALPHA,
  43. PAT_WIDTH, PAT_HEIGHT);
  44. cr = cairo_create (surface);
  45. cairo_surface_destroy (surface);
  46. cairo_set_source_rgba (cr, 1, 0, 1, 0.5);
  47. cairo_rectangle (cr, PAT_WIDTH/6.0, PAT_HEIGHT/6.0, PAT_WIDTH/4.0, PAT_HEIGHT/4.0);
  48. cairo_fill (cr);
  49. cairo_set_source_rgba (cr, 0, 1, 1, 0.5);
  50. cairo_rectangle (cr, PAT_WIDTH/2.0, PAT_HEIGHT/2.0, PAT_WIDTH/4.0, PAT_HEIGHT/4.0);
  51. cairo_fill (cr);
  52. cairo_set_line_width (cr, 1);
  53. cairo_move_to (cr, PAT_WIDTH/6.0, 0);
  54. cairo_line_to (cr, 0, 0);
  55. cairo_line_to (cr, 0, PAT_HEIGHT/6.0);
  56. cairo_set_source_rgb (cr, 1, 0, 0);
  57. cairo_stroke (cr);
  58. cairo_move_to (cr, PAT_WIDTH/6.0, PAT_HEIGHT);
  59. cairo_line_to (cr, 0, PAT_HEIGHT);
  60. cairo_line_to (cr, 0, 5*PAT_HEIGHT/6.0);
  61. cairo_set_source_rgb (cr, 0, 1, 0);
  62. cairo_stroke (cr);
  63. cairo_move_to (cr, 5*PAT_WIDTH/6.0, 0);
  64. cairo_line_to (cr, PAT_WIDTH, 0);
  65. cairo_line_to (cr, PAT_WIDTH, PAT_HEIGHT/6.0);
  66. cairo_set_source_rgb (cr, 0, 0, 1);
  67. cairo_stroke (cr);
  68. cairo_move_to (cr, 5*PAT_WIDTH/6.0, PAT_HEIGHT);
  69. cairo_line_to (cr, PAT_WIDTH, PAT_HEIGHT);
  70. cairo_line_to (cr, PAT_WIDTH, 5*PAT_HEIGHT/6.0);
  71. cairo_set_source_rgb (cr, 1, 1, 0);
  72. cairo_stroke (cr);
  73. cairo_set_source_rgb (cr, 0.5, 0.5, 0.5);
  74. cairo_set_line_width (cr, PAT_WIDTH/10.0);
  75. cairo_move_to (cr, 0, PAT_HEIGHT/4.0);
  76. cairo_line_to (cr, PAT_WIDTH, PAT_HEIGHT/4.0);
  77. cairo_stroke (cr);
  78. cairo_move_to (cr, PAT_WIDTH/4.0, 0);
  79. cairo_line_to (cr, PAT_WIDTH/4.0, PAT_WIDTH);
  80. cairo_stroke (cr);
  81. pattern = cairo_pattern_create_for_surface (cairo_get_target (cr));
  82. cairo_destroy (cr);
  83. return pattern;
  84. }
  85. static cairo_test_status_t
  86. over (cairo_t *cr, int width, int height)
  87. {
  88. cairo_pattern_t *pattern;
  89. cairo_matrix_t mat;
  90. cairo_translate (cr, PAD, PAD);
  91. pattern = create_pattern (cr);
  92. cairo_matrix_init_identity (&mat);
  93. cairo_matrix_scale (&mat, 2, 1.5);
  94. cairo_matrix_rotate (&mat, 1);
  95. cairo_matrix_translate (&mat, -PAT_WIDTH/4.0, -PAT_WIDTH/2.0);
  96. cairo_pattern_set_matrix (pattern, &mat);
  97. cairo_pattern_set_extend (pattern, CAIRO_EXTEND_NONE);
  98. cairo_set_source (cr, pattern);
  99. cairo_paint (cr);
  100. cairo_pattern_destroy (pattern);
  101. return CAIRO_TEST_SUCCESS;
  102. }
  103. static cairo_test_status_t
  104. source (cairo_t *cr, int width, int height)
  105. {
  106. cairo_pattern_t *pattern;
  107. cairo_matrix_t mat;
  108. cairo_translate (cr, PAD, PAD);
  109. pattern = create_pattern (cr);
  110. cairo_matrix_init_identity (&mat);
  111. cairo_matrix_scale (&mat, 2, 1.5);
  112. cairo_matrix_rotate (&mat, 1);
  113. cairo_matrix_translate (&mat, -PAT_WIDTH/4.0, -PAT_WIDTH/2.0);
  114. cairo_pattern_set_matrix (pattern, &mat);
  115. cairo_pattern_set_extend (pattern, CAIRO_EXTEND_NONE);
  116. cairo_set_source (cr, pattern);
  117. cairo_set_operator (cr, CAIRO_OPERATOR_SOURCE);
  118. cairo_paint (cr);
  119. cairo_pattern_destroy (pattern);
  120. return CAIRO_TEST_SUCCESS;
  121. }
  122. CAIRO_TEST (recording_surface_over,
  123. "Paint recording surface pattern with non identity pattern matrix",
  124. "recording", /* keywords */
  125. NULL, /* requirements */
  126. WIDTH, HEIGHT,
  127. NULL, over)
  128. CAIRO_TEST (recording_surface_source,
  129. "Paint recording surface pattern with non identity pattern matrix",
  130. "recording", /* keywords */
  131. NULL, /* requirements */
  132. WIDTH, HEIGHT,
  133. NULL, source)