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.2, 0.3, 0.3, 0.3);
82
27
	cairo_pattern_add_color_stop_rgb (pattern, 0.8, 1.0, 1.0, 1.0);
83
    } else {
84
27
	cairo_pattern_add_color_stop_rgb (pattern, 0.2, 1.0, 0.0, 0.0);
85
27
	cairo_pattern_add_color_stop_rgb (pattern, 0.5, 1.0, 1.0, 1.0);
86
27
	cairo_pattern_add_color_stop_rgb (pattern, 0.8, 0.0, 0.0, 1.0);
87
    }
88

            
89
54
    cairo_set_source (cr, pattern);
90
54
    cairo_pattern_destroy (pattern);
91
54
    cairo_rectangle (cr, -0.5, -0.5, 1, 1);
92
54
    cairo_fill (cr);
93
54
}
94

            
95
static cairo_test_status_t
96
3
draw (cairo_t *cr, int width, int height)
97
{
98
    int i, j, k;
99

            
100
3
    cairo_set_source_rgb (cr, 0.5, 0.5, 0.5);
101
3
    cairo_paint (cr);
102

            
103
12
    for (i = 0; i < N_GRADIENT_ANGLES; i++)
104
36
	for (j = 0; j < N_ROTATE_ANGLES; j++)
105
81
	  for (k = 0; k < N_N_STOPS; k++) {
106
54
		cairo_save (cr);
107
54
		cairo_translate (cr,
108
54
				 PAD + (PAD + UNIT_SIZE) * i,
109
54
				 PAD + (PAD + UNIT_SIZE) * (N_ROTATE_ANGLES * k + j));
110
54
		cairo_scale (cr, UNIT_SIZE, UNIT_SIZE);
111

            
112
54
		draw_unit (cr,
113
54
			   gradient_angles[i] * M_PI / 180.,
114
54
			   rotate_angles[j] * M_PI / 180.,
115
54
			   n_stops[k]);
116
54
		cairo_restore (cr);
117
	    }
118

            
119
3
    return CAIRO_TEST_SUCCESS;
120
}
121

            
122
1
CAIRO_TEST (linear_gradient_subset,
123
	    "Tests the drawing of linear gradients",
124
	    "gradient", /* keywords */
125
	    NULL, /* requirements */
126
	    WIDTH, HEIGHT,
127
	    NULL, draw)