--- nono/wx/wxtextpanel.cpp 2026/04/29 17:04:28 1.1 +++ nono/wx/wxtextpanel.cpp 2026/04/29 17:04:53 1.1.1.5 @@ -1,113 +1,106 @@ // // nono -// Copyright (C) 2019 isaki@NetBSD.org +// Copyright (C) 2020 nono project +// Licensed under nono-license.txt // -#include "wxheader.h" -#include "wxcolor.h" #include "wxtextpanel.h" -#include "cgrom.h" +#include "wxcolor.h" #include "bitrev.h" - -// wxOSX(-3.0.2) の wxLIGHT_GREY は値が(色も透明度も)正しくない。 -// wxGTK の wxLIGHT_GREY は 0xffd3d3d3 だけどこれを Mac で見ると -// (Mac の統一感としては) 暗く見えるので色コード自体を変える。 -#if defined(__WXOSX__) -#define LIGHT_GREY wxColour(0xffe0e0e0) -#else -#define LIGHT_GREY *wxLIGHT_GREY -#endif +#include "cgrom.h" +#include "kaname12.h" +#include "sjis.h" // // テキストフォントを管理するクラス // -struct fontinfo { - int width; - int height; -} fontinfo[] = { - // 順序はヘッダの FONT_* 識別子の順と揃えること - { 6, 12 }, - { 8, 16 }, - { 12, 24 }, +static const wxSize fontinfo[] = { + // 順序はヘッダの FontId_* 識別子の順と揃えること + FONT_6x12, + FONT_8x16, }; -static_assert(countof(fontinfo) == FONT_MAX, ""); +static_assert(countof(fontinfo) == FontId::Max, ""); -// コンストラクタ +// コンストラクタ (style 指定なし) +// (wxPanel の style のデフォルトは wxTAB_TRAVERSAL) WXTextPanel::WXTextPanel(wxWindow *parent) - : inherited(parent, wxID_ANY) + : WXTextPanel(parent, wxTAB_TRAVERSAL) +{ +} + +// コンストラクタ (style 指定あり) +WXTextPanel::WXTextPanel(wxWindow *parent, long style) + : inherited(parent, wxID_ANY, wxDefaultPosition, wxDefaultSize, style) { - UpdateFont(global_fontsize); + UpdateFont(global_fontid); // デフォルト色 - SetTextColor(*wxBLACK, LIGHT_GREY, wxColour(0x50, 0x50, 0x50)); + ResetTextColor(); } // デストラクタ WXTextPanel::~WXTextPanel() { - FreeFont(fontsize); + FreeFont(fontid); } // フォント関連の内部情報をよしなに更新する void -WXTextPanel::UpdateFont(fontsize_t new_fontsize) +WXTextPanel::UpdateFont(FontId new_fontid) { - assert(fontsize != new_fontsize); + assert(fontid != new_fontid); // フォントデータを更新 - FreeFont(fontsize); - AllocFont(new_fontsize); + FreeFont(fontid); + AllocFont(new_fontid); // 内部変数を更新 - fontsize = new_fontsize; - font_width = fontinfo[fontsize].width; - font_height = fontinfo[fontsize].height; - ascii_glyph = ascii_glyph_table[fontsize]; - bold_glyph = bold_glyph_table[fontsize]; + fontid = new_fontid; + fontsize = fontinfo[fontid]; + ascii_glyph = ascii_glyph_table[fontid]; + bold_glyph = bold_glyph_table[fontid]; } // 必要ならフォントを用意する /*static*/ void -WXTextPanel::AllocFont(fontsize_t fontsize) +WXTextPanel::AllocFont(FontId fontid) { const uint8 *base; std::unique_lock lock(global_font_mutex); - if (font_refcnt[fontsize]++ != 0) { + if (font_refcnt[fontid]++ != 0) { return; } - // 6x12, 8x16, 12x24 半角は X68030 CGROM にあるのでそれを使う - switch (fontsize) { - case FONT_6x12: - base = builtin_cgrom + 0xbf400; - break; - case FONT_8x16: - base = builtin_cgrom + 0x3a800; + // 6x12, 8x16 半角は X68030 CGROM にあるのでそれを使う + switch (fontid) { + case FontId::_6x12: + base = BuiltinCGROM::Get6x12(); break; - case FONT_12x24: - base = builtin_cgrom + 0x3d000; + case FontId::_8x16: + base = BuiltinCGROM::Get8x16(); break; default: - fprintf(stderr, "%s:%d fontsize=%d\n", - __FILE__, __LINE__, fontsize); - assert(false); - __unreachable(); + assertmsg(0, "fontid=%d", fontid); } - // フォントの行ストライド[byte] - int font_width = fontinfo[fontsize].width; - int font_height = fontinfo[fontsize].height; + // フォントの行ストライド[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; - char buf[bytes]; + std::vector buf(bytes); - wxBitmap **ascii_glyph = ascii_glyph_table[fontsize]; - wxBitmap **bold_glyph = bold_glyph_table[fontsize]; - // ASCII というか半角というかは通常 0x7f までだが + wxBitmap **ascii_glyph = ascii_glyph_table[fontid]; + wxBitmap **bold_glyph = bold_glyph_table[fontid]; + // 半角文字領域 256 文字全部をキャッシュする。ASCII は 0x7f までだが // X680x0 の CGROM の 0x80, 0x81, 0x82 には '\\', '~', '|' が - // 収納されているのでここまで含めてデータを作っておく。 + // 収納されている。 + // また CGROM に収録されている半角ひらがなは使わないが、避けるのも + // 手間なのでここまで含めてデータを作っておく。 for (int ch = 0; ch < countof(ascii_glyph_table[0]); ch++) { const uint8 *ptr = base + ch * bytes; @@ -118,32 +111,32 @@ WXTextPanel::AllocFont(fontsize_t fontsi buf[i] = bitrev(src); } // その XBM データから1文字分のビットマップオブジェクトを作成 - ascii_glyph[ch] = new wxBitmap(buf, font_width, font_height, 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, font_width, font_height, 1); + bold_glyph[ch] = new wxBitmap(&buf[0], font_width, font_height, 1); } } // 最後の一人ならフォントを解放する /*static*/ void -WXTextPanel::FreeFont(fontsize_t fontsize) +WXTextPanel::FreeFont(FontId fontid) { std::unique_lock lock(global_font_mutex); - if (fontsize < 0 || fontsize >= FONT_MAX) { + if (fontid < 0 || fontid >= FontId::Max) { return; } - if (--font_refcnt[fontsize] != 0) { + if (--font_refcnt[fontid] != 0) { return; } - wxBitmap **ascii_glyph = ascii_glyph_table[fontsize]; - wxBitmap **bold_glyph = bold_glyph_table[fontsize]; + 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]; @@ -156,20 +149,26 @@ WXTextPanel::FreeFont(fontsize_t fontsiz } } +// テキスト色をデフォルトに設定する(戻す) +void +WXTextPanel::ResetTextColor() +{ + SetTextColor(*wxBLACK, BGPANEL); +} + // テキスト色を指定する void -WXTextPanel::SetTextColor(const wxColour& fg, const wxColour& bg, - const wxColour& dc) +WXTextPanel::SetTextColor(const wxColour& fg, const wxColour& bg) { text_fgcolor = fg; text_bgcolor = bg; - text_disable_color = dc; } // (px, py) を左上とする座標から Shift_JIS 文字列を属性 attr で描画する。 -// 文字列中で属性を変えることはできない。 -// キャンバスをはみ出すかどうかについてはこちらでは関知しない。 +// ただし attr のうちここで参照するのは EM (ボールド) かそうでないかのみ。 +// 描画色、背景色は事前に SetTextColor() で設定したもの。 // フォントサイズは現行のフォント。 +// キャンバスをはみ出すかどうかについてはこちらでは関知しない。 void WXTextPanel::DrawStringSJIS(wxDC& dc, int px, int py, const char *sjis, uint attr) @@ -177,7 +176,7 @@ WXTextPanel::DrawStringSJIS(wxDC& dc, in const uint8 *s; for (s = (const uint8 *)sjis; *s; ) { - if (__predict_true(*s < 128)) { + if (__predict_true(SJIS::IsHankaku(*s))) { // 半角 DrawChar1(dc, px, py, *s++, attr); px += font_width; @@ -192,15 +191,15 @@ WXTextPanel::DrawStringSJIS(wxDC& dc, in } } -// (px, py) を左上とする座標からASCII文字 code を属性 attr で描画する。 -// code は 0x80 未満。 +// (px, py) を左上とする座標から半角文字 code を属性 attr で描画する。 +// code は ASCII または半角カナ。 // フォントサイズは現行のフォント。 void WXTextPanel::DrawChar1(wxDC& dc, int px, int py, uint code, uint attr) { wxBitmap **glyph; - assert(code < 0x80); + assertmsg(code < 0x100, "code=0x%x", code); // ボールドかどうかでテーブルが別 if ((attr & TA::Em)) { @@ -223,7 +222,7 @@ WXTextPanel::DrawChar1(wxDC& dc, int px, code = 0x82; } } - DrawBitmap(dc, px, py, *glyph[code], attr); + DrawBitmap(dc, px, py, *glyph[code]); } // (px, py) を左上とする座標から漢字1文字を出力する。 @@ -232,44 +231,25 @@ WXTextPanel::DrawChar1(wxDC& dc, int px, void WXTextPanel::DrawChar2(wxDC& dc, int px, int py, uint sjiscode, uint attr) { - wxBitmap *kanji = KanjiBitmap(sjiscode, attr); - DrawBitmap(dc, px, py, *kanji, attr); - delete kanji; + std::unique_ptr kanji; + kanji.reset(KanjiBitmap(sjiscode, attr)); + DrawBitmap(dc, px, py, *kanji); } // (px, py) を左上とする座標に bitmap を描画する。 -// この DrawBitmap() は dc のペン色を設定して dc.DrawBitmap() を呼ぶ。 -// ちょっと紛らわしいので注意。 +// 前景色、背景色は事前に SetTextColor() で予約された色を使う。 void -WXTextPanel::DrawBitmap(wxDC& dc, int px, int py, const wxBitmap& bitmap, - uint attr) +WXTextPanel::DrawBitmap(wxDC& dc, int px, int py, const wxBitmap& bitmap) { // 文字の背景は透過ではなく背景色で塗りつぶす - // XXX もっと初期化時に近いところへ移してよさそうだが + // (そうしないと元あった文字の上に重なってしまう) + // XXX dc につき最初の1回だけでいいのだがほどよいところがない dc.SetBackgroundMode(wxSOLID); - // 属性を設定 - switch (attr) { - case TA::On: - dc.SetTextForeground(text_bgcolor); - dc.SetTextBackground(text_fgcolor); - break; - case TA::Disable: - dc.SetTextForeground(text_disable_color); - dc.SetTextBackground(text_bgcolor); - break; - default: - dc.SetTextForeground(text_fgcolor); - dc.SetTextBackground(text_bgcolor); - break; - } - - // 1文字出力 - dc.DrawBitmap(bitmap, px, py); - - // 1文字ずつ属性を元に戻す dc.SetTextForeground(text_fgcolor); dc.SetTextBackground(text_bgcolor); + + dc.DrawBitmap(bitmap, px, py); } // Shift_JIS の漢字1文字のビットマップを作成する。 @@ -310,18 +290,14 @@ WXTextPanel::KanjiBitmap(uint16 sjiscode // フォント領域 const uint8 *base; - switch (fontsize) { - case FONT_6x12: + switch (fontid) { + case FontId::_6x12: // 12x12 は内蔵フォントデータ base = builtin_kanji12; break; - case FONT_8x16: + case FontId::_8x16: // 16x16 は X680x0 CGROM のを使う。 - base = builtin_cgrom + 0; - break; - case FONT_12x24: - // 24x24 は X680x0 CGROM のを使う。 - base = builtin_cgrom + 0x40000; + base = BuiltinCGROM::Get16x16(); break; default: // ここより先に必ず半角のほうでエラーが出るはず @@ -329,7 +305,7 @@ WXTextPanel::KanjiBitmap(uint16 sjiscode } int bytes = (((font_width * 2) + 7) / 8) * font_height; - char buf[bytes]; + std::vector buf(bytes); const uint8 *ptr; ptr = base + code * bytes; @@ -344,12 +320,12 @@ WXTextPanel::KanjiBitmap(uint16 sjiscode } // その XBM データから1文字分のビットマップオブジェクトを作成 - return new wxBitmap(buf, font_width * 2, font_height, 1); + return new wxBitmap(&buf[0], font_width * 2, font_height, 1); } // スタティック変数 -fontsize_t WXTextPanel::global_fontsize; +FontId WXTextPanel::global_fontid; std::mutex WXTextPanel::global_font_mutex; -u_int WXTextPanel::font_refcnt[FONT_MAX]; -wxBitmap *WXTextPanel::ascii_glyph_table[FONT_MAX][0x83]; -wxBitmap *WXTextPanel::bold_glyph_table[FONT_MAX][0x83]; +u_int WXTextPanel::font_refcnt[FontId::Max]; +wxBitmap *WXTextPanel::ascii_glyph_table[FontId::Max][256]; +wxBitmap *WXTextPanel::bold_glyph_table[FontId::Max][256];