| 1 |
----------------------------------------------------------------------- |
| 2 |
-- FILE: luaotfload-unicode.lua |
| 3 |
-- DESCRIPTION: part of luaotfload / unicode |
| 4 |
----------------------------------------------------------------------- |
| 5 |
|
| 6 |
assert(luaotfload_module, "This is a part of luaotfload and should not be loaded independently") { |
| 7 |
name = "luaotfload-unicode", |
| 8 |
version = "3.29", --TAGVERSION |
| 9 |
date = "2024-12-03", --TAGDATE |
| 10 |
description = "luaotfload submodule / Unicode helpers", |
| 11 |
license = "CC0 1.0 Universal", |
| 12 |
author = "Marcel Krüger" |
| 13 |
} |
| 14 |
|
| 15 |
local utf8codes = utf8.codes |
| 16 |
local utf8char = utf8.char |
| 17 |
local sub = string.sub |
| 18 |
local unpack = table.unpack |
| 19 |
local concat = table.concat |
| 20 |
local move = table.move |
| 21 |
|
| 22 |
local codepoint = lpeg.S'0123456789ABCDEF'^4/function(c)return tonumber(c, 16)end |
| 23 |
local empty = {} |
| 24 |
local result = {} |
| 25 |
|
| 26 |
local alphnum_only do |
| 27 |
local niceentry = lpeg.Cg(codepoint * ';' * (1-lpeg.P';')^0 * ';' * lpeg.S'LN' * lpeg.Cc(true)) |
| 28 |
local entry = niceentry^0 * (1-lpeg.P'\n')^0 * lpeg.P'\n' |
| 29 |
local file = lpeg.Cf( |
| 30 |
lpeg.Ct'' |
| 31 |
* entry^0 |
| 32 |
, rawset) |
| 33 |
|
| 34 |
local f = io.open(kpse.find_file"UnicodeData.txt") |
| 35 |
local data = file:match(f:read'*a') |
| 36 |
f:close() |
| 37 |
function alphnum_only(s) |
| 38 |
local result = result |
| 39 |
for i = #result, 1, -1 do result[i] = nil end |
| 40 |
local nice = nil |
| 41 |
for p, c in utf8codes(s) do |
| 42 |
if data[c] |
| 43 |
or (c >= 0x3400 and c<= 0x3DB5) |
| 44 |
or (c >= 0x4E00 and c<= 0x9FEF) |
| 45 |
or (c >= 0xAC00 and c<= 0xD7A3) |
| 46 |
then |
| 47 |
if not nice then nice = p end |
| 48 |
else |
| 49 |
if nice then |
| 50 |
result[#result + 1] = sub(s, nice, p-1) |
| 51 |
nice = nil |
| 52 |
end |
| 53 |
end |
| 54 |
end |
| 55 |
if nice then |
| 56 |
result[#result + 1] = sub(s, nice, #s) |
| 57 |
end |
| 58 |
return concat(result) |
| 59 |
end |
| 60 |
end |
| 61 |
|
| 62 |
local uppercase, lowercase, ccc, cased, case_ignorable, titlecase = {}, {}, {}, {}, {}, nil do |
| 63 |
titlecase = nil -- Not implemented yet(?) |
| 64 |
local ignored_field = (1-lpeg.P';')^0 * ';' |
| 65 |
local cased_category = lpeg.P'Ll;' + 'Lu;' + 'Lt;' |
| 66 |
local case_ignore_category = lpeg.P'Mn;' + 'Me;' + 'Cf;' + 'Lm;' + 'Sk;' |
| 67 |
|
| 68 |
local simple_entry = |
| 69 |
codepoint/0 * ';' |
| 70 |
* ignored_field -- Name |
| 71 |
* (ignored_field - cased_category - case_ignore_category) -- General_Category |
| 72 |
* '0;' -- ccc |
| 73 |
* ignored_field -- Bidi |
| 74 |
* ignored_field -- Decomp |
| 75 |
* ignored_field -- Numeric |
| 76 |
* ignored_field -- Numeric |
| 77 |
* ignored_field -- Numeric |
| 78 |
* ignored_field -- Mirrored |
| 79 |
* ignored_field -- Obsolete |
| 80 |
* ignored_field -- Obsolete |
| 81 |
* ';;\n' |
| 82 |
local entry = simple_entry |
| 83 |
+ codepoint * ';' |
| 84 |
* ignored_field -- Name |
| 85 |
* (cased_category * lpeg.Cc(cased) + case_ignore_category * lpeg.Cc(case_ignorable) + ignored_field * lpeg.Cc(nil)) -- General_Category |
| 86 |
* ('0;' * lpeg.Cc(nil) + lpeg.R'09'^1/tonumber * ';') -- ccc |
| 87 |
* ignored_field -- Bidi |
| 88 |
* ignored_field -- Decomp |
| 89 |
* ignored_field -- Numeric |
| 90 |
* ignored_field -- Numeric |
| 91 |
* ignored_field -- Numeric |
| 92 |
* ignored_field -- Mirrored |
| 93 |
* ignored_field -- Obsolete |
| 94 |
* ignored_field -- Obsolete |
| 95 |
* (codepoint + lpeg.Cc(nil)) * ';' -- uppercase |
| 96 |
* (codepoint + lpeg.Cc(nil)) * ';' -- lowercase |
| 97 |
* (codepoint + lpeg.Cc(nil)) * '\n' -- titlecase |
| 98 |
/ function(codepoint, cased_flag, ccc_val, upper, lower, title) |
| 99 |
if cased_flag then cased_flag[codepoint] = true end |
| 100 |
ccc[codepoint] = ccc_val |
| 101 |
uppercase[codepoint] = upper |
| 102 |
lowercase[codepoint] = lower |
| 103 |
-- if title then titlecase[codepoint] = title end -- Not implemented yet(?) |
| 104 |
end |
| 105 |
local file = entry^0 * -1 |
| 106 |
|
| 107 |
local f = io.open(kpse.find_file"UnicodeData.txt") |
| 108 |
assert(file:match(f:read'*a')) |
| 109 |
f:close() |
| 110 |
end |
| 111 |
|
| 112 |
local props do |
| 113 |
local ws = lpeg.P' '^0 |
| 114 |
local nl = ws * ('#' * (1-lpeg.P'\n')^0)^-1 * '\n' |
| 115 |
local entry = codepoint * (".." * codepoint + lpeg.Cc(false)) * ws * ";" * ws * lpeg.C(lpeg.R("AZ", "az", "__")^1) * nl |
| 116 |
local file = lpeg.Cf( |
| 117 |
lpeg.Ct( |
| 118 |
lpeg.Cg(lpeg.Ct"", "Soft_Dotted") |
| 119 |
* lpeg.Cg(lpeg.Cc(cased), "Other_Lowercase") |
| 120 |
* lpeg.Cg(lpeg.Cc(cased), "Other_Uppercase")) |
| 121 |
* (lpeg.Cg(entry) + nl)^0 |
| 122 |
, function(t, cp_start, cp_end, prop) |
| 123 |
local prop_table = t[prop] |
| 124 |
if prop_table then |
| 125 |
for cp = cp_start, cp_end or cp_start do |
| 126 |
prop_table[cp] = true |
| 127 |
end |
| 128 |
end |
| 129 |
return t |
| 130 |
end) * -1 |
| 131 |
|
| 132 |
local f = io.open(kpse.find_file"PropList.txt") |
| 133 |
props = file:match(f:read'*a') |
| 134 |
f:close() |
| 135 |
end |
| 136 |
|
| 137 |
do |
| 138 |
local ws = lpeg.P' '^0 |
| 139 |
local nl = ws * ('#' * (1-lpeg.P'\n')^0)^-1 * '\n' |
| 140 |
local file = (codepoint * (".." * codepoint + lpeg.Cc(false)) * ws * ";" * ws * (lpeg.P'Single_Quote' + 'MidLetter' + 'MidNumLet') * nl / function(cp_start, cp_end) |
| 141 |
for cp = cp_start, cp_end or cp_start do |
| 142 |
case_ignorable[cp] = true |
| 143 |
end |
| 144 |
end + (1-lpeg.P'\n')^0 * '\n')^0 * -1 |
| 145 |
|
| 146 |
local f = io.open(kpse.find_file"WordBreakProperty.txt") |
| 147 |
assert(file:match(f:read'*a')) |
| 148 |
f:close() |
| 149 |
end |
| 150 |
|
| 151 |
do |
| 152 |
local ws = lpeg.P' '^0 |
| 153 |
local nl = ws * ('#' * (1-lpeg.P'\n')^0)^-1 * '\n' |
| 154 |
local empty = {} |
| 155 |
local function set(t, cp, condition, value) |
| 156 |
local old = t[cp] or cp |
| 157 |
if not condition then |
| 158 |
if #value == 1 and tonumber(old) then |
| 159 |
t[cp] = value[1] |
| 160 |
return |
| 161 |
end |
| 162 |
condition = empty |
| 163 |
end |
| 164 |
if tonumber(old or cp) then |
| 165 |
old = {_ = {old}} |
| 166 |
t[cp] = old |
| 167 |
end |
| 168 |
for i=1, #condition do |
| 169 |
local cond = condition[i] |
| 170 |
local step = old[cond] |
| 171 |
if not step then |
| 172 |
step = {} |
| 173 |
old[cond] = step |
| 174 |
end |
| 175 |
old = step |
| 176 |
end |
| 177 |
old._ = value |
| 178 |
end |
| 179 |
local entry = codepoint * ";" |
| 180 |
* lpeg.Ct((ws * codepoint)^1 + ws) * ";" |
| 181 |
* lpeg.Ct((ws * codepoint)^1 + ws) * ";" |
| 182 |
* lpeg.Ct((ws * codepoint)^1 + ws) * ";" |
| 183 |
* (lpeg.Ct((ws * lpeg.C(lpeg.R('AZ', 'az', '__')^1))^1) * ";")^-1 |
| 184 |
* ws * nl / function(cp, lower, title, upper, condition) |
| 185 |
set(lowercase, cp, condition, lower) |
| 186 |
set(uppercase, cp, condition, upper) |
| 187 |
end |
| 188 |
local file = (entry + nl)^0 * -1 |
| 189 |
|
| 190 |
local f = io.open(kpse.find_file"SpecialCasing.txt") |
| 191 |
assert(file:match(f:read'*a')) |
| 192 |
f:close() |
| 193 |
end |
| 194 |
|
| 195 |
do |
| 196 |
local function eq(a, b) |
| 197 |
if not a then return false end |
| 198 |
if not b then return false end |
| 199 |
if a == b then return true end |
| 200 |
if #a ~= #b then return false end |
| 201 |
for i=1,#a do if a[i] ~= b[i] then return false end end |
| 202 |
return true |
| 203 |
end |
| 204 |
local function collapse(t, inherited) |
| 205 |
inherited = t._ or inherited |
| 206 |
local empty = true |
| 207 |
for k,v in next, t do |
| 208 |
if k ~= '_' then |
| 209 |
if eq(inherited, collapse(v, inherited)) then |
| 210 |
t[k] = nil |
| 211 |
else |
| 212 |
empty = false |
| 213 |
end |
| 214 |
end |
| 215 |
end |
| 216 |
return empty and inherited |
| 217 |
end |
| 218 |
local function cleanup(t) |
| 219 |
for k,v in next, t do |
| 220 |
if not tonumber(v) then |
| 221 |
local collapsed = collapse(v) |
| 222 |
if collapsed and #collapsed == 1 then |
| 223 |
v = collapsed[1] |
| 224 |
if k == v then |
| 225 |
v = nil |
| 226 |
end |
| 227 |
t[k] = v |
| 228 |
end |
| 229 |
end |
| 230 |
end |
| 231 |
end |
| 232 |
cleanup(uppercase) |
| 233 |
cleanup(lowercase) |
| 234 |
end |
| 235 |
|
| 236 |
-- Here we manipulate the uppercase table a bit to add the `de-alt` language using capital eszett. |
| 237 |
uppercase[0x00DF]['de-x-eszett'] = { _ = { 0x1E9E } } |
| 238 |
uppercase[0x00DF]['de-alt'] = uppercase[0x00DF]['de-x-eszett'] |
| 239 |
|
| 240 |
-- Special handling for Eastern Armenian based on Unicode document L2/20-143. |
| 241 |
uppercase[0x0587]['hy'] = { _ = { 0x0535, 0x054E } } |
| 242 |
-- Restore Unicode behavior. This entry is redundant, but we have to be aware of it |
| 243 |
-- if we later start to ignore unknown private use tags |
| 244 |
uppercase[0x0587]['hy-x-yiwn'] = { _ = uppercase[0x0587]._ } |
| 245 |
|
| 246 |
return { |
| 247 |
alphnum_only = alphnum_only, |
| 248 |
casemapping = { |
| 249 |
uppercase = uppercase, |
| 250 |
lowercase = lowercase, |
| 251 |
cased = cased, |
| 252 |
case_ignorable = case_ignorable, |
| 253 |
-- titlecase = titlecase, |
| 254 |
}, |
| 255 |
ccc = ccc, |
| 256 |
soft_dotted = props.Soft_Dotted, |
| 257 |
} |