Viewport clipping now works with both GL and Gallium.
[cairo/gpu.git] / test / ft-text-antialias-none.c
bloba6ab41c5cd07c0e4652d537b6ac76b291c859f5e
1 /*
2 * Copyright © 2006 Jinghua Luo
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 * JINGHUA LUO 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: Jinghua Luo <sunmoon1997@gmail.com>
24 * Derived from:
25 * text-antialias-none.c,
26 * ft-font-create-for-ft-face.c.
27 * Original Author: Carl D. Worth <cworth@cworth.org>
29 #include "cairo-test.h"
30 #include <cairo-ft.h>
32 #define WIDTH 40
33 #define HEIGHT 30
34 #define TEXT_SIZE 12
36 static cairo_status_t
37 create_scaled_font (cairo_t * cr,
38 cairo_scaled_font_t **out)
40 FcPattern *pattern, *resolved;
41 FcResult result;
42 cairo_font_face_t *font_face;
43 cairo_scaled_font_t *scaled_font;
44 cairo_font_options_t *font_options;
45 cairo_matrix_t font_matrix, ctm;
46 cairo_status_t status;
47 double pixel_size;
49 font_options = cairo_font_options_create ();
51 cairo_get_font_options (cr, font_options);
53 pattern = FcPatternCreate ();
54 if (pattern == NULL)
55 return CAIRO_STATUS_NO_MEMORY;
57 FcPatternAddString (pattern, FC_FAMILY, (FcChar8 *)"Bitstream vera sans");
58 FcPatternAddDouble (pattern, FC_SIZE, TEXT_SIZE);
59 FcConfigSubstitute (NULL, pattern, FcMatchPattern);
61 cairo_ft_font_options_substitute (font_options, pattern);
63 FcDefaultSubstitute (pattern);
64 resolved = FcFontMatch (NULL, pattern, &result);
65 if (resolved == NULL) {
66 FcPatternDestroy (pattern);
67 return CAIRO_STATUS_NO_MEMORY;
70 /* turn antialiasing off */
71 FcPatternDel (resolved, FC_ANTIALIAS);
72 FcPatternAddBool (resolved, FC_ANTIALIAS, FcFalse);
74 FcPatternGetDouble (resolved, FC_PIXEL_SIZE, 0, &pixel_size);
76 font_face = cairo_ft_font_face_create_for_pattern (resolved);
78 cairo_matrix_init_identity (&font_matrix);
79 cairo_matrix_scale (&font_matrix, pixel_size, pixel_size);
81 cairo_get_matrix (cr, &ctm);
83 scaled_font = cairo_scaled_font_create (font_face,
84 &font_matrix,
85 &ctm,
86 font_options);
88 cairo_font_options_destroy (font_options);
89 cairo_font_face_destroy (font_face);
90 FcPatternDestroy (pattern);
91 FcPatternDestroy (resolved);
93 status = cairo_scaled_font_status (scaled_font);
94 if (status) {
95 cairo_scaled_font_destroy (scaled_font);
96 return status;
99 *out = scaled_font;
100 return CAIRO_STATUS_SUCCESS;
103 static cairo_test_status_t
104 draw (cairo_t *cr, int width, int height)
106 cairo_text_extents_t extents;
107 cairo_scaled_font_t *scaled_font;
108 cairo_status_t status;
109 const char black[] = "black", blue[] = "blue";
111 /* We draw in the default black, so paint white first. */
112 cairo_save (cr);
113 cairo_set_source_rgb (cr, 1.0, 1.0, 1.0); /* white */
114 cairo_paint (cr);
115 cairo_restore (cr);
117 status = create_scaled_font (cr, &scaled_font);
118 if (status) {
119 return cairo_test_status_from_status (cairo_test_get_context (cr),
120 status);
123 cairo_set_scaled_font (cr, scaled_font);
125 cairo_set_source_rgb (cr, 0, 0, 0); /* black */
126 cairo_text_extents (cr, black, &extents);
127 cairo_move_to (cr, -extents.x_bearing, -extents.y_bearing);
128 cairo_show_text (cr, black);
129 cairo_translate (cr, 0, -extents.y_bearing + 1);
131 cairo_set_source_rgb (cr, 0, 0, 1); /* blue */
132 cairo_text_extents (cr, blue, &extents);
133 cairo_move_to (cr, -extents.x_bearing, -extents.y_bearing);
134 cairo_show_text (cr, blue);
136 cairo_scaled_font_destroy (scaled_font);
138 return CAIRO_TEST_SUCCESS;
141 CAIRO_TEST (ft_text_antialias_none,
142 "Tests text rendering with no antialiasing",
143 "ft, text", /* keywords */
144 "target=raster", /* requirements */
145 WIDTH, HEIGHT,
146 NULL, draw)