8138884e8e9177eb6d14c9f01ddeb65ffcb4d4e8
[framework/graphics/cairo.git] / test / mesh-pattern-conical.c
1 /*
2  * Copyright © 2009 Adrian Johnson
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: Adrian Johnson <ajohnson@redneon.com>
25  */
26
27 #include "cairo-test.h"
28 #include <math.h>
29
30 #define PAT_WIDTH  100
31 #define PAT_HEIGHT 100
32 #define SIZE PAT_WIDTH
33 #define PAD 2
34 #define WIDTH (PAD + SIZE + PAD)
35 #define HEIGHT WIDTH
36
37
38 /*
39  * This test is designed to paint a mesh pattern which contains 8
40  * circular sectors approximating a conical gradient.
41  */
42
43 #define CENTER_X 50
44 #define CENTER_Y 50
45 #define RADIUS   50
46
47 static void
48 sector_patch (cairo_pattern_t *pattern,
49               double angle_A,
50               double A_r, double A_g, double A_b,
51               double angle_B,
52               double B_r, double B_g, double B_b)
53 {
54     double r_sin_A, r_cos_A;
55     double r_sin_B, r_cos_B;
56     double h;
57
58     r_sin_A = RADIUS * sin (angle_A);
59     r_cos_A = RADIUS * cos (angle_A);
60     r_sin_B = RADIUS * sin (angle_B);
61     r_cos_B = RADIUS * cos (angle_B);
62
63     h = 4.0/3.0 * tan ((angle_B - angle_A) / 4.0);
64
65     cairo_mesh_pattern_begin_patch (pattern);
66
67     cairo_mesh_pattern_move_to (pattern, CENTER_X, CENTER_Y);
68     cairo_mesh_pattern_line_to (pattern,
69                                 CENTER_X + r_cos_A,
70                                 CENTER_Y + r_sin_A);
71
72     cairo_mesh_pattern_curve_to (pattern,
73                                  CENTER_X + r_cos_A - h * r_sin_A,
74                                  CENTER_Y + r_sin_A + h * r_cos_A,
75                                  CENTER_X + r_cos_B + h * r_sin_B,
76                                  CENTER_Y + r_sin_B - h * r_cos_B,
77                                  CENTER_X + r_cos_B,
78                                  CENTER_Y + r_sin_B);
79
80     cairo_mesh_pattern_set_corner_color_rgb (pattern, 0, 1, 1, 1);
81     cairo_mesh_pattern_set_corner_color_rgb (pattern, 1, A_r, A_g, A_b);
82     cairo_mesh_pattern_set_corner_color_rgb (pattern, 2, B_r, B_g, B_b);
83
84     cairo_mesh_pattern_end_patch (pattern);
85 }
86
87 static cairo_test_status_t
88 draw (cairo_t *cr, int width, int height)
89 {
90     cairo_pattern_t *pattern;
91
92     cairo_set_source_rgb (cr, 1, 1, 1);
93     cairo_paint (cr);
94
95     cairo_translate (cr, PAD, PAD);
96
97     pattern = cairo_pattern_create_mesh ();
98     sector_patch (pattern,
99                   0,         1, 0, 0,
100                   M_PI/4,    1, 1, 0);
101     sector_patch (pattern,
102                   M_PI/4,    0, 1, 0,
103                   M_PI/2,    0, 1, 1);
104     sector_patch (pattern,
105                   M_PI/2,    0, 0, 1,
106                   3*M_PI/4,  1, 0, 1);
107     sector_patch (pattern,
108                   3*M_PI/4,  1, 0, 0,
109                   M_PI,      1, 1, 0);
110     sector_patch (pattern,
111                   -M_PI,     1, 1, 0,
112                   -3*M_PI/4, 0, 1, 0);
113     sector_patch (pattern,
114                   -3*M_PI/4, 0, 1, 0,
115                   -M_PI/2,   0, 1, 1);
116     sector_patch (pattern,
117                   -M_PI/2,   0, 1, 1,
118                   -M_PI/4,   0, 0, 1);
119     sector_patch (pattern,
120                   -M_PI/4,   0, 0, 1,
121                   0,         1, 0, 0);
122
123     cairo_set_source (cr, pattern);
124     cairo_paint (cr);
125     cairo_pattern_destroy (pattern);
126
127     return CAIRO_TEST_SUCCESS;
128 }
129
130 CAIRO_TEST (mesh_pattern_conical,
131             "Paint a conical pattern using a mesh pattern",
132             "conical,mesh,pattern", /* keywords */
133             NULL, /* requirements */
134             WIDTH, HEIGHT,
135             NULL, draw)