| 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/typeface.h>
|
|---|
| 33 | #include <pcut/pcut.h>
|
|---|
| 34 | #include "../private/font.h"
|
|---|
| 35 | #include "../private/typeface.h"
|
|---|
| 36 |
|
|---|
| 37 | PCUT_INIT;
|
|---|
| 38 |
|
|---|
| 39 | PCUT_TEST_SUITE(font);
|
|---|
| 40 |
|
|---|
| 41 | static errno_t testgc_set_color(void *, gfx_color_t *);
|
|---|
| 42 | static errno_t testgc_fill_rect(void *, gfx_rect_t *);
|
|---|
| 43 | static errno_t testgc_bitmap_create(void *, gfx_bitmap_params_t *,
|
|---|
| 44 | gfx_bitmap_alloc_t *, void **);
|
|---|
| 45 | static errno_t testgc_bitmap_destroy(void *);
|
|---|
| 46 | static errno_t testgc_bitmap_render(void *, gfx_rect_t *, gfx_coord2_t *);
|
|---|
| 47 | static errno_t testgc_bitmap_get_alloc(void *, gfx_bitmap_alloc_t *);
|
|---|
| 48 |
|
|---|
| 49 | static 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 |
|
|---|
| 58 | typedef 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 |
|
|---|
| 65 | typedef struct {
|
|---|
| 66 | test_gc_t *tgc;
|
|---|
| 67 | gfx_bitmap_alloc_t alloc;
|
|---|
| 68 | bool myalloc;
|
|---|
| 69 | } testgc_bitmap_t;
|
|---|
| 70 |
|
|---|
| 71 | /** Test creating and destroying font */
|
|---|
| 72 | PCUT_TEST(create_destroy)
|
|---|
| 73 | {
|
|---|
| 74 | gfx_font_props_t props;
|
|---|
| 75 | gfx_font_metrics_t metrics;
|
|---|
| 76 | gfx_typeface_t *tface;
|
|---|
| 77 | gfx_font_t *font;
|
|---|
| 78 | gfx_context_t *gc;
|
|---|
| 79 | test_gc_t tgc;
|
|---|
| 80 | errno_t rc;
|
|---|
| 81 |
|
|---|
| 82 | rc = gfx_context_new(&test_ops, (void *)&tgc, &gc);
|
|---|
| 83 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
|---|
| 84 |
|
|---|
| 85 | rc = gfx_typeface_create(gc, &tface);
|
|---|
| 86 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
|---|
| 87 |
|
|---|
| 88 | gfx_font_props_init(&props);
|
|---|
| 89 | gfx_font_metrics_init(&metrics);
|
|---|
| 90 | rc = gfx_font_create(tface, &props, &metrics, &font);
|
|---|
| 91 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
|---|
| 92 |
|
|---|
| 93 | gfx_font_close(font);
|
|---|
| 94 | gfx_typeface_destroy(tface);
|
|---|
| 95 |
|
|---|
| 96 | rc = gfx_context_delete(gc);
|
|---|
| 97 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
|---|
| 98 | }
|
|---|
| 99 |
|
|---|
| 100 | /** Test gfx_font_get_metrics() */
|
|---|
| 101 | PCUT_TEST(get_metrics)
|
|---|
| 102 | {
|
|---|
| 103 | gfx_font_props_t props;
|
|---|
| 104 | gfx_font_metrics_t metrics;
|
|---|
| 105 | gfx_font_metrics_t gmetrics;
|
|---|
| 106 | gfx_typeface_t *tface;
|
|---|
| 107 | gfx_font_t *font;
|
|---|
| 108 | gfx_context_t *gc;
|
|---|
| 109 | test_gc_t tgc;
|
|---|
| 110 | errno_t rc;
|
|---|
| 111 |
|
|---|
| 112 | rc = gfx_context_new(&test_ops, (void *)&tgc, &gc);
|
|---|
| 113 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
|---|
| 114 |
|
|---|
| 115 | gfx_font_metrics_init(&metrics);
|
|---|
| 116 | metrics.ascent = 1;
|
|---|
| 117 | metrics.descent = 2;
|
|---|
| 118 | metrics.leading = 3;
|
|---|
| 119 |
|
|---|
| 120 | rc = gfx_typeface_create(gc, &tface);
|
|---|
| 121 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
|---|
| 122 |
|
|---|
| 123 | gfx_font_props_init(&props);
|
|---|
| 124 | gfx_font_metrics_init(&metrics);
|
|---|
| 125 | rc = gfx_font_create(tface, &props, &metrics, &font);
|
|---|
| 126 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
|---|
| 127 |
|
|---|
| 128 | gfx_font_get_metrics(font, &gmetrics);
|
|---|
| 129 | PCUT_ASSERT_INT_EQUALS(metrics.ascent, gmetrics.ascent);
|
|---|
| 130 | PCUT_ASSERT_INT_EQUALS(metrics.descent, gmetrics.descent);
|
|---|
| 131 | PCUT_ASSERT_INT_EQUALS(metrics.leading, gmetrics.leading);
|
|---|
| 132 |
|
|---|
| 133 | gfx_font_close(font);
|
|---|
| 134 | gfx_typeface_destroy(tface);
|
|---|
| 135 | rc = gfx_context_delete(gc);
|
|---|
| 136 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
|---|
| 137 | }
|
|---|
| 138 |
|
|---|
| 139 | /** Test gfx_font_set_metrics() */
|
|---|
| 140 | PCUT_TEST(set_metrics)
|
|---|
| 141 | {
|
|---|
| 142 | gfx_font_props_t props;
|
|---|
| 143 | gfx_font_metrics_t metrics1;
|
|---|
| 144 | gfx_font_metrics_t metrics2;
|
|---|
| 145 | gfx_font_metrics_t gmetrics;
|
|---|
| 146 | gfx_typeface_t *tface;
|
|---|
| 147 | gfx_font_t *font;
|
|---|
| 148 | gfx_context_t *gc;
|
|---|
| 149 | test_gc_t tgc;
|
|---|
| 150 | errno_t rc;
|
|---|
| 151 |
|
|---|
| 152 | rc = gfx_context_new(&test_ops, (void *)&tgc, &gc);
|
|---|
| 153 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
|---|
| 154 |
|
|---|
| 155 | rc = gfx_typeface_create(gc, &tface);
|
|---|
| 156 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
|---|
| 157 |
|
|---|
| 158 | gfx_font_props_init(&props);
|
|---|
| 159 |
|
|---|
| 160 | gfx_font_metrics_init(&metrics1);
|
|---|
| 161 | metrics1.ascent = 1;
|
|---|
| 162 | metrics1.descent = 2;
|
|---|
| 163 | metrics1.leading = 3;
|
|---|
| 164 |
|
|---|
| 165 | rc = gfx_font_create(tface, &props, &metrics1, &font);
|
|---|
| 166 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
|---|
| 167 |
|
|---|
| 168 | gfx_font_metrics_init(&metrics2);
|
|---|
| 169 | metrics1.ascent = 4;
|
|---|
| 170 | metrics1.descent = 5;
|
|---|
| 171 | metrics1.leading = 6;
|
|---|
| 172 |
|
|---|
| 173 | rc = gfx_font_set_metrics(font, &metrics2);
|
|---|
| 174 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
|---|
| 175 |
|
|---|
| 176 | gfx_font_get_metrics(font, &gmetrics);
|
|---|
| 177 | PCUT_ASSERT_INT_EQUALS(metrics2.ascent, gmetrics.ascent);
|
|---|
| 178 | PCUT_ASSERT_INT_EQUALS(metrics2.descent, gmetrics.descent);
|
|---|
| 179 | PCUT_ASSERT_INT_EQUALS(metrics2.leading, gmetrics.leading);
|
|---|
| 180 |
|
|---|
| 181 | gfx_font_close(font);
|
|---|
| 182 | gfx_typeface_destroy(tface);
|
|---|
| 183 | rc = gfx_context_delete(gc);
|
|---|
| 184 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
|---|
| 185 | }
|
|---|
| 186 |
|
|---|
| 187 | /** Test gfx_font_first_glyph() */
|
|---|
| 188 | PCUT_TEST(first_glyph)
|
|---|
| 189 | {
|
|---|
| 190 | gfx_font_props_t props;
|
|---|
| 191 | gfx_font_metrics_t metrics;
|
|---|
| 192 | gfx_glyph_metrics_t gmetrics;
|
|---|
| 193 | gfx_typeface_t *tface;
|
|---|
| 194 | gfx_font_t *font;
|
|---|
| 195 | gfx_context_t *gc;
|
|---|
| 196 | gfx_glyph_t *glyph;
|
|---|
| 197 | gfx_glyph_t *gfirst;
|
|---|
| 198 | test_gc_t tgc;
|
|---|
| 199 | errno_t rc;
|
|---|
| 200 |
|
|---|
| 201 | rc = gfx_context_new(&test_ops, (void *)&tgc, &gc);
|
|---|
| 202 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
|---|
| 203 |
|
|---|
| 204 | gfx_font_metrics_init(&metrics);
|
|---|
| 205 |
|
|---|
| 206 | rc = gfx_typeface_create(gc, &tface);
|
|---|
| 207 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
|---|
| 208 |
|
|---|
| 209 | gfx_font_props_init(&props);
|
|---|
| 210 | gfx_font_metrics_init(&metrics);
|
|---|
| 211 | rc = gfx_font_create(tface, &props, &metrics, &font);
|
|---|
| 212 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
|---|
| 213 |
|
|---|
| 214 | /* Should get NULL since there is no glyph in the font */
|
|---|
| 215 | glyph = gfx_font_first_glyph(font);
|
|---|
| 216 | PCUT_ASSERT_NULL(glyph);
|
|---|
| 217 |
|
|---|
| 218 | /* Now add one */
|
|---|
| 219 | gfx_glyph_metrics_init(&gmetrics);
|
|---|
| 220 | rc = gfx_glyph_create(font, &gmetrics, &glyph);
|
|---|
| 221 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
|---|
| 222 |
|
|---|
| 223 | /* gfx_font_first_glyph() should return the same */
|
|---|
| 224 | gfirst = gfx_font_first_glyph(font);
|
|---|
| 225 | PCUT_ASSERT_EQUALS(glyph, gfirst);
|
|---|
| 226 |
|
|---|
| 227 | gfx_glyph_destroy(glyph);
|
|---|
| 228 | gfx_font_close(font);
|
|---|
| 229 | gfx_typeface_destroy(tface);
|
|---|
| 230 | rc = gfx_context_delete(gc);
|
|---|
| 231 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
|---|
| 232 | }
|
|---|
| 233 |
|
|---|
| 234 | /** Test gfx_font_next_glyph() */
|
|---|
| 235 | PCUT_TEST(next_glyph)
|
|---|
| 236 | {
|
|---|
| 237 | gfx_font_props_t props;
|
|---|
| 238 | gfx_font_metrics_t metrics;
|
|---|
| 239 | gfx_glyph_metrics_t gmetrics;
|
|---|
| 240 | gfx_typeface_t *tface;
|
|---|
| 241 | gfx_font_t *font;
|
|---|
| 242 | gfx_context_t *gc;
|
|---|
| 243 | gfx_glyph_t *glyph1;
|
|---|
| 244 | gfx_glyph_t *glyph2;
|
|---|
| 245 | gfx_glyph_t *gfirst;
|
|---|
| 246 | gfx_glyph_t *gsecond;
|
|---|
| 247 | test_gc_t tgc;
|
|---|
| 248 | errno_t rc;
|
|---|
| 249 |
|
|---|
| 250 | rc = gfx_context_new(&test_ops, (void *)&tgc, &gc);
|
|---|
| 251 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
|---|
| 252 |
|
|---|
| 253 | gfx_font_metrics_init(&metrics);
|
|---|
| 254 |
|
|---|
| 255 | rc = gfx_typeface_create(gc, &tface);
|
|---|
| 256 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
|---|
| 257 |
|
|---|
| 258 | gfx_font_props_init(&props);
|
|---|
| 259 | gfx_font_metrics_init(&metrics);
|
|---|
| 260 | rc = gfx_font_create(tface, &props, &metrics, &font);
|
|---|
| 261 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
|---|
| 262 |
|
|---|
| 263 | /* Add first glyph */
|
|---|
| 264 | gfx_glyph_metrics_init(&gmetrics);
|
|---|
| 265 | rc = gfx_glyph_create(font, &gmetrics, &glyph1);
|
|---|
| 266 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
|---|
| 267 |
|
|---|
| 268 | /* Add second glyph */
|
|---|
| 269 | gfx_glyph_metrics_init(&gmetrics);
|
|---|
| 270 | rc = gfx_glyph_create(font, &gmetrics, &glyph2);
|
|---|
| 271 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
|---|
| 272 |
|
|---|
| 273 | /* gfx_font_first_glyph() should return glyph1 */
|
|---|
| 274 | gfirst = gfx_font_first_glyph(font);
|
|---|
| 275 | PCUT_ASSERT_EQUALS(glyph1, gfirst);
|
|---|
| 276 |
|
|---|
| 277 | /* gfx_font_next_glyph() should return glyph2 */
|
|---|
| 278 | gsecond = gfx_font_next_glyph(gfirst);
|
|---|
| 279 | PCUT_ASSERT_EQUALS(glyph2, gsecond);
|
|---|
| 280 |
|
|---|
| 281 | gfx_glyph_destroy(glyph1);
|
|---|
| 282 | gfx_glyph_destroy(glyph2);
|
|---|
| 283 | gfx_font_close(font);
|
|---|
| 284 | gfx_typeface_destroy(tface);
|
|---|
| 285 | rc = gfx_context_delete(gc);
|
|---|
| 286 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
|---|
| 287 | }
|
|---|
| 288 |
|
|---|
| 289 | /** Test gfx_font_last_glyph() */
|
|---|
| 290 | PCUT_TEST(last_glyph)
|
|---|
| 291 | {
|
|---|
| 292 | gfx_font_props_t props;
|
|---|
| 293 | gfx_font_metrics_t metrics;
|
|---|
| 294 | gfx_glyph_metrics_t gmetrics;
|
|---|
| 295 | gfx_typeface_t *tface;
|
|---|
| 296 | gfx_font_t *font;
|
|---|
| 297 | gfx_context_t *gc;
|
|---|
| 298 | gfx_glyph_t *glyph;
|
|---|
| 299 | gfx_glyph_t *glast;
|
|---|
| 300 | test_gc_t tgc;
|
|---|
| 301 | errno_t rc;
|
|---|
| 302 |
|
|---|
| 303 | rc = gfx_context_new(&test_ops, (void *)&tgc, &gc);
|
|---|
| 304 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
|---|
| 305 |
|
|---|
| 306 | gfx_font_metrics_init(&metrics);
|
|---|
| 307 |
|
|---|
| 308 | rc = gfx_typeface_create(gc, &tface);
|
|---|
| 309 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
|---|
| 310 |
|
|---|
| 311 | gfx_font_props_init(&props);
|
|---|
| 312 | gfx_font_metrics_init(&metrics);
|
|---|
| 313 | rc = gfx_font_create(tface, &props, &metrics, &font);
|
|---|
| 314 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
|---|
| 315 |
|
|---|
| 316 | /* Should get NULL since there is no glyph in the font */
|
|---|
| 317 | glyph = gfx_font_last_glyph(font);
|
|---|
| 318 | PCUT_ASSERT_NULL(glyph);
|
|---|
| 319 |
|
|---|
| 320 | /* Now add one */
|
|---|
| 321 | gfx_glyph_metrics_init(&gmetrics);
|
|---|
| 322 | rc = gfx_glyph_create(font, &gmetrics, &glyph);
|
|---|
| 323 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
|---|
| 324 |
|
|---|
| 325 | /* gfx_font_last_glyph() should return the same */
|
|---|
| 326 | glast = gfx_font_last_glyph(font);
|
|---|
| 327 | PCUT_ASSERT_EQUALS(glyph, glast);
|
|---|
| 328 |
|
|---|
| 329 | gfx_glyph_destroy(glyph);
|
|---|
| 330 | gfx_font_close(font);
|
|---|
| 331 | gfx_typeface_destroy(tface);
|
|---|
| 332 | rc = gfx_context_delete(gc);
|
|---|
| 333 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
|---|
| 334 | }
|
|---|
| 335 |
|
|---|
| 336 | /** Test gfx_font_prev_glyph() */
|
|---|
| 337 | PCUT_TEST(prev_glyph)
|
|---|
| 338 | {
|
|---|
| 339 | gfx_font_props_t props;
|
|---|
| 340 | gfx_font_metrics_t metrics;
|
|---|
| 341 | gfx_glyph_metrics_t gmetrics;
|
|---|
| 342 | gfx_typeface_t *tface;
|
|---|
| 343 | gfx_font_t *font;
|
|---|
| 344 | gfx_context_t *gc;
|
|---|
| 345 | gfx_glyph_t *glyph1;
|
|---|
| 346 | gfx_glyph_t *glyph2;
|
|---|
| 347 | gfx_glyph_t *gfirst;
|
|---|
| 348 | gfx_glyph_t *gsecond;
|
|---|
| 349 | test_gc_t tgc;
|
|---|
| 350 | errno_t rc;
|
|---|
| 351 |
|
|---|
| 352 | rc = gfx_context_new(&test_ops, (void *)&tgc, &gc);
|
|---|
| 353 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
|---|
| 354 |
|
|---|
| 355 | gfx_font_metrics_init(&metrics);
|
|---|
| 356 |
|
|---|
| 357 | rc = gfx_typeface_create(gc, &tface);
|
|---|
| 358 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
|---|
| 359 |
|
|---|
| 360 | gfx_font_props_init(&props);
|
|---|
| 361 | gfx_font_metrics_init(&metrics);
|
|---|
| 362 | rc = gfx_font_create(tface, &props, &metrics, &font);
|
|---|
| 363 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
|---|
| 364 |
|
|---|
| 365 | /* Add first glyph */
|
|---|
| 366 | gfx_glyph_metrics_init(&gmetrics);
|
|---|
| 367 | rc = gfx_glyph_create(font, &gmetrics, &glyph1);
|
|---|
| 368 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
|---|
| 369 |
|
|---|
| 370 | /* Add second glyph */
|
|---|
| 371 | gfx_glyph_metrics_init(&gmetrics);
|
|---|
| 372 | rc = gfx_glyph_create(font, &gmetrics, &glyph2);
|
|---|
| 373 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
|---|
| 374 |
|
|---|
| 375 | /* gfx_font_last_glyph() should return glyph2 */
|
|---|
| 376 | gsecond = gfx_font_last_glyph(font);
|
|---|
| 377 | PCUT_ASSERT_EQUALS(glyph2, gsecond);
|
|---|
| 378 |
|
|---|
| 379 | /* gfx_font_prev_glyph() should return glyph1 */
|
|---|
| 380 | gfirst = gfx_font_prev_glyph(gsecond);
|
|---|
| 381 | PCUT_ASSERT_EQUALS(glyph1, gfirst);
|
|---|
| 382 |
|
|---|
| 383 | gfx_glyph_destroy(glyph1);
|
|---|
| 384 | gfx_glyph_destroy(glyph2);
|
|---|
| 385 | gfx_font_close(font);
|
|---|
| 386 | gfx_typeface_destroy(tface);
|
|---|
| 387 | rc = gfx_context_delete(gc);
|
|---|
| 388 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
|---|
| 389 | }
|
|---|
| 390 |
|
|---|
| 391 | /** Test gfx_font_search_glyph() */
|
|---|
| 392 | PCUT_TEST(search_glyph)
|
|---|
| 393 | {
|
|---|
| 394 | gfx_font_props_t props;
|
|---|
| 395 | gfx_font_metrics_t metrics;
|
|---|
| 396 | gfx_typeface_t *tface;
|
|---|
| 397 | gfx_font_t *font;
|
|---|
| 398 | gfx_context_t *gc;
|
|---|
| 399 | gfx_glyph_t *glyph;
|
|---|
| 400 | size_t bytes;
|
|---|
| 401 | test_gc_t tgc;
|
|---|
| 402 | errno_t rc;
|
|---|
| 403 |
|
|---|
| 404 | rc = gfx_context_new(&test_ops, (void *)&tgc, &gc);
|
|---|
| 405 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
|---|
| 406 |
|
|---|
| 407 | gfx_font_metrics_init(&metrics);
|
|---|
| 408 |
|
|---|
| 409 | rc = gfx_typeface_create(gc, &tface);
|
|---|
| 410 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
|---|
| 411 |
|
|---|
| 412 | gfx_font_props_init(&props);
|
|---|
| 413 | gfx_font_metrics_init(&metrics);
|
|---|
| 414 | rc = gfx_font_create(tface, &props, &metrics, &font);
|
|---|
| 415 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
|---|
| 416 |
|
|---|
| 417 | rc = gfx_font_search_glyph(font, "Hello", &glyph, &bytes);
|
|---|
| 418 | PCUT_ASSERT_ERRNO_VAL(ENOENT, rc);
|
|---|
| 419 |
|
|---|
| 420 | gfx_font_close(font);
|
|---|
| 421 | gfx_typeface_destroy(tface);
|
|---|
| 422 | rc = gfx_context_delete(gc);
|
|---|
| 423 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
|---|
| 424 | }
|
|---|
| 425 |
|
|---|
| 426 | /** Test gfx_font_splice_at_glyph() */
|
|---|
| 427 | PCUT_TEST(splice_at_glyph)
|
|---|
| 428 | {
|
|---|
| 429 | gfx_font_props_t props;
|
|---|
| 430 | gfx_font_metrics_t fmetrics;
|
|---|
| 431 | gfx_typeface_t *tface;
|
|---|
| 432 | gfx_font_t *font;
|
|---|
| 433 | gfx_glyph_metrics_t gmetrics;
|
|---|
| 434 | gfx_glyph_t *glyph;
|
|---|
| 435 | gfx_context_t *gc;
|
|---|
| 436 | gfx_rect_t nrect;
|
|---|
| 437 | test_gc_t tgc;
|
|---|
| 438 | errno_t rc;
|
|---|
| 439 |
|
|---|
| 440 | rc = gfx_context_new(&test_ops, (void *)&tgc, &gc);
|
|---|
| 441 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
|---|
| 442 |
|
|---|
| 443 | rc = gfx_typeface_create(gc, &tface);
|
|---|
| 444 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
|---|
| 445 |
|
|---|
| 446 | gfx_font_props_init(&props);
|
|---|
| 447 | gfx_font_metrics_init(&fmetrics);
|
|---|
| 448 | rc = gfx_font_create(tface, &props, &fmetrics, &font);
|
|---|
| 449 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
|---|
| 450 |
|
|---|
| 451 | gfx_glyph_metrics_init(&gmetrics);
|
|---|
| 452 | rc = gfx_glyph_create(font, &gmetrics, &glyph);
|
|---|
| 453 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
|---|
| 454 |
|
|---|
| 455 | nrect.p0.x = -5;
|
|---|
| 456 | nrect.p0.y = -5;
|
|---|
| 457 | nrect.p1.x = 5;
|
|---|
| 458 | nrect.p1.y = 5;
|
|---|
| 459 | rc = gfx_font_splice_at_glyph(font, glyph, &nrect);
|
|---|
| 460 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
|---|
| 461 |
|
|---|
| 462 | gfx_glyph_destroy(glyph);
|
|---|
| 463 |
|
|---|
| 464 | gfx_font_close(font);
|
|---|
| 465 | gfx_typeface_destroy(tface);
|
|---|
| 466 | rc = gfx_context_delete(gc);
|
|---|
| 467 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
|---|
| 468 | }
|
|---|
| 469 |
|
|---|
| 470 | /** Test gfx_font_bitmap_pack() properly packs bitmap */
|
|---|
| 471 | PCUT_TEST(bitmap_pack)
|
|---|
| 472 | {
|
|---|
| 473 | errno_t rc;
|
|---|
| 474 | gfx_coord_t width, height;
|
|---|
| 475 | gfx_coord_t i;
|
|---|
| 476 | uint32_t *pixels;
|
|---|
| 477 | void *data;
|
|---|
| 478 | size_t size;
|
|---|
| 479 | uint8_t *dp;
|
|---|
| 480 |
|
|---|
| 481 | width = 10;
|
|---|
| 482 | height = 10;
|
|---|
| 483 |
|
|---|
| 484 | pixels = calloc(sizeof(uint32_t), width * height);
|
|---|
| 485 | PCUT_ASSERT_NOT_NULL(pixels);
|
|---|
| 486 |
|
|---|
| 487 | for (i = 0; i < 10; i++)
|
|---|
| 488 | pixels[i * width + i] = PIXEL(255, 255, 255, 255);
|
|---|
| 489 |
|
|---|
| 490 | rc = gfx_font_bitmap_pack(width, height, pixels, &data, &size);
|
|---|
| 491 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
|---|
| 492 | PCUT_ASSERT_NOT_NULL(data);
|
|---|
| 493 | PCUT_ASSERT_INT_EQUALS(20, size);
|
|---|
| 494 |
|
|---|
| 495 | dp = data;
|
|---|
| 496 |
|
|---|
| 497 | for (i = 0; i < 8; i++) {
|
|---|
| 498 | PCUT_ASSERT_INT_EQUALS(0x80 >> i, dp[2 * i]);
|
|---|
| 499 | PCUT_ASSERT_INT_EQUALS(0, dp[2 * i + 1]);
|
|---|
| 500 | }
|
|---|
| 501 |
|
|---|
| 502 | for (i = 8; i < 10; i++) {
|
|---|
| 503 | PCUT_ASSERT_INT_EQUALS(0, dp[2 * i]);
|
|---|
| 504 | PCUT_ASSERT_INT_EQUALS(0x80 >> (i - 8), dp[2 * i + 1]);
|
|---|
| 505 | }
|
|---|
| 506 | }
|
|---|
| 507 |
|
|---|
| 508 | /** Test gfx_font_bitmap_unpack() properly unpacks bitmap */
|
|---|
| 509 | PCUT_TEST(bitmap_unpack)
|
|---|
| 510 | {
|
|---|
| 511 | errno_t rc;
|
|---|
| 512 | gfx_coord_t width, height;
|
|---|
| 513 | gfx_coord_t i, j;
|
|---|
| 514 | uint32_t epix;
|
|---|
| 515 | uint32_t *pixels;
|
|---|
| 516 | uint8_t data[20];
|
|---|
| 517 |
|
|---|
| 518 | width = 10;
|
|---|
| 519 | height = 10;
|
|---|
| 520 |
|
|---|
| 521 | for (i = 0; i < 8; i++) {
|
|---|
| 522 | data[2 * i] = 0x80 >> i;
|
|---|
| 523 | data[2 * i + 1] = 0;
|
|---|
| 524 | }
|
|---|
| 525 |
|
|---|
| 526 | for (i = 8; i < 10; i++) {
|
|---|
| 527 | data[2 * i] = 0;
|
|---|
| 528 | data[2 * i + 1] = 0x80 >> (i - 8);
|
|---|
| 529 | }
|
|---|
| 530 |
|
|---|
| 531 | pixels = calloc(sizeof(uint32_t), width * height);
|
|---|
| 532 | PCUT_ASSERT_NOT_NULL(pixels);
|
|---|
| 533 |
|
|---|
| 534 | rc = gfx_font_bitmap_unpack(width, height, data, sizeof(data),
|
|---|
| 535 | pixels);
|
|---|
| 536 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
|---|
| 537 |
|
|---|
| 538 | for (j = 0; j < 10; j++) {
|
|---|
| 539 | for (i = 0; i < 10; i++) {
|
|---|
| 540 | epix = (i == j) ? PIXEL(255, 255, 255, 255) :
|
|---|
| 541 | PIXEL(0, 0, 0, 0);
|
|---|
| 542 | PCUT_ASSERT_INT_EQUALS(epix, pixels[j * width + i]);
|
|---|
| 543 | }
|
|---|
| 544 | }
|
|---|
| 545 | }
|
|---|
| 546 |
|
|---|
| 547 | static errno_t testgc_set_color(void *arg, gfx_color_t *color)
|
|---|
| 548 | {
|
|---|
| 549 | return EOK;
|
|---|
| 550 | }
|
|---|
| 551 |
|
|---|
| 552 | static errno_t testgc_fill_rect(void *arg, gfx_rect_t *rect)
|
|---|
| 553 | {
|
|---|
| 554 | return EOK;
|
|---|
| 555 | }
|
|---|
| 556 |
|
|---|
| 557 | static errno_t testgc_bitmap_create(void *arg, gfx_bitmap_params_t *params,
|
|---|
| 558 | gfx_bitmap_alloc_t *alloc, void **rbm)
|
|---|
| 559 | {
|
|---|
| 560 | test_gc_t *tgc = (test_gc_t *) arg;
|
|---|
| 561 | testgc_bitmap_t *tbm;
|
|---|
| 562 |
|
|---|
| 563 | tbm = calloc(1, sizeof(testgc_bitmap_t));
|
|---|
| 564 | if (tbm == NULL)
|
|---|
| 565 | return ENOMEM;
|
|---|
| 566 |
|
|---|
| 567 | if (alloc == NULL) {
|
|---|
| 568 | tbm->alloc.pitch = (params->rect.p1.x - params->rect.p0.x) *
|
|---|
| 569 | sizeof(uint32_t);
|
|---|
| 570 | tbm->alloc.off0 = 0;
|
|---|
| 571 | tbm->alloc.pixels = calloc(sizeof(uint32_t),
|
|---|
| 572 | tbm->alloc.pitch * (params->rect.p1.y - params->rect.p0.y));
|
|---|
| 573 | tbm->myalloc = true;
|
|---|
| 574 | if (tbm->alloc.pixels == NULL) {
|
|---|
| 575 | free(tbm);
|
|---|
| 576 | return ENOMEM;
|
|---|
| 577 | }
|
|---|
| 578 | } else {
|
|---|
| 579 | tbm->alloc = *alloc;
|
|---|
| 580 | }
|
|---|
| 581 |
|
|---|
| 582 | tbm->tgc = tgc;
|
|---|
| 583 | tgc->bm_params = *params;
|
|---|
| 584 | tgc->bm_pixels = tbm->alloc.pixels;
|
|---|
| 585 | *rbm = (void *)tbm;
|
|---|
| 586 | return EOK;
|
|---|
| 587 | }
|
|---|
| 588 |
|
|---|
| 589 | static errno_t testgc_bitmap_destroy(void *bm)
|
|---|
| 590 | {
|
|---|
| 591 | testgc_bitmap_t *tbm = (testgc_bitmap_t *)bm;
|
|---|
| 592 | if (tbm->myalloc)
|
|---|
| 593 | free(tbm->alloc.pixels);
|
|---|
| 594 | free(tbm);
|
|---|
| 595 | return EOK;
|
|---|
| 596 | }
|
|---|
| 597 |
|
|---|
| 598 | static errno_t testgc_bitmap_render(void *bm, gfx_rect_t *srect,
|
|---|
| 599 | gfx_coord2_t *offs)
|
|---|
| 600 | {
|
|---|
| 601 | testgc_bitmap_t *tbm = (testgc_bitmap_t *)bm;
|
|---|
| 602 | tbm->tgc->bm_srect = *srect;
|
|---|
| 603 | tbm->tgc->bm_offs = *offs;
|
|---|
| 604 | return EOK;
|
|---|
| 605 | }
|
|---|
| 606 |
|
|---|
| 607 | static errno_t testgc_bitmap_get_alloc(void *bm, gfx_bitmap_alloc_t *alloc)
|
|---|
| 608 | {
|
|---|
| 609 | testgc_bitmap_t *tbm = (testgc_bitmap_t *)bm;
|
|---|
| 610 | *alloc = tbm->alloc;
|
|---|
| 611 | return EOK;
|
|---|
| 612 | }
|
|---|
| 613 |
|
|---|
| 614 | PCUT_EXPORT(font);
|
|---|