--- nono/wx/wxtextpanel.cpp 2026/04/29 17:04:28 1.1 +++ nono/wx/wxtextpanel.cpp 2026/04/29 17:04:39 1.1.1.4 @@ -1,13 +1,14 @@ // // 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" +#include "cgrom.h" +#include "kaname12.h" // wxOSX(-3.0.2) の wxLIGHT_GREY は値が(色も透明度も)正しくない。 // wxGTK の wxLIGHT_GREY は 0xffd3d3d3 だけどこれを Mac で見ると @@ -22,22 +23,18 @@ // テキストフォントを管理するクラス // -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, ""); // コンストラクタ WXTextPanel::WXTextPanel(wxWindow *parent) : inherited(parent, wxID_ANY) { - UpdateFont(global_fontsize); + UpdateFont(global_fontid); // デフォルト色 SetTextColor(*wxBLACK, LIGHT_GREY, wxColour(0x50, 0x50, 0x50)); @@ -46,65 +43,60 @@ WXTextPanel::WXTextPanel(wxWindow *paren // デストラクタ 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]; + wxBitmap **ascii_glyph = ascii_glyph_table[fontid]; + wxBitmap **bold_glyph = bold_glyph_table[fontid]; // ASCII というか半角というかは通常 0x7f までだが // X680x0 の CGROM の 0x80, 0x81, 0x82 には '\\', '~', '|' が // 収納されているのでここまで含めてデータを作っておく。 @@ -118,32 +110,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]; @@ -177,10 +169,14 @@ WXTextPanel::DrawStringSJIS(wxDC& dc, in const uint8 *s; for (s = (const uint8 *)sjis; *s; ) { - if (__predict_true(*s < 128)) { - // 半角 + if (__predict_true(*s < 0x80)) { + // ASCII DrawChar1(dc, px, py, *s++, attr); px += font_width; + } else if (0xa0 <= *s && *s < 0xe0) { + // 半角カナ + DrawCharKana(dc, px, py, *s++, attr); + px += font_width; } else { // 全角 int code; @@ -200,7 +196,7 @@ WXTextPanel::DrawChar1(wxDC& dc, int px, { wxBitmap **glyph; - assert(code < 0x80); + assertmsg(code < 0x80, "code=0x%x", code); // ボールドかどうかでテーブルが別 if ((attr & TA::Em)) { @@ -226,15 +222,25 @@ WXTextPanel::DrawChar1(wxDC& dc, int px, DrawBitmap(dc, px, py, *glyph[code], attr); } +// (px, py) を左上とする座標から半角カナ1文字を出力する。 +// フォントサイズは現行のフォント。 +void +WXTextPanel::DrawCharKana(wxDC& dc, int px, int py, uint sjiscode, uint attr) +{ + std::unique_ptr kana; + kana.reset(KanaBitmap(sjiscode, attr)); + DrawBitmap(dc, px, py, *kana, attr); +} + // (px, py) を左上とする座標から漢字1文字を出力する。 // sjiscode は Shift_JIS。 // フォントサイズは現行のフォント。 void WXTextPanel::DrawChar2(wxDC& dc, int px, int py, uint sjiscode, uint attr) { - wxBitmap *kanji = KanjiBitmap(sjiscode, attr); + std::unique_ptr kanji; + kanji.reset(KanjiBitmap(sjiscode, attr)); DrawBitmap(dc, px, py, *kanji, attr); - delete kanji; } // (px, py) を左上とする座標に bitmap を描画する。 @@ -272,6 +278,42 @@ WXTextPanel::DrawBitmap(wxDC& dc, int px dc.SetTextBackground(text_bgcolor); } +// Shift_JIS の半角カナ1文字分のビットマップを作成する。 +wxBitmap * +WXTextPanel::KanaBitmap(uint16 sjiscode, uint attr) +{ + const uint8 *base; + // X680x0 CGROM + switch (fontid) { + case FontId::_6x12: + base = BuiltinCGROM::Get6x12(); + break; + case FontId::_8x16: + base = BuiltinCGROM::Get8x16(); + break; + default: + __unreachable(); + } + + int bytes = ((font_width + 7) / 8) * font_height; + std::vector buf(bytes); + + const uint8 *ptr; + ptr = base + sjiscode * 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, font_height, 1); +} + // Shift_JIS の漢字1文字のビットマップを作成する。 wxBitmap * WXTextPanel::KanjiBitmap(uint16 sjiscode, uint attr) @@ -310,18 +352,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 +367,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 +382,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][0x83]; +wxBitmap *WXTextPanel::bold_glyph_table[FontId::Max][0x83];