surface-pattern-operator.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. /* -*- Mode: c; c-basic-offset: 4; indent-tabs-mode: t; tab-width: 8; -*- */
  2. /*
  3. * Copyright 2009 Andrea Canciani
  4. *
  5. * Permission is hereby granted, free of charge, to any person
  6. * obtaining a copy of this software and associated documentation
  7. * files (the "Software"), to deal in the Software without
  8. * restriction, including without limitation the rights to use, copy,
  9. * modify, merge, publish, distribute, sublicense, and/or sell copies
  10. * of the Software, and to permit persons to whom the Software is
  11. * furnished to do so, subject to the following conditions:
  12. *
  13. * The above copyright notice and this permission notice shall be
  14. * included in all copies or substantial portions of the Software.
  15. *
  16. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  17. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  18. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  19. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  20. * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  21. * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  22. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  23. * SOFTWARE.
  24. *
  25. * Author: Andrea Canciani <ranma42@gmail.com>
  26. */
  27. #include "cairo-test.h"
  28. #define N_OPERATORS (CAIRO_OPERATOR_SATURATE + 1)
  29. #define HEIGHT 16
  30. #define WIDTH 16
  31. #define PAD 3
  32. static cairo_pattern_t*
  33. _create_pattern (cairo_surface_t *target, cairo_content_t content, int width, int height)
  34. {
  35. cairo_pattern_t *pattern;
  36. cairo_surface_t *surface;
  37. cairo_t *cr;
  38. surface = cairo_surface_create_similar (target, content, width, height);
  39. cr = cairo_create (surface);
  40. cairo_surface_destroy (surface);
  41. cairo_set_source_rgb (cr, 1, 0, 0);
  42. cairo_arc (cr, 0.5 * width, 0.5 * height, 0.45 * height, -M_PI / 4, 3 * M_PI / 4);
  43. cairo_fill (cr);
  44. pattern = cairo_pattern_create_for_surface (cairo_get_target (cr));
  45. cairo_destroy (cr);
  46. return pattern;
  47. }
  48. static cairo_test_status_t
  49. draw (cairo_t *cr, int width, int height)
  50. {
  51. cairo_pattern_t *alpha_pattern, *color_alpha_pattern, *pattern;
  52. unsigned int n, i;
  53. alpha_pattern = _create_pattern (cairo_get_target (cr),
  54. CAIRO_CONTENT_ALPHA,
  55. 0.9 * WIDTH, 0.9 * HEIGHT);
  56. color_alpha_pattern = _create_pattern (cairo_get_target (cr),
  57. CAIRO_CONTENT_COLOR_ALPHA,
  58. 0.9 * WIDTH, 0.9 * HEIGHT);
  59. pattern = cairo_pattern_create_linear (WIDTH, 0, 0, HEIGHT);
  60. cairo_pattern_add_color_stop_rgba (pattern, 0.2, 0, 0, 1, 1);
  61. cairo_pattern_add_color_stop_rgba (pattern, 0.8, 0, 0, 1, 0);
  62. cairo_translate (cr, PAD, PAD);
  63. for (n = 0; n < N_OPERATORS; n++) {
  64. cairo_save (cr);
  65. for (i = 0; i < 4; i++) {
  66. cairo_reset_clip (cr);
  67. cairo_rectangle (cr, 0, 0, WIDTH, HEIGHT);
  68. cairo_clip (cr);
  69. cairo_set_source (cr, pattern);
  70. cairo_set_operator (cr, CAIRO_OPERATOR_OVER);
  71. if (i & 2) {
  72. cairo_paint (cr);
  73. } else {
  74. cairo_rectangle (cr, WIDTH/2, HEIGHT/2, WIDTH, HEIGHT);
  75. cairo_fill (cr);
  76. }
  77. cairo_set_source (cr, i & 1 ? alpha_pattern : color_alpha_pattern);
  78. cairo_set_operator (cr, n);
  79. if (i & 2) {
  80. cairo_paint (cr);
  81. } else {
  82. cairo_rectangle (cr, WIDTH/2, HEIGHT/2, WIDTH, HEIGHT);
  83. cairo_fill (cr);
  84. }
  85. cairo_translate (cr, 0, HEIGHT+PAD);
  86. }
  87. cairo_restore (cr);
  88. cairo_translate (cr, WIDTH+PAD, 0);
  89. }
  90. cairo_pattern_destroy (pattern);
  91. cairo_pattern_destroy (alpha_pattern);
  92. cairo_pattern_destroy (color_alpha_pattern);
  93. return CAIRO_TEST_SUCCESS;
  94. }
  95. CAIRO_TEST (surface_pattern_operator,
  96. "Tests alpha-only and alpha-color sources with all operators",
  97. "surface, pattern, operator", /* keywords */
  98. NULL, /* requirements */
  99. (WIDTH+PAD) * N_OPERATORS + PAD, 4*HEIGHT + 5*PAD,
  100. NULL, draw)