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

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

Only save minimum used rectangle of glyph bitmap

When the image becomes smaller (clearing pixels), we don't want to keep
saving zero pixels.

  • Property mode set to 100644
File size: 15.5 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 "../private/glyph_bmp.h"
36
37PCUT_INIT;
38
39PCUT_TEST_SUITE(glyph_bmp);
40
41static errno_t testgc_set_color(void *, gfx_color_t *);
42static errno_t testgc_fill_rect(void *, gfx_rect_t *);
43static errno_t testgc_bitmap_create(void *, gfx_bitmap_params_t *,
44 gfx_bitmap_alloc_t *, void **);
45static errno_t testgc_bitmap_destroy(void *);
46static errno_t testgc_bitmap_render(void *, gfx_rect_t *, gfx_coord2_t *);
47static errno_t testgc_bitmap_get_alloc(void *, gfx_bitmap_alloc_t *);
48
49static gfx_context_ops_t test_ops = {
50 .set_color = testgc_set_color,
51 .fill_rect = testgc_fill_rect,
52 .bitmap_create = testgc_bitmap_create,
53 .bitmap_destroy = testgc_bitmap_destroy,
54 .bitmap_render = testgc_bitmap_render,
55 .bitmap_get_alloc = testgc_bitmap_get_alloc
56};
57
58typedef struct {
59 gfx_bitmap_params_t bm_params;
60 void *bm_pixels;
61 gfx_rect_t bm_srect;
62 gfx_coord2_t bm_offs;
63} test_gc_t;
64
65typedef struct {
66 test_gc_t *tgc;
67 gfx_bitmap_alloc_t alloc;
68 bool myalloc;
69} testgc_bitmap_t;
70
71/** Test opening and closing glyph bitmap */
72PCUT_TEST(open_close)
73{
74 gfx_font_props_t fprops;
75 gfx_font_metrics_t fmetrics;
76 gfx_typeface_t *tface;
77 gfx_font_t *font;
78 gfx_glyph_metrics_t gmetrics;
79 gfx_glyph_t *glyph;
80 gfx_context_t *gc;
81 gfx_glyph_bmp_t *bmp;
82 test_gc_t tgc;
83 errno_t rc;
84
85 rc = gfx_context_new(&test_ops, (void *) &tgc, &gc);
86 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
87
88 rc = gfx_typeface_create(gc, &tface);
89 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
90
91 gfx_font_props_init(&fprops);
92 gfx_font_metrics_init(&fmetrics);
93 rc = gfx_font_create(tface, &fprops, &fmetrics, &font);
94 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
95
96 gfx_glyph_metrics_init(&gmetrics);
97 gmetrics.advance = 1;
98
99 rc = gfx_glyph_create(font, &gmetrics, &glyph);
100 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
101
102 bmp = NULL;
103
104 rc = gfx_glyph_bmp_open(glyph, &bmp);
105 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
106 PCUT_ASSERT_NOT_NULL(bmp);
107
108 gfx_glyph_bmp_close(bmp);
109
110 gfx_glyph_destroy(glyph);
111
112 gfx_font_close(font);
113 gfx_typeface_destroy(tface);
114 rc = gfx_context_delete(gc);
115 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
116}
117
118/** Test glyph_bmp_save() */
119PCUT_TEST(save)
120{
121 gfx_font_props_t fprops;
122 gfx_font_metrics_t fmetrics;
123 gfx_typeface_t *tface;
124 gfx_font_t *font;
125 gfx_glyph_metrics_t gmetrics;
126 gfx_glyph_t *glyph;
127 gfx_context_t *gc;
128 gfx_glyph_bmp_t *bmp;
129 int pix;
130 test_gc_t tgc;
131 errno_t rc;
132
133 rc = gfx_context_new(&test_ops, (void *) &tgc, &gc);
134 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
135
136 rc = gfx_typeface_create(gc, &tface);
137 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
138
139 gfx_font_props_init(&fprops);
140 gfx_font_metrics_init(&fmetrics);
141 rc = gfx_font_create(tface, &fprops, &fmetrics, &font);
142 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
143
144 gfx_glyph_metrics_init(&gmetrics);
145 gmetrics.advance = 1;
146
147 rc = gfx_glyph_create(font, &gmetrics, &glyph);
148 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
149
150 bmp = NULL;
151
152 /* Open bitmap and set some pixels */
153
154 rc = gfx_glyph_bmp_open(glyph, &bmp);
155 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
156 PCUT_ASSERT_NOT_NULL(bmp);
157
158 rc = gfx_glyph_bmp_setpix(bmp, 0, 0, 1);
159 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
160
161 rc = gfx_glyph_bmp_setpix(bmp, 1, 1, 1);
162 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
163
164 rc = gfx_glyph_bmp_save(bmp);
165 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
166
167 gfx_glyph_bmp_close(bmp);
168 bmp = NULL;
169
170 /* Re-open the saved bimap and verify pixel values were preserved */
171
172 rc = gfx_glyph_bmp_open(glyph, &bmp);
173 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
174 PCUT_ASSERT_NOT_NULL(bmp);
175
176 pix = gfx_glyph_bmp_getpix(bmp, 0, 0);
177 PCUT_ASSERT_INT_EQUALS(1, pix);
178
179 pix = gfx_glyph_bmp_getpix(bmp, 1, 1);
180 PCUT_ASSERT_INT_EQUALS(1, pix);
181
182 pix = gfx_glyph_bmp_getpix(bmp, 1, 0);
183 PCUT_ASSERT_INT_EQUALS(0, pix);
184
185 pix = gfx_glyph_bmp_getpix(bmp, 0, 1);
186 PCUT_ASSERT_INT_EQUALS(0, pix);
187
188 /* ... */
189
190 rc = gfx_glyph_bmp_setpix(bmp, 1, -1, 1);
191 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
192
193 rc = gfx_glyph_bmp_save(bmp);
194 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
195
196 gfx_glyph_bmp_close(bmp);
197
198 /* Once again */
199
200 rc = gfx_glyph_bmp_open(glyph, &bmp);
201 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
202 PCUT_ASSERT_NOT_NULL(bmp);
203
204 pix = gfx_glyph_bmp_getpix(bmp, 0, 0);
205 PCUT_ASSERT_INT_EQUALS(1, pix);
206
207 pix = gfx_glyph_bmp_getpix(bmp, 1, 1);
208 PCUT_ASSERT_INT_EQUALS(1, pix);
209
210 pix = gfx_glyph_bmp_getpix(bmp, 1, 0);
211 PCUT_ASSERT_INT_EQUALS(0, pix);
212
213 pix = gfx_glyph_bmp_getpix(bmp, 0, 1);
214 PCUT_ASSERT_INT_EQUALS(0, pix);
215
216 pix = gfx_glyph_bmp_getpix(bmp, 1, -1);
217 PCUT_ASSERT_INT_EQUALS(1, pix);
218
219 pix = gfx_glyph_bmp_getpix(bmp, 0, -1);
220 PCUT_ASSERT_INT_EQUALS(0, pix);
221
222 gfx_glyph_destroy(glyph);
223
224 gfx_font_close(font);
225 gfx_typeface_destroy(tface);
226 rc = gfx_context_delete(gc);
227 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
228}
229
230/** Test glyph_bmp_getpix() */
231PCUT_TEST(getpix)
232{
233 gfx_font_props_t fprops;
234 gfx_font_metrics_t fmetrics;
235 gfx_typeface_t *tface;
236 gfx_font_t *font;
237 gfx_glyph_metrics_t gmetrics;
238 gfx_glyph_t *glyph;
239 gfx_context_t *gc;
240 gfx_glyph_bmp_t *bmp;
241 test_gc_t tgc;
242 int pix;
243 errno_t rc;
244
245 rc = gfx_context_new(&test_ops, (void *) &tgc, &gc);
246 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
247
248 rc = gfx_typeface_create(gc, &tface);
249 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
250
251 gfx_font_props_init(&fprops);
252 gfx_font_metrics_init(&fmetrics);
253 rc = gfx_font_create(tface, &fprops, &fmetrics, &font);
254 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
255
256 gfx_glyph_metrics_init(&gmetrics);
257 gmetrics.advance = 1;
258
259 rc = gfx_glyph_create(font, &gmetrics, &glyph);
260 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
261
262 bmp = NULL;
263
264 rc = gfx_glyph_bmp_open(glyph, &bmp);
265 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
266 PCUT_ASSERT_NOT_NULL(bmp);
267
268 pix = gfx_glyph_bmp_getpix(bmp, 0, 0);
269 PCUT_ASSERT_INT_EQUALS(0, pix);
270
271 gfx_glyph_bmp_close(bmp);
272
273 gfx_glyph_destroy(glyph);
274
275 gfx_font_close(font);
276 gfx_typeface_destroy(tface);
277 rc = gfx_context_delete(gc);
278 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
279}
280
281/** Test glyph_bmp_setpix() can flip pixel value both ways */
282PCUT_TEST(setpix_flip)
283{
284 gfx_font_props_t fprops;
285 gfx_font_metrics_t fmetrics;
286 gfx_typeface_t *tface;
287 gfx_font_t *font;
288 gfx_glyph_metrics_t gmetrics;
289 gfx_glyph_t *glyph;
290 gfx_context_t *gc;
291 gfx_glyph_bmp_t *bmp;
292 test_gc_t tgc;
293 int pix;
294 errno_t rc;
295
296 rc = gfx_context_new(&test_ops, (void *) &tgc, &gc);
297 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
298
299 rc = gfx_typeface_create(gc, &tface);
300 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
301
302 gfx_font_props_init(&fprops);
303 gfx_font_metrics_init(&fmetrics);
304 rc = gfx_font_create(tface, &fprops, &fmetrics, &font);
305 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
306
307 gfx_glyph_metrics_init(&gmetrics);
308 gmetrics.advance = 1;
309
310 rc = gfx_glyph_create(font, &gmetrics, &glyph);
311 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
312
313 bmp = NULL;
314
315 rc = gfx_glyph_bmp_open(glyph, &bmp);
316 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
317 PCUT_ASSERT_NOT_NULL(bmp);
318
319 pix = gfx_glyph_bmp_getpix(bmp, 0, 0);
320 PCUT_ASSERT_INT_EQUALS(0, pix);
321
322 rc = gfx_glyph_bmp_setpix(bmp, 0, 0, 1);
323 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
324
325 pix = gfx_glyph_bmp_getpix(bmp, 0, 0);
326 PCUT_ASSERT_INT_EQUALS(1, pix);
327
328 rc = gfx_glyph_bmp_setpix(bmp, 0, 0, 0);
329 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
330
331 pix = gfx_glyph_bmp_getpix(bmp, 0, 0);
332 PCUT_ASSERT_INT_EQUALS(0, pix);
333
334 gfx_glyph_bmp_close(bmp);
335
336 gfx_glyph_destroy(glyph);
337
338 gfx_font_close(font);
339 gfx_typeface_destroy(tface);
340 rc = gfx_context_delete(gc);
341 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
342}
343
344/** Test glyph_bmp_setpix() properly extends pixel array */
345PCUT_TEST(setpix_extend)
346{
347 gfx_font_props_t fprops;
348 gfx_font_metrics_t fmetrics;
349 gfx_typeface_t *tface;
350 gfx_font_t *font;
351 gfx_glyph_metrics_t gmetrics;
352 gfx_glyph_t *glyph;
353 gfx_context_t *gc;
354 gfx_glyph_bmp_t *bmp;
355 gfx_coord_t x, y;
356 test_gc_t tgc;
357 int pix;
358 errno_t rc;
359
360 rc = gfx_context_new(&test_ops, (void *) &tgc, &gc);
361 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
362
363 rc = gfx_typeface_create(gc, &tface);
364 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
365
366 gfx_font_props_init(&fprops);
367 gfx_font_metrics_init(&fmetrics);
368 rc = gfx_font_create(tface, &fprops, &fmetrics, &font);
369 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
370
371 gfx_glyph_metrics_init(&gmetrics);
372 gmetrics.advance = 1;
373
374 rc = gfx_glyph_create(font, &gmetrics, &glyph);
375 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
376
377 bmp = NULL;
378
379 rc = gfx_glyph_bmp_open(glyph, &bmp);
380 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
381 PCUT_ASSERT_NOT_NULL(bmp);
382
383 /*
384 * Fill the rectangle [0, 0]..[3, 3] with alternating pixel pattern
385 * and then check it.
386 */
387
388 rc = gfx_glyph_bmp_setpix(bmp, 0, 0, 1);
389 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
390
391 rc = gfx_glyph_bmp_setpix(bmp, 1, 1, 1);
392 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
393
394 rc = gfx_glyph_bmp_setpix(bmp, 2, 0, 1);
395 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
396
397 rc = gfx_glyph_bmp_setpix(bmp, 0, 2, 1);
398 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
399
400 rc = gfx_glyph_bmp_setpix(bmp, 2, 2, 1);
401 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
402
403 for (y = 0; y <= 2; y++) {
404 for (x = 0; x <= 2; x++) {
405 pix = gfx_glyph_bmp_getpix(bmp, x, y);
406 PCUT_ASSERT_INT_EQUALS((x & 1) ^ (y & 1) ^ 1, pix);
407 }
408 }
409
410 gfx_glyph_bmp_close(bmp);
411
412 gfx_glyph_destroy(glyph);
413
414 gfx_font_close(font);
415 gfx_typeface_destroy(tface);
416 rc = gfx_context_delete(gc);
417 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
418}
419
420/** Test glyph_bmp_clear() properly clears bitmap */
421PCUT_TEST(clear)
422{
423 gfx_font_props_t fprops;
424 gfx_font_metrics_t fmetrics;
425 gfx_typeface_t *tface;
426 gfx_font_t *font;
427 gfx_glyph_metrics_t gmetrics;
428 gfx_glyph_t *glyph;
429 gfx_context_t *gc;
430 gfx_glyph_bmp_t *bmp;
431 gfx_rect_t rect;
432 test_gc_t tgc;
433 int pix;
434 errno_t rc;
435
436 rc = gfx_context_new(&test_ops, (void *) &tgc, &gc);
437 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
438
439 rc = gfx_typeface_create(gc, &tface);
440 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
441
442 gfx_font_props_init(&fprops);
443 gfx_font_metrics_init(&fmetrics);
444 rc = gfx_font_create(tface, &fprops, &fmetrics, &font);
445 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
446
447 gfx_glyph_metrics_init(&gmetrics);
448 gmetrics.advance = 1;
449
450 rc = gfx_glyph_create(font, &gmetrics, &glyph);
451 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
452
453 bmp = NULL;
454
455 rc = gfx_glyph_bmp_open(glyph, &bmp);
456 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
457 PCUT_ASSERT_NOT_NULL(bmp);
458
459 /* Set some pixels */
460
461 rc = gfx_glyph_bmp_setpix(bmp, 0, 0, 1);
462 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
463
464 rc = gfx_glyph_bmp_setpix(bmp, 1, 1, 1);
465 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
466
467 /* Clear the bitmap and check */
468 gfx_glyph_bmp_clear(bmp);
469
470 gfx_glyph_bmp_get_rect(bmp, &rect);
471 PCUT_ASSERT_INT_EQUALS(0, rect.p0.x);
472 PCUT_ASSERT_INT_EQUALS(0, rect.p0.y);
473 PCUT_ASSERT_INT_EQUALS(0, rect.p1.x);
474 PCUT_ASSERT_INT_EQUALS(0, rect.p1.y);
475
476 pix = gfx_glyph_bmp_getpix(bmp, 0, 0);
477 PCUT_ASSERT_INT_EQUALS(0, pix);
478 pix = gfx_glyph_bmp_getpix(bmp, 1, 1);
479 PCUT_ASSERT_INT_EQUALS(0, pix);
480
481 gfx_glyph_bmp_close(bmp);
482
483 gfx_glyph_destroy(glyph);
484
485 gfx_font_close(font);
486 gfx_typeface_destroy(tface);
487 rc = gfx_context_delete(gc);
488 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
489}
490
491/** Test glyph_bmp_find_used_rect() find minimum used rectangle */
492PCUT_TEST(find_used_rect)
493{
494 gfx_font_props_t fprops;
495 gfx_font_metrics_t fmetrics;
496 gfx_typeface_t *tface;
497 gfx_font_t *font;
498 gfx_glyph_metrics_t gmetrics;
499 gfx_glyph_t *glyph;
500 gfx_context_t *gc;
501 gfx_glyph_bmp_t *bmp;
502 gfx_rect_t rect;
503 test_gc_t tgc;
504 errno_t rc;
505
506 rc = gfx_context_new(&test_ops, (void *) &tgc, &gc);
507 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
508
509 rc = gfx_typeface_create(gc, &tface);
510 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
511
512 gfx_font_props_init(&fprops);
513 gfx_font_metrics_init(&fmetrics);
514 rc = gfx_font_create(tface, &fprops, &fmetrics, &font);
515 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
516
517 gfx_glyph_metrics_init(&gmetrics);
518 gmetrics.advance = 1;
519
520 rc = gfx_glyph_create(font, &gmetrics, &glyph);
521 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
522
523 bmp = NULL;
524
525 rc = gfx_glyph_bmp_open(glyph, &bmp);
526 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
527 PCUT_ASSERT_NOT_NULL(bmp);
528
529 /* Check used rectangle */
530
531 gfx_glyph_bmp_find_used_rect(bmp, &rect);
532 PCUT_ASSERT_INT_EQUALS(0, rect.p0.x);
533 PCUT_ASSERT_INT_EQUALS(0, rect.p0.y);
534 PCUT_ASSERT_INT_EQUALS(0, rect.p1.x);
535 PCUT_ASSERT_INT_EQUALS(0, rect.p1.y);
536
537 /* Set some pixels */
538
539 rc = gfx_glyph_bmp_setpix(bmp, -4, -5, 1);
540 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
541
542 rc = gfx_glyph_bmp_setpix(bmp, -2, -1, 1);
543 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
544
545 rc = gfx_glyph_bmp_setpix(bmp, 3, 4, 1);
546 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
547
548 rc = gfx_glyph_bmp_setpix(bmp, 7, 6, 1);
549 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
550
551 /* Check used rectangle */
552
553 gfx_glyph_bmp_find_used_rect(bmp, &rect);
554 PCUT_ASSERT_INT_EQUALS(-4, rect.p0.x);
555 PCUT_ASSERT_INT_EQUALS(-5, rect.p0.y);
556 PCUT_ASSERT_INT_EQUALS(8, rect.p1.x);
557 PCUT_ASSERT_INT_EQUALS(7, rect.p1.y);
558
559 /* Clear the corner pixels */
560
561 rc = gfx_glyph_bmp_setpix(bmp, -4, -5, 0);
562 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
563
564 rc = gfx_glyph_bmp_setpix(bmp, 7, 6, 0);
565 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
566
567 /* Check used rectangle got smaller */
568
569 gfx_glyph_bmp_find_used_rect(bmp, &rect);
570 PCUT_ASSERT_INT_EQUALS(-2, rect.p0.x);
571 PCUT_ASSERT_INT_EQUALS(-1, rect.p0.y);
572 PCUT_ASSERT_INT_EQUALS(4, rect.p1.x);
573 PCUT_ASSERT_INT_EQUALS(5, rect.p1.y);
574
575 gfx_glyph_bmp_close(bmp);
576
577 gfx_glyph_destroy(glyph);
578
579 gfx_font_close(font);
580 gfx_typeface_destroy(tface);
581 rc = gfx_context_delete(gc);
582 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
583}
584
585static errno_t testgc_set_color(void *arg, gfx_color_t *color)
586{
587 return EOK;
588}
589
590static errno_t testgc_fill_rect(void *arg, gfx_rect_t *rect)
591{
592 return EOK;
593}
594
595static errno_t testgc_bitmap_create(void *arg, gfx_bitmap_params_t *params,
596 gfx_bitmap_alloc_t *alloc, void **rbm)
597{
598 test_gc_t *tgc = (test_gc_t *) arg;
599 testgc_bitmap_t *tbm;
600
601 tbm = calloc(1, sizeof(testgc_bitmap_t));
602 if (tbm == NULL)
603 return ENOMEM;
604
605 if (alloc == NULL) {
606 tbm->alloc.pitch = (params->rect.p1.x - params->rect.p0.x) *
607 sizeof(uint32_t);
608 tbm->alloc.off0 = 0;
609 tbm->alloc.pixels = calloc(sizeof(uint32_t),
610 tbm->alloc.pitch * (params->rect.p1.y - params->rect.p0.y));
611 tbm->myalloc = true;
612 if (tbm->alloc.pixels == NULL) {
613 free(tbm);
614 return ENOMEM;
615 }
616 } else {
617 tbm->alloc = *alloc;
618 }
619
620 tbm->tgc = tgc;
621 tgc->bm_params = *params;
622 tgc->bm_pixels = tbm->alloc.pixels;
623 *rbm = (void *)tbm;
624 return EOK;
625}
626
627static errno_t testgc_bitmap_destroy(void *bm)
628{
629 testgc_bitmap_t *tbm = (testgc_bitmap_t *)bm;
630 if (tbm->myalloc)
631 free(tbm->alloc.pixels);
632 free(tbm);
633 return EOK;
634}
635
636static errno_t testgc_bitmap_render(void *bm, gfx_rect_t *srect,
637 gfx_coord2_t *offs)
638{
639 testgc_bitmap_t *tbm = (testgc_bitmap_t *)bm;
640 tbm->tgc->bm_srect = *srect;
641 tbm->tgc->bm_offs = *offs;
642 return EOK;
643}
644
645static errno_t testgc_bitmap_get_alloc(void *bm, gfx_bitmap_alloc_t *alloc)
646{
647 testgc_bitmap_t *tbm = (testgc_bitmap_t *)bm;
648 *alloc = tbm->alloc;
649 return EOK;
650}
651
652PCUT_EXPORT(glyph_bmp);
Note: See TracBrowser for help on using the repository browser.