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

            
26
/* Test pdf-operators text positioning. This test is designed to expose rounding
27
 * errors in the PDF operations for relative positioning in very long strings.
28
 *
29
 * The text width is expected to match the width of the rectangle
30
 * enclosing the text.
31
 */
32

            
33
#include "cairo-test.h"
34

            
35
#define WIDTH  10000
36
#define HEIGHT 60
37

            
38
/* Using a non integer size helps expose rounding errors */
39
#define FONT_SIZE 10.12345678912345
40

            
41
#define WORD "Text"
42
#define NUM_WORDS 450
43

            
44
#define BORDER 10
45

            
46
static cairo_user_data_key_t font_face_key;
47

            
48
static cairo_status_t
49
3
user_font_init (cairo_scaled_font_t  *scaled_font,
50
                cairo_t              *cr,
51
                cairo_font_extents_t *metrics)
52
{
53
3
    cairo_font_face_t *font_face = cairo_font_face_get_user_data (cairo_scaled_font_get_font_face (scaled_font),
54
                                                                  &font_face_key);
55
3
    cairo_set_font_face (cr, font_face);
56
3
    cairo_font_extents (cr, metrics);
57

            
58
3
    return CAIRO_STATUS_SUCCESS;
59
}
60

            
61
static cairo_status_t
62
12
user_font_render_glyph (cairo_scaled_font_t  *scaled_font,
63
                        unsigned long         index,
64
                        cairo_t              *cr,
65
                        cairo_text_extents_t *metrics)
66
{
67
    char text[2];
68
12
    cairo_font_face_t *font_face = cairo_font_face_get_user_data (cairo_scaled_font_get_font_face (scaled_font),
69
                                                                  &font_face_key);
70

            
71
12
    text[0] = index; /* Only using ASCII for this test */
72
12
    text[1] = 0;
73
12
    cairo_set_font_face (cr, font_face);
74
12
    cairo_text_extents (cr, text, metrics);
75
12
    cairo_text_path (cr, text);
76
12
    cairo_fill (cr);
77
12
    return CAIRO_STATUS_SUCCESS;
78
}
79

            
80
static cairo_font_face_t *
81
3
create_user_font_face (cairo_font_face_t *orig_font)
82
{
83
    cairo_font_face_t *user_font_face;
84

            
85
3
    user_font_face = cairo_user_font_face_create ();
86
3
    cairo_user_font_face_set_init_func (user_font_face, user_font_init);
87
3
    cairo_user_font_face_set_render_glyph_func (user_font_face, user_font_render_glyph);
88
3
    cairo_font_face_set_user_data (user_font_face, &font_face_key, (void*) orig_font, NULL);
89
3
    return user_font_face;
90
}
91

            
92
static void
93
6
draw_text (cairo_t *cr, const char *text)
94
{
95
    cairo_text_extents_t extents;
96

            
97
6
    cairo_move_to (cr, BORDER, BORDER);
98
6
    cairo_set_source_rgb (cr, 0, 0, 0);
99

            
100
6
    cairo_show_text (cr, text);
101
6
    cairo_text_extents (cr, text,&extents);
102

            
103
6
    cairo_rectangle (cr,
104
6
                     BORDER + extents.x_bearing,
105
6
                     BORDER + extents.y_bearing,
106
                     extents.width,
107
                     extents.height);
108
6
    cairo_set_line_width (cr, 1);
109
6
    cairo_stroke (cr);
110
6
}
111

            
112

            
113
static cairo_test_status_t
114
3
draw (cairo_t *cr, int width, int height)
115
{
116
    int i;
117
    char *text;
118
    cairo_font_face_t *font_face;
119

            
120
3
    cairo_set_source_rgb (cr, 1, 1, 1);
121
3
    cairo_paint (cr);
122

            
123
3
    cairo_select_font_face (cr, "Dejavu Sans",
124
			    CAIRO_FONT_SLANT_NORMAL,
125
			    CAIRO_FONT_WEIGHT_NORMAL);
126

            
127
3
    cairo_set_font_size (cr, FONT_SIZE);
128

            
129
3
    text = malloc (strlen(WORD) * NUM_WORDS + 1);
130
3
    text[0] = '\0';
131
1353
    for (i = 0; i < NUM_WORDS; i++)
132
1350
	strcat (text, WORD);
133

            
134
3
    cairo_save (cr);
135
3
    cairo_translate (cr, BORDER, BORDER);
136
3
    draw_text (cr, text);
137
3
    cairo_restore (cr);
138

            
139
3
    font_face = create_user_font_face (cairo_get_font_face (cr));
140
3
    cairo_set_font_face (cr, font_face);
141
3
    cairo_font_face_destroy (font_face);
142
3
    cairo_set_font_size (cr, FONT_SIZE);
143

            
144
3
    cairo_save (cr);
145
3
    cairo_translate (cr, BORDER, BORDER*3);
146
3
    draw_text (cr, text);
147
3
    cairo_restore (cr);
148

            
149
3
    free (text);
150
3
    return CAIRO_TEST_SUCCESS;
151
}
152

            
153
1
CAIRO_TEST (pdf_operators_text,
154
	    "Test pdf-operators.c glyph positioning",
155
	    "pdf", /* keywords */
156
	    NULL, /* requirements */
157
	    WIDTH, HEIGHT,
158
	    NULL, draw)