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 200
31
#define HEIGHT SIZE
32
#define WIDTH  (SIZE * 1.5)
33
#define FONT "Noto Sans"
34

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

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

            
50
3
    FcPatternAddString (pattern, FC_FAMILY, (FcChar8 *) FONT);
51
3
    FcPatternAddBool (pattern, FC_VARIABLE, TRUE);
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
3
    if (FcPatternGetBool (resolved, FC_VARIABLE, 0, &variable) == FcResultMatch) {
75
3
        if (!variable) {
76
            const cairo_test_context_t *ctx = cairo_test_get_context (cr);
77
            cairo_test_log (ctx, "Could not find %s font\n", FONT);
78
            return CAIRO_TEST_UNTESTED;
79
        }
80
    } else {
81
        return CAIRO_TEST_FAILURE;
82
    }
83

            
84
3
    font_face = cairo_ft_font_face_create_for_pattern (resolved);
85
3
    cairo_set_font_face (cr, font_face);
86

            
87
3
    cairo_font_options_destroy (font_options);
88
3
    cairo_font_face_destroy (font_face);
89
3
    FcPatternDestroy (pattern);
90
3
    FcPatternDestroy (resolved);
91

            
92
3
    return CAIRO_TEST_SUCCESS;
93
}
94

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

            
101
3
    cairo_set_source_rgb (cr, 1, 1, 1);
102
3
    cairo_paint (cr);
103
3
    cairo_set_source_rgb (cr, 0, 0, 0);
104

            
105
3
    result = set_variable_font (cr);
106
3
    if (result != CAIRO_TEST_SUCCESS)
107
        return result;
108

            
109
3
    font_options = cairo_font_options_create ();
110
3
    cairo_font_options_set_variations (font_options, "wght=700");
111
3
    cairo_set_font_options (cr, font_options);
112
3
    cairo_font_options_destroy (font_options);
113

            
114
3
    cairo_set_font_size (cr, SIZE/2);
115
3
    cairo_move_to (cr, SIZE/8, 0.7 * SIZE);
116

            
117
3
    cairo_show_text(cr, "Test");
118

            
119
3
    return CAIRO_TEST_SUCCESS;
120
}
121

            
122
1
CAIRO_TEST (ft_variable_font,
123
	    "Test variable font",
124
	    "ft, font", /* keywords */
125
	    NULL, /* requirements */
126
	    WIDTH, HEIGHT,
127
	    NULL, draw)