| 1 |
-- luatexko-uhc2utf8.lua |
| 2 |
-- |
| 3 |
-- Copyright (c) 2013-2025 Dohyun Kim <nomosnomos at gmail com> |
| 4 |
-- Soojin Nam <jsunam at gmail com> |
| 5 |
-- |
| 6 |
-- This work may be distributed and/or modified under the |
| 7 |
-- conditions of the LaTeX Project Public License, either version 1.3c |
| 8 |
-- of this license or (at your option) any later version. |
| 9 |
-- The latest version of this license is in |
| 10 |
-- http://www.latex-project.org/lppl.txt |
| 11 |
-- and version 1.3c or later is part of all distributions of LaTeX |
| 12 |
-- version 2006/05/20 or later. |
| 13 |
|
| 14 |
luatexbase.provides_module({ |
| 15 |
name = "luatexko-uhc2utf8", |
| 16 |
version = "3.7", |
| 17 |
date = "2025/02/24", |
| 18 |
author = "Dohyun Kim, Soojin Nam", |
| 19 |
description = "UHC (CP949) input encoding", |
| 20 |
license = "LPPL v1.3+", |
| 21 |
}) |
| 22 |
|
| 23 |
luatexko = luatexko or {} |
| 24 |
luatexko.uhc2utf8 = luatexko.uhc2utf8 or {} |
| 25 |
local luatexkouhc2utf8 = luatexko.uhc2utf8 |
| 26 |
|
| 27 |
local kpse_find_file = kpse.find_file |
| 28 |
local add_to_callback = luatexbase.add_to_callback |
| 29 |
local remove_from_callback = luatexbase.remove_from_callback |
| 30 |
|
| 31 |
local function get_uhc_uni_table() |
| 32 |
local t_uhc2ucs = {} |
| 33 |
local file = kpse_find_file("KSCms-UHC-UCS2","cmap files") |
| 34 |
if file then |
| 35 |
file = io.open(file, "r") |
| 36 |
while true do |
| 37 |
local line = file:read"*line" |
| 38 |
if not line then break end |
| 39 |
local ea,eb,uni = line:match"<(%x+)>%s+<(%x+)>%s+<(%x+)>" |
| 40 |
if ea and eb and uni then |
| 41 |
ea, eb, uni = tonumber(ea,16),tonumber(eb,16),tonumber(uni,16) |
| 42 |
for i=ea,eb do |
| 43 |
t_uhc2ucs[i] = uni |
| 44 |
uni = uni + 1 |
| 45 |
end |
| 46 |
end |
| 47 |
end |
| 48 |
file:close() |
| 49 |
end |
| 50 |
return t_uhc2ucs |
| 51 |
end |
| 52 |
|
| 53 |
local t_uhc2ucs = t_uhc2ucs or get_uhc_uni_table() |
| 54 |
|
| 55 |
local function uhc_to_utf8 (buffer) |
| 56 |
if not buffer then return end |
| 57 |
if utf8.len(buffer) then return end -- check if buffer is already utf-8 |
| 58 |
buffer = buffer:gsub("([\129-\253])([\65-\254])", function(a, b) |
| 59 |
local u = t_uhc2ucs[a:byte() * 256 + b:byte()] |
| 60 |
if u then |
| 61 |
return utf8.char(u) |
| 62 |
end |
| 63 |
end) |
| 64 |
return buffer |
| 65 |
end |
| 66 |
|
| 67 |
local function startconvert () |
| 68 |
add_to_callback('process_input_buffer', uhc_to_utf8, 'luatexko.uhctoutf8', 1) |
| 69 |
end |
| 70 |
luatexkouhc2utf8.startconvert = startconvert |
| 71 |
|
| 72 |
local function stopconvert () |
| 73 |
remove_from_callback('process_input_buffer', 'luatexko.uhctoutf8') |
| 74 |
end |
| 75 |
luatexkouhc2utf8.stopconvert = stopconvert |
| 76 |
|
| 77 |
----------------------------------------- |
| 78 |
-- Hangul Windows OS uses CP949 filenames |
| 79 |
----------------------------------------- |
| 80 |
local function get_uni_uhc_table() |
| 81 |
local t_ucs2uhc = {} |
| 82 |
for i,v in pairs(t_uhc2ucs) do |
| 83 |
t_ucs2uhc[v] = i |
| 84 |
end |
| 85 |
return t_ucs2uhc |
| 86 |
end |
| 87 |
|
| 88 |
local t_ucs2uhc = t_ucs2uhc or get_uni_uhc_table() |
| 89 |
|
| 90 |
local function utf8_to_uhc (name) |
| 91 |
if not name then return end |
| 92 |
local t = {} |
| 93 |
for _, u in utf8.codes(name) do |
| 94 |
if u >= 0xA1 and u <= 0xFFE6 then |
| 95 |
local c = t_ucs2uhc[u] |
| 96 |
if c then |
| 97 |
t[#t + 1] = c // 256 |
| 98 |
t[#t + 1] = c % 256 |
| 99 |
else |
| 100 |
t[#t + 1] = u |
| 101 |
end |
| 102 |
else |
| 103 |
t[#t + 1] = u |
| 104 |
end |
| 105 |
end |
| 106 |
return string.char(table.unpack(t)) |
| 107 |
end |
| 108 |
|
| 109 |
local function uhc_find_file (file, ...) |
| 110 |
local f = kpse_find_file(file, ...) |
| 111 |
if f then return f end |
| 112 |
f = utf8_to_uhc(file) |
| 113 |
return f and kpse_find_file(f, ...) |
| 114 |
end |
| 115 |
|
| 116 |
local function start_uhc_filename () |
| 117 |
add_to_callback('find_read_file', function(id, name) return uhc_find_file(name) end, 'luatexko.touhc_findreadfile') |
| 118 |
add_to_callback('find_image_file', uhc_find_file, 'luatexko.touhc_findimagefile') |
| 119 |
kpse.find_file = uhc_find_file |
| 120 |
end |
| 121 |
luatexkouhc2utf8.start_uhc_filename = start_uhc_filename |
| 122 |
|
| 123 |
local function stop_uhc_filename () |
| 124 |
remove_from_callback('find_read_file', 'luatexko.touhc_findreadfile') |
| 125 |
remove_from_callback('find_image_file', 'luatexko.touhc_findimagefile') |
| 126 |
kpse.find_file = kpse_find_file |
| 127 |
end |
| 128 |
luatexkouhc2utf8.stop_uhc_filename = stop_uhc_filename |