1
/*
2
 * Copyright © 2006 Jinghua Luo
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
 * JINGHUA LUO 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: Jinghua Luo <sunmoon1997@gmail.com>
24
 * Derived from:
25
 *  text-antialias-none.c,
26
 *  ft-font-create-for-ft-face.c.
27
 * Original Author: Carl D. Worth <cworth@cworth.org>
28
 */
29
#include "cairo-test.h"
30
#include <cairo-ft.h>
31

            
32
#define WIDTH  80
33
#define HEIGHT 240
34
#define TEXT_SIZE 30
35

            
36
static cairo_status_t
37
3
create_scaled_font (cairo_t * cr,
38
		    cairo_scaled_font_t **out)
39
{
40
    FcPattern *pattern, *resolved;
41
    FcResult result;
42
    cairo_font_face_t *font_face;
43
    cairo_scaled_font_t *scaled_font;
44
    cairo_font_options_t *font_options;
45
    cairo_matrix_t font_matrix, ctm;
46
    cairo_status_t status;
47
    double pixel_size;
48

            
49
3
    font_options = cairo_font_options_create ();
50

            
51
3
    cairo_get_font_options (cr, font_options);
52

            
53
3
    pattern = FcPatternCreate ();
54
3
    if (pattern == NULL)
55
	return CAIRO_STATUS_NO_MEMORY;
56

            
57
3
    FcPatternAddString (pattern, FC_FAMILY, (FcChar8 *)"Nimbus Sans L");
58
3
    FcPatternAddDouble (pattern, FC_PIXEL_SIZE, TEXT_SIZE);
59
3
    FcConfigSubstitute (NULL, pattern, FcMatchPattern);
60

            
61
3
    cairo_ft_font_options_substitute (font_options, pattern);
62

            
63
3
    FcDefaultSubstitute (pattern);
64
3
    resolved = FcFontMatch (NULL, pattern, &result);
65
3
    if (resolved == NULL) {
66
	FcPatternDestroy (pattern);
67
	return CAIRO_STATUS_NO_MEMORY;
68
    }
69

            
70
    /* set layout to vertical */
71
3
    FcPatternDel (resolved, FC_VERTICAL_LAYOUT);
72
3
    FcPatternAddBool (resolved, FC_VERTICAL_LAYOUT, FcTrue);
73

            
74
3
    FcPatternGetDouble (resolved, FC_PIXEL_SIZE, 0, &pixel_size);
75

            
76
3
    font_face = cairo_ft_font_face_create_for_pattern (resolved);
77

            
78
3
    cairo_matrix_init_translate (&font_matrix, 10, 30);
79
3
    cairo_matrix_rotate (&font_matrix, M_PI_2/3);
80
3
    cairo_matrix_scale (&font_matrix, pixel_size, pixel_size);
81

            
82
3
    cairo_get_matrix (cr, &ctm);
83

            
84
3
    scaled_font = cairo_scaled_font_create (font_face,
85
					    &font_matrix,
86
					    &ctm,
87
					    font_options);
88

            
89
3
    cairo_font_options_destroy (font_options);
90
3
    cairo_font_face_destroy (font_face);
91
3
    FcPatternDestroy (pattern);
92
3
    FcPatternDestroy (resolved);
93

            
94
3
    status = cairo_scaled_font_status (scaled_font);
95
3
    if (status) {
96
	cairo_scaled_font_destroy (scaled_font);
97
	return status;
98
    }
99

            
100
3
    *out = scaled_font;
101
3
    return CAIRO_STATUS_SUCCESS;
102
}
103

            
104
static cairo_test_status_t
105
3
draw (cairo_t *cr, int width, int height)
106
{
107
    cairo_text_extents_t extents;
108
    cairo_scaled_font_t *scaled_font;
109
    cairo_status_t status;
110
3
    const char text[] = "i-W";
111
    double line_width, x, y;
112

            
113
3
    line_width = cairo_get_line_width (cr);
114

            
115
    /* We draw in the default black, so paint white first. */
116
3
    cairo_save (cr);
117
3
    cairo_set_source_rgb (cr, 1.0, 1.0, 1.0); /* white */
118
3
    cairo_paint (cr);
119
3
    cairo_restore (cr);
120

            
121
3
    status = create_scaled_font (cr, &scaled_font);
122
3
    if (status) {
123
	return cairo_test_status_from_status (cairo_test_get_context (cr),
124
					      status);
125
    }
126

            
127
3
    cairo_set_scaled_font (cr, scaled_font);
128
3
    cairo_scaled_font_destroy (scaled_font);
129

            
130
3
    cairo_set_line_width (cr, 1.0);
131
3
    cairo_set_source_rgb (cr, 0, 0, 0); /* black */
132
3
    cairo_text_extents (cr, text, &extents);
133
3
    x = width  - (extents.width  + extents.x_bearing) - 5;
134
3
    y = height - (extents.height + extents.y_bearing) - 5;
135
3
    cairo_move_to (cr, x, y);
136
3
    cairo_show_text (cr, text);
137
3
    cairo_rectangle (cr,
138
3
		     x + extents.x_bearing - line_width / 2,
139
3
		     y + extents.y_bearing - line_width / 2,
140
3
		     extents.width  + line_width,
141
3
		     extents.height + line_width);
142
3
    cairo_stroke (cr);
143

            
144
3
    cairo_set_source_rgb (cr, 0, 0, 1); /* blue */
145
3
    cairo_text_extents (cr, text, &extents);
146
3
    x = -extents.x_bearing + 5;
147
3
    y = -extents.y_bearing + 5;
148
3
    cairo_move_to (cr, x, y);
149
3
    cairo_text_path (cr, text);
150
3
    cairo_fill (cr);
151
3
    cairo_rectangle (cr,
152
3
		     x + extents.x_bearing - line_width / 2,
153
3
		     y + extents.y_bearing - line_width / 2,
154
3
		     extents.width  + line_width,
155
3
		     extents.height + line_width);
156
3
    cairo_stroke (cr);
157

            
158
3
    return CAIRO_TEST_SUCCESS;
159
}
160

            
161
1
CAIRO_TEST (ft_text_vertical_layout_type1,
162
	    "Tests text rendering for vertical layout with Type1 fonts"
163
	    "\nCan fail if an incorrect font is loaded---need to bundle the desired font",
164
	    "ft, fc, text", /* keywords */
165
	    NULL, /* requirements */
166
	    WIDTH, HEIGHT,
167
	    NULL, draw)