| 1 |
-- |
| 2 |
-- File ctex-zhconv.lua |
| 3 |
-- |
| 4 |
-- Copyright (C) 2020 by Qing Lee <[email protected]> |
| 5 |
-------------------------------------------------------------------------- |
| 6 |
-- |
| 7 |
-- This work may be distributed and/or modified under the |
| 8 |
-- conditions of the LaTeX Project Public License, either |
| 9 |
-- version 1.3c of this license or (at your option) any later |
| 10 |
-- version. This version of this license is in |
| 11 |
-- http://www.latex-project.org/lppl/lppl-1-3c.txt |
| 12 |
-- and the latest version of this license is in |
| 13 |
-- http://www.latex-project.org/lppl.txt |
| 14 |
-- and version 1.3 or later is part of all distributions of |
| 15 |
-- LaTeX version 2005/12/01 or later. |
| 16 |
-- |
| 17 |
-- This work has the LPPL maintenance status "maintained". |
| 18 |
-- |
| 19 |
-- The Current Maintainer of this work is Qing Lee. |
| 20 |
-- |
| 21 |
-- This work consists of the files ctex-zhconv.lua, ctex-zhconv-make.lua |
| 22 |
-- and the derived file ctex-zhconv-index.lua. |
| 23 |
-- |
| 24 |
-------------------------------------------------------------------------- |
| 25 |
-- |
| 26 |
|
| 27 |
if status.kpse_used ~= 1 then |
| 28 |
kpse.set_program_name("luatex") |
| 29 |
end |
| 30 |
|
| 31 |
local zhconv = { } |
| 32 |
zhconv.module = { |
| 33 |
name = "ctex-zhconv", |
| 34 |
version = "0.2", |
| 35 |
date = "2020/05/02", |
| 36 |
description = "GBK/GB18030/Big5 encoder", |
| 37 |
author = "Qing Lee", |
| 38 |
copyright = "Qing Lee", |
| 39 |
license = "LPPL v1.3c" |
| 40 |
} |
| 41 |
|
| 42 |
local utf = require("unicode").utf8 |
| 43 |
local ubyte, ugsub = utf.byte, utf.gsub |
| 44 |
|
| 45 |
local floor = math.floor |
| 46 |
local unpack, insert, sort = table.unpack, table.insert, table.sort |
| 47 |
local char, format = string.char, string.format |
| 48 |
|
| 49 |
zhconv.index = require("ctex-zhconv-index") |
| 50 |
local index = zhconv.index |
| 51 |
|
| 52 |
zhconv.mapping = { } |
| 53 |
local mapping = zhconv.mapping |
| 54 |
|
| 55 |
mapping.big5, mapping.gbk, mapping.gb18030 = { }, { }, { } |
| 56 |
local gbk, gb18030, big5 = mapping.gbk, mapping.gb18030, mapping.big5 |
| 57 |
|
| 58 |
zhconv.bytes = { } |
| 59 |
local bytes = zhconv.bytes |
| 60 |
|
| 61 |
-- Let lead be pointer / 190 + 0x81. |
| 62 |
-- Let trail be pointer % 190. |
| 63 |
-- Let offset be 0x40 if trail is less than 0x3F and 0x41 otherwise. |
| 64 |
-- Return two bytes whose values are lead and trail + offset. |
| 65 |
function bytes.gbk (pointer) |
| 66 |
local lead = floor(pointer / 190) + 0x81 |
| 67 |
local trail = pointer % 190 |
| 68 |
local offset = trail < 0x3F and 0x40 or 0x41 |
| 69 |
return format("%c%c", lead, trail + offset) |
| 70 |
end |
| 71 |
|
| 72 |
-- Let lead be pointer / 157 + 0x81. |
| 73 |
-- If lead is less than 0xA1, return error with code point. |
| 74 |
-- Let trail be pointer % 157. |
| 75 |
-- Let offset be 0x40 if trail is less than 0x3F and 0x62 otherwise. |
| 76 |
-- Return two bytes whose values are lead and trail + offset. |
| 77 |
function bytes.big5 (pointer) |
| 78 |
local lead = floor(pointer / 157) + 0x81 |
| 79 |
local trail = pointer % 157 |
| 80 |
local offset = trail < 0x3F and 0x40 or 0x62 |
| 81 |
return format("%c%c", lead, trail + offset) |
| 82 |
end |
| 83 |
|
| 84 |
-- Let byte1 be pointer / 10 / 126 / 10. |
| 85 |
-- Set pointer to pointer - byte1 × 10 × 126 × 10. |
| 86 |
-- Let byte2 be pointer / 10 / 126. |
| 87 |
-- Set pointer to pointer - byte2 × 10 × 126. |
| 88 |
-- Let byte3 be pointer / 10. |
| 89 |
-- Let byte4 be pointer - byte3 × 10. |
| 90 |
-- Return four bytes whose values are byte1 + 0x81, byte2 + 0x30, byte3 + 0x81, byte4 + 0x30. |
| 91 |
function bytes.gb18030 (pointer) |
| 92 |
local pointer = pointer |
| 93 |
local byte1 = floor(pointer / 12600) + 0x81 |
| 94 |
pointer = pointer % 12600 |
| 95 |
local byte2 = floor(pointer / 1260) + 0x30 |
| 96 |
pointer = pointer % 1260 |
| 97 |
local byte3 = floor(pointer / 10) + 0x81 |
| 98 |
local byte4 = pointer % 10 + 0x30 |
| 99 |
return format("%c%c%c%c", byte1, byte2, byte3, byte4) |
| 100 |
end |
| 101 |
|
| 102 |
-- Let offset be the last code point in index gb18030 ranges that is equal to or |
| 103 |
-- less than code point and let pointer offset be its corresponding pointer. |
| 104 |
-- Return a pointer whose value is pointer offset + code point - offset. |
| 105 |
do |
| 106 |
local metatable = { } |
| 107 |
local bytes, ranges = bytes.gb18030, index["gb18030_ranges"] |
| 108 |
function metatable.__index (t, key) |
| 109 |
if type(key) == "number" then |
| 110 |
local n = #ranges |
| 111 |
if key < 0x10000 then |
| 112 |
local s = gbk[key] |
| 113 |
if s then return s end |
| 114 |
repeat |
| 115 |
n = n - 1 |
| 116 |
until ranges[n][2] <= key |
| 117 |
end |
| 118 |
local pointer, offset = unpack(ranges[n]) |
| 119 |
return bytes(pointer + key - offset) |
| 120 |
end |
| 121 |
end |
| 122 |
gb18030 = setmetatable(gb18030, metatable) |
| 123 |
end |
| 124 |
|
| 125 |
-- The index Big5 pointer for code point is the return value of these steps: |
| 126 |
-- 1. Let index be index Big5 excluding all entries whose pointer |
| 127 |
-- is less than (0xA1 - 0x81) × 157. |
| 128 |
-- 2. If code point is U+2550, U+255E, U+2561, U+256A, U+5341, or U+5345, |
| 129 |
-- return the last pointer corresponding to code point in index. |
| 130 |
local big5_last = { |
| 131 |
[0x2550] = true , |
| 132 |
[0x255E] = true , |
| 133 |
[0x2561] = true , |
| 134 |
[0x256A] = true , |
| 135 |
[0x5341] = true , |
| 136 |
[0x5345] = true , |
| 137 |
} |
| 138 |
|
| 139 |
do |
| 140 |
|
| 141 |
local function spairs (t) |
| 142 |
local a = { } |
| 143 |
for i, n in pairs(t) do insert(a, {i, n}) end |
| 144 |
sort(a, function(b, c) return b[1] < c[1] end) |
| 145 |
local i = 0 |
| 146 |
local iter = function () |
| 147 |
i = i + 1 |
| 148 |
if a[i] then |
| 149 |
return a[i][1], a[i][2] |
| 150 |
end |
| 151 |
end |
| 152 |
return iter |
| 153 |
end |
| 154 |
|
| 155 |
local chars = bytes.big5 |
| 156 |
local m = (0xA1 - 0x81) * 157 |
| 157 |
for i, v in spairs(index.big5) do |
| 158 |
if i >= m then |
| 159 |
if big5[v] then |
| 160 |
if big5_last[v] then big5[v] = chars(i) end |
| 161 |
else |
| 162 |
big5[v] = chars(i) |
| 163 |
end |
| 164 |
end |
| 165 |
end |
| 166 |
|
| 167 |
local chars = bytes.gbk |
| 168 |
for i, v in pairs(index.gb18030) do |
| 169 |
gbk[v] = chars(i) |
| 170 |
end |
| 171 |
|
| 172 |
end |
| 173 |
|
| 174 |
-- If the gbk flag is set and code point is U+20AC, return byte 0x80. |
| 175 |
gb18030[0x20AC] = gbk[0x20AC] |
| 176 |
gbk[0x20AC] = char(0x80) |
| 177 |
|
| 178 |
local io_open = io.open |
| 179 |
local encode_error = "Encoding %q not available!" |
| 180 |
local file_error = "Open file %q failed!" |
| 181 |
|
| 182 |
function zhconv.conv (encoding, input, output) |
| 183 |
local encoding = encoding:lower() |
| 184 |
local mapping = assert(mapping[encoding], encode_error:format(encoding)) |
| 185 |
local encoder = function (s) |
| 186 |
local code_point = ubyte(s) |
| 187 |
return code_point > 0x7F and mapping[code_point] |
| 188 |
end |
| 189 |
if output then |
| 190 |
local handle = assert(io_open(input, "rb"), file_error:format(input)) |
| 191 |
local stream = handle:read("*all") |
| 192 |
handle:close() |
| 193 |
handle = assert(io_open(output, "wb"), file_error:format(output)) |
| 194 |
stream = stream:gsub("^\xEF\xBB\xBF", "") |
| 195 |
stream = ugsub(stream, ".", encoder) |
| 196 |
handle:write(stream) |
| 197 |
handle:close() |
| 198 |
else |
| 199 |
local s = ugsub(input, ".", encoder) |
| 200 |
return s |
| 201 |
end |
| 202 |
end |
| 203 |
|
| 204 |
return zhconv |