// SPDX-License-Identifier: MIT // SPDX-FileCopyrightText: 2023 SASANO Takayoshi #include #include "conv.h" extern uint16_t ucs2euc[65536]; extern wchar_t zh[]; extern wchar_t ja[]; // half width character, U+FF5E〜 (U+FF5F, U+FF60 unimplemented) // reference: https://www.asahi-net.or.jp/~ax2s-kmtn/ref/unicode/uff00.html static uint16_t ucshalf2full[] = { 0x301C, 0x0000, 0x0000, 0x3002, 0x300C, 0x300D, 0x3001, 0x30FB, 0x30F2, 0x30A1, 0x30A3, 0x30A5, 0x30A7, 0x30A9, 0x30E3, 0x30E5, 0x30E7, 0x30C3, 0x30FC, 0x30A2, 0x30A4, 0x30A6, 0x30A8, 0x30AA, 0x30AB, 0x30AD, 0x30AF, 0x30B1, 0x30B3, 0x30B5, 0x30B7, 0x30B9, 0x30BB, 0x30BD, 0x30BF, 0x30C1, 0x30C4, 0x30C6, 0x30C8, 0x30CA, 0x30CB, 0x30CC, 0x30CD, 0x30CE, 0x30CF, 0x30D2, 0x30D5, 0x30D8, 0x30DB, 0x30DE, 0x30DF, 0x30E0, 0x30E1, 0x30E2, 0x30E4, 0x30E6, 0x30E8, 0x30E9, 0x30EA, 0x30EB, 0x30EC, 0x30ED, 0x30EF, 0x30F3, 0x3099, 0x309A, }; #define UCS2EUC_SIZE (sizeof(ucs2euc) / sizeof(uint16_t)) #define UCSHALF2FULL_SIZE (sizeof(ucshalf2full) / sizeof(uint16_t)) wchar_t convert_ja_zh(wchar_t wc) { wchar_t *p; if ((p = wcschr(ja, wc)) != NULL) return zh[p - ja]; else return wc; } size_t convert_euc(uint8_t *euc, wchar_t *wc) { size_t i; wchar_t c; i = 0; for (; *wc; wc++) { c = *wc; if (c >= UCS2EUC_SIZE) c = 0x3013; // '〓' in UCS else if (c >= 0xff5e && c < 0xff5e + UCSHALF2FULL_SIZE) c = ucshalf2full[c - 0xff5e]; // hankaku -> zenkaku switch (c) { case '(': case ')': // avoid ')', end of LCD command c = 0xff08 + (c - '('); // '(' in UCS /*FALLTHROUGH*/ case 0x80 ... UCS2EUC_SIZE: switch (c) { case 0x30fc: // '―' in GB2312 (no 'ー') c = 0xa1aa; break; case 0x3099: c = 0xa1a7; // '¨' in GB2312 (no '゛') break; case 0x309a: c = 0xa1e3; // '°' in GB2312 (no '゜') break; default: c = ucs2euc[convert_ja_zh(c)]; if (!c) c = 0xa1fe; // '〓' in GB2312 break; } if (euc != NULL) { euc[i] = c >> 8; euc[i + 1] = c; } i += 2; break; default: if (euc != NULL) euc[i] = c; i++; break; } } if (euc != NULL) euc[i] = 0; i++; return i; }