Back to index

lightning-sunbird  0.9+nobinonly
linear_gradient.c
Go to the documentation of this file.
00001 /*
00002  * Copyright © 2005 Red Hat, Inc.
00003  *
00004  * Permission to use, copy, modify, distribute, and sell this software
00005  * and its documentation for any purpose is hereby granted without
00006  * fee, provided that the above copyright notice appear in all copies
00007  * and that both that copyright notice and this permission notice
00008  * appear in supporting documentation, and that the name of
00009  * Red Hat, Inc. not be used in advertising or publicity pertaining to
00010  * distribution of the software without specific, written prior
00011  * permission. Red Hat, Inc. makes no representations about the
00012  * suitability of this software for any purpose.  It is provided "as
00013  * is" without express or implied warranty.
00014  *
00015  * RED HAT, INC. DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS
00016  * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
00017  * FITNESS, IN NO EVENT SHALL RED HAT, INC. BE LIABLE FOR ANY SPECIAL,
00018  * INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
00019  * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
00020  * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR
00021  * IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
00022  *
00023  * Author: Owen Taylor <otaylor@redhat.com>
00024  */
00025 
00026 #include "cairo_test.h"
00027 #include "stdio.h"
00028 
00029 /* The test matrix is
00030  *
00031  * A) Horizontal   B) 5°          C) 45°          D) Vertical
00032  * 1) Rotated 0°   2) Rotated 45° C) Rotated 90°
00033  * a) 2 stop       b) 3 stop
00034  *
00035  *  A1a   B1a  C1a  D1a
00036  *  A2a   B2a  C2a  D2a
00037  *  A3a   B3a  C3a  D3a
00038  *  A1b   B1b  C1b  D1b
00039  *  A2b   B2b  C2b  D2b
00040  *  A3b   B3b  C3b  D3b
00041  */
00042 
00043 static const double gradient_angles[] = { 0, 45, 90 };
00044 #define N_GRADIENT_ANGLES 3
00045 static const double rotate_angles[] = { 0, 45, 90 };
00046 #define N_ROTATE_ANGLES 3
00047 static const int n_stops[] = { 2, 3 };
00048 #define N_N_STOPS 2
00049 
00050 #define UNIT_SIZE 75
00051 #define UNIT_SIZE 75
00052 #define PAD 5
00053 
00054 #define WIDTH  N_GRADIENT_ANGLES * UNIT_SIZE + (N_GRADIENT_ANGLES + 1) * PAD
00055 #define HEIGHT N_N_STOPS * N_ROTATE_ANGLES * UNIT_SIZE + (N_N_STOPS * N_ROTATE_ANGLES + 1) * PAD
00056 
00057 cairo_test_t test = {
00058     "linear_gradient",
00059     "Tests the drawing of linear gradients",
00060     WIDTH, HEIGHT
00061 };
00062 
00063 static void
00064 draw_unit (cairo_t *cr,
00065           double   gradient_angle,
00066           double   rotate_angle,
00067           int      n_stops)
00068 {
00069     cairo_pattern_t *pattern;
00070 
00071     cairo_rectangle (cr, 0, 0, 1, 1);
00072     cairo_clip (cr);
00073     cairo_new_path(cr);
00074     
00075     cairo_set_rgb_color (cr, 0.0, 0.0, 0.0);
00076     cairo_rectangle (cr, 0, 0, 1, 1);
00077     cairo_fill (cr);
00078     
00079     cairo_translate (cr, 0.5, 0.5);
00080     cairo_scale (cr, 1 / 1.5, 1 / 1.5);
00081     cairo_rotate (cr, rotate_angle);
00082     
00083     pattern = cairo_pattern_create_linear (-0.5 * cos (gradient_angle),  -0.5 * sin (gradient_angle),
00084                                        0.5 * cos (gradient_angle),   0.5 * sin (gradient_angle));
00085 
00086     if (n_stops == 2) {
00087        cairo_pattern_add_color_stop (pattern, 0.,
00088                                   0.3, 0.3, 0.3,
00089                                   1.0);
00090        cairo_pattern_add_color_stop (pattern, 1.,
00091                                   1.0, 1.0, 1.0,
00092                                   1.0);
00093     } else {
00094        cairo_pattern_add_color_stop (pattern, 0.,
00095                                   1.0, 0.0, 0.0,
00096                                   1.0);
00097        cairo_pattern_add_color_stop (pattern, 0.5,
00098                                   1.0, 1.0, 1.0,
00099                                   1.0);
00100        cairo_pattern_add_color_stop (pattern, 1.,
00101                                   0.0, 0.0, 1.0,
00102                                   1.0);
00103     }
00104 
00105     cairo_set_pattern (cr, pattern);
00106     cairo_pattern_destroy (pattern);
00107     cairo_rectangle (cr, -0.5, -0.5, 1, 1);
00108     cairo_fill (cr);
00109 }
00110 
00111 static void
00112 draw (cairo_t *cr, int width, int height)
00113 {
00114     int i, j, k;
00115 
00116     cairo_set_rgb_color (cr, 0.5, 0.5, 0.5);
00117     cairo_rectangle (cr, 0, 0, width, height);
00118     cairo_fill (cr);
00119 
00120     for (i = 0; i < N_GRADIENT_ANGLES; i++)
00121        for (j = 0; j < N_ROTATE_ANGLES; j++)
00122          for (k = 0; k < N_N_STOPS; k++) {
00123               cairo_save (cr);
00124               cairo_translate (cr,
00125                              PAD + (PAD + UNIT_SIZE) * i,
00126                              PAD + (PAD + UNIT_SIZE) * (N_ROTATE_ANGLES * k + j));
00127               cairo_scale (cr, UNIT_SIZE, UNIT_SIZE);
00128               
00129               draw_unit (cr,
00130                         gradient_angles[i] * M_PI / 180.,
00131                         rotate_angles[j] * M_PI / 180.,
00132                         n_stops[k]);
00133               cairo_restore (cr);
00134            }
00135 }
00136 
00137 int
00138 main (void)
00139 {
00140     return cairo_test (&test, draw);
00141 }