[test] Add test case for a leaky dashed rectangle.
[cairo/haiku.git] / test / ft-show-glyphs-positioning.c
blobd23c53181e91723aa9b56b0f874248186a1c7388
1 /*
2 * Copyright © 2008 Adrian Johnson
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:
12 * The above copyright notice and this permission notice shall be
13 * included in all copies or substantial portions of the Software.
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.
24 * Author: Adrian Johnson <ajohnson@redneon.com>
27 #include "cairo-test.h"
28 #include <cairo-ft.h>
30 #define TEXT_SIZE 12
32 static cairo_test_draw_function_t draw;
34 cairo_test_t test = {
35 "ft-show-glyphs-positioning",
36 "Test that the PS/PDF glyph positioning optimizations are correct",
37 235, (TEXT_SIZE + 4)*2,
38 draw
41 typedef struct {
42 cairo_glyph_t glyph_list[100];
43 int num_glyphs;
44 double x;
45 double y;
46 } glyph_array_t;
48 static void
49 glyph_array_init (glyph_array_t *glyphs, double x, double y)
51 glyphs->num_glyphs = 0;
52 glyphs->x = x;
53 glyphs->y = y;
56 static void
57 glyph_array_rel_move_to (glyph_array_t *glyphs, double x, double y)
59 glyphs->x += x;
60 glyphs->y += y;
63 static void
64 glyph_array_show (glyph_array_t *glyphs, cairo_t *cr)
66 cairo_show_glyphs (cr, glyphs->glyph_list, glyphs->num_glyphs);
69 #define DOUBLE_FROM_26_6(t) ((double)(t) / 64.0)
71 static void
72 glyph_array_add_text(glyph_array_t *glyphs, cairo_t *cr, const char *s, double spacing)
74 cairo_scaled_font_t *scaled_font;
75 FT_Face face;
76 unsigned long charcode;
77 unsigned int index;
78 cairo_text_extents_t extents;
79 const char *p;
80 FT_Vector kerning;
81 double kern_x;
82 int first = TRUE;
84 scaled_font = cairo_get_scaled_font (cr);
85 face = cairo_ft_scaled_font_lock_face (scaled_font);
86 p = s;
87 while (*p)
89 charcode = *p;
90 index = FT_Get_Char_Index (face, charcode);
91 glyphs->glyph_list[glyphs->num_glyphs].index = index;
92 if (first) {
93 first = FALSE;
94 glyphs->glyph_list[glyphs->num_glyphs].x = glyphs->x;
95 glyphs->glyph_list[glyphs->num_glyphs].y = glyphs->y;
96 } else {
97 cairo_glyph_extents (cr, &glyphs->glyph_list[glyphs->num_glyphs - 1], 1, &extents);
98 FT_Get_Kerning (face,
99 glyphs->glyph_list[glyphs->num_glyphs - 1].index,
100 glyphs->glyph_list[glyphs->num_glyphs].index,
101 FT_KERNING_UNSCALED,
102 &kerning);
103 kern_x = DOUBLE_FROM_26_6(kerning.x);
104 glyphs->glyph_list[glyphs->num_glyphs].x =
105 glyphs->glyph_list[glyphs->num_glyphs - 1].x + extents.x_advance + kern_x + spacing;
106 glyphs->glyph_list[glyphs->num_glyphs].y =
107 glyphs->glyph_list[glyphs->num_glyphs - 1].y + extents.y_advance;
110 cairo_glyph_extents (cr, &glyphs->glyph_list[glyphs->num_glyphs], 1, &extents);
111 glyphs->x = glyphs->glyph_list[glyphs->num_glyphs].x + extents.x_advance + spacing;
112 glyphs->y = glyphs->glyph_list[glyphs->num_glyphs].y + extents.y_advance;
113 p++;
114 glyphs->num_glyphs++;
117 cairo_ft_scaled_font_unlock_face (scaled_font);
120 static cairo_test_status_t
121 draw (cairo_t *cr, int width, int height)
123 glyph_array_t glyphs;
124 cairo_font_options_t *font_options;
126 /* paint white so we don't need separate ref images for
127 * RGB24 and ARGB32 */
128 cairo_set_source_rgb (cr, 1.0, 1.0, 1.0);
129 cairo_paint (cr);
131 cairo_select_font_face (cr, "Bitstream Vera Sans",
132 CAIRO_FONT_SLANT_NORMAL,
133 CAIRO_FONT_WEIGHT_NORMAL);
134 cairo_set_font_size (cr, TEXT_SIZE);
136 font_options = cairo_font_options_create ();
137 cairo_get_font_options (cr, font_options);
138 cairo_font_options_set_hint_metrics (font_options, CAIRO_HINT_METRICS_OFF);
139 cairo_set_font_options (cr, font_options);
140 cairo_font_options_destroy (font_options);
142 cairo_set_source_rgb (cr, 0.0, 0.0, 0.0);
144 glyph_array_init (&glyphs, 1, TEXT_SIZE);
145 glyph_array_add_text(&glyphs, cr, "AWAY again", 0.0);
146 glyph_array_rel_move_to (&glyphs, TEXT_SIZE*1, 0.0);
147 glyph_array_add_text(&glyphs, cr, "character space", TEXT_SIZE*0.3);
148 glyph_array_show (&glyphs, cr);
150 glyph_array_init (&glyphs, 1, TEXT_SIZE*2 + 4);
151 glyph_array_add_text(&glyphs, cr, "Increasing", 0.0);
152 glyph_array_rel_move_to (&glyphs, TEXT_SIZE*0.5, 0.0);
153 glyph_array_add_text(&glyphs, cr, "space", 0.0);
154 glyph_array_rel_move_to (&glyphs, TEXT_SIZE*1.0, 0.0);
155 glyph_array_add_text(&glyphs, cr, "between", 0.0);
156 glyph_array_rel_move_to (&glyphs, TEXT_SIZE*1.5, 0.0);
157 glyph_array_add_text(&glyphs, cr, "words", 0.0);
158 glyph_array_show (&glyphs, cr);
160 return CAIRO_TEST_SUCCESS;
164 main (void)
166 return cairo_test (&test);