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

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

Add GC operation to set clipping rectangle

The number of changed files is due to the proliferation of GC
implementations, mostly these are just dummies in unit tests.
Definitely need to tame those in the future.

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