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>
28 #include "cairo-test.h"
37 #define WIDTH (TEXT_SIZE * 12 + 2*BORDER)
39 #define HEIGHT ((TEXT_SIZE + 2*BORDER)*2)
43 #define TEXT "geez... cairo user-font"
45 static cairo_user_data_key_t fallback_font_key;
48 test_scaled_font_init (cairo_scaled_font_t *scaled_font,
50 cairo_font_extents_t *extents)
52 cairo_status_t status;
54 cairo_set_font_face (cr,
55 cairo_font_face_get_user_data (cairo_scaled_font_get_font_face (scaled_font),
58 status = cairo_scaled_font_set_user_data (scaled_font,
60 cairo_scaled_font_reference (cairo_get_scaled_font (cr)),
61 (cairo_destroy_func_t) cairo_scaled_font_destroy);
62 if (unlikely (status)) {
63 cairo_scaled_font_destroy (cairo_get_scaled_font (cr));
67 cairo_font_extents (cr, extents);
69 return CAIRO_STATUS_SUCCESS;
73 test_scaled_font_render_glyph (cairo_scaled_font_t *scaled_font,
76 cairo_text_extents_t *extents)
78 cairo_glyph_t cairo_glyph;
80 cairo_glyph.index = glyph;
84 cairo_set_font_face (cr,
85 cairo_font_face_get_user_data (cairo_scaled_font_get_font_face (scaled_font),
88 cairo_show_glyphs (cr, &cairo_glyph, 1);
89 cairo_glyph_extents (cr, &cairo_glyph, 1, extents);
91 return CAIRO_STATUS_SUCCESS;
95 test_scaled_font_text_to_glyphs (cairo_scaled_font_t *scaled_font,
98 cairo_glyph_t **glyphs,
100 cairo_text_cluster_t **clusters,
102 cairo_text_cluster_flags_t *cluster_flags)
104 cairo_scaled_font_t *fallback_scaled_font;
106 fallback_scaled_font = cairo_scaled_font_get_user_data (scaled_font,
109 return cairo_scaled_font_text_to_glyphs (fallback_scaled_font, 0, 0,
112 clusters, num_clusters, cluster_flags);
115 static cairo_status_t
116 _user_font_face_create (cairo_font_face_t **out)
118 cairo_font_face_t *user_font_face;
119 cairo_font_face_t *fallback_font_face;
120 cairo_status_t status;
122 user_font_face = cairo_user_font_face_create ();
123 cairo_user_font_face_set_init_func (user_font_face, test_scaled_font_init);
124 cairo_user_font_face_set_render_glyph_func (user_font_face, test_scaled_font_render_glyph);
125 cairo_user_font_face_set_text_to_glyphs_func (user_font_face, test_scaled_font_text_to_glyphs);
127 /* This also happens to be default font face on cairo_t, so does
128 * not make much sense here. For demonstration only.
130 fallback_font_face = cairo_toy_font_face_create ("",
131 CAIRO_FONT_SLANT_NORMAL,
132 CAIRO_FONT_WEIGHT_NORMAL);
134 status = cairo_font_face_set_user_data (user_font_face,
137 (cairo_destroy_func_t) cairo_font_face_destroy);
139 cairo_font_face_destroy (fallback_font_face);
140 cairo_font_face_destroy (user_font_face);
144 *out = user_font_face;
145 return CAIRO_STATUS_SUCCESS;
148 static cairo_test_status_t
149 draw (cairo_t *cr, int width, int height)
151 const char text[] = TEXT;
152 cairo_font_extents_t font_extents;
153 cairo_text_extents_t extents;
154 cairo_font_face_t *font_face;
155 cairo_status_t status;
157 cairo_set_source_rgb (cr, 1, 1, 1);
161 cairo_translate (cr, TEXT_SIZE, 0);
162 cairo_rotate (cr, .6);
165 status = _user_font_face_create (&font_face);
167 return cairo_test_status_from_status (cairo_test_get_context (cr),
171 cairo_set_font_face (cr, font_face);
172 cairo_font_face_destroy (font_face);
174 cairo_set_font_size (cr, TEXT_SIZE);
176 cairo_font_extents (cr, &font_extents);
177 cairo_text_extents (cr, text, &extents);
179 /* logical boundaries in red */
180 cairo_move_to (cr, 0, BORDER);
181 cairo_rel_line_to (cr, WIDTH, 0);
182 cairo_move_to (cr, 0, BORDER + font_extents.ascent);
183 cairo_rel_line_to (cr, WIDTH, 0);
184 cairo_move_to (cr, 0, BORDER + font_extents.ascent + font_extents.descent);
185 cairo_rel_line_to (cr, WIDTH, 0);
186 cairo_move_to (cr, BORDER, 0);
187 cairo_rel_line_to (cr, 0, 2*BORDER + TEXT_SIZE);
188 cairo_move_to (cr, BORDER + extents.x_advance, 0);
189 cairo_rel_line_to (cr, 0, 2*BORDER + TEXT_SIZE);
190 cairo_set_source_rgb (cr, 1, 0, 0);
191 cairo_set_line_width (cr, 2);
194 /* ink boundaries in green */
196 BORDER + extents.x_bearing, BORDER + font_extents.ascent + extents.y_bearing,
197 extents.width, extents.height);
198 cairo_set_source_rgb (cr, 0, 1, 0);
199 cairo_set_line_width (cr, 2);
203 cairo_set_source_rgb (cr, 0, 0, 0);
204 cairo_move_to (cr, BORDER, BORDER + font_extents.ascent);
205 cairo_show_text (cr, text);
208 /* filled version of text in light blue */
209 cairo_set_source_rgb (cr, 0, 0, 1);
210 cairo_move_to (cr, BORDER, BORDER + font_extents.height + BORDER + font_extents.ascent);
211 cairo_text_path (cr, text);
214 return CAIRO_TEST_SUCCESS;
217 CAIRO_TEST (user_font_proxy,
218 "Tests a user-font using a native font in its render_glyph",
219 "font, user-font", /* keywords */
220 "cairo >= 1.7.4", /* requirements */