push-group.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. /*
  2. * Copyright © 2005 Mozilla Corporation
  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. * Mozilla Corporation not be used in advertising or publicity pertaining to
  10. * distribution of the software without specific, written prior
  11. * permission. Mozilla Corporation 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. * MOZILLA CORPORATION DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS
  16. * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
  17. * FITNESS, IN NO EVENT SHALL MOZILLA CORPORATION 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. * Author: Vladimir Vukicevic <vladimir@pobox.com>
  24. */
  25. #include "cairo-test.h"
  26. #define UNIT_SIZE 100
  27. #define PAD 5
  28. #define INNER_PAD 10
  29. #define WIDTH (UNIT_SIZE + PAD) + PAD
  30. #define HEIGHT (UNIT_SIZE + PAD) + PAD
  31. static cairo_test_status_t
  32. draw (cairo_t *cr, int width, int height)
  33. {
  34. cairo_pattern_t *gradient;
  35. int i, j;
  36. gradient = cairo_pattern_create_linear (UNIT_SIZE - (INNER_PAD*2), 0,
  37. UNIT_SIZE - (INNER_PAD*2), UNIT_SIZE - (INNER_PAD*2));
  38. cairo_pattern_add_color_stop_rgba (gradient, 0.0, 0.3, 0.3, 0.3, 1.0);
  39. cairo_pattern_add_color_stop_rgba (gradient, 1.0, 1.0, 1.0, 1.0, 1.0);
  40. for (j = 0; j < 1; j++) {
  41. for (i = 0; i < 1; i++) {
  42. double x = (i * UNIT_SIZE) + (i + 1) * PAD;
  43. double y = (j * UNIT_SIZE) + (j + 1) * PAD;
  44. cairo_save (cr);
  45. cairo_translate (cr, x, y);
  46. /* draw a gradient background */
  47. cairo_save (cr);
  48. cairo_translate (cr, INNER_PAD, INNER_PAD);
  49. cairo_new_path (cr);
  50. cairo_rectangle (cr, 0, 0,
  51. UNIT_SIZE - (INNER_PAD*2), UNIT_SIZE - (INNER_PAD*2));
  52. cairo_set_source (cr, gradient);
  53. cairo_fill (cr);
  54. cairo_restore (cr);
  55. /* clip to the unit size */
  56. cairo_rectangle (cr, 0, 0,
  57. UNIT_SIZE, UNIT_SIZE);
  58. cairo_clip (cr);
  59. cairo_rectangle (cr, 0, 0,
  60. UNIT_SIZE, UNIT_SIZE);
  61. cairo_set_source_rgba (cr, 0, 0, 0, 1);
  62. cairo_set_line_width (cr, 2);
  63. cairo_stroke (cr);
  64. /* start a group */
  65. cairo_push_group (cr);
  66. /* draw diamond */
  67. cairo_move_to (cr, UNIT_SIZE / 2, 0);
  68. cairo_line_to (cr, UNIT_SIZE , UNIT_SIZE / 2);
  69. cairo_line_to (cr, UNIT_SIZE / 2, UNIT_SIZE);
  70. cairo_line_to (cr, 0 , UNIT_SIZE / 2);
  71. cairo_close_path (cr);
  72. cairo_set_source_rgba (cr, 0, 0, 1, 1);
  73. cairo_fill (cr);
  74. /* draw circle */
  75. cairo_arc (cr,
  76. UNIT_SIZE / 2, UNIT_SIZE / 2,
  77. UNIT_SIZE / 3.5,
  78. 0, M_PI * 2);
  79. cairo_set_source_rgba (cr, 1, 0, 0, 1);
  80. cairo_fill (cr);
  81. cairo_pop_group_to_source (cr);
  82. cairo_paint_with_alpha (cr, 0.5);
  83. cairo_restore (cr);
  84. }
  85. }
  86. cairo_pattern_destroy (gradient);
  87. return CAIRO_TEST_SUCCESS;
  88. }
  89. CAIRO_TEST (push_group,
  90. "Verify that cairo_push_group works.",
  91. "group", /* keywords */
  92. NULL, /* requirements */
  93. WIDTH, HEIGHT,
  94. NULL, draw)