1
/*
2
 * Copyright 2009 Chris Wilson
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
 * 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.
14
 *
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.
22
 *
23
 * Author: Chris Wilson <chris@chris-wilson.co.uk>
24
 */
25

            
26
#include "cairo-test.h"
27

            
28
#include <assert.h>
29

            
30
static const char *png_filename = "romedalen.png";
31

            
32
#define WIDTH 800
33
#define HEIGHT 600
34

            
35
static cairo_status_t
36
_image_to_glyphs (cairo_surface_t *image,
37
		  int channel,
38
		  int level,
39
		  cairo_scaled_font_t *scaled_font,
40
		  double tx, double ty,
41
		  cairo_glyph_t *glyphs,
42
		  int *num_glyphs)
43
{
44
    int width, height, stride;
45
    const unsigned char *data;
46
    int x, y, z, n;
47

            
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);
52

            
53
    n = 0;
54
    for (y = 0; y < height; y++) {
55
	const uint32_t *row = (uint32_t *) (data + y * stride);
56

            
57
	for (x = 0; x < width; x++) {
58
	    z = (row[x] >> channel) & 0xff;
59
	    if (z == level) {
60
		double xx, yy, zz;
61
		char c = n % 26 + 'a';
62
		int count = 1;
63
		cairo_glyph_t *glyphs_p = &glyphs[n];
64
		cairo_status_t status;
65

            
66
		xx = 4 * (x - width/2.) + width/2.;
67
		yy = 4 * (y - height/2.) + height/2.;
68

            
69
		zz = z / 1000.;
70
		xx = xx + zz*(width/2. - xx);
71
		yy = yy + zz*(height/2. - yy);
72

            
73
		cairo_scaled_font_text_to_glyphs (scaled_font,
74
						  tx + xx, ty + yy,
75
						  &c, 1,
76
						  &glyphs_p, &count,
77
						  NULL, NULL,
78
						  NULL);
79
		status = cairo_scaled_font_status (scaled_font);
80
		if (status)
81
		    return status;
82

            
83
		assert (glyphs_p == &glyphs[n]);
84
		assert (count == 1);
85
		n++;
86
	    }
87
	}
88
    }
89

            
90
    *num_glyphs = n;
91
    return CAIRO_STATUS_SUCCESS;
92
}
93

            
94
static cairo_status_t
95
_render_image (cairo_t *cr,
96
	       int width, int height,
97
	       cairo_surface_t *image)
98
{
99
    int ww, hh;
100
    cairo_glyph_t *glyphs;
101
    cairo_pattern_t *mask;
102
    cairo_scaled_font_t *scaled_font;
103
    double tx, ty;
104
    const struct {
105
	int shift;
106
	double red;
107
	double green;
108
	double blue;
109
    } channel[3] = {
110
	{  0, 0.9, 0.3, 0.4 },
111
	{  8, 0.4, 0.9, 0.3 },
112
	{ 16, 0.3, 0.4, 0.9 },
113
    };
114
    unsigned int n, i;
115

            
116
    ww = cairo_image_surface_get_width (image);
117
    hh = cairo_image_surface_get_height (image);
118

            
119
    glyphs = cairo_glyph_allocate (ww * hh);
120
    if (glyphs == NULL)
121
	return CAIRO_STATUS_NO_MEMORY;
122

            
123
    tx = (width - ww) / 2.;
124
    ty = (height - hh) / 2.;
125

            
126
    cairo_set_font_size (cr, 5);
127
    scaled_font = cairo_get_scaled_font (cr);
128

            
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;
133
	    int num_glyphs;
134

            
135
	    status = _image_to_glyphs (image, channel[i].shift, n,
136
				       scaled_font,
137
				       tx, ty, glyphs, &num_glyphs);
138
	    if (status) {
139
		cairo_glyph_free (glyphs);
140
		return status;
141
	    }
142

            
143
	    cairo_set_source_rgba (cr,
144
				   0, 0, 0,
145
				   .15 + .85 * n / 255.);
146
	    cairo_show_glyphs (cr, glyphs, num_glyphs);
147
	}
148
	mask = cairo_pop_group (cr);
149
	cairo_set_source_rgb (cr,
150
			      channel[i].red,
151
			      channel[i].green,
152
			      channel[i].blue);
153
	cairo_mask (cr, mask);
154
	cairo_pattern_destroy (mask);
155
    }
156

            
157
    cairo_glyph_free (glyphs);
158
    return CAIRO_STATUS_SUCCESS;
159
}
160

            
161
static cairo_test_status_t
162
draw (cairo_t *cr, int width, int height)
163
{
164
    const cairo_test_context_t *ctx = cairo_test_get_context (cr);
165
    cairo_surface_t *image;
166
    cairo_status_t status;
167

            
168
    cairo_set_source_rgb (cr, 0, 0, 0);
169
    cairo_paint (cr);
170

            
171
    image = cairo_test_create_surface_from_png (ctx, png_filename);
172
    status = cairo_surface_status (image);
173
    if (status)
174
	return cairo_test_status_from_status (ctx, status);
175

            
176
    status = _render_image (cr, width, height, image);
177
    cairo_surface_destroy (image);
178

            
179
    return cairo_test_status_from_status (ctx, status);
180
}
181

            
182
1
CAIRO_TEST (mask_glyphs,
183
	    "Creates a mask using a distorted array of overlapping glyphs",
184
	    "mask, glyphs", /* keywords */
185
	    "slow", /* requirements */
186
	    WIDTH, HEIGHT,
187
	    NULL, draw)