📄 convbdf.c
字号:
if (isprefix(buf, "DWIDTH ")) { if (sscanf(buf, "DWIDTH %d", &width) != 1) { fprintf(stderr, "Error: bad 'DWIDTH'\n"); return 0; } /* use font boundingbox width if DWIDTH <= 0*/ if (width <= 0) width = pf->fbbw - pf->fbbx; continue; } if (isprefix(buf, "BBX ")) { if (sscanf(buf, "BBX %d %d %d %d", &bbw, &bbh, &bbx, &bby) != 4) { fprintf(stderr, "Error: bad 'BBX'\n"); return 0; } continue; } if (strequal(buf, "BITMAP")) { bitmap_t *ch_bitmap = pf->bits + ofs; int ch_words; if (encoding < 0) continue; /* set bits offset in encode map*/ if (pf->offset[encoding-pf->firstchar] != (unsigned long)-1) { fprintf(stderr, "Error: duplicate encoding for character %d (0x%02x), ignoring duplicate\n", encoding, encoding); continue; } pf->offset[encoding-pf->firstchar] = ofs; /* calc char width*/ if (bbx < 0) { width -= bbx; /*if (width > maxwidth) width = maxwidth;*/ bbx = 0; } if (width > maxwidth) maxwidth = width; pf->width[encoding-pf->firstchar] = width; /* clear bitmap*/ memset(ch_bitmap, 0, BITMAP_BYTES(width) * pf->height); ch_words = BITMAP_WORDS(width);#define BM(row,col) (*(ch_bitmap + ((row)*ch_words) + (col)))#define BITMAP_NIBBLES (BITMAP_BITSPERIMAGE/4) /* read bitmaps*/ for (i=0; ; ++i) { int hexnibbles; if (!bdf_getline(fp, buf, sizeof(buf))) { fprintf(stderr, "Error: EOF reading BITMAP data\n"); return 0; } if (isprefix(buf, "ENDCHAR")) break; hexnibbles = strlen(buf); for (k=0; k<ch_words; ++k) { int ndx = k * BITMAP_NIBBLES; int padnibbles = hexnibbles - ndx; bitmap_t value; if (padnibbles <= 0) break; if (padnibbles >= BITMAP_NIBBLES) padnibbles = 0; value = bdf_hexval((unsigned char *)buf, ndx, ndx+BITMAP_NIBBLES-1-padnibbles); value <<= padnibbles * BITMAP_NIBBLES; BM(pf->height - pf->descent - bby - bbh + i, k) |= value >> bbx; /* handle overflow into next image word*/ if (bbx) { BM(pf->height - pf->descent - bby - bbh + i, k+1) = value << (BITMAP_BITSPERIMAGE - bbx); } } } ofs += BITMAP_WORDS(width) * pf->height; continue; } if (strequal(buf, "ENDFONT")) break; } /* set max width*/ pf->maxwidth = maxwidth; /* change unused offset/width values to default char values*/ for (i=0; i<pf->size; ++i) { int defchar = pf->defaultchar - pf->firstchar; if (pf->offset[i] == (unsigned long)-1) { pf->offset[i] = pf->offset[defchar]; pf->width[i] = pf->width[defchar]; } } /* determine whether font doesn't require encode table*/ l = 0; for (i=0; i<pf->size; ++i) { if (pf->offset[i] != l) { encodetable = 1; break; } l += BITMAP_WORDS(pf->width[i]) * pf->height; } if (!encodetable) { free(pf->offset); pf->offset = NULL; } /* determine whether font is fixed-width*/ for (i=0; i<pf->size; ++i) { if (pf->width[i] != maxwidth) { proportional = 1; break; } } if (!proportional) { free(pf->width); pf->width = NULL; } /* reallocate bits array to actual bits used*/ if (ofs < pf->bits_size) { pf->bits = realloc(pf->bits, ofs * sizeof(bitmap_t)); pf->bits_size = ofs; } else { if (ofs > pf->bits_size) { fprintf(stderr, "Warning: DWIDTH spec > max FONTBOUNDINGBOX\n"); if (ofs > pf->bits_size+EXTRA) { fprintf(stderr, "Error: Not enough bits initially allocated\n"); return 0; } pf->bits_size = ofs; } } return 1;}/* read the next non-comment line, returns buf or NULL if EOF*/char *bdf_getline(FILE *fp, char *buf, int len){ int c; char *b; for (;;) { b = buf; while ((c = getc(fp)) != EOF) { if (c == '\r') continue; if (c == '\n') break; if (b - buf >= (len - 1)) break; *b++ = c; } *b = '\0'; if (c == EOF && b == buf) return NULL; if (b != buf && !isprefix(buf, "COMMENT")) break; } return buf;}/* return hex value of portion of buffer*/bitmap_t bdf_hexval(unsigned char *buf, int ndx1, int ndx2){ bitmap_t val = 0; int i, c; for (i=ndx1; i<=ndx2; ++i) { c = buf[i]; if (c >= '0' && c <= '9') c -= '0'; else if (c >= 'A' && c <= 'F') c = c - 'A' + 10; else if (c >= 'a' && c <= 'f') c = c - 'a' + 10; else c = 0; val = (val << 4) | c; } return val;}/* generate C source from in-core font*/int gen_c_source(struct font* pf, char *path){ FILE *ofp; int i; int did_defaultchar = 0; int did_syncmsg = 0; time_t t = time(0); bitmap_t *ofs = pf->bits; char buf[256]; char obuf[256]; char hdr1[] = { "/* Generated by convbdf on %s. */\n" "#include \"font.h\"\n" "\n" "/* Font information:\n" " name: %s\n" " facename: %s\n" " w x h: %dx%d\n" " size: %d\n" " ascent: %d\n" " descent: %d\n" " first char: %d (0x%02x)\n" " last char: %d (0x%02x)\n" " default char: %d (0x%02x)\n" " proportional: %s\n" " %s\n" "*/\n" "\n" "/* Font character bitmap data. */\n" "static bitmap_t _font_bits[] = {\n" }; ofp = fopen(path, "w"); if (!ofp) { fprintf(stderr, "Can't create %s\n", path); return 1; } strcpy(buf, ctime(&t)); buf[strlen(buf)-1] = 0; fprintf(ofp, hdr1, buf, pf->name, pf->facename? pf->facename: "", pf->maxwidth, pf->height, pf->size, pf->ascent, pf->descent, pf->firstchar, pf->firstchar, pf->firstchar+pf->size-1, pf->firstchar+pf->size-1, pf->defaultchar, pf->defaultchar, pf->width? "yes": "no", pf->copyright? pf->copyright: ""); /* generate bitmaps*/ for (i=0; i<pf->size; ++i) { int x; int bitcount = 0; int width = pf->width ? pf->width[i] : pf->maxwidth; int height = pf->height; bitmap_t *bits = pf->bits + (pf->offset? pf->offset[i]: (height * i)); bitmap_t bitvalue; /* * Generate bitmap bits only if not this index isn't * the default character in encode map, or the default * character hasn't been generated yet. */ if (pf->offset && (pf->offset[i] == pf->offset[pf->defaultchar-pf->firstchar])) { if (did_defaultchar) continue; did_defaultchar = 1; } fprintf(ofp, "\n/* Character %d (0x%02x):\n width %d", i+pf->firstchar, i+pf->firstchar, width); if (gen_map) { fprintf(ofp, "\n +"); for (x=0; x<width; ++x) fprintf(ofp, "-"); fprintf(ofp, "+\n"); x = 0; while (height > 0) { if (x == 0) fprintf(ofp, " |"); if (bitcount <= 0) { bitcount = BITMAP_BITSPERIMAGE; bitvalue = *bits++; } fprintf(ofp, BITMAP_TESTBIT(bitvalue)? "*": " "); bitvalue = BITMAP_SHIFTBIT(bitvalue); --bitcount; if (++x == width) { fprintf(ofp, "|\n"); --height; x = 0; bitcount = 0; } } fprintf(ofp, " +"); for (x=0; x<width; ++x) fprintf(ofp, "-"); fprintf(ofp, "+ */\n"); } else fprintf(ofp, " */\n"); bits = pf->bits + (pf->offset? pf->offset[i]: (pf->height * i)); for (x=BITMAP_WORDS(width)*pf->height; x>0; --x) { fprintf(ofp, "0x%04x,\n", *bits); if (!did_syncmsg && *bits++ != *ofs++) { fprintf(stderr, "Warning: found encoding values in non-sorted order (not an error).\n"); did_syncmsg = 1; } } } fprintf(ofp, "};\n\n"); if (pf->offset) { /* output offset table*/ fprintf(ofp, "/* Character->glyph mapping. */\n" "static unsigned long _sysfont_offset[] = {\n"); for (i=0; i<pf->size; ++i) fprintf(ofp, " %ld,\t/* (0x%02x) */\n", pf->offset[i], i+pf->firstchar); fprintf(ofp, "};\n\n"); } /* output width table for proportional fonts*/ if (pf->width) { fprintf(ofp, "/* Character width data. */\n" "static unsigned char _sysfont_width[] = {\n"); for (i=0; i<pf->size; ++i) fprintf(ofp, " %d,\t/* (0x%02x) */\n", pf->width[i], i+pf->firstchar); fprintf(ofp, "};\n\n"); } /* output struct font struct*/ if (pf->offset) sprintf(obuf, "_sysfont_offset,"); else sprintf(obuf, "0, /* no encode table*/"); if (pf->width) sprintf(buf, "_sysfont_width,"); else sprintf(buf, "0, /* fixed width*/"); fprintf(ofp, "/* Exported structure definition. */\n" "struct font sysfont = {\n" " \"%s\",\n" " %d,\n" " %d,\n" " %d,\n" " %d,\n" " %d,\n" " _font_bits,\n" " %s\n" " %s\n" " %d,\n" " sizeof(_font_bits)/sizeof(bitmap_t),\n" "};\n", pf->name, pf->maxwidth, pf->height, pf->ascent, pf->firstchar, pf->size, obuf, buf, pf->defaultchar); return 0;}static int writebyte(FILE *fp, unsigned char c){ return putc(c, fp) != EOF;}static int writeshort(FILE *fp, unsigned short s){ putc(s, fp); return putc(s>>8, fp) != EOF;}static int writelong(FILE *fp, unsigned long l){ putc(l, fp); putc(l>>8, fp); putc(l>>16, fp); return putc(l>>24, fp) != EOF;}static int writestr(FILE *fp, char *str, int count){ return fwrite(str, 1, count, fp) == count;}static int writestrpad(FILE *fp, char *str, int totlen){ int ret; while (str && *str && totlen > 0) { if (*str) { ret = putc(*str++, fp); --totlen; } } while (--totlen >= 0) ret = putc(' ', fp); return ret;}/* generate .fnt format file from in-core font*/int gen_fnt_file(struct font* pf, char *path){ FILE *ofp; int i; ofp = fopen(path, "wb"); if (!ofp) { fprintf(stderr, "Can't create %s\n", path); return 1; } /* write magic and version #*/ writestr(ofp, VERSION, 4); /* internal font name*/ writestrpad(ofp, pf->name, 64); /* copyright*/ writestrpad(ofp, pf->copyright, 256); /* font info*/ writeshort(ofp, pf->maxwidth); writeshort(ofp, pf->height); writeshort(ofp, pf->ascent); writeshort(ofp, 0); writelong(ofp, pf->firstchar); writelong(ofp, pf->defaultchar); writelong(ofp, pf->size); /* variable font data sizes*/ writelong(ofp, pf->bits_size); /* # words of bitmap_t*/ writelong(ofp, pf->offset? pf->size: 0); /* # longs of offset*/ writelong(ofp, pf->width? pf->size: 0); /* # bytes of width*/ /* variable font data*/ for (i=0; i<pf->bits_size; ++i) writeshort(ofp, pf->bits[i]); if (ftell(ofp) & 2) writeshort(ofp, 0); /* pad to 32-bit boundary*/ if (pf->offset) for (i=0; i<pf->size; ++i) writelong(ofp, pf->offset[i]); if (pf->width) for (i=0; i<pf->size; ++i) writebyte(ofp, pf->width[i]); fclose(ofp); return 0;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -