|
| 1 | +#include "../codeeditor.h" |
| 2 | +#include "interpret_as_iso_2022_jp.h" |
| 3 | +#include <QFile> |
| 4 | +#include <QDebug> |
| 5 | + |
| 6 | +// Singleton instance |
| 7 | +Interpret_As_ISO_2022_JP& Interpret_As_ISO_2022_JP::instance() { |
| 8 | + static Interpret_As_ISO_2022_JP instance; |
| 9 | + return instance; |
| 10 | +} |
| 11 | + |
| 12 | +// Extended JIS Table for ISO-2022-JP (JIS X 0208 mapping) |
| 13 | +const std::unordered_map<QByteArray, QChar> Interpret_As_ISO_2022_JP::jisTable = { |
| 14 | + {QByteArray("\x24\x21"), QChar(0x3000)}, // Ideographic space |
| 15 | + {QByteArray("\x24\x22"), QChar(0x3001)}, // 。 (period) |
| 16 | + {QByteArray("\x24\x23"), QChar(0x3002)}, // 、 (comma) |
| 17 | + {QByteArray("\x24\x3C"), QChar(0x3093)}, // ん (n) |
| 18 | + {QByteArray("\x24\x35"), QChar(0x3061)}, // ち (chi) |
| 19 | + {QByteArray("\x24\x25"), QChar(0x4E00)}, // 一 (one) |
| 20 | + {QByteArray("\x24\x26"), QChar(0x4E8C)}, // 二 (two) |
| 21 | + {QByteArray("\x24\x27"), QChar(0x4E09)}, // 三 (three) |
| 22 | + {QByteArray("\x24\x28"), QChar(0x56DB)}, // 四 (four) |
| 23 | + {QByteArray("\x24\x29"), QChar(0x4E94)}, // 五 (five) |
| 24 | + {QByteArray("\x24\x2A"), QChar(0x516D)}, // 六 (six) |
| 25 | + {QByteArray("\x24\x2B"), QChar(0x4E03)}, // 七 (seven) |
| 26 | + {QByteArray("\x24\x2C"), QChar(0x516B)}, // 八 (eight) |
| 27 | + {QByteArray("\x24\x2D"), QChar(0x4E5D)}, // 九 (nine) |
| 28 | + {QByteArray("\x24\x2E"), QChar(0x5341)}, // 十 (ten) |
| 29 | + {QByteArray("\x24\x50"), QChar(0x65E5)}, // 日 (sun, day) |
| 30 | + {QByteArray("\x24\x51"), QChar(0x6708)}, // 月 (moon, month) |
| 31 | + {QByteArray("\x24\x52"), QChar(0x706B)}, // 火 (fire, Tuesday) |
| 32 | + {QByteArray("\x24\x53"), QChar(0x6C34)}, // 水 (water, Wednesday) |
| 33 | + {QByteArray("\x24\x54"), QChar(0x6728)}, // 木 (tree, Thursday) |
| 34 | + {QByteArray("\x24\x55"), QChar(0x91D1)}, // 金 (gold, Friday) |
| 35 | + {QByteArray("\x24\x56"), QChar(0x571F)}, // 土 (earth, Saturday) |
| 36 | + {QByteArray("\x24\x30"), QChar(0x3042)}, // あ (a) |
| 37 | + {QByteArray("\x24\x31"), QChar(0x3044)}, // い (i) |
| 38 | + {QByteArray("\x24\x32"), QChar(0x306B)}, // に (ni) |
| 39 | + {QByteArray("\x24\x33"), QChar(0x3053)}, // こ (ko) |
| 40 | +}; |
| 41 | + |
| 42 | +// Constructor |
| 43 | +Interpret_As_ISO_2022_JP::Interpret_As_ISO_2022_JP() {} |
| 44 | + |
| 45 | +// Main execution function |
| 46 | +void Interpret_As_ISO_2022_JP::execute(QPlainTextEdit* editor) { |
| 47 | + if (!editor) { |
| 48 | + qWarning() << "[ERROR] No editor instance provided."; |
| 49 | + return; |
| 50 | + } |
| 51 | + |
| 52 | + CodeEditor* codeEditor = qobject_cast<CodeEditor*>(editor); |
| 53 | + if (!codeEditor) { |
| 54 | + qWarning() << "[ERROR] Invalid CodeEditor instance."; |
| 55 | + return; |
| 56 | + } |
| 57 | + |
| 58 | + QString filePath = codeEditor->filePath(); |
| 59 | + if (filePath.isEmpty()) { |
| 60 | + qWarning() << "[ERROR] No file path associated with the editor."; |
| 61 | + return; |
| 62 | + } |
| 63 | + |
| 64 | + QFile file(filePath); |
| 65 | + if (!file.open(QIODevice::ReadOnly)) { |
| 66 | + qWarning() << "[ERROR] Cannot open file:" << filePath; |
| 67 | + return; |
| 68 | + } |
| 69 | + |
| 70 | + QByteArray rawData = file.readAll(); |
| 71 | + file.close(); |
| 72 | + |
| 73 | + QString decodedText = decodeISO2022JP(rawData); |
| 74 | + editor->setPlainText(decodedText); |
| 75 | + qDebug() << "[DEBUG] ISO-2022-JP Decoding applied for file:" << filePath; |
| 76 | +} |
| 77 | + |
| 78 | +// Decode raw data from ISO-2022-JP |
| 79 | +QString Interpret_As_ISO_2022_JP::decodeISO2022JP(const QByteArray& rawData) { |
| 80 | + QString result; |
| 81 | + QByteArray buffer; |
| 82 | + bool inKanjiMode = false; |
| 83 | + |
| 84 | + for (int i = 0; i < rawData.size(); ++i) { |
| 85 | + if (rawData.mid(i, 3) == "\x1B\x24\x42") { |
| 86 | + inKanjiMode = true; |
| 87 | + i += 2; |
| 88 | + } else if (rawData.mid(i, 3) == "\x1B\x28\x42") { |
| 89 | + inKanjiMode = false; |
| 90 | + i += 2; |
| 91 | + } else if (inKanjiMode) { |
| 92 | + buffer.append(rawData[i]); |
| 93 | + if (buffer.size() == 2) { |
| 94 | + result.append(jisTable.contains(buffer) ? jisTable.at(buffer) : QChar(0xFFFD)); |
| 95 | + buffer.clear(); |
| 96 | + } |
| 97 | + } else { |
| 98 | + result.append(QChar(rawData[i] & 0xFF)); |
| 99 | + } |
| 100 | + } |
| 101 | + return result; |
| 102 | +} |
0 commit comments