2 * Copyright © 2008 Jeff Muizelaar
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 * Jeff Muizelaar not be used in advertising or publicity pertaining to
10 * distribution of the software without specific, written prior
11 * permission. Jeff Muizelaar 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 * JEFF MUIZELAAR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS
16 * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
17 * FITNESS, IN NO EVENT SHALL JEFF MUIZELAAR 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 * Jeff Muizelaar <jeff@infidigm.net>
25 * Kristian Høgsberg <krh@redhat.com>
26 * Behdad Esfahbod <behdad@behdad.org>
29 #include "cairo-test.h"
35 #define WIDTH (TEXT_SIZE * 13.75 + 2*BORDER)
36 #define HEIGHT ((TEXT_SIZE + 2*BORDER)*3 + BORDER)
37 #define TEXT "test of rescaled glyphs"
39 static const cairo_user_data_key_t rescale_font_closure_key;
41 struct rescaled_font {
42 cairo_font_face_t *substitute_font;
43 cairo_scaled_font_t *measuring_font;
44 unsigned long glyph_count;
46 double *desired_width;
47 double *rescale_factor;
51 test_scaled_font_render_glyph (cairo_scaled_font_t *scaled_font,
54 cairo_text_extents_t *metrics)
56 cairo_font_face_t *user_font;
57 struct rescaled_font *r;
58 cairo_glyph_t cairo_glyph;
60 cairo_glyph.index = glyph;
64 user_font = cairo_scaled_font_get_font_face (scaled_font);
65 r = cairo_font_face_get_user_data (user_font, &rescale_font_closure_key);
66 cairo_set_font_face (cr, r->substitute_font);
68 if (glyph - r->start < r->glyph_count) {
69 cairo_matrix_t matrix;
71 if (isnan (r->rescale_factor[glyph - r->start])) {
74 cairo_text_extents_t extents;
76 /* measure the glyph and compute the necessary rescaling factor */
77 cairo_scaled_font_glyph_extents (r->measuring_font,
81 desired_width = r->desired_width[glyph - r->start];
82 actual_width = extents.x_advance;
84 r->rescale_factor[glyph - r->start] = desired_width / actual_width;
87 /* scale the font so that the glyph width matches the desired width */
88 cairo_get_font_matrix (cr, &matrix);
89 cairo_matrix_scale (&matrix, r->rescale_factor[glyph - r->start], 1.);
90 cairo_set_font_matrix (cr, &matrix);
93 cairo_show_glyphs (cr, &cairo_glyph, 1);
94 cairo_glyph_extents (cr, &cairo_glyph, 1, metrics);
96 return CAIRO_STATUS_SUCCESS;
100 unichar_to_utf8 (uint32_t ucs4, char utf8[7])
102 int i, charlen, first;
107 } else if (ucs4 < 0x800) {
110 } else if (ucs4 < 0x10000) {
113 } else if (ucs4 < 0x200000) {
116 } else if (ucs4 < 0x4000000) {
124 for (i = charlen - 1; i > 0; --i) {
125 utf8[i] = (ucs4 & 0x3f) | 0x80;
128 utf8[0] = ucs4 | first;
129 utf8[charlen] = '\0';
132 static cairo_status_t
133 test_scaled_font_unicode_to_glyph (cairo_scaled_font_t *scaled_font,
134 unsigned long unicode,
135 unsigned long *glyph_index)
137 cairo_font_face_t *user_font;
138 struct rescaled_font *r;
140 cairo_glyph_t *glyphs = NULL;
141 cairo_status_t status;
144 user_font = cairo_scaled_font_get_font_face (scaled_font);
146 unichar_to_utf8 (unicode, utf8);
147 r = cairo_font_face_get_user_data (user_font, &rescale_font_closure_key);
148 status = cairo_scaled_font_text_to_glyphs (r->measuring_font, 0, 0,
150 &glyphs, &num_glyphs,
155 *glyph_index = glyphs[0].index;
157 cairo_glyph_free (glyphs);
158 return CAIRO_STATUS_SUCCESS;
161 static void rescale_font_closure_destroy (void *data)
163 struct rescaled_font *r = data;
165 cairo_font_face_destroy (r->substitute_font);
166 cairo_scaled_font_destroy (r->measuring_font);
167 free (r->desired_width);
168 free (r->rescale_factor);
172 static cairo_status_t
173 create_rescaled_font (cairo_font_face_t *substitute_font,
176 double *desired_width,
177 cairo_font_face_t **out)
179 cairo_font_face_t *user_font_face;
180 struct rescaled_font *r;
181 cairo_font_options_t *options;
182 cairo_status_t status;
186 user_font_face = cairo_user_font_face_create ();
187 cairo_user_font_face_set_render_glyph_func (user_font_face, test_scaled_font_render_glyph);
188 cairo_user_font_face_set_unicode_to_glyph_func (user_font_face, test_scaled_font_unicode_to_glyph);
190 r = xmalloc (sizeof (struct rescaled_font));
191 r->substitute_font = cairo_font_face_reference (substitute_font);
193 /* we don't want any hinting when doing the measuring */
194 options = cairo_font_options_create ();
195 cairo_font_options_set_hint_style (options, CAIRO_HINT_STYLE_NONE);
196 cairo_font_options_set_hint_metrics (options, CAIRO_HINT_METRICS_OFF);
198 cairo_matrix_init_identity (&m);
200 r->measuring_font = cairo_scaled_font_create (r->substitute_font,
203 cairo_font_options_destroy (options);
206 r->start = glyph_start;
207 r->glyph_count = glyph_count;
208 r->desired_width = xcalloc (sizeof (double), r->glyph_count);
209 r->rescale_factor = xcalloc (sizeof (double), r->glyph_count);
211 for (i = 0; i < r->glyph_count; i++) {
212 r->desired_width[i] = desired_width[i];
213 /* use NaN to specify unset */
214 r->rescale_factor[i] = cairo_test_NaN ();
217 status = cairo_font_face_set_user_data (user_font_face,
218 &rescale_font_closure_key,
219 r, rescale_font_closure_destroy);
221 rescale_font_closure_destroy (r);
222 cairo_font_face_destroy (user_font_face);
226 *out = user_font_face;
227 return CAIRO_STATUS_SUCCESS;
230 static cairo_status_t
231 get_user_font_face (cairo_font_face_t *substitute_font,
233 cairo_font_face_t *old,
234 cairo_font_face_t **out)
236 cairo_font_options_t *options;
238 cairo_scaled_font_t *measure;
243 unsigned long min_index, max_index;
244 cairo_status_t status;
246 cairo_glyph_t *glyphs = NULL;
248 /* we don't want any hinting when doing the measuring */
249 options = cairo_font_options_create ();
250 cairo_font_options_set_hint_style (options, CAIRO_HINT_STYLE_NONE);
251 cairo_font_options_set_hint_metrics (options, CAIRO_HINT_METRICS_OFF);
253 cairo_matrix_init_identity (&m);
254 measure = cairo_scaled_font_create (old, &m, &m, options);
256 status = cairo_scaled_font_text_to_glyphs (measure, 0, 0,
258 &glyphs, &num_glyphs,
260 cairo_font_options_destroy (options);
263 cairo_scaled_font_destroy (measure);
267 /* find the glyph range the text covers */
268 max_index = glyphs[0].index;
269 min_index = glyphs[0].index;
270 for (i=0; i<num_glyphs; i++) {
271 if (glyphs[i].index < min_index)
272 min_index = glyphs[i].index;
273 if (glyphs[i].index > max_index)
274 max_index = glyphs[i].index;
277 count = max_index - min_index + 1;
278 widths = xcalloc (sizeof (double), count);
279 /* measure all of the necessary glyphs individually */
280 for (i=0; i<num_glyphs; i++) {
281 cairo_text_extents_t extents;
282 cairo_scaled_font_glyph_extents (measure, &glyphs[i], 1, &extents);
283 widths[glyphs[i].index - min_index] = extents.x_advance;
286 status = cairo_scaled_font_status (measure);
287 cairo_scaled_font_destroy (measure);
288 cairo_glyph_free (glyphs);
290 if (status == CAIRO_STATUS_SUCCESS) {
291 status = create_rescaled_font (substitute_font,
292 min_index, count, widths,
300 static cairo_test_status_t
301 draw (cairo_t *cr, int width, int height)
303 cairo_font_extents_t font_extents;
304 cairo_text_extents_t extents;
305 cairo_font_face_t *rescaled;
306 cairo_font_face_t *old;
307 cairo_font_face_t *substitute;
308 const char text[] = TEXT;
309 cairo_status_t status;
311 cairo_set_source_rgb (cr, 1, 1, 1);
314 cairo_select_font_face (cr,
315 CAIRO_TEST_FONT_FAMILY " Sans",
316 CAIRO_FONT_SLANT_NORMAL,
317 CAIRO_FONT_WEIGHT_NORMAL);
319 cairo_set_font_size (cr, TEXT_SIZE);
321 cairo_font_extents (cr, &font_extents);
322 cairo_text_extents (cr, text, &extents);
324 cairo_set_source_rgb (cr, 0, 0, 0);
325 cairo_move_to (cr, BORDER, BORDER + font_extents.ascent);
326 cairo_show_text (cr, text);
328 /* same text in 'mono' with widths that match the 'sans' version */
329 old = cairo_font_face_reference (cairo_get_font_face (cr));
330 cairo_select_font_face (cr,
331 CAIRO_TEST_FONT_FAMILY " Sans Mono",
332 CAIRO_FONT_SLANT_NORMAL,
333 CAIRO_FONT_WEIGHT_NORMAL);
334 substitute = cairo_get_font_face (cr);
336 status = get_user_font_face (substitute, text, old, &rescaled);
337 cairo_font_face_destroy (old);
339 return cairo_test_status_from_status (cairo_test_get_context (cr),
343 cairo_set_font_face (cr, rescaled);
344 cairo_font_face_destroy (rescaled);
346 cairo_set_source_rgb (cr, 0, 0, 1);
347 cairo_move_to (cr, BORDER, BORDER + font_extents.height + 2*BORDER + font_extents.ascent);
348 cairo_show_text (cr, text);
351 cairo_select_font_face (cr,
352 CAIRO_TEST_FONT_FAMILY " Sans Mono",
353 CAIRO_FONT_SLANT_NORMAL,
354 CAIRO_FONT_WEIGHT_NORMAL);
356 cairo_set_source_rgb (cr, 0, 0, 1);
357 cairo_move_to (cr, BORDER, BORDER + 2*font_extents.height + 4*BORDER + font_extents.ascent);
358 cairo_show_text (cr, text);
360 return CAIRO_TEST_SUCCESS;
363 CAIRO_TEST (user_font_rescale,
364 "Tests drawing text with user defined widths",
365 "user-font, font", /* keywords */
366 NULL, /* requirements */