2 * Copyright © 2006, 2008 Red Hat, Inc.
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.
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.
24 * Kristian Høgsberg <krh@redhat.com>
25 * Behdad Esfahbod <behdad@behdad.org>
26 * Adrian Johnson <ajohnson@redneon.com>
32 #include "cairo-test.h"
38 #define WIDTH (TEXT_SIZE * 15 + 2*BORDER)
40 #define HEIGHT ((TEXT_SIZE + 2*BORDER)*2)
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
51 #define CAIRO_BITSWAP8(c) ((((c) * 0x0802LU & 0x22110LU) | ((c) * 0x8020LU & 0x88440LU)) * 0x10101LU >> 16)
53 #ifdef WORDS_BIGENDIAN
54 #define CAIRO_BITSWAP8_IF_LITTLE_ENDIAN(c) (c)
56 #define CAIRO_BITSWAP8_IF_LITTLE_ENDIAN(c) CAIRO_BITSWAP8(c)
61 /* Simple glyph definition. data is an 8x8 bitmap.
67 } test_scaled_font_glyph_t;
69 static cairo_user_data_key_t test_font_face_glyphs_key;
72 test_scaled_font_init (cairo_scaled_font_t *scaled_font,
74 cairo_font_extents_t *metrics)
78 return CAIRO_STATUS_SUCCESS;
82 test_scaled_font_unicode_to_glyph (cairo_scaled_font_t *scaled_font,
83 unsigned long unicode,
86 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);
90 for (i = 0; glyphs[i].ucs4 != (unsigned long) -1; i++)
91 if (glyphs[i].ucs4 == unicode) {
93 return CAIRO_STATUS_SUCCESS;
96 /* Not found. Default to glyph 0 */
97 return CAIRO_STATUS_SUCCESS;
100 static cairo_status_t
101 test_scaled_font_render_glyph (cairo_scaled_font_t *scaled_font,
104 cairo_text_extents_t *metrics)
106 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);
110 cairo_surface_t *image;
111 cairo_pattern_t *pattern;
112 cairo_matrix_t matrix;
115 /* FIXME: We simply crash on out-of-bound glyph indices */
117 metrics->x_advance = (glyphs[glyph].width + 1) / 8.0;
119 image = cairo_image_surface_create (CAIRO_FORMAT_A1, glyphs[glyph].width, 8);
120 if (cairo_surface_status (image))
121 return cairo_surface_status (image);
123 data = cairo_image_surface_get_data (image);
124 for (i = 0; i < 8; i++) {
125 byte = glyphs[glyph].data[i];
126 *data = CAIRO_BITSWAP8_IF_LITTLE_ENDIAN (byte);
127 data += cairo_image_surface_get_stride (image);
129 cairo_surface_mark_dirty (image);
131 pattern = cairo_pattern_create_for_surface (image);
132 cairo_surface_destroy (image);
134 cairo_matrix_init_identity (&matrix);
135 cairo_matrix_scale (&matrix, 1.0/8.0, 1.0/8.0);
136 cairo_matrix_translate (&matrix, 0, -8);
137 cairo_matrix_invert (&matrix);
138 cairo_pattern_set_matrix (pattern, &matrix);
140 cairo_set_source (cr, pattern);
141 cairo_mask (cr, pattern);
142 cairo_pattern_destroy (pattern);
144 return CAIRO_STATUS_SUCCESS;
147 static cairo_status_t
148 _user_font_face_create (cairo_font_face_t **out)
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 } },
159 cairo_font_face_t *user_font_face;
160 cairo_status_t status;
162 user_font_face = cairo_user_font_face_create ();
163 cairo_user_font_face_set_init_func (user_font_face, test_scaled_font_init);
164 cairo_user_font_face_set_render_glyph_func (user_font_face, test_scaled_font_render_glyph);
165 cairo_user_font_face_set_unicode_to_glyph_func (user_font_face, test_scaled_font_unicode_to_glyph);
167 status = cairo_font_face_set_user_data (user_font_face,
168 &test_font_face_glyphs_key,
169 (void*) glyphs, NULL);
171 cairo_font_face_destroy (user_font_face);
175 *out = user_font_face;
176 return CAIRO_STATUS_SUCCESS;
179 static cairo_test_status_t
180 draw (cairo_t *cr, int width, int height)
182 cairo_font_face_t *font_face;
183 const char text[] = TEXT;
184 cairo_font_extents_t font_extents;
185 cairo_text_extents_t extents;
186 cairo_status_t status;
188 cairo_set_source_rgb (cr, 1, 1, 1);
192 cairo_translate (cr, TEXT_SIZE, 0);
193 cairo_rotate (cr, .6);
196 status = _user_font_face_create (&font_face);
198 return cairo_test_status_from_status (cairo_test_get_context (cr),
202 cairo_set_font_face (cr, font_face);
203 cairo_font_face_destroy (font_face);
205 cairo_set_font_size (cr, TEXT_SIZE);
207 cairo_font_extents (cr, &font_extents);
208 cairo_text_extents (cr, text, &extents);
210 /* logical boundaries in red */
211 cairo_move_to (cr, 0, BORDER);
212 cairo_rel_line_to (cr, WIDTH, 0);
213 cairo_move_to (cr, 0, BORDER + font_extents.ascent);
214 cairo_rel_line_to (cr, WIDTH, 0);
215 cairo_move_to (cr, 0, BORDER + font_extents.ascent + font_extents.descent);
216 cairo_rel_line_to (cr, WIDTH, 0);
217 cairo_move_to (cr, BORDER, 0);
218 cairo_rel_line_to (cr, 0, 2*BORDER + TEXT_SIZE);
219 cairo_move_to (cr, BORDER + extents.x_advance, 0);
220 cairo_rel_line_to (cr, 0, 2*BORDER + TEXT_SIZE);
221 cairo_set_source_rgb (cr, 1, 0, 0);
222 cairo_set_line_width (cr, 2);
225 /* ink boundaries in green */
227 BORDER + extents.x_bearing, BORDER + font_extents.ascent + extents.y_bearing,
228 extents.width, extents.height);
229 cairo_set_source_rgb (cr, 0, 1, 0);
230 cairo_set_line_width (cr, 2);
234 cairo_set_source_rgb (cr, 0, 0, 0);
235 cairo_move_to (cr, BORDER, BORDER + font_extents.ascent);
236 cairo_show_text (cr, text);
239 /* filled version of text in blue */
240 cairo_set_source_rgb (cr, 0, 0, 1);
241 cairo_move_to (cr, BORDER, BORDER + font_extents.height + 2*BORDER + font_extents.ascent);
242 cairo_text_path (cr, text);
245 return CAIRO_TEST_SUCCESS;
248 CAIRO_TEST (user_font_mask,
249 "Tests a user-font using cairo_mask with bitmap images",
250 "user-font, mask", /* keywords */
251 NULL, /* requirements */