Changeset ea459d4 in mainline for uspace/lib/gfxfont/src/font.c
- Timestamp:
- 2020-09-24T14:25:21Z (4 years ago)
- Branches:
- lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
- Children:
- 120031a5
- Parents:
- aaf962e6
- git-author:
- Jiri Svoboda <jiri@…> (2020-09-22 17:25:10)
- git-committer:
- Jiri Svoboda <jiri@…> (2020-09-24 14:25:21)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
uspace/lib/gfxfont/src/font.c
raaf962e6 rea459d4 81 81 } 82 82 83 /** Create font .83 /** Create font with existing font info structure. 84 84 * 85 85 * @param tface Typeface 86 * @param finfo Font info 86 87 * @param metrics Font metrics 87 88 * @param rfont Place to store pointer to new font … … 91 92 * was lost 92 93 */ 93 errno_t gfx_font_create(gfx_typeface_t *tface, gfx_font_props_t *props, 94 gfx_font_metrics_t *metrics, gfx_font_t **rfont) 95 { 96 gfx_font_info_t *finfo = NULL; 94 static errno_t gfx_font_create_with_info(gfx_typeface_t *tface, 95 gfx_font_info_t *finfo, gfx_font_metrics_t *metrics, gfx_font_t **rfont) 96 { 97 97 gfx_font_t *font = NULL; 98 98 gfx_bitmap_params_t params; 99 99 errno_t rc; 100 101 finfo = calloc(1, sizeof(gfx_font_info_t));102 if (finfo == NULL) {103 rc = ENOMEM;104 goto error;105 }106 100 107 101 font = calloc(1, sizeof(gfx_font_t)); … … 110 104 goto error; 111 105 } 112 113 finfo->typeface = tface;114 finfo->props = *props;115 finfo->font = font;116 117 106 font->typeface = tface; 107 font->finfo = finfo; 118 108 119 109 font->rect.p0.x = 0; … … 138 128 font->metrics = *metrics; 139 129 list_initialize(&font->glyphs); 130 *rfont = font; 131 return EOK; 132 error: 133 if (font != NULL) 134 free(font); 135 return rc; 136 } 137 138 /** Create font. 139 * 140 * @param tface Typeface 141 * @param props Font properties 142 * @param metrics Font metrics 143 * @param rfont Place to store pointer to new font 144 * 145 * @return EOK on success, EINVAL if parameters are invald, 146 * ENOMEM if insufficient resources, EIO if graphic device connection 147 * was lost 148 */ 149 errno_t gfx_font_create(gfx_typeface_t *tface, gfx_font_props_t *props, 150 gfx_font_metrics_t *metrics, gfx_font_t **rfont) 151 { 152 gfx_font_info_t *finfo = NULL; 153 gfx_font_t *font = NULL; 154 errno_t rc; 155 156 finfo = calloc(1, sizeof(gfx_font_info_t)); 157 if (finfo == NULL) { 158 rc = ENOMEM; 159 goto error; 160 } 161 162 finfo->typeface = tface; 163 finfo->props = *props; 164 165 rc = gfx_font_create_with_info(tface, finfo, metrics, &font); 166 if (rc != EOK) 167 goto error; 168 169 finfo->font = font; 140 170 list_append(&finfo->lfonts, &tface->fonts); 141 171 *rfont = font; … … 144 174 if (finfo != NULL) 145 175 free(finfo); 146 if (font != NULL)147 free(font);148 176 return rc; 149 177 } … … 157 185 errno_t gfx_font_open(gfx_font_info_t *finfo, gfx_font_t **rfont) 158 186 { 187 errno_t rc; 188 159 189 if (finfo->font == NULL) { 160 /* 161 * We cannot load an absent font yet. 162 * This should not happen. 163 */ 164 assert(false); 165 return ENOTSUP; 190 /* Load absent font from TPF file */ 191 rc = gfx_font_load(finfo); 192 if (rc != EOK) 193 return rc; 194 195 assert(finfo->font != NULL); 196 finfo->font->finfo = finfo; 166 197 } 167 198 … … 184 215 } 185 216 217 font->finfo->font = NULL; 186 218 free(font); 187 219 } … … 360 392 } 361 393 394 /** Load font properties from RIFF TPF file. 395 * 396 * @param parent Parent chunk 397 * @param props Font properties 398 * @return EOK on success or an error code 399 */ 400 static errno_t gfx_font_props_load(riff_rchunk_t *parent, 401 gfx_font_props_t *props) 402 { 403 errno_t rc; 404 riff_rchunk_t propsck; 405 size_t nread; 406 407 rc = riff_rchunk_match(parent, CKID_fprp, &propsck); 408 if (rc != EOK) 409 return rc; 410 411 rc = riff_read(&propsck, (void *) props, sizeof(*props), &nread); 412 if (rc != EOK || nread != sizeof(*props)) 413 return EIO; 414 415 rc = riff_rchunk_end(&propsck); 416 if (rc != EOK) 417 return rc; 418 419 return EOK; 420 } 421 362 422 /** Save font properties to RIFF TPF file. 363 423 * … … 386 446 } 387 447 448 /** Load font metrics from RIFF TPF file. 449 * 450 * @param parent Parent chunk 451 * @param metrics Font metrics 452 * @return EOK on success or an error code 453 */ 454 static errno_t gfx_font_metrics_load(riff_rchunk_t *parent, 455 gfx_font_metrics_t *metrics) 456 { 457 errno_t rc; 458 riff_rchunk_t mtrck; 459 size_t nread; 460 461 rc = riff_rchunk_match(parent, CKID_fmtr, &mtrck); 462 if (rc != EOK) 463 return rc; 464 465 rc = riff_read(&mtrck, (void *) metrics, sizeof(*metrics), &nread); 466 if (rc != EOK || nread != sizeof(*metrics)) 467 return EIO; 468 469 rc = riff_rchunk_end(&mtrck); 470 if (rc != EOK) 471 return rc; 472 473 return EOK; 474 } 475 388 476 /** Save font metrics to RIFF TPF file. 389 477 * … … 413 501 } 414 502 503 /** Load font bitmap from RIFF TPF file. 504 * 505 * @param parent Parent chunk 506 * @param font Font 507 * @return EOK on success or an error code 508 */ 509 static errno_t gfx_font_bitmap_load(riff_rchunk_t *parent, gfx_font_t *font) 510 { 511 errno_t rc; 512 riff_rchunk_t bmpck; 513 gfx_bitmap_params_t params; 514 gfx_bitmap_alloc_t alloc; 515 gfx_bitmap_t *bitmap = NULL; 516 uint32_t width; 517 uint32_t height; 518 uint32_t depth; 519 size_t nread; 520 521 rc = riff_rchunk_match(parent, CKID_fbmp, &bmpck); 522 if (rc != EOK) 523 goto error; 524 525 rc = riff_read_uint32(&bmpck, &width); 526 if (rc != EOK) 527 goto error; 528 529 rc = riff_read_uint32(&bmpck, &height); 530 if (rc != EOK) 531 goto error; 532 533 rc = riff_read_uint32(&bmpck, &depth); 534 if (rc != EOK) 535 goto error; 536 537 if (depth != 8 * sizeof(uint32_t)) { 538 rc = ENOTSUP; 539 goto error; 540 } 541 542 gfx_bitmap_params_init(¶ms); 543 params.rect.p0.x = 0; 544 params.rect.p0.y = 0; 545 params.rect.p1.x = width; 546 params.rect.p1.y = height; 547 548 rc = gfx_bitmap_create(font->typeface->gc, ¶ms, NULL, &bitmap); 549 if (rc != EOK) 550 goto error; 551 552 rc = gfx_bitmap_get_alloc(bitmap, &alloc); 553 if (rc != EOK) 554 goto error; 555 556 rc = riff_read(&bmpck, (void *) alloc.pixels, 557 width * height * sizeof(uint32_t), &nread); 558 if (rc != EOK) 559 goto error; 560 561 if (nread != width * height * sizeof(uint32_t)) { 562 rc = EIO; 563 goto error; 564 } 565 566 rc = riff_rchunk_end(&bmpck); 567 if (rc != EOK) 568 goto error; 569 570 gfx_bitmap_destroy(font->bitmap); 571 font->bitmap = bitmap; 572 font->rect = params.rect; 573 return EOK; 574 error: 575 if (bitmap != NULL) 576 gfx_bitmap_destroy(bitmap); 577 return rc; 578 } 579 415 580 /** Save font bitmap to RIFF TPF file. 416 581 * … … 457 622 } 458 623 624 /** Load font info from RIFF TPF file. 625 * 626 * @param tface Containing typeface 627 * @param parent Parent chunk 628 * @return EOK on success or an error code 629 */ 630 errno_t gfx_font_info_load(gfx_typeface_t *tface, riff_rchunk_t *parent) 631 { 632 errno_t rc; 633 riff_rchunk_t fontck; 634 gfx_font_props_t props; 635 gfx_font_info_t *finfo = NULL; 636 gfx_font_t *font = NULL; 637 638 rc = riff_rchunk_list_match(parent, LTYPE_font, &fontck); 639 if (rc != EOK) 640 goto error; 641 642 finfo = calloc(1, sizeof(gfx_font_info_t)); 643 if (finfo == NULL) { 644 rc = ENOMEM; 645 goto error; 646 } 647 648 finfo->fontck = fontck; 649 650 rc = gfx_font_props_load(&fontck, &props); 651 if (rc != EOK) 652 goto error; 653 654 rc = riff_rchunk_end(&fontck); 655 if (rc != EOK) 656 goto error; 657 658 finfo->typeface = tface; 659 list_append(&finfo->lfonts, &tface->fonts); 660 finfo->props = props; 661 finfo->font = NULL; 662 663 return EOK; 664 error: 665 if (finfo != NULL) 666 free(finfo); 667 if (font != NULL) 668 gfx_font_close(font); 669 return rc; 670 } 671 672 /** Load font from RIFF TPF file. 673 * 674 * @param finfo Font information 675 * @return EOK on success or an error code 676 */ 677 errno_t gfx_font_load(gfx_font_info_t *finfo) 678 { 679 errno_t rc; 680 gfx_font_metrics_t metrics; 681 gfx_font_t *font = NULL; 682 683 /* Seek to beginning of chunk (just after list type) */ 684 rc = riff_rchunk_seek(&finfo->fontck, sizeof(uint32_t), SEEK_SET); 685 if (rc != EOK) 686 goto error; 687 688 rc = gfx_font_props_load(&finfo->fontck, &finfo->props); 689 if (rc != EOK) 690 goto error; 691 692 rc = gfx_font_metrics_load(&finfo->fontck, &metrics); 693 if (rc != EOK) 694 goto error; 695 696 rc = gfx_font_create_with_info(finfo->typeface, finfo, &metrics, &font); 697 if (rc != EOK) 698 goto error; 699 700 rc = gfx_font_bitmap_load(&finfo->fontck, font); 701 if (rc != EOK) 702 goto error; 703 704 while (true) { 705 rc = gfx_glyph_load(font, &finfo->fontck); 706 if (rc == ENOENT) 707 break; 708 if (rc != EOK) 709 goto error; 710 } 711 712 finfo->font = font; 713 return EOK; 714 error: 715 if (font != NULL) 716 gfx_font_close(font); 717 return rc; 718 } 719 459 720 /** Save font into RIFF TPF file. 460 721 * 461 722 * @param finfo Font info 462 723 * @param riffw RIFF writer 724 * @return EOK on success or an error code 463 725 */ 464 726 errno_t gfx_font_save(gfx_font_info_t *finfo, riffw_t *riffw)
Note:
See TracChangeset
for help on using the changeset viewer.