/* txt30fontx.c code: SASANO Takayoshi ---- public domain, no warranty. [charset=ISO-2022-JP] ●とりあえず 説明文書くの面倒だから、省略。 …ソースコード読んで、頑張れ。 */ #include #include #include /* 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 FONTX2_CODETABLE CodeTable_SJIS[] = { {0x8140, 0x817e}, {0x8180, 0x81ac}, /* symbol */ {0x824f, 0x8258}, /* number */ {0x8260, 0x8279}, /* alphabet (large) */ {0x8281, 0x829a}, /* alphabet (small) */ {0x829f, 0x82f1}, /* hiragana */ {0x8340, 0x837e}, {0x8380, 0x8396}, /* katakana */ {0x839f, 0x83b6}, /* greek (large) */ {0x83bf, 0x83d6}, /* greek (small) */ {0x8440, 0x8460}, /* russian (large) */ {0x8470, 0x847e}, {0x8480, 0x8491}, /* russian (small) */ {0x889f, 0x88fc}, /* kanji (JIS1) */ {0x8940, 0x897e}, {0x8980, 0x89fc}, {0x8a40, 0x8a7e}, {0x8a80, 0x8afc}, {0x8b40, 0x8b7e}, {0x8b80, 0x8bfc}, {0x8c40, 0x8c7e}, {0x8c80, 0x8cfc}, {0x8d40, 0x8d7e}, {0x8d80, 0x8dfc}, {0x8e40, 0x8e7e}, {0x8e80, 0x8efc}, {0x8f40, 0x8f7e}, {0x8f80, 0x8ffc}, {0x9040, 0x907e}, {0x9080, 0x90fc}, {0x9140, 0x917e}, {0x9180, 0x91fc}, {0x9240, 0x927e}, {0x9280, 0x92fc}, {0x9340, 0x937e}, {0x9380, 0x93fc}, {0x9440, 0x947e}, {0x9480, 0x94fc}, {0x9540, 0x957e}, {0x9580, 0x95fc}, {0x9640, 0x967e}, {0x9680, 0x96fc}, {0x9740, 0x977e}, {0x9780, 0x97fc}, {0x9840, 0x9872}, }; /* data buffer (in/out) */ #define BUFFERSIZE (2048 * 1024) static char InData[BUFFERSIZE]; static char OutData[BUFFERSIZE]; static char FontName[] = "TXT30"; /* fetch X */ static void fetchX(unsigned char *in, unsigned long *out, int index) { int i; for (i = 0; i < 24; i++) { out[i] = (in[index * 0x30 + i * 2 + 0] << 8) | (in[index * 0x30 + i * 2 + 1] << 0); } return; } /* SJIS -> JIS */ static int _mbcjmstojis(int jms) { int low, high; high = (jms >> 8) & 0xff; low = jms & 0xff; if (low < 0x9f) { if (high < 0xa0) { high -= 0x81; high *= 2; high += 0x21; } else { high -= 0xe0; high *= 2; high += 0x5f; } if (low > 0x7f) low--; low -= 0x1f; } else { if (high < 0xa0) { high -= 0x81; high *= 2; high += 0x22; } else { high -= 0xe0; high *= 2; high += 0x60; } low -= 0x7e; } return (high << 8) | low; } /* get kanji font index */ static int get_kanji_index(int sjis) { int kuten, ku, ten, idx; kuten = _mbcjmstojis(sjis) - 0x2020; ku = (kuten >> 8) & 0xff; ten = kuten & 0xff; idx = ku * 96 + ten + 1536; return idx; } /* copy CodeTable */ static char *copy_table(char *dst, FONTX2_CODETABLE *src, int entry) { int i; /* table must be little-endian */ for (i = 0; i < entry; i++) { *dst++ = src[i].start & 0xff; *dst++ = (src[i].start >> 8) & 0xff; *dst++ = src[i].end & 0xff; *dst++ = (src[i].end >> 8) & 0xff; } return dst; } /* convert (Kanji) */ static void convert_kanji(void) { int i, j, tnum, sjis, chr; unsigned char *d; FILE *fp; FONTX2_KANJI *f; unsigned long t[24]; memset(OutData, 0, sizeof(OutData)); f = (FONTX2_KANJI *)OutData; tnum = sizeof(CodeTable_SJIS) / sizeof(FONTX2_CODETABLE); /* create header */ strncpy(f->h.identifier, "FONTX2", sizeof(f->h.identifier)); strncpy(f->h.fontname, FontName, sizeof(f->h.fontname)); for (i = 0; i < sizeof(f->h.fontname); i++) { if (f->h.fontname[i] < 0x20) f->h.fontname[i] = 0x20; } f->h.xsize = 18; f->h.ysize = 18; f->h.codetype = CODETYPE_KANJI; f->tnum = tnum; /* make CodeTable */ d = f->d; d = copy_table(d, CodeTable_SJIS, tnum); chr = 0; /* convert font */ for (i = 0; i < tnum; i++) { for (sjis = CodeTable_SJIS[i].start; sjis <= CodeTable_SJIS[i].end; sjis++, chr++) { fetchX(InData, t, get_kanji_index(sjis)); for (j = 0; j < 18; j++) { t[j + 3] <<= 7; *d++ = (t[j + 3] >> 16) & 0x7f; *d++ = (t[j + 3] >> 8) & 0xff; *d++ = (t[j + 3] >> 0) & 0x80; } } } /* save result */ fp = fopen("jpnzn18x.t30", "wb"); if (fp != NULL) { fwrite(OutData, 1, (sizeof(FONTX2_HEADER) + tnum * sizeof(FONTX2_CODETABLE) + 54 * chr + 1), fp); } fclose(fp); return; } /* convert (ASCII) */ static void convert_ascii(void) { int i, j; unsigned char *d; FILE *fp; FONTX2_ASCII *f; unsigned long t[24]; memset(OutData, 0, sizeof(OutData)); f = (FONTX2_ASCII *)OutData; /* create header */ strncpy(f->h.identifier, "FONTX2", sizeof(f->h.identifier)); strncpy(f->h.fontname, FontName, sizeof(f->h.fontname)); for (i = 0; i < sizeof(f->h.fontname); i++) { if (f->h.fontname[i] < 0x20) f->h.fontname[i] = 0x20; } f->h.xsize = 18 / 2; f->h.ysize = 18; f->h.codetype = CODETYPE_ASCII; /* converrt font (ASCII, 0x20-0x7e) */ for (i = 0x020; i < 0x7f; i++) { fetchX(InData, t, i - 0x20 + 864); for (d = &f->d[36 * i], j = 0; j < 18; j++) { t[j + 3] <<= 7; *d++ = (t[j + 3] >> 16) & 0x7f; *d++ = (t[j + 3] >> 8) & 0x80; } } /* convert font (Kana, 0xa0-0xdf) */ for (i = 0x0a0; i < 0xe0; i++) { fetchX(InData, t, i - 0xa0 + 960); for (d = &f->d[36 * i], j = 0; j < 18; j++) { t[j + 3] <<= 7; *d++ = (t[j + 3] >> 16) & 0x7f; *d++ = (t[j + 3] >> 8) & 0x80; } } /* save result */ fp = fopen("jpnhn18x.t30", "wp"); if (fp != NULL) { fwrite(OutData, 1, sizeof(FONTX2_HEADER) + 256 * 36, fp); } fclose(fp); return; } /* entry */ int main(int argc, char *argv[]) { int result; FILE *fp; fp = fopen("txt30-cg.bin", "rb"); if (fp == NULL) { printf("file not found\n"); result = EXIT_FAILURE; goto fin0; } memset(InData, ~0, sizeof(InData)); fread(InData, 1, sizeof(InData), fp); convert_ascii(); convert_kanji(); result = EXIT_SUCCESS; //fin1: fclose(fp); fin0: return result; }