Ticket #2172: font-wip.diff
File font-wip.diff, 22.9 KB (added by , 18 years ago) |
---|
-
tools/convbdf.c
37 37 #include <string.h> 38 38 #include <time.h> 39 39 40 int READ_UINT16(void *addr) { 41 unsigned char *buf = addr; 42 return (buf[0] << 8) | buf[1]; 43 } 44 45 void WRITE_UINT16(void *addr, int value) { 46 unsigned char *buf = addr; 47 buf[0] = (value >> 8) & 0xFF; 48 buf[1] = value & 0xFF; 49 } 50 40 51 /* BEGIN font.h*/ 41 52 /* bitmap_t helper macros*/ 42 53 #define BITMAP_WORDS(x) (((x)+15)/16) /* image size in words*/ … … 49 60 50 61 typedef unsigned short bitmap_t; /* bitmap image unit size*/ 51 62 63 typedef struct { 64 int w; 65 int h; 66 int x; 67 int y; 68 } BBX; 69 52 70 /* builtin C-based proportional/fixed font structure */ 53 71 /* based on The Microwindows Project http://microwindows.org */ 54 72 struct font { 55 73 char * name; /* font name*/ 56 74 int maxwidth; /* max width in pixels*/ 57 75 int height; /* height in pixels*/ 76 int fbbw, fbbh, fbbx, fbby; /* max bounding box */ 58 77 int ascent; /* ascent (baseline) height*/ 59 78 int firstchar; /* first character in bitmap*/ 60 79 int size; /* font size in glyphs*/ 61 80 bitmap_t* bits; /* 16-bit right-padded bitmap data*/ 62 81 unsigned long* offset; /* offsets into bitmap data*/ 63 82 unsigned char* width; /* character widths or NULL if fixed*/ 83 BBX* bbx; /* character bounding box or NULL if fixed*/ 64 84 int defaultchar; /* default char (not glyph index)*/ 65 85 long bits_size; /* # words of bitmap_t bits*/ 66 86 … … 69 89 char * copyright; /* copyright info for loadable fonts*/ 70 90 int pixel_size; 71 91 int descent; 72 int fbbw, fbbh, fbbx, fbby;73 92 }; 74 93 /* END font.h*/ 75 94 … … 93 112 int bdf_read_header(FILE *fp, struct font* pf); 94 113 int bdf_read_bitmaps(FILE *fp, struct font* pf); 95 114 char * bdf_getline(FILE *fp, char *buf, int len); 96 bitmap_t bdf_hexval(unsigned char *buf , int ndx1, int ndx2);115 bitmap_t bdf_hexval(unsigned char *buf); 97 116 98 117 int gen_c_source(struct font* pf, char *path); 99 118 … … 446 465 pf->bits = (bitmap_t *)malloc(pf->bits_size * sizeof(bitmap_t) + EXTRA); 447 466 pf->offset = (unsigned long *)malloc(pf->size * sizeof(unsigned long)); 448 467 pf->width = (unsigned char *)malloc(pf->size * sizeof(unsigned char)); 468 pf->bbx = (BBX *)malloc(pf->size * sizeof(BBX)); 449 469 450 470 if (!pf->bits || !pf->offset || !pf->width) { 451 471 fprintf(stderr, "Error: no memory for font load\n"); … … 463 483 int i, k, encoding, width; 464 484 int bbw, bbh, bbx, bby; 465 485 int proportional = 0; 486 int need_bbx = 0; 466 487 int encodetable = 0; 467 488 long l; 468 489 char buf[256]; … … 523 544 continue; 524 545 } 525 546 pf->offset[encoding-pf->firstchar] = ofs; 547 pf->width[encoding-pf->firstchar] = width; 526 548 527 /* calc char width*/ 528 if (bbx < 0) { 529 width -= bbx; 530 /*if (width > maxwidth) 531 width = maxwidth;*/ 532 bbx = 0; 533 } 549 pf->bbx[encoding-pf->firstchar].w = bbw; 550 pf->bbx[encoding-pf->firstchar].h = bbh; 551 pf->bbx[encoding-pf->firstchar].x = bbx; 552 pf->bbx[encoding-pf->firstchar].y = bby; 553 534 554 if (width > maxwidth) 535 555 maxwidth = width; 536 pf->width[encoding-pf->firstchar] = width;537 556 538 557 /* clear bitmap*/ 539 memset(ch_bitmap, 0, BITMAP_BYTES( width) * pf->height);558 memset(ch_bitmap, 0, BITMAP_BYTES(bbw) * bbh); 540 559 541 ch_words = BITMAP_WORDS(width); 542 #define BM(row,col) (*(ch_bitmap + ((row)*ch_words) + (col))) 543 #define BITMAP_NIBBLES (BITMAP_BITSPERIMAGE/4) 560 ch_words = BITMAP_WORDS(bbw); 544 561 545 562 /* read bitmaps*/ 546 for (i = 0; ; ++i) { 547 int hexnibbles; 548 563 for (i = 0; i < bbh; ++i) { 549 564 if (!bdf_getline(fp, buf, sizeof(buf))) { 550 565 fprintf(stderr, "Error: EOF reading BITMAP data\n"); 551 566 return 0; … … 553 568 if (isprefix(buf, "ENDCHAR")) 554 569 break; 555 570 556 hexnibbles = strlen(buf);557 571 for (k = 0; k < ch_words; ++k) { 558 int ndx = k * BITMAP_NIBBLES;559 int padnibbles = hexnibbles - ndx;560 572 bitmap_t value; 561 573 562 if (padnibbles <= 0) 563 break; 564 if (padnibbles >= BITMAP_NIBBLES) 565 padnibbles = 0; 574 value = bdf_hexval((unsigned char *)buf); 566 575 567 value = bdf_hexval((unsigned char *)buf, 568 ndx, ndx+BITMAP_NIBBLES-1-padnibbles); 569 value <<= padnibbles * BITMAP_NIBBLES; 570 571 BM(pf->height - pf->descent - bby - bbh + i, k) |= 572 value >> bbx; 573 /* handle overflow into next image word*/ 574 if (bbx) { 575 BM(pf->height - pf->descent - bby - bbh + i, k+1) = 576 value << (BITMAP_BITSPERIMAGE - bbx); 576 if (bbw > 8) { 577 WRITE_UINT16(ch_bitmap, value); 578 } else { 579 WRITE_UINT16(ch_bitmap, value << 8); 577 580 } 581 ch_bitmap++; 578 582 } 579 583 } 580 584 581 ofs += BITMAP_WORDS(width) * pf->height; 582 585 ofs += BITMAP_WORDS(width) * bbh; 583 586 continue; 584 587 } 585 588 if (strequal(buf, "ENDFONT")) … … 596 599 if (pf->offset[i] == (unsigned long)-1) { 597 600 pf->offset[i] = pf->offset[defchar]; 598 601 pf->width[i] = pf->width[defchar]; 602 pf->bbx[i].w = pf->bbx[defchar].w; 603 pf->bbx[i].h = pf->bbx[defchar].h; 604 pf->bbx[i].x = pf->bbx[defchar].x; 605 pf->bbx[i].y = pf->bbx[defchar].y; 599 606 } 600 607 } 601 608 … … 606 613 encodetable = 1; 607 614 break; 608 615 } 609 l += BITMAP_WORDS(pf-> width[i]) * pf->height;616 l += BITMAP_WORDS(pf->bbx[i].w) * pf->bbx[i].h; 610 617 } 611 618 if (!encodetable) { 612 619 free(pf->offset); … … 625 632 pf->width = NULL; 626 633 } 627 634 635 /* determine if the font needs a bbx table */ 636 for (i = 0; i < pf->size; ++i) { 637 if (pf->bbx[i].w != pf->fbbw || pf->bbx[i].h != pf->fbbh || pf->bbx[i].x != pf->fbbx || pf->bbx[i].y != pf->fbby) { 638 need_bbx = 1; 639 break; 640 } 641 } 642 if (!need_bbx) { 643 free(pf->bbx); 644 pf->bbx = NULL; 645 } 646 628 647 /* reallocate bits array to actual bits used*/ 629 648 if (ofs < pf->bits_size) { 630 649 pf->bits = realloc(pf->bits, ofs * sizeof(bitmap_t)); … … 670 689 return buf; 671 690 } 672 691 673 /* return hex value of portion of buffer*/ 674 bitmap_t bdf_hexval(unsigned char *buf, int ndx1, int ndx2) 675 { 692 /* return hex value of buffer */ 693 bitmap_t bdf_hexval(unsigned char *buf) { 676 694 bitmap_t val = 0; 677 int i, c;695 unsigned char *ptr; 678 696 679 for (i = ndx1; i <= ndx2; ++i) { 680 c = buf[i]; 697 for (ptr = buf; *ptr; ptr++) { 698 int c = *ptr; 699 681 700 if (c >= '0' && c <= '9') 682 701 c -= '0'; 702 else if (c >= 'A' && c <= 'F') 703 c = c - 'A' + 10; 704 else if (c >= 'a' && c <= 'f') 705 c = c - 'a' + 10; 683 706 else 684 if (c >= 'A' && c <= 'F') 685 c = c - 'A' + 10; 686 else 687 if (c >= 'a' && c <= 'f') 688 c = c - 'a' + 10; 689 else 690 c = 0; 707 c = 0; 691 708 val = (val << 4) | c; 692 709 } 693 710 return val; … … 704 721 bitmap_t *ofs = pf->bits; 705 722 char buf[256]; 706 723 char obuf[256]; 724 char bbuf[256]; 707 725 char hdr1[] = { 708 726 "/* Generated by convbdf on %s. */\n" 709 727 "#include \"common/stdafx.h\"\n" … … 713 731 " name: %s\n" 714 732 " facename: %s\n" 715 733 " w x h: %dx%d\n" 734 " bbx: %d %d %d %d\n" 716 735 " size: %d\n" 717 736 " ascent: %d\n" 718 737 " descent: %d\n" … … 742 761 pf->name, 743 762 pf->facename? pf->facename: "", 744 763 pf->maxwidth, pf->height, 764 pf->fbbw, pf->fbbh, pf->fbbx, pf->fbby, 745 765 pf->size, 746 766 pf->ascent, pf->descent, 747 767 pf->firstchar, pf->firstchar, … … 754 774 for (i = 0; i < pf->size; ++i) { 755 775 int x; 756 776 int bitcount = 0; 757 int width = pf->width ? pf->width[i] : pf->maxwidth; 758 int height = pf->height; 777 int width = pf->bbx ? pf->bbx[i].w : pf->fbbw; 778 int height = pf->bbx ? pf->bbx[i].h : pf->fbbh; 779 int xoff = pf->bbx ? pf->bbx[i].x : pf->fbbx; 780 int yoff = pf->bbx ? pf->bbx[i].y : pf->fbby; 759 781 bitmap_t *bits = pf->bits + (pf->offset? pf->offset[i]: (height * i)); 760 782 bitmap_t bitvalue = 0; 761 783 … … 771 793 did_defaultchar = 1; 772 794 } 773 795 774 fprintf(ofp, "\n/* Character %d (0x%02x):\n width %d", 775 i+pf->firstchar, i+pf->firstchar, width); 796 fprintf(ofp, "\n/* Character %d (0x%02x):\n width %d\n bbx ( %d, %d, %d, %d )\n", 797 i+pf->firstchar, i+pf->firstchar, 798 pf->width ? pf->width[i+pf->firstchar] : pf->maxwidth, 799 width, height, xoff, yoff); 776 800 777 801 if (gen_map) { 778 802 fprintf(ofp, "\n +"); … … 780 804 fprintf(ofp, "+\n"); 781 805 782 806 x = 0; 783 while (height > 0) { 807 int h = height; 808 while (h > 0) { 784 809 if (x == 0) fprintf(ofp, " |"); 785 810 786 811 if (bitcount <= 0) { 787 812 bitcount = BITMAP_BITSPERIMAGE; 788 bitvalue = *bits++; 813 bitvalue = READ_UINT16(bits); 814 bits++; 789 815 } 790 816 791 817 fprintf(ofp, BITMAP_TESTBIT(bitvalue)? "*": " "); … … 794 820 --bitcount; 795 821 if (++x == width) { 796 822 fprintf(ofp, "|\n"); 797 --h eight;823 --h; 798 824 x = 0; 799 825 bitcount = 0; 800 826 } … … 806 832 } else 807 833 fprintf(ofp, "\n*/\n"); 808 834 809 bits = pf->bits + (pf->offset? pf->offset[i]: ( pf->height * i));810 for (x = BITMAP_WORDS(width) * pf->height; x > 0; --x) {811 fprintf(ofp, "0x%04x,\n", *bits);835 bits = pf->bits + (pf->offset? pf->offset[i]: (height * i)); 836 for (x = BITMAP_WORDS(width) * height; x > 0; --x) { 837 fprintf(ofp, "0x%04x,\n", READ_UINT16(bits)); 812 838 if (!did_syncmsg && *bits++ != *ofs++) { 813 839 fprintf(stderr, "Warning: found encoding values in non-sorted order (not an error).\n"); 814 840 did_syncmsg = 1; … … 839 865 fprintf(ofp, "};\n\n"); 840 866 } 841 867 868 /* output bbox table */ 869 if (pf->bbx) { 870 fprintf(ofp, "/* Bounding box data. */\n" 871 "static const BBX _sysfont_bbx[] = {\n"); 872 873 for (i = 0; i < pf->size; ++i) 874 fprintf(ofp, " { %d, %d, %d, %d },\t/* (0x%02x) */\n", 875 pf->bbx[i].w, pf->bbx[i].h, pf->bbx[i].x, pf->bbx[i].y, i+pf->firstchar); 876 fprintf(ofp, "};\n\n"); 877 } 878 842 879 /* output struct font struct*/ 843 880 if (pf->offset) 844 881 sprintf(obuf, "_sysfont_offset,"); … … 850 887 else 851 888 sprintf(buf, "0, /* fixed width*/"); 852 889 890 if (pf->width) 891 sprintf(bbuf, "_sysfont_bbx,"); 892 else 893 sprintf(bbuf, "0, /* fixed bbox*/"); 894 853 895 fprintf(ofp, 854 896 "/* Exported structure definition. */\n" 855 897 "static const FontDesc desc = {\n" 856 898 "\t" "\"%s\",\n" 857 899 "\t" "%d,\n" 858 900 "\t" "%d,\n" 901 "\t" "%d, %d, %d, %d,\n" 859 902 "\t" "%d,\n" 860 903 "\t" "%d,\n" 861 904 "\t" "%d,\n" 862 905 "\t" "_font_bits,\n" 863 906 "\t" "%s\n" 864 907 "\t" "%s\n" 908 "\t" "%s\n" 865 909 "\t" "%d,\n" 866 910 "\t" "sizeof(_font_bits)/sizeof(bitmap_t)\n" 867 911 "};\n", 868 912 pf->name, 869 913 pf->maxwidth, pf->height, 914 pf->fbbw, pf->fbbh, pf->fbbx, pf->fbby, 870 915 pf->ascent, 871 916 pf->firstchar, 872 917 pf->size, 873 918 obuf, 874 919 buf, 920 bbuf, 875 921 pf->defaultchar); 876 922 877 fprintf(ofp, "\n" "extern const NewFont g_sysfont(desc);\n"); 923 fprintf(ofp, "\n" "#if !(defined(PALMOS_ARM) || defined(PALMOS_DEBUG) || defined(__GP32__))\n"); 924 fprintf(ofp, "extern const NewFont g_sysfont(desc);\n"); 925 fprintf(ofp, "#else\n"); 926 fprintf(ofp, "DEFINE_FONT(g_sysfont)\n"); 927 fprintf(ofp, "#endif\n"); 878 928 fprintf(ofp, "\n} // End of namespace Graphics\n"); 879 929 880 930 return 0; -
graphics/font.cpp
48 48 49 49 void NewFont::drawChar(Surface *dst, byte chr, int tx, int ty, uint32 color) const { 50 50 assert(dst != 0); 51 byte *ptr = (byte *)dst->getBasePtr(tx, ty);52 51 53 52 assert(desc.bits != 0 && desc.maxwidth <= 17); 54 53 assert(dst->bytesPerPixel == 1 || dst->bytesPerPixel == 2); … … 58 57 chr = desc.defaultchar; 59 58 } 60 59 61 const int w = getCharWidth(chr);62 60 chr -= desc.firstchar; 63 const bitmap_t *tmp = desc.bits + (desc.offset ? desc.offset[chr] : (chr * desc.height));64 61 65 for (int y = 0; y < desc.height; y++, ptr += dst->pitch) { 66 const bitmap_t buffer = *tmp++; 62 int bbw, bbh, bbx, bby; 63 64 // Get the bounding box of the character 65 if (!desc.bbx) { 66 bbw = desc.fbbw; 67 bbh = desc.fbbh; 68 bbx = desc.fbbx; 69 bby = desc.fbby; 70 } else { 71 bbw = desc.bbx[chr].w; 72 bbh = desc.bbx[chr].h; 73 bbx = desc.bbx[chr].x; 74 bby = desc.bbx[chr].y; 75 } 76 77 byte *ptr = (byte *)dst->getBasePtr(tx + bbx, ty + desc.ascent - bby - bbh); 78 79 const bitmap_t *tmp = desc.bits + (desc.offset ? desc.offset[chr] : (chr * desc.fbbh)); 80 81 for (int y = 0; y < bbh; y++, ptr += dst->pitch) { 82 const bitmap_t buffer = READ_UINT16(tmp); 83 tmp++; 67 84 bitmap_t mask = 0x8000; 68 85 if (ty + y < 0 || ty + y >= dst->h) 69 86 continue; 70 87 71 for (int x = 0; x < w; x++, mask >>= 1) {88 for (int x = 0; x < bbw; x++, mask >>= 1) { 72 89 if (tx + x < 0 || tx + x >= dst->w) 73 90 continue; 74 91 if ((buffer & mask) != 0) { … … 94 111 #define BITMAP_TESTBIT(m) ((m) & BITMAP_FIRSTBIT) 95 112 #define BITMAP_SHIFTBIT(m) ((bitmap_t) ((m) << 1)) 96 113 97 typedef unsigned short bitmap_t; /* bitmap image unit size*/98 99 114 /* builtin C-based proportional/fixed font structure */ 100 115 /* based on The Microwindows Project http://microwindows.org */ 101 116 struct NewFontData { … … 108 123 bitmap_t* bits; /* 16-bit right-padded bitmap data*/ 109 124 unsigned long* offset; /* offsets into bitmap data*/ 110 125 unsigned char* width; /* character widths or NULL if fixed*/ 126 BBX* bbx; /* character bounding box or NULL if fixed */ 111 127 int defaultchar; /* default char (not glyph index)*/ 112 128 long bits_size; /* # words of bitmap_t bits*/ 113 129 … … 132 148 int bdf_read_header(Common::SeekableReadStream &fp, NewFontData* pf); 133 149 int bdf_read_bitmaps(Common::SeekableReadStream &fp, NewFontData* pf); 134 150 char * bdf_getline(Common::SeekableReadStream &fp, char *buf, int len); 135 bitmap_t bdf_hexval(unsigned char *buf , int ndx1, int ndx2);151 bitmap_t bdf_hexval(unsigned char *buf); 136 152 137 153 void free_font(NewFontData* pf) { 138 154 if (!pf) … … 148 164 /* build incore structure from .bdf file*/ 149 165 NewFontData* bdf_read_font(Common::SeekableReadStream &fp) { 150 166 NewFontData* pf; 167 uint32 pos = fp.pos(); 151 168 152 169 pf = (NewFontData*)calloc(1, sizeof(NewFontData)); 153 170 if (!pf) … … 158 175 goto errout; 159 176 } 160 177 178 fp.seek(pos, SEEK_SET); 179 161 180 if (!bdf_read_bitmaps(fp, pf)) { 162 181 fprintf(stderr, "Error reading font bitmaps\n"); 163 182 goto errout; … … 173 192 /* read bdf font header information, return 0 on error*/ 174 193 int bdf_read_header(Common::SeekableReadStream &fp, NewFontData* pf) { 175 194 int encoding = 0; 176 int nchars = 0, maxwidth ;195 int nchars = 0, maxwidth, maxheight; 177 196 int firstchar = 65535; 178 197 int lastchar = -1; 179 198 char buf[256]; … … 287 306 /* use the font boundingbox to get initial maxwidth*/ 288 307 /*maxwidth = pf->fbbw - pf->fbbx;*/ 289 308 maxwidth = pf->fbbw; 309 maxheight = pf->fbbh; 290 310 291 /* initially use font maxwidth * heightfor bits allocation*/292 pf->bits_size = nchars * BITMAP_WORDS(maxwidth) * pf->height;311 /* initially use font bounding box for bits allocation*/ 312 pf->bits_size = nchars * BITMAP_WORDS(maxwidth) * maxheight; 293 313 294 314 /* allocate bits, offset, and width arrays*/ 295 315 pf->bits = (bitmap_t *)malloc(pf->bits_size * sizeof(bitmap_t) + EXTRA); 296 316 pf->offset = (unsigned long *)malloc(pf->size * sizeof(unsigned long)); 297 317 pf->width = (unsigned char *)malloc(pf->size * sizeof(unsigned char)); 318 pf->bbx = (BBX *)malloc(pf->size * sizeof(BBX)); 298 319 299 320 if (!pf->bits || !pf->offset || !pf->width) { 300 321 fprintf(stderr, "Error: no memory for font load\n"); … … 311 332 int i, k, encoding = 0, width = 0; 312 333 int bbw, bbh = 0, bbx = 0, bby = 0; 313 334 int proportional = 0; 335 int need_bbx = 0; 314 336 int encodetable = 0; 315 337 long l; 316 338 char buf[256]; 317 339 318 /* reset file pointer*/319 fp.seek(0, SEEK_SET);320 321 340 /* initially mark offsets as not used*/ 322 341 for (i = 0; i < pf->size; ++i) 323 342 pf->offset[i] = (unsigned long)-1; … … 371 390 continue; 372 391 } 373 392 pf->offset[encoding-pf->firstchar] = ofs; 393 pf->width[encoding-pf->firstchar] = width; 374 394 375 /* calc char width*/ 376 if (bbx < 0) { 377 width -= bbx; 378 /*if (width > maxwidth) 379 width = maxwidth;*/ 380 bbx = 0; 381 } 395 pf->bbx[encoding-pf->firstchar].w = bbw; 396 pf->bbx[encoding-pf->firstchar].h = bbh; 397 pf->bbx[encoding-pf->firstchar].x = bbx; 398 pf->bbx[encoding-pf->firstchar].y = bby; 399 382 400 if (width > maxwidth) 383 401 maxwidth = width; 384 pf->width[encoding-pf->firstchar] = width;385 402 386 403 /* clear bitmap*/ 387 memset(ch_bitmap, 0, BITMAP_BYTES( width) * pf->height);404 memset(ch_bitmap, 0, BITMAP_BYTES(bbw) * bbh); 388 405 389 ch_words = BITMAP_WORDS(width); 390 #define BM(row,col) (*(ch_bitmap + ((row)*ch_words) + (col))) 391 #define BITMAP_NIBBLES (BITMAP_BITSPERIMAGE/4) 406 ch_words = BITMAP_WORDS(bbw); 392 407 393 408 /* read bitmaps*/ 394 for (i = 0; ; ++i) { 395 int hexnibbles; 396 409 for (i = 0; i < bbh; ++i) { 397 410 if (!bdf_getline(fp, buf, sizeof(buf))) { 398 411 fprintf(stderr, "Error: EOF reading BITMAP data\n"); 399 412 return 0; … … 401 414 if (isprefix(buf, "ENDCHAR")) 402 415 break; 403 416 404 hexnibbles = strlen(buf);405 417 for (k = 0; k < ch_words; ++k) { 406 int ndx = k * BITMAP_NIBBLES;407 uint padnibbles = hexnibbles - ndx;408 418 bitmap_t value; 409 419 410 if (padnibbles <= 0) 411 break; 412 if (padnibbles >= BITMAP_NIBBLES) 413 padnibbles = 0; 414 415 value = bdf_hexval((unsigned char *)buf, 416 ndx, ndx+BITMAP_NIBBLES-1-padnibbles); 417 value <<= padnibbles * BITMAP_NIBBLES; 418 419 BM(pf->height - pf->descent - bby - bbh + i, k) |= 420 value >> bbx; 421 /* handle overflow into next image word*/ 422 if (bbx) { 423 BM(pf->height - pf->descent - bby - bbh + i, k+1) = 424 value << (BITMAP_BITSPERIMAGE - bbx); 420 value = bdf_hexval((unsigned char *)buf); 421 if (bbw > 8) { 422 WRITE_UINT16(ch_bitmap, value); 423 } else { 424 WRITE_UINT16(ch_bitmap, value << 8); 425 425 } 426 ch_bitmap++; 426 427 } 427 428 } 428 429 429 ofs += BITMAP_WORDS(width) * pf->height; 430 430 ofs += BITMAP_WORDS(bbw) * bbh; 431 431 continue; 432 432 } 433 433 if (strequal(buf, "ENDFONT")) … … 444 444 if (pf->offset[i] == (unsigned long)-1) { 445 445 pf->offset[i] = pf->offset[defchar]; 446 446 pf->width[i] = pf->width[defchar]; 447 pf->bbx[i].w = pf->bbx[defchar].w; 448 pf->bbx[i].h = pf->bbx[defchar].h; 449 pf->bbx[i].x = pf->bbx[defchar].x; 450 pf->bbx[i].y = pf->bbx[defchar].y; 447 451 } 448 452 } 449 453 … … 454 458 encodetable = 1; 455 459 break; 456 460 } 457 l += BITMAP_WORDS(pf-> width[i]) * pf->height;461 l += BITMAP_WORDS(pf->bbx[i].w) * pf->bbx[i].h; 458 462 } 459 463 if (!encodetable) { 460 464 free(pf->offset); … … 473 477 pf->width = NULL; 474 478 } 475 479 480 /* determine if the font needs a bbx table */ 481 for (i = 0; i < pf->size; ++i) { 482 if (pf->bbx[i].w != pf->fbbw || pf->bbx[i].h != pf->fbbh || pf->bbx[i].x != pf->fbbx || pf->bbx[i].y != pf->fbby) { 483 need_bbx = 1; 484 break; 485 } 486 } 487 if (!need_bbx) { 488 free(pf->bbx); 489 pf->bbx = NULL; 490 } 491 476 492 /* reallocate bits array to actual bits used*/ 477 493 if (ofs < pf->bits_size) { 478 494 pf->bits = (bitmap_t *)realloc(pf->bits, ofs * sizeof(bitmap_t)); … … 518 534 return buf; 519 535 } 520 536 521 /* return hex value of portion of buffer*/522 bitmap_t bdf_hexval(unsigned char *buf , int ndx1, int ndx2) {537 /* return hex value of buffer */ 538 bitmap_t bdf_hexval(unsigned char *buf) { 523 539 bitmap_t val = 0; 524 int i, c;525 540 526 for (i = ndx1; i <= ndx2; ++i) { 527 c = buf[i]; 541 for (unsigned char *ptr = buf; *ptr; ptr++) { 542 int c = *ptr; 543 528 544 if (c >= '0' && c <= '9') 529 545 c -= '0'; 546 else if (c >= 'A' && c <= 'F') 547 c = c - 'A' + 10; 548 else if (c >= 'a' && c <= 'f') 549 c = c - 'a' + 10; 530 550 else 531 if (c >= 'A' && c <= 'F') 532 c = c - 'A' + 10; 533 else 534 if (c >= 'a' && c <= 'f') 535 c = c - 'a' + 10; 536 else 537 c = 0; 551 c = 0; 538 552 val = (val << 4) | c; 539 553 } 540 554 return val; … … 549 563 desc.name = data->name; 550 564 desc.maxwidth = data->maxwidth; 551 565 desc.height = data->height; 566 desc.fbbw = data->fbbw; 567 desc.fbbh = data->fbbh; 568 desc.fbbx = data->fbbx; 569 desc.fbby = data->fbby; 552 570 desc.ascent = data->ascent; 553 571 desc.firstchar = data->firstchar; 554 572 desc.size = data->size; 555 573 desc.bits = data->bits; 556 574 desc.offset = data->offset; 557 575 desc.width = data->width; 576 desc.bbx = data->bbx; 558 577 desc.defaultchar = data->defaultchar; 559 578 desc.bits_size = data->bits_size; 560 579 … … 570 589 571 590 cacheFile.writeUint16BE(font.desc.maxwidth); 572 591 cacheFile.writeUint16BE(font.desc.height); 592 cacheFile.writeUint16BE(font.desc.fbbw); 593 cacheFile.writeUint16BE(font.desc.fbbh); 594 cacheFile.writeUint16BE(font.desc.fbbx); 595 cacheFile.writeUint16BE(font.desc.fbby); 573 596 cacheFile.writeUint16BE(font.desc.ascent); 574 597 cacheFile.writeUint16BE(font.desc.firstchar); 575 598 cacheFile.writeUint16BE(font.desc.size); … … 598 621 cacheFile.writeByte(0); 599 622 } 600 623 624 if (font.desc.bbx) { 625 cacheFile.writeByte(1); 626 for (int i = 0; i < font.desc.size; ++i) { 627 cacheFile.writeByte(font.desc.bbx[i].w); 628 cacheFile.writeByte(font.desc.bbx[i].h); 629 cacheFile.writeByte(font.desc.bbx[i].x); 630 cacheFile.writeByte(font.desc.bbx[i].y); 631 } 632 } else { 633 cacheFile.writeByte(0); 634 } 635 601 636 return !cacheFile.ioFailed(); 602 637 } 603 638 … … 605 640 NewFont *font = 0; 606 641 607 642 NewFontData *data = (NewFontData *)malloc(sizeof(NewFontData)); 608 if (!data) return 0; 643 if (!data) 644 return 0; 609 645 610 646 memset(data, 0, sizeof(NewFontData)); 611 647 612 648 data->maxwidth = stream.readUint16BE(); 613 649 data->height = stream.readUint16BE(); 650 data->fbbw = stream.readUint16BE(); 651 data->fbbh = stream.readUint16BE(); 652 data->fbbx = stream.readUint16BE(); 653 data->fbby = stream.readUint16BE(); 614 654 data->ascent = stream.readUint16BE(); 615 655 data->firstchar = stream.readUint16BE(); 616 656 data->size = stream.readUint16BE(); … … 656 696 } 657 697 } 658 698 699 bool hasBBXTable = (stream.readByte() != 0); 700 if (hasBBXTable) { 701 data->bbx = (BBX *)malloc(sizeof(BBX)*data->size); 702 if (!data->bbx) { 703 free(data->bits); 704 free(data->offset); 705 free(data->width); 706 free(data); 707 return 0; 708 } 709 710 for (int i = 0; i < data->size; ++i) { 711 data->bbx[i].w = stream.readByte(); 712 data->bbx[i].h = stream.readByte(); 713 data->bbx[i].x = stream.readByte(); 714 data->bbx[i].y = stream.readByte(); 715 } 716 } 717 659 718 FontDesc desc; 660 719 desc.name = data->name; 661 720 desc.maxwidth = data->maxwidth; 662 721 desc.height = data->height; 722 desc.fbbw = data->fbbw; 723 desc.fbbh = data->fbbh; 724 desc.fbbx = data->fbbx; 725 desc.fbby = data->fbby; 663 726 desc.ascent = data->ascent; 664 727 desc.firstchar = data->firstchar; 665 728 desc.size = data->size; 666 729 desc.bits = data->bits; 667 730 desc.offset = data->offset; 668 731 desc.width = data->width; 732 desc.bbx = data->bbx; 669 733 desc.defaultchar = data->defaultchar; 670 734 desc.bits_size = data->bits_size; 671 735 … … 843 907 } 844 908 } 845 909 846 847 910 tmpWidth += w; 848 911 tmpStr += c; 849 912 } -
graphics/font.h
87 87 virtual void drawChar(Surface *dst, byte chr, int x, int y, uint32 color) const; 88 88 }; 89 89 90 typedef unsigned short bitmap_t; /* bitmap image unit size*/ 90 91 92 struct BBX { 93 int w; 94 int h; 95 int x; 96 int y; 97 }; 91 98 92 typedef unsigned short bitmap_t; /* bitmap image unit size*/93 94 99 /* builtin C-based proportional/fixed font structure */ 95 100 /* based on The Microwindows Project http://microwindows.org */ 96 101 struct FontDesc { 97 102 const char * name; /* font name*/ 98 103 int maxwidth; /* max width in pixels*/ 99 104 int height; /* height in pixels*/ 105 int fbbw, fbbh, fbbx, fbby; /* max bounding box */ 100 106 int ascent; /* ascent (baseline) height*/ 101 107 int firstchar; /* first character in bitmap*/ 102 108 int size; /* font size in glyphs*/ 103 109 const bitmap_t* bits; /* 16-bit right-padded bitmap data*/ 104 110 const unsigned long* offset; /* offsets into bitmap data*/ 105 111 const unsigned char* width; /* character widths or NULL if fixed*/ 112 const BBX* bbx; /* character bounding box or NULL if fixed */ 106 113 int defaultchar; /* default char (not glyph index)*/ 107 114 long bits_size; /* # words of bitmap_t bits*/ 108 115 };