linear-gradient.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. /*
  2. * Copyright © 2005 Red Hat, Inc.
  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. * Red Hat, Inc. not be used in advertising or publicity pertaining to
  10. * distribution of the software without specific, written prior
  11. * permission. Red Hat, Inc. 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. * RED HAT, INC. DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS
  16. * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
  17. * FITNESS, IN NO EVENT SHALL RED HAT, INC. 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: Owen Taylor <otaylor@redhat.com>
  24. */
  25. #include "cairo-test.h"
  26. #include "stdio.h"
  27. /* The test matrix is
  28. *
  29. * A) Horizontal B) 5° C) 45° D) Vertical
  30. * 1) Rotated 0° 2) Rotated 45° C) Rotated 90°
  31. * a) 2 stop b) 3 stop
  32. *
  33. * A1a B1a C1a D1a
  34. * A2a B2a C2a D2a
  35. * A3a B3a C3a D3a
  36. * A1b B1b C1b D1b
  37. * A2b B2b C2b D2b
  38. * A3b B3b C3b D3b
  39. */
  40. static const double gradient_angles[] = { 0, 45, 90 };
  41. #define N_GRADIENT_ANGLES 3
  42. static const double rotate_angles[] = { 0, 45, 90 };
  43. #define N_ROTATE_ANGLES 3
  44. static const int n_stops[] = { 2, 3 };
  45. #define N_N_STOPS 2
  46. #define UNIT_SIZE 6
  47. #define UNIT_SIZE 6
  48. #define PAD 1
  49. #define WIDTH N_GRADIENT_ANGLES * UNIT_SIZE + (N_GRADIENT_ANGLES + 1) * PAD
  50. #define HEIGHT N_N_STOPS * N_ROTATE_ANGLES * UNIT_SIZE + (N_N_STOPS * N_ROTATE_ANGLES + 1) * PAD
  51. static void
  52. draw_unit (cairo_t *cr,
  53. double gradient_angle,
  54. double rotate_angle,
  55. int n_stops)
  56. {
  57. cairo_pattern_t *pattern;
  58. cairo_rectangle (cr, 0, 0, 1, 1);
  59. cairo_clip (cr);
  60. cairo_new_path(cr);
  61. cairo_set_source_rgb (cr, 0.0, 0.0, 0.0);
  62. cairo_rectangle (cr, 0, 0, 1, 1);
  63. cairo_fill (cr);
  64. cairo_translate (cr, 0.5, 0.5);
  65. cairo_scale (cr, 1 / 1.5, 1 / 1.5);
  66. cairo_rotate (cr, rotate_angle);
  67. pattern = cairo_pattern_create_linear (-0.5 * cos (gradient_angle), -0.5 * sin (gradient_angle),
  68. 0.5 * cos (gradient_angle), 0.5 * sin (gradient_angle));
  69. if (n_stops == 2) {
  70. cairo_pattern_add_color_stop_rgb (pattern, 0.,
  71. 0.3, 0.3, 0.3);
  72. cairo_pattern_add_color_stop_rgb (pattern, 1.,
  73. 1.0, 1.0, 1.0);
  74. } else {
  75. cairo_pattern_add_color_stop_rgb (pattern, 0.,
  76. 1.0, 0.0, 0.0);
  77. cairo_pattern_add_color_stop_rgb (pattern, 0.5,
  78. 1.0, 1.0, 1.0);
  79. cairo_pattern_add_color_stop_rgb (pattern, 1.,
  80. 0.0, 0.0, 1.0);
  81. }
  82. cairo_set_source (cr, pattern);
  83. cairo_pattern_destroy (pattern);
  84. cairo_rectangle (cr, -0.5, -0.5, 1, 1);
  85. cairo_fill (cr);
  86. }
  87. static cairo_test_status_t
  88. draw (cairo_t *cr, int width, int height)
  89. {
  90. int i, j, k;
  91. cairo_set_source_rgb (cr, 0.5, 0.5, 0.5);
  92. cairo_paint (cr);
  93. for (i = 0; i < N_GRADIENT_ANGLES; i++)
  94. for (j = 0; j < N_ROTATE_ANGLES; j++)
  95. for (k = 0; k < N_N_STOPS; k++) {
  96. cairo_save (cr);
  97. cairo_translate (cr,
  98. PAD + (PAD + UNIT_SIZE) * i,
  99. PAD + (PAD + UNIT_SIZE) * (N_ROTATE_ANGLES * k + j));
  100. cairo_scale (cr, UNIT_SIZE, UNIT_SIZE);
  101. draw_unit (cr,
  102. gradient_angles[i] * M_PI / 180.,
  103. rotate_angles[j] * M_PI / 180.,
  104. n_stops[k]);
  105. cairo_restore (cr);
  106. }
  107. return CAIRO_TEST_SUCCESS;
  108. }
  109. CAIRO_TEST (linear_gradient,
  110. "Tests the drawing of linear gradients",
  111. "gradient", /* keywords */
  112. NULL, /* requirements */
  113. WIDTH, HEIGHT,
  114. NULL, draw)