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/bitmap.h>
|
---|
30 | #include <gfx/context.h>
|
---|
31 | #include <gfx/font.h>
|
---|
32 | #include <gfx/glyph.h>
|
---|
33 | #include <gfx/glyph_bmp.h>
|
---|
34 | #include <gfx/typeface.h>
|
---|
35 | #include <io/pixelmap.h>
|
---|
36 | #include <pcut/pcut.h>
|
---|
37 | #include <stdbool.h>
|
---|
38 | #include <str.h>
|
---|
39 | #include "../private/glyph.h"
|
---|
40 |
|
---|
41 | PCUT_INIT;
|
---|
42 |
|
---|
43 | PCUT_TEST_SUITE(glyph);
|
---|
44 |
|
---|
45 | static errno_t testgc_set_color(void *, gfx_color_t *);
|
---|
46 | static errno_t testgc_fill_rect(void *, gfx_rect_t *);
|
---|
47 | static errno_t testgc_bitmap_create(void *, gfx_bitmap_params_t *,
|
---|
48 | gfx_bitmap_alloc_t *, void **);
|
---|
49 | static errno_t testgc_bitmap_destroy(void *);
|
---|
50 | static errno_t testgc_bitmap_render(void *, gfx_rect_t *, gfx_coord2_t *);
|
---|
51 | static errno_t testgc_bitmap_get_alloc(void *, gfx_bitmap_alloc_t *);
|
---|
52 |
|
---|
53 | static gfx_context_ops_t test_ops = {
|
---|
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 |
|
---|
62 | typedef 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 |
|
---|
69 | typedef struct {
|
---|
70 | test_gc_t *tgc;
|
---|
71 | gfx_bitmap_alloc_t alloc;
|
---|
72 | bool myalloc;
|
---|
73 | } testgc_bitmap_t;
|
---|
74 |
|
---|
75 | /** Test creating and destroying glyph */
|
---|
76 | PCUT_TEST(create_destroy)
|
---|
77 | {
|
---|
78 | gfx_font_props_t fprops;
|
---|
79 | gfx_font_metrics_t fmetrics;
|
---|
80 | gfx_typeface_t *tface;
|
---|
81 | gfx_font_t *font;
|
---|
82 | gfx_glyph_metrics_t gmetrics;
|
---|
83 | gfx_glyph_t *glyph;
|
---|
84 | gfx_context_t *gc;
|
---|
85 | test_gc_t tgc;
|
---|
86 | errno_t rc;
|
---|
87 |
|
---|
88 | rc = gfx_context_new(&test_ops, (void *) &tgc, &gc);
|
---|
89 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
90 |
|
---|
91 | rc = gfx_typeface_create(gc, &tface);
|
---|
92 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
93 |
|
---|
94 | gfx_font_props_init(&fprops);
|
---|
95 | gfx_font_metrics_init(&fmetrics);
|
---|
96 | rc = gfx_font_create(tface, &fprops, &fmetrics, &font);
|
---|
97 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
98 |
|
---|
99 | gfx_glyph_metrics_init(&gmetrics);
|
---|
100 | rc = gfx_glyph_create(font, &gmetrics, &glyph);
|
---|
101 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
102 |
|
---|
103 | gfx_glyph_destroy(glyph);
|
---|
104 |
|
---|
105 | gfx_font_close(font);
|
---|
106 | gfx_typeface_destroy(tface);
|
---|
107 | rc = gfx_context_delete(gc);
|
---|
108 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
109 | }
|
---|
110 |
|
---|
111 | /** Test gfx_glyph_get_metrics() */
|
---|
112 | PCUT_TEST(get_metrics)
|
---|
113 | {
|
---|
114 | gfx_font_props_t fprops;
|
---|
115 | gfx_font_metrics_t fmetrics;
|
---|
116 | gfx_typeface_t *tface;
|
---|
117 | gfx_font_t *font;
|
---|
118 | gfx_glyph_metrics_t gmetrics;
|
---|
119 | gfx_glyph_t *glyph;
|
---|
120 | gfx_glyph_metrics_t rmetrics;
|
---|
121 | gfx_context_t *gc;
|
---|
122 | test_gc_t tgc;
|
---|
123 | errno_t rc;
|
---|
124 |
|
---|
125 | rc = gfx_context_new(&test_ops, (void *) &tgc, &gc);
|
---|
126 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
127 |
|
---|
128 | rc = gfx_typeface_create(gc, &tface);
|
---|
129 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
130 |
|
---|
131 | gfx_font_props_init(&fprops);
|
---|
132 | gfx_font_metrics_init(&fmetrics);
|
---|
133 | rc = gfx_font_create(tface, &fprops, &fmetrics, &font);
|
---|
134 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
135 |
|
---|
136 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
137 |
|
---|
138 | gfx_glyph_metrics_init(&gmetrics);
|
---|
139 | gmetrics.advance = 42;
|
---|
140 |
|
---|
141 | rc = gfx_glyph_create(font, &gmetrics, &glyph);
|
---|
142 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
143 |
|
---|
144 | gfx_glyph_get_metrics(glyph, &rmetrics);
|
---|
145 | PCUT_ASSERT_INT_EQUALS(gmetrics.advance, rmetrics.advance);
|
---|
146 |
|
---|
147 | gfx_glyph_destroy(glyph);
|
---|
148 |
|
---|
149 | gfx_font_close(font);
|
---|
150 | gfx_typeface_destroy(tface);
|
---|
151 | rc = gfx_context_delete(gc);
|
---|
152 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
153 | }
|
---|
154 |
|
---|
155 | /** Test gfx_glyph_set_metrics() */
|
---|
156 | PCUT_TEST(set_metrics)
|
---|
157 | {
|
---|
158 | gfx_font_props_t fprops;
|
---|
159 | gfx_font_metrics_t fmetrics;
|
---|
160 | gfx_typeface_t *tface;
|
---|
161 | gfx_font_t *font;
|
---|
162 | gfx_glyph_metrics_t gmetrics1;
|
---|
163 | gfx_glyph_metrics_t gmetrics2;
|
---|
164 | gfx_glyph_t *glyph;
|
---|
165 | gfx_glyph_metrics_t rmetrics;
|
---|
166 | gfx_context_t *gc;
|
---|
167 | test_gc_t tgc;
|
---|
168 | errno_t rc;
|
---|
169 |
|
---|
170 | rc = gfx_context_new(&test_ops, (void *) &tgc, &gc);
|
---|
171 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
172 |
|
---|
173 | rc = gfx_typeface_create(gc, &tface);
|
---|
174 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
175 |
|
---|
176 | gfx_font_props_init(&fprops);
|
---|
177 | gfx_font_metrics_init(&fmetrics);
|
---|
178 | rc = gfx_font_create(tface, &fprops, &fmetrics, &font);
|
---|
179 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
180 |
|
---|
181 | gfx_glyph_metrics_init(&gmetrics1);
|
---|
182 | gmetrics1.advance = 1;
|
---|
183 |
|
---|
184 | rc = gfx_glyph_create(font, &gmetrics1, &glyph);
|
---|
185 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
186 |
|
---|
187 | gfx_glyph_metrics_init(&gmetrics2);
|
---|
188 | gmetrics2.advance = 2;
|
---|
189 |
|
---|
190 | gfx_glyph_set_metrics(glyph, &gmetrics2);
|
---|
191 |
|
---|
192 | gfx_glyph_get_metrics(glyph, &rmetrics);
|
---|
193 | PCUT_ASSERT_INT_EQUALS(gmetrics2.advance, rmetrics.advance);
|
---|
194 |
|
---|
195 | gfx_glyph_destroy(glyph);
|
---|
196 |
|
---|
197 | gfx_font_close(font);
|
---|
198 | gfx_typeface_destroy(tface);
|
---|
199 | rc = gfx_context_delete(gc);
|
---|
200 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
201 | }
|
---|
202 |
|
---|
203 | /** Test gfx_glyph_set_pattern() */
|
---|
204 | PCUT_TEST(set_pattern)
|
---|
205 | {
|
---|
206 | gfx_font_props_t fprops;
|
---|
207 | gfx_font_metrics_t fmetrics;
|
---|
208 | gfx_typeface_t *tface;
|
---|
209 | gfx_font_t *font;
|
---|
210 | gfx_glyph_metrics_t gmetrics;
|
---|
211 | gfx_glyph_t *glyph;
|
---|
212 | gfx_context_t *gc;
|
---|
213 | test_gc_t tgc;
|
---|
214 | errno_t rc;
|
---|
215 |
|
---|
216 | rc = gfx_context_new(&test_ops, (void *) &tgc, &gc);
|
---|
217 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
218 |
|
---|
219 | rc = gfx_typeface_create(gc, &tface);
|
---|
220 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
221 |
|
---|
222 | gfx_font_props_init(&fprops);
|
---|
223 | gfx_font_metrics_init(&fmetrics);
|
---|
224 | rc = gfx_font_create(tface, &fprops, &fmetrics, &font);
|
---|
225 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
226 |
|
---|
227 | gfx_glyph_metrics_init(&gmetrics);
|
---|
228 | gmetrics.advance = 1;
|
---|
229 |
|
---|
230 | rc = gfx_glyph_create(font, &gmetrics, &glyph);
|
---|
231 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
232 |
|
---|
233 | PCUT_ASSERT_NULL(gfx_glyph_first_pattern(glyph));
|
---|
234 |
|
---|
235 | /* Set a pattern */
|
---|
236 | rc = gfx_glyph_set_pattern(glyph, "A");
|
---|
237 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
238 | PCUT_ASSERT_NOT_NULL(gfx_glyph_first_pattern(glyph));
|
---|
239 |
|
---|
240 | /* Setting the same pattern again should be OK */
|
---|
241 | rc = gfx_glyph_set_pattern(glyph, "A");
|
---|
242 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
243 | PCUT_ASSERT_NOT_NULL(gfx_glyph_first_pattern(glyph));
|
---|
244 |
|
---|
245 | gfx_glyph_destroy(glyph);
|
---|
246 |
|
---|
247 | gfx_font_close(font);
|
---|
248 | gfx_typeface_destroy(tface);
|
---|
249 | rc = gfx_context_delete(gc);
|
---|
250 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
251 | }
|
---|
252 |
|
---|
253 | /** Test gfx_glyph_clear_pattern() */
|
---|
254 | PCUT_TEST(clear_pattern)
|
---|
255 | {
|
---|
256 | gfx_font_props_t fprops;
|
---|
257 | gfx_font_metrics_t fmetrics;
|
---|
258 | gfx_typeface_t *tface;
|
---|
259 | gfx_font_t *font;
|
---|
260 | gfx_glyph_metrics_t gmetrics;
|
---|
261 | gfx_glyph_t *glyph;
|
---|
262 | gfx_context_t *gc;
|
---|
263 | test_gc_t tgc;
|
---|
264 | errno_t rc;
|
---|
265 |
|
---|
266 | rc = gfx_context_new(&test_ops, (void *) &tgc, &gc);
|
---|
267 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
268 |
|
---|
269 | rc = gfx_typeface_create(gc, &tface);
|
---|
270 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
271 |
|
---|
272 | gfx_font_props_init(&fprops);
|
---|
273 | gfx_font_metrics_init(&fmetrics);
|
---|
274 | rc = gfx_font_create(tface, &fprops, &fmetrics, &font);
|
---|
275 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
276 |
|
---|
277 | gfx_glyph_metrics_init(&gmetrics);
|
---|
278 | gmetrics.advance = 1;
|
---|
279 |
|
---|
280 | rc = gfx_glyph_create(font, &gmetrics, &glyph);
|
---|
281 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
282 |
|
---|
283 | PCUT_ASSERT_NULL(gfx_glyph_first_pattern(glyph));
|
---|
284 |
|
---|
285 | /* Set a pattern */
|
---|
286 | rc = gfx_glyph_set_pattern(glyph, "A");
|
---|
287 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
288 | PCUT_ASSERT_NOT_NULL(gfx_glyph_first_pattern(glyph));
|
---|
289 |
|
---|
290 | /* Now clear a different pattern - should be OK */
|
---|
291 | gfx_glyph_clear_pattern(glyph, "AA");
|
---|
292 | PCUT_ASSERT_NOT_NULL(gfx_glyph_first_pattern(glyph));
|
---|
293 |
|
---|
294 | /* Now clear the pattern which has been set */
|
---|
295 | gfx_glyph_clear_pattern(glyph, "A");
|
---|
296 | PCUT_ASSERT_NULL(gfx_glyph_first_pattern(glyph));
|
---|
297 |
|
---|
298 | gfx_glyph_destroy(glyph);
|
---|
299 |
|
---|
300 | gfx_font_close(font);
|
---|
301 | gfx_typeface_destroy(tface);
|
---|
302 | rc = gfx_context_delete(gc);
|
---|
303 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
304 | }
|
---|
305 |
|
---|
306 | /** Test gfx_glyph_matches() */
|
---|
307 | PCUT_TEST(matches)
|
---|
308 | {
|
---|
309 | gfx_font_props_t fprops;
|
---|
310 | gfx_font_metrics_t fmetrics;
|
---|
311 | gfx_typeface_t *tface;
|
---|
312 | gfx_font_t *font;
|
---|
313 | gfx_glyph_metrics_t gmetrics;
|
---|
314 | gfx_glyph_t *glyph;
|
---|
315 | gfx_context_t *gc;
|
---|
316 | test_gc_t tgc;
|
---|
317 | bool match;
|
---|
318 | size_t msize;
|
---|
319 | errno_t rc;
|
---|
320 |
|
---|
321 | rc = gfx_context_new(&test_ops, (void *) &tgc, &gc);
|
---|
322 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
323 |
|
---|
324 | rc = gfx_typeface_create(gc, &tface);
|
---|
325 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
326 |
|
---|
327 | gfx_font_props_init(&fprops);
|
---|
328 | gfx_font_metrics_init(&fmetrics);
|
---|
329 | rc = gfx_font_create(tface, &fprops, &fmetrics, &font);
|
---|
330 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
331 |
|
---|
332 | gfx_glyph_metrics_init(&gmetrics);
|
---|
333 | gmetrics.advance = 1;
|
---|
334 |
|
---|
335 | rc = gfx_glyph_create(font, &gmetrics, &glyph);
|
---|
336 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
337 |
|
---|
338 | PCUT_ASSERT_NULL(gfx_glyph_first_pattern(glyph));
|
---|
339 |
|
---|
340 | /* Set a pattern */
|
---|
341 | rc = gfx_glyph_set_pattern(glyph, "AB");
|
---|
342 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
343 | PCUT_ASSERT_NOT_NULL(gfx_glyph_first_pattern(glyph));
|
---|
344 |
|
---|
345 | match = gfx_glyph_matches(glyph, "A", &msize);
|
---|
346 | PCUT_ASSERT_FALSE(match);
|
---|
347 |
|
---|
348 | match = gfx_glyph_matches(glyph, "AB", &msize);
|
---|
349 | PCUT_ASSERT_TRUE(match);
|
---|
350 | PCUT_ASSERT_INT_EQUALS(2, msize);
|
---|
351 |
|
---|
352 | match = gfx_glyph_matches(glyph, "ABC", &msize);
|
---|
353 | PCUT_ASSERT_TRUE(match);
|
---|
354 | PCUT_ASSERT_INT_EQUALS(2, msize);
|
---|
355 |
|
---|
356 | match = gfx_glyph_matches(glyph, "BAB", &msize);
|
---|
357 | PCUT_ASSERT_FALSE(match);
|
---|
358 |
|
---|
359 | gfx_glyph_destroy(glyph);
|
---|
360 |
|
---|
361 | gfx_font_close(font);
|
---|
362 | gfx_typeface_destroy(tface);
|
---|
363 | rc = gfx_context_delete(gc);
|
---|
364 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
365 | }
|
---|
366 |
|
---|
367 | /** Test gfx_glyph_first_pattern(), gfx_glyph_next_pattern() */
|
---|
368 | PCUT_TEST(first_next_pattern)
|
---|
369 | {
|
---|
370 | gfx_font_props_t fprops;
|
---|
371 | gfx_font_metrics_t fmetrics;
|
---|
372 | gfx_typeface_t *tface;
|
---|
373 | gfx_font_t *font;
|
---|
374 | gfx_glyph_metrics_t gmetrics;
|
---|
375 | gfx_glyph_t *glyph;
|
---|
376 | gfx_context_t *gc;
|
---|
377 | test_gc_t tgc;
|
---|
378 | gfx_glyph_pattern_t *pat;
|
---|
379 | errno_t rc;
|
---|
380 |
|
---|
381 | rc = gfx_context_new(&test_ops, (void *) &tgc, &gc);
|
---|
382 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
383 |
|
---|
384 | rc = gfx_typeface_create(gc, &tface);
|
---|
385 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
386 |
|
---|
387 | gfx_font_props_init(&fprops);
|
---|
388 | gfx_font_metrics_init(&fmetrics);
|
---|
389 | rc = gfx_font_create(tface, &fprops, &fmetrics, &font);
|
---|
390 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
391 |
|
---|
392 | gfx_glyph_metrics_init(&gmetrics);
|
---|
393 | gmetrics.advance = 1;
|
---|
394 |
|
---|
395 | rc = gfx_glyph_create(font, &gmetrics, &glyph);
|
---|
396 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
397 |
|
---|
398 | PCUT_ASSERT_NULL(gfx_glyph_first_pattern(glyph));
|
---|
399 |
|
---|
400 | /* Set a pattern */
|
---|
401 | rc = gfx_glyph_set_pattern(glyph, "A");
|
---|
402 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
403 | PCUT_ASSERT_NOT_NULL(gfx_glyph_first_pattern(glyph));
|
---|
404 |
|
---|
405 | pat = gfx_glyph_first_pattern(glyph);
|
---|
406 | PCUT_ASSERT_NOT_NULL(pat);
|
---|
407 |
|
---|
408 | pat = gfx_glyph_next_pattern(pat);
|
---|
409 | PCUT_ASSERT_NULL(pat);
|
---|
410 |
|
---|
411 | gfx_glyph_destroy(glyph);
|
---|
412 |
|
---|
413 | gfx_font_close(font);
|
---|
414 | gfx_typeface_destroy(tface);
|
---|
415 | rc = gfx_context_delete(gc);
|
---|
416 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
417 | }
|
---|
418 |
|
---|
419 | /** Test gfx_glyph_pattern_str() */
|
---|
420 | PCUT_TEST(pattern_str)
|
---|
421 | {
|
---|
422 | gfx_font_props_t fprops;
|
---|
423 | gfx_font_metrics_t fmetrics;
|
---|
424 | gfx_typeface_t *tface;
|
---|
425 | gfx_font_t *font;
|
---|
426 | gfx_glyph_metrics_t gmetrics;
|
---|
427 | gfx_glyph_t *glyph;
|
---|
428 | gfx_context_t *gc;
|
---|
429 | test_gc_t tgc;
|
---|
430 | gfx_glyph_pattern_t *pat;
|
---|
431 | const char *pstr;
|
---|
432 | errno_t rc;
|
---|
433 |
|
---|
434 | rc = gfx_context_new(&test_ops, (void *) &tgc, &gc);
|
---|
435 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
436 |
|
---|
437 | rc = gfx_typeface_create(gc, &tface);
|
---|
438 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
439 |
|
---|
440 | gfx_font_props_init(&fprops);
|
---|
441 | gfx_font_metrics_init(&fmetrics);
|
---|
442 | rc = gfx_font_create(tface, &fprops, &fmetrics, &font);
|
---|
443 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
444 |
|
---|
445 | gfx_glyph_metrics_init(&gmetrics);
|
---|
446 | gmetrics.advance = 1;
|
---|
447 |
|
---|
448 | rc = gfx_glyph_create(font, &gmetrics, &glyph);
|
---|
449 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
450 |
|
---|
451 | PCUT_ASSERT_NULL(gfx_glyph_first_pattern(glyph));
|
---|
452 |
|
---|
453 | /* Set a pattern */
|
---|
454 | rc = gfx_glyph_set_pattern(glyph, "A");
|
---|
455 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
456 | PCUT_ASSERT_NOT_NULL(gfx_glyph_first_pattern(glyph));
|
---|
457 |
|
---|
458 | pat = gfx_glyph_first_pattern(glyph);
|
---|
459 | PCUT_ASSERT_NOT_NULL(pat);
|
---|
460 |
|
---|
461 | pstr = gfx_glyph_pattern_str(pat);
|
---|
462 | PCUT_ASSERT_INT_EQUALS(0, str_cmp("A", pstr));
|
---|
463 |
|
---|
464 | gfx_glyph_destroy(glyph);
|
---|
465 |
|
---|
466 | gfx_font_close(font);
|
---|
467 | gfx_typeface_destroy(tface);
|
---|
468 | rc = gfx_context_delete(gc);
|
---|
469 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
470 | }
|
---|
471 |
|
---|
472 | /** Test gfx_glyph_transfer() */
|
---|
473 | PCUT_TEST(transfer)
|
---|
474 | {
|
---|
475 | gfx_font_props_t fprops;
|
---|
476 | gfx_font_metrics_t fmetrics;
|
---|
477 | gfx_typeface_t *tface;
|
---|
478 | gfx_font_t *font;
|
---|
479 | gfx_glyph_metrics_t gmetrics;
|
---|
480 | gfx_glyph_t *glyph;
|
---|
481 | gfx_context_t *gc;
|
---|
482 | gfx_bitmap_t *bitmap;
|
---|
483 | gfx_bitmap_params_t params;
|
---|
484 | gfx_bitmap_alloc_t alloc;
|
---|
485 | gfx_glyph_bmp_t *bmp;
|
---|
486 | pixelmap_t pmap;
|
---|
487 | pixel_t pixel;
|
---|
488 | test_gc_t tgc;
|
---|
489 | errno_t rc;
|
---|
490 |
|
---|
491 | rc = gfx_context_new(&test_ops, (void *) &tgc, &gc);
|
---|
492 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
493 |
|
---|
494 | rc = gfx_typeface_create(gc, &tface);
|
---|
495 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
496 |
|
---|
497 | gfx_font_props_init(&fprops);
|
---|
498 | gfx_font_metrics_init(&fmetrics);
|
---|
499 | rc = gfx_font_create(tface, &fprops, &fmetrics, &font);
|
---|
500 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
501 |
|
---|
502 | gfx_glyph_metrics_init(&gmetrics);
|
---|
503 | gmetrics.advance = 1;
|
---|
504 |
|
---|
505 | rc = gfx_glyph_create(font, &gmetrics, &glyph);
|
---|
506 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
507 |
|
---|
508 | /*
|
---|
509 | * We need to fill some pixels of the glyph.
|
---|
510 | * We'll use the glyph bitmap for that.
|
---|
511 | * That means this test won't pass unless glyph
|
---|
512 | * bitmap works.
|
---|
513 | */
|
---|
514 |
|
---|
515 | rc = gfx_glyph_bmp_open(glyph, &bmp);
|
---|
516 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
517 | PCUT_ASSERT_NOT_NULL(bmp);
|
---|
518 |
|
---|
519 | rc = gfx_glyph_bmp_setpix(bmp, 0, 0, 1);
|
---|
520 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
521 |
|
---|
522 | rc = gfx_glyph_bmp_setpix(bmp, 1, 1, 1);
|
---|
523 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
524 |
|
---|
525 | rc = gfx_glyph_bmp_save(bmp);
|
---|
526 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
527 | gfx_glyph_bmp_close(bmp);
|
---|
528 |
|
---|
529 | /* Now create a new bitmap */
|
---|
530 |
|
---|
531 | gfx_bitmap_params_init(¶ms);
|
---|
532 | params.rect.p0.x = 0;
|
---|
533 | params.rect.p0.y = 0;
|
---|
534 | params.rect.p1.x = 10;
|
---|
535 | params.rect.p1.y = 10;
|
---|
536 | rc = gfx_bitmap_create(gc, ¶ms, NULL, &bitmap);
|
---|
537 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
538 |
|
---|
539 | rc = gfx_bitmap_get_alloc(bitmap, &alloc);
|
---|
540 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
541 |
|
---|
542 | /* Transfer the glyph to new bitmap */
|
---|
543 | rc = gfx_glyph_transfer(glyph, 0, bitmap, ¶ms.rect);
|
---|
544 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
545 |
|
---|
546 | /* Now let's read pixels from the new bitmap */
|
---|
547 | pmap.width = params.rect.p1.x;
|
---|
548 | pmap.height = params.rect.p1.y;
|
---|
549 | pmap.data = alloc.pixels;
|
---|
550 |
|
---|
551 | pixel = pixelmap_get_pixel(&pmap, 0, 0);
|
---|
552 | PCUT_ASSERT_INT_EQUALS(PIXEL(255, 255, 255, 255), pixel);
|
---|
553 |
|
---|
554 | pixel = pixelmap_get_pixel(&pmap, 1, 1);
|
---|
555 | PCUT_ASSERT_INT_EQUALS(PIXEL(255, 255, 255, 255), pixel);
|
---|
556 |
|
---|
557 | pixel = pixelmap_get_pixel(&pmap, 1, 0);
|
---|
558 | PCUT_ASSERT_INT_EQUALS(PIXEL(0, 0, 0, 0), pixel);
|
---|
559 |
|
---|
560 | pixel = pixelmap_get_pixel(&pmap, 0, 1);
|
---|
561 | PCUT_ASSERT_INT_EQUALS(PIXEL(0, 0, 0, 0), pixel);
|
---|
562 |
|
---|
563 | gfx_glyph_destroy(glyph);
|
---|
564 |
|
---|
565 | gfx_font_close(font);
|
---|
566 | gfx_typeface_destroy(tface);
|
---|
567 | rc = gfx_context_delete(gc);
|
---|
568 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
569 | }
|
---|
570 |
|
---|
571 | static errno_t testgc_set_color(void *arg, gfx_color_t *color)
|
---|
572 | {
|
---|
573 | return EOK;
|
---|
574 | }
|
---|
575 |
|
---|
576 | static errno_t testgc_fill_rect(void *arg, gfx_rect_t *rect)
|
---|
577 | {
|
---|
578 | return EOK;
|
---|
579 | }
|
---|
580 |
|
---|
581 | static errno_t testgc_bitmap_create(void *arg, gfx_bitmap_params_t *params,
|
---|
582 | gfx_bitmap_alloc_t *alloc, void **rbm)
|
---|
583 | {
|
---|
584 | test_gc_t *tgc = (test_gc_t *) arg;
|
---|
585 | testgc_bitmap_t *tbm;
|
---|
586 |
|
---|
587 | tbm = calloc(1, sizeof(testgc_bitmap_t));
|
---|
588 | if (tbm == NULL)
|
---|
589 | return ENOMEM;
|
---|
590 |
|
---|
591 | if (alloc == NULL) {
|
---|
592 | tbm->alloc.pitch = (params->rect.p1.x - params->rect.p0.x) *
|
---|
593 | sizeof(uint32_t);
|
---|
594 | tbm->alloc.off0 = 0;
|
---|
595 | tbm->alloc.pixels = calloc(sizeof(uint32_t),
|
---|
596 | tbm->alloc.pitch * (params->rect.p1.y - params->rect.p0.y));
|
---|
597 | tbm->myalloc = true;
|
---|
598 | if (tbm->alloc.pixels == NULL) {
|
---|
599 | free(tbm);
|
---|
600 | return ENOMEM;
|
---|
601 | }
|
---|
602 | } else {
|
---|
603 | tbm->alloc = *alloc;
|
---|
604 | }
|
---|
605 |
|
---|
606 | tbm->tgc = tgc;
|
---|
607 | tgc->bm_params = *params;
|
---|
608 | tgc->bm_pixels = tbm->alloc.pixels;
|
---|
609 | *rbm = (void *)tbm;
|
---|
610 | return EOK;
|
---|
611 | }
|
---|
612 |
|
---|
613 | static errno_t testgc_bitmap_destroy(void *bm)
|
---|
614 | {
|
---|
615 | testgc_bitmap_t *tbm = (testgc_bitmap_t *)bm;
|
---|
616 | if (tbm->myalloc)
|
---|
617 | free(tbm->alloc.pixels);
|
---|
618 | free(tbm);
|
---|
619 | return EOK;
|
---|
620 | }
|
---|
621 |
|
---|
622 | static errno_t testgc_bitmap_render(void *bm, gfx_rect_t *srect,
|
---|
623 | gfx_coord2_t *offs)
|
---|
624 | {
|
---|
625 | testgc_bitmap_t *tbm = (testgc_bitmap_t *)bm;
|
---|
626 | tbm->tgc->bm_srect = *srect;
|
---|
627 | tbm->tgc->bm_offs = *offs;
|
---|
628 | return EOK;
|
---|
629 | }
|
---|
630 |
|
---|
631 | static errno_t testgc_bitmap_get_alloc(void *bm, gfx_bitmap_alloc_t *alloc)
|
---|
632 | {
|
---|
633 | testgc_bitmap_t *tbm = (testgc_bitmap_t *)bm;
|
---|
634 | *alloc = tbm->alloc;
|
---|
635 | return EOK;
|
---|
636 | }
|
---|
637 |
|
---|
638 | PCUT_EXPORT(glyph);
|
---|