--- nono/util/mkcgrom/mkcgrom.cpp 2026/04/29 17:04:54 1.1.1.3 +++ nono/util/mkcgrom/mkcgrom.cpp 2026/04/29 17:05:16 1.1.1.4 @@ -1,23 +1,24 @@ // // CGROM.DAT 生成 // -// Copyright (C) 2012-2019 isaki@NetBSD.org +// Copyright (C) 2012-2022 isaki@NetBSD.org // +#include "header.h" #include -#include +#include +#include +#include #include -#include "wxheader.h" -PRAGMA_PUSH_WARNINGS -#include -PRAGMA_POP_WARNINGS -#include - -#define countof(x) (sizeof(x) / sizeof(x[0])) -#define ROUND(x, y) (((x) + (y - 1)) / (y)) -#define ROUND8(x) ROUND(x, 8) -/* フォント種別 */ +#define HOWMANY(x, y) (((x) + (y - 1)) / (y)) + +#define warning(...) do { \ + printf("Warning: "); \ + printf(__VA_ARGS__); \ +} while (0) + +// フォント種別 enum fonttype_t { FONT_16x16, FONT_8x8, @@ -29,75 +30,96 @@ enum fonttype_t { FONT_MAX, }; -/* 範囲 */ -#define R_CTRL (0x0001) /* 制御文字 */ -#define R_ASCII (0x0002) /* ASCII(20-7F) */ -#define R_KANA (0x0004) /* カナ(A0-DF) */ -#define R_HIRA (0x0008) /* ひら(86-BF,E0-FF) */ -#define R_80 (0x0010) /* \(0x80) */ -#define R_81 (0x0020) /* 〜(0x81) */ -#define R_82 (0x0040) /* |(0x82) */ +// 範囲 +#define R_CTRL (0x0001) // 制御文字 +#define R_ASCII (0x0002) // ASCII(20-7F) +#define R_KANA (0x0004) // カナ(A0-DF) +#define R_HIRA (0x0008) // ひら(86-BF,E0-FF) +#define R_80 (0x0010) // \(0x80) +#define R_81 (0x0020) // 〜(0x81) +#define R_82 (0x0040) // |(0x82) #define R_SWITCH (R_80 | R_81 | R_82) -#define R_ALL (0x007f) /* 全部 */ +#define R_ALL (0x007f) // 全部 -/* BDF 形式で使われるバウンディングボックス */ -struct BBX -{ +// BDF 形式で使われるバウンディングボックス +struct BBX { int w; int h; int x; int y; }; -/* 1文字分のフォントデータ */ +// 1文字分のフォントデータ struct font_t { int code; - unsigned char pattern[3 * 24]; + std::array pattern; +}; + +// エラーが起きたら即終了する +class File +{ + public: + bool Create(const std::string& filename_) { + fd = open(filename_.c_str(), O_RDWR | O_CREAT | O_TRUNC, 0644); + if (fd < 0) { + err(1, "open: %s", filename_.c_str()); + } + return true; + } + + void Close() { + close(fd); + } + + ssize_t Write(const void *buf, size_t len) { + auto r = write(fd, buf, len); + if (r < 0) { + err(1, "File.Write(%d)", fd); + } + return r; + } + + off_t Seek(off_t off) { + auto r = lseek(fd, off, SEEK_SET); + if (r < 0) { + err(1, "File.Seek(%d, %jd)", fd, (intmax_t)off); + } + return r; + } + + private: + int fd {}; }; -/* グローバル変数 */ -static wxString fontdir; -static wxFile cgrom; - -/* ワーニングを表示 */ -static void -warning(const char *fmt, ...) -{ - va_list ap; - - printf("Warning: "); - - va_start(ap, fmt); - vprintf(fmt, ap); - va_end(ap); -} - -/* - * クラス構造 - * - * Font : すべてのフォントの基底 - * | - * +- F24Font : F24形式ファイルから 12x24(一部)と24x24を作るクラス - * | - * +- BDF : BDF形式ファイルを読み込むすべての基底クラス - * | - * +- BDF_1byte : 文字コードマッピングが不要な1バイトフォントを出力 - * | | - * | +- BDF_1byte_unicode : Unicode から1バイトフォントを出力 - * | | - * | +- BDF_1byte_jis0208 : JIS X 0208 から1バイトフォントを出力 - * | - * +- BDF_kanji_jis0208 : JIS X 0208 から漢字フォントを出力する - */ +// グローバル変数 +static std::string fontdir; +static File cgrom; + +// クラス構造 +// +// Font : すべてのフォントの基底 +// | +// +- BDF : BDF形式ファイルを読み込むすべての基底クラス +// | +// +- BDF_1byte : 文字コードマッピングが不要な1バイトフォントを出力 +// | | +// | +- BDF_1byte_unicode : Unicode から1バイトフォントを出力 +// | | +// | +- BDF_1byte_jis0208 : JIS X 0208 から1バイトフォントを出力 +// | +// +- BDF_kanji_jis0208 : JIS X 0208 から漢字フォントを出力する -/* フォント基底クラス */ +// +// フォント基底クラス +// class Font { public: - Font(const char *fname, int, int, int); - ~Font(); + Font(const std::string& filename_, int, int, int); + virtual ~Font(); void SetRange(int flag) { range_flag = flag; } - bool debug; + + bool debug {}; protected: bool Open(); @@ -115,56 +137,51 @@ class Font } bool IsRange(int code) const; - int pattern_len; /* 1文字分のバイト数 */ - int offset; - const char *filename; - FILE *fp; - int range_flag; /* 出力範囲フラグ */ + int pattern_len {}; // 1文字分のバイト数 + int offset {}; + std::string filename {}; + FILE *fp {}; + int range_flag {}; // 出力範囲フラグ }; -/* コンストラクタ */ -Font::Font(const char *fname, int font_x, int font_y, int out_offset) +// コンストラクタ +Font::Font(const std::string& filename_, int font_x, int font_y, int out_offset) { - debug = false; range_flag = R_ALL; - fp = NULL; - /* 引数 */ - filename = fname; - pattern_len = ROUND8(font_x) * font_y; + // 引数 + filename = filename_; + pattern_len = HOWMANY(font_x, 8) * font_y; offset = out_offset; - printf("generate %dx%d from %s\n", font_x, font_y, filename); + printf("generate %dx%d from %s\n", font_x, font_y, filename.c_str()); - /* ここでもうオープンする */ + // ここでもうオープンする Open(); } -/* デストラクタ */ +// デストラクタ Font::~Font() { Close(); } -/* オープン */ +// オープン bool Font::Open() { - char pathname[PATH_MAX]; - - snprintf(pathname, sizeof(pathname), "%s/%s", - (const char *)::fontdir.mb_str(), filename); + std::string pathname = ::fontdir + "/" + filename; - fp = fopen(pathname, "r"); + fp = fopen(pathname.c_str(), "r"); if (fp == NULL) { - printf("Cannot open: %s\n", pathname); + printf("Cannot open: %s\n", pathname.c_str()); throw 0; } return true; } -/* クローズ */ +// クローズ void Font::Close() { @@ -174,7 +191,7 @@ Font::Close() fp = NULL; } -/* 文字コードが今回出力を許可された範囲内か */ +// 文字コードが今回出力を許可された範囲内か bool Font::IsRange(int code) const { @@ -203,64 +220,67 @@ Font::IsRange(int code) const } -/* BDF 基底クラス */ +// +// BDF 基底クラス +// class BDF : public Font { - typedef Font inherited; + using inherited = Font; public: - BDF(const char *fname, int, int, int); - virtual ~BDF() { } + BDF(const std::string& filename_, int, int, int); + virtual ~BDF() override; void Convert(); protected: bool ReadProperties(); bool ReadNextChar(font_t& data); - virtual int GetAddr(int code) const; /* CGROM 収録アドレスを求める */ - int Unicode2JIS(int unicode) const; - int JIS2Addr(int code) const; + virtual int GetAddr(int code) const; // CGROM 収録アドレスを求める + static int Unicode2JIS(int unicode); + static int JIS2Addr(int code); - BBX fontbbx; /* FONTBOUNDINGBOX。.w = 横サイズ、.h = 縦サイズ */ - int font_descent; + BBX fontbbx {}; // FONTBOUNDINGBOX。.w = 横サイズ、.h = 縦サイズ + int font_descent {}; }; -/* コンストラクタ */ -BDF::BDF(const char *fname, int font_width, int font_height, int out_offset) - : inherited(fname, font_width, font_height, out_offset) +// コンストラクタ +BDF::BDF(const std::string& filename_, int font_width, int font_height, + int out_offset) + : inherited(filename_, font_width, font_height, out_offset) { - font_descent = 0; - memset(&fontbbx, 0, sizeof(fontbbx)); - - /* ここでプロパティを読んでおく */ + // ここでプロパティを読んでおく ReadProperties(); } -/* - * BDF のプロパティ (ヘッダみたいなもの) を読み込む - */ +// デストラクタ +BDF::~BDF() +{ +} + +// BDF のプロパティ (ヘッダみたいなもの) を読み込む bool BDF::ReadProperties() { char buf[100]; char *p; - int pixel_size; /* 縦サイズ */ - int quad_width; /* 横サイズ */ + int pixel_size; // 縦サイズ + int quad_width; // 横サイズ pixel_size = -1; quad_width = -1; while (fgets(buf, sizeof(buf), fp)) { if (strncmp(buf, "FONTBOUNDINGBOX ", 15) == 0) { - /* フォントの縦横サイズ */ + // フォントの縦横サイズ fontbbx.w = strtol(buf + 15, &p, 10); fontbbx.h = strtol(p, NULL, 10); } else if (strncmp(buf, "PIXEL_SIZE ", 11) == 0) { - /* フォントの縦サイズ */ + // フォントの縦サイズ pixel_size = strtol(buf + 11, NULL, 10); } else if (strncmp(buf, "QUAD_WIDTH ", 11) == 0) { - /* フォントの横サイズ */ + // フォントの横サイズ quad_width = strtol(buf + 11, NULL, 10); } else if (strncmp(buf, "FONT_DESCENT ", 13) == 0) { @@ -271,11 +291,9 @@ BDF::ReadProperties() } } - /* - * PIXEL_SIZE, QUAD_WIDTH のほうを優先。 - * TTF→BDF では FONTBOUNDINGBOX がうまく適切な値にならないため。 - * 元から BDF なフォントでは大抵同じ値になってるはず。 - */ + // PIXEL_SIZE, QUAD_WIDTH のほうを優先。 + // TTF→BDF では FONTBOUNDINGBOX がうまく適切な値にならないため。 + // 元から BDF なフォントでは大抵同じ値になってるはず。 if (pixel_size != -1) { fontbbx.h = pixel_size; } @@ -286,10 +304,8 @@ BDF::ReadProperties() return true; } -/* - * BDF から次の1文字を読み込んで data に格納する。 - * 取り出せなければ false を返す。 - */ +// BDF から次の1文字を読み込んで data に格納する。 +// 取り出せなければ false を返す。 bool BDF::ReadNextChar(font_t& data) { @@ -304,7 +320,7 @@ BDF::ReadNextChar(font_t& data) if (strncmp(buf, "STARTCHAR ", 10) == 0) { data.code = strtol(buf + 10, NULL, 16); dwidth = 0; - memset(data.pattern, 0, sizeof(data.pattern)); + memset(&data.pattern[0], 0, data.pattern.size()); } else if (strncmp(buf, "ENCODING ", 9) == 0) { data.code = strtol(buf + 9, NULL, 10); @@ -339,7 +355,7 @@ BDF::ReadNextChar(font_t& data) } in_bitmap = true; - /* BBX による縦方向の開始行を調整 */ + // BBX による縦方向の開始行を調整 y = fontbbx.h - font_descent; // 上から原点までの行数 y -= bbx.y; // 上から BBX 底辺までの行数 y -= bbx.h; // 上から BBX 上辺までの行数 @@ -356,16 +372,14 @@ BDF::ReadNextChar(font_t& data) throw 0; } int len = strlen(buf) - 1; // '\n'分を引く - uint32_t val = strtol(buf, NULL, 16); - /* val を左詰めしてから */ + uint32 val = strtol(buf, NULL, 16); + // val を左詰めしてから val <<= (4 - (len / 2)) * 8; - /* BBX による横方向の位置調整 */ + // BBX による横方向の位置調整 val >>= bbx.x; - /* - * 1行を構成するバイト列を出力。等幅フォントを作っているので - * グリフごとの幅(DWIDTH)ではなく出力したいフォントの幅。 - */ + // 1行を構成するバイト列を出力。等幅フォントを作っているので + // グリフごとの幅(DWIDTH)ではなく出力したいフォントの幅。 data.pattern[y++] = (val >> 24) & 0xff; if (fontbbx.w > 8) { data.pattern[y++] = (val >> 16) & 0xff; @@ -376,60 +390,68 @@ BDF::ReadNextChar(font_t& data) } } - /* EOF なので取り出す文字がない */ + // EOF なので取り出す文字がない return false; } -/* 変換メイン */ +// 変換メイン void BDF::Convert() { font_t data; - /* フォントを1文字ずつ読み込んで */ + // フォントを1文字ずつ読み込んで while (ReadNextChar(data)) { - /* CGROM 収録アドレスを求めて */ + // CGROM 収録アドレスを求めて書き出す int addr = GetAddr(data.code); - if (addr < 0) { - continue; + if (addr >= 0) { + cgrom.Seek(offset + addr * pattern_len); + cgrom.Write(&data.pattern[0], pattern_len); } - /* 書き出す */ - cgrom.Seek(offset + addr * pattern_len); - cgrom.Write(data.pattern, pattern_len); } } -/* CGROM 収録アドレスを得る */ +// CGROM 収録アドレスを得る int BDF::GetAddr(int code) const { - /* デフォルトはそのまま返す */ + // デフォルトはそのまま返す return code; } -/* Unicode を JIS に変換する */ -int -BDF::Unicode2JIS(int unicode) const -{ - wxCSConv convjis("iso-2022-jp"); +// Unicode を JIS に変換する +/*static*/ int +BDF::Unicode2JIS(int unicode) +{ + auto cd = iconv_open("iso-2022-jp", "unicode"); + if (cd == (iconv_t)-1) { + printf("iconv_open failed\n"); + return 0; + } + + // ホストバイトオーダー4バイトのコードポイント + const char *src = (const char *)&unicode; + size_t srcleft = sizeof(unicode); + // JIS は前後のエスケープも含む + std::array dstbuf; + char *dst = dstbuf.data(); + size_t dstlen = dstbuf.size(); - wxString str((wxChar)unicode); - const char *jisstr = str.mb_str(convjis); - int jiscode = ((unsigned int)jisstr[0] << 8) + jisstr[1]; + ICONV(cd, &src, &srcleft, &dst, &dstlen); + iconv_close(cd); + int jiscode = ((unsigned int)dstbuf[3] << 8) + (unsigned int)dstbuf[4]; return jiscode; } -/* JIS コードから CGROM 収録コードを求めて返す */ -int -BDF::JIS2Addr(int code) const +// JIS コードから CGROM 収録コードを求めて返す +/*static*/ int +BDF::JIS2Addr(int code) { int jh, jl, addr; - /* - * JIS コード 7425、7426 (区点84-05、84-06) の2文字は - * JIS X 0208:1990 で追加されたものなので、CGROM にはない。 - */ + // JIS コード 7425、7426 (区点84-05、84-06) の2文字は + // JIS X 0208:1990 で追加されたものなので、CGROM にはない。 if (code > 0x7424) { return -1; } @@ -437,7 +459,7 @@ BDF::JIS2Addr(int code) const jh = (code >> 8); jl = (code & 0xff); - /* (Unicodeから変換した)コードが JIS の範囲外なら無視 */ + // (Unicodeから変換した)コードが JIS の範囲外なら無視 if (jl < 0x21 || jl >= 0x7f) { return -1; } @@ -451,31 +473,38 @@ BDF::JIS2Addr(int code) const } -/* - * BDF フォントから1バイトフォントを作るクラス。 - * 文字コードは ASCII/ISO-8859 か JIS X 0201 など - * そのまま1バイトコードにマップできるもの。 - */ +// +// BDF フォントから1バイトフォントを作るクラス。 +// 文字コードは ASCII/ISO-8859 か JIS X 0201 など +// そのまま1バイトコードにマップできるもの。 +// class BDF_1byte : public BDF { - typedef BDF inherited; + using inherited = BDF; public: - BDF_1byte(const char *filename, int, int, int); - virtual ~BDF_1byte() { } - virtual int GetAddr(int code) const; + BDF_1byte(const std::string& filename_, int, int, int); + virtual ~BDF_1byte() override; + + int GetAddr(int code) const override; }; -/* コンストラクタ */ -BDF_1byte::BDF_1byte(const char *fname, int font_x, int font_y, int out_offset) - : inherited(fname, font_x, font_y, out_offset) +// コンストラクタ +BDF_1byte::BDF_1byte(const std::string& filename_, int font_x, int font_y, + int out_offset) + : inherited(filename_, font_x, font_y, out_offset) +{ +} + +// デストラクタ +BDF_1byte::~BDF_1byte() { } -/* CGROM 収録アドレスを返す */ +// CGROM 収録アドレスを返す int BDF_1byte::GetAddr(int code) const { - /* 指定範囲に入っているかどうかだけ */ + // 指定範囲に入っているかどうかだけ if (IsRange(code)) { return code; } @@ -483,30 +512,36 @@ BDF_1byte::GetAddr(int code) const } -/* - * BDF 日本語フォントから1バイトフォントを作るクラス。 - * 文字コードは Unicode。 - */ +// +// BDF 日本語フォントから1バイトフォントを作るクラス。 +// 文字コードは Unicode。 +// class BDF_1byte_unicode : public BDF_1byte { - typedef BDF_1byte inherited; + using inherited = BDF_1byte; public: - BDF_1byte_unicode(const char *filename, int, int, int); - virtual ~BDF_1byte_unicode() { } - virtual int GetAddr(int code) const; + BDF_1byte_unicode(const std::string& filename_, int, int, int); + virtual ~BDF_1byte_unicode() override; + + int GetAddr(int code) const override; protected: - static const wxChar table_uni[]; + static const wchar_t table_uni[]; }; -/* コンストラクタ */ -BDF_1byte_unicode::BDF_1byte_unicode(const char *fname, int font_x, int font_y, - int out_offset) - : inherited(fname, font_x, font_y, out_offset) +// コンストラクタ +BDF_1byte_unicode::BDF_1byte_unicode(const std::string& filename_, + int font_x, int font_y, int out_offset) + : inherited(filename_, font_x, font_y, out_offset) { } -/* CGROM 収録アドレスを返す */ +// デストラクタ +BDF_1byte_unicode::~BDF_1byte_unicode() +{ +} + +// CGROM 収録アドレスを返す int BDF_1byte_unicode::GetAddr(int code) const { @@ -519,61 +554,60 @@ BDF_1byte_unicode::GetAddr(int code) con return -1; } -/* - * Unicode を1バイト文字コードに変換するテーブル。 - * 制御記号(0x01〜0x1b)、矢印(0x1c〜0x1f)、チルダ(0x81)、パイプ(0x82)は - * ここでは扱わない。 - */ -const wxChar BDF_1byte_unicode::table_uni[] = - wxT("                ") // +00 - wxT("            →←↑↓") // +10 - wxT(" !”#$%&’()*+,−./") // +20 - wxT("0123456789:;<=>?") // +30 - wxT("@ABCDEFGHIJKLMNO") // +40 - wxT("PQRSTUVWXYZ[¥]^_") // +50 - wxT("`abcdefghijklmno") // +60 - wxT("pqrstuvwxyz{|} ̄ ") // +70 - wxT("\     をぁぃぅぇぉゃゅょっ") // +80 - wxT(" あいうえおかきくけこさしすせそ") // +90 - wxT(" 。「」、・ヲァィゥェォャュョッ") // +a0 - wxT("ーアイウエオカキクケコサシスセソ") // +b0 - wxT("タチツテトナニヌネノハヒフヘホマ") // +c0 - wxT("ミムメモヤユヨラリルレロワン゛゜") // +d0 - wxT("たちつてとなにぬねのはひふへほま") // +e0 - wxT("みむめもやゆよらりるれろわん  "); // +f0 - - -/* - * BDF 日本語フォントから1バイトフォントを作るクラス。 - * 文字コードは JIS X 0208 (の非漢字部分)。 - */ +// Unicode を1バイト文字コードに変換するテーブル。 +// 制御記号(0x01〜0x1b)、矢印(0x1c〜0x1f)、チルダ(0x81)、パイプ(0x82)は +// ここでは扱わない。 +const wchar_t BDF_1byte_unicode::table_uni[] = + L"                " // +00 + L"            →←↑↓" // +10 + L" !”#$%&’()*+,−./" // +20 + L"0123456789:;<=>?" // +30 + L"@ABCDEFGHIJKLMNO" // +40 + L"PQRSTUVWXYZ[¥]^_" // +50 + L"`abcdefghijklmno" // +60 + L"pqrstuvwxyz{|} ̄ " // +70 + L"\     をぁぃぅぇぉゃゅょっ" // +80 + L" あいうえおかきくけこさしすせそ" // +90 + L" 。「」、・ヲァィゥェォャュョッ" // +a0 + L"ーアイウエオカキクケコサシスセソ" // +b0 + L"タチツテトナニヌネノハヒフヘホマ" // +c0 + L"ミムメモヤユヨラリルレロワン゛゜" // +d0 + L"たちつてとなにぬねのはひふへほま" // +e0 + L"みむめもやゆよらりるれろわん  "; // +f0 + + +// +// BDF 日本語フォントから1バイトフォントを作るクラス。 +// 文字コードは JIS X 0208 (の非漢字部分)。 +// class BDF_1byte_jis0208 : public BDF_1byte_unicode { - typedef BDF_1byte_unicode inherited; + using inherited = BDF_1byte_unicode; public: - BDF_1byte_jis0208(const char *filename, int, int, int); - virtual ~BDF_1byte_jis0208() { } - virtual int GetAddr(int code) const; + BDF_1byte_jis0208(const std::string& filename_, int, int, int); + virtual ~BDF_1byte_jis0208() override; + + int GetAddr(int code) const override; protected: - static int table_jis[256]; + static std::array table_jis; static bool table_jis_prepared; }; -/* JIS コードを1バイト文字コードに変換するテーブル */ -int BDF_1byte_jis0208::table_jis[256]; +// JIS コードを1バイト文字コードに変換するテーブル +std::array BDF_1byte_jis0208::table_jis; -/* テーブルが用意できているか */ +// テーブルが用意できているか bool BDF_1byte_jis0208::table_jis_prepared; -/* コンストラクタ */ -BDF_1byte_jis0208::BDF_1byte_jis0208(const char *fname, int font_x, int font_y, - int out_offset) - : inherited(fname, font_x, font_y, out_offset) +// コンストラクタ +BDF_1byte_jis0208::BDF_1byte_jis0208(const std::string& filename_, + int font_x, int font_y, int out_offset) + : inherited(filename_, font_x, font_y, out_offset) { - /* 最初に一度だけ table_uni から table_jis を用意しておく */ + // 最初に一度だけ table_uni から table_jis を用意しておく if (!table_jis_prepared) { - for (int i = 0; i < countof(table_jis); i++) { + for (int i = 0, end = table_jis.size(); i < end; i++) { int unicode = table_uni[i]; table_jis[i] = Unicode2JIS(unicode); } @@ -581,11 +615,16 @@ BDF_1byte_jis0208::BDF_1byte_jis0208(con } } -/* CGROM 収録アドレスを求める */ +// デストラクタ +BDF_1byte_jis0208::~BDF_1byte_jis0208() +{ +} + +// CGROM 収録アドレスを求める int BDF_1byte_jis0208::GetAddr(int code) const { - for (int i = 0; i < countof(table_jis); i++) { + for (int i = 0, end = table_jis.size(); i < end; i++) { if (code == table_jis[i]) { return i; } @@ -595,187 +634,91 @@ BDF_1byte_jis0208::GetAddr(int code) con } -/* - * BDF フォントから漢字フォントを作るクラス。 - * 文字コードは JIS X 0208。 - */ +// +// BDF フォントから漢字フォントを作るクラス。 +// 文字コードは JIS X 0208。 +// class BDF_kanji_jis0208 : public BDF { - typedef BDF inherited; + using inherited = BDF; public: - BDF_kanji_jis0208(const char *fname, int, int, int); - virtual ~BDF_kanji_jis0208() { } - virtual int GetAddr(int code) const; + BDF_kanji_jis0208(const std::string& filename_, int, int, int); + virtual ~BDF_kanji_jis0208() override; + + int GetAddr(int code) const override; }; -/* コンストラクタ */ -BDF_kanji_jis0208::BDF_kanji_jis0208(const char *fname, int font_x, int font_y, - int out_offset) - : inherited(fname, font_x, font_y, out_offset) +// コンストラクタ +BDF_kanji_jis0208::BDF_kanji_jis0208(const std::string& filename_, + int font_x, int font_y, int out_offset) + : inherited(filename_, font_x, font_y, out_offset) +{ +} + +// デストラクタ +BDF_kanji_jis0208::~BDF_kanji_jis0208() { } -/* CGROM 収録コードを求めて返す */ +// CGROM 収録コードを求めて返す int BDF_kanji_jis0208::GetAddr(int code) const { - /* JIS コードを変換するだけ */ + // JIS コードを変換するだけ return JIS2Addr(code); } -/* - * F24 フォントから 12x12(一部)と 24x24 フォントを作るクラス - */ -class F24Font : public Font -{ - public: - F24Font(const char *fname, int out_offset); - virtual ~F24Font() { } - bool Convert(); -}; +// +// アプリケーション +// -/* コンストラクタ */ -F24Font::F24Font(const char *fname, int out_offset) - : Font(fname, 24, 24, out_offset) -{ -} +static void MakeCGROM(const std::string& outfile); +static void MakeMon(const std::string& outfile); -/* メイン処理 */ -bool -F24Font::Convert() +int +main(int ac, char *av[]) { - std::vector buf(pattern_len * 94); /* 1区94文字分 */ - int len; - int i; - int n; - - /* - * 日比谷フォントは先頭に半角256文字分があるので、 - * これの制御文字/ASCII/カナ部分を 12x24 に使う。ひらがなはない - */ - len = 2 * 24; - for (i = 0; i < 256; i++) { - n = fread(&buf[0], 1, len, fp); - if (n < len) { - printf("fread error: %d\n", n); - throw 0; - } - if (IsRange(i)) { - cgrom.Seek(0x3d000 + i * len); - cgrom.Write(&buf[0], n); - } - } + int c; + std::string outfile; + bool opt_m; - /* ここから 24x24 全角 */ - cgrom.Seek(offset); - len = buf.size(); - - /* 全角非漢字が8区 */ - for (i = 1; i < 9; i++) { - n = fread(&buf[0], 1, len, fp); - if (n < len) { - printf("fread error: %d\n", n); - throw 0; - } - cgrom.Write(&buf[0], n); - } + ::fontdir = "./fonts"; + outfile = "./cgrom.dat"; + opt_m = false; - /* 9区〜15区は JIS X 0208 にはないのでスキップ */ - for (; i < 16; i++) { - fseek(fp, buf.size(), SEEK_CUR); - } + while ((c = getopt(ac, av, "f:ho:m")) != -1) { + switch (c) { + case 'f': + ::fontdir = optarg; + break; + case 'o': + outfile = optarg; + break; + case 'm': + opt_m = true; + break; - /* 16区〜83区が第一、第二水準漢字 */ - for (; i < 84; i++) { - n = fread(&buf[0], 1, len, fp); - if (n < len) { - printf("fread error: %d\n", n); - throw 0; + case 'h': + default: + errx(1, "usage: [-f ./font] [-o ./cgrom.dat] [-m]"); } - cgrom.Write(&buf[0], n); } - /* - * 84区 05、06 の2文字は 90JIS で追加されたものなので、 - * CGROM にはない。 - */ - len = pattern_len * 4; - n = fread(&buf[0], 1, len, fp); - if (n < len) { - printf("fread error: %d\n", n); - throw 0; - } - cgrom.Write(&buf[0], n); - - return true; -} - - -/* - * アプリケーションクラス - */ -class MyApp : public wxApp -{ - public: - virtual bool OnInit(); - void MakeCGROM(); - void MakeMon(); - - private: - wxString outfile; - static const wxCmdLineEntryDesc desc[]; -}; - -// IMPLEMENT_APP() は闇マクロすぎて clang のちからには耐えられない -#if defined(__clang__) -#pragma clang diagnostic push -#pragma clang diagnostic ignored "-Weverything" -#endif -IMPLEMENT_APP(MyApp) -#if defined(__clang__) -#pragma clang diagnostic pop -#endif - -const wxCmdLineEntryDesc MyApp::desc[] = { - { wxCMD_LINE_OPTION, "f", "f", "font dir (default: ./fonts)", }, - { wxCMD_LINE_OPTION, "o", "o", "output filename (default: ./cgrom.dat)", }, - { wxCMD_LINE_SWITCH, "m", "m", "make monitor cgrom", }, - { wxCMD_LINE_NONE, }, -}; - -bool MyApp::OnInit() -{ - /* 引数解析 */ - wxCmdLineParser cmd(argc, argv); - cmd.SetDesc(desc); - cmd.Parse(); - ::fontdir = "./fonts"; - outfile = "./cgrom.dat"; - cmd.Found("f", &::fontdir); - cmd.Found("o", &outfile); - - if (cmd.Found("m")) { - // モニタウィンドウ用 12dot - MakeMon(); + if (opt_m) { + MakeMon(outfile); } else { - // それ以外なら X680x0 CGROM - MakeCGROM(); + MakeCGROM(outfile); } - - /* 即終了する */ - return false; + return 0; } - // X680x0 CGROM を作成する void -MyApp::MakeCGROM() +MakeCGROM(const std::string& outfile) { - /* 出力ファイルを空で作成 */ - printf("output file: %s\n", (const char *)outfile.mb_str()); - if (!cgrom.Create(outfile, wxFile::write)) { - return; - } + // 出力ファイルを空で作成 + printf("output file: %s\n", outfile.c_str()); + cgrom.Create(outfile); cgrom.Seek(768 * 1024 - 1); cgrom.Write("", 1); @@ -812,17 +755,18 @@ MyApp::MakeCGROM() BDF_kanji_jis0208 k16("shnmk16min.bdf", 16, 16, 0); k16.Convert(); - /* これだけ変則で 12x24 と 24x24 を生成する */ - F24Font k24("MIN.F24", 0x40000); - /* これは 12x24 についての指定 */ - k24.SetRange(R_CTRL | R_ASCII | R_KANA); - k24.Convert(); + BDF_1byte h12("hibiya-12x24.bdf", 12, 24, 0x3d000); + h12.SetRange(R_ASCII); + h12.Convert(); BDF_1byte o24("nono-12x24.bdf", 12, 24, 0x3d000); o24.Convert(); - BDF_1byte_unicode t12("ume-tmo3-12x24-hira.bdf", 12, 24, 0x3d000); - t12.SetRange(R_HIRA); + BDF_kanji_jis0208 k24("hibiya-24x24.bdf", 24, 24, 0x40000); + k24.Convert(); + + BDF_1byte_unicode t12("ume-tmo3-12x24-kana.bdf", 12, 24, 0x3d000); + t12.SetRange(R_HIRA | R_KANA); t12.Convert(); } catch (...) { } @@ -832,13 +776,11 @@ MyApp::MakeCGROM() // モニタウィンドウ用 12dot フォントデータ void -MyApp::MakeMon() +MakeMon(const std::string& outfile) { // 出力ファイルを空で作成 - printf("output file: %s\n", (const char *)outfile.mb_str()); - if (!cgrom.Create(outfile, wxFile::write)) { - return; - } + printf("output file: %s\n", outfile.c_str()); + cgrom.Create(outfile); try { BDF_kanji_jis0208 k12("knmzn12x.bdf", 12, 12, 0); k12.Convert();