--- nono/wx/wxtextpanel.cpp 2026/04/29 17:04:56 1.1.1.6 +++ nono/wx/wxtextpanel.cpp 2026/04/29 17:05:16 1.1.1.9 @@ -4,39 +4,20 @@ // Licensed under nono-license.txt // -#include "wxtextpanel.h" -#include "wxcolor.h" -#include "bitrev.h" -#include "cgrom.h" -#include "kaname12.h" -#include "privatechar.h" -#include "sjis.h" - // -// テキストフォントを管理するクラス +// テキストフォントを描画するパネル // -static const wxSize fontinfo[] = { - // 順序はヘッダの FontId_* 識別子の順と揃えること - FONT_6x12, - FONT_8x16, -}; -static_assert(countof(fontinfo) == FontId::Max, ""); - - -// コンストラクタ (style 指定なし) -// (wxPanel の style のデフォルトは wxTAB_TRAVERSAL) -WXTextPanel::WXTextPanel(wxWindow *parent) - : WXTextPanel(parent, wxTAB_TRAVERSAL) -{ -} +#include "wxtextpanel.h" +#include "fontmanager.h" +#include "wxcolor.h" +#include "sjis.h" -// コンストラクタ (style 指定あり) -WXTextPanel::WXTextPanel(wxWindow *parent, long style) - : inherited(parent, wxID_ANY, wxDefaultPosition, wxDefaultSize, style) +// コンストラクタ +WXTextPanel::WXTextPanel(wxWindow *parent, wxWindowID id, + const wxPoint& position, const wxSize& size, long style) + : inherited(parent, id, position, size, style) { - UpdateFont(global_fontid); - // デフォルト色 ResetTextColor(); } @@ -44,182 +25,67 @@ WXTextPanel::WXTextPanel(wxWindow *paren // デストラクタ WXTextPanel::~WXTextPanel() { - FreeFont(fontid); } -// フォント関連の内部情報をよしなに更新する void -WXTextPanel::UpdateFont(FontId new_fontid) -{ - assert(fontid != new_fontid); - - // フォントデータを更新 - FreeFont(fontid); - AllocFont(new_fontid); - - // 内部変数を更新 - fontid = new_fontid; - fontsize = fontinfo[fontid]; - ascii_glyph = ascii_glyph_table[fontid]; - bold_glyph = bold_glyph_table[fontid]; -} - -// 必要ならフォントを用意する -/*static*/ void -WXTextPanel::AllocFont(FontId fontid) +WXTextPanel::FontChanged() { - const uint8 *base; - - std::unique_lock lock(global_font_mutex); - if (font_refcnt[fontid]++ != 0) { - return; - } - - // 6x12, 8x16 半角は X68030 CGROM にあるのでそれを使う - switch (fontid) { - case FontId::_6x12: - base = BuiltinCGROM::Get6x12(); - break; - case FontId::_8x16: - base = BuiltinCGROM::Get8x16(); - break; - default: - assertmsg(0, "fontid=%d", fontid); - } - - // フォントの行ストライド[byte] を計算。 - // ここは static 関数内なので WXTextPanel インスタンス内の変数 - // font_width, font_height は見えない。(shadow にもならない) - int font_width = fontinfo[fontid].GetWidth(); - int font_height = fontinfo[fontid].GetHeight(); - int font_stride = (font_width + 7) / 8; - int bytes = font_stride * font_height; - std::vector buf(bytes); - - // 半角文字領域 256 文字全部をキャッシュする。 - // ASCII は 0x7f までだが X680x0 の CGROM の 0x80, 0x81, 0x82 には - // '\\', '~', '|' が収納されている。 - // 制御文字領域を外字として使う。 - wxBitmap **ascii_glyph = ascii_glyph_table[fontid]; - wxBitmap **bold_glyph = bold_glyph_table[fontid]; - - // 先に指定されたところに外字を割り当てる - for (int i = 0; private_chars[i].code != 0; i++) { - const auto& pchar = private_chars[i]; - const char *ptr; - - switch (fontid) { - case FontId::_6x12: - ptr = (const char *)pchar.xbm12; - break; - case FontId::_8x16: - ptr = (const char *)pchar.xbm16; - break; - default: - __unreachable(); - } - - // 元データはすでに XBM 形式。 - // ボールドにはしないので同じデータを入れておく。 - int ch = pchar.code; - assert(ascii_glyph[ch] == NULL); - assert(bold_glyph[ch] == NULL); - ascii_glyph[ch] = new wxBitmap(ptr, font_width, font_height, 1); - bold_glyph[ch] = new wxBitmap(ptr, font_width, font_height, 1); - } - - // CGROM に収録されている文字のうち半角ひらがななど使わないものもあるが - // 避けるのも手間なので気にせずデータを作っておく。 - for (int ch = 0; ch < countof(ascii_glyph_table[0]); ch++) { - // すでに外字が設定されているところは避ける - if (ascii_glyph[ch]) { - continue; - } - - const uint8 *ptr = base + ch * bytes; - - // wxBitmap は XBM 形式で 1bpp ビットマップを受け取る。 - // XBM は MSB が右なので、ビット左右を反転する - for (int i = 0; i < bytes; i++) { - uint8 src = ptr[i]; - buf[i] = bitrev(src); - } - // その XBM データから1文字分のビットマップオブジェクトを作成 - ascii_glyph[ch] = new wxBitmap(&buf[0], font_width, font_height, 1); - - // 続いてボールドに加工 - for (int i = 0; i < bytes; i++) { - // XBM 形式は MSB が右なので左シフトすると一般的なボールド。 - buf[i] |= ((uint8)buf[i] << 1); - } - bold_glyph[ch] = new wxBitmap(&buf[0], font_width, font_height, 1); - } -} - -// 最後の一人ならフォントを解放する -/*static*/ void -WXTextPanel::FreeFont(FontId fontid) -{ - std::unique_lock lock(global_font_mutex); - - if (fontid < 0 || fontid >= FontId::Max) { - return; - } - if (--font_refcnt[fontid] != 0) { - return; - } - - wxBitmap **ascii_glyph = ascii_glyph_table[fontid]; - wxBitmap **bold_glyph = bold_glyph_table[fontid]; - for (int i = 0; i < countof(ascii_glyph_table[0]); i++) { - if (ascii_glyph[i]) { - delete ascii_glyph[i]; - ascii_glyph[i] = NULL; - } - if (bold_glyph[i]) { - delete bold_glyph[i]; - bold_glyph[i] = NULL; - } - } + font_width = gFontManager->GetFontWidth(); + font_height = gFontManager->GetFontHeight(); } // テキスト色をデフォルトに設定する(戻す) void WXTextPanel::ResetTextColor() { - SetTextColor(*wxBLACK, BGPANEL); + SetTextColor(UD_BLACK, BGPANEL); } // テキスト色を指定する void -WXTextPanel::SetTextColor(const wxColour& fg, const wxColour& bg) +WXTextPanel::SetTextColor(Color fg, Color bg) +{ + palette[FG] = fg; + palette[BG] = bg; +} + +// (px, py) を左上とする座標から Shift_JIS 文字列を、前景色 c、属性 attr で +// 描画する。 +// ただし attr のうちここで参照するのは EM (ボールド) かそうでないかのみ。 +// フォントサイズは現行のフォント。 +// キャンバスをはみ出さないこと。 +void +WXTextPanel::DrawStringSJIS(Color c, int px, int py, const char *sjis, + uint attr) { - text_fgcolor = fg; - text_bgcolor = bg; + Color backup = palette[FG]; + + palette[FG] = c; + DrawStringSJIS(px, py, sjis, attr); + palette[FG] = backup; } // (px, py) を左上とする座標から Shift_JIS 文字列を属性 attr で描画する。 // ただし attr のうちここで参照するのは EM (ボールド) かそうでないかのみ。 // 描画色、背景色は事前に SetTextColor() で設定したもの。 // フォントサイズは現行のフォント。 -// キャンバスをはみ出すかどうかについてはこちらでは関知しない。 +// キャンバスをはみ出さないこと。 void -WXTextPanel::DrawStringSJIS(wxDC& dc, int px, int py, const char *sjis, - uint attr) +WXTextPanel::DrawStringSJIS(int px, int py, const char *sjis, uint attr) { const uint8 *s; for (s = (const uint8 *)sjis; *s; ) { if (__predict_true(SJIS::IsHankaku(*s))) { // 半角 - DrawChar1(dc, px, py, *s++, attr); + DrawChar1(px, py, *s++, attr); px += font_width; } else { // 全角 int code; code = (*s++ << 8); code |= *s++; - DrawChar2(dc, px, py, code, attr); + DrawChar2(px, py, code, attr); px += font_width * 2; } } @@ -229,133 +95,26 @@ WXTextPanel::DrawStringSJIS(wxDC& dc, in // code は ASCII または半角カナ。 // フォントサイズは現行のフォント。 void -WXTextPanel::DrawChar1(wxDC& dc, int px, int py, uint code, uint attr) +WXTextPanel::DrawChar1(int px, int py, uint code, uint attr) { - wxBitmap **glyph; + const uint8 *glyph; assertmsg(code < 0x100, "code=0x%x", code); - // ボールドかどうかでテーブルが別 - if ((attr & TA::Em)) { - glyph = bold_glyph; - } else { - glyph = ascii_glyph; - } - - // 変更可能なグリフ - if (__predict_false(code == 0x5c)) { - if (glyph_5c) { - code = 0x80; - } - } else if (__predict_false(code == 0x7e)) { - if (glyph_7e) { - code = 0x81; - } - } else if (__predict_false(code == 0x7c)) { - if (glyph_7c) { - code = 0x82; - } - } - DrawBitmap(dc, px, py, *glyph[code]); + glyph = gFontManager->AsciiGlyph(code, attr); + BitmapI1 src(glyph, font_width, font_height); + bitmap.DrawBitmap(px, py, src, palette); } -// (px, py) を左上とする座標から漢字1文字を出力する。 +// (px, py) を左上とする座標から漢字1文字を属性 attr で描画する。 // sjiscode は Shift_JIS。 // フォントサイズは現行のフォント。 void -WXTextPanel::DrawChar2(wxDC& dc, int px, int py, uint sjiscode, uint attr) +WXTextPanel::DrawChar2(int px, int py, uint sjiscode, uint attr) { - std::unique_ptr kanji; - kanji.reset(KanjiBitmap(sjiscode, attr)); - DrawBitmap(dc, px, py, *kanji); -} + std::vector glyph; -// (px, py) を左上とする座標に bitmap を描画する。 -// 前景色、背景色は事前に SetTextColor() で予約された色を使う。 -void -WXTextPanel::DrawBitmap(wxDC& dc, int px, int py, const wxBitmap& bitmap) -{ - dc.SetTextForeground(text_fgcolor); - dc.SetTextBackground(text_bgcolor); - - dc.DrawBitmap(bitmap, px, py); + glyph = gFontManager->KanjiGlyph(sjiscode, attr); + BitmapI1 src(glyph.data(), font_width * 2, font_height); + bitmap.DrawBitmap(px, py, src, palette); } - -// Shift_JIS の漢字1文字のビットマップを作成する。 -wxBitmap * -WXTextPanel::KanjiBitmap(uint16 sjiscode, uint attr) -{ - // Shift_JIS を JIS というかコードポイントに変換 - uint sh = (sjiscode >> 8); - uint sl = sjiscode & 0xff; - uint jh; - uint jl; - if (sh < 0xa0) { - jh = sh - 0x71; - } else { - jh = sh - 0xb1; - } - jh = jh * 2 + 1; - jl = sl; - if (sl > 0x7f) { - jl--; - } - if (jl < 0x9e) { - jl -= 0x1f; - } else { - jl -= 0x7d; - jh++; - } - // ここで JIS。これを CGROM 格納コードポイントに変換。 - jh -= 0x21; - jl -= 0x21; - if (jh >= 15) { - jh -= 7; - } - int code = jh * 94 + jl; - - // XXX 見付からなかったらゲタ文字にしたい - if (code < 0) - code=0x6b; - - // フォント領域 - const uint8 *base; - switch (fontid) { - case FontId::_6x12: - // 12x12 は内蔵フォントデータ - base = builtin_kanji12; - break; - case FontId::_8x16: - // 16x16 は X680x0 CGROM のを使う。 - base = BuiltinCGROM::Get16x16(); - break; - default: - // ここより先に必ず半角のほうでエラーが出るはず - __unreachable(); - } - - int bytes = (((font_width * 2) + 7) / 8) * font_height; - std::vector buf(bytes); - - const uint8 *ptr; - ptr = base + code * bytes; - - for (int i = 0; i < bytes; i++) { - buf[i] = bitrev(ptr[i]); - } - if (attr == TA::Em) { - for (int i = 0; i < bytes; i++) { - buf[i] |= buf[i] >> 1; - } - } - - // その XBM データから1文字分のビットマップオブジェクトを作成 - return new wxBitmap(&buf[0], font_width * 2, font_height, 1); -} - -// スタティック変数 -FontId WXTextPanel::global_fontid; -std::mutex WXTextPanel::global_font_mutex; -u_int WXTextPanel::font_refcnt[FontId::Max]; -wxBitmap *WXTextPanel::ascii_glyph_table[FontId::Max][256]; -wxBitmap *WXTextPanel::bold_glyph_table[FontId::Max][256];