1
/*
2
 * Copyright © 2022 Adrian Johnson
3
 *
4
 * Permission is hereby granted, free of charge, to any person
5
 * obtaining a copy of this software and associated documentation
6
 * files (the "Software"), to deal in the Software without
7
 * restriction, including without limitation the rights to use, copy,
8
 * modify, merge, publish, distribute, sublicense, and/or sell copies
9
 * of the Software, and to permit persons to whom the Software is
10
 * furnished to do so, subject to the following conditions:
11
 *
12
 * The above copyright notice and this permission notice shall be
13
 * included in all copies or substantial portions of the Software.
14
 *
15
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
19
 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
20
 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
21
 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22
 * SOFTWARE.
23
 *
24
 * Author: Adrian Johnson <ajohnson@redneon.com>
25
 */
26

            
27
#include "cairo-test.h"
28
#include <cairo-ft.h>
29

            
30
#define FONT_SIZE 50
31
#define MARGIN 5
32
#define WIDTH  (FONT_SIZE*4 + MARGIN*2)
33
#define HEIGHT (FONT_SIZE*3 + MARGIN*5)
34

            
35
/* Check the full name to ensure we got an SVG font. */
36
#define FONT_FAMILY "Twitter Color Emoji"
37
#define FONT_FULLNAME "Twitter Color Emoji SVGinOT"
38

            
39
static const char spade_utf8[] =   { 0xe2, 0x99, 0xa0, 0x00 }; /* U+2660 glyph 87 */
40
static const char club_utf8[] =    { 0xe2, 0x99, 0xa3, 0x00 }; /* U+2663 glyph 88 */
41
static const char heart_utf8[] =   { 0xe2, 0x99, 0xa5, 0x00 }; /* U+2665 glyph 89 */
42
static const char diamond_utf8[] = { 0xe2, 0x99, 0xa6, 0x00 }; /* U+2666 glyph 90 */
43

            
44
static cairo_test_status_t
45
3
set_color_emoji_font (cairo_t *cr)
46
{
47
    cairo_font_options_t *font_options;
48
    cairo_font_face_t *font_face;
49
    FcPattern *pattern;
50
    FcPattern *resolved;
51
    FcChar8 *font_name;
52
    FcResult result;
53

            
54
3
    pattern = FcPatternCreate ();
55
3
    if (pattern == NULL)
56
	return CAIRO_TEST_NO_MEMORY;
57

            
58
3
    FcPatternAddString (pattern, FC_FAMILY, (FcChar8 *) FONT_FAMILY);
59
3
    FcConfigSubstitute (NULL, pattern, FcMatchPattern);
60

            
61
3
    font_options = cairo_font_options_create ();
62
3
    cairo_get_font_options (cr, font_options);
63
3
    cairo_ft_font_options_substitute (font_options, pattern);
64

            
65
3
    FcDefaultSubstitute (pattern);
66
3
    resolved = FcFontMatch (NULL, pattern, &result);
67
3
    if (resolved == NULL) {
68
	FcPatternDestroy (pattern);
69
	return CAIRO_TEST_NO_MEMORY;
70
    }
71

            
72
3
    if (FcPatternGetString (resolved, FC_FULLNAME, 0, &font_name) == FcResultMatch) {
73
3
        if (strcmp((char*)font_name, FONT_FULLNAME) != 0) {
74
3
            const cairo_test_context_t *ctx = cairo_test_get_context (cr);
75
3
            cairo_test_log (ctx, "Could not find %s font\n", FONT_FULLNAME);
76
3
            return CAIRO_TEST_UNTESTED;
77
        }
78
    } else {
79
        return CAIRO_TEST_FAILURE;
80
    }
81

            
82
    font_face = cairo_ft_font_face_create_for_pattern (resolved);
83
    cairo_set_font_face (cr, font_face);
84

            
85
    cairo_font_options_destroy (font_options);
86
    cairo_font_face_destroy (font_face);
87
    FcPatternDestroy (pattern);
88
    FcPatternDestroy (resolved);
89

            
90
    return CAIRO_TEST_SUCCESS;
91
}
92

            
93
static cairo_test_status_t
94
3
draw (cairo_t *cr, int width, int height)
95
{
96
    cairo_font_options_t *font_options;
97
    cairo_test_status_t result;
98

            
99
3
    cairo_set_source_rgb (cr, 1, 1, 1);
100
3
    cairo_paint (cr);
101
3
    cairo_set_source_rgb (cr, 0, 0, 0);
102

            
103
3
    result = set_color_emoji_font (cr);
104
3
    if (result != CAIRO_TEST_SUCCESS)
105
3
        return result;
106

            
107
    cairo_set_font_size (cr, FONT_SIZE);
108

            
109
    /* Color glyphs */
110
    cairo_move_to (cr, MARGIN, FONT_SIZE + MARGIN);
111
    cairo_show_text (cr, diamond_utf8);
112
    cairo_show_text (cr, club_utf8);
113
    cairo_show_text (cr, heart_utf8);
114
    cairo_show_text (cr, spade_utf8);
115

            
116
    /* Non-color glyphs */
117
    font_options = cairo_font_options_create ();
118
    cairo_font_options_set_color_mode (font_options, CAIRO_COLOR_MODE_NO_COLOR);
119
    cairo_set_font_options (cr, font_options);
120
    cairo_move_to (cr, MARGIN, FONT_SIZE*2 + MARGIN*2);
121
    cairo_show_text (cr, diamond_utf8);
122
    cairo_show_text (cr, club_utf8);
123
    cairo_show_text (cr, heart_utf8);
124
    cairo_show_text (cr, spade_utf8);
125

            
126
    /* Color glyph text path */
127
    cairo_font_options_set_color_mode (font_options, CAIRO_COLOR_MODE_COLOR);
128
    cairo_set_font_options (cr, font_options);
129
    cairo_font_options_destroy (font_options);
130
    cairo_move_to (cr, MARGIN, FONT_SIZE*3 + MARGIN*3);
131
    cairo_text_path (cr, diamond_utf8);
132
    cairo_text_path (cr, club_utf8);
133
    cairo_text_path (cr, heart_utf8);
134
    cairo_text_path (cr, spade_utf8);
135
    cairo_set_line_width (cr, 1);
136
    cairo_stroke (cr);
137

            
138
    return CAIRO_TEST_SUCCESS;
139
}
140

            
141
1
CAIRO_TEST (ft_svg_color_font,
142
	    "Test color font",
143
	    "svgrender", /* keywords */
144
	    NULL, /* requirements */
145
	    WIDTH, HEIGHT,
146
	    NULL, draw)