| 1 | // Copyright 2010-2023 Chris Spiegel. |
| 2 | // |
| 3 | // SPDX-License-Identifier: MIT |
| 4 | |
| 5 | #include <array> |
| 6 | #include <vector> |
| 7 | |
| 8 | #ifdef ZTERP_ICU |
| 9 | #include <unicode/uchar.h> |
| 10 | #endif |
| 11 | |
| 12 | #include "unicode.h" |
| 13 | #include "memory.h" |
| 14 | #include "options.h" |
| 15 | #include "types.h" |
| 16 | #include "util.h" |
| 17 | #include "zterp.h" |
| 18 | |
| 19 | #define MAX_UNICODE_TABLE_SIZE 97 |
| 20 | |
| 21 | // The index is the ZSCII value, minus 155 (so entry 0 refers to ZSCII |
| 22 | // value 155); and the value at the index is the Unicode character that |
| 23 | // the ZSCII value maps to. Because Latin-1 and Unicode are equivalent |
| 24 | // for 0–255, this table maps to both Unicode and Latin-1, with the |
| 25 | // caveat that values greater than 255 should be considered invalid in |
| 26 | // Latin-1, and are translated as a question mark below in |
| 27 | // setup_tables() where appropriate. |
| 28 | static std::vector<uint16_t> unicode_table = { |
| 29 | 0x00e4, 0x00f6, 0x00fc, 0x00c4, 0x00d6, 0x00dc, 0x00df, 0x00bb, 0x00ab, |
| 30 | 0x00eb, 0x00ef, 0x00ff, 0x00cb, 0x00cf, 0x00e1, 0x00e9, 0x00ed, 0x00f3, |
| 31 | 0x00fa, 0x00fd, 0x00c1, 0x00c9, 0x00cd, 0x00d3, 0x00da, 0x00dd, 0x00e0, |
| 32 | 0x00e8, 0x00ec, 0x00f2, 0x00f9, 0x00c0, 0x00c8, 0x00cc, 0x00d2, 0x00d9, |
| 33 | 0x00e2, 0x00ea, 0x00ee, 0x00f4, 0x00fb, 0x00c2, 0x00ca, 0x00ce, 0x00d4, |
| 34 | 0x00db, 0x00e5, 0x00c5, 0x00f8, 0x00d8, 0x00e3, 0x00f1, 0x00f5, 0x00c3, |
| 35 | 0x00d1, 0x00d5, 0x00e6, 0x00c6, 0x00e7, 0x00c7, 0x00fe, 0x00f0, 0x00de, |
| 36 | 0x00d0, 0x00a3, 0x0153, 0x0152, 0x00a1, 0x00bf, |
| 37 | }; |
| 38 | |
| 39 | // If a non-default Unicode table is found, this function is called with |
| 40 | // its address; it updates the Unicode table above. |
| 41 | void parse_unicode_table(uint16_t utable) |
| 42 | { |
| 43 | if (utable >= memory_size) { |
| 44 | die("corrupted story: unicode table out of range"); |
| 45 | } |
| 46 | |
| 47 | auto unicode_entries = byte(utable++); |
| 48 | if (unicode_entries > MAX_UNICODE_TABLE_SIZE) { |
| 49 | die("corrupted story: too many entries in the unicode table"); |
| 50 | } |
| 51 | if (utable + (2UL * unicode_entries) > memory_size) { |
| 52 | die("corrupted story: unicode table out of range"); |
| 53 | } |
| 54 | |
| 55 | unicode_table.resize(unicode_entries); |
| 56 | for (int i = 0; i < unicode_entries; i++) { |
| 57 | unicode_table[i] = word(utable + (2 * i)); |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | // Table used to convert a ZSCII value to Unicode; and since this is |
| 62 | // only used for output, non-output values will be returned as the |
| 63 | // Unicode replacement character. |
| 64 | std::array<uint16_t, UINT8_MAX + 1> zscii_to_unicode; |
| 65 | |
| 66 | // These tables translate a Unicode or Latin-1 character into its ZSCII |
| 67 | // equivalent. Only valid Unicode characters are translated (that is, |
| 68 | // those in the range 32–126, or 160 and above). These are meant for |
| 69 | // output, so do not translate delete and escape. |
| 70 | // |
| 71 | // The first table will translate invalid Unicode characters to zero; |
| 72 | // the second, to a question mark. |
| 73 | std::array<uint8_t, UINT16_MAX + 1UL> unicode_to_zscii; |
| 74 | std::array<uint8_t, UINT16_MAX + 1UL> unicode_to_zscii_q; |
| 75 | |
| 76 | // Convert ZSCII to Unicode line-drawing/rune characters. |
| 77 | std::array<uint16_t, UINT8_MAX + 1> zscii_to_font3; |
| 78 | |
| 79 | // Lookup table to see if a character is in the alphabet table. Key is |
| 80 | // the character, value is the index in the alphabet table, or -1. |
| 81 | std::array<int8_t, UINT8_MAX + 1> atable_pos; |
| 82 | |
| 83 | static void build_zscii_to_unicode_table() |
| 84 | { |
| 85 | zscii_to_unicode.fill(UNICODE_REPLACEMENT); |
| 86 | zscii_to_unicode[0] = 0; |
| 87 | zscii_to_unicode[ZSCII_NEWLINE] = UNICODE_LINEFEED; |
| 88 | |
| 89 | if (zversion == 6) { |
| 90 | zscii_to_unicode[ 9] = UNICODE_SPACE; // Tab. |
| 91 | zscii_to_unicode[11] = UNICODE_SPACE; // Sentence space. |
| 92 | } |
| 93 | |
| 94 | for (int i = 32; i < 127; i++) { |
| 95 | zscii_to_unicode[i] = i; |
| 96 | } |
| 97 | for (int i = 0; i < unicode_table.size(); i++) { |
| 98 | uint16_t c = unicode_table[i]; |
| 99 | |
| 100 | if (!valid_unicode(c)) { |
| 101 | c = UNICODE_REPLACEMENT; |
| 102 | } |
| 103 | |
| 104 | zscii_to_unicode[i + 155] = c; |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | static void build_unicode_to_zscii_tables() |
| 109 | { |
| 110 | // Default values. |
| 111 | unicode_to_zscii.fill(0); |
| 112 | unicode_to_zscii_q.fill(ZSCII_QUESTIONMARK); |
| 113 | |
| 114 | // First fill up the entries found in the Unicode table. |
| 115 | for (int i = 0; i < unicode_table.size(); i++) { |
| 116 | uint16_t c = unicode_table[i]; |
| 117 | |
| 118 | if (valid_unicode(c)) { |
| 119 | unicode_to_zscii [c] = i + 155; |
| 120 | unicode_to_zscii_q[c] = i + 155; |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | // Now the values that are equivalent in ZSCII and Unicode. |
| 125 | for (int i = 32; i < 127; i++) { |
| 126 | unicode_to_zscii [i] = i; |
| 127 | unicode_to_zscii_q[i] = i; |
| 128 | } |
| 129 | |
| 130 | // Properly translate a newline. |
| 131 | unicode_to_zscii [UNICODE_LINEFEED] = ZSCII_NEWLINE; |
| 132 | unicode_to_zscii_q[UNICODE_LINEFEED] = ZSCII_NEWLINE; |
| 133 | } |
| 134 | |
| 135 | // Not all fonts provide all characters, so there |
| 136 | // may well be a lot of question marks. |
| 137 | static void build_zscii_to_character_graphics_table() |
| 138 | { |
| 139 | zscii_to_font3.fill(UNICODE_REPLACEMENT); |
| 140 | |
| 141 | zscii_to_font3[ 32] = UNICODE_SPACE; |
| 142 | zscii_to_font3[ 33] = 0x2190; // ← |
| 143 | zscii_to_font3[ 34] = 0x2192; // → |
| 144 | zscii_to_font3[ 35] = 0x2571; // ╱ |
| 145 | zscii_to_font3[ 36] = 0x2572; // ╲ |
| 146 | zscii_to_font3[ 37] = UNICODE_SPACE; |
| 147 | zscii_to_font3[ 38] = 0x2500; // ─ |
| 148 | zscii_to_font3[ 39] = 0x2500; // ─ |
| 149 | zscii_to_font3[ 40] = 0x2502; // │ |
| 150 | zscii_to_font3[ 41] = 0x2502; // │ (this should be slightly offset, but whatever) |
| 151 | zscii_to_font3[ 42] = 0x2534; // ┴ |
| 152 | zscii_to_font3[ 43] = 0x252c; // ┬ |
| 153 | zscii_to_font3[ 44] = 0x251c; // ├ |
| 154 | zscii_to_font3[ 45] = 0x2524; // ┤ |
| 155 | zscii_to_font3[ 46] = 0x2514; // └ |
| 156 | zscii_to_font3[ 47] = 0x250c; // ┌ |
| 157 | zscii_to_font3[ 48] = 0x2510; // ┐ |
| 158 | zscii_to_font3[ 49] = 0x2518; // ┘ |
| 159 | |
| 160 | // There are a few characters that have no box-drawing equivalents. |
| 161 | // These are the pieces that have connections sticking out of them, |
| 162 | // used to link rooms together. There are two options: have filled |
| 163 | // boxes with no connections which makes the rooms look nice but the |
| 164 | // connections look bad, or unfilled boxes with connections which |
| 165 | // results in bad looking rooms but attached connections. The end |
| 166 | // result is something like this: |
| 167 | // |
| 168 | // No connections: Connections: |
| 169 | // ╲ ╱ ╲ ╱ |
| 170 | // ┌─┐ ▗▄▖ ╲─┐ ▗▄╱ |
| 171 | // │ ├─▐█▌─ │ ├─┤█├─ |
| 172 | // └─┘ ▝▀▘ └─┘ ▝┬▘ |
| 173 | // │ │ |
| 174 | // |
| 175 | // By default the former is done, but the latter can be chosen. |
| 176 | zscii_to_font3[ 50] = options.enable_alt_graphics ? 0x2571 : 0x2514; // ╱ or └ |
| 177 | zscii_to_font3[ 51] = options.enable_alt_graphics ? 0x2572 : 0x250c; // ╲ or ┌ |
| 178 | zscii_to_font3[ 52] = options.enable_alt_graphics ? 0x2571 : 0x2510; // ╱ or ┐ |
| 179 | zscii_to_font3[ 53] = options.enable_alt_graphics ? 0x2572 : 0x2518; // ╲ or ┘ |
| 180 | |
| 181 | zscii_to_font3[ 54] = 0x2588; // █ |
| 182 | zscii_to_font3[ 55] = 0x2580; // ▀ |
| 183 | zscii_to_font3[ 56] = 0x2584; // ▄ |
| 184 | zscii_to_font3[ 57] = 0x258c; // ▌ |
| 185 | zscii_to_font3[ 58] = 0x2590; // ▐ |
| 186 | |
| 187 | zscii_to_font3[ 59] = options.enable_alt_graphics ? 0x2534 : 0x2584; // ┴ or ▄ |
| 188 | zscii_to_font3[ 60] = options.enable_alt_graphics ? 0x252c : 0x2580; // ┬ or ▀ |
| 189 | zscii_to_font3[ 61] = options.enable_alt_graphics ? 0x251c : 0x258c; // ├ or ▌ |
| 190 | zscii_to_font3[ 62] = options.enable_alt_graphics ? 0x2524 : 0x2590; // ┤ or ▐ |
| 191 | |
| 192 | zscii_to_font3[ 63] = 0x259d; // ▝ |
| 193 | zscii_to_font3[ 64] = 0x2597; // ▗ |
| 194 | zscii_to_font3[ 65] = 0x2596; // ▖ |
| 195 | zscii_to_font3[ 66] = 0x2598; // ▘ |
| 196 | |
| 197 | zscii_to_font3[ 67] = options.enable_alt_graphics ? 0x2571 : 0x259d; // ╱ or ▝ |
| 198 | zscii_to_font3[ 68] = options.enable_alt_graphics ? 0x2572 : 0x2597; // ╲ or ▗ |
| 199 | zscii_to_font3[ 69] = options.enable_alt_graphics ? 0x2571 : 0x2596; // ╱ or ▖ |
| 200 | zscii_to_font3[ 70] = options.enable_alt_graphics ? 0x2572 : 0x2598; // ╲ or ▘ |
| 201 | |
| 202 | zscii_to_font3[ 75] = 0x2594; // ▔ |
| 203 | zscii_to_font3[ 76] = 0x2581; // ▁ |
| 204 | zscii_to_font3[ 77] = 0x258f; // ▏ |
| 205 | zscii_to_font3[ 78] = 0x2595; // ▕ |
| 206 | |
| 207 | zscii_to_font3[ 79] = UNICODE_SPACE; |
| 208 | zscii_to_font3[ 80] = 0x258f; // ▏ |
| 209 | zscii_to_font3[ 81] = 0x258e; // ▎ |
| 210 | zscii_to_font3[ 82] = 0x258d; // ▍ |
| 211 | zscii_to_font3[ 83] = 0x258c; // ▌ |
| 212 | zscii_to_font3[ 84] = 0x258b; // ▋ |
| 213 | zscii_to_font3[ 85] = 0x258a; // ▊ |
| 214 | zscii_to_font3[ 86] = 0x2589; // ▉ |
| 215 | zscii_to_font3[ 87] = 0x2588; // █ |
| 216 | zscii_to_font3[ 88] = 0x2595; // ▕ |
| 217 | zscii_to_font3[ 89] = 0x258f; // ▏ |
| 218 | |
| 219 | zscii_to_font3[ 90] = 0x2573; // ╳ |
| 220 | zscii_to_font3[ 91] = 0x253c; // ┼ |
| 221 | zscii_to_font3[ 92] = 0x2191; // ↑ |
| 222 | zscii_to_font3[ 93] = 0x2193; // ↓ |
| 223 | zscii_to_font3[ 94] = 0x2195; // ↕ |
| 224 | zscii_to_font3[ 95] = 0x2395; // ⎕ |
| 225 | zscii_to_font3[ 96] = 0x003f; // ? |
| 226 | zscii_to_font3[ 97] = 0x16aa; // ᚪ |
| 227 | zscii_to_font3[ 98] = 0x16d2; // ᛒ |
| 228 | zscii_to_font3[ 99] = 0x16c7; // ᛇ |
| 229 | zscii_to_font3[100] = 0x16de; // ᛞ |
| 230 | zscii_to_font3[101] = 0x16d6; // ᛖ |
| 231 | zscii_to_font3[102] = 0x16a0; // ᚠ |
| 232 | zscii_to_font3[103] = 0x16b7; // ᚷ |
| 233 | zscii_to_font3[104] = 0x16bb; // ᚻ |
| 234 | zscii_to_font3[105] = 0x16c1; // ᛁ |
| 235 | zscii_to_font3[106] = 0x16c4; // ᛄ |
| 236 | zscii_to_font3[107] = 0x16e6; // ᛦ |
| 237 | zscii_to_font3[108] = 0x16da; // ᛚ |
| 238 | zscii_to_font3[109] = 0x16d7; // ᛗ |
| 239 | zscii_to_font3[110] = 0x16be; // ᚾ |
| 240 | zscii_to_font3[111] = 0x16a9; // ᚩ |
| 241 | zscii_to_font3[112] = 0x15be; // ᖾ |
| 242 | zscii_to_font3[113] = 0x16b3; // ᚳ |
| 243 | zscii_to_font3[114] = 0x16b1; // ᚱ |
| 244 | zscii_to_font3[115] = 0x16cb; // ᛋ |
| 245 | zscii_to_font3[116] = 0x16cf; // ᛏ |
| 246 | zscii_to_font3[117] = 0x16a2; // ᚢ |
| 247 | zscii_to_font3[118] = 0x16e0; // ᛠ |
| 248 | zscii_to_font3[119] = 0x16b9; // ᚹ |
| 249 | zscii_to_font3[120] = 0x16c9; // ᛉ |
| 250 | zscii_to_font3[121] = 0x16a5; // ᚥ |
| 251 | zscii_to_font3[122] = 0x16df; // ᛟ |
| 252 | |
| 253 | // These are reversed (see §16); a slightly ugly hack in screen.cpp |
| 254 | // is used to accomplish this. |
| 255 | zscii_to_font3[123] = 0x2191; // ↑ |
| 256 | zscii_to_font3[124] = 0x2193; // ↓ |
| 257 | zscii_to_font3[125] = 0x2195; // ↕ |
| 258 | zscii_to_font3[126] = 0x003f; // ? |
| 259 | } |
| 260 | |
| 261 | static void build_alphabet_table() |
| 262 | { |
| 263 | atable_pos.fill(-1); |
| 264 | |
| 265 | // 52 is A2 character 6, which is special and should not |
| 266 | // be matched, so skip over it. |
| 267 | for (int i = 0; i < 52; i++) { |
| 268 | atable_pos[atable[i]] = i; |
| 269 | } |
| 270 | for (int i = 53; i < 26 * 3; i++) { |
| 271 | atable_pos[atable[i]] = i; |
| 272 | } |
| 273 | } |
| 274 | |
| 275 | void setup_tables() |
| 276 | { |
| 277 | build_zscii_to_unicode_table(); |
| 278 | build_unicode_to_zscii_tables(); |
| 279 | build_zscii_to_character_graphics_table(); |
| 280 | build_alphabet_table(); |
| 281 | } |
| 282 | |
| 283 | uint16_t unicode_tolower(uint16_t c) |
| 284 | { |
| 285 | #ifdef ZTERP_ICU |
| 286 | uint32_t lower = u_tolower(c); |
| 287 | |
| 288 | if (lower > UINT16_MAX) { |
| 289 | return UNICODE_REPLACEMENT; |
| 290 | } |
| 291 | |
| 292 | return lower; |
| 293 | #else |
| 294 | // This is adapted from Zip2000 (Copyright 2001 Kevin Bracey). |
| 295 | static const std::array<unsigned char, 0x100> basic_latin = { |
| 296 | 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, |
| 297 | 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, |
| 298 | 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f, |
| 299 | 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f, |
| 300 | 0x40, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f, |
| 301 | 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, 0x79, 0x7a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f, |
| 302 | 0x60, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f, |
| 303 | 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, 0x79, 0x7a, 0x7b, 0x7c, 0x7d, 0x7e, 0x7f, |
| 304 | 0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87, 0x88, 0x89, 0x8a, 0x8b, 0x8c, 0x8d, 0x8e, 0x8f, |
| 305 | 0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98, 0x99, 0x9a, 0x9b, 0x9c, 0x9d, 0x9e, 0x9f, |
| 306 | 0xa0, 0xa1, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7, 0xa8, 0xa9, 0xaa, 0xab, 0xac, 0xad, 0xae, 0xaf, |
| 307 | 0xb0, 0xb1, 0xb2, 0xb3, 0xb4, 0xb5, 0xb6, 0xb7, 0xb8, 0xb9, 0xba, 0xbb, 0xbc, 0xbd, 0xbe, 0xbf, |
| 308 | 0xe0, 0xe1, 0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7, 0xe8, 0xe9, 0xea, 0xeb, 0xec, 0xed, 0xee, 0xef, |
| 309 | 0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xd7, 0xf8, 0xf9, 0xfa, 0xfb, 0xfc, 0xfd, 0xfe, 0xdf, |
| 310 | 0xe0, 0xe1, 0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7, 0xe8, 0xe9, 0xea, 0xeb, 0xec, 0xed, 0xee, 0xef, |
| 311 | 0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8, 0xf9, 0xfa, 0xfb, 0xfc, 0xfd, 0xfe, 0xff |
| 312 | }; |
| 313 | static const std::array<unsigned char, 0x80> latin_extended_a = { |
| 314 | 0x01, 0x01, 0x03, 0x03, 0x05, 0x05, 0x07, 0x07, 0x09, 0x09, 0x0b, 0x0b, 0x0d, 0x0d, 0x0f, 0x0f, |
| 315 | 0x11, 0x11, 0x13, 0x13, 0x15, 0x15, 0x17, 0x17, 0x19, 0x19, 0x1b, 0x1b, 0x1d, 0x1d, 0x1f, 0x1f, |
| 316 | 0x21, 0x21, 0x23, 0x23, 0x25, 0x25, 0x27, 0x27, 0x29, 0x29, 0x2b, 0x2b, 0x2d, 0x2d, 0x2f, 0x2f, |
| 317 | 0x00, 0x31, 0x33, 0x33, 0x35, 0x35, 0x37, 0x37, 0x38, 0x3a, 0x3a, 0x3c, 0x3c, 0x3e, 0x3e, 0x40, |
| 318 | 0x40, 0x42, 0x42, 0x44, 0x44, 0x46, 0x46, 0x48, 0x48, 0x49, 0x4b, 0x4b, 0x4d, 0x4d, 0x4f, 0x4f, |
| 319 | 0x51, 0x51, 0x53, 0x53, 0x55, 0x55, 0x57, 0x57, 0x59, 0x59, 0x5b, 0x5b, 0x5d, 0x5d, 0x5f, 0x5f, |
| 320 | 0x61, 0x61, 0x63, 0x63, 0x65, 0x65, 0x67, 0x67, 0x69, 0x69, 0x6b, 0x6b, 0x6d, 0x6d, 0x6f, 0x6f, |
| 321 | 0x71, 0x71, 0x73, 0x73, 0x75, 0x75, 0x77, 0x77, 0x00, 0x7a, 0x7a, 0x7c, 0x7c, 0x7e, 0x7e, 0x7f |
| 322 | }; |
| 323 | static const std::array<unsigned char, 0x50> greek = { |
| 324 | 0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0xac, 0x87, 0xad, 0xae, 0xaf, 0x8b, 0xcc, 0x8d, 0xcd, 0xce, |
| 325 | 0x90, 0xb1, 0xb2, 0xb3, 0xb4, 0xb5, 0xb6, 0xb7, 0xb8, 0xb9, 0xba, 0xbb, 0xbc, 0xbd, 0xbe, 0xbf, |
| 326 | 0xc0, 0xc1, 0xa2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7, 0xc8, 0xc9, 0xca, 0xcb, 0xac, 0xad, 0xae, 0xaf, |
| 327 | 0xb0, 0xb1, 0xb2, 0xb3, 0xb4, 0xb5, 0xb6, 0xb7, 0xb8, 0xb9, 0xba, 0xbb, 0xbc, 0xbd, 0xbe, 0xbf, |
| 328 | 0xc0, 0xc1, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7, 0xc8, 0xc9, 0xca, 0xcb, 0xcc, 0xcd, 0xce, 0xcf |
| 329 | }; |
| 330 | static const std::array<unsigned char, 0x60> cyrillic = { |
| 331 | 0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f, |
| 332 | 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f, |
| 333 | 0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f, |
| 334 | 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f, |
| 335 | 0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f, |
| 336 | 0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f |
| 337 | }; |
| 338 | |
| 339 | if (c < 0x0100) { |
| 340 | c = basic_latin[c]; |
| 341 | } else if (c == 0x0130) { |
| 342 | c = 0x0069; // capital i with dot -> lower case i |
| 343 | } else if (c == 0x0178) { |
| 344 | c = 0x00ff; // capital y diaeresis -> lower case y diaeresis |
| 345 | } else if (c < 0x0180) { |
| 346 | c = latin_extended_a[c - 0x100] + 0x100; |
| 347 | } else if (c >= 0x380 && c < 0x3d0) { |
| 348 | c = greek[c - 0x380] + 0x300; |
| 349 | } else if (c >= 0x400 && c < 0x460) { |
| 350 | c = cyrillic[c - 0x400] + 0x400; |
| 351 | } |
| 352 | |
| 353 | return c; |
| 354 | #endif |
| 355 | } |
| 356 | |
| 357 | #ifdef ZTERP_GLK |
| 358 | // Convert a char (assumed to be ASCII) to Unicode. Printable values, |
| 359 | // plus newline, are converted to their corresponding Unicode values; |
| 360 | // the rest are converted to the Unicode replacement character (�). |
| 361 | // This is a function rather than a table because it is rarely used, so |
| 362 | // performance is not a concern. |
| 363 | uint16_t char_to_unicode(char c) |
| 364 | { |
| 365 | if (c == '\n') { |
| 366 | return UNICODE_LINEFEED; |
| 367 | } else if (c < 32 || c > 126) { |
| 368 | return UNICODE_REPLACEMENT; |
| 369 | } else { |
| 370 | return c; |
| 371 | } |
| 372 | } |
| 373 | #endif |
| 374 | |
| 375 | // Standard 1.1 notes that Unicode characters 0–31 and 127–159 |
| 376 | // are invalid due to the fact that they’re control codes. |
| 377 | bool valid_unicode(uint16_t c) |
| 378 | { |
| 379 | return (c >= 32 && c <= 126) || c >= 160; |
| 380 | } |
| 381 | |
| 382 | #ifdef ZTERP_OS_DOS |
| 383 | // Convert from Unicode to code page 437, used by DOS. |
| 384 | uint8_t unicode_to_437(uint16_t c) |
| 385 | { |
| 386 | switch (c) { |
| 387 | case 0x0020: return 32; case 0x0021: return 33; case 0x0022: return 34; case 0x0023: return 35; |
| 388 | case 0x0024: return 36; case 0x0025: return 37; case 0x0026: return 38; case 0x0027: return 39; |
| 389 | case 0x0028: return 40; case 0x0029: return 41; case 0x002A: return 42; case 0x002B: return 43; |
| 390 | case 0x002C: return 44; case 0x002D: return 45; case 0x002E: return 46; case 0x002F: return 47; |
| 391 | case 0x0030: return 48; case 0x0031: return 49; case 0x0032: return 50; case 0x0033: return 51; |
| 392 | case 0x0034: return 52; case 0x0035: return 53; case 0x0036: return 54; case 0x0037: return 55; |
| 393 | case 0x0038: return 56; case 0x0039: return 57; case 0x003A: return 58; case 0x003B: return 59; |
| 394 | case 0x003C: return 60; case 0x003D: return 61; case 0x003E: return 62; case 0x003F: return 63; |
| 395 | case 0x0040: return 64; case 0x0041: return 65; case 0x0042: return 66; case 0x0043: return 67; |
| 396 | case 0x0044: return 68; case 0x0045: return 69; case 0x0046: return 70; case 0x0047: return 71; |
| 397 | case 0x0048: return 72; case 0x0049: return 73; case 0x004A: return 74; case 0x004B: return 75; |
| 398 | case 0x004C: return 76; case 0x004D: return 77; case 0x004E: return 78; case 0x004F: return 79; |
| 399 | case 0x0050: return 80; case 0x0051: return 81; case 0x0052: return 82; case 0x0053: return 83; |
| 400 | case 0x0054: return 84; case 0x0055: return 85; case 0x0056: return 86; case 0x0057: return 87; |
| 401 | case 0x0058: return 88; case 0x0059: return 89; case 0x005A: return 90; case 0x005B: return 91; |
| 402 | case 0x005C: return 92; case 0x005D: return 93; case 0x005E: return 94; case 0x005F: return 95; |
| 403 | case 0x0060: return 96; case 0x0061: return 97; case 0x0062: return 98; case 0x0063: return 99; |
| 404 | case 0x0064: return 100; case 0x0065: return 101; case 0x0066: return 102; case 0x0067: return 103; |
| 405 | case 0x0068: return 104; case 0x0069: return 105; case 0x006A: return 106; case 0x006B: return 107; |
| 406 | case 0x006C: return 108; case 0x006D: return 109; case 0x006E: return 110; case 0x006F: return 111; |
| 407 | case 0x0070: return 112; case 0x0071: return 113; case 0x0072: return 114; case 0x0073: return 115; |
| 408 | case 0x0074: return 116; case 0x0075: return 117; case 0x0076: return 118; case 0x0077: return 119; |
| 409 | case 0x0078: return 120; case 0x0079: return 121; case 0x007A: return 122; case 0x007B: return 123; |
| 410 | case 0x007C: return 124; case 0x007D: return 125; case 0x007E: return 126; case 0x00A1: return 173; |
| 411 | case 0x00A2: return 155; case 0x00A3: return 156; case 0x00A5: return 157; case 0x00AA: return 166; |
| 412 | case 0x00AB: return 174; case 0x00AC: return 170; case 0x00B0: return 248; case 0x00B1: return 241; |
| 413 | case 0x00B2: return 253; case 0x00B5: return 230; case 0x00B7: return 250; case 0x00BA: return 167; |
| 414 | case 0x00BB: return 175; case 0x00BC: return 172; case 0x00BD: return 171; case 0x00BF: return 168; |
| 415 | case 0x00C4: return 142; case 0x00C5: return 143; case 0x00C6: return 146; case 0x00C7: return 128; |
| 416 | case 0x00C9: return 144; case 0x00D1: return 165; case 0x00D6: return 153; case 0x00DC: return 154; |
| 417 | case 0x00DF: return 225; case 0x00E0: return 133; case 0x00E1: return 160; case 0x00E2: return 131; |
| 418 | case 0x00E4: return 132; case 0x00E5: return 134; case 0x00E6: return 145; case 0x00E7: return 135; |
| 419 | case 0x00E8: return 138; case 0x00E9: return 130; case 0x00EA: return 136; case 0x00EB: return 137; |
| 420 | case 0x00EC: return 141; case 0x00ED: return 161; case 0x00EE: return 140; case 0x00EF: return 139; |
| 421 | case 0x00F1: return 164; case 0x00F2: return 149; case 0x00F3: return 162; case 0x00F4: return 147; |
| 422 | case 0x00F6: return 148; case 0x00F7: return 246; case 0x00F9: return 151; case 0x00FA: return 163; |
| 423 | case 0x00FB: return 150; case 0x00FC: return 129; case 0x00FF: return 152; case 0x0192: return 159; |
| 424 | case 0x0393: return 226; case 0x0398: return 233; case 0x03A3: return 228; case 0x03A6: return 232; |
| 425 | case 0x03A9: return 234; case 0x03B1: return 224; case 0x03B4: return 235; case 0x03B5: return 238; |
| 426 | case 0x03C0: return 227; case 0x03C3: return 229; case 0x03C4: return 231; case 0x03C6: return 237; |
| 427 | case 0x207F: return 252; case 0x20A7: return 158; case 0x2219: return 249; case 0x221A: return 251; |
| 428 | case 0x221E: return 236; case 0x2229: return 239; case 0x2248: return 247; case 0x2261: return 240; |
| 429 | case 0x2264: return 243; case 0x2265: return 242; case 0x2302: return 127; case 0x2310: return 169; |
| 430 | case 0x2320: return 244; case 0x2321: return 245; case 0x2500: return 196; case 0x2502: return 179; |
| 431 | case 0x250C: return 218; case 0x2510: return 191; case 0x2514: return 192; case 0x2518: return 217; |
| 432 | case 0x251C: return 195; case 0x2524: return 180; case 0x252C: return 194; case 0x2534: return 193; |
| 433 | case 0x253C: return 197; case 0x2550: return 205; case 0x2551: return 186; case 0x2552: return 213; |
| 434 | case 0x2553: return 214; case 0x2554: return 201; case 0x2555: return 184; case 0x2556: return 183; |
| 435 | case 0x2557: return 187; case 0x2558: return 212; case 0x2559: return 211; case 0x255A: return 200; |
| 436 | case 0x255B: return 190; case 0x255C: return 189; case 0x255D: return 188; case 0x255E: return 198; |
| 437 | case 0x255F: return 199; case 0x2560: return 204; case 0x2561: return 181; case 0x2562: return 182; |
| 438 | case 0x2563: return 185; case 0x2564: return 209; case 0x2565: return 210; case 0x2566: return 203; |
| 439 | case 0x2567: return 207; case 0x2568: return 208; case 0x2569: return 202; case 0x256A: return 216; |
| 440 | case 0x256B: return 215; case 0x256C: return 206; case 0x2580: return 223; case 0x2584: return 220; |
| 441 | case 0x2588: return 219; case 0x258C: return 221; case 0x2590: return 222; case 0x2591: return 176; |
| 442 | case 0x2592: return 177; case 0x2593: return 178; case 0x25A0: return 254; |
| 443 | |
| 444 | case UNICODE_LINEFEED: |
| 445 | return 10; |
| 446 | |
| 447 | // Question mark. |
| 448 | default: |
| 449 | return 63; |
| 450 | } |
| 451 | } |
| 452 | #endif |