Initialize Tizen 2.3
[framework/graphics/cairo.git] / test / user-font-mask.c
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 <stdlib.h>
30 #include <stdio.h>
31
32 #include "cairo-test.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 test_scaled_font_init (cairo_scaled_font_t  *scaled_font,
73                        cairo_t              *cr,
74                        cairo_font_extents_t *metrics)
75 {
76   metrics->ascent  = 1;
77   metrics->descent = 0;
78   return CAIRO_STATUS_SUCCESS;
79 }
80
81 static cairo_status_t
82 test_scaled_font_unicode_to_glyph (cairo_scaled_font_t *scaled_font,
83                                    unsigned long        unicode,
84                                    unsigned long       *glyph)
85 {
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);
88     int i;
89
90     for (i = 0; glyphs[i].ucs4 != (unsigned long) -1; i++)
91         if (glyphs[i].ucs4 == unicode) {
92             *glyph = i;
93             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 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     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     metrics->x_advance = (glyphs[glyph].width + 1) / 8.0;
118
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);
122
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);
128     }
129     cairo_surface_mark_dirty (image);
130
131     pattern = cairo_pattern_create_for_surface (image);
132     cairo_surface_destroy (image);
133
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);
139
140     cairo_set_source (cr, pattern);
141     cairo_mask (cr, pattern);
142     cairo_pattern_destroy (pattern);
143
144     return CAIRO_STATUS_SUCCESS;
145 }
146
147 static cairo_status_t
148 _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     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);
166
167     status = cairo_font_face_set_user_data (user_font_face,
168                                             &test_font_face_glyphs_key,
169                                             (void*) glyphs, NULL);
170     if (status) {
171         cairo_font_face_destroy (user_font_face);
172         return status;
173     }
174
175     *out = user_font_face;
176     return CAIRO_STATUS_SUCCESS;
177 }
178
179 static cairo_test_status_t
180 draw (cairo_t *cr, int width, int height)
181 {
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;
187
188     cairo_set_source_rgb (cr, 1, 1, 1);
189     cairo_paint (cr);
190
191 #ifdef ROTATED
192     cairo_translate (cr, TEXT_SIZE, 0);
193     cairo_rotate (cr, .6);
194 #endif
195
196     status = _user_font_face_create (&font_face);
197     if (status) {
198         return cairo_test_status_from_status (cairo_test_get_context (cr),
199                                               status);
200     }
201
202     cairo_set_font_face (cr, font_face);
203     cairo_font_face_destroy (font_face);
204
205     cairo_set_font_size (cr, TEXT_SIZE);
206
207     cairo_font_extents (cr, &font_extents);
208     cairo_text_extents (cr, text, &extents);
209
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);
223     cairo_stroke (cr);
224
225     /* ink boundaries in green */
226     cairo_rectangle (cr,
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);
231     cairo_stroke (cr);
232
233     /* text in black */
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);
237
238
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);
243     cairo_fill (cr);
244
245     return CAIRO_TEST_SUCCESS;
246 }
247
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 */
252             WIDTH, HEIGHT,
253             NULL, draw)