summaryrefslogtreecommitdiff
path: root/test/mask-glyphs.c
blob: 213722d0977995185dddb2ea12b9deabbfc82a6e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
/*
 * Copyright 2009 Chris Wilson
 *
 * Permission to use, copy, modify, distribute, and sell this software
 * and its documentation for any purpose is hereby granted without
 * fee, provided that the above copyright notice appear in all copies
 * and that both that copyright notice and this permission notice
 * appear in supporting documentation, and that the name of
 * Chris Wilson not be used in advertising or publicity pertaining to
 * distribution of the software without specific, written prior
 * permission. Chris Wilson makes no representations about the
 * suitability of this software for any purpose.  It is provided "as
 * is" without express or implied warranty.
 *
 * CHRIS WILSON DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS
 * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
 * FITNESS, IN NO EVENT SHALL CHRIS WILSON BE LIABLE FOR ANY SPECIAL,
 * INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
 * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
 * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR
 * IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 *
 * Author: Chris Wilson <chris@chris-wilson.co.uk>
 */

#include "cairo-test.h"

#include <assert.h>

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

#define WIDTH 800
#define HEIGHT 600

static int
_image_to_glyphs (cairo_surface_t *image,
		  int channel,
		  int level,
		  cairo_scaled_font_t *scaled_font,
		  cairo_glyph_t *glyphs,
		  double tx, double ty)
{
    int width, height, stride;
    const unsigned char *data;
    cairo_format_t format;
    int x, y, z, n;

    width = cairo_image_surface_get_width (image);
    height = cairo_image_surface_get_height (image);
    stride = cairo_image_surface_get_stride (image);
    format = cairo_image_surface_get_format (image);
    data = cairo_image_surface_get_data (image);

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

	for (x = 0; x < width; x++) {
	    z = (row[x] >> channel) & 0xff;
	    if (z == level) {
		double xx, yy, zz;
		char c = n % 26 + 'a';
		int count = 1;
		cairo_glyph_t *glyphs_p = &glyphs[n];

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

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

		cairo_scaled_font_text_to_glyphs (scaled_font,
						  tx + xx, ty + yy,
						  &c, 1,
						  &glyphs_p, &count,
						  NULL, NULL,
						  NULL);
		assert (glyphs_p == &glyphs[n]);
		assert (count == 1);
		n++;
	    }
	}
    }

    return n;
}

static cairo_status_t
_render_image (cairo_t *cr,
	       int width, int height,
	       cairo_surface_t *image)
{
    int ww, hh;
    cairo_glyph_t *glyphs;
    cairo_pattern_t *mask;
    cairo_scaled_font_t *scaled_font;
    double tx, ty;
    const struct {
	int shift;
	double red;
	double green;
	double blue;
    } channel[3] = {
	{  0, 0.9, 0.3, 0.4 },
	{  8, 0.4, 0.9, 0.3 },
	{ 16, 0.3, 0.4, 0.9 },
    };
    unsigned int n, i;

    ww = cairo_image_surface_get_width (image);
    hh = cairo_image_surface_get_height (image);

    glyphs = cairo_glyph_allocate (ww * hh);
    if (glyphs == NULL) {
	cairo_surface_destroy (image);
	return CAIRO_STATUS_NO_MEMORY;
    }

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

    cairo_set_font_size (cr, 5);
    scaled_font = cairo_get_scaled_font (cr);

    for (i = 0; i < sizeof (channel) / sizeof (channel[0]); i++) {
	cairo_push_group_with_content (cr, CAIRO_CONTENT_ALPHA);
	for (n = 0; n < 256; n++) {
	    int num_glyphs;

	    num_glyphs = _image_to_glyphs (image, channel[i].shift, n,
					   scaled_font,
					   glyphs, tx, ty);

	    cairo_set_source_rgba (cr,
				   0, 0, 0,
				   .15 + .85 * n / 255.);
	    cairo_show_glyphs (cr, glyphs, num_glyphs);
	}
	mask = cairo_pop_group (cr);
	cairo_set_source_rgb (cr,
			      channel[i].red,
			      channel[i].green,
			      channel[i].blue);
	cairo_mask (cr, mask);
	cairo_pattern_destroy (mask);
    }

    cairo_glyph_free (glyphs);
    return CAIRO_STATUS_SUCCESS;
}

static cairo_test_status_t
draw (cairo_t *cr, int width, int height)
{
    const cairo_test_context_t *ctx = cairo_test_get_context (cr);
    cairo_surface_t *image;
    cairo_status_t status;

    cairo_set_source_rgb (cr, 0, 0, 0);
    cairo_paint (cr);

    image = cairo_test_create_surface_from_png (ctx, png_filename);
    status = _render_image (cr, width, height, image);
    cairo_surface_destroy (image);

    return cairo_test_status_from_status (ctx, status);
}

CAIRO_TEST (mask_glyphs,
	    "Creates a mask using a distorted array of overlapping glyphs",
	    "mask, glyphs", /* keywords */
	    NULL, /* requirements */
	    WIDTH, HEIGHT,
	    NULL, draw)