93067abc7c6f5e5be22846f5f3102cdd9f6003bb
[framework/graphics/cairo.git] / test / overlapping-glyphs.c
1 /*
2  * Copyright © 2009 Chris Wilson
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  * Chris Wilson not be used in advertising or publicity pertaining to
10  * distribution of the software without specific, written prior
11  * permission. Chris Wilson 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  * CHRIS WILSON DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS
16  * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
17  * FITNESS, IN NO EVENT SHALL CHRIS WILSON 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: Chris Wilson <chris@chris-wilson.co.uk>
24  */
25
26 #include "cairo-test.h"
27
28 #include <assert.h>
29
30 #define TEXT_SIZE 12
31 #define HEIGHT (TEXT_SIZE + 4)
32 #define WIDTH 50
33
34 #define MAX_GLYPHS 80
35
36 static cairo_test_status_t
37 draw (cairo_t *cr, int width, int height)
38 {
39     cairo_glyph_t glyphs_stack[MAX_GLYPHS], *glyphs;
40     const char *cairo = "Cairo";
41     const char *giza = "Giza";
42     cairo_text_extents_t cairo_extents;
43     cairo_text_extents_t giza_extents;
44     int count, num_glyphs;
45     double x0, y0;
46
47     /* We draw in the default black, so paint white first. */
48     cairo_set_source_rgb (cr, 1.0, 1.0, 1.0); /* white */
49     cairo_paint (cr);
50
51     cairo_select_font_face (cr, CAIRO_TEST_FONT_FAMILY " Sans",
52                             CAIRO_FONT_SLANT_NORMAL,
53                             CAIRO_FONT_WEIGHT_NORMAL);
54     cairo_set_font_size (cr, TEXT_SIZE);
55
56     /* We want to overlap two strings, so compute overlapping glyphs.  */
57
58     cairo_text_extents (cr, cairo, &cairo_extents);
59     cairo_text_extents (cr, giza, &giza_extents);
60
61     x0 = WIDTH/2. - (cairo_extents.width/2. + cairo_extents.x_bearing);
62     y0 = HEIGHT/2. - (cairo_extents.height/2. + cairo_extents.y_bearing);
63     glyphs = glyphs_stack;
64     count = MAX_GLYPHS;
65     cairo_scaled_font_text_to_glyphs (cairo_get_scaled_font (cr),
66                                       x0, y0,
67                                       cairo, strlen (cairo),
68                                       &glyphs, &count,
69                                       NULL, NULL,
70                                       NULL);
71     assert (glyphs == glyphs_stack);
72     num_glyphs = count;
73
74     x0 = WIDTH/2. - (giza_extents.width/2. + giza_extents.x_bearing);
75     y0 = HEIGHT/2. - (giza_extents.height/2. + giza_extents.y_bearing);
76     glyphs = glyphs_stack + count;
77     count = MAX_GLYPHS - count;
78     cairo_scaled_font_text_to_glyphs (cairo_get_scaled_font (cr),
79                                       x0, y0,
80                                       giza, strlen (giza),
81                                       &glyphs, &count,
82                                       NULL, NULL,
83                                       NULL);
84     assert (glyphs == glyphs_stack + num_glyphs);
85     glyphs = glyphs_stack;
86     num_glyphs += count;
87
88     cairo_set_source_rgba (cr, 0, 0, 0, .5); /* translucent black, gray! */
89     cairo_show_glyphs (cr, glyphs, num_glyphs);
90
91     /* and compare with filling */
92     cairo_translate (cr, 0, HEIGHT);
93     cairo_glyph_path (cr, glyphs, num_glyphs);
94     cairo_fill (cr);
95
96     /* switch to using an unbounded operator for added complexity */
97     cairo_set_operator (cr, CAIRO_OPERATOR_IN);
98
99     cairo_translate (cr, WIDTH, -HEIGHT);
100     cairo_save (cr);
101     cairo_rectangle (cr, 0, 0, WIDTH, HEIGHT);
102     cairo_clip (cr);
103     cairo_show_glyphs (cr, glyphs, num_glyphs);
104     cairo_restore (cr);
105
106     cairo_translate (cr, 0, HEIGHT);
107     cairo_save (cr);
108     cairo_rectangle (cr, 0, 0, WIDTH, HEIGHT);
109     cairo_clip (cr);
110     cairo_glyph_path (cr, glyphs, num_glyphs);
111     cairo_fill (cr);
112     cairo_restore (cr);
113
114     return CAIRO_TEST_SUCCESS;
115 }
116
117 CAIRO_TEST (overlapping_glyphs,
118             "Test handing of overlapping glyphs",
119             "text, glyphs", /* keywords */
120             NULL, /* requirements */
121             2 * WIDTH, 2 * HEIGHT,
122             NULL, draw)
123