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

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

Make TPF save/load test more elaborate

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