Changes in uspace/lib/gfxfont/test/text.c [1fa6292:8fa65af0] in mainline
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
uspace/lib/gfxfont/test/text.c
r1fa6292 r8fa65af0 1 1 /* 2 * Copyright (c) 202 1Jiri Svoboda2 * Copyright (c) 2020 Jiri Svoboda 3 3 * All rights reserved. 4 4 * … … 27 27 */ 28 28 29 #include <gfx/color.h>30 29 #include <gfx/context.h> 31 30 #include <gfx/font.h> 32 #include <gfx/glyph.h>33 31 #include <gfx/text.h> 34 32 #include <gfx/typeface.h> … … 36 34 #include "../private/font.h" 37 35 #include "../private/typeface.h" 38 #include "../private/testgc.h"39 36 40 37 PCUT_INIT; 41 38 42 39 PCUT_TEST_SUITE(text); 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; 43 70 44 71 /** Test text width computation with a dummy font */ … … 83 110 gfx_font_t *font; 84 111 gfx_context_t *gc; 85 gfx_color_t *color;86 112 gfx_text_fmt_t fmt; 87 113 gfx_coord2_t pos; … … 92 118 PCUT_ASSERT_ERRNO_VAL(EOK, rc); 93 119 94 rc = gfx_color_new_rgb_i16(0, 0, 0, &color);95 PCUT_ASSERT_ERRNO_VAL(EOK, rc);96 97 120 rc = gfx_typeface_create(gc, &tface); 98 121 PCUT_ASSERT_ERRNO_VAL(EOK, rc); … … 104 127 105 128 gfx_text_fmt_init(&fmt); 106 fmt.font = font;107 fmt.color = color;108 129 pos.x = 0; 109 130 pos.y = 0; 110 131 111 rc = gfx_puttext( &pos, &fmt, "Hello world!");132 rc = gfx_puttext(font, &pos, &fmt, "Hello world!"); 112 133 PCUT_ASSERT_ERRNO_VAL(EOK, rc); 113 134 114 135 gfx_font_close(font); 115 136 gfx_typeface_destroy(tface); 116 gfx_color_delete(color);117 137 118 138 rc = gfx_context_delete(gc); … … 120 140 } 121 141 122 /** gfx_text_start_pos() correctly computes text start position */ 123 PCUT_TEST(text_start_pos) 124 { 125 gfx_font_props_t props; 126 gfx_font_metrics_t metrics; 127 gfx_typeface_t *tface; 128 gfx_font_t *font; 129 gfx_context_t *gc; 130 gfx_color_t *color; 131 gfx_text_fmt_t fmt; 132 gfx_coord2_t pos; 133 test_gc_t tgc; 134 errno_t rc; 135 136 rc = gfx_context_new(&test_ops, (void *)&tgc, &gc); 137 PCUT_ASSERT_ERRNO_VAL(EOK, rc); 138 139 rc = gfx_color_new_rgb_i16(0, 0, 0, &color); 140 PCUT_ASSERT_ERRNO_VAL(EOK, rc); 141 142 rc = gfx_typeface_create(gc, &tface); 143 PCUT_ASSERT_ERRNO_VAL(EOK, rc); 144 145 gfx_font_props_init(&props); 146 gfx_font_metrics_init(&metrics); 147 metrics.ascent = 10; // XXX 148 metrics.descent = 10; // XXX 149 rc = gfx_font_create(tface, &props, &metrics, &font); 150 PCUT_ASSERT_ERRNO_VAL(EOK, rc); 151 152 gfx_text_fmt_init(&fmt); 153 fmt.font = font; 154 fmt.color = color; 155 pos.x = 0; 156 pos.y = 0; 157 158 rc = gfx_puttext(&pos, &fmt, "Hello world!"); 159 PCUT_ASSERT_ERRNO_VAL(EOK, rc); 160 161 gfx_font_close(font); 162 gfx_typeface_destroy(tface); 163 gfx_color_delete(color); 164 165 rc = gfx_context_delete(gc); 166 PCUT_ASSERT_ERRNO_VAL(EOK, rc); 167 } 168 169 /** gfx_text_find_pos() finds position in text */ 170 PCUT_TEST(text_find_pos) 171 { 172 gfx_font_props_t props; 173 gfx_font_metrics_t metrics; 174 gfx_typeface_t *tface; 175 gfx_font_t *font; 176 gfx_glyph_metrics_t gmetrics; 177 gfx_glyph_t *glyph1; 178 gfx_glyph_t *glyph2; 179 gfx_context_t *gc; 180 gfx_text_fmt_t fmt; 181 gfx_coord2_t anchor; 182 gfx_coord2_t fpos; 183 size_t off; 184 test_gc_t tgc; 185 errno_t rc; 186 187 rc = gfx_context_new(&test_ops, (void *)&tgc, &gc); 188 PCUT_ASSERT_ERRNO_VAL(EOK, rc); 189 190 rc = gfx_typeface_create(gc, &tface); 191 PCUT_ASSERT_ERRNO_VAL(EOK, rc); 192 193 gfx_font_props_init(&props); 194 gfx_font_metrics_init(&metrics); 195 rc = gfx_font_create(tface, &props, &metrics, &font); 196 PCUT_ASSERT_ERRNO_VAL(EOK, rc); 197 198 /* Need to create some glyphs with metrics */ 199 gfx_glyph_metrics_init(&gmetrics); 200 gmetrics.advance = 10; 201 202 rc = gfx_glyph_create(font, &gmetrics, &glyph1); 203 PCUT_ASSERT_ERRNO_VAL(EOK, rc); 204 205 rc = gfx_glyph_set_pattern(glyph1, "A"); 206 PCUT_ASSERT_ERRNO_VAL(EOK, rc); 207 208 gfx_glyph_metrics_init(&gmetrics); 209 gmetrics.advance = 1; 210 211 rc = gfx_glyph_create(font, &gmetrics, &glyph2); 212 PCUT_ASSERT_ERRNO_VAL(EOK, rc); 213 214 rc = gfx_glyph_set_pattern(glyph2, "i"); 215 PCUT_ASSERT_ERRNO_VAL(EOK, rc); 216 217 gfx_text_fmt_init(&fmt); 218 fmt.font = font; 219 anchor.x = 10; 220 anchor.y = 0; 221 222 fpos.x = 9; 223 fpos.y = 0; 224 off = gfx_text_find_pos(&anchor, &fmt, "Aii", &fpos); 225 PCUT_ASSERT_INT_EQUALS(0, off); 226 227 fpos.x = 10; 228 fpos.y = 0; 229 off = gfx_text_find_pos(&anchor, &fmt, "Aii", &fpos); 230 PCUT_ASSERT_INT_EQUALS(0, off); 231 232 fpos.x = 11; 233 fpos.y = 0; 234 off = gfx_text_find_pos(&anchor, &fmt, "Aii", &fpos); 235 PCUT_ASSERT_INT_EQUALS(0, off); 236 237 fpos.x = 19; 238 fpos.y = 0; 239 off = gfx_text_find_pos(&anchor, &fmt, "Aii", &fpos); 240 PCUT_ASSERT_INT_EQUALS(1, off); 241 242 fpos.x = 20; 243 fpos.y = 0; 244 off = gfx_text_find_pos(&anchor, &fmt, "Aii", &fpos); 245 PCUT_ASSERT_INT_EQUALS(2, off); 246 247 fpos.x = 21; 248 fpos.y = 0; 249 off = gfx_text_find_pos(&anchor, &fmt, "Aii", &fpos); 250 PCUT_ASSERT_INT_EQUALS(3, off); 251 252 fpos.x = 22; 253 fpos.y = 0; 254 off = gfx_text_find_pos(&anchor, &fmt, "Aii", &fpos); 255 PCUT_ASSERT_INT_EQUALS(3, off); 256 257 gfx_glyph_destroy(glyph1); 258 gfx_glyph_destroy(glyph2); 259 260 gfx_font_close(font); 261 gfx_typeface_destroy(tface); 262 263 rc = gfx_context_delete(gc); 264 PCUT_ASSERT_ERRNO_VAL(EOK, rc); 265 } 266 267 /** gfx_text_find_pos() finds position in text in text mode */ 268 PCUT_TEST(text_find_pos_text) 269 { 270 gfx_typeface_t *tface; 271 gfx_font_t *font; 272 gfx_context_t *gc; 273 test_gc_t tgc; 274 size_t off; 275 gfx_text_fmt_t fmt; 276 gfx_coord2_t anchor; 277 gfx_coord2_t fpos; 278 errno_t rc; 279 280 rc = gfx_context_new(&test_ops, (void *)&tgc, &gc); 281 PCUT_ASSERT_ERRNO_VAL(EOK, rc); 282 283 rc = gfx_typeface_create(gc, &tface); 284 PCUT_ASSERT_ERRNO_VAL(EOK, rc); 285 286 rc = gfx_font_create_textmode(tface, &font); 287 PCUT_ASSERT_ERRNO_VAL(EOK, rc); 288 289 anchor.x = 10; 290 anchor.y = 0; 291 gfx_text_fmt_init(&fmt); 292 fmt.font = font; 293 294 fpos.x = 9; 295 fpos.y = 0; 296 off = gfx_text_find_pos(&anchor, &fmt, "Abc", &fpos); 297 PCUT_ASSERT_INT_EQUALS(0, off); 298 299 fpos.x = 10; 300 fpos.y = 0; 301 off = gfx_text_find_pos(&anchor, &fmt, "Abc", &fpos); 302 PCUT_ASSERT_INT_EQUALS(0, off); 303 304 fpos.x = 11; 305 fpos.y = 0; 306 off = gfx_text_find_pos(&anchor, &fmt, "Abc", &fpos); 307 PCUT_ASSERT_INT_EQUALS(1, off); 308 309 fpos.x = 12; 310 fpos.y = 0; 311 off = gfx_text_find_pos(&anchor, &fmt, "Abc", &fpos); 312 PCUT_ASSERT_INT_EQUALS(2, off); 313 314 fpos.x = 13; 315 fpos.y = 0; 316 off = gfx_text_find_pos(&anchor, &fmt, "Abc", &fpos); 317 PCUT_ASSERT_INT_EQUALS(3, off); 318 319 fpos.x = 14; 320 fpos.y = 0; 321 off = gfx_text_find_pos(&anchor, &fmt, "Abc", &fpos); 322 PCUT_ASSERT_INT_EQUALS(3, off); 323 324 gfx_font_close(font); 325 gfx_typeface_destroy(tface); 326 327 rc = gfx_context_delete(gc); 328 PCUT_ASSERT_ERRNO_VAL(EOK, rc); 329 } 330 331 /** gfx_text_cont() produces correct continuation parameters */ 332 PCUT_TEST(text_cont) 333 { 334 gfx_typeface_t *tface; 335 gfx_font_t *font; 336 gfx_context_t *gc; 337 gfx_color_t *color; 338 test_gc_t tgc; 339 gfx_text_fmt_t fmt; 340 gfx_coord2_t anchor; 341 gfx_coord2_t cpos; 342 gfx_text_fmt_t cfmt; 343 errno_t rc; 344 345 rc = gfx_context_new(&test_ops, (void *)&tgc, &gc); 346 PCUT_ASSERT_ERRNO_VAL(EOK, rc); 347 348 rc = gfx_typeface_create(gc, &tface); 349 PCUT_ASSERT_ERRNO_VAL(EOK, rc); 350 351 rc = gfx_font_create_textmode(tface, &font); 352 PCUT_ASSERT_ERRNO_VAL(EOK, rc); 353 354 rc = gfx_color_new_rgb_i16(0, 0, 0, &color); 355 PCUT_ASSERT_ERRNO_VAL(EOK, rc); 356 357 anchor.x = 10; 358 anchor.y = 20; 359 gfx_text_fmt_init(&fmt); 360 fmt.font = font; 361 fmt.color = color; 362 363 gfx_text_cont(&anchor, &fmt, "Abc", &cpos, &cfmt); 364 365 PCUT_ASSERT_INT_EQUALS(13, cpos.x); 366 PCUT_ASSERT_INT_EQUALS(20, cpos.y); 367 PCUT_ASSERT_EQUALS(fmt.color, cfmt.color); 368 PCUT_ASSERT_EQUALS(gfx_halign_left, cfmt.halign); 369 PCUT_ASSERT_EQUALS(gfx_valign_baseline, cfmt.valign); 370 371 gfx_font_close(font); 372 gfx_typeface_destroy(tface); 373 gfx_color_delete(color); 374 375 rc = gfx_context_delete(gc); 376 PCUT_ASSERT_ERRNO_VAL(EOK, rc); 377 } 378 379 /** gfx_text_rect() computes bounding rectangle */ 380 PCUT_TEST(text_rect) 381 { 382 gfx_typeface_t *tface; 383 gfx_font_t *font; 384 gfx_context_t *gc; 385 gfx_color_t *color; 386 test_gc_t tgc; 387 gfx_text_fmt_t fmt; 388 gfx_coord2_t anchor; 389 gfx_rect_t rect; 390 errno_t rc; 391 392 rc = gfx_context_new(&test_ops, (void *)&tgc, &gc); 393 PCUT_ASSERT_ERRNO_VAL(EOK, rc); 394 395 rc = gfx_typeface_create(gc, &tface); 396 PCUT_ASSERT_ERRNO_VAL(EOK, rc); 397 398 rc = gfx_font_create_textmode(tface, &font); 399 PCUT_ASSERT_ERRNO_VAL(EOK, rc); 400 401 rc = gfx_color_new_rgb_i16(0, 0, 0, &color); 402 PCUT_ASSERT_ERRNO_VAL(EOK, rc); 403 404 anchor.x = 10; 405 anchor.y = 20; 406 gfx_text_fmt_init(&fmt); 407 fmt.font = font; 408 fmt.color = color; 409 410 gfx_text_rect(&anchor, &fmt, "Abc", &rect); 411 412 PCUT_ASSERT_INT_EQUALS(10, rect.p0.x); 413 PCUT_ASSERT_INT_EQUALS(20, rect.p0.y); 414 PCUT_ASSERT_INT_EQUALS(13, rect.p1.x); 415 PCUT_ASSERT_INT_EQUALS(21, rect.p1.y); 416 417 gfx_font_close(font); 418 gfx_typeface_destroy(tface); 419 gfx_color_delete(color); 420 421 rc = gfx_context_delete(gc); 422 PCUT_ASSERT_ERRNO_VAL(EOK, rc); 142 static errno_t testgc_set_color(void *arg, gfx_color_t *color) 143 { 144 return EOK; 145 } 146 147 static errno_t testgc_fill_rect(void *arg, gfx_rect_t *rect) 148 { 149 return EOK; 150 } 151 152 static errno_t testgc_bitmap_create(void *arg, gfx_bitmap_params_t *params, 153 gfx_bitmap_alloc_t *alloc, void **rbm) 154 { 155 test_gc_t *tgc = (test_gc_t *) arg; 156 testgc_bitmap_t *tbm; 157 158 tbm = calloc(1, sizeof(testgc_bitmap_t)); 159 if (tbm == NULL) 160 return ENOMEM; 161 162 if (alloc == NULL) { 163 tbm->alloc.pitch = (params->rect.p1.x - params->rect.p0.x) * 164 sizeof(uint32_t); 165 tbm->alloc.off0 = 0; 166 tbm->alloc.pixels = calloc(sizeof(uint32_t), 167 tbm->alloc.pitch * (params->rect.p1.y - params->rect.p0.y)); 168 tbm->myalloc = true; 169 if (tbm->alloc.pixels == NULL) { 170 free(tbm); 171 return ENOMEM; 172 } 173 } else { 174 tbm->alloc = *alloc; 175 } 176 177 tbm->tgc = tgc; 178 tgc->bm_params = *params; 179 tgc->bm_pixels = tbm->alloc.pixels; 180 *rbm = (void *)tbm; 181 return EOK; 182 } 183 184 static errno_t testgc_bitmap_destroy(void *bm) 185 { 186 testgc_bitmap_t *tbm = (testgc_bitmap_t *)bm; 187 if (tbm->myalloc) 188 free(tbm->alloc.pixels); 189 free(tbm); 190 return EOK; 191 } 192 193 static errno_t testgc_bitmap_render(void *bm, gfx_rect_t *srect, 194 gfx_coord2_t *offs) 195 { 196 testgc_bitmap_t *tbm = (testgc_bitmap_t *)bm; 197 tbm->tgc->bm_srect = *srect; 198 tbm->tgc->bm_offs = *offs; 199 return EOK; 200 } 201 202 static errno_t testgc_bitmap_get_alloc(void *bm, gfx_bitmap_alloc_t *alloc) 203 { 204 testgc_bitmap_t *tbm = (testgc_bitmap_t *)bm; 205 *alloc = tbm->alloc; 206 return EOK; 423 207 } 424 208
Note:
See TracChangeset
for help on using the changeset viewer.