1
/*
2
 * Copyright © 2006, 2008 Red Hat, Inc.
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
 * Red Hat, Inc. not be used in advertising or publicity pertaining to
10
 * distribution of the software without specific, written prior
11
 * permission. Red Hat, Inc. 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
 * RED HAT, INC. DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS
16
 * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
17
 * FITNESS, IN NO EVENT SHALL RED HAT, INC. 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
 * Contributor(s):
24
 *	Kristian Høgsberg <krh@redhat.com>
25
 *	Behdad Esfahbod <behdad@behdad.org>
26
 *	Adrian Johnson <ajohnson@redneon.com>
27
 */
28

            
29
#include "cairo-test.h"
30

            
31
#include <stdlib.h>
32
#include <stdio.h>
33

            
34
/*#define ROTATED 1*/
35

            
36
#define BORDER 10
37
#define TEXT_SIZE 64
38
#define WIDTH  (TEXT_SIZE * 15 + 2*BORDER)
39
#ifndef ROTATED
40
 #define HEIGHT ((TEXT_SIZE + 2*BORDER)*2)
41
#else
42
 #define HEIGHT WIDTH
43
#endif
44
#define END_GLYPH 0
45
#define TEXT   "cairo"
46

            
47
/* Reverse the bits in a byte with 7 operations (no 64-bit):
48
 * Devised by Sean Anderson, July 13, 2001.
49
 * Source: http://graphics.stanford.edu/~seander/bithacks.html#ReverseByteWith32Bits
50
 */
51
#define CAIRO_BITSWAP8(c) ((((c) * 0x0802LU & 0x22110LU) | ((c) * 0x8020LU & 0x88440LU)) * 0x10101LU >> 16)
52

            
53
#ifdef WORDS_BIGENDIAN
54
#define CAIRO_BITSWAP8_IF_LITTLE_ENDIAN(c) (c)
55
#else
56
#define CAIRO_BITSWAP8_IF_LITTLE_ENDIAN(c) CAIRO_BITSWAP8(c)
57
#endif
58

            
59

            
60

            
61
/* Simple glyph definition. data is an 8x8 bitmap.
62
 */
63
typedef struct {
64
    unsigned long ucs4;
65
    int width;
66
    char data[8];
67
} test_scaled_font_glyph_t;
68

            
69
static cairo_user_data_key_t test_font_face_glyphs_key;
70

            
71
static cairo_status_t
72
3
test_scaled_font_init (cairo_scaled_font_t  *scaled_font,
73
		       cairo_t              *cr,
74
		       cairo_font_extents_t *metrics)
75
{
76
3
  metrics->ascent  = 1;
77
3
  metrics->descent = 0;
78
3
  return CAIRO_STATUS_SUCCESS;
79
}
80

            
81
static cairo_status_t
82
45
test_scaled_font_unicode_to_glyph (cairo_scaled_font_t *scaled_font,
83
				   unsigned long        unicode,
84
				   unsigned long       *glyph)
85
{
86
45
    test_scaled_font_glyph_t *glyphs = cairo_font_face_get_user_data (cairo_scaled_font_get_font_face (scaled_font),
87
								      &test_font_face_glyphs_key);
88
    int i;
89

            
90
135
    for (i = 0; glyphs[i].ucs4 != (unsigned long) -1; i++)
91
135
	if (glyphs[i].ucs4 == unicode) {
92
45
	    *glyph = i;
93
45
	    return CAIRO_STATUS_SUCCESS;
94
	}
95

            
96
    /* Not found.  Default to glyph 0 */
97
    return CAIRO_STATUS_SUCCESS;
98
}
99

            
100
static cairo_status_t
101
15
test_scaled_font_render_glyph (cairo_scaled_font_t  *scaled_font,
102
			       unsigned long         glyph,
103
			       cairo_t              *cr,
104
			       cairo_text_extents_t *metrics)
105
{
106
15
    test_scaled_font_glyph_t *glyphs = cairo_font_face_get_user_data (cairo_scaled_font_get_font_face (scaled_font),
107
								      &test_font_face_glyphs_key);
108
    int i;
109
    unsigned char *data;
110
    cairo_surface_t *image;
111
    cairo_pattern_t *pattern;
112
    cairo_matrix_t matrix;
113
    uint8_t byte;
114

            
115
    /* FIXME: We simply crash on out-of-bound glyph indices */
116

            
117
15
    metrics->x_advance = (glyphs[glyph].width + 1) / 8.0;
118

            
119
15
    image = cairo_image_surface_create (CAIRO_FORMAT_A1, glyphs[glyph].width, 8);
120
15
    if (cairo_surface_status (image))
121
	return cairo_surface_status (image);
122

            
123
15
    data = cairo_image_surface_get_data (image);
124
135
    for (i = 0; i < 8; i++) {
125
120
	byte = glyphs[glyph].data[i];
126
120
	*data = CAIRO_BITSWAP8_IF_LITTLE_ENDIAN (byte);
127
120
	data += cairo_image_surface_get_stride (image);
128
    }
129
15
    cairo_surface_mark_dirty (image);
130

            
131
15
    pattern = cairo_pattern_create_for_surface (image);
132
15
    cairo_surface_destroy (image);
133

            
134
15
    cairo_matrix_init_identity (&matrix);
135
15
    cairo_matrix_scale (&matrix, 1.0/8.0, 1.0/8.0);
136
15
    cairo_matrix_translate (&matrix, 0, -8);
137
15
    cairo_matrix_invert (&matrix);
138
15
    cairo_pattern_set_matrix (pattern, &matrix);
139

            
140
15
    cairo_set_source (cr, pattern);
141
15
    cairo_mask (cr, pattern);
142
15
    cairo_pattern_destroy (pattern);
143

            
144
15
    return CAIRO_STATUS_SUCCESS;
145
}
146

            
147
static cairo_status_t
148
3
_user_font_face_create (cairo_font_face_t **out)
149
{
150
    static const test_scaled_font_glyph_t glyphs [] = {
151
	{ 'c',  6, { 0x00, 0x38, 0x44, 0x80, 0x80, 0x80, 0x44, 0x38 } },
152
	{ 'a',  6, { 0x00, 0x70, 0x88, 0x3c, 0x44, 0x84, 0x8c, 0x74 } },
153
	{ 'i',  1, { 0x80, 0x00, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80 } },
154
	{ 'r',  6, { 0x00, 0xb8, 0xc4, 0x80, 0x80, 0x80, 0x80, 0x80 } },
155
	{ 'o',  7, { 0x00, 0x38, 0x44, 0x82, 0x82, 0x82, 0x44, 0x38 } },
156
	{  -1,  8, { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } },
157
    };
158

            
159
    cairo_font_face_t *user_font_face;
160
    cairo_status_t status;
161

            
162
3
    user_font_face = cairo_user_font_face_create ();
163
3
    cairo_user_font_face_set_init_func             (user_font_face, test_scaled_font_init);
164
3
    cairo_user_font_face_set_render_glyph_func     (user_font_face, test_scaled_font_render_glyph);
165
3
    cairo_user_font_face_set_unicode_to_glyph_func (user_font_face, test_scaled_font_unicode_to_glyph);
166

            
167
3
    status = cairo_font_face_set_user_data (user_font_face,
168
					    &test_font_face_glyphs_key,
169
					    (void*) glyphs, NULL);
170
3
    if (status) {
171
	cairo_font_face_destroy (user_font_face);
172
	return status;
173
    }
174

            
175
3
    *out = user_font_face;
176
3
    return CAIRO_STATUS_SUCCESS;
177
}
178

            
179
static cairo_test_status_t
180
3
draw (cairo_t *cr, int width, int height)
181
{
182
    cairo_font_face_t *font_face;
183
3
    const char text[] = TEXT;
184
    cairo_font_extents_t font_extents;
185
    cairo_text_extents_t extents;
186
    cairo_status_t status;
187

            
188
3
    cairo_set_source_rgb (cr, 1, 1, 1);
189
3
    cairo_paint (cr);
190

            
191
#ifdef ROTATED
192
    cairo_translate (cr, TEXT_SIZE, 0);
193
    cairo_rotate (cr, .6);
194
#endif
195

            
196
3
    status = _user_font_face_create (&font_face);
197
3
    if (status) {
198
	return cairo_test_status_from_status (cairo_test_get_context (cr),
199
					      status);
200
    }
201

            
202
3
    cairo_set_font_face (cr, font_face);
203
3
    cairo_font_face_destroy (font_face);
204

            
205
3
    cairo_set_font_size (cr, TEXT_SIZE);
206

            
207
3
    cairo_font_extents (cr, &font_extents);
208
3
    cairo_text_extents (cr, text, &extents);
209

            
210
    /* logical boundaries in red */
211
3
    cairo_move_to (cr, 0, BORDER);
212
3
    cairo_rel_line_to (cr, WIDTH, 0);
213
3
    cairo_move_to (cr, 0, BORDER + font_extents.ascent);
214
3
    cairo_rel_line_to (cr, WIDTH, 0);
215
3
    cairo_move_to (cr, 0, BORDER + font_extents.ascent + font_extents.descent);
216
3
    cairo_rel_line_to (cr, WIDTH, 0);
217
3
    cairo_move_to (cr, BORDER, 0);
218
3
    cairo_rel_line_to (cr, 0, 2*BORDER + TEXT_SIZE);
219
3
    cairo_move_to (cr, BORDER + extents.x_advance, 0);
220
3
    cairo_rel_line_to (cr, 0, 2*BORDER + TEXT_SIZE);
221
3
    cairo_set_source_rgb (cr, 1, 0, 0);
222
3
    cairo_set_line_width (cr, 2);
223
3
    cairo_stroke (cr);
224

            
225
    /* ink boundaries in green */
226
3
    cairo_rectangle (cr,
227
3
		     BORDER + extents.x_bearing, BORDER + font_extents.ascent + extents.y_bearing,
228
		     extents.width, extents.height);
229
3
    cairo_set_source_rgb (cr, 0, 1, 0);
230
3
    cairo_set_line_width (cr, 2);
231
3
    cairo_stroke (cr);
232

            
233
    /* text in black */
234
3
    cairo_set_source_rgb (cr, 0, 0, 0);
235
3
    cairo_move_to (cr, BORDER, BORDER + font_extents.ascent);
236
3
    cairo_show_text (cr, text);
237

            
238

            
239
    /* filled version of text in blue */
240
3
    cairo_set_source_rgb (cr, 0, 0, 1);
241
3
    cairo_move_to (cr, BORDER, BORDER + font_extents.height + 2*BORDER + font_extents.ascent);
242
3
    cairo_text_path (cr, text);
243
3
    cairo_fill (cr);
244

            
245
3
    return CAIRO_TEST_SUCCESS;
246
}
247

            
248
1
CAIRO_TEST (user_font_mask,
249
	    "Tests a user-font using cairo_mask with bitmap images",
250
	    "user-font, mask", /* keywords */
251
	    NULL, /* requirements */
252
	    WIDTH, HEIGHT,
253
	    NULL, draw)