summaryrefslogtreecommitdiff
path: root/test/pdf-operators-text.c
blob: 6d654d1158f9670c8a00a2e9835cb29586e302cf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
/*
 * Copyright © 2021 Adrian Johnson
 *
 * Permission to use, copy, modify, distribute, and sell this software
 * and its documentation for any purpose is hereby granted without
 * fee, provided that the above copyright notice appear in all copies
 * and that both that copyright notice and this permission notice
 * appear in supporting documentation, and that the name of
 * Red Hat, Inc. not be used in advertising or publicity pertaining to
 * distribution of the software without specific, written prior
 * permission. Red Hat, Inc. makes no representations about the
 * suitability of this software for any purpose.  It is provided "as
 * is" without express or implied warranty.
 *
 * RED HAT, INC. DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS
 * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
 * FITNESS, IN NO EVENT SHALL RED HAT, INC. BE LIABLE FOR ANY SPECIAL,
 * INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
 * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
 * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR
 * IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 *
 * Author: Adrian Johnson <ajohnson@redneon.com>
 */

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

#include "cairo-test.h"

#define WIDTH  10000
#define HEIGHT 60

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

#define WORD "Text"
#define NUM_WORDS 450

#define BORDER 10

static cairo_user_data_key_t font_face_key;

static cairo_status_t
user_font_init (cairo_scaled_font_t  *scaled_font,
                cairo_t              *cr,
                cairo_font_extents_t *metrics)
{
    cairo_font_face_t *font_face = cairo_font_face_get_user_data (cairo_scaled_font_get_font_face (scaled_font),
                                                                  &font_face_key);
    cairo_set_font_face (cr, font_face);
    cairo_font_extents (cr, metrics);

    return CAIRO_STATUS_SUCCESS;
}

static cairo_status_t
user_font_render_glyph (cairo_scaled_font_t  *scaled_font,
                        unsigned long         index,
                        cairo_t              *cr,
                        cairo_text_extents_t *metrics)
{
    char text[2];
    cairo_font_face_t *font_face = cairo_font_face_get_user_data (cairo_scaled_font_get_font_face (scaled_font),
                                                                  &font_face_key);

    text[0] = index; /* Only using ASCII for this test */
    text[1] = 0;
    cairo_set_font_face (cr, font_face);
    cairo_text_extents (cr, text, metrics);
    cairo_text_path (cr, text);
    cairo_fill (cr);
    return CAIRO_STATUS_SUCCESS;
}

static cairo_font_face_t *
create_user_font_face (cairo_font_face_t *orig_font)
{
    cairo_font_face_t *user_font_face;

    user_font_face = cairo_user_font_face_create ();
    cairo_user_font_face_set_init_func (user_font_face, user_font_init);
    cairo_user_font_face_set_render_glyph_func (user_font_face, user_font_render_glyph);
    cairo_font_face_set_user_data (user_font_face, &font_face_key, (void*) orig_font, NULL);
    return user_font_face;
}

static void
draw_text (cairo_t *cr, const char *text)
{
    cairo_text_extents_t extents;

    cairo_move_to (cr, BORDER, BORDER);
    cairo_set_source_rgb (cr, 0, 0, 0);

    cairo_show_text (cr, text);
    cairo_text_extents (cr, text,&extents);

    cairo_rectangle (cr,
                     BORDER + extents.x_bearing,
                     BORDER + extents.y_bearing,
                     extents.width,
                     extents.height);
    cairo_set_line_width (cr, 1);
    cairo_stroke (cr);
}


static cairo_test_status_t
draw (cairo_t *cr, int width, int height)
{
    int i;
    char *text;
    cairo_font_face_t *font_face;

    cairo_set_source_rgb (cr, 1, 1, 1);
    cairo_paint (cr);

    cairo_select_font_face (cr, "Dejavu Sans",
			    CAIRO_FONT_SLANT_NORMAL,
			    CAIRO_FONT_WEIGHT_NORMAL);

    cairo_set_font_size (cr, FONT_SIZE);

    text = malloc (strlen(WORD) * NUM_WORDS + 1);
    text[0] = '\0';
    for (i = 0; i < NUM_WORDS; i++)
	strcat (text, WORD);

    cairo_save (cr);
    cairo_translate (cr, BORDER, BORDER);
    draw_text (cr, text);
    cairo_restore (cr);

    font_face = create_user_font_face (cairo_get_font_face (cr));
    cairo_set_font_face (cr, font_face);
    cairo_font_face_destroy (font_face);
    cairo_set_font_size (cr, FONT_SIZE);

    cairo_save (cr);
    cairo_translate (cr, BORDER, BORDER*3);
    draw_text (cr, text);
    cairo_restore (cr);

    free (text);
    return CAIRO_TEST_SUCCESS;
}

CAIRO_TEST (pdf_operators_text,
	    "Test pdf-operators.c glyph positioning",
	    "pdf", /* keywords */
	    NULL, /* requirements */
	    WIDTH, HEIGHT,
	    NULL, draw)