source: mainline/uspace/lib/gfxfont/test/tpf.c@ e3997a8

Last change on this file since e3997a8 was cc73f6d4, checked in by Jiří Zárevúcky <zarevucky.jiri@…>, 22 months ago

Avoid casting between enum types

  • Property mode set to 100644
File size: 7.5 KB
Line 
1/*
2 * Copyright (c) 2021 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 <gfx/typeface.h>
34#include <pcut/pcut.h>
35#include <str.h>
36#include "../private/font.h"
37#include "../private/typeface.h"
38
39PCUT_INIT;
40
41PCUT_TEST_SUITE(tpf);
42
43static errno_t testgc_set_clip_rect(void *, gfx_rect_t *);
44static errno_t testgc_set_color(void *, gfx_color_t *);
45static errno_t testgc_fill_rect(void *, gfx_rect_t *);
46static errno_t testgc_bitmap_create(void *, gfx_bitmap_params_t *,
47 gfx_bitmap_alloc_t *, void **);
48static errno_t testgc_bitmap_destroy(void *);
49static errno_t testgc_bitmap_render(void *, gfx_rect_t *, gfx_coord2_t *);
50static errno_t testgc_bitmap_get_alloc(void *, gfx_bitmap_alloc_t *);
51
52static gfx_context_ops_t test_ops = {
53 .set_clip_rect = testgc_set_clip_rect,
54 .set_color = testgc_set_color,
55 .fill_rect = testgc_fill_rect,
56 .bitmap_create = testgc_bitmap_create,
57 .bitmap_destroy = testgc_bitmap_destroy,
58 .bitmap_render = testgc_bitmap_render,
59 .bitmap_get_alloc = testgc_bitmap_get_alloc
60};
61
62typedef struct {
63 gfx_bitmap_params_t bm_params;
64 void *bm_pixels;
65 gfx_rect_t bm_srect;
66 gfx_coord2_t bm_offs;
67} test_gc_t;
68
69typedef struct {
70 test_gc_t *tgc;
71 gfx_bitmap_alloc_t alloc;
72 bool myalloc;
73} testgc_bitmap_t;
74
75static const gfx_font_flags_t test_font_flags = gff_bold_italic;
76
77enum {
78 test_font_size = 9,
79 test_font_ascent = 4,
80 test_font_descent = 3,
81 test_font_leading = 2,
82 test_glyph_advance = 10
83};
84
85static const char *test_glyph_pattern = "ff";
86
87/** Test saving typeface to and loading from TPF file */
88PCUT_TEST(save_load)
89{
90 char fname[L_tmpnam];
91 char *p;
92 gfx_font_props_t props;
93 gfx_font_props_t rprops;
94 gfx_font_metrics_t metrics;
95 gfx_font_metrics_t rmetrics;
96 gfx_glyph_metrics_t gmetrics;
97 gfx_glyph_metrics_t rgmetrics;
98 gfx_glyph_pattern_t *pat;
99 const char *str;
100 gfx_typeface_t *tface;
101 gfx_font_t *font;
102 gfx_font_info_t *finfo;
103 gfx_context_t *gc;
104 gfx_glyph_t *glyph;
105 gfx_glyph_bmp_t *bmp;
106 int pix;
107 test_gc_t tgc;
108 errno_t rc;
109
110 p = tmpnam(fname);
111 PCUT_ASSERT_NOT_NULL(p);
112
113 rc = gfx_context_new(&test_ops, (void *)&tgc, &gc);
114 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
115
116 rc = gfx_typeface_create(gc, &tface);
117 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
118
119 gfx_font_props_init(&props);
120 props.size = test_font_size;
121 props.flags = test_font_flags;
122
123 gfx_font_metrics_init(&metrics);
124 metrics.ascent = test_font_ascent;
125 metrics.descent = test_font_descent;
126 metrics.leading = test_font_leading;
127
128 rc = gfx_font_create(tface, &props, &metrics, &font);
129 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
130
131 gfx_glyph_metrics_init(&gmetrics);
132 gmetrics.advance = test_glyph_advance;
133
134 rc = gfx_glyph_create(font, &gmetrics, &glyph);
135 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
136
137 rc = gfx_glyph_set_pattern(glyph, test_glyph_pattern);
138 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
139
140 rc = gfx_glyph_bmp_open(glyph, &bmp);
141 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
142
143 rc = gfx_glyph_bmp_setpix(bmp, 0, 0, 1);
144 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
145
146 rc = gfx_glyph_bmp_setpix(bmp, 1, 1, 1);
147 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
148
149 rc = gfx_glyph_bmp_save(bmp);
150 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
151
152 gfx_glyph_bmp_close(bmp);
153
154 rc = gfx_typeface_save(tface, fname);
155 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
156
157 gfx_typeface_destroy(tface);
158 tface = NULL;
159
160 rc = gfx_typeface_open(gc, fname, &tface);
161 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
162 PCUT_ASSERT_NOT_NULL(tface);
163
164 finfo = gfx_typeface_first_font(tface);
165 PCUT_ASSERT_NOT_NULL(finfo);
166
167 gfx_font_get_props(finfo, &rprops);
168 PCUT_ASSERT_INT_EQUALS(props.size, rprops.size);
169 PCUT_ASSERT_INT_EQUALS(props.flags, rprops.flags);
170
171 rc = gfx_font_open(finfo, &font);
172 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
173 PCUT_ASSERT_NOT_NULL(font);
174
175 gfx_font_get_metrics(font, &rmetrics);
176 PCUT_ASSERT_INT_EQUALS(metrics.ascent, rmetrics.ascent);
177 PCUT_ASSERT_INT_EQUALS(metrics.descent, rmetrics.descent);
178 PCUT_ASSERT_INT_EQUALS(metrics.leading, rmetrics.leading);
179
180 glyph = gfx_font_first_glyph(font);
181 PCUT_ASSERT_NOT_NULL(glyph);
182
183 gfx_glyph_get_metrics(glyph, &rgmetrics);
184 PCUT_ASSERT_INT_EQUALS(gmetrics.advance, rgmetrics.advance);
185
186 pat = gfx_glyph_first_pattern(glyph);
187 str = gfx_glyph_pattern_str(pat);
188 PCUT_ASSERT_INT_EQUALS(0, str_cmp(test_glyph_pattern, str));
189
190 rc = gfx_glyph_bmp_open(glyph, &bmp);
191 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
192
193 pix = gfx_glyph_bmp_getpix(bmp, 0, 0);
194 PCUT_ASSERT_INT_EQUALS(1, pix);
195 pix = gfx_glyph_bmp_getpix(bmp, 1, 1);
196 PCUT_ASSERT_INT_EQUALS(1, pix);
197 pix = gfx_glyph_bmp_getpix(bmp, 1, 0);
198 PCUT_ASSERT_INT_EQUALS(0, pix);
199 pix = gfx_glyph_bmp_getpix(bmp, 0, 1);
200 PCUT_ASSERT_INT_EQUALS(0, pix);
201
202 gfx_glyph_bmp_close(bmp);
203
204 gfx_typeface_destroy(tface);
205 tface = NULL;
206
207 rc = gfx_context_delete(gc);
208 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
209
210 (void) remove(p);
211}
212
213static errno_t testgc_set_clip_rect(void *arg, gfx_rect_t *rect)
214{
215 return EOK;
216}
217
218static errno_t testgc_set_color(void *arg, gfx_color_t *color)
219{
220 return EOK;
221}
222
223static errno_t testgc_fill_rect(void *arg, gfx_rect_t *rect)
224{
225 return EOK;
226}
227
228static errno_t testgc_bitmap_create(void *arg, gfx_bitmap_params_t *params,
229 gfx_bitmap_alloc_t *alloc, void **rbm)
230{
231 test_gc_t *tgc = (test_gc_t *) arg;
232 testgc_bitmap_t *tbm;
233
234 tbm = calloc(1, sizeof(testgc_bitmap_t));
235 if (tbm == NULL)
236 return ENOMEM;
237
238 if (alloc == NULL) {
239 tbm->alloc.pitch = (params->rect.p1.x - params->rect.p0.x) *
240 sizeof(uint32_t);
241 tbm->alloc.off0 = 0;
242 tbm->alloc.pixels = calloc(sizeof(uint32_t),
243 tbm->alloc.pitch * (params->rect.p1.y - params->rect.p0.y));
244 tbm->myalloc = true;
245 if (tbm->alloc.pixels == NULL) {
246 free(tbm);
247 return ENOMEM;
248 }
249 } else {
250 tbm->alloc = *alloc;
251 }
252
253 tbm->tgc = tgc;
254 tgc->bm_params = *params;
255 tgc->bm_pixels = tbm->alloc.pixels;
256 *rbm = (void *)tbm;
257 return EOK;
258}
259
260static errno_t testgc_bitmap_destroy(void *bm)
261{
262 testgc_bitmap_t *tbm = (testgc_bitmap_t *)bm;
263 if (tbm->myalloc)
264 free(tbm->alloc.pixels);
265 free(tbm);
266 return EOK;
267}
268
269static errno_t testgc_bitmap_render(void *bm, gfx_rect_t *srect,
270 gfx_coord2_t *offs)
271{
272 testgc_bitmap_t *tbm = (testgc_bitmap_t *)bm;
273 tbm->tgc->bm_srect = *srect;
274 tbm->tgc->bm_offs = *offs;
275 return EOK;
276}
277
278static errno_t testgc_bitmap_get_alloc(void *bm, gfx_bitmap_alloc_t *alloc)
279{
280 testgc_bitmap_t *tbm = (testgc_bitmap_t *)bm;
281 *alloc = tbm->alloc;
282 return EOK;
283}
284
285PCUT_EXPORT(tpf);
Note: See TracBrowser for help on using the repository browser.