[ee2f0beb] | 1 | /*
|
---|
[7470d97] | 2 | * Copyright (c) 2021 Jiri Svoboda
|
---|
[ee2f0beb] | 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 |
|
---|
[c78a03d] | 29 | #include <gfx/context.h>
|
---|
[ee2f0beb] | 30 | #include <gfx/font.h>
|
---|
[d2100e2] | 31 | #include <gfx/glyph.h>
|
---|
[06b8383] | 32 | #include <gfx/typeface.h>
|
---|
[ee2f0beb] | 33 | #include <pcut/pcut.h>
|
---|
[d2100e2] | 34 | #include "../private/font.h"
|
---|
[06b8383] | 35 | #include "../private/typeface.h"
|
---|
[ee2f0beb] | 36 |
|
---|
| 37 | PCUT_INIT;
|
---|
| 38 |
|
---|
| 39 | PCUT_TEST_SUITE(font);
|
---|
| 40 |
|
---|
[7470d97] | 41 | static errno_t testgc_set_clip_rect(void *, gfx_rect_t *);
|
---|
[c78a03d] | 42 | static errno_t testgc_set_color(void *, gfx_color_t *);
|
---|
| 43 | static errno_t testgc_fill_rect(void *, gfx_rect_t *);
|
---|
[d2100e2] | 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 *);
|
---|
[c78a03d] | 49 |
|
---|
| 50 | static gfx_context_ops_t test_ops = {
|
---|
[7470d97] | 51 | .set_clip_rect = testgc_set_clip_rect,
|
---|
[c78a03d] | 52 | .set_color = testgc_set_color,
|
---|
[d2100e2] | 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
|
---|
[c78a03d] | 58 | };
|
---|
| 59 |
|
---|
[d2100e2] | 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 |
|
---|
[c78a03d] | 73 | /** Test creating and destroying font */
|
---|
| 74 | PCUT_TEST(create_destroy)
|
---|
| 75 | {
|
---|
[06b8383] | 76 | gfx_font_props_t props;
|
---|
[c78a03d] | 77 | gfx_font_metrics_t metrics;
|
---|
[06b8383] | 78 | gfx_typeface_t *tface;
|
---|
[c78a03d] | 79 | gfx_font_t *font;
|
---|
| 80 | gfx_context_t *gc;
|
---|
[d2100e2] | 81 | test_gc_t tgc;
|
---|
[c78a03d] | 82 | errno_t rc;
|
---|
| 83 |
|
---|
[d2100e2] | 84 | rc = gfx_context_new(&test_ops, (void *)&tgc, &gc);
|
---|
[c78a03d] | 85 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
| 86 |
|
---|
[06b8383] | 87 | rc = gfx_typeface_create(gc, &tface);
|
---|
| 88 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
| 89 |
|
---|
| 90 | gfx_font_props_init(&props);
|
---|
[c78a03d] | 91 | gfx_font_metrics_init(&metrics);
|
---|
[06b8383] | 92 | rc = gfx_font_create(tface, &props, &metrics, &font);
|
---|
[c78a03d] | 93 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
| 94 |
|
---|
[06b8383] | 95 | gfx_font_close(font);
|
---|
| 96 | gfx_typeface_destroy(tface);
|
---|
[6a87f28] | 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);
|
---|
[06b8383] | 122 |
|
---|
[c78a03d] | 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 | {
|
---|
[06b8383] | 130 | gfx_font_props_t props;
|
---|
[c78a03d] | 131 | gfx_font_metrics_t metrics;
|
---|
| 132 | gfx_font_metrics_t gmetrics;
|
---|
[06b8383] | 133 | gfx_typeface_t *tface;
|
---|
[c78a03d] | 134 | gfx_font_t *font;
|
---|
| 135 | gfx_context_t *gc;
|
---|
[d2100e2] | 136 | test_gc_t tgc;
|
---|
[c78a03d] | 137 | errno_t rc;
|
---|
| 138 |
|
---|
[d2100e2] | 139 | rc = gfx_context_new(&test_ops, (void *)&tgc, &gc);
|
---|
[c78a03d] | 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 |
|
---|
[06b8383] | 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);
|
---|
[c78a03d] | 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 |
|
---|
[06b8383] | 160 | gfx_font_close(font);
|
---|
| 161 | gfx_typeface_destroy(tface);
|
---|
[c78a03d] | 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 | {
|
---|
[06b8383] | 169 | gfx_font_props_t props;
|
---|
[c78a03d] | 170 | gfx_font_metrics_t metrics1;
|
---|
| 171 | gfx_font_metrics_t metrics2;
|
---|
| 172 | gfx_font_metrics_t gmetrics;
|
---|
[06b8383] | 173 | gfx_typeface_t *tface;
|
---|
[c78a03d] | 174 | gfx_font_t *font;
|
---|
| 175 | gfx_context_t *gc;
|
---|
[d2100e2] | 176 | test_gc_t tgc;
|
---|
[c78a03d] | 177 | errno_t rc;
|
---|
| 178 |
|
---|
[d2100e2] | 179 | rc = gfx_context_new(&test_ops, (void *)&tgc, &gc);
|
---|
[c78a03d] | 180 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
| 181 |
|
---|
[06b8383] | 182 | rc = gfx_typeface_create(gc, &tface);
|
---|
| 183 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
| 184 |
|
---|
| 185 | gfx_font_props_init(&props);
|
---|
| 186 |
|
---|
[c78a03d] | 187 | gfx_font_metrics_init(&metrics1);
|
---|
| 188 | metrics1.ascent = 1;
|
---|
| 189 | metrics1.descent = 2;
|
---|
| 190 | metrics1.leading = 3;
|
---|
| 191 |
|
---|
[06b8383] | 192 | rc = gfx_font_create(tface, &props, &metrics1, &font);
|
---|
[c78a03d] | 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 |
|
---|
[06b8383] | 208 | gfx_font_close(font);
|
---|
| 209 | gfx_typeface_destroy(tface);
|
---|
[c78a03d] | 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 | {
|
---|
[06b8383] | 217 | gfx_font_props_t props;
|
---|
[c78a03d] | 218 | gfx_font_metrics_t metrics;
|
---|
[d2100e2] | 219 | gfx_glyph_metrics_t gmetrics;
|
---|
[06b8383] | 220 | gfx_typeface_t *tface;
|
---|
[c78a03d] | 221 | gfx_font_t *font;
|
---|
| 222 | gfx_context_t *gc;
|
---|
| 223 | gfx_glyph_t *glyph;
|
---|
[d2100e2] | 224 | gfx_glyph_t *gfirst;
|
---|
| 225 | test_gc_t tgc;
|
---|
[c78a03d] | 226 | errno_t rc;
|
---|
| 227 |
|
---|
[d2100e2] | 228 | rc = gfx_context_new(&test_ops, (void *)&tgc, &gc);
|
---|
[c78a03d] | 229 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
| 230 |
|
---|
| 231 | gfx_font_metrics_init(&metrics);
|
---|
| 232 |
|
---|
[06b8383] | 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);
|
---|
[c78a03d] | 239 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
| 240 |
|
---|
[d2100e2] | 241 | /* Should get NULL since there is no glyph in the font */
|
---|
[c78a03d] | 242 | glyph = gfx_font_first_glyph(font);
|
---|
| 243 | PCUT_ASSERT_NULL(glyph);
|
---|
| 244 |
|
---|
[d2100e2] | 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);
|
---|
[06b8383] | 255 | gfx_font_close(font);
|
---|
| 256 | gfx_typeface_destroy(tface);
|
---|
[c78a03d] | 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 | {
|
---|
[06b8383] | 264 | gfx_font_props_t props;
|
---|
[d2100e2] | 265 | gfx_font_metrics_t metrics;
|
---|
| 266 | gfx_glyph_metrics_t gmetrics;
|
---|
[06b8383] | 267 | gfx_typeface_t *tface;
|
---|
[d2100e2] | 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 |
|
---|
[06b8383] | 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);
|
---|
[d2100e2] | 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);
|
---|
[06b8383] | 310 | gfx_font_close(font);
|
---|
| 311 | gfx_typeface_destroy(tface);
|
---|
[d2100e2] | 312 | rc = gfx_context_delete(gc);
|
---|
| 313 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
[c78a03d] | 314 | }
|
---|
| 315 |
|
---|
[d884672] | 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 |
|
---|
[c78a03d] | 418 | /** Test gfx_font_search_glyph() */
|
---|
| 419 | PCUT_TEST(search_glyph)
|
---|
| 420 | {
|
---|
[06b8383] | 421 | gfx_font_props_t props;
|
---|
[c78a03d] | 422 | gfx_font_metrics_t metrics;
|
---|
[06b8383] | 423 | gfx_typeface_t *tface;
|
---|
[c78a03d] | 424 | gfx_font_t *font;
|
---|
| 425 | gfx_context_t *gc;
|
---|
| 426 | gfx_glyph_t *glyph;
|
---|
| 427 | size_t bytes;
|
---|
[d2100e2] | 428 | test_gc_t tgc;
|
---|
[c78a03d] | 429 | errno_t rc;
|
---|
| 430 |
|
---|
[d2100e2] | 431 | rc = gfx_context_new(&test_ops, (void *)&tgc, &gc);
|
---|
[c78a03d] | 432 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
| 433 |
|
---|
| 434 | gfx_font_metrics_init(&metrics);
|
---|
| 435 |
|
---|
[06b8383] | 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);
|
---|
[c78a03d] | 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 |
|
---|
[06b8383] | 447 | gfx_font_close(font);
|
---|
| 448 | gfx_typeface_destroy(tface);
|
---|
[c78a03d] | 449 | rc = gfx_context_delete(gc);
|
---|
| 450 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
| 451 | }
|
---|
| 452 |
|
---|
[d2100e2] | 453 | /** Test gfx_font_splice_at_glyph() */
|
---|
| 454 | PCUT_TEST(splice_at_glyph)
|
---|
| 455 | {
|
---|
[06b8383] | 456 | gfx_font_props_t props;
|
---|
[d2100e2] | 457 | gfx_font_metrics_t fmetrics;
|
---|
[06b8383] | 458 | gfx_typeface_t *tface;
|
---|
[d2100e2] | 459 | gfx_font_t *font;
|
---|
| 460 | gfx_glyph_metrics_t gmetrics;
|
---|
| 461 | gfx_glyph_t *glyph;
|
---|
| 462 | gfx_context_t *gc;
|
---|
[313ac8e] | 463 | gfx_rect_t nrect;
|
---|
[d2100e2] | 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 |
|
---|
[06b8383] | 470 | rc = gfx_typeface_create(gc, &tface);
|
---|
| 471 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
| 472 |
|
---|
| 473 | gfx_font_props_init(&props);
|
---|
[d2100e2] | 474 | gfx_font_metrics_init(&fmetrics);
|
---|
[06b8383] | 475 | rc = gfx_font_create(tface, &props, &fmetrics, &font);
|
---|
[d2100e2] | 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 |
|
---|
[313ac8e] | 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);
|
---|
[d2100e2] | 487 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
| 488 |
|
---|
| 489 | gfx_glyph_destroy(glyph);
|
---|
| 490 |
|
---|
[06b8383] | 491 | gfx_font_close(font);
|
---|
| 492 | gfx_typeface_destroy(tface);
|
---|
[d2100e2] | 493 | rc = gfx_context_delete(gc);
|
---|
| 494 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
| 495 | }
|
---|
| 496 |
|
---|
[f13f1495] | 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 |
|
---|
[7470d97] | 574 | static errno_t testgc_set_clip_rect(void *arg, gfx_rect_t *rect)
|
---|
| 575 | {
|
---|
| 576 | return EOK;
|
---|
| 577 | }
|
---|
| 578 |
|
---|
[c78a03d] | 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)
|
---|
[ee2f0beb] | 585 | {
|
---|
[c78a03d] | 586 | return EOK;
|
---|
[ee2f0beb] | 587 | }
|
---|
| 588 |
|
---|
[d2100e2] | 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 |
|
---|
[ee2f0beb] | 646 | PCUT_EXPORT(font);
|
---|