1
/*
2
 * Copyright © 2021 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 SIZE 40
31
#define HEIGHT SIZE
32
#define WIDTH  (SIZE * 1.5)
33
#define FONT "Noto Color Emoji"
34

            
35
static const char smiley_face_utf8[] = { 0xf0, 0x9f, 0x99, 0x82, 0x00 }; /* U+1F642 */
36

            
37
static cairo_test_status_t
38
3
set_color_emoji_font (cairo_t *cr)
39
{
40
    cairo_font_options_t *font_options;
41
    cairo_font_face_t *font_face;
42
    FcPattern *pattern;
43
    FcPattern *resolved;
44
    FcChar8 *font_name;
45
    FcResult result;
46

            
47
3
    pattern = FcPatternCreate ();
48
3
    if (pattern == NULL)
49
	return CAIRO_TEST_NO_MEMORY;
50

            
51
3
    FcPatternAddString (pattern, FC_FAMILY, (FcChar8 *) FONT);
52
3
    FcConfigSubstitute (NULL, pattern, FcMatchPattern);
53

            
54
3
    font_options = cairo_font_options_create ();
55
3
    cairo_get_font_options (cr, font_options);
56
3
    cairo_ft_font_options_substitute (font_options, pattern);
57

            
58
3
    FcDefaultSubstitute (pattern);
59
3
    resolved = FcFontMatch (NULL, pattern, &result);
60
3
    if (resolved == NULL) {
61
	FcPatternDestroy (pattern);
62
	return CAIRO_TEST_NO_MEMORY;
63
    }
64

            
65
3
    if (FcPatternGetString (resolved, FC_FAMILY, 0, &font_name) == FcResultMatch) {
66
3
        if (strcmp((char*)font_name, FONT) != 0) {
67
            const cairo_test_context_t *ctx = cairo_test_get_context (cr);
68
            cairo_test_log (ctx, "Could not find %s font\n", FONT);
69
            return CAIRO_TEST_UNTESTED;
70
        }
71
    } else {
72
        return CAIRO_TEST_FAILURE;
73
    }
74

            
75
3
    font_face = cairo_ft_font_face_create_for_pattern (resolved);
76
3
    cairo_set_font_face (cr, font_face);
77

            
78
3
    cairo_font_options_destroy (font_options);
79
3
    cairo_font_face_destroy (font_face);
80
3
    FcPatternDestroy (pattern);
81
3
    FcPatternDestroy (resolved);
82

            
83
3
    return CAIRO_TEST_SUCCESS;
84
}
85

            
86
static cairo_test_status_t
87
3
draw (cairo_t *cr, int width, int height)
88
{
89
    cairo_test_status_t result;
90
    cairo_font_options_t *font_options;
91

            
92
3
    result = set_color_emoji_font (cr);
93
3
    if (result != CAIRO_TEST_SUCCESS)
94
        return result;
95

            
96
3
    cairo_set_font_size (cr, SIZE/2);
97
3
    cairo_move_to (cr, SIZE/8, 0.7 * SIZE);
98

            
99
3
    cairo_show_text(cr, smiley_face_utf8);
100

            
101
    /* Show that the color mode font option can disable color rendering */
102
3
    font_options = cairo_font_options_create ();
103
3
    cairo_get_font_options (cr, font_options);
104
3
    cairo_font_options_set_color_mode (font_options, CAIRO_COLOR_MODE_NO_COLOR);
105
3
    cairo_set_font_options (cr, font_options);
106
3
    cairo_font_options_destroy (font_options);
107

            
108
3
    cairo_show_text(cr, smiley_face_utf8);
109

            
110
3
    return CAIRO_TEST_SUCCESS;
111
}
112

            
113
1
CAIRO_TEST (ft_color_font,
114
	    "Test color font",
115
	    "ft, font", /* keywords */
116
	    NULL, /* requirements */
117
	    WIDTH, HEIGHT,
118
	    NULL, draw)