Viewport clipping now works with both GL and Gallium.
[cairo/gpu.git] / test / bitmap-font.c
blobe63b70c1a2ab2a14618e590aab9b9e7ab0117f3a
1 /*
2 * Copyright © 2005 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.
23 * Author: Carl D. Worth <cworth@cworth.org>
26 #include "cairo-test.h"
28 #include <sys/types.h>
29 #include <sys/stat.h>
30 #include <unistd.h>
32 #include <cairo-ft.h>
33 #include <fontconfig/fontconfig.h>
34 #include <fontconfig/fcfreetype.h>
36 #define FONT "6x13.pcf"
37 #define TEXT_SIZE 13
39 static cairo_bool_t
40 font_extents_equal (const cairo_font_extents_t *A,
41 const cairo_font_extents_t *B)
43 return
44 CAIRO_TEST_DOUBLE_EQUALS (A->ascent, B->ascent) &&
45 CAIRO_TEST_DOUBLE_EQUALS (A->descent, B->descent) &&
46 CAIRO_TEST_DOUBLE_EQUALS (A->height, B->height) &&
47 CAIRO_TEST_DOUBLE_EQUALS (A->max_x_advance, B->max_x_advance) &&
48 CAIRO_TEST_DOUBLE_EQUALS (A->max_y_advance, B->max_y_advance);
51 static cairo_test_status_t
52 check_font_extents (const cairo_test_context_t *ctx, cairo_t *cr, const char *comment)
54 cairo_font_extents_t font_extents, ref_font_extents = {11, 2, 13, 6, 0};
55 cairo_status_t status;
57 memset (&font_extents, 0xff, sizeof (cairo_font_extents_t));
58 cairo_font_extents (cr, &font_extents);
60 status = cairo_status (cr);
61 if (status)
62 return cairo_test_status_from_status (ctx, status);
64 if (! font_extents_equal (&font_extents, &ref_font_extents)) {
65 cairo_test_log (ctx, "Error: %s: cairo_font_extents(); extents (%g, %g, %g, %g, %g)\n",
66 comment,
67 font_extents.ascent, font_extents.descent,
68 font_extents.height,
69 font_extents.max_x_advance, font_extents.max_y_advance);
70 return CAIRO_TEST_FAILURE;
73 return CAIRO_TEST_SUCCESS;
76 static cairo_test_status_t
77 draw (cairo_t *cr, int width, int height)
79 const cairo_test_context_t *ctx = cairo_test_get_context (cr);
80 FcPattern *pattern;
81 cairo_font_face_t *font_face;
82 cairo_font_extents_t font_extents;
83 cairo_font_options_t *font_options;
84 cairo_status_t status;
85 char *filename;
86 int face_count;
87 struct stat stat_buf;
89 xasprintf (&filename, "%s/%s", ctx->srcdir, FONT);
91 if (stat (filename, &stat_buf) || ! S_ISREG (stat_buf.st_mode)) {
92 cairo_test_log (ctx, "Error finding font: %s: file not found?\n", filename);
93 return CAIRO_TEST_FAILURE;
96 pattern = FcFreeTypeQuery ((unsigned char *)filename, 0, NULL, &face_count);
97 free (filename);
98 if (! pattern) {
99 cairo_test_log (ctx, "FcFreeTypeQuery failed.\n");
100 return CAIRO_TEST_FAILURE;
103 font_face = cairo_ft_font_face_create_for_pattern (pattern);
105 status = cairo_font_face_status (font_face);
106 if (status) {
107 cairo_test_log (ctx, "Error creating font face for %s: %s\n",
108 filename,
109 cairo_status_to_string (status));
110 FcPatternDestroy (pattern);
111 return CAIRO_TEST_FAILURE;
114 if (cairo_font_face_get_type (font_face) != CAIRO_FONT_TYPE_FT) {
115 cairo_test_log (ctx, "Unexpected value from cairo_font_face_get_type: %d (expected %d)\n",
116 cairo_font_face_get_type (font_face), CAIRO_FONT_TYPE_FT);
117 cairo_font_face_destroy (font_face);
118 FcPatternDestroy (pattern);
119 return CAIRO_TEST_FAILURE;
122 cairo_set_font_face (cr, font_face);
124 #define CHECK_FONT_EXTENTS(comment) if (check_font_extents (ctx, cr, (comment)) != CAIRO_TEST_SUCCESS) return CAIRO_TEST_FAILURE
126 cairo_font_extents (cr, &font_extents);
127 CHECK_FONT_EXTENTS ("default");
129 FcPatternDestroy (pattern);
130 cairo_font_face_destroy (font_face);
132 font_options = cairo_font_options_create ();
134 cairo_font_options_set_hint_metrics (font_options, CAIRO_HINT_METRICS_ON);
135 cairo_set_font_options (cr, font_options);
137 CHECK_FONT_EXTENTS ("HINT_METRICS_ON");
139 cairo_move_to (cr, 1, font_extents.ascent - 1);
140 cairo_set_source_rgb (cr, 0.0, 0.0, 1.0); /* blue */
143 cairo_font_options_set_hint_style (font_options, CAIRO_HINT_STYLE_NONE);
144 cairo_set_font_options (cr, font_options);
145 CHECK_FONT_EXTENTS ("HINT_METRICS_ON HINT_STYLE_NONE");
146 cairo_show_text (cr, "the ");
148 cairo_font_options_set_hint_style (font_options, CAIRO_HINT_STYLE_SLIGHT);
149 cairo_set_font_options (cr, font_options);
150 CHECK_FONT_EXTENTS ("HINT_METRICS_ON HINT_STYLE_SLIGHT");
151 cairo_show_text (cr, "quick ");
153 cairo_font_options_set_hint_style (font_options, CAIRO_HINT_STYLE_MEDIUM);
154 cairo_set_font_options (cr, font_options);
155 CHECK_FONT_EXTENTS ("HINT_METRICS_ON HINT_STYLE_MEDIUM");
156 cairo_show_text (cr, "brown");
158 cairo_font_options_set_hint_style (font_options, CAIRO_HINT_STYLE_FULL);
159 cairo_set_font_options (cr, font_options);
160 CHECK_FONT_EXTENTS ("HINT_METRICS_ON HINT_STYLE_FULL");
161 cairo_show_text (cr, " fox");
163 /* Switch from show_text to text_path/fill to exercise bug #7889 */
164 cairo_text_path (cr, " jumps over a lazy dog");
165 cairo_fill (cr);
167 /* And test it rotated as well for the sake of bug #7888 */
169 cairo_translate (cr, width, height);
170 cairo_rotate (cr, M_PI);
172 cairo_font_options_set_hint_style (font_options, CAIRO_HINT_STYLE_DEFAULT);
173 cairo_font_options_set_hint_metrics (font_options, CAIRO_HINT_METRICS_OFF);
174 cairo_set_font_options (cr, font_options);
175 CHECK_FONT_EXTENTS ("HINT_METRICS_OFF");
177 cairo_move_to (cr, 1, font_extents.height - font_extents.descent - 1);
179 cairo_font_options_set_hint_style (font_options, CAIRO_HINT_STYLE_NONE);
180 cairo_set_font_options (cr, font_options);
181 CHECK_FONT_EXTENTS ("HINT_METRICS_OFF HINT_STYLE_NONE");
182 cairo_show_text (cr, "the ");
184 cairo_font_options_set_hint_style (font_options, CAIRO_HINT_STYLE_SLIGHT);
185 cairo_set_font_options (cr, font_options);
186 CHECK_FONT_EXTENTS ("HINT_METRICS_OFF HINT_STYLE_SLIGHT");
187 cairo_show_text (cr, "quick");
189 cairo_font_options_set_hint_style (font_options, CAIRO_HINT_STYLE_MEDIUM);
190 cairo_set_font_options (cr, font_options);
191 CHECK_FONT_EXTENTS ("HINT_METRICS_OFF HINT_STYLE_MEDIUM");
192 cairo_show_text (cr, " brown");
194 cairo_font_options_set_hint_style (font_options, CAIRO_HINT_STYLE_FULL);
195 cairo_set_font_options (cr, font_options);
196 CHECK_FONT_EXTENTS ("HINT_METRICS_OFF HINT_STYLE_FULL");
197 cairo_show_text (cr, " fox");
199 cairo_text_path (cr, " jumps over");
200 cairo_text_path (cr, " a lazy dog");
201 cairo_fill (cr);
203 cairo_font_options_destroy (font_options);
205 return CAIRO_TEST_SUCCESS;
208 CAIRO_TEST (bitmap_font,
209 "Test drawing with a font consisting only of bitmaps"
210 "\nThe PDF and PS backends embed a slightly distorted font for the rotated case.",
211 "text", /* keywords */
212 "ft", /* requirements */
213 246 + 1, 2 * TEXT_SIZE,
214 NULL, draw)