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