source: mainline/uspace/lib/gfxfont/test/glyph_bmp.c@ c78a03d

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since c78a03d was c78a03d, checked in by Jiri Svoboda <jiri@…>, 5 years ago

Flesh out most of font, glyph and glyph bitmap implementation and tests

  • Property mode set to 100644
File size: 7.2 KB
Line 
1/*
2 * Copyright (c) 2020 Jiri Svoboda
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * - Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * - Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * - The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29#include <gfx/context.h>
30#include <gfx/font.h>
31#include <gfx/glyph.h>
32#include <gfx/glyph_bmp.h>
33#include <pcut/pcut.h>
34
35PCUT_INIT;
36
37PCUT_TEST_SUITE(glyph_bmp);
38
39static errno_t testgc_set_color(void *, gfx_color_t *);
40static errno_t testgc_fill_rect(void *, gfx_rect_t *);
41
42static gfx_context_ops_t test_ops = {
43 .set_color = testgc_set_color,
44 .fill_rect = testgc_fill_rect
45};
46
47/** Test opening and closing glyph bitmap */
48PCUT_TEST(open_close)
49{
50 gfx_font_metrics_t fmetrics;
51 gfx_font_t *font;
52 gfx_glyph_metrics_t gmetrics;
53 gfx_glyph_t *glyph;
54 gfx_context_t *gc;
55 gfx_glyph_bmp_t *bmp;
56 errno_t rc;
57
58 rc = gfx_context_new(&test_ops, NULL, &gc);
59 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
60
61 gfx_font_metrics_init(&fmetrics);
62 rc = gfx_font_create(gc, &fmetrics, &font);
63 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
64
65 gfx_glyph_metrics_init(&gmetrics);
66 gmetrics.advance = 1;
67
68 rc = gfx_glyph_create(font, &gmetrics, &glyph);
69 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
70
71 bmp = NULL;
72
73 rc = gfx_glyph_bmp_open(glyph, &bmp);
74 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
75 PCUT_ASSERT_NOT_NULL(bmp);
76
77 gfx_glyph_bmp_close(bmp);
78
79 gfx_glyph_destroy(glyph);
80
81 gfx_font_destroy(font);
82 rc = gfx_context_delete(gc);
83 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
84}
85
86/** Test glyph_bmp_save() */
87PCUT_TEST(save)
88{
89 gfx_font_metrics_t fmetrics;
90 gfx_font_t *font;
91 gfx_glyph_metrics_t gmetrics;
92 gfx_glyph_t *glyph;
93 gfx_context_t *gc;
94 gfx_glyph_bmp_t *bmp;
95 errno_t rc;
96
97 rc = gfx_context_new(&test_ops, NULL, &gc);
98 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
99
100 gfx_font_metrics_init(&fmetrics);
101 rc = gfx_font_create(gc, &fmetrics, &font);
102 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
103
104 gfx_glyph_metrics_init(&gmetrics);
105 gmetrics.advance = 1;
106
107 rc = gfx_glyph_create(font, &gmetrics, &glyph);
108 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
109
110 bmp = NULL;
111
112 rc = gfx_glyph_bmp_open(glyph, &bmp);
113 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
114 PCUT_ASSERT_NOT_NULL(bmp);
115
116 /* TODO: Need a test to verify that save actually worked */
117 rc = gfx_glyph_bmp_save(bmp);
118 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
119
120 gfx_glyph_bmp_close(bmp);
121
122 gfx_glyph_destroy(glyph);
123
124 gfx_font_destroy(font);
125 rc = gfx_context_delete(gc);
126 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
127}
128
129/** Test glyph_bmp_getpix() */
130PCUT_TEST(getpix)
131{
132 gfx_font_metrics_t fmetrics;
133 gfx_font_t *font;
134 gfx_glyph_metrics_t gmetrics;
135 gfx_glyph_t *glyph;
136 gfx_context_t *gc;
137 gfx_glyph_bmp_t *bmp;
138 int pix;
139 errno_t rc;
140
141 rc = gfx_context_new(&test_ops, NULL, &gc);
142 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
143
144 gfx_font_metrics_init(&fmetrics);
145 rc = gfx_font_create(gc, &fmetrics, &font);
146 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
147
148 gfx_glyph_metrics_init(&gmetrics);
149 gmetrics.advance = 1;
150
151 rc = gfx_glyph_create(font, &gmetrics, &glyph);
152 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
153
154 bmp = NULL;
155
156 rc = gfx_glyph_bmp_open(glyph, &bmp);
157 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
158 PCUT_ASSERT_NOT_NULL(bmp);
159
160 pix = gfx_glyph_bmp_getpix(bmp, 0, 0);
161 PCUT_ASSERT_INT_EQUALS(0, pix);
162
163 gfx_glyph_bmp_close(bmp);
164
165 gfx_glyph_destroy(glyph);
166
167 gfx_font_destroy(font);
168 rc = gfx_context_delete(gc);
169 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
170}
171
172/** Test glyph_bmp_setpix() can flip pixel value both ways */
173PCUT_TEST(setpix_flip)
174{
175 gfx_font_metrics_t fmetrics;
176 gfx_font_t *font;
177 gfx_glyph_metrics_t gmetrics;
178 gfx_glyph_t *glyph;
179 gfx_context_t *gc;
180 gfx_glyph_bmp_t *bmp;
181 int pix;
182 errno_t rc;
183
184 rc = gfx_context_new(&test_ops, NULL, &gc);
185 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
186
187 gfx_font_metrics_init(&fmetrics);
188 rc = gfx_font_create(gc, &fmetrics, &font);
189 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
190
191 gfx_glyph_metrics_init(&gmetrics);
192 gmetrics.advance = 1;
193
194 rc = gfx_glyph_create(font, &gmetrics, &glyph);
195 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
196
197 bmp = NULL;
198
199 rc = gfx_glyph_bmp_open(glyph, &bmp);
200 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
201 PCUT_ASSERT_NOT_NULL(bmp);
202
203 pix = gfx_glyph_bmp_getpix(bmp, 0, 0);
204 PCUT_ASSERT_INT_EQUALS(0, pix);
205
206 rc = gfx_glyph_bmp_setpix(bmp, 0, 0, 1);
207 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
208
209 pix = gfx_glyph_bmp_getpix(bmp, 0, 0);
210 PCUT_ASSERT_INT_EQUALS(1, pix);
211
212 rc = gfx_glyph_bmp_setpix(bmp, 0, 0, 0);
213 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
214
215 pix = gfx_glyph_bmp_getpix(bmp, 0, 0);
216 PCUT_ASSERT_INT_EQUALS(0, pix);
217
218 gfx_glyph_bmp_close(bmp);
219
220 gfx_glyph_destroy(glyph);
221
222 gfx_font_destroy(font);
223 rc = gfx_context_delete(gc);
224 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
225}
226
227/** Test glyph_bmp_setpix() properly extends pixel array */
228PCUT_TEST(setpix_externd)
229{
230 gfx_font_metrics_t fmetrics;
231 gfx_font_t *font;
232 gfx_glyph_metrics_t gmetrics;
233 gfx_glyph_t *glyph;
234 gfx_context_t *gc;
235 gfx_glyph_bmp_t *bmp;
236 gfx_coord_t x, y;
237 int pix;
238 errno_t rc;
239
240 rc = gfx_context_new(&test_ops, NULL, &gc);
241 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
242
243 gfx_font_metrics_init(&fmetrics);
244 rc = gfx_font_create(gc, &fmetrics, &font);
245 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
246
247 gfx_glyph_metrics_init(&gmetrics);
248 gmetrics.advance = 1;
249
250 rc = gfx_glyph_create(font, &gmetrics, &glyph);
251 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
252
253 bmp = NULL;
254
255 rc = gfx_glyph_bmp_open(glyph, &bmp);
256 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
257 PCUT_ASSERT_NOT_NULL(bmp);
258
259 /*
260 * Fill the rectangle [0, 0]..[3, 3] with alternating pixel pattern
261 * and then check it.
262 */
263
264 rc = gfx_glyph_bmp_setpix(bmp, 0, 0, 1);
265 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
266
267 rc = gfx_glyph_bmp_setpix(bmp, 1, 1, 1);
268 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
269
270 rc = gfx_glyph_bmp_setpix(bmp, 2, 0, 1);
271 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
272
273 rc = gfx_glyph_bmp_setpix(bmp, 0, 2, 1);
274 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
275
276 rc = gfx_glyph_bmp_setpix(bmp, 2, 2, 1);
277 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
278
279 for (y = 0; y <= 2; y++) {
280 for (x = 0; x <= 2; x++) {
281 pix = gfx_glyph_bmp_getpix(bmp, x, y);
282 PCUT_ASSERT_INT_EQUALS((x & 1) ^ (y & 1) ^ 1, pix);
283 }
284 }
285
286 gfx_glyph_bmp_close(bmp);
287
288 gfx_glyph_destroy(glyph);
289
290 gfx_font_destroy(font);
291 rc = gfx_context_delete(gc);
292 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
293}
294
295static errno_t testgc_set_color(void *arg, gfx_color_t *color)
296{
297 return EOK;
298}
299
300static errno_t testgc_fill_rect(void *arg, gfx_rect_t *rect)
301{
302 return EOK;
303}
304
305PCUT_EXPORT(glyph_bmp);
Note: See TracBrowser for help on using the repository browser.