forked from mtcontrib/signs_lib
Full Unicode Plane 0 charset support (63000+ chars, only ~2MB) (#2)
Co-authored-by: SX <50966843+S-S-X@users.noreply.github.com>
This commit is contained in:
54
encoding.lua
54
encoding.lua
@ -1,5 +1,8 @@
|
||||
-- encoding borrowed from signs_lib fork at https://github.com/lord-server/lord
|
||||
|
||||
-- The "ANSI" encoding here actually refers to "windows-1251", which shows up as
|
||||
-- "ANSI" on Russian version of MS Windows
|
||||
|
||||
local ansi_decode = {
|
||||
[128] = "\208\130",
|
||||
[129] = "\208\131",
|
||||
@ -256,9 +259,13 @@ function AnsiToUtf8(s)
|
||||
end
|
||||
|
||||
function Utf8ToAnsi(s)
|
||||
local a, j, r, b, scope = 0, 0, ""
|
||||
local r, b = ""
|
||||
local scope
|
||||
local j, l, u
|
||||
for i = 1, s and s:len() or 0 do
|
||||
b = s:byte(i)
|
||||
|
||||
-- legacy parser
|
||||
if b == 0x26 then
|
||||
r = r .. "&"
|
||||
elseif b < 128 then
|
||||
@ -272,17 +279,56 @@ function Utf8ToAnsi(s)
|
||||
scope = scope[b]
|
||||
if "string" == type(scope) then
|
||||
r, scope = r .. scope
|
||||
j = -1 -- supress general UTF-8 parser
|
||||
end
|
||||
else
|
||||
r, scope = r .. "_"
|
||||
scope = nil
|
||||
end
|
||||
elseif utf8_decode[b] then
|
||||
scope = utf8_decode[b]
|
||||
else
|
||||
r = r .. "_"
|
||||
end
|
||||
|
||||
-- general UTF-8 parser
|
||||
if j == -1 then -- supressed by legacy parser
|
||||
j, l, u = nil
|
||||
elseif b < 0x80 then
|
||||
if j then
|
||||
r = r .. "&#ufffd;"
|
||||
j, l, u = nil
|
||||
end
|
||||
-- ASCII handled by legacy parser
|
||||
elseif b >= 0xc0 then
|
||||
if j then
|
||||
r = r .. "&#ufffd;"
|
||||
end
|
||||
j = i
|
||||
if b >= 0xf8 then
|
||||
r = r .. "&#ufffd;"
|
||||
j, l, u = nil
|
||||
elseif b >= 0xf0 then
|
||||
l, u = 4, b % (2 ^ 3)
|
||||
elseif b >= 0xe0 then
|
||||
l, u = 3, b % (2 ^ 4)
|
||||
else
|
||||
l, u = 2, b % (2 ^ 5)
|
||||
end
|
||||
else
|
||||
if j then
|
||||
u = u * (2 ^ 6) + b % (2 ^ 6)
|
||||
if i == j + l - 1 then
|
||||
r = r .. string.format("&#u%x;", u)
|
||||
j, l, u = nil
|
||||
end
|
||||
else
|
||||
r = r .. "&#ufffd;"
|
||||
end
|
||||
end
|
||||
end
|
||||
if j then
|
||||
r = r .. "&#ufffd;"
|
||||
end
|
||||
return r
|
||||
end
|
||||
|
||||
signs_lib.wide_character_codes = wide_character_codes
|
||||
signs_lib.unifont_halfwidth = dofile(signs_lib.path.."/unifont-halfwidth.lua")
|
||||
|
Reference in New Issue
Block a user