diff options
-rw-r--r-- | main.c | 38 |
1 files changed, 23 insertions, 15 deletions
@@ -37,6 +37,22 @@ static void usage(int argc, char **argv, char *message, ...) { exit(1); } +static inline size_t utf8_encode(uint8_t *buf, uint16_t codepoint) { + if ((codepoint & 0x007f) == codepoint) { + buf[0] = codepoint & 0xff; + return 1; + } else if ((codepoint & 0x07ff) == codepoint) { + buf[0] = (codepoint & 0x07c0) >> 6; + buf[1] = codepoint & 0x003f; + return 2; + } else if ((codepoint & 0xffff) == codepoint) { + buf[0] = (codepoint & 0xf000) >> 12; + buf[1] = (codepoint & 0x0fc0) >> 6; + buf[2] = codepoint & 0x003f; + return 3; + } +} + static ssize_t dump_line(off_t offset, void *buf, size_t len) { size_t i; @@ -69,23 +85,15 @@ static ssize_t dump_line(off_t offset, void *buf, size_t len) { for (i=0; i<ZXDUMP_STRIDE_LINE; i++) { uint8_t c = ((uint8_t *)buf)[offset+i]; - if (c <= ZXDUMP_CHARSET_LEN) { - uint16_t printable = charset[c]; + if (c < ZXDUMP_CHARSET_LEN) { + uint16_t codepoint = charset[c]; + uint8_t sequence[4]; + + size_t len = utf8_encode(sequence, codepoint); /* XXX: Implement support for inverse colours */ - if (printable & 0xf800) { - uint8_t sequence[2] = { - 0xc0 | ((printable & 0x1f00) >> 8), - 0x80 | (printable & 0x003f) - }; - - if (fwrite(sequence, 2, 1, stdout) < 1) { - goto error_io; - } - } else { - if (putchar((uint8_t)(printable & 0x00ff)) < 0) { - goto error_io; - } + if (fwrite(sequence, len, 1, stdout) < 1) { + goto error_io; } } else { if (putchar('.') < 0) { |