#include #include #include using namespace std; typedef struct { unsigned int code; unsigned char buf[256]; } SLetter; typedef struct { int start, end; } STable; #define ENCODING_STR "ENCODING " #define BOUNDING_STR "FONTBOUNDINGBOX " #define BBX_STR "BBX " #define BITMAP_STR "BITMAP" #define ENDCHAR_STR "ENDCHAR" unsigned int jis2sjis(unsigned int jis); vector data; vector table; void main(int argc, char **argv) { if(argc < 3) { printf("bdf2fnt \n"); return; } FILE *Fin = fopen(argv[1], "rt"); int status = -1; SLetter let; int width, height; int bufPtr = 0; STable entry; entry.start = -1; entry.end = -1; while(true) { // 1 行読み込み char buf[65536]; if((fgets(buf, 65535, Fin)) == NULL) break; switch(status) { case -1: // 初期化 if(strncmp(buf, BOUNDING_STR, strlen(BOUNDING_STR)) == 0) { if(buf[strlen(buf)-1] == '\n') buf[strlen(buf)-1] = 0; int n, m; sscanf(&buf[strlen(BOUNDING_STR)], "%d %d %d %d", &width, &height, &n, &m); printf("%dx%d\n", width, height); status ++; } break; case 0: // 初期状態 if(strncmp(buf, ENCODING_STR, strlen(ENCODING_STR)) == 0) { if(buf[strlen(buf)-1] == '\n') buf[strlen(buf)-1] = 0; let.code = atoi(&buf[strlen(ENCODING_STR)]); let.code = jis2sjis(let.code); if(let.code - entry.end != 1) { // 連続でない if(entry.start != -1) { STable *ptr = new STable; *ptr = entry; table.push_back(ptr); } entry.end = entry.start = let.code; } entry.end = let.code; printf("%x \r", let.code); fflush(stdout); bufPtr = 0; status ++; } break; case 1: // 文字コード決定、BBX 確認 if(strncmp(buf, BBX_STR, strlen(BBX_STR)) == 0) { if(buf[strlen(buf)-1] == '\n') buf[strlen(buf)-1] = 0; int n, m, o, p; sscanf(&buf[strlen(BBX_STR)], "%d %d %d %d", &m, &n, &o, &p); if(m != width || n != height) { printf("\nerror : %d\n", let.code); let.code = -1; } status ++; } break; case 2: // データ開始待ち if(strncmp(buf, BITMAP_STR, strlen(BITMAP_STR)) == 0) status ++; break; case 3: // データ書き込み if(strncmp(buf, ENDCHAR_STR, strlen(ENDCHAR_STR)) == 0) { SLetter *ptr = new SLetter; *ptr = let; data.push_back(ptr); status = 0; } else { for(int i = 0; i < (width +7)/8; i ++) { int n = ((unsigned char)buf[i*2+0]); n = (n >= '0' && n <= '9') ? n - '0' : n - 'a' +10; int m = ((unsigned char)buf[i*2+1]); m = (m >= '0' && m <= '9') ? m - '0' : m - 'a' +10; let.buf[bufPtr ++] = n*16 + m; } } break; } } STable *ptr = new STable; *ptr = entry; table.push_back(ptr); fclose(Fin); printf("\ntable:%d\n", table.size( )); char head[18]; sprintf(head, "FONTX2CONVERT."); head[14] = width; head[15] = height; head[16] = 1; head[17] = table.size( ); FILE *Fout = fopen(argv[2], "wb"); fwrite(head, 18, 1, Fout); for(int i = 0; i < table.size( ); i ++) { char entry[4]; entry[0] = table[i]->start % 256; entry[1] = (table[i]->start /256) % 256; entry[2] = table[i]->end % 256; entry[3] = (table[i]->end /256) % 256; fwrite(entry, 4, 1, Fout); } for(i = 0; i < data.size( ); i ++) { fwrite(&data[i]->buf, (width+7)/8*height, 1, Fout); } fclose(Fout); } unsigned int jis2sjis(unsigned int jis) { unsigned int hib, lob; hib = (jis >> 8) & 0xff; lob = jis & 0xff; lob += (hib & 1) ? 0x1f : 0x7d; if (lob >= 0x7f) lob++; hib = ((hib - 0x21) >> 1) + 0x81; if (hib > 0x9f) hib += 0x40; return (hib << 8) | lob; }