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

            
26
#include "cairo-test.h"
27
#include "stdio.h"
28

            
29
/* The test matrix is
30
 *
31
 * A) Horizontal   B) 5°          C) 45°          D) Vertical
32
 * 1) Rotated 0°   2) Rotated 45° C) Rotated 90°
33
 * a) 2 stop       b) 3 stop
34
 *
35
 *  A1a   B1a  C1a  D1a
36
 *  A2a   B2a  C2a  D2a
37
 *  A3a   B3a  C3a  D3a
38
 *  A1b   B1b  C1b  D1b
39
 *  A2b   B2b  C2b  D2b
40
 *  A3b   B3b  C3b  D3b
41
 */
42

            
43
static const double gradient_angles[] = { 0, 45, 90 };
44
#define N_GRADIENT_ANGLES 3
45
static const double rotate_angles[] = { 0, 45, 90 };
46
#define N_ROTATE_ANGLES 3
47
static const int n_stops[] = { 2, 3 };
48
#define N_N_STOPS 2
49

            
50
#define UNIT_SIZE 6
51
#define UNIT_SIZE 6
52
#define PAD 1
53

            
54
#define WIDTH  N_GRADIENT_ANGLES * UNIT_SIZE + (N_GRADIENT_ANGLES + 1) * PAD
55
#define HEIGHT N_N_STOPS * N_ROTATE_ANGLES * UNIT_SIZE + (N_N_STOPS * N_ROTATE_ANGLES + 1) * PAD
56

            
57
static void
58
54
draw_unit (cairo_t *cr,
59
	   double   gradient_angle,
60
	   double   rotate_angle,
61
	   int      n_stops)
62
{
63
    cairo_pattern_t *pattern;
64

            
65
54
    cairo_rectangle (cr, 0, 0, 1, 1);
66
54
    cairo_clip (cr);
67
54
    cairo_new_path(cr);
68

            
69
54
    cairo_set_source_rgb (cr, 0.0, 0.0, 0.0);
70
54
    cairo_rectangle (cr, 0, 0, 1, 1);
71
54
    cairo_fill (cr);
72

            
73
54
    cairo_translate (cr, 0.5, 0.5);
74
54
    cairo_scale (cr, 1 / 1.5, 1 / 1.5);
75
54
    cairo_rotate (cr, rotate_angle);
76

            
77
54
    pattern = cairo_pattern_create_linear (-0.5 * cos (gradient_angle),  -0.5 * sin (gradient_angle),
78
54
					    0.5 * cos (gradient_angle),   0.5 * sin (gradient_angle));
79

            
80
54
    if (n_stops == 2) {
81
27
	cairo_pattern_add_color_stop_rgb (pattern, 0.,
82
					  0.3, 0.3, 0.3);
83
27
	cairo_pattern_add_color_stop_rgb (pattern, 1.,
84
					  1.0, 1.0, 1.0);
85
    } else {
86
27
	cairo_pattern_add_color_stop_rgb (pattern, 0.,
87
					  1.0, 0.0, 0.0);
88
27
	cairo_pattern_add_color_stop_rgb (pattern, 0.5,
89
					  1.0, 1.0, 1.0);
90
27
	cairo_pattern_add_color_stop_rgb (pattern, 1.,
91
					  0.0, 0.0, 1.0);
92
    }
93

            
94
54
    cairo_set_source (cr, pattern);
95
54
    cairo_pattern_destroy (pattern);
96
54
    cairo_rectangle (cr, -0.5, -0.5, 1, 1);
97
54
    cairo_fill (cr);
98
54
}
99

            
100
static cairo_test_status_t
101
3
draw (cairo_t *cr, int width, int height)
102
{
103
    int i, j, k;
104

            
105
3
    cairo_set_source_rgb (cr, 0.5, 0.5, 0.5);
106
3
    cairo_paint (cr);
107

            
108
12
    for (i = 0; i < N_GRADIENT_ANGLES; i++)
109
36
	for (j = 0; j < N_ROTATE_ANGLES; j++)
110
81
	  for (k = 0; k < N_N_STOPS; k++) {
111
54
		cairo_save (cr);
112
54
		cairo_translate (cr,
113
54
				 PAD + (PAD + UNIT_SIZE) * i,
114
54
				 PAD + (PAD + UNIT_SIZE) * (N_ROTATE_ANGLES * k + j));
115
54
		cairo_scale (cr, UNIT_SIZE, UNIT_SIZE);
116

            
117
54
		draw_unit (cr,
118
54
			   gradient_angles[i] * M_PI / 180.,
119
54
			   rotate_angles[j] * M_PI / 180.,
120
54
			   n_stops[k]);
121
54
		cairo_restore (cr);
122
	    }
123

            
124
3
    return CAIRO_TEST_SUCCESS;
125
}
126

            
127
1
CAIRO_TEST (linear_gradient,
128
	    "Tests the drawing of linear gradients",
129
	    "gradient", /* keywords */
130
	    NULL, /* requirements */
131
	    WIDTH, HEIGHT,
132
	    NULL, draw)