1
/*
2
 * Copyright © 2008 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 TEXT_SIZE 12
31

            
32
typedef struct {
33
    cairo_glyph_t glyph_list[100];
34
    int num_glyphs;
35
    double x;
36
    double y;
37
} glyph_array_t;
38

            
39
static void
40
6
glyph_array_init (glyph_array_t *glyphs, double x, double y)
41
{
42
6
    glyphs->num_glyphs = 0;
43
6
    glyphs->x = x;
44
6
    glyphs->y = y;
45
6
}
46

            
47
static void
48
12
glyph_array_rel_move_to (glyph_array_t *glyphs, double x, double y)
49
{
50
12
    glyphs->x += x;
51
12
    glyphs->y += y;
52
12
}
53

            
54
static void
55
6
glyph_array_show (glyph_array_t *glyphs, cairo_t *cr)
56
{
57
6
    cairo_show_glyphs (cr, glyphs->glyph_list, glyphs->num_glyphs);
58
6
}
59

            
60
#define DOUBLE_FROM_26_6(t) ((double)(t) / 64.0)
61

            
62
static cairo_status_t
63
18
glyph_array_add_text(glyph_array_t *glyphs, cairo_t *cr, const char *s, double spacing)
64
{
65
    cairo_scaled_font_t *scaled_font;
66
    cairo_status_t status;
67
    FT_Face face;
68
    unsigned long charcode;
69
    unsigned int index;
70
    cairo_text_extents_t extents;
71
    const char *p;
72
    FT_Vector kerning;
73
    double kern_x;
74
18
    int first = TRUE;
75

            
76
18
    scaled_font = cairo_get_scaled_font (cr);
77
18
    status = cairo_scaled_font_status (scaled_font);
78
18
    if (status)
79
	return status;
80

            
81
18
    face = cairo_ft_scaled_font_lock_face (scaled_font);
82
18
    if (face == NULL)
83
	return CAIRO_STATUS_FONT_TYPE_MISMATCH;
84

            
85
18
    p = s;
86
174
    while (*p)
87
    {
88
156
        charcode = *p;
89
156
        index = FT_Get_Char_Index (face, charcode);
90
156
        glyphs->glyph_list[glyphs->num_glyphs].index = index;
91
156
        if (first) {
92
18
            first = FALSE;
93
18
            glyphs->glyph_list[glyphs->num_glyphs].x = glyphs->x;
94
18
            glyphs->glyph_list[glyphs->num_glyphs].y = glyphs->y;
95
        } else {
96
138
            cairo_glyph_extents (cr, &glyphs->glyph_list[glyphs->num_glyphs - 1], 1, &extents);
97
138
            FT_Get_Kerning (face,
98
138
                            glyphs->glyph_list[glyphs->num_glyphs - 1].index,
99
138
                            glyphs->glyph_list[glyphs->num_glyphs].index,
100
                            FT_KERNING_UNSCALED,
101
                            &kerning);
102
138
            kern_x = DOUBLE_FROM_26_6(kerning.x);
103
138
            glyphs->glyph_list[glyphs->num_glyphs].x =
104
138
		glyphs->glyph_list[glyphs->num_glyphs - 1].x + extents.x_advance + kern_x + spacing;
105
138
            glyphs->glyph_list[glyphs->num_glyphs].y =
106
138
		glyphs->glyph_list[glyphs->num_glyphs - 1].y + extents.y_advance;
107
	}
108

            
109
156
	cairo_glyph_extents (cr, &glyphs->glyph_list[glyphs->num_glyphs], 1, &extents);
110
156
	glyphs->x = glyphs->glyph_list[glyphs->num_glyphs].x + extents.x_advance + spacing;
111
156
	glyphs->y = glyphs->glyph_list[glyphs->num_glyphs].y + extents.y_advance;
112
156
	p++;
113
156
        glyphs->num_glyphs++;
114
    }
115

            
116
18
    cairo_ft_scaled_font_unlock_face (scaled_font);
117
18
    return CAIRO_STATUS_SUCCESS;
118
}
119

            
120
static cairo_test_status_t
121
3
draw (cairo_t *cr, int width, int height)
122
{
123
3
    const cairo_test_context_t *ctx = cairo_test_get_context (cr);
124
    glyph_array_t glyphs;
125
    cairo_font_options_t *font_options;
126
    cairo_status_t status;
127

            
128
    /* paint white so we don't need separate ref images for
129
     * RGB24 and ARGB32 */
130
3
    cairo_set_source_rgb (cr, 1.0, 1.0, 1.0);
131
3
    cairo_paint (cr);
132

            
133
3
    cairo_select_font_face (cr, CAIRO_TEST_FONT_FAMILY " Sans",
134
			    CAIRO_FONT_SLANT_NORMAL,
135
			    CAIRO_FONT_WEIGHT_NORMAL);
136
3
    cairo_set_font_size (cr, TEXT_SIZE);
137

            
138
3
    font_options = cairo_font_options_create ();
139
3
    cairo_get_font_options (cr, font_options);
140
3
    cairo_font_options_set_hint_metrics (font_options, CAIRO_HINT_METRICS_OFF);
141
3
    cairo_set_font_options (cr, font_options);
142
3
    cairo_font_options_destroy (font_options);
143

            
144
3
    cairo_set_source_rgb (cr, 0.0, 0.0, 0.0);
145

            
146
3
    glyph_array_init (&glyphs, 1, TEXT_SIZE);
147

            
148
3
    status = glyph_array_add_text(&glyphs, cr, "AWAY again", 0.0);
149
3
    if (status)
150
	return cairo_test_status_from_status (ctx, status);
151

            
152
3
    glyph_array_rel_move_to (&glyphs, TEXT_SIZE*1, 0.0);
153
3
    status = glyph_array_add_text(&glyphs, cr, "character space", TEXT_SIZE*0.3);
154
3
    if (status)
155
	return cairo_test_status_from_status (ctx, status);
156

            
157
3
    glyph_array_show (&glyphs, cr);
158

            
159

            
160
3
    glyph_array_init (&glyphs, 1, TEXT_SIZE*2 + 4);
161

            
162
3
    status = glyph_array_add_text(&glyphs, cr, "Increasing", 0.0);
163
3
    if (status)
164
	return cairo_test_status_from_status (ctx, status);
165

            
166
3
    glyph_array_rel_move_to (&glyphs, TEXT_SIZE*0.5, 0.0);
167
3
    status = glyph_array_add_text(&glyphs, cr, "space", 0.0);
168
3
    if (status)
169
	return cairo_test_status_from_status (ctx, status);
170

            
171
3
    glyph_array_rel_move_to (&glyphs, TEXT_SIZE*1.0, 0.0);
172
3
    status = glyph_array_add_text(&glyphs, cr, "between", 0.0);
173
3
    if (status)
174
	return cairo_test_status_from_status (ctx, status);
175

            
176
3
    glyph_array_rel_move_to (&glyphs, TEXT_SIZE*1.5, 0.0);
177
3
    status = glyph_array_add_text(&glyphs, cr, "words", 0.0);
178
3
    if (status)
179
	return cairo_test_status_from_status (ctx, status);
180

            
181
3
    glyph_array_show (&glyphs, cr);
182

            
183
3
    return CAIRO_TEST_SUCCESS;
184
}
185

            
186
1
CAIRO_TEST (ft_show_glyphs_positioning,
187
	    "Test that the PS/PDF glyph positioning optimizations are correct",
188
	    "ft, text", /* keywords */
189
	    NULL, /* requirements */
190
	    235, (TEXT_SIZE + 4)*2,
191
	    NULL, draw)