// code by SASANO Takayoshi, CC BY-NC-SA #include #include #include #include #include #define isInvalidSJIS(x) \ ( (((x) & 0x00ff) < 0x0040) || ((x) < 0x8000) || \ (((x) >= 0xa000) && ((x) < 0xe000)) ) /* FONTX2 header */ #define CODETYPE_ASCII 0 #define CODETYPE_KANJI 1 /* common */ typedef struct { unsigned char identifier[6]; unsigned char fontname[8]; unsigned char xsize; unsigned char ysize; unsigned char codetype; } __attribute__((packed)) FONTX2_HEADER; /* ascii */ typedef struct { FONTX2_HEADER h; unsigned char d[1]; } __attribute__((packed)) FONTX2_ASCII; /* sjis */ typedef struct { FONTX2_HEADER h; unsigned char tnum; unsigned char d[1]; } __attribute__((packed)) FONTX2_KANJI; typedef struct { unsigned short start; unsigned short end; } __attribute__((packed)) FONTX2_CODETABLE; static uint8_t OutBuf[4176]; static uint8_t *Buf; static size_t BufSize; int loadfile(char *filename) { FILE *fp; int result; fp = fopen(filename, "r"); if (fp == NULL) { fprintf(stderr, "loadfile: %s open error\n", filename); result = -1; goto fin0; } fseek(fp, 0, SEEK_END); BufSize = ftell(fp); fseek(fp, 0, SEEK_SET); Buf = calloc(BufSize, 2); // XXX if (Buf == NULL) { fprintf(stderr, "loadfile: memory allocation error\n"); result = -1; goto fin1; } fread(Buf, BufSize, 1, fp); result = 0; fin1: fclose(fp); fin0: return result; } int savefile(char *filename, int filesize) { int result; FILE *fp; fp = fopen(filename, "w"); if (fp == NULL) { fprintf(fp, "savefile: %s open error\n", filename); result = -1; goto fin0; } fwrite(OutBuf, filesize, 1, fp); result = 0; fclose(fp); fin0: return result; } static void rotate(uint32_t *out, uint8_t *font) { int i, j; uint32_t tmp[8]; memset(tmp, ~0, sizeof(tmp)); for (i = 0; i < 6; i++) { for (j = 0; j < 12; j++) { if (font[j] & (0x80 >> i)) tmp[6 - i] ^= (1 << j); } } for (i = 0; i < 8; i++) { out[i] = htobe32(0x003f003f | ((tmp[i] & 0x00ff) << 8) | ((tmp[i] & 0xff00) << 16)); } } static void convert(uint8_t *font) { int i; uint32_t tmp[8]; uint32_t *p = (uint32_t *)&OutBuf[4]; for (i = 0x20; i < 0x80; i++) { rotate(tmp, font + (i * 12)); memcpy(p, tmp, sizeof(tmp)); p += 8; } } int main(int argc, char *argv[]) { FONTX2_HEADER *h; if (argc < 3) { fprintf(stderr, "usage: %s [infile] [outfile]\n", argv[0]); goto fin0; } if (loadfile(argv[1]) < 0) { goto fin0; } h = (FONTX2_HEADER *)Buf; if (h->xsize != 6 || h->ysize != 12 || h->codetype != CODETYPE_ASCII) { fprintf(stderr, "unsupported format\n"); goto fin1; } memset(OutBuf, ~0, sizeof(OutBuf)); // clear all pixel convert(((FONTX2_ASCII *)Buf)->d); savefile(argv[2], sizeof(OutBuf)); fin1: free(Buf); fin0: return 0; }