2 * Copyright 2009 Chris Wilson
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 * Chris Wilson not be used in advertising or publicity pertaining to
10 * distribution of the software without specific, written prior
11 * permission. Chris Wilson makes no representations about the
12 * suitability of this software for any purpose. It is provided "as
13 * is" without express or implied warranty.
15 * CHRIS WILSON DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS
16 * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
17 * FITNESS, IN NO EVENT SHALL CHRIS WILSON 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.
23 * Author: Chris Wilson <chris@chris-wilson.co.uk>
26 #include "cairo-test.h"
30 static const char *png_filename = "romedalen.png";
36 _image_to_glyphs (cairo_surface_t *image,
39 cairo_scaled_font_t *scaled_font,
41 cairo_glyph_t *glyphs,
44 int width, height, stride;
45 const unsigned char *data;
48 width = cairo_image_surface_get_width (image);
49 height = cairo_image_surface_get_height (image);
50 stride = cairo_image_surface_get_stride (image);
51 data = cairo_image_surface_get_data (image);
54 for (y = 0; y < height; y++) {
55 const uint32_t *row = (uint32_t *) (data + y * stride);
57 for (x = 0; x < width; x++) {
58 z = (row[x] >> channel) & 0xff;
61 char c = n % 26 + 'a';
63 cairo_glyph_t *glyphs_p = &glyphs[n];
64 cairo_status_t status;
66 xx = 4 * (x - width/2.) + width/2.;
67 yy = 4 * (y - height/2.) + height/2.;
70 xx = xx + zz*(width/2. - xx);
71 yy = yy + zz*(height/2. - yy);
73 cairo_scaled_font_text_to_glyphs (scaled_font,
79 status = cairo_scaled_font_status (scaled_font);
83 assert (glyphs_p == &glyphs[n]);
91 return CAIRO_STATUS_SUCCESS;
95 _render_image (cairo_t *cr,
96 int width, int height,
97 cairo_surface_t *image)
100 cairo_glyph_t *glyphs;
101 cairo_pattern_t *mask;
102 cairo_scaled_font_t *scaled_font;
110 { 0, 0.9, 0.3, 0.4 },
111 { 8, 0.4, 0.9, 0.3 },
112 { 16, 0.3, 0.4, 0.9 },
116 ww = cairo_image_surface_get_width (image);
117 hh = cairo_image_surface_get_height (image);
119 glyphs = cairo_glyph_allocate (ww * hh);
121 return CAIRO_STATUS_NO_MEMORY;
123 tx = (width - ww) / 2.;
124 ty = (height - hh) / 2.;
126 cairo_set_font_size (cr, 5);
127 scaled_font = cairo_get_scaled_font (cr);
129 for (i = 0; i < ARRAY_LENGTH (channel); i++) {
130 cairo_push_group_with_content (cr, CAIRO_CONTENT_ALPHA);
131 for (n = 0; n < 256; n++) {
132 cairo_status_t status;
135 status = _image_to_glyphs (image, channel[i].shift, n,
137 tx, ty, glyphs, &num_glyphs);
139 cairo_glyph_free (glyphs);
143 cairo_set_source_rgba (cr,
145 .15 + .85 * n / 255.);
146 cairo_show_glyphs (cr, glyphs, num_glyphs);
148 mask = cairo_pop_group (cr);
149 cairo_set_source_rgb (cr,
153 cairo_mask (cr, mask);
154 cairo_pattern_destroy (mask);
157 cairo_glyph_free (glyphs);
158 return CAIRO_STATUS_SUCCESS;
161 static cairo_test_status_t
162 draw (cairo_t *cr, int width, int height)
164 const cairo_test_context_t *ctx = cairo_test_get_context (cr);
165 cairo_surface_t *image;
166 cairo_status_t status;
168 cairo_set_source_rgb (cr, 0, 0, 0);
171 image = cairo_test_create_surface_from_png (ctx, png_filename);
172 status = cairo_surface_status (image);
174 return cairo_test_status_from_status (ctx, status);
176 status = _render_image (cr, width, height, image);
177 cairo_surface_destroy (image);
179 return cairo_test_status_from_status (ctx, status);
182 CAIRO_TEST (mask_glyphs,
183 "Creates a mask using a distorted array of overlapping glyphs",
184 "mask, glyphs", /* keywords */
185 "slow", /* requirements */