[test/shape] Check shape output
[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 static const char test_data[] = "test\0data";
33
34 static void
35 glyph_advance_func (hb_font_t *font, void *font_data,
36                     hb_codepoint_t glyph,
37                     hb_position_t *x_advance, hb_position_t *y_advance,
38                     void *user_data)
39 {
40   switch (glyph) {
41   case 1: *x_advance = 10; return;
42   case 2: *x_advance =  6; return;
43   case 3: *x_advance =  5; return;
44   }
45 }
46
47 static hb_codepoint_t
48 glyph_func (hb_font_t *font, void *font_data,
49             hb_codepoint_t unicode, hb_codepoint_t variant_selector,
50             void *user_data)
51 {
52   switch (unicode) {
53   case 'T': return 1;
54   case 'e': return 2;
55   case 's': return 3;
56   default:  return 0;
57   }
58 }
59
60 static void
61 kerning_func (hb_font_t *font, void *font_data,
62                     hb_codepoint_t left, hb_codepoint_t right,
63                     hb_position_t *x_kern, hb_position_t *y_kern,
64                     void *user_data)
65 {
66   if (left == 1 && right == 2) {
67     *x_kern = -2;
68   }
69 }
70
71 static const char TesT[] = "TesT";
72
73 static void
74 test_shape (void)
75 {
76   hb_blob_t *blob;
77   hb_face_t *face;
78   hb_font_funcs_t *ffuncs;
79   hb_font_t *font;
80   hb_buffer_t *buffer;
81   unsigned int len;
82   hb_glyph_info_t *glyphs;
83   hb_glyph_position_t *positions;
84
85   blob = hb_blob_create (test_data, sizeof (test_data), HB_MEMORY_MODE_READONLY, NULL, NULL);
86   face = hb_face_create (blob, 0);
87   hb_blob_destroy (blob);
88   font = hb_font_create (face);
89   hb_face_destroy (face);
90   hb_font_set_scale (font, 10, 10);
91
92   ffuncs = hb_font_funcs_create ();
93   hb_font_funcs_set_glyph_advance_func (ffuncs, glyph_advance_func, NULL, NULL);
94   hb_font_funcs_set_glyph_func (ffuncs, glyph_func, NULL, NULL);
95   hb_font_funcs_set_kerning_func (ffuncs, kerning_func, NULL, NULL);
96   hb_font_set_funcs (font, ffuncs, NULL, NULL);
97   hb_font_funcs_destroy (ffuncs);
98
99   buffer =  hb_buffer_create (0);
100   hb_buffer_set_direction (buffer, HB_DIRECTION_LTR);
101   hb_buffer_add_utf8 (buffer, TesT, 4, 0, 4);
102
103   hb_shape (font, buffer, NULL, 0);
104
105   len = hb_buffer_get_length (buffer);
106   glyphs = hb_buffer_get_glyph_infos (buffer, NULL);
107   positions = hb_buffer_get_glyph_positions (buffer, NULL);
108
109   {
110     const hb_codepoint_t output_glyphs[] = {1, 2, 3, 1};
111     const hb_position_t output_x_advances[] = {9, 5, 5, 10};
112     const hb_position_t output_x_offsets[] = {0, -1, 0, 0};
113     unsigned int i;
114     g_assert_cmpint (len, ==, 4);
115     for (i = 0; i < len; i++) {
116       g_assert_cmphex (glyphs[i].codepoint, ==, output_glyphs[i]);
117       g_assert_cmphex (glyphs[i].cluster,   ==, i);
118     }
119     for (i = 0; i < len; i++) {
120       g_assert_cmpint (output_x_advances[i], ==, positions[i].x_advance);
121       g_assert_cmpint (output_x_offsets [i], ==, positions[i].x_offset);
122       g_assert_cmpint (0, ==, positions[i].y_advance);
123       g_assert_cmpint (0, ==, positions[i].y_offset);
124     }
125   }
126
127   hb_buffer_destroy (buffer);
128   hb_font_destroy (font);
129 }
130
131
132 int
133 main (int argc, char **argv)
134 {
135   hb_test_init (&argc, &argv);
136
137   hb_test_add (test_shape);
138
139   return hb_test_run();
140 }