1
/*
2
 * Copyright © 2006, 2008 Red Hat, Inc.
3
 * Copyright © 2021 Adrian Johnson
4
 *
5
 * Permission to use, copy, modify, distribute, and sell this software
6
 * and its documentation for any purpose is hereby granted without
7
 * fee, provided that the above copyright notice appear in all copies
8
 * and that both that copyright notice and this permission notice
9
 * appear in supporting documentation, and that the name of
10
 * Red Hat, Inc. not be used in advertising or publicity pertaining to
11
 * distribution of the software without specific, written prior
12
 * permission. Red Hat, Inc. makes no representations about the
13
 * suitability of this software for any purpose.  It is provided "as
14
 * is" without express or implied warranty.
15
 *
16
 * RED HAT, INC. DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS
17
 * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
18
 * FITNESS, IN NO EVENT SHALL RED HAT, INC. BE LIABLE FOR ANY SPECIAL,
19
 * INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
20
 * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
21
 * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR
22
 * IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
23
 *
24
 * Contributor(s):
25
 *	Kristian Høgsberg <krh@redhat.com>
26
 *	Behdad Esfahbod <behdad@behdad.org>
27
 */
28

            
29
/* Test that user-fonts can handle color and non-color glyphs in the
30
 * same font.
31
 */
32

            
33
#include "cairo-test.h"
34

            
35
#include <ctype.h>
36
#include <stdlib.h>
37
#include <stdio.h>
38

            
39

            
40
#define BORDER 10
41
#define TEXT_SIZE 64
42
#define WIDTH  (TEXT_SIZE * 11 + 2*BORDER)
43
#define HEIGHT (4*TEXT_SIZE + 5*BORDER)
44

            
45
#define TEXT          "abcdefghij"
46

            
47
/* These characters will be drawn twice with a different foreground color */
48
#define FG_TEXT       "acfh"
49

            
50
/* Uppercase draws the same text but forces the use of the non-color
51
 * render callback */
52
#define TEXT_NO_COLOR    "ABCDEFGHIJ"
53
#define FG_TEXT_NO_COLOR "ACFH"
54

            
55
#define TEXT_PATH       "aabccdeffghhij"
56

            
57

            
58
static cairo_status_t
59
6
test_scaled_font_init (cairo_scaled_font_t  *scaled_font,
60
		       cairo_t              *cr,
61
		       cairo_font_extents_t *metrics)
62
{
63
6
    metrics->ascent  = .75;
64
6
    metrics->descent = .25;
65
6
    return CAIRO_STATUS_SUCCESS;
66
}
67

            
68
static void
69
48
render_glyph_solid (cairo_t *cr,
70
                    double width,
71
                    double height,
72
                    cairo_bool_t color,
73
                    cairo_scaled_font_t *scaled_font)
74
{
75
48
    if (color)
76
12
        cairo_set_source_rgba (cr, 0.7, 0.2, 0.1, 0.9);
77
48
    cairo_rectangle (cr, 0, 0, width/2, height/2);
78
48
    cairo_fill (cr);
79

            
80
48
    if (color) {
81
12
        if (scaled_font)
82
6
            cairo_set_source (cr, cairo_user_scaled_font_get_foreground_marker (scaled_font));
83
        else
84
6
            cairo_set_source_rgba (cr, 0.2, 0.5, 0.3, 0.9);
85
    }
86
48
    cairo_rectangle (cr, width/4, height/4, width/2, height/2);
87
48
    cairo_fill (cr);
88

            
89
48
    if (color)
90
12
        cairo_set_source_rgba (cr, 0.2, 0.3, 0.5, 0.9);
91
48
    cairo_rectangle (cr, width/2, height/2, width/2, height/2);
92
48
    cairo_fill (cr);
93
48
}
94

            
95
static void
96
60
render_glyph_linear (cairo_t *cr,
97
                     double width,
98
                     double height,
99
                     cairo_bool_t color,
100
                     cairo_scaled_font_t *scaled_font)
101
{
102
    cairo_pattern_t *pat;
103
    cairo_pattern_t *fg;
104

            
105
60
    pat = cairo_pattern_create_linear (0.0, 0.0, width, height);
106
60
    if (scaled_font) {
107
        double r, g, b, a;
108

            
109
36
        fg = cairo_user_scaled_font_get_foreground_source (scaled_font);
110
36
        if (cairo_pattern_get_rgba (fg, &r, &g, &b, &a) != CAIRO_STATUS_SUCCESS) {
111
            r = g = b = 0;
112
            a = 1;
113
        }
114
36
        cairo_pattern_add_color_stop_rgba (pat, 0,  r, g, b, a);
115
36
        cairo_pattern_add_color_stop_rgb  (pat, 1,  0, 0, 1);
116
    } else {
117
24
        cairo_pattern_add_color_stop_rgb (pat, 0,   1, 0.4, 0.2);
118
24
        cairo_pattern_add_color_stop_rgb (pat, 0.5, 0.2, 1, 0.4);
119
24
        cairo_pattern_add_color_stop_rgb (pat, 1,   0.2, 0.3, 1);
120
    }
121

            
122
60
    cairo_set_source (cr, pat);
123
60
    cairo_rectangle (cr, 0, 0, width, height);
124
60
    cairo_fill (cr);
125
60
}
126

            
127
static void
128
24
render_glyph_text (cairo_t *cr, double width, double height, cairo_bool_t color)
129
{
130
24
    cairo_select_font_face (cr, CAIRO_TEST_FONT_FAMILY " Sans",
131
			    CAIRO_FONT_SLANT_NORMAL,
132
			    CAIRO_FONT_WEIGHT_NORMAL);
133
24
    cairo_set_font_size(cr, 0.5);
134

            
135
24
    if (color)
136
6
        cairo_set_source_rgb (cr, 0.5, 0.7, 0);
137
24
    cairo_move_to (cr, width*0.1, height/2);
138
24
    cairo_show_text (cr, "a");
139

            
140
24
    if (color)
141
6
        cairo_set_source_rgb (cr, 0, 0.5, 0.7);
142
24
    cairo_move_to (cr, width*0.4, height*0.9);
143
24
    cairo_show_text (cr, "z");
144
24
}
145

            
146
static cairo_status_t
147
132
test_scaled_font_render_glyph_common (cairo_scaled_font_t  *scaled_font,
148
                                      unsigned long         glyph,
149
                                      cairo_t              *cr,
150
                                      cairo_text_extents_t *metrics,
151
                                      cairo_bool_t          color)
152
{
153
132
    double width = 0.5;
154
132
    double height = 0.8;
155

            
156
132
    metrics->x_advance = 0.75;
157
132
    cairo_translate (cr,  0.125, -0.6);
158
132
    switch (glyph) {
159
12
        case 'a':
160
12
            render_glyph_solid (cr, width, height, color, scaled_font);
161
12
            break;
162
12
        case 'b':
163
12
            render_glyph_solid (cr, width, height, color, NULL);
164
12
            break;
165
18
        case 'c':
166
18
            render_glyph_linear (cr, width, height, color, scaled_font);
167
18
            break;
168
12
        case 'd':
169
12
            render_glyph_linear (cr, width, height, color, NULL);
170
12
            break;
171
12
        case 'e':
172
12
            render_glyph_text (cr, width, height, color);
173
12
            break;
174
12
        case 'f':
175
12
            cairo_push_group (cr);
176
12
            render_glyph_solid (cr, width, height, color, scaled_font);
177
12
            cairo_pop_group_to_source (cr);
178
12
            cairo_paint (cr);
179
12
            break;
180
12
        case 'g':
181
12
            cairo_push_group (cr);
182
12
            render_glyph_solid (cr, width, height, color, NULL);
183
12
            cairo_pop_group_to_source (cr);
184
12
            cairo_paint (cr);
185
12
            break;
186
18
        case 'h':
187
18
            cairo_push_group (cr);
188
18
            render_glyph_linear (cr, width, height, color, scaled_font);
189
18
            cairo_pop_group_to_source (cr);
190
18
            cairo_paint (cr);
191
18
            break;
192
12
        case 'i':
193
12
            cairo_push_group (cr);
194
12
            render_glyph_linear (cr, width, height, color, NULL);
195
12
            cairo_pop_group_to_source (cr);
196
12
            cairo_paint (cr);
197
12
            break;
198
12
        case 'j':
199
12
            cairo_push_group (cr);
200
12
            render_glyph_text (cr, width, height, color);
201
12
            cairo_pop_group_to_source (cr);
202
12
            cairo_paint (cr);
203
12
            break;
204
    }
205

            
206
132
    return CAIRO_STATUS_SUCCESS;
207
}
208

            
209
static cairo_status_t
210
102
test_scaled_font_render_color_glyph_callback (cairo_scaled_font_t  *scaled_font,
211
                                     unsigned long         glyph,
212
                                     cairo_t              *cr,
213
                                     cairo_text_extents_t *metrics)
214
{
215
102
    if (isupper(glyph))
216
60
        return CAIRO_STATUS_USER_FONT_NOT_IMPLEMENTED;
217

            
218
42
    return test_scaled_font_render_glyph_common (scaled_font, glyph, cr, metrics, TRUE);
219
}
220

            
221
static cairo_status_t
222
90
test_scaled_font_render_glyph_callback (cairo_scaled_font_t  *scaled_font,
223
                                        unsigned long         glyph,
224
                                        cairo_t              *cr,
225
                                        cairo_text_extents_t *metrics)
226
{
227
90
    int c = glyph;
228
90
    if (isupper(c))
229
60
        c = tolower(c);
230

            
231
90
    return test_scaled_font_render_glyph_common (scaled_font, c, cr, metrics, FALSE);
232
}
233

            
234
static cairo_status_t
235
3
_user_font_face_create (cairo_font_face_t **out)
236
{
237

            
238
    cairo_font_face_t *user_font_face;
239

            
240
3
    user_font_face = cairo_user_font_face_create ();
241
3
    cairo_user_font_face_set_init_func (user_font_face, test_scaled_font_init);
242
3
    cairo_user_font_face_set_render_color_glyph_func (user_font_face,
243
                                                      test_scaled_font_render_color_glyph_callback);
244
3
    cairo_user_font_face_set_render_glyph_func (user_font_face,
245
                                                test_scaled_font_render_glyph_callback);
246

            
247
3
    *out = user_font_face;
248
3
    return CAIRO_STATUS_SUCCESS;
249
}
250

            
251
/* Any text characters that are in fg_text will be drawn with a different color */
252
static void
253
9
draw_line (cairo_t *cr, const char *text, const char *fg_text)
254
{
255
    char buf[10];
256

            
257
99
    for (unsigned i = 0; i < strlen(text); i++) {
258
90
        buf[0] = text[i];
259
90
        buf[1] = 0;
260

            
261
90
        if (strchr (fg_text, text[i])) {
262
36
            cairo_set_source_rgb (cr, 1, 0, 0);
263
36
            cairo_show_text (cr, buf);
264
        }
265

            
266
90
        cairo_set_source_rgb (cr, 0, 1, 0);
267
90
        cairo_show_text (cr, buf);
268
    }
269
9
}
270

            
271
static cairo_test_status_t
272
3
draw (cairo_t *cr, int width, int height)
273
{
274
    cairo_font_face_t *font_face;
275
3
    const char text[] = TEXT;
276
    cairo_font_extents_t font_extents;
277
    cairo_text_extents_t extents;
278
    cairo_status_t status;
279
    cairo_font_options_t *font_options;
280

            
281
3
    cairo_set_source_rgb (cr, 1, 1, 1);
282
3
    cairo_paint (cr);
283

            
284
3
    status = _user_font_face_create (&font_face);
285
3
    if (status) {
286
	return cairo_test_status_from_status (cairo_test_get_context (cr),
287
					      status);
288
    }
289

            
290
3
    cairo_set_font_face (cr, font_face);
291
3
    cairo_font_face_destroy (font_face);
292

            
293
3
    cairo_set_font_size (cr, TEXT_SIZE);
294

            
295
3
    cairo_font_extents (cr, &font_extents);
296
3
    cairo_text_extents (cr, text, &extents);
297

            
298
    /* logical boundaries in red */
299
3
    cairo_move_to (cr, 0, BORDER);
300
3
    cairo_rel_line_to (cr, WIDTH, 0);
301
3
    cairo_move_to (cr, 0, BORDER + font_extents.ascent);
302
3
    cairo_rel_line_to (cr, WIDTH, 0);
303
3
    cairo_move_to (cr, 0, BORDER + font_extents.ascent + font_extents.descent);
304
3
    cairo_rel_line_to (cr, WIDTH, 0);
305
3
    cairo_move_to (cr, BORDER, 0);
306
3
    cairo_rel_line_to (cr, 0, 2*BORDER + TEXT_SIZE);
307
3
    cairo_move_to (cr, BORDER + extents.x_advance, 0);
308
3
    cairo_rel_line_to (cr, 0, 2*BORDER + TEXT_SIZE);
309
3
    cairo_set_source_rgb (cr, 1, 0, 0);
310
3
    cairo_set_line_width (cr, 2);
311
3
    cairo_stroke (cr);
312

            
313
    /* ink boundaries in green */
314
3
    cairo_rectangle (cr,
315
3
		     BORDER + extents.x_bearing, BORDER + font_extents.ascent + extents.y_bearing,
316
		     extents.width, extents.height);
317
3
    cairo_set_source_rgb (cr, 0, 1, 0);
318
3
    cairo_set_line_width (cr, 2);
319
3
    cairo_stroke (cr);
320

            
321
    /* Line 1: text in color */
322
3
    cairo_move_to (cr, BORDER, BORDER + font_extents.ascent);
323
3
    draw_line (cr, TEXT, FG_TEXT);
324

            
325
    /* Line 2: text in non-color (color render callback returns
326
     * CAIRO_STATUS_USER_FONT_NOT_IMPLEMENTED.
327
     */
328
3
    cairo_move_to (cr, BORDER, BORDER + font_extents.height + 1*BORDER + font_extents.ascent);
329
3
    draw_line (cr, TEXT_NO_COLOR, FG_TEXT_NO_COLOR);
330

            
331
    /* Line 3: Filled version of color text in blue */
332
3
    cairo_move_to (cr, BORDER, BORDER + 2*font_extents.height + 2*BORDER + font_extents.ascent);
333
3
    cairo_set_source_rgb (cr, 0, 0, 1);
334
3
    cairo_text_path (cr, TEXT_PATH);
335
3
    cairo_fill (cr);
336

            
337
    /* Line 4: color glyphs with CAIRO_COLOR_MODE_NO_COLOR font option. */
338
3
    font_options = cairo_font_options_create ();
339
3
    cairo_get_font_options (cr, font_options);
340
3
    cairo_font_options_set_color_mode (font_options, CAIRO_COLOR_MODE_NO_COLOR);
341
3
    cairo_set_font_options (cr, font_options);
342
3
    cairo_font_options_destroy (font_options);
343

            
344
3
    cairo_move_to (cr, BORDER, BORDER + 3*font_extents.height + 3*BORDER + font_extents.ascent);
345
3
    draw_line (cr, TEXT, FG_TEXT);
346

            
347
3
    return CAIRO_TEST_SUCCESS;
348
}
349

            
350
1
CAIRO_TEST (user_font_color,
351
	    "Tests user font color feature",
352
	    "font, user-font", /* keywords */
353
	    "cairo >= 1.17.4", /* requirements */
354
	    WIDTH, HEIGHT,
355
	    NULL, draw)