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 GLYPH_SIZE 50
31
#define PAD 5
32
#define WIDTH  (4*(GLYPH_SIZE + PAD) + PAD)
33
#define HEIGHT WIDTH
34

            
35
//#define CLIP 1
36
#define LOG_EXTENTS 1
37

            
38
static cairo_test_status_t
39
21
draw_font (cairo_t *cr, int width, int height, const char *font_file)
40
{
41
    cairo_test_status_t result;
42
    char buf[10];
43
    cairo_text_extents_t extents;
44
    cairo_font_options_t *font_options;
45

            
46
21
    cairo_set_source_rgb (cr, 1, 1, 1);
47
21
    cairo_paint (cr);
48
21
    cairo_set_source_rgb (cr, 0, 0, 0);
49

            
50
21
    result = cairo_test_ft_select_font_from_file (cr, font_file);
51
21
    if (result)
52
        return result;
53

            
54
21
    font_options = cairo_font_options_create ();
55
21
    cairo_font_options_set_color_mode (font_options, CAIRO_COLOR_MODE_NO_COLOR);
56
//    cairo_set_font_options (cr, font_options);
57
21
    cairo_font_options_destroy (font_options);
58

            
59
21
    cairo_set_font_size (cr, GLYPH_SIZE);
60
105
    for (int i = 0; i < 4; i++) {
61
420
        for (int j = 0; j < 4; j++) {
62
336
            int x = j * (GLYPH_SIZE + PAD) + PAD;
63
336
            int y = i * (GLYPH_SIZE + PAD) + PAD;
64
336
            int glyph_number = 4*i + j;
65
336
            buf[0] = glyph_number < 10 ? '0' + glyph_number : 'A' + glyph_number - 10;
66
336
            buf[1] = 0;
67
336
            cairo_save (cr);
68
336
            cairo_text_extents (cr, buf, &extents);
69
#if LOG_EXTENTS
70
336
            cairo_test_log (cairo_test_get_context (cr),
71
                            "Char '%c' extents: x_bearing: %f  y_bearing: %f  width: %f  height: %f  x_advance: %f  y_advance: %f\n",
72
336
                            buf[0],
73
                            extents.x_bearing,
74
                            extents.y_bearing,
75
                            extents.width,
76
                            extents.height,
77
                            extents.x_advance,
78
                            extents.y_advance);
79
#endif
80
#if CLIP
81
            cairo_rectangle (cr, x, y, GLYPH_SIZE, GLYPH_SIZE);
82
            cairo_clip (cr);
83
#endif
84
336
            cairo_move_to (cr, x, y + GLYPH_SIZE);
85
336
            cairo_show_text (cr, buf);
86
336
            cairo_restore (cr);
87
336
            if (cairo_status (cr)) {
88
                cairo_test_log (cairo_test_get_context (cr),
89
                                "cairo_show_text() failed with \"%s\"\n",
90
                                buf);
91
                return CAIRO_TEST_FAILURE;
92
            }
93
        }
94
    }
95

            
96
21
    return CAIRO_TEST_SUCCESS;
97
}
98

            
99
#define DRAW_FUNC(name) \
100
static cairo_test_status_t \
101
draw_##name (cairo_t *cr, int width, int height) { \
102
    return draw_font (cr, width, height, "cairo-svg-test-" #name ".ttf"); \
103
}
104

            
105
3
DRAW_FUNC(doc)
106
1
CAIRO_TEST (ft_svg_render_doc,
107
	    "Test SVG glyph render",
108
	    "svgrender", /* keywords */
109
	    NULL, /* requirements */
110
	    WIDTH, HEIGHT,
111
	    NULL, draw_doc)
112

            
113
3
DRAW_FUNC(fill)
114
1
CAIRO_TEST (ft_svg_render_fill,
115
	    "Test SVG glyph render",
116
	    "svgrender", /* keywords */
117
	    NULL, /* requirements */
118
	    WIDTH, HEIGHT,
119
	    NULL, draw_fill)
120
    
121
3
DRAW_FUNC(gradient)
122
1
CAIRO_TEST (ft_svg_render_gradient,
123
	    "Test SVG glyph render",
124
	    "svgrender", /* keywords */
125
	    NULL, /* requirements */
126
	    WIDTH, HEIGHT,
127
	    NULL, draw_gradient)
128
    
129
3
DRAW_FUNC(path)
130
1
CAIRO_TEST (ft_svg_render_path,
131
	    "Test SVG glyph render",
132
	    "svgrender", /* keywords */
133
	    NULL, /* requirements */
134
	    WIDTH, HEIGHT,
135
	    NULL, draw_path)
136
    
137
3
DRAW_FUNC(shapes)
138
1
CAIRO_TEST (ft_svg_render_shapes,
139
	    "Test SVG glyph render",
140
	    "svgrender", /* keywords */
141
	    NULL, /* requirements */
142
	    WIDTH, HEIGHT,
143
	    NULL, draw_shapes)
144

            
145
3
DRAW_FUNC(stroke)
146
1
CAIRO_TEST (ft_svg_render_stroke,
147
	    "Test SVG glyph render",
148
	    "svgrender", /* keywords */
149
	    NULL, /* requirements */
150
	    WIDTH, HEIGHT,
151
	    NULL, draw_stroke)
152

            
153
3
DRAW_FUNC(transform)
154
1
CAIRO_TEST (ft_svg_render_transform,
155
	    "Test SVG glyph render",
156
	    "svgrender", /* keywords */
157
	    NULL, /* requirements */
158
	    WIDTH, HEIGHT,
159
	    NULL, draw_transform)