[API] Add support for vertical text
[apps/home/video-player.git] / test / test-shape.c
1 /*
2  * Copyright © 2011  Google, Inc.
3  *
4  *  This is part of HarfBuzz, a text shaping library.
5  *
6  * Permission is hereby granted, without written agreement and without
7  * license or royalty fees, to use, copy, modify, and distribute this
8  * software and its documentation for any purpose, provided that the
9  * above copyright notice and the following two paragraphs appear in
10  * all copies of this software.
11  *
12  * IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE TO ANY PARTY FOR
13  * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
14  * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN
15  * IF THE COPYRIGHT HOLDER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH
16  * DAMAGE.
17  *
18  * THE COPYRIGHT HOLDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING,
19  * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
20  * FITNESS FOR A PARTICULAR PURPOSE.  THE SOFTWARE PROVIDED HEREUNDER IS
21  * ON AN "AS IS" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO
22  * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
23  *
24  * Google Author(s): Behdad Esfahbod
25  */
26
27 #include "hb-test.h"
28
29 /* Unit tests for hb-shape.h */
30
31 /*
32  * This test provides a framework to test aspects of hb_shape() that are
33  * font-independent.  Please add tests for any feature that fits that
34  * description.
35  */
36
37
38 static const char test_data[] = "test\0data";
39
40 static hb_bool_t
41 glyph_advance_func (hb_font_t *font, void *font_data,
42                     hb_codepoint_t glyph,
43                     hb_position_t *x_advance, hb_position_t *y_advance,
44                     void *user_data)
45 {
46   switch (glyph) {
47   case 1: *x_advance = 10; return TRUE;
48   case 2: *x_advance =  6; return TRUE;
49   case 3: *x_advance =  5; return TRUE;
50   }
51   return FALSE;
52 }
53
54 static hb_bool_t
55 glyph_func (hb_font_t *font, void *font_data,
56             hb_codepoint_t unicode, hb_codepoint_t variant_selector,
57             hb_codepoint_t *glyph,
58             void *user_data)
59 {
60   switch (unicode) {
61   case 'T': *glyph = 1; return TRUE;
62   case 'e': *glyph = 2; return TRUE;
63   case 's': *glyph = 3; return TRUE;
64   }
65
66   return FALSE;
67 }
68
69 static hb_bool_t
70 kerning_func (hb_font_t *font, void *font_data,
71               hb_codepoint_t left, hb_codepoint_t right,
72               hb_position_t *x_kern, hb_position_t *y_kern,
73               void *user_data)
74 {
75   if (left == 1 && right == 2) {
76     *x_kern = -2;
77     return TRUE;
78   }
79   return FALSE;
80 }
81
82 static const char TesT[] = "TesT";
83
84 static void
85 test_shape (void)
86 {
87   hb_blob_t *blob;
88   hb_face_t *face;
89   hb_font_funcs_t *ffuncs;
90   hb_font_t *font;
91   hb_buffer_t *buffer;
92   unsigned int len;
93   hb_glyph_info_t *glyphs;
94   hb_glyph_position_t *positions;
95
96   blob = hb_blob_create (test_data, sizeof (test_data), HB_MEMORY_MODE_READONLY, NULL, NULL);
97   face = hb_face_create (blob, 0);
98   hb_blob_destroy (blob);
99   font = hb_font_create (face);
100   hb_face_destroy (face);
101   hb_font_set_scale (font, 10, 10);
102
103   ffuncs = hb_font_funcs_create ();
104   hb_font_funcs_set_glyph_h_advance_func (ffuncs, glyph_advance_func, NULL, NULL);
105   hb_font_funcs_set_glyph_func (ffuncs, glyph_func, NULL, NULL);
106   hb_font_funcs_set_h_kerning_func (ffuncs, kerning_func, NULL, NULL);
107   hb_font_set_funcs (font, ffuncs, NULL, NULL);
108   hb_font_funcs_destroy (ffuncs);
109
110   buffer =  hb_buffer_create (0);
111   hb_buffer_set_direction (buffer, HB_DIRECTION_LTR);
112   hb_buffer_add_utf8 (buffer, TesT, 4, 0, 4);
113
114   hb_shape (font, buffer, NULL, 0);
115
116   len = hb_buffer_get_length (buffer);
117   glyphs = hb_buffer_get_glyph_infos (buffer, NULL);
118   positions = hb_buffer_get_glyph_positions (buffer, NULL);
119
120   {
121     const hb_codepoint_t output_glyphs[] = {1, 2, 3, 1};
122     const hb_position_t output_x_advances[] = {9, 5, 5, 10};
123     const hb_position_t output_x_offsets[] = {0, -1, 0, 0};
124     unsigned int i;
125     g_assert_cmpint (len, ==, 4);
126     for (i = 0; i < len; i++) {
127       g_assert_cmphex (glyphs[i].codepoint, ==, output_glyphs[i]);
128       g_assert_cmphex (glyphs[i].cluster,   ==, i);
129     }
130     for (i = 0; i < len; i++) {
131       g_assert_cmpint (output_x_advances[i], ==, positions[i].x_advance);
132       g_assert_cmpint (output_x_offsets [i], ==, positions[i].x_offset);
133       g_assert_cmpint (0, ==, positions[i].y_advance);
134       g_assert_cmpint (0, ==, positions[i].y_offset);
135     }
136   }
137
138   hb_buffer_destroy (buffer);
139   hb_font_destroy (font);
140 }
141
142
143 int
144 main (int argc, char **argv)
145 {
146   hb_test_init (&argc, &argv);
147
148   hb_test_add (test_shape);
149
150   return hb_test_run();
151 }