| 1 |
-- luatexko-normalize.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-normalize", |
| 16 |
version = "3.7", |
| 17 |
date = "2025/02/24", |
| 18 |
author = "Dohyun Kim, Soojin Nam", |
| 19 |
description = "Hangul normalization", |
| 20 |
license = "LPPL v1.3+", |
| 21 |
}) |
| 22 |
|
| 23 |
luatexko = luatexko or {} |
| 24 |
luatexko.normalize = luatexko.normalize or {} |
| 25 |
local luatexkonormalize = luatexko.normalize |
| 26 |
|
| 27 |
local utf8codes = utf8.codes |
| 28 |
local utf8char = utf8.char |
| 29 |
local tableinsert = table.insert |
| 30 |
local tableappend = table.append |
| 31 |
local tableunpack = table.unpack |
| 32 |
|
| 33 |
local normalize = require'lua-uni-normalize' |
| 34 |
|
| 35 |
local jamotocjamo = { |
| 36 |
ccho = { |
| 37 |
[0x1100] = 0x3131, |
| 38 |
[0x1101] = 0x3132, |
| 39 |
-- [0x11AA] = 0x3133, |
| 40 |
[0x1102] = 0x3134, |
| 41 |
-- [0x11AC] = 0x3135, |
| 42 |
-- [0x11AD] = 0x3136, |
| 43 |
[0x1103] = 0x3137, |
| 44 |
[0x1104] = 0x3138, |
| 45 |
[0x1105] = 0x3139, |
| 46 |
-- [0x11B0] = 0x313A, |
| 47 |
-- [0x11B1] = 0x313B, |
| 48 |
-- [0x11B2] = 0x313C, |
| 49 |
-- [0x11B3] = 0x313D, |
| 50 |
-- [0x11B4] = 0x313E, |
| 51 |
-- [0x11B5] = 0x313F, |
| 52 |
[0x111A] = 0x3140, |
| 53 |
[0x1106] = 0x3141, |
| 54 |
[0x1107] = 0x3142, |
| 55 |
[0x1108] = 0x3143, |
| 56 |
[0x1121] = 0x3144, |
| 57 |
[0x1109] = 0x3145, |
| 58 |
[0x110A] = 0x3146, |
| 59 |
[0x110B] = 0x3147, |
| 60 |
[0x110C] = 0x3148, |
| 61 |
[0x110D] = 0x3149, |
| 62 |
[0x110E] = 0x314A, |
| 63 |
[0x110F] = 0x314B, |
| 64 |
[0x1110] = 0x314C, |
| 65 |
[0x1111] = 0x314D, |
| 66 |
[0x1112] = 0x314E, |
| 67 |
[0x1114] = 0x3165, |
| 68 |
[0x1115] = 0x3166, |
| 69 |
-- [0x11C7] = 0x3167, |
| 70 |
-- [0x11C8] = 0x3168, |
| 71 |
-- [0x11CC] = 0x3169, |
| 72 |
-- [0x11CE] = 0x316A, |
| 73 |
-- [0x11D3] = 0x316B, |
| 74 |
-- [0x11D7] = 0x316C, |
| 75 |
-- [0x11D9] = 0x316D, |
| 76 |
[0x111C] = 0x316E, |
| 77 |
-- [0x11DD] = 0x316F, |
| 78 |
-- [0x11DF] = 0x3170, |
| 79 |
[0x111D] = 0x3171, |
| 80 |
[0x111E] = 0x3172, |
| 81 |
[0x1120] = 0x3173, |
| 82 |
[0x1122] = 0x3174, |
| 83 |
[0x1123] = 0x3175, |
| 84 |
[0x1127] = 0x3176, |
| 85 |
[0x1129] = 0x3177, |
| 86 |
[0x112B] = 0x3178, |
| 87 |
[0x112C] = 0x3179, |
| 88 |
[0x112D] = 0x317A, |
| 89 |
[0x112E] = 0x317B, |
| 90 |
[0x112F] = 0x317C, |
| 91 |
[0x1132] = 0x317D, |
| 92 |
[0x1136] = 0x317E, |
| 93 |
[0x1140] = 0x317F, |
| 94 |
[0x1147] = 0x3180, |
| 95 |
[0x114C] = 0x3181, |
| 96 |
-- [0x11F1] = 0x3182, |
| 97 |
-- [0x11F2] = 0x3183, |
| 98 |
[0x1157] = 0x3184, |
| 99 |
[0x1158] = 0x3185, |
| 100 |
[0x1159] = 0x3186, |
| 101 |
}, |
| 102 |
cjung = { |
| 103 |
[0x1161] = 0x314F, |
| 104 |
[0x1162] = 0x3150, |
| 105 |
[0x1163] = 0x3151, |
| 106 |
[0x1164] = 0x3152, |
| 107 |
[0x1165] = 0x3153, |
| 108 |
[0x1166] = 0x3154, |
| 109 |
[0x1167] = 0x3155, |
| 110 |
[0x1168] = 0x3156, |
| 111 |
[0x1169] = 0x3157, |
| 112 |
[0x116A] = 0x3158, |
| 113 |
[0x116B] = 0x3159, |
| 114 |
[0x116C] = 0x315A, |
| 115 |
[0x116D] = 0x315B, |
| 116 |
[0x116E] = 0x315C, |
| 117 |
[0x116F] = 0x315D, |
| 118 |
[0x1170] = 0x315E, |
| 119 |
[0x1171] = 0x315F, |
| 120 |
[0x1172] = 0x3160, |
| 121 |
[0x1173] = 0x3161, |
| 122 |
[0x1174] = 0x3162, |
| 123 |
[0x1175] = 0x3163, |
| 124 |
-- [0x1160] = 0x3164, |
| 125 |
[0x1184] = 0x3187, |
| 126 |
[0x1185] = 0x3188, |
| 127 |
[0x1188] = 0x3189, |
| 128 |
[0x1191] = 0x318A, |
| 129 |
[0x1192] = 0x318B, |
| 130 |
[0x1194] = 0x318C, |
| 131 |
[0x119E] = 0x318D, |
| 132 |
[0x11A1] = 0x318E, |
| 133 |
} |
| 134 |
} |
| 135 |
|
| 136 |
local function is_hangul (c) |
| 137 |
return c >= 0xAC00 and c <= 0xD7A3 |
| 138 |
end |
| 139 |
|
| 140 |
local function is_modern_jong (c) |
| 141 |
return c >= 0x11A8 and c <= 0x11C2 |
| 142 |
end |
| 143 |
|
| 144 |
local function is_old_jong (c) |
| 145 |
return c >= 0x11C3 and c <= 0x11FF |
| 146 |
or c >= 0xD7CB and c <= 0xD7FB |
| 147 |
end |
| 148 |
|
| 149 |
local function is_jongsong (c) |
| 150 |
return is_modern_jong(c) or is_old_jong(c) |
| 151 |
end |
| 152 |
|
| 153 |
local function syllable2jamo (s) -- integer -> table |
| 154 |
local t = {} |
| 155 |
s = s - 0xAC00 |
| 156 |
t[1] = s // 588 + 0x1100 |
| 157 |
t[2] = s % 588 // 28 + 0x1161 |
| 158 |
local jong = s % 28 |
| 159 |
if jong ~= 0 then |
| 160 |
t[3] = jong + 0x11A7 |
| 161 |
end |
| 162 |
return t |
| 163 |
end |
| 164 |
|
| 165 |
local hanguldecompose = normalize.NFD |
| 166 |
|
| 167 |
-- LV | LVT, T -> L, V, T+ |
| 168 |
local function flush_syllable_jong (t, s) |
| 169 |
if #s == 2 then |
| 170 |
tableappend(t, syllable2jamo( s[1] )) |
| 171 |
tableinsert(t, s[2]) |
| 172 |
else |
| 173 |
tableappend(t, s) |
| 174 |
end |
| 175 |
return t, {} |
| 176 |
end |
| 177 |
|
| 178 |
local function compose_hanguldecompose (buffer) -- string -> table |
| 179 |
local t, s = {}, {} |
| 180 |
for _, c in utf8codes(buffer) do |
| 181 |
if #s == 1 and is_jongsong(c) then |
| 182 |
tableinsert(s, c) |
| 183 |
else |
| 184 |
t, s = flush_syllable_jong(t, s) |
| 185 |
tableinsert(is_hangul(c) and s or t, c) |
| 186 |
end |
| 187 |
end |
| 188 |
t = flush_syllable_jong(t, s) |
| 189 |
return t |
| 190 |
end |
| 191 |
|
| 192 |
-- L, VF -> CL |
| 193 |
local function flush_cjamocho (t, s) |
| 194 |
if #s == 2 then |
| 195 |
tableinsert(t, jamotocjamo.ccho[ s[1] ]) |
| 196 |
else |
| 197 |
tableappend(t, s) |
| 198 |
end |
| 199 |
return t, {} |
| 200 |
end |
| 201 |
|
| 202 |
local function compose_jamo_chosong (ot) |
| 203 |
local t, s = {}, {} |
| 204 |
for _, c in ipairs(ot) do |
| 205 |
if #s == 1 and c == 0x1160 then |
| 206 |
tableinsert(s, c) |
| 207 |
else |
| 208 |
t, s = flush_cjamocho(t, s) |
| 209 |
tableinsert(jamotocjamo.ccho[c] and s or t, c) |
| 210 |
end |
| 211 |
end |
| 212 |
t = flush_cjamocho(t, s) |
| 213 |
return t |
| 214 |
end |
| 215 |
|
| 216 |
-- LF, V, ^T -> CV, ^T |
| 217 |
local function flush_cjamojung (t, s) |
| 218 |
if #s == 2 then |
| 219 |
tableinsert(t, jamotocjamo.cjung[ s[2] ]) |
| 220 |
else |
| 221 |
tableappend(t, s) |
| 222 |
end |
| 223 |
return t, {} |
| 224 |
end |
| 225 |
|
| 226 |
local function compose_jamo_jungsong (ot) |
| 227 |
local t, s = {}, {} |
| 228 |
for _, c in ipairs(ot) do |
| 229 |
if #s == 1 and jamotocjamo.cjung[c] or #s == 2 and is_jongsong(c) then |
| 230 |
tableinsert(s, c) |
| 231 |
else |
| 232 |
t, s = flush_cjamojung(t, s) |
| 233 |
tableinsert(c == 0x115F and s or t, c) |
| 234 |
end |
| 235 |
end |
| 236 |
t = flush_cjamojung(t, s) |
| 237 |
return t |
| 238 |
end |
| 239 |
|
| 240 |
local function hangulcompose (buffer) |
| 241 |
buffer = normalize.NFC(buffer) |
| 242 |
|
| 243 |
local t = compose_hanguldecompose(buffer) |
| 244 |
t = compose_jamo_chosong (t) |
| 245 |
t = compose_jamo_jungsong(t) |
| 246 |
|
| 247 |
return utf8char(tableunpack(t)) |
| 248 |
end |
| 249 |
|
| 250 |
local loaded = false |
| 251 |
local add_to_callback = luatexbase.add_to_callback |
| 252 |
local remove_from_callback = luatexbase.remove_from_callback |
| 253 |
|
| 254 |
local function unload() |
| 255 |
if loaded then |
| 256 |
remove_from_callback('process_input_buffer', 'luatexko.hangul_normalize') |
| 257 |
loaded = false |
| 258 |
end |
| 259 |
end |
| 260 |
luatexkonormalize.unload = unload |
| 261 |
|
| 262 |
local function compose() |
| 263 |
unload() |
| 264 |
add_to_callback('process_input_buffer', hangulcompose, 'luatexko.hangul_normalize') |
| 265 |
loaded = true |
| 266 |
end |
| 267 |
luatexkonormalize.compose = compose |
| 268 |
|
| 269 |
local function decompose() |
| 270 |
unload() |
| 271 |
add_to_callback('process_input_buffer', hanguldecompose, 'luatexko.hangul_normalize') |
| 272 |
loaded = true |
| 273 |
end |
| 274 |
luatexkonormalize.decompose = decompose |