clip-complex-shape.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. /*
  2. * Copyright 2011 SCore Corporation
  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: Taekyun Kim <podain77@gmail.com>
  25. */
  26. #include "cairo-test.h"
  27. static void
  28. rounded_rectangle(cairo_t *cr,
  29. double x, double y,
  30. double width, double height,
  31. double radius)
  32. {
  33. cairo_move_to (cr, x, y + radius);
  34. cairo_line_to (cr, x, y + height - radius);
  35. cairo_curve_to (cr, x, y + height - radius/2.0,
  36. x + radius/2.0, y + height,
  37. x + radius, y + height);
  38. cairo_line_to (cr, x + width - radius, y + height);
  39. cairo_curve_to (cr, x + width - radius/2.0, y + height,
  40. x + width, y + height - radius/2.0,
  41. x + width, y + height - radius);
  42. cairo_line_to (cr, x + width, y + radius);
  43. cairo_curve_to (cr, x + width, y + radius/2.0,
  44. x + width - radius/2.0, y,
  45. x + width - radius, y);
  46. cairo_line_to (cr, x + radius, y);
  47. cairo_curve_to (cr, x + radius/2.0, y, x, y + radius/2.0, x, y + radius);
  48. cairo_close_path(cr);
  49. }
  50. static void
  51. background (cairo_t *cr)
  52. {
  53. cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
  54. cairo_set_source_rgba(cr, 1.0, 1.0, 1.0, 1.0);
  55. cairo_paint(cr);
  56. }
  57. static void
  58. foreground (cairo_t *cr)
  59. {
  60. cairo_set_source_rgba(cr, 1.0, 0.0, 0.0, 1.0);
  61. cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
  62. cairo_rectangle(cr, 20, 20, 60, 60);
  63. cairo_fill(cr);
  64. }
  65. static cairo_test_status_t
  66. clip_eo_mono (cairo_t *cr, int width, int height)
  67. {
  68. background (cr);
  69. cairo_set_antialias(cr, CAIRO_ANTIALIAS_NONE);
  70. cairo_set_fill_rule(cr, CAIRO_FILL_RULE_EVEN_ODD);
  71. rounded_rectangle(cr, 0, 0, 40, 100, 10);
  72. rounded_rectangle(cr, 60, 0, 40, 100, 10);
  73. cairo_clip(cr);
  74. foreground (cr);
  75. return CAIRO_TEST_SUCCESS;
  76. }
  77. static cairo_test_status_t
  78. clip_eo_aa (cairo_t *cr, int width, int height)
  79. {
  80. background (cr);
  81. cairo_set_antialias(cr, CAIRO_ANTIALIAS_DEFAULT);
  82. cairo_set_fill_rule(cr, CAIRO_FILL_RULE_EVEN_ODD);
  83. rounded_rectangle(cr, 0, 0, 40, 100, 10);
  84. rounded_rectangle(cr, 60, 0, 40, 100, 10);
  85. cairo_clip(cr);
  86. foreground (cr);
  87. return CAIRO_TEST_SUCCESS;
  88. }
  89. CAIRO_TEST (clip_complex_shape_eo_mono,
  90. "Test clipping against a complex shape",
  91. "clip", /* keywords */
  92. NULL, /* requirements */
  93. 100, 100,
  94. NULL, clip_eo_mono)
  95. CAIRO_TEST (clip_complex_shape_eo_aa,
  96. "Test clipping against a complex shape",
  97. "clip", /* keywords */
  98. NULL, /* requirements */
  99. 100, 100,
  100. NULL, clip_eo_aa)