diff --git a/common/msg.c b/common/msg.c index cbb06e9e97..b231986235 100644 --- a/common/msg.c +++ b/common/msg.c @@ -25,14 +25,13 @@ #include "mpv_talloc.h" -#include "misc/bstr.h" #include "common/common.h" #include "common/global.h" -#include "misc/bstr.h" +#include "misc/codepoint_width.h" #include "options/options.h" #include "options/path.h" -#include "osdep/terminal.h" #include "osdep/io.h" +#include "osdep/terminal.h" #include "osdep/threads.h" #include "osdep/timer.h" @@ -374,37 +373,6 @@ static bool test_terminal_level(struct mp_log *log, int lev) !(lev == MSGL_STATUS && terminal_in_background()); } -// This is very basic way to infer needed width for a string. -static int term_disp_width(bstr str) -{ - int width = 0; - - while (str.len) { - if (bstr_eatstart0(&str, "\033[")) { - while (str.len && !((*str.start >= '@' && *str.start <= '~') || *str.start == 'm')) - str = bstr_cut(str, 1); - str = bstr_cut(str, 1); - continue; - } - - bstr code = bstr_split_utf8(str, &str); - if (code.len == 0) - return 0; - - if (code.len == 1 && *code.start == '\n') - continue; - - // Only single-width characters are supported - width++; - - // Assume that everything before \r should be discarded for simplicity - if (code.len == 1 && *code.start == '\r') - width = 0; - } - - return width; -} - static void append_terminal_line(struct mp_log *log, int lev, bstr text, bstr *term_msg, int *line_w) { diff --git a/meson.build b/meson.build index 67b7b3d107..b4d6af5428 100644 --- a/meson.build +++ b/meson.build @@ -134,6 +134,7 @@ sources = files( ## Misc 'misc/bstr.c', 'misc/charset_conv.c', + 'misc/codepoint_width.c', 'misc/dispatch.c', 'misc/io_utils.c', 'misc/json.c', diff --git a/misc/codepoint_width.c b/misc/codepoint_width.c new file mode 100644 index 0000000000..2d894028e0 --- /dev/null +++ b/misc/codepoint_width.c @@ -0,0 +1,737 @@ +#include "codepoint_width.h" + +#include +#include + +typedef uint_least32_t char32_t; + +// Generated by GraphemeTableGen (https://github.com/microsoft/terminal/blob/a7e47b711a2adc7b9e80eddea8168089f7d3b11e/src/tools/GraphemeTableGen/Program.cs) +// on 2024-09-21T14:12:16Z, from Unicode 16.0.0, 9285 bytes +// Copyright (c) Microsoft Corporation. +// Licensed under the MIT license. + +// Manually adjusted for C compatibility +// To generate: +// 0. Install .NET 8.0 +// 1. Download the tool from: https://github.com/microsoft/terminal/tree/main/src/tools/GraphemeTableGen +// 2. Download the Unicode data from: https://www.unicode.org/Public/UCD/latest/ucdxml/ucd.nounihan.grouped.zip +// 3. Build the project: `dotnet build GraphemeTableGen.csproj` +// 4. Run the tool: `./GraphemeTableGen ucd.nounihan.grouped.xml` +// 5. Copy the generated code and adjust it for C compatibility by replacing `constexpr` and `auto` keywords. + +// clang-format off +static const uint16_t s_stage0[] = { + 0x0000, 0x0020, 0x0040, 0x0060, 0x0080, 0x009f, 0x00bf, 0x00ca, 0x00ca, 0x00ca, 0x00ca, 0x00ca, 0x00ca, 0x00ca, 0x00ca, 0x00ca, + 0x00ca, 0x00ca, 0x00ca, 0x00ca, 0x00d8, 0x00f8, 0x010a, 0x010e, 0x010b, 0x0108, 0x0113, 0x0133, 0x0153, 0x0153, 0x0153, 0x016f, + 0x018f, 0x01a7, 0x01c7, 0x01e7, 0x0133, 0x0133, 0x0205, 0x0133, 0x0133, 0x0133, 0x0133, 0x0133, 0x0221, 0x0236, 0x00ca, 0x00ca, + 0x0256, 0x0276, 0x0133, 0x0133, 0x0133, 0x028b, 0x02ab, 0x02b9, 0x0133, 0x02cc, 0x02ea, 0x0302, 0x0322, 0x033f, 0x035f, 0x037f, + 0x00ca, 0x00ca, 0x00ca, 0x00ca, 0x00ca, 0x00ca, 0x00ca, 0x00ca, 0x00ca, 0x00ca, 0x00ca, 0x00ca, 0x00ca, 0x00ca, 0x00ca, 0x00ca, + 0x00ca, 0x00ca, 0x00ca, 0x00ca, 0x00ca, 0x00ca, 0x00ca, 0x00ca, 0x00ca, 0x00ca, 0x00ca, 0x00ca, 0x00ca, 0x00ca, 0x00ca, 0x039f, + 0x00ca, 0x00ca, 0x00ca, 0x00ca, 0x00ca, 0x00ca, 0x00ca, 0x00ca, 0x00ca, 0x00ca, 0x00ca, 0x00ca, 0x00ca, 0x00ca, 0x00ca, 0x00ca, + 0x00ca, 0x00ca, 0x00ca, 0x00ca, 0x00ca, 0x00ca, 0x00ca, 0x00ca, 0x00ca, 0x00ca, 0x00ca, 0x00ca, 0x00ca, 0x00ca, 0x00ca, 0x039f, + 0x0133, 0x0133, 0x0133, 0x0133, 0x0133, 0x0133, 0x0133, 0x0133, 0x0133, 0x0133, 0x0133, 0x0133, 0x0133, 0x0133, 0x0133, 0x0133, + 0x0133, 0x0133, 0x0133, 0x0133, 0x0133, 0x0133, 0x0133, 0x0133, 0x0133, 0x0133, 0x0133, 0x0133, 0x0133, 0x0133, 0x0133, 0x0133, + 0x0133, 0x0133, 0x0133, 0x0133, 0x0133, 0x0133, 0x0133, 0x0133, 0x0133, 0x0133, 0x0133, 0x0133, 0x0133, 0x0133, 0x0133, 0x0133, + 0x0133, 0x0133, 0x0133, 0x0133, 0x0133, 0x0133, 0x0133, 0x0133, 0x0133, 0x0133, 0x0133, 0x0133, 0x0133, 0x0133, 0x0133, 0x0133, + 0x0133, 0x0133, 0x0133, 0x0133, 0x0133, 0x0133, 0x0133, 0x0133, 0x0133, 0x0133, 0x0133, 0x0133, 0x0133, 0x0133, 0x0133, 0x0133, + 0x0133, 0x0133, 0x0133, 0x0133, 0x0133, 0x0133, 0x0133, 0x0133, 0x0133, 0x0133, 0x0133, 0x0133, 0x0133, 0x0133, 0x0133, 0x0133, + 0x0133, 0x0133, 0x0133, 0x0133, 0x0133, 0x0133, 0x0133, 0x0133, 0x0133, 0x0133, 0x0133, 0x0133, 0x0133, 0x0133, 0x0133, 0x0133, + 0x0133, 0x0133, 0x0133, 0x0133, 0x0133, 0x0133, 0x0133, 0x0133, 0x0133, 0x0133, 0x0133, 0x0133, 0x0133, 0x0133, 0x0133, 0x0133, + 0x0133, 0x0133, 0x0133, 0x0133, 0x0133, 0x0133, 0x0133, 0x0133, 0x0133, 0x0133, 0x0133, 0x0133, 0x0133, 0x0133, 0x0133, 0x0133, + 0x0133, 0x0133, 0x0133, 0x0133, 0x0133, 0x0133, 0x0133, 0x0133, 0x0133, 0x0133, 0x0133, 0x0133, 0x0133, 0x0133, 0x0133, 0x0133, + 0x0133, 0x0133, 0x0133, 0x0133, 0x0133, 0x0133, 0x0133, 0x0133, 0x0133, 0x0133, 0x0133, 0x0133, 0x0133, 0x0133, 0x0133, 0x0133, + 0x0133, 0x0133, 0x0133, 0x0133, 0x0133, 0x0133, 0x0133, 0x0133, 0x0133, 0x0133, 0x0133, 0x0133, 0x0133, 0x0133, 0x0133, 0x0133, + 0x0133, 0x0133, 0x0133, 0x0133, 0x0133, 0x0133, 0x0133, 0x0133, 0x0133, 0x0133, 0x0133, 0x0133, 0x0133, 0x0133, 0x0133, 0x0133, + 0x0133, 0x0133, 0x0133, 0x0133, 0x0133, 0x0133, 0x0133, 0x0133, 0x0133, 0x0133, 0x0133, 0x0133, 0x0133, 0x0133, 0x0133, 0x0133, + 0x0133, 0x0133, 0x0133, 0x0133, 0x0133, 0x0133, 0x0133, 0x0133, 0x0133, 0x0133, 0x0133, 0x0133, 0x0133, 0x0133, 0x0133, 0x0133, + 0x0133, 0x0133, 0x0133, 0x0133, 0x0133, 0x0133, 0x0133, 0x0133, 0x0133, 0x0133, 0x0133, 0x0133, 0x0133, 0x0133, 0x0133, 0x0133, + 0x0133, 0x0133, 0x0133, 0x0133, 0x0133, 0x0133, 0x0133, 0x0133, 0x0133, 0x0133, 0x0133, 0x0133, 0x0133, 0x0133, 0x0133, 0x0133, + 0x0133, 0x0133, 0x0133, 0x0133, 0x0133, 0x0133, 0x0133, 0x0133, 0x0133, 0x0133, 0x0133, 0x0133, 0x0133, 0x0133, 0x0133, 0x0133, + 0x0133, 0x0133, 0x0133, 0x0133, 0x0133, 0x0133, 0x0133, 0x0133, 0x0133, 0x0133, 0x0133, 0x0133, 0x0133, 0x0133, 0x0133, 0x0133, + 0x0133, 0x0133, 0x0133, 0x0133, 0x0133, 0x0133, 0x0133, 0x0133, 0x0133, 0x0133, 0x0133, 0x0133, 0x0133, 0x0133, 0x0133, 0x0133, + 0x03bf, 0x03c7, 0x0133, 0x0133, 0x0133, 0x0133, 0x0133, 0x0133, 0x0133, 0x0133, 0x0133, 0x0133, 0x0133, 0x0133, 0x0133, 0x0133, + 0x0133, 0x0133, 0x0133, 0x0133, 0x0133, 0x0133, 0x0133, 0x0133, 0x0133, 0x0133, 0x0133, 0x0133, 0x0133, 0x0133, 0x0133, 0x0133, + 0x0153, 0x0153, 0x0153, 0x0153, 0x0153, 0x0153, 0x0153, 0x0153, 0x0153, 0x0153, 0x0153, 0x0153, 0x0153, 0x0153, 0x0153, 0x0153, + 0x0153, 0x0153, 0x0153, 0x0153, 0x0153, 0x0153, 0x0153, 0x0153, 0x0153, 0x0153, 0x0153, 0x0153, 0x0153, 0x0153, 0x0153, 0x03e7, + 0x0153, 0x0153, 0x0153, 0x0153, 0x0153, 0x0153, 0x0153, 0x0153, 0x0153, 0x0153, 0x0153, 0x0153, 0x0153, 0x0153, 0x0153, 0x0153, + 0x0153, 0x0153, 0x0153, 0x0153, 0x0153, 0x0153, 0x0153, 0x0153, 0x0153, 0x0153, 0x0153, 0x0153, 0x0153, 0x0153, 0x0153, 0x03e7, +}; +static const uint16_t s_stage1[] = { + 0x0000, 0x0004, 0x000c, 0x0014, 0x001c, 0x0024, 0x002a, 0x0031, 0x002a, 0x0037, 0x002a, 0x003f, 0x0047, 0x0049, 0x004f, 0x0057, 0x005f, 0x0065, 0x006d, 0x002a, 0x002a, 0x002a, 0x0073, 0x007b, 0x0083, 0x008a, 0x002a, 0x0091, 0x0098, 0x009f, 0x00a3, 0x00aa, + 0x00b2, 0x00b8, 0x00be, 0x00c5, 0x00cd, 0x00d5, 0x00dd, 0x00e5, 0x00ed, 0x00f5, 0x00fd, 0x0105, 0x010d, 0x0115, 0x011d, 0x0125, 0x012d, 0x0135, 0x013d, 0x0145, 0x014d, 0x0155, 0x015d, 0x0164, 0x016b, 0x0173, 0x0175, 0x017d, 0x0182, 0x018a, 0x0192, 0x019a, + 0x019d, 0x01a5, 0x01ad, 0x002a, 0x01b5, 0x01b9, 0x01bd, 0x01c2, 0x002a, 0x002a, 0x002a, 0x002a, 0x002a, 0x01ca, 0x002a, 0x002a, 0x002a, 0x002a, 0x002a, 0x002a, 0x002a, 0x002a, 0x002a, 0x002a, 0x002a, 0x002a, 0x002a, 0x002a, 0x01d0, 0x01d7, 0x01de, 0x01e6, + 0x01ed, 0x002a, 0x01f5, 0x002a, 0x01fb, 0x002a, 0x002a, 0x002a, 0x0203, 0x0209, 0x0211, 0x0218, 0x0220, 0x0228, 0x0230, 0x0236, 0x023d, 0x002a, 0x002a, 0x0244, 0x002a, 0x002a, 0x002a, 0x0047, 0x002a, 0x002a, 0x002a, 0x002a, 0x002a, 0x002a, 0x002a, 0x002a, + 0x024c, 0x0254, 0x025c, 0x0262, 0x026a, 0x0272, 0x027a, 0x0282, 0x028a, 0x0292, 0x029a, 0x002a, 0x02a2, 0x002a, 0x02a9, 0x02b0, 0x002a, 0x02b8, 0x02bc, 0x02c4, 0x002a, 0x002a, 0x02cc, 0x02d4, 0x02dc, 0x02e4, 0x02ec, 0x02f4, 0x02fc, 0x0304, 0x030c, 0x002a, + 0x002a, 0x002a, 0x002a, 0x0314, 0x002a, 0x002a, 0x002a, 0x002a, 0x002a, 0x002a, 0x002a, 0x031c, 0x0322, 0x002a, 0x002a, 0x002a, 0x002a, 0x002a, 0x0326, 0x002a, 0x032d, 0x002a, 0x0043, 0x002a, 0x002a, 0x0335, 0x0339, 0x0341, 0x0341, 0x0341, 0x0347, 0x034d, + 0x0355, 0x035b, 0x0341, 0x0363, 0x0341, 0x036a, 0x036e, 0x0374, 0x037b, 0x0381, 0x0341, 0x0341, 0x0341, 0x0341, 0x0341, 0x0341, 0x0341, 0x0341, 0x0341, 0x0341, 0x0341, 0x0341, 0x0341, 0x0341, 0x0341, 0x0341, 0x0341, 0x0341, 0x0341, 0x0341, 0x0341, 0x0341, + 0x0341, 0x0341, 0x0341, 0x0341, 0x0341, 0x0341, 0x0341, 0x0341, 0x0341, 0x0341, 0x0388, 0x0390, 0x002a, 0x002a, 0x002a, 0x002a, 0x002a, 0x0393, 0x039b, 0x017f, 0x002a, 0x002a, 0x002a, 0x002a, 0x03a3, 0x002a, 0x03ab, 0x03b3, 0x03bb, 0x03c3, 0x03cb, 0x03d3, + 0x03d8, 0x03e0, 0x03e8, 0x03f0, 0x002a, 0x002a, 0x002a, 0x03f7, 0x03ff, 0x0400, 0x0401, 0x0402, 0x0403, 0x0404, 0x0405, 0x03ff, 0x0400, 0x0401, 0x0402, 0x0403, 0x0404, 0x0405, 0x03ff, 0x0400, 0x0401, 0x0402, 0x0403, 0x0404, 0x0405, 0x03ff, 0x0400, 0x0401, + 0x0402, 0x0403, 0x0404, 0x0405, 0x03ff, 0x0400, 0x0401, 0x0402, 0x0403, 0x0404, 0x0405, 0x03ff, 0x0400, 0x0401, 0x0402, 0x0403, 0x0404, 0x040c, 0x0414, 0x002a, 0x002a, 0x002a, 0x002a, 0x002a, 0x002a, 0x002a, 0x002a, 0x002a, 0x002a, 0x002a, 0x002a, 0x002a, + 0x002a, 0x002a, 0x002a, 0x002a, 0x002a, 0x002a, 0x002a, 0x002a, 0x002a, 0x002a, 0x002a, 0x002a, 0x002a, 0x002a, 0x002a, 0x002a, 0x002a, 0x002a, 0x002a, 0x02bc, 0x02bc, 0x02bc, 0x02bc, 0x02bc, 0x02bc, 0x02bc, 0x02bc, 0x02bc, 0x02bc, 0x02bc, 0x02bc, 0x02bc, + 0x02bc, 0x02bc, 0x02bc, 0x02bc, 0x02bc, 0x02bc, 0x02bc, 0x02bc, 0x02bc, 0x02bc, 0x02bc, 0x02bc, 0x02bc, 0x02bc, 0x02bc, 0x02bc, 0x02bc, 0x02bc, 0x02bc, 0x0341, 0x0341, 0x0341, 0x0341, 0x0341, 0x0341, 0x0341, 0x0341, 0x041c, 0x002a, 0x002a, 0x002a, 0x002a, + 0x002a, 0x002a, 0x002a, 0x002a, 0x002a, 0x002a, 0x002a, 0x0424, 0x042a, 0x002a, 0x0430, 0x0355, 0x0438, 0x043d, 0x0441, 0x002a, 0x002a, 0x002a, 0x002a, 0x002a, 0x002a, 0x002a, 0x0449, 0x002a, 0x002a, 0x002a, 0x0451, 0x002a, 0x0456, 0x002a, 0x002a, 0x002a, + 0x002a, 0x002a, 0x002a, 0x002a, 0x002a, 0x002a, 0x002a, 0x002a, 0x002a, 0x002a, 0x002a, 0x002a, 0x002a, 0x002a, 0x002a, 0x045e, 0x002a, 0x002a, 0x01f1, 0x002a, 0x002a, 0x002a, 0x002a, 0x002a, 0x002a, 0x002a, 0x002a, 0x0466, 0x046b, 0x002a, 0x002a, 0x002a, + 0x002a, 0x0471, 0x0477, 0x002a, 0x00a7, 0x047f, 0x002a, 0x0487, 0x048f, 0x0497, 0x049f, 0x04a7, 0x04af, 0x04b7, 0x04bf, 0x04c2, 0x04ca, 0x002a, 0x04cf, 0x04d7, 0x04df, 0x04e6, 0x04ee, 0x04f3, 0x04fb, 0x04ff, 0x0507, 0x002a, 0x002a, 0x050a, 0x0512, 0x0516, + 0x051e, 0x0521, 0x002a, 0x0528, 0x002a, 0x002a, 0x002a, 0x052e, 0x002a, 0x002a, 0x002a, 0x0536, 0x053e, 0x002a, 0x0544, 0x054c, 0x0554, 0x055c, 0x002a, 0x002a, 0x002a, 0x002a, 0x002a, 0x0560, 0x002a, 0x0568, 0x002a, 0x056f, 0x0577, 0x057e, 0x002a, 0x002a, + 0x002a, 0x002a, 0x0581, 0x0589, 0x0591, 0x002a, 0x002a, 0x002a, 0x002a, 0x002a, 0x002a, 0x002a, 0x002a, 0x002a, 0x002a, 0x002a, 0x002a, 0x002a, 0x002a, 0x002a, 0x002a, 0x0595, 0x059d, 0x002a, 0x002a, 0x002a, 0x002a, 0x002a, 0x002a, 0x002a, 0x002a, 0x002a, + 0x002a, 0x002a, 0x002a, 0x002a, 0x002a, 0x05a2, 0x002a, 0x002a, 0x002a, 0x002a, 0x002a, 0x002a, 0x002a, 0x002a, 0x002a, 0x002a, 0x002a, 0x002a, 0x002a, 0x002a, 0x002a, 0x002a, 0x002a, 0x002a, 0x002a, 0x002a, 0x002a, 0x002a, 0x002a, 0x002a, 0x002a, 0x002a, + 0x002a, 0x05a8, 0x05af, 0x002a, 0x002a, 0x002a, 0x002a, 0x002a, 0x002a, 0x002a, 0x002a, 0x05b6, 0x002a, 0x002a, 0x002a, 0x002a, 0x002a, 0x002a, 0x002a, 0x05bd, 0x05c4, 0x05c8, 0x0341, 0x0341, 0x0341, 0x0341, 0x0341, 0x0341, 0x0341, 0x0341, 0x0341, 0x0341, + 0x0341, 0x0341, 0x0341, 0x0341, 0x0341, 0x0341, 0x0341, 0x0341, 0x0341, 0x0341, 0x0341, 0x0341, 0x0341, 0x0341, 0x0341, 0x0341, 0x0341, 0x0341, 0x0341, 0x0341, 0x0341, 0x05d0, 0x0341, 0x0341, 0x0341, 0x0341, 0x0341, 0x0341, 0x0341, 0x0341, 0x0341, 0x0341, + 0x0341, 0x0341, 0x0341, 0x0341, 0x0341, 0x0341, 0x0341, 0x0341, 0x0341, 0x05d8, 0x05e0, 0x002a, 0x002a, 0x002a, 0x002a, 0x002a, 0x002a, 0x002a, 0x002a, 0x002a, 0x002a, 0x002a, 0x002a, 0x002a, 0x002a, 0x002a, 0x002a, 0x002a, 0x002a, 0x002a, 0x002a, 0x002a, + 0x002a, 0x002a, 0x002a, 0x002a, 0x002a, 0x002a, 0x002a, 0x002a, 0x002a, 0x002a, 0x05e2, 0x0341, 0x0341, 0x0341, 0x0341, 0x05ea, 0x05f1, 0x0341, 0x0341, 0x0341, 0x0341, 0x0341, 0x05f7, 0x002a, 0x002a, 0x002a, 0x002a, 0x002a, 0x002a, 0x002a, 0x002a, 0x002a, + 0x002a, 0x002a, 0x002a, 0x002a, 0x002a, 0x002a, 0x002a, 0x002a, 0x002a, 0x002a, 0x002a, 0x05ff, 0x002a, 0x002a, 0x002a, 0x002a, 0x002a, 0x002a, 0x002a, 0x002a, 0x002a, 0x002a, 0x002a, 0x002a, 0x002a, 0x002a, 0x002a, 0x002a, 0x002a, 0x002a, 0x002a, 0x002a, + 0x002a, 0x002a, 0x002a, 0x002a, 0x002a, 0x002a, 0x002a, 0x002a, 0x0607, 0x060f, 0x002a, 0x002a, 0x002a, 0x002a, 0x002a, 0x0613, 0x061b, 0x002a, 0x002a, 0x0623, 0x002a, 0x002a, 0x0341, 0x062b, 0x002a, 0x002a, 0x002a, 0x002a, 0x002a, 0x002a, 0x002a, 0x002a, + 0x002a, 0x002a, 0x002a, 0x002a, 0x002a, 0x002a, 0x002a, 0x002a, 0x002a, 0x002a, 0x0633, 0x063b, 0x0643, 0x002a, 0x002a, 0x002a, 0x002a, 0x002a, 0x002a, 0x002a, 0x002a, 0x002a, 0x002a, 0x002a, 0x002a, 0x002a, 0x002a, 0x002a, 0x002a, 0x002a, 0x002a, 0x002a, + 0x002a, 0x002a, 0x064b, 0x002a, 0x0652, 0x002a, 0x05af, 0x002a, 0x002a, 0x002a, 0x002a, 0x002a, 0x0655, 0x065b, 0x002a, 0x002a, 0x002a, 0x002a, 0x002a, 0x002a, 0x002a, 0x065b, 0x002a, 0x002a, 0x002a, 0x0661, 0x002a, 0x002a, 0x002a, 0x002a, 0x002a, 0x002a, + 0x002a, 0x002a, 0x0667, 0x002a, 0x066f, 0x002a, 0x002a, 0x002a, 0x002a, 0x002a, 0x002a, 0x002a, 0x002a, 0x002a, 0x002a, 0x002a, 0x002a, 0x002a, 0x002a, 0x002a, 0x002a, 0x002a, 0x002a, 0x002a, 0x002a, 0x002a, 0x002a, 0x002a, 0x002a, 0x002a, 0x002a, 0x0677, + 0x0678, 0x0678, 0x067f, 0x0687, 0x068d, 0x0695, 0x069b, 0x06a3, 0x06ab, 0x0678, 0x0678, 0x06b3, 0x06ba, 0x06c2, 0x06c9, 0x06d1, 0x06d9, 0x06da, 0x06db, 0x06e3, 0x06eb, 0x06f3, 0x06f8, 0x06da, 0x0700, 0x06da, 0x0708, 0x002a, 0x0710, 0x002a, 0x0718, 0x0720, + 0x0727, 0x072e, 0x0678, 0x0736, 0x073e, 0x06da, 0x06da, 0x0678, 0x0746, 0x074e, 0x0756, 0x002a, 0x002a, 0x002a, 0x002a, 0x0678, 0x0678, 0x0678, 0x0678, 0x0678, 0x0678, 0x0678, 0x0678, 0x0678, 0x0678, 0x0678, 0x0678, 0x0678, 0x0678, 0x0678, 0x075e, 0x0341, + 0x0341, 0x0341, 0x0341, 0x0341, 0x0341, 0x0341, 0x0341, 0x0341, 0x0341, 0x0341, 0x0341, 0x0341, 0x0341, 0x0341, 0x0341, 0x0341, 0x0341, 0x0341, 0x0341, 0x0341, 0x0341, 0x0341, 0x0341, 0x0341, 0x0341, 0x0341, 0x0341, 0x0341, 0x0341, 0x0341, 0x0342, 0x0766, + 0x0047, 0x076e, 0x076e, 0x0047, 0x0047, 0x0047, 0x0776, 0x076e, 0x076e, 0x076e, 0x076e, 0x076e, 0x076e, 0x076e, 0x076e, 0x076e, 0x076e, 0x076e, 0x076e, 0x076e, 0x076e, 0x076e, 0x076e, 0x076e, 0x076e, 0x076e, 0x076e, 0x076e, 0x076e, 0x076e, 0x076e, 0x076e, + 0x076e, 0x076e, 0x076e, 0x076e, 0x076e, 0x076e, 0x076e, 0x02bc, 0x02bc, 0x02bc, 0x02bc, 0x02bc, 0x02bc, 0x02bc, 0x02bc, 0x02bc, 0x02bc, 0x02bc, 0x02bc, 0x02bc, 0x02bc, 0x02bc, 0x02bc, 0x02bc, 0x02bc, 0x02bc, 0x02bc, 0x02bc, 0x02bc, 0x02bc, 0x02bc, 0x02bc, + 0x02bc, 0x02bc, 0x02bc, 0x02bc, 0x02bc, 0x02bc, 0x077e, +}; +static const uint16_t s_stage2[] = { + 0x0000, 0x0000, 0x0000, 0x0000, 0x0008, 0x0008, 0x0008, 0x0008, + 0x0008, 0x0008, 0x0008, 0x0009, 0x0000, 0x0000, 0x0000, 0x0000, + 0x0011, 0x0018, 0x0020, 0x0022, 0x002a, 0x0008, 0x0029, 0x0030, + 0x0036, 0x003e, 0x0040, 0x0047, 0x004c, 0x0008, 0x004a, 0x002d, + 0x004e, 0x002d, 0x0053, 0x0029, 0x005b, 0x0063, 0x0034, 0x0008, + 0x004e, 0x002d, 0x0008, 0x0008, 0x0008, 0x0008, 0x0008, 0x0008, + 0x0008, 0x0008, 0x002a, 0x006b, 0x0049, 0x0008, 0x0008, 0x0008, + 0x0008, 0x004c, 0x0008, 0x004c, 0x0008, 0x0008, 0x0008, 0x0072, + 0x005a, 0x0079, 0x0081, 0x0008, 0x0008, 0x0008, 0x0008, 0x0089, + 0x0089, 0x0089, 0x0089, 0x0089, 0x0089, 0x0089, 0x0089, 0x0008, + 0x0008, 0x0091, 0x0092, 0x0098, 0x0028, 0x0091, 0x0092, 0x0098, + 0x0028, 0x0008, 0x0008, 0x0008, 0x0008, 0x0008, 0x0008, 0x004c, + 0x0008, 0x0092, 0x0092, 0x0092, 0x0092, 0x0092, 0x0092, 0x004c, + 0x0008, 0x0008, 0x0008, 0x0008, 0x0008, 0x00a0, 0x00a6, 0x0008, + 0x0008, 0x0008, 0x0008, 0x0008, 0x0008, 0x00ad, 0x0089, 0x0089, + 0x0089, 0x0089, 0x00af, 0x00b5, 0x0008, 0x0008, 0x0008, 0x0008, + 0x0008, 0x0008, 0x0008, 0x00bd, 0x0008, 0x0089, 0x00c5, 0x0008, + 0x0008, 0x0008, 0x0008, 0x00a0, 0x0089, 0x0089, 0x0008, 0x0008, + 0x00cd, 0x0008, 0x0008, 0x00a8, 0x00d5, 0x00dc, 0x00e3, 0x0008, + 0x0008, 0x00e9, 0x00cc, 0x0008, 0x0008, 0x0008, 0x0089, 0x0089, + 0x00a5, 0x0008, 0x0008, 0x0008, 0x0008, 0x0008, 0x0008, 0x00a8, + 0x0089, 0x00cd, 0x0008, 0x0008, 0x0008, 0x0008, 0x0008, 0x00a0, + 0x00a4, 0x00f1, 0x0008, 0x0008, 0x00a8, 0x00f9, 0x00fd, 0x00a2, + 0x0008, 0x0008, 0x0008, 0x0101, 0x0008, 0x0008, 0x0008, 0x0008, + 0x0109, 0x0089, 0x0008, 0x0008, 0x0008, 0x0008, 0x00ac, 0x0089, + 0x0089, 0x0110, 0x0089, 0x0089, 0x0089, 0x0115, 0x0008, 0x0119, + 0x011e, 0x011e, 0x011e, 0x011e, 0x0124, 0x012b, 0x0132, 0x00ad, + 0x011e, 0x013a, 0x0008, 0x0008, 0x011e, 0x0141, 0x0008, 0x0119, + 0x011e, 0x011e, 0x0149, 0x0150, 0x0156, 0x015d, 0x0164, 0x016a, + 0x0172, 0x013a, 0x0008, 0x0179, 0x017b, 0x0182, 0x0008, 0x0008, + 0x0008, 0x0008, 0x0008, 0x0008, 0x0186, 0x018d, 0x0194, 0x00cc, + 0x0008, 0x0008, 0x0008, 0x0198, 0x0008, 0x0182, 0x0008, 0x0119, + 0x011e, 0x011e, 0x0149, 0x01a0, 0x0156, 0x01a8, 0x01af, 0x0008, + 0x0008, 0x013a, 0x0008, 0x0008, 0x01b6, 0x0141, 0x0008, 0x0119, + 0x011e, 0x011e, 0x0149, 0x01a0, 0x01be, 0x015d, 0x0164, 0x01c6, + 0x0172, 0x013a, 0x0008, 0x01ce, 0x0008, 0x00cb, 0x0008, 0x0008, + 0x0008, 0x0008, 0x0008, 0x0008, 0x01d0, 0x01d8, 0x01df, 0x016a, + 0x0008, 0x0008, 0x0008, 0x0008, 0x0008, 0x01e7, 0x0008, 0x0119, + 0x011e, 0x011e, 0x0149, 0x011e, 0x01ef, 0x01f6, 0x01fd, 0x0203, + 0x020b, 0x013a, 0x0008, 0x0008, 0x0008, 0x0141, 0x0008, 0x0008, + 0x0008, 0x0008, 0x0008, 0x0008, 0x020f, 0x0217, 0x021e, 0x0224, + 0x0008, 0x013a, 0x0008, 0x016e, 0x0008, 0x022c, 0x0008, 0x0119, + 0x011e, 0x011e, 0x011e, 0x011e, 0x0234, 0x023b, 0x0242, 0x016a, + 0x0008, 0x013a, 0x0008, 0x0008, 0x0008, 0x0141, 0x0008, 0x0008, + 0x0008, 0x0008, 0x0008, 0x0008, 0x0008, 0x0249, 0x0250, 0x0258, + 0x0008, 0x0008, 0x0260, 0x0008, 0x0008, 0x0008, 0x0008, 0x0008, + 0x0008, 0x0267, 0x00a5, 0x00ce, 0x008a, 0x0008, 0x0008, 0x0008, + 0x0008, 0x0008, 0x0008, 0x0267, 0x00a3, 0x0008, 0x008a, 0x0008, + 0x0008, 0x0008, 0x0008, 0x0008, 0x0008, 0x00a6, 0x0008, 0x0008, + 0x017c, 0x024a, 0x0008, 0x0008, 0x0008, 0x0008, 0x0008, 0x0008, + 0x00ad, 0x012c, 0x00b0, 0x00a9, 0x0089, 0x00ad, 0x0089, 0x0089, + 0x0089, 0x00a3, 0x017b, 0x0008, 0x0008, 0x0008, 0x0008, 0x0008, + 0x0008, 0x0008, 0x00a9, 0x026e, 0x0276, 0x0008, 0x0008, 0x01d0, + 0x027b, 0x00cd, 0x0008, 0x00e4, 0x0008, 0x0283, 0x00f1, 0x0008, + 0x00f1, 0x0008, 0x0008, 0x0008, 0x0008, 0x028b, 0x028b, 0x028b, + 0x028b, 0x028b, 0x028b, 0x028b, 0x028b, 0x0293, 0x0293, 0x0293, + 0x0293, 0x0293, 0x029b, 0x029b, 0x029b, 0x029b, 0x029b, 0x029b, + 0x029b, 0x029b, 0x0008, 0x0008, 0x0008, 0x00a9, 0x0008, 0x0008, + 0x0008, 0x0008, 0x02a3, 0x0008, 0x0008, 0x0008, 0x02a9, 0x0008, + 0x0008, 0x013a, 0x0008, 0x0008, 0x0008, 0x013a, 0x0008, 0x0008, + 0x0008, 0x0008, 0x0008, 0x0008, 0x02ae, 0x012d, 0x02b6, 0x012b, + 0x00a4, 0x00f1, 0x0008, 0x0008, 0x0008, 0x0008, 0x02be, 0x0008, + 0x0008, 0x0008, 0x0008, 0x0008, 0x0008, 0x0203, 0x0008, 0x0008, + 0x0008, 0x0008, 0x00cc, 0x0008, 0x0008, 0x0008, 0x0008, 0x02c5, + 0x02cc, 0x02d4, 0x02db, 0x0008, 0x0008, 0x00ce, 0x02e3, 0x0008, + 0x0008, 0x0008, 0x0008, 0x02e7, 0x008a, 0x02ef, 0x012e, 0x02f7, + 0x00dc, 0x0008, 0x0008, 0x0008, 0x0008, 0x0008, 0x0008, 0x0089, + 0x0089, 0x008a, 0x0008, 0x0008, 0x0008, 0x0008, 0x0008, 0x0008, + 0x0114, 0x0008, 0x0008, 0x0008, 0x0008, 0x0008, 0x02ff, 0x0305, + 0x01d6, 0x0008, 0x0008, 0x0008, 0x0008, 0x00a0, 0x00a4, 0x0008, + 0x0116, 0x0008, 0x0008, 0x0008, 0x030d, 0x0315, 0x0008, 0x0008, + 0x0008, 0x0008, 0x031b, 0x0323, 0x022c, 0x0008, 0x0008, 0x0008, + 0x0008, 0x032b, 0x032f, 0x030f, 0x0008, 0x0008, 0x0334, 0x0089, + 0x026e, 0x018f, 0x033c, 0x00a6, 0x0008, 0x0344, 0x034c, 0x0351, + 0x0022, 0x0359, 0x0361, 0x0367, 0x0008, 0x036e, 0x0008, 0x0008, + 0x0376, 0x037c, 0x002c, 0x007a, 0x0025, 0x0008, 0x0008, 0x0008, + 0x0008, 0x002c, 0x0008, 0x0008, 0x0089, 0x0089, 0x0089, 0x0089, + 0x00cd, 0x0008, 0x0384, 0x004c, 0x0073, 0x0008, 0x038b, 0x002d, + 0x0008, 0x036e, 0x0008, 0x0008, 0x0033, 0x0060, 0x0092, 0x0026, + 0x0092, 0x0028, 0x0008, 0x004c, 0x0393, 0x0399, 0x0008, 0x03a0, + 0x0008, 0x0028, 0x0008, 0x0008, 0x03a6, 0x0008, 0x007a, 0x0008, + 0x0008, 0x0008, 0x0040, 0x03ae, 0x03a9, 0x03b3, 0x0068, 0x03b8, + 0x007d, 0x0032, 0x0008, 0x03be, 0x002e, 0x0008, 0x03c6, 0x03c4, + 0x0008, 0x0008, 0x03c4, 0x0008, 0x002b, 0x004c, 0x002b, 0x0008, + 0x0008, 0x007a, 0x0008, 0x0008, 0x002e, 0x03ce, 0x0008, 0x03d6, + 0x0008, 0x0008, 0x03de, 0x0008, 0x0008, 0x0008, 0x0008, 0x0008, + 0x0008, 0x03df, 0x0008, 0x0008, 0x0008, 0x03e7, 0x03ef, 0x03f7, + 0x0008, 0x0008, 0x0008, 0x0008, 0x0092, 0x0092, 0x0092, 0x0092, + 0x0092, 0x0092, 0x0092, 0x0092, 0x03ff, 0x0092, 0x0092, 0x0092, + 0x0092, 0x0098, 0x0092, 0x0092, 0x0008, 0x0008, 0x0008, 0x0008, + 0x0098, 0x0405, 0x040b, 0x0032, 0x0411, 0x0418, 0x0028, 0x0008, + 0x034d, 0x007a, 0x0008, 0x0420, 0x0428, 0x042f, 0x0437, 0x043d, + 0x0444, 0x0444, 0x044c, 0x0444, 0x0441, 0x044c, 0x0450, 0x0444, + 0x0458, 0x045b, 0x0444, 0x0445, 0x0463, 0x0469, 0x0471, 0x0475, + 0x0473, 0x047d, 0x0444, 0x0481, 0x0482, 0x0488, 0x048a, 0x048f, + 0x045f, 0x048c, 0x0495, 0x049b, 0x04a3, 0x047d, 0x04ab, 0x03d9, + 0x036e, 0x04b3, 0x039e, 0x002b, 0x04b7, 0x04bf, 0x04c6, 0x0008, + 0x04ce, 0x0008, 0x004e, 0x0092, 0x0008, 0x0008, 0x04d6, 0x0008, + 0x036e, 0x0008, 0x04b3, 0x04de, 0x0008, 0x0008, 0x0008, 0x0008, + 0x0008, 0x0008, 0x039d, 0x0008, 0x04e6, 0x0008, 0x0008, 0x04ee, + 0x0008, 0x0008, 0x0008, 0x0008, 0x04f2, 0x0028, 0x0008, 0x0008, + 0x0008, 0x0008, 0x0008, 0x00ce, 0x00a6, 0x0008, 0x0008, 0x0008, + 0x0008, 0x0008, 0x0008, 0x0008, 0x00ce, 0x04fa, 0x04fa, 0x04fa, + 0x0500, 0x04fa, 0x04fa, 0x04fa, 0x04fa, 0x04fa, 0x04fa, 0x0504, + 0x0008, 0x04fa, 0x04fa, 0x04fa, 0x04fa, 0x04fa, 0x04fa, 0x04fa, + 0x04fa, 0x050c, 0x0008, 0x0008, 0x0008, 0x04fa, 0x04fa, 0x04fa, + 0x04fa, 0x04fa, 0x0514, 0x051c, 0x051f, 0x0526, 0x04fa, 0x04fa, + 0x04fa, 0x04fa, 0x04fa, 0x04fa, 0x04fa, 0x04fb, 0x052e, 0x04fa, + 0x04fa, 0x04fa, 0x04fa, 0x0536, 0x04fa, 0x04fa, 0x04fa, 0x04fa, + 0x04fa, 0x0526, 0x04fa, 0x04fb, 0x04fa, 0x04fa, 0x04fa, 0x04fa, + 0x04fa, 0x04fa, 0x050c, 0x053e, 0x04fa, 0x04fa, 0x04fa, 0x04fb, + 0x04fa, 0x04fa, 0x04fa, 0x04fa, 0x0092, 0x04fa, 0x04fa, 0x04fa, + 0x04fa, 0x04fa, 0x04fa, 0x051d, 0x0545, 0x04fa, 0x04fa, 0x04fa, + 0x04fa, 0x0503, 0x04fa, 0x04fa, 0x04fa, 0x04fa, 0x04fa, 0x04fa, + 0x04fb, 0x0008, 0x0008, 0x0008, 0x0008, 0x0008, 0x0008, 0x0008, + 0x00ce, 0x0334, 0x054d, 0x0008, 0x0008, 0x0008, 0x00a8, 0x0008, + 0x0008, 0x0008, 0x0008, 0x0553, 0x00ca, 0x0008, 0x0008, 0x055a, + 0x0562, 0x0008, 0x0008, 0x056a, 0x0008, 0x0008, 0x0008, 0x0008, + 0x0008, 0x032b, 0x0258, 0x0572, 0x0008, 0x0008, 0x0008, 0x0089, + 0x0089, 0x00a6, 0x00ce, 0x0008, 0x0008, 0x0008, 0x0008, 0x00a8, + 0x054d, 0x0008, 0x0008, 0x00ce, 0x0089, 0x022c, 0x0008, 0x028b, + 0x028b, 0x028b, 0x057a, 0x0115, 0x0008, 0x0008, 0x0008, 0x0008, + 0x0008, 0x057f, 0x0585, 0x058c, 0x0008, 0x0008, 0x0008, 0x00f1, + 0x0008, 0x0008, 0x0008, 0x0008, 0x0008, 0x0593, 0x059a, 0x0008, + 0x00ca, 0x05a1, 0x0008, 0x0008, 0x0008, 0x0008, 0x0008, 0x0562, + 0x0008, 0x0008, 0x0008, 0x0008, 0x0008, 0x0008, 0x05a9, 0x01c5, + 0x00cc, 0x0008, 0x0008, 0x0008, 0x0008, 0x05b1, 0x05b9, 0x0008, + 0x0008, 0x0008, 0x0008, 0x01d3, 0x05c1, 0x0008, 0x0008, 0x05c9, + 0x05ca, 0x05ca, 0x05ce, 0x05ca, 0x05ca, 0x05ca, 0x05c9, 0x05ca, + 0x05ca, 0x05ce, 0x05ca, 0x05ca, 0x05ca, 0x05c9, 0x05ca, 0x05ca, + 0x05d3, 0x0008, 0x0293, 0x0293, 0x05db, 0x05e2, 0x029b, 0x029b, + 0x029b, 0x029b, 0x029b, 0x05e6, 0x0008, 0x0008, 0x0008, 0x017b, + 0x0008, 0x0008, 0x0008, 0x0008, 0x0089, 0x0089, 0x04fa, 0x053c, + 0x0089, 0x0089, 0x04fa, 0x04fa, 0x04ff, 0x04fa, 0x04fb, 0x0504, + 0x0008, 0x0008, 0x0008, 0x0008, 0x0008, 0x0008, 0x0008, 0x05ea, + 0x04fa, 0x04fa, 0x04fa, 0x04fa, 0x053d, 0x0008, 0x0008, 0x0008, + 0x01d0, 0x0008, 0x0008, 0x0008, 0x0008, 0x04fb, 0x0008, 0x0000, + 0x05f2, 0x0008, 0x0008, 0x0008, 0x0008, 0x0008, 0x0008, 0x0008, + 0x00f1, 0x0008, 0x0008, 0x0008, 0x0008, 0x00cd, 0x0008, 0x0008, + 0x0008, 0x0008, 0x0008, 0x0008, 0x00a8, 0x00a5, 0x05f9, 0x00aa, + 0x0008, 0x0008, 0x0008, 0x0008, 0x0008, 0x02dc, 0x0008, 0x0008, + 0x0008, 0x0008, 0x00aa, 0x0008, 0x0008, 0x0008, 0x0008, 0x0008, + 0x00a2, 0x0008, 0x0008, 0x0008, 0x0008, 0x0008, 0x0600, 0x0008, + 0x0008, 0x0008, 0x0008, 0x0008, 0x0008, 0x0008, 0x00aa, 0x0606, + 0x0008, 0x0008, 0x0008, 0x0008, 0x0008, 0x0008, 0x0008, 0x060e, + 0x0008, 0x0008, 0x0008, 0x0008, 0x0008, 0x0008, 0x0089, 0x008a, + 0x0008, 0x0008, 0x0008, 0x0008, 0x0008, 0x0616, 0x00ce, 0x0116, + 0x0008, 0x0008, 0x0008, 0x0008, 0x0008, 0x061e, 0x0625, 0x00cb, + 0x00eb, 0x0008, 0x0008, 0x0008, 0x0008, 0x0008, 0x0008, 0x00a5, + 0x0008, 0x0008, 0x0008, 0x00ce, 0x026b, 0x00a3, 0x0008, 0x0224, + 0x0008, 0x0008, 0x0008, 0x0008, 0x0008, 0x00ca, 0x0008, 0x0116, + 0x0008, 0x0008, 0x0008, 0x0008, 0x0008, 0x061b, 0x012c, 0x062d, + 0x0634, 0x0008, 0x0008, 0x0008, 0x0008, 0x0008, 0x0008, 0x063c, + 0x0643, 0x017b, 0x00cc, 0x0008, 0x0008, 0x0008, 0x0008, 0x0008, + 0x0008, 0x0008, 0x00ce, 0x02f7, 0x00a5, 0x0008, 0x0008, 0x022c, + 0x0008, 0x0008, 0x0008, 0x0008, 0x0008, 0x0008, 0x064b, 0x0653, + 0x0657, 0x016a, 0x0008, 0x065d, 0x00a3, 0x00a3, 0x0008, 0x0008, + 0x0008, 0x0008, 0x0008, 0x0008, 0x0008, 0x02f7, 0x0664, 0x066b, + 0x0673, 0x0008, 0x067a, 0x0008, 0x0008, 0x0008, 0x0008, 0x0008, + 0x0008, 0x056d, 0x0089, 0x0682, 0x0008, 0x0008, 0x017b, 0x0008, + 0x0008, 0x0008, 0x0008, 0x0008, 0x0008, 0x02f7, 0x068a, 0x0691, + 0x0008, 0x0008, 0x0008, 0x0008, 0x0008, 0x0008, 0x0008, 0x016a, + 0x015c, 0x0699, 0x00cd, 0x0008, 0x0008, 0x027d, 0x0008, 0x0008, + 0x0008, 0x0008, 0x0008, 0x0008, 0x02f7, 0x06a0, 0x00cd, 0x0008, + 0x0008, 0x0008, 0x0008, 0x0008, 0x0008, 0x0008, 0x06a8, 0x0594, + 0x0008, 0x0008, 0x0008, 0x031c, 0x06b0, 0x00a4, 0x0008, 0x0008, + 0x0008, 0x0008, 0x0008, 0x063c, 0x0089, 0x0221, 0x0008, 0x0008, + 0x0008, 0x0008, 0x0008, 0x0008, 0x06b8, 0x06bf, 0x06c7, 0x0008, + 0x0008, 0x0008, 0x0008, 0x0008, 0x0008, 0x0008, 0x061d, 0x06cd, + 0x06d5, 0x0008, 0x0008, 0x0008, 0x00ad, 0x00a5, 0x0008, 0x0008, + 0x0008, 0x0008, 0x00a0, 0x06dd, 0x00ce, 0x0008, 0x0593, 0x02db, + 0x0008, 0x0008, 0x0008, 0x0008, 0x06e4, 0x06ea, 0x012c, 0x00a6, + 0x0008, 0x0008, 0x0008, 0x0008, 0x0008, 0x016a, 0x008a, 0x0594, + 0x0008, 0x0008, 0x00ac, 0x0089, 0x0089, 0x06f2, 0x06f9, 0x0008, + 0x0008, 0x0008, 0x0008, 0x0008, 0x0008, 0x0700, 0x0707, 0x070e, + 0x0008, 0x0008, 0x0008, 0x0008, 0x0008, 0x0008, 0x0008, 0x0716, + 0x071e, 0x0008, 0x0008, 0x0008, 0x0008, 0x0008, 0x0008, 0x0726, + 0x0008, 0x072e, 0x0008, 0x0008, 0x0008, 0x0008, 0x0008, 0x024c, + 0x0736, 0x073e, 0x0008, 0x0008, 0x00cb, 0x0008, 0x0008, 0x0008, + 0x0008, 0x0008, 0x0008, 0x037c, 0x037c, 0x00a7, 0x0089, 0x054d, + 0x0008, 0x0008, 0x0008, 0x0008, 0x0008, 0x00a8, 0x0089, 0x02f5, + 0x0008, 0x0008, 0x0008, 0x0008, 0x0008, 0x0008, 0x00a3, 0x0008, + 0x0008, 0x0008, 0x0008, 0x0008, 0x0008, 0x008a, 0x0008, 0x0008, + 0x0008, 0x0008, 0x0743, 0x074a, 0x0008, 0x0008, 0x00ce, 0x0257, + 0x0258, 0x0258, 0x0258, 0x0258, 0x0258, 0x00ce, 0x00a5, 0x0008, + 0x0008, 0x0008, 0x0008, 0x0008, 0x0752, 0x0008, 0x075a, 0x0008, + 0x04fa, 0x04fa, 0x04fa, 0x04fa, 0x04fa, 0x04fa, 0x04fa, 0x0008, + 0x04fa, 0x04fa, 0x050c, 0x0008, 0x0008, 0x0008, 0x0008, 0x053e, + 0x04fa, 0x053d, 0x0008, 0x0008, 0x0008, 0x0008, 0x0008, 0x0008, + 0x04fe, 0x0762, 0x04fa, 0x04fa, 0x04fa, 0x04fa, 0x0533, 0x0008, + 0x0769, 0x0008, 0x0008, 0x0771, 0x0008, 0x0508, 0x0008, 0x04fa, + 0x04fa, 0x04fa, 0x04fa, 0x04fa, 0x04fa, 0x04fa, 0x0504, 0x0008, + 0x0008, 0x0008, 0x0203, 0x0779, 0x0008, 0x0008, 0x0008, 0x0089, + 0x0089, 0x0089, 0x0089, 0x0089, 0x054d, 0x0089, 0x0089, 0x008a, + 0x0008, 0x0008, 0x0008, 0x0008, 0x0008, 0x0008, 0x0008, 0x01d1, + 0x0619, 0x0781, 0x0786, 0x078b, 0x00a4, 0x0008, 0x0008, 0x0008, + 0x0606, 0x0008, 0x0008, 0x0195, 0x0008, 0x0008, 0x0008, 0x0008, + 0x0008, 0x0008, 0x0008, 0x04fa, 0x04fa, 0x04fb, 0x0008, 0x04fa, + 0x04fa, 0x04fb, 0x0008, 0x0089, 0x0089, 0x0089, 0x0089, 0x0089, + 0x0089, 0x008a, 0x00a0, 0x0089, 0x0089, 0x0089, 0x0089, 0x0089, + 0x00a3, 0x00f1, 0x0008, 0x0562, 0x0008, 0x0008, 0x00a0, 0x00ad, + 0x0089, 0x0008, 0x0008, 0x008a, 0x0089, 0x0089, 0x078d, 0x00b3, + 0x00a5, 0x0008, 0x0008, 0x00ce, 0x0008, 0x0008, 0x0008, 0x0008, + 0x0008, 0x0008, 0x017b, 0x0008, 0x0008, 0x0008, 0x0008, 0x0008, + 0x00aa, 0x0008, 0x0008, 0x0008, 0x0008, 0x0008, 0x00a8, 0x0008, + 0x0008, 0x008a, 0x0008, 0x0008, 0x0008, 0x0008, 0x0008, 0x00aa, + 0x00a5, 0x0008, 0x0008, 0x0008, 0x0008, 0x0008, 0x0008, 0x0795, + 0x0444, 0x0444, 0x0444, 0x0444, 0x0444, 0x0444, 0x0444, 0x0444, + 0x0445, 0x0444, 0x0444, 0x0444, 0x0444, 0x0444, 0x0444, 0x0092, + 0x079d, 0x0092, 0x0092, 0x0092, 0x07a5, 0x0092, 0x0092, 0x0092, + 0x0092, 0x0092, 0x07ad, 0x07b5, 0x07b7, 0x0092, 0x07bf, 0x07c6, + 0x07cb, 0x0092, 0x07ce, 0x0444, 0x0444, 0x0444, 0x0444, 0x07d3, + 0x07d9, 0x07d9, 0x07d9, 0x07e1, 0x0444, 0x04fa, 0x07e9, 0x04fa, + 0x051d, 0x07ef, 0x07f4, 0x04fa, 0x07f7, 0x07ff, 0x0444, 0x044e, + 0x0444, 0x0444, 0x0444, 0x044c, 0x044c, 0x044c, 0x044c, 0x0800, + 0x0447, 0x0808, 0x044c, 0x044c, 0x044c, 0x044c, 0x044c, 0x044c, + 0x044c, 0x0809, 0x044c, 0x044c, 0x0450, 0x0444, 0x044c, 0x044c, + 0x044c, 0x044c, 0x080f, 0x0450, 0x0444, 0x044c, 0x044c, 0x0816, + 0x081e, 0x044c, 0x044c, 0x044c, 0x044c, 0x044c, 0x044c, 0x044c, + 0x044d, 0x0826, 0x044c, 0x044c, 0x044c, 0x044c, 0x044c, 0x044c, + 0x044c, 0x044c, 0x0829, 0x044c, 0x044c, 0x044c, 0x044c, 0x044c, + 0x044c, 0x044c, 0x0830, 0x039b, 0x0838, 0x044c, 0x044c, 0x044c, + 0x0444, 0x0444, 0x0472, 0x0444, 0x0444, 0x07fa, 0x0444, 0x0795, + 0x0444, 0x0444, 0x0444, 0x0444, 0x0444, 0x0444, 0x0444, 0x0449, + 0x044c, 0x044c, 0x0008, 0x0008, 0x0008, 0x0008, 0x0008, 0x0008, + 0x044e, 0x0795, 0x082b, 0x0448, 0x0444, 0x07fc, 0x0448, 0x044f, + 0x0008, 0x0008, 0x0008, 0x0008, 0x0008, 0x0008, 0x0840, 0x0444, + 0x0008, 0x0008, 0x04e6, 0x0444, 0x044c, 0x0450, 0x0800, 0x0444, + 0x0008, 0x0840, 0x0008, 0x0008, 0x0008, 0x0008, 0x0008, 0x0008, + 0x0444, 0x0008, 0x0842, 0x0008, 0x0008, 0x0008, 0x0008, 0x0444, + 0x0008, 0x0008, 0x0008, 0x039b, 0x0444, 0x0444, 0x0008, 0x084a, + 0x044c, 0x044c, 0x044c, 0x044c, 0x044c, 0x084f, 0x0853, 0x044c, + 0x044c, 0x044c, 0x044c, 0x044c, 0x044c, 0x044c, 0x0444, 0x0444, + 0x0444, 0x0444, 0x0444, 0x0444, 0x044c, 0x044f, 0x044c, 0x047f, + 0x044c, 0x044c, 0x044c, 0x044c, 0x044c, 0x044c, 0x044d, 0x0446, + 0x044c, 0x0829, 0x044c, 0x07ff, 0x044c, 0x0800, 0x0444, 0x0444, + 0x0444, 0x0444, 0x0444, 0x0444, 0x0444, 0x0463, 0x085b, 0x0000, + 0x0000, 0x0000, 0x0089, 0x0089, 0x0089, 0x0089, 0x0000, 0x0000, + 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0089, 0x0089, + 0x0089, 0x0089, 0x0089, 0x0089, 0x0000, 0x0000, 0x0092, 0x0092, + 0x0092, 0x0092, 0x0092, 0x0092, 0x0092, 0x0863, +}; +static const uint8_t s_stage3[] = { + 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, + 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, + 0x41, 0x40, 0xc0, 0x40, 0x40, 0xc0, 0x40, 0x40, + 0xc0, 0x4c, 0xc0, 0x40, 0x40, 0x01, 0xcc, 0x40, + 0xc0, 0xc0, 0xc0, 0xc0, 0xc0, 0x40, 0xc0, 0xc0, + 0xc0, 0xc0, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, + 0xc0, 0x40, 0x40, 0x40, 0x40, 0x40, 0xc0, 0xc0, + 0x40, 0x40, 0x40, 0x40, 0xc0, 0x40, 0xc0, 0xc0, + 0xc0, 0x40, 0xc0, 0xc0, 0x40, 0x40, 0x40, 0xc0, + 0xc0, 0xc0, 0x40, 0xc0, 0x40, 0xc0, 0x40, 0x40, + 0x40, 0x40, 0x40, 0x40, 0xc0, 0xc0, 0xc0, 0x40, + 0x40, 0x40, 0x40, 0xc0, 0xc0, 0xc0, 0x40, 0xc0, + 0x40, 0x40, 0x40, 0xc0, 0xc0, 0xc0, 0xc0, 0x40, + 0xc0, 0x40, 0x40, 0xc0, 0x40, 0xc0, 0x40, 0xc0, + 0x40, 0xc0, 0x40, 0x40, 0x40, 0x40, 0xc0, 0x40, + 0x40, 0xc0, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, + 0x40, 0xc0, 0xc0, 0xc0, 0xc0, 0x40, 0xc0, 0x40, + 0xc0, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, + 0x02, 0x40, 0xc0, 0xc0, 0xc0, 0xc0, 0xc0, 0xc0, + 0xc0, 0xc0, 0x40, 0xc0, 0xc0, 0xc0, 0xc0, 0xc0, + 0x40, 0x40, 0x40, 0x02, 0x02, 0x02, 0x02, 0x02, + 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x02, 0x02, + 0x02, 0x02, 0x02, 0x02, 0x02, 0x40, 0x02, 0x02, + 0x40, 0x02, 0x02, 0x40, 0x02, 0x04, 0x04, 0x04, + 0x04, 0x04, 0x04, 0x40, 0x40, 0x02, 0x02, 0x02, + 0x40, 0x01, 0x40, 0x40, 0x40, 0x02, 0x40, 0x40, + 0x40, 0x40, 0x40, 0x40, 0x40, 0x02, 0x02, 0x02, + 0x02, 0x02, 0x04, 0x40, 0x02, 0x02, 0x02, 0x02, + 0x02, 0x40, 0x40, 0x02, 0x40, 0x02, 0x02, 0x02, + 0x02, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, + 0x04, 0x40, 0x40, 0x40, 0x40, 0x40, 0x02, 0x40, + 0x40, 0x02, 0x02, 0x40, 0x02, 0x02, 0x02, 0x02, + 0x02, 0x40, 0x02, 0x02, 0x02, 0x40, 0x40, 0x40, + 0x40, 0x04, 0x04, 0x40, 0x40, 0x40, 0x40, 0x40, + 0x02, 0x02, 0x04, 0x02, 0x02, 0x02, 0x02, 0x02, + 0x42, 0x40, 0x40, 0x40, 0x40, 0x40, 0x4b, 0x4b, + 0x4b, 0x4b, 0x4b, 0x4b, 0x4b, 0x4b, 0x02, 0x42, + 0x02, 0x40, 0x42, 0x42, 0x02, 0x02, 0x02, 0x02, + 0x02, 0x02, 0x02, 0x42, 0x42, 0x42, 0x42, 0x0a, + 0x42, 0x42, 0x40, 0x40, 0x02, 0x02, 0x40, 0x40, + 0x40, 0x40, 0x02, 0x42, 0x42, 0x40, 0x40, 0x40, + 0x40, 0x4b, 0x40, 0x4b, 0x4b, 0x4b, 0x4b, 0x4b, + 0x4b, 0x40, 0x4b, 0x40, 0x40, 0x40, 0x4b, 0x4b, + 0x40, 0x40, 0x02, 0x40, 0x42, 0x42, 0x02, 0x02, + 0x02, 0x02, 0x40, 0x40, 0x42, 0x40, 0x40, 0x42, + 0x42, 0x0a, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, + 0x40, 0x42, 0x40, 0x40, 0x40, 0x40, 0x4b, 0x4b, + 0x40, 0x4b, 0x4b, 0x40, 0x40, 0x40, 0x40, 0x40, + 0x40, 0x02, 0x40, 0x02, 0x02, 0x42, 0x40, 0x40, + 0x40, 0x40, 0x02, 0x40, 0x42, 0x42, 0x02, 0x02, + 0x40, 0x40, 0x40, 0x40, 0x02, 0x40, 0x40, 0x02, + 0x02, 0x02, 0x40, 0x40, 0x40, 0x02, 0x40, 0x40, + 0x4b, 0x40, 0x4b, 0x4b, 0x40, 0x4b, 0x4b, 0x4b, + 0x42, 0x02, 0x02, 0x02, 0x02, 0x02, 0x40, 0x02, + 0x42, 0x40, 0x42, 0x42, 0x0a, 0x40, 0x40, 0x4b, + 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x4b, 0x4b, + 0x40, 0x40, 0x02, 0x40, 0x42, 0x02, 0x40, 0x40, + 0x40, 0x40, 0x40, 0x02, 0x02, 0x42, 0x40, 0x4b, + 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x42, 0x42, + 0x02, 0x42, 0x42, 0x40, 0x40, 0x40, 0x42, 0x42, + 0x40, 0x42, 0x42, 0x42, 0x02, 0x40, 0x40, 0x02, + 0x42, 0x42, 0x42, 0x02, 0x40, 0x40, 0x40, 0x4b, + 0x4b, 0x40, 0x40, 0x02, 0x40, 0x02, 0x02, 0x42, + 0x42, 0x42, 0x42, 0x40, 0x02, 0x02, 0x40, 0x02, + 0x02, 0x02, 0x0a, 0x40, 0x40, 0x40, 0x40, 0x40, + 0x02, 0x02, 0x40, 0x4b, 0x4b, 0x4b, 0x40, 0x40, + 0x40, 0x40, 0x40, 0x02, 0x40, 0x42, 0x02, 0x42, + 0x42, 0x42, 0x42, 0x42, 0x40, 0x02, 0x42, 0x40, + 0x42, 0x42, 0x02, 0x02, 0x40, 0x40, 0x40, 0x40, + 0x40, 0x42, 0x42, 0x40, 0x02, 0x02, 0x42, 0x42, + 0x40, 0x40, 0x40, 0x40, 0x4b, 0x4b, 0x4b, 0x02, + 0x02, 0x40, 0x42, 0x42, 0x02, 0x02, 0x02, 0x02, + 0x40, 0x42, 0x42, 0x40, 0x42, 0x42, 0x42, 0x0a, + 0x44, 0x40, 0x40, 0x02, 0x40, 0x40, 0x40, 0x40, + 0x42, 0x42, 0x02, 0x02, 0x02, 0x40, 0x02, 0x40, + 0x42, 0x42, 0x42, 0x42, 0x42, 0x42, 0x42, 0x42, + 0x40, 0x40, 0x42, 0x42, 0x40, 0x40, 0x40, 0x40, + 0x02, 0x40, 0x42, 0x02, 0x02, 0x02, 0x02, 0x42, + 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x40, 0x02, + 0x02, 0x42, 0x42, 0x02, 0x02, 0x40, 0x40, 0x40, + 0x40, 0x02, 0x02, 0x40, 0x40, 0x02, 0x40, 0x42, + 0x02, 0x02, 0x40, 0x85, 0x85, 0x85, 0x85, 0x85, + 0x85, 0x85, 0x85, 0x46, 0x46, 0x46, 0x46, 0x46, + 0x46, 0x46, 0x46, 0x47, 0x47, 0x47, 0x47, 0x47, + 0x47, 0x47, 0x47, 0x40, 0x40, 0x02, 0x02, 0x02, + 0x42, 0x40, 0x40, 0x02, 0x02, 0x42, 0x40, 0x40, + 0x40, 0x40, 0x02, 0x02, 0x42, 0x02, 0x42, 0x42, + 0x42, 0x42, 0x42, 0x42, 0x02, 0x42, 0x40, 0x40, + 0x40, 0x02, 0x02, 0x02, 0x01, 0x02, 0x02, 0x02, + 0x42, 0x42, 0x42, 0x42, 0x02, 0x42, 0x42, 0x42, + 0x40, 0x40, 0x40, 0x40, 0x42, 0x42, 0x02, 0x42, + 0x42, 0x42, 0x42, 0x42, 0x02, 0x02, 0x02, 0x40, + 0x40, 0x40, 0x40, 0x02, 0x42, 0x42, 0x02, 0x40, + 0x40, 0x40, 0x40, 0x40, 0x42, 0x02, 0x42, 0x02, + 0x40, 0x02, 0x40, 0x40, 0x02, 0x02, 0x02, 0x42, + 0x42, 0x42, 0x02, 0x02, 0x02, 0x02, 0x02, 0x40, + 0x40, 0x40, 0x40, 0x02, 0x42, 0x02, 0x02, 0x02, + 0x42, 0x02, 0x42, 0x42, 0x42, 0x40, 0x42, 0x02, + 0x02, 0x02, 0x02, 0x42, 0x42, 0x02, 0x02, 0x42, + 0x02, 0x02, 0x02, 0x40, 0x40, 0x40, 0x40, 0x40, + 0x40, 0x02, 0x42, 0x02, 0x02, 0x42, 0x42, 0x42, + 0x02, 0x42, 0x02, 0x40, 0x40, 0x40, 0x40, 0x42, + 0x42, 0x42, 0x42, 0x02, 0x02, 0x02, 0x02, 0x40, + 0x02, 0x02, 0x02, 0x02, 0x40, 0x40, 0x40, 0x40, + 0x02, 0x40, 0x40, 0x42, 0x40, 0x40, 0x40, 0x01, + 0x02, 0x0d, 0x01, 0x01, 0xc0, 0x40, 0x40, 0xc0, + 0xc0, 0xc0, 0xc0, 0x40, 0x40, 0xc0, 0xc0, 0x40, + 0x40, 0x41, 0x41, 0x01, 0x01, 0x01, 0x01, 0x01, + 0x40, 0xc0, 0x40, 0xc0, 0xc0, 0x40, 0xc0, 0x40, + 0x40, 0x40, 0xc0, 0x4c, 0x40, 0xc0, 0x40, 0x4c, + 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x01, 0x01, + 0x01, 0x01, 0x01, 0x41, 0x01, 0x01, 0x01, 0x01, + 0x01, 0x01, 0x01, 0x01, 0x40, 0x40, 0x40, 0xc0, + 0x40, 0xc0, 0x40, 0x40, 0xc0, 0xcc, 0x40, 0x40, + 0x40, 0xc0, 0x40, 0xc0, 0xc0, 0xc0, 0xc0, 0xcc, + 0xcc, 0xcc, 0xcc, 0x40, 0x40, 0x40, 0x40, 0x40, + 0x40, 0x4c, 0x4c, 0x40, 0x40, 0x40, 0x40, 0x40, + 0xc0, 0x40, 0xc0, 0x40, 0x40, 0x40, 0xc0, 0x40, + 0x40, 0xc0, 0x40, 0x40, 0x40, 0xc0, 0x40, 0x40, + 0xc0, 0xc0, 0xc0, 0xc0, 0xc0, 0x40, 0xc0, 0x40, + 0x40, 0x40, 0xc0, 0x40, 0x40, 0x40, 0xc0, 0xc0, + 0x40, 0x40, 0xc0, 0xc0, 0xc0, 0xc0, 0x40, 0x40, + 0x8c, 0x8c, 0x40, 0x40, 0x40, 0x40, 0x4c, 0x80, + 0x80, 0x40, 0x40, 0x40, 0x40, 0x40, 0x4c, 0x40, + 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x4c, 0x40, + 0x8c, 0x8c, 0x8c, 0x8c, 0x4c, 0x4c, 0x4c, 0x8c, + 0x4c, 0x4c, 0x8c, 0x40, 0x40, 0x40, 0x40, 0x4c, + 0x4c, 0x4c, 0x40, 0x40, 0x40, 0x40, 0x40, 0xc0, + 0xc0, 0xcc, 0xc0, 0xc0, 0xc0, 0xc0, 0xc0, 0x4c, + 0x4c, 0x40, 0x40, 0x40, 0x40, 0xc0, 0xc0, 0x40, + 0x40, 0xcc, 0xc0, 0x40, 0x40, 0x40, 0x40, 0xc0, + 0xc0, 0x40, 0x40, 0xc0, 0x40, 0x40, 0xc0, 0xc0, + 0x40, 0x40, 0x40, 0x4c, 0x4c, 0x8c, 0x8c, 0x40, + 0x4c, 0x4c, 0x4c, 0x4c, 0x4c, 0xcc, 0xc0, 0x4c, + 0xcc, 0x4c, 0x4c, 0x4c, 0x4c, 0xcc, 0xcc, 0x4c, + 0x4c, 0x4c, 0x40, 0x8c, 0x8c, 0x4c, 0x4c, 0x4c, + 0x4c, 0xcc, 0x4c, 0xcc, 0x4c, 0x4c, 0x4c, 0x4c, + 0x4c, 0x4c, 0x4c, 0x4c, 0x8c, 0x8c, 0x8c, 0x8c, + 0x8c, 0x8c, 0x8c, 0x8c, 0x4c, 0x4c, 0x4c, 0x4c, + 0xcc, 0xcc, 0x4c, 0xcc, 0xcc, 0xcc, 0x4c, 0xcc, + 0xcc, 0x4c, 0xcc, 0x4c, 0x4c, 0x4c, 0x4c, 0x4c, + 0x4c, 0x40, 0x40, 0x80, 0x80, 0x80, 0x80, 0x80, + 0x80, 0x4c, 0x4c, 0x4c, 0x8c, 0x4c, 0x4c, 0x4c, + 0x4c, 0x4c, 0x4c, 0xcc, 0xcc, 0x4c, 0x4c, 0x8c, + 0x8c, 0x4c, 0x4c, 0x4c, 0x4c, 0x4c, 0x8c, 0x8c, + 0xcc, 0xcc, 0xcc, 0xcc, 0xcc, 0xcc, 0x8c, 0xcc, + 0xcc, 0xcc, 0xcc, 0xcc, 0xcc, 0xcc, 0xcc, 0x8c, + 0x8c, 0xcc, 0x8c, 0xcc, 0xcc, 0x8c, 0xcc, 0xcc, + 0x8c, 0xcc, 0xcc, 0x4c, 0x4c, 0x4c, 0x4c, 0x4c, + 0x8c, 0x40, 0x40, 0x4c, 0x4c, 0x4c, 0x40, 0x4c, + 0x40, 0x4c, 0x40, 0x8c, 0x40, 0x40, 0x40, 0x40, + 0x40, 0x40, 0x40, 0x4c, 0x40, 0x40, 0x4c, 0x40, + 0x40, 0x40, 0x40, 0x8c, 0x40, 0x8c, 0x40, 0x40, + 0x40, 0x8c, 0x8c, 0x8c, 0x40, 0x8c, 0x40, 0x40, + 0x40, 0x4c, 0x4c, 0x4c, 0x4c, 0x4c, 0x40, 0x40, + 0x40, 0x40, 0x40, 0x8c, 0x8c, 0x8c, 0x40, 0x40, + 0x40, 0x40, 0x40, 0x40, 0x40, 0x8c, 0x40, 0x40, + 0x40, 0x40, 0x40, 0x4c, 0x4c, 0x4c, 0x40, 0x40, + 0x40, 0x8c, 0x8c, 0x40, 0x40, 0x40, 0x40, 0x8c, + 0xc0, 0xc0, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, + 0x80, 0x80, 0x40, 0x80, 0x80, 0x80, 0x80, 0x80, + 0x40, 0x40, 0x40, 0x40, 0x80, 0x80, 0x80, 0x80, + 0x80, 0x80, 0x40, 0x40, 0x80, 0x80, 0x02, 0x02, + 0x02, 0x02, 0x82, 0x82, 0x8c, 0x80, 0x80, 0x80, + 0x80, 0x80, 0x80, 0x80, 0x8c, 0x80, 0x40, 0x80, + 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x40, 0x02, + 0x02, 0x80, 0x80, 0x80, 0x80, 0x80, 0x40, 0x40, + 0x40, 0x40, 0x40, 0x80, 0x80, 0x80, 0x40, 0x40, + 0x40, 0x40, 0x40, 0x40, 0x40, 0x80, 0x8c, 0x80, + 0x80, 0x80, 0x80, 0x80, 0x80, 0x02, 0x02, 0x02, + 0x02, 0x02, 0x02, 0x40, 0x40, 0x02, 0x40, 0x40, + 0x40, 0x02, 0x40, 0x40, 0x40, 0x42, 0x42, 0x02, + 0x02, 0x42, 0x40, 0x40, 0x40, 0x40, 0x02, 0x40, + 0x40, 0x40, 0x42, 0x42, 0x40, 0x40, 0x40, 0x40, + 0x40, 0x40, 0x42, 0x42, 0x42, 0x42, 0x02, 0x02, + 0x40, 0x40, 0x85, 0x85, 0x85, 0x85, 0x85, 0x40, + 0x40, 0x40, 0x02, 0x42, 0x42, 0x02, 0x02, 0x42, + 0x42, 0x02, 0x02, 0x42, 0x42, 0x40, 0x40, 0x40, + 0x40, 0x40, 0x40, 0x40, 0x02, 0x02, 0x02, 0x02, + 0x02, 0x02, 0x42, 0x02, 0x02, 0x42, 0x42, 0x02, + 0x02, 0x40, 0x40, 0x40, 0x40, 0x02, 0x42, 0x40, + 0x40, 0x02, 0x40, 0x02, 0x02, 0x02, 0x40, 0x40, + 0x02, 0x40, 0x40, 0x40, 0x42, 0x02, 0x02, 0x42, + 0x42, 0x40, 0x40, 0x40, 0x40, 0x40, 0x42, 0x02, + 0x40, 0x02, 0x42, 0x42, 0x40, 0x42, 0x02, 0x40, + 0x40, 0x88, 0x89, 0x89, 0x89, 0x89, 0x89, 0x89, + 0x89, 0x89, 0x88, 0x89, 0x89, 0x89, 0x89, 0x40, + 0x40, 0x40, 0x40, 0x46, 0x46, 0x46, 0x46, 0x46, + 0x46, 0x46, 0x40, 0x40, 0x40, 0x47, 0x47, 0x47, + 0x47, 0x47, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, + 0x40, 0x01, 0x41, 0x01, 0x01, 0x01, 0x40, 0xc0, + 0x40, 0x40, 0x02, 0x02, 0x02, 0x40, 0x02, 0x02, + 0x40, 0x40, 0x40, 0x02, 0x02, 0x40, 0x40, 0x40, + 0x02, 0x02, 0x02, 0x02, 0x40, 0x40, 0x42, 0x02, + 0x42, 0x40, 0x40, 0x40, 0x40, 0x40, 0x02, 0x40, + 0x40, 0x02, 0x02, 0x40, 0x40, 0x40, 0x42, 0x42, + 0x42, 0x02, 0x02, 0x02, 0x02, 0x42, 0x02, 0x02, + 0x40, 0x40, 0x04, 0x40, 0x40, 0x42, 0x40, 0x44, + 0x44, 0x40, 0x40, 0x40, 0x40, 0x02, 0x02, 0x02, + 0x02, 0x40, 0x42, 0x02, 0x40, 0x40, 0x40, 0x40, + 0x42, 0x42, 0x42, 0x02, 0x02, 0x42, 0x42, 0x02, + 0x42, 0x02, 0x02, 0x40, 0x40, 0x40, 0x02, 0x02, + 0x40, 0x42, 0x42, 0x02, 0x42, 0x42, 0x42, 0x42, + 0x40, 0x40, 0x42, 0x42, 0x42, 0x40, 0x40, 0x42, + 0x42, 0x40, 0x40, 0x02, 0x02, 0x40, 0x42, 0x40, + 0x40, 0x42, 0x40, 0x42, 0x42, 0x42, 0x40, 0x42, + 0x42, 0x02, 0x42, 0x02, 0x44, 0x02, 0x40, 0x40, + 0x40, 0x40, 0x40, 0x02, 0x02, 0x40, 0x40, 0x40, + 0x40, 0x40, 0x42, 0x42, 0x02, 0x02, 0x02, 0x42, + 0x02, 0x40, 0x02, 0x42, 0x02, 0x42, 0x42, 0x42, + 0x42, 0x02, 0x42, 0x02, 0x02, 0x40, 0x40, 0x40, + 0x40, 0x42, 0x42, 0x42, 0x42, 0x02, 0x02, 0x42, + 0x02, 0x02, 0x02, 0x42, 0x42, 0x02, 0x42, 0x02, + 0x40, 0x40, 0x40, 0x02, 0x42, 0x02, 0x42, 0x42, + 0x40, 0x40, 0x02, 0x02, 0x02, 0x02, 0x42, 0x02, + 0x42, 0x42, 0x42, 0x42, 0x42, 0x42, 0x40, 0x42, + 0x40, 0x40, 0x02, 0x02, 0x42, 0x02, 0x44, 0x42, + 0x44, 0x42, 0x02, 0x40, 0x40, 0x40, 0x40, 0x02, + 0x02, 0x42, 0x42, 0x42, 0x42, 0x02, 0x40, 0x40, + 0x40, 0x42, 0x40, 0x40, 0x40, 0x02, 0x42, 0x44, + 0x02, 0x02, 0x02, 0x02, 0x40, 0x40, 0x40, 0x40, + 0x44, 0x44, 0x44, 0x44, 0x02, 0x02, 0x02, 0x02, + 0x02, 0x02, 0x40, 0x42, 0x02, 0x02, 0x02, 0x02, + 0x02, 0x02, 0x42, 0x02, 0x02, 0x42, 0x02, 0x02, + 0x40, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x40, + 0x40, 0x02, 0x40, 0x02, 0x02, 0x40, 0x02, 0x02, + 0x02, 0x02, 0x02, 0x02, 0x44, 0x02, 0x40, 0x40, + 0x42, 0x42, 0x42, 0x42, 0x42, 0x40, 0x02, 0x02, + 0x40, 0x42, 0x42, 0x02, 0x42, 0x02, 0x40, 0x40, + 0x40, 0x02, 0x02, 0x42, 0x42, 0x40, 0x02, 0x02, + 0x44, 0x42, 0x40, 0x40, 0x40, 0x40, 0x02, 0x02, + 0x02, 0x40, 0x40, 0x40, 0x42, 0x42, 0x02, 0x42, + 0x02, 0x40, 0x40, 0x40, 0x40, 0x40, 0x46, 0x40, + 0x40, 0x40, 0x46, 0x46, 0x46, 0x40, 0x40, 0x40, + 0x40, 0x40, 0x80, 0x80, 0x80, 0x80, 0x02, 0x40, + 0x40, 0x40, 0x82, 0x82, 0x40, 0x40, 0x40, 0x40, + 0x40, 0x40, 0x80, 0x80, 0x80, 0x80, 0x40, 0x80, + 0x80, 0x40, 0x40, 0x80, 0x40, 0x40, 0x40, 0x40, + 0x40, 0x80, 0x80, 0x80, 0x40, 0x40, 0x80, 0x40, + 0x40, 0x01, 0x01, 0x01, 0x01, 0x40, 0x40, 0x40, + 0x40, 0x42, 0x42, 0x42, 0x01, 0x01, 0x01, 0x01, + 0x01, 0x02, 0x02, 0x02, 0x02, 0x02, 0x40, 0x40, + 0x02, 0x02, 0x02, 0x02, 0x02, 0x4c, 0x4c, 0x4c, + 0x4c, 0x8c, 0x4c, 0x4c, 0x4c, 0xc0, 0xc0, 0xc0, + 0x40, 0x40, 0x4c, 0x4c, 0x4c, 0xc0, 0xc0, 0xc0, + 0xc0, 0xc0, 0xc0, 0x40, 0x4c, 0xc0, 0xc0, 0x40, + 0x40, 0x4c, 0x4c, 0x4c, 0x4c, 0xcc, 0xcc, 0xc0, + 0xc0, 0xc0, 0xc0, 0xc0, 0xc0, 0xcc, 0xcc, 0xc0, + 0xc0, 0xc0, 0xc0, 0xc0, 0xc0, 0x8c, 0xc0, 0x8c, + 0x8c, 0x8c, 0x8c, 0x8c, 0x8c, 0x8c, 0xc0, 0xc0, + 0xc0, 0xc0, 0xc0, 0x4c, 0x4c, 0x4c, 0x4c, 0x4c, + 0x4c, 0x43, 0x43, 0x43, 0x43, 0x43, 0x43, 0x43, + 0x43, 0x80, 0x8c, 0x8c, 0x4c, 0x4c, 0x4c, 0x4c, + 0x4c, 0x80, 0x80, 0x8c, 0x80, 0x80, 0x80, 0x80, + 0x80, 0x8c, 0x8c, 0x8c, 0x8c, 0x8c, 0x8c, 0x80, + 0x4c, 0x4c, 0x4c, 0x4c, 0x4c, 0x4c, 0x4c, 0x8c, + 0x8c, 0x4c, 0x4c, 0x4c, 0x4c, 0x4c, 0x4c, 0x4c, + 0x8c, 0x8c, 0x8c, 0x8c, 0x8c, 0x8c, 0x4c, 0x8c, + 0x8c, 0x8c, 0x4c, 0x4c, 0x4c, 0x4c, 0x8c, 0x4c, + 0x4c, 0x4c, 0x8c, 0x4c, 0x4c, 0x4c, 0x8c, 0x8c, + 0x8c, 0x82, 0x82, 0x82, 0x82, 0x82, 0x8c, 0x4c, + 0x8c, 0x8c, 0x8c, 0x8c, 0x8c, 0x8c, 0x4c, 0x4c, + 0x8c, 0x8c, 0x8c, 0x8c, 0x8c, 0x8c, 0x40, 0x40, + 0x4c, 0x4c, 0x4c, 0x8c, 0x8c, 0x8c, 0x8c, 0x4c, + 0x40, 0x40, 0x40, 0x40, 0x4c, 0x4c, 0x4c, 0x4c, + 0x4c, 0x4c, 0x40, 0x40, 0x40, 0x40, 0x8c, 0x8c, + 0x8c, 0x8c, 0x40, 0x8c, 0x8c, 0x8c, 0x8c, 0x8c, + 0x8c, 0x40, 0x8c, 0x41, 0x01, 0x41, 0x41, 0x41, + 0x41, 0x41, 0x41, 0xc0, 0xc0, 0xc0, 0xc0, 0xc0, + 0xc0, 0x40, 0x40, +}; +static const uint32_t s_joinRules[2][16] = { + { + 0x03cfffcf, 0x0fffffff, 0x03cfffcf, 0x03cfff4f, + 0x0000000c, 0x03c0c3cf, 0x03cf0fcf, 0x03cf3fcf, + 0x03cf0fcf, 0x03cf3fcf, 0x030fffcf, 0x03cfffcf, + 0x03cfffcf, 0x00cfffcf, 0x00000000, 0x00000000, + }, + { + 0x03cfffcf, 0x0fffffff, 0x03cfffcf, 0x03cfffcf, + 0x0000000c, 0x03c0c3cf, 0x03cf0fcf, 0x03cf3fcf, + 0x03cf0fcf, 0x03cf3fcf, 0x030fffcf, 0x03cfffcf, + 0x03cfffcf, 0x00cfffcf, 0x00000000, 0x00000000, + }, +}; +static int ucdLookup(const char32_t cp) +{ + const uint16_t s0 = s_stage0[cp >> 11]; + const uint16_t s1 = s_stage1[s0 + ((cp >> 6) & 31)]; + const uint16_t s2 = s_stage2[s1 + ((cp >> 3) & 7)]; + const uint8_t s3 = s_stage3[s2 + ((cp >> 0) & 7)]; + return s3; +} +static int ucdGraphemeJoins(const int state, const int lead, const int trail) +{ + const int l = lead & 15; + const int t = trail & 15; + return (s_joinRules[state][l] >> (t * 2)) & 3; +} +static bool ucdGraphemeDone(const int state) +{ + return state == 3; +} +static int ucdToCharacterWidth(const int val) +{ + return val >> 6; +} +// clang-format on +// End of generated code + +/* + * This file is part of mpv. + * + * mpv is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * mpv is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with mpv. If not, see . + */ + +int term_disp_width(bstr str) +{ + static const int ambiguous_width = 1; + + int width = 0; + + while (str.len) { + if (bstr_eatstart0(&str, "\033[")) { + while (str.len && !((*str.start >= '@' && *str.start <= '~') || *str.start == 'm')) + str = bstr_cut(str, 1); + str = bstr_cut(str, 1); + continue; + } + + int cp = bstr_decode_utf8(str, &str); + + // Stop processing on any invalid input + if (cp < 0) + return 0; + + if (cp == '\r') { + width = 0; + continue; + } + + if (cp < 0x20) + continue; + + if (cp <= 0x7E) { + width++; + continue; + } + + int grapheme_width = 0; + int state = 0; + + while (true) { + int lead = ucdLookup(cp); + int w = ucdToCharacterWidth(lead); + if (w == 3) + w = ambiguous_width; + + // U+FE0F Variation Selector-16 is used to turn unqualified Emojis into qualified ones. + // By convention, this turns them from being ambiguous width (= narrow) into wide ones. + // We achieve this here by explicitly giving this codepoint a wide width. + // Later down below we'll clamp width back to <= 2. + if (cp == 0xFE0F) + w = 2; + + grapheme_width += w; + + if (!str.len) + break; + + // Fetch next codepoint in grapheme cluster + bstr cluster_end; + cp = bstr_decode_utf8(str, &cluster_end); + // Stop processing on any invalid input + if (cp < 0) + return 0; + int trail = ucdLookup(cp); + + state = ucdGraphemeJoins(state, lead, trail); + if (ucdGraphemeDone(state)) + break; + + str = cluster_end; + } + width += grapheme_width > 2 ? 2 : grapheme_width; + } + + return width; +} diff --git a/misc/codepoint_width.h b/misc/codepoint_width.h new file mode 100644 index 0000000000..6bb068fd7d --- /dev/null +++ b/misc/codepoint_width.h @@ -0,0 +1,11 @@ +#pragma once + +#include "misc/bstr.h" + +/** + * @brief Determines the number of columns required to display a given string. + * + * @param str Sequence of UTF-8 chars + * @return int width of the string + */ +int term_disp_width(bstr str); diff --git a/test/codepoint_width.c b/test/codepoint_width.c new file mode 100644 index 0000000000..86ae6dfe3f --- /dev/null +++ b/test/codepoint_width.c @@ -0,0 +1,67 @@ +/* + * This file is part of mpv. + * + * mpv is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * mpv is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with mpv. If not, see . + */ + +#include "test_utils.h" + +#include "misc/codepoint_width.h" + +#define W(s) term_disp_width((bstr)bstr0_lit(s)) + +int main(void) { + assert_int_equal(W("A"), 1); // Single ASCII character + assert_int_equal(W("ABC"), 3); // Multiple ASCII characters + + assert_int_equal(W("\u3042"), 2); // Full-width Japanese Hiragana 'あ' (U+3042) + assert_int_equal(W("\u4F60"), 2); // Full-width Chinese character '你' (U+4F60) + assert_int_equal(W("\u4F60\u597D"), 4); // Two full-width Chinese characters '你好' (U+4F60 U+597D) + + assert_int_equal(W("a\u0301"), 1); // 'a' + combining acute accent '́' (U+0301) + assert_int_equal(W("e\u0301"), 1); // 'e' + combining acute accent '́' (U+0301) + assert_int_equal(W("a\u0308"), 1); // 'a' + combining diaeresis '̈' (U+0308) + + // Family emoji: 👩‍❤️‍👩 (Woman + ZWJ + Heart + ZWJ + Woman; + // Code points: U+1F469 U+200D U+2764 U+FE0F U+200D U+1F469) + assert_int_equal(W("\U0001F469\u200D\u2764\uFE0F\u200D\U0001F469"), 2); + // Man emoji with skin tone modifier + assert_int_equal(W("\U0001F468\U0001F3FE"), 2); + // Person Shrugging + skin tone modifier + ZWJ + woman sign + variant selector + assert_int_equal(W("\U0001F937\U0001F3FE\u200D\U00002640\U0000FE0F"), 2); + // Regional indicator symbols forming the flag of Poland + assert_int_equal(W("\U0001F1F5\U0001F1F1"), 2); + + assert_int_equal(W("\n"), 0); // Newline (should not take up any visual space) + assert_int_equal(W("\t"), 0); // Tab (no visual width itself) + assert_int_equal(W("\0"), 0); // Null character (non-visible) + + assert_int_equal(W("A\u3042"), 3); // ASCII 'A' + full-width Japanese Hiragana 'あ' (U+3042) + assert_int_equal(W("a\u0301A"), 2); // Combining character 'á' (a + U+0301) and ASCII 'A' + + // Grapheme cluster + ASCII 'A' + full-width Japanese Hiragana 'あ' (U+3042) + assert_int_equal(W("\U0001F469\u200D\u2764\uFE0F\u200D\U0001F469A\u3042"), 5); + + assert_int_equal(W("A\nB"), 2); // ASCII characters with newline (newline should not affect width) + assert_int_equal(W("ABC\tDEF"), 6); // Tab inside a string (no visual width for '\t') + + // ASCII characters with color + assert_int_equal(W("\033[31mABC\033[0m\033[32mDEF\033[0m"), 6); + + // ASCII characters with color and a newline + assert_int_equal(W("\033[31mABC\033[0m\033[32mDEF\033[0m\n"), 6); + + // ASCII characters with carriage return + assert_int_equal(W("ABC\rDEF"), 3); +} diff --git a/test/meson.build b/test/meson.build index 9a0daf687c..41ff111250 100644 --- a/test/meson.build +++ b/test/meson.build @@ -105,6 +105,11 @@ test('format', format) language = executable('language', files('language.c'), include_directories: incdir, link_with: test_utils) test('language', language) +codepoint_width = executable('codepoint-width', files('codepoint_width.c'), + objects: libmpv.extract_objects('misc/codepoint_width.c'), + include_directories: incdir, link_with: test_utils) +test('codepoint-width', codepoint_width) + paths_objects = libmpv.extract_objects('options/path.c', path_source) paths = executable('paths', 'paths.c', include_directories: incdir, objects: paths_objects, link_with: test_utils)