Back to index

lightning-sunbird  0.9+nobinonly
coverage.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: Kristian Høgsberg <krh@redhat.com>
00024  */
00025 
00026 #include <math.h>
00027 #include "cairo_test.h"
00028 
00029 #define WIDTH 64
00030 #define HEIGHT 64
00031 #define PAD 10
00032 
00033 /* XXX The test image uses Bitstream Vera Sans as the font - how do we
00034  * ensure that it's available?  Can we ship it with this test? */
00035 
00036 const char    fontname[]    = "Bitstream Vera Sans";
00037 const int     fontsize      = 40;
00038 const char    png_filename[]       = "romedalen.png";
00039 
00040 static void
00041 set_solid_pattern (cairo_t *cr, int x, int y)
00042 {
00043     cairo_set_rgb_color (cr, 0, 0, 0.6);
00044     cairo_set_alpha (cr, 1.0);
00045 }
00046 
00047 static void
00048 set_translucent_pattern (cairo_t *cr, int x, int y)
00049 {
00050     cairo_set_rgb_color (cr, 0, 0, 0.6);
00051     cairo_set_alpha (cr, 0.5);
00052 }
00053 
00054 static void
00055 set_gradient_pattern (cairo_t *cr, int x, int y)
00056 {
00057     cairo_pattern_t *pattern;
00058 
00059     pattern =
00060        cairo_pattern_create_linear (x, y, x + WIDTH, y + HEIGHT);
00061     cairo_pattern_add_color_stop (pattern, 0, 1, 1, 1, 1);
00062     cairo_pattern_add_color_stop (pattern, 1, 0, 0, 0.4, 1);
00063     cairo_set_pattern (cr, pattern);
00064     cairo_set_alpha (cr, 1);
00065 }
00066 
00067 static void
00068 set_image_pattern (cairo_t *cr, int x, int y)
00069 {
00070     cairo_pattern_t *pattern;
00071 
00072     pattern = cairo_test_create_png_pattern (cr, png_filename);
00073     cairo_set_pattern (cr, pattern);
00074     cairo_set_alpha (cr, 1);
00075 }
00076 
00077 static void
00078 set_translucent_image_pattern (cairo_t *cr, int x, int y)
00079 {
00080     cairo_pattern_t *pattern;
00081 
00082     pattern = cairo_test_create_png_pattern (cr, png_filename);
00083     cairo_set_pattern (cr, pattern);
00084     cairo_set_alpha (cr, 0.5);
00085 }
00086 
00087 static void
00088 draw_text (cairo_t *cr, int x, int y)
00089 {
00090     cairo_rel_move_to (cr, 0, fontsize);
00091     cairo_show_text (cr, "Aa");
00092 }
00093 
00094 static void
00095 draw_polygon (cairo_t *cr, int x, int y)
00096 {
00097     cairo_new_path (cr);
00098     cairo_move_to (cr, x, y);
00099     cairo_line_to (cr, x, y + HEIGHT);
00100     cairo_line_to (cr, x + WIDTH / 2, y + 3 * HEIGHT / 4);
00101     cairo_line_to (cr, x + WIDTH, y + HEIGHT);
00102     cairo_line_to (cr, x + WIDTH, y);
00103     cairo_line_to (cr, x + WIDTH / 2, y + HEIGHT / 4);
00104     cairo_close_path (cr);
00105     cairo_fill (cr);
00106 }
00107 
00108 static void (*pattern_funcs[])(cairo_t *cr, int x, int y) = {
00109     set_solid_pattern,
00110     set_translucent_pattern,
00111     set_gradient_pattern,
00112     set_image_pattern,
00113     set_translucent_image_pattern
00114 };
00115 
00116 static void (*draw_funcs[])(cairo_t *cr, int x, int y) = {
00117     draw_text,
00118     draw_polygon,
00119 };
00120 
00121 #define ARRAY_SIZE(a) (sizeof (a) / sizeof ((a)[0]))
00122 #define IMAGE_WIDTH (ARRAY_SIZE (pattern_funcs) * (WIDTH + PAD) + PAD)
00123 #define IMAGE_HEIGHT (ARRAY_SIZE (draw_funcs) * (HEIGHT + PAD) * 2 + PAD)
00124 
00125 static cairo_test_t test = {
00126     "coverage",
00127     "Various coverage test of cairo",
00128     IMAGE_WIDTH, IMAGE_HEIGHT
00129 };
00130 
00131 
00132 static void
00133 draw (cairo_t *cr, int width, int height)
00134 {
00135     /* TODO: pattern fill, gradient fill, clipping, gradient clipping,
00136        path+solid alpha mask clipping */
00137 
00138     int i, j, x, y;
00139 
00140     cairo_select_font (cr, fontname,
00141                      CAIRO_FONT_SLANT_NORMAL,
00142                      CAIRO_FONT_WEIGHT_BOLD);
00143     cairo_scale_font (cr, fontsize);
00144 
00145     for (j = 0; j < ARRAY_SIZE (draw_funcs); j++) {
00146        for (i = 0; i < ARRAY_SIZE (pattern_funcs); i++) {
00147            x = i * (WIDTH + PAD) + PAD;
00148            y = j * (HEIGHT + PAD) + PAD;
00149            cairo_move_to (cr, x, y);
00150            pattern_funcs[i] (cr, x, y);
00151            draw_funcs[j] (cr, x, y);
00152        }
00153     }
00154 
00155     for (j = 0; j < ARRAY_SIZE (draw_funcs); j++) {
00156        for (i = 0; i < ARRAY_SIZE (pattern_funcs); i++) {
00157            x = i * (WIDTH + PAD) + PAD;
00158            y = (ARRAY_SIZE (draw_funcs) + j) * (HEIGHT + PAD) + PAD;
00159 
00160            cairo_save (cr);
00161 
00162            cairo_set_alpha (cr, 1.0);
00163            cairo_new_path (cr);
00164            cairo_arc (cr, x + WIDTH / 2, y + HEIGHT / 2,
00165                      WIDTH / 3, 0, 2 * M_PI);
00166            cairo_clip (cr);
00167 
00168            cairo_move_to (cr, x, y);
00169            pattern_funcs[i] (cr, x, y);
00170            draw_funcs[j] (cr, x, y);
00171 
00172            cairo_restore (cr);
00173 
00174        }
00175     }
00176 }
00177 
00178 int
00179 main (void)
00180 {
00181     return cairo_test (&test, draw);
00182 }