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