| 1 |
----------------------------------------------------------------------- |
| 2 |
-- FILE: luaotfload-multiscript.lua |
| 3 |
-- DESCRIPTION: part of luaotfload / multiscript |
| 4 |
----------------------------------------------------------------------- |
| 5 |
|
| 6 |
assert(luaotfload_module, "This is a part of luaotfload and should not be loaded independently") { |
| 7 |
name = "luaotfload-multiscript", |
| 8 |
version = "3.29", --TAGVERSION |
| 9 |
date = "2024-12-03", --TAGDATE |
| 10 |
description = "luaotfload submodule / multiscript", |
| 11 |
license = "GPL v2.0", |
| 12 |
author = "Marcel Krüger" |
| 13 |
} |
| 14 |
|
| 15 |
local nodenew = node.direct.new |
| 16 |
local getfont = font.getfont |
| 17 |
local setfont = node.direct.setfont |
| 18 |
local getwhd = node.direct.getwhd |
| 19 |
local insert_after = node.direct.insert_after |
| 20 |
local traverse_char = node.direct.traverse_char |
| 21 |
local protect_glyph = node.direct.protect_glyph |
| 22 |
local flush_node = node.direct.flush_node |
| 23 |
local remove = node.direct.remove |
| 24 |
local otffeatures = fonts.constructors.newfeatures "otf" |
| 25 |
-- local normalize = fonts.handlers.otf.features.normalize |
| 26 |
local definers = fonts.definers |
| 27 |
local define_font -- = luaotfload.define_font % This is set when the first font is loaded. |
| 28 |
local scripts_lib = require'luaotfload-scripts'.script |
| 29 |
local script_to_iso = scripts_lib.to_iso |
| 30 |
local script_to_ot = scripts_lib.to_ot |
| 31 |
|
| 32 |
local delayedremove, finishremove do |
| 33 |
local removed |
| 34 |
function finishremove() |
| 35 |
if removed then |
| 36 |
flush_node(removed) |
| 37 |
end |
| 38 |
removed = nil |
| 39 |
end |
| 40 |
function delayedremove(head, current) |
| 41 |
finishremove() |
| 42 |
removed = current |
| 43 |
return remove(head, current) |
| 44 |
end |
| 45 |
end |
| 46 |
local harf = luaotfload.harfbuzz |
| 47 |
local GSUBtag, GPOStag |
| 48 |
if harf then |
| 49 |
GSUBtag = harf.Tag.new("GSUB") |
| 50 |
GPOStag = harf.Tag.new("GPOS") |
| 51 |
end |
| 52 |
|
| 53 |
local sep = lpeg.P' '^0 * ';' * lpeg.P' '^0 |
| 54 |
local codepoint = lpeg.S'0123456789ABCDEF'^4/function(c)return tonumber(c, 16)end |
| 55 |
local codepoint_range = codepoint * ('..' * codepoint + lpeg.Cc(false)) |
| 56 |
local function multirawset(table, key1, key2, value) |
| 57 |
for key = key1,(key2 or key1) do |
| 58 |
rawset(table, key, value) |
| 59 |
end |
| 60 |
return table |
| 61 |
end |
| 62 |
local script_extensions do |
| 63 |
local entry = lpeg.Cg(codepoint_range * sep * lpeg.Ct((lpeg.C(lpeg.R'AZ' * lpeg.R'az'^1)/string.lower)^1 * ' ') * '#')^-1 * (1-lpeg.P'\n')^0 * '\n' |
| 64 |
local file = lpeg.Cf( |
| 65 |
lpeg.Ct'' |
| 66 |
* entry^0 |
| 67 |
, multirawset) |
| 68 |
|
| 69 |
local f = io.open(kpse.find_file"ScriptExtensions.txt") |
| 70 |
script_extensions = file:match(f:read'*a') |
| 71 |
f:close() |
| 72 |
for cp,t in next, script_extensions do |
| 73 |
for i=1,#t do |
| 74 |
t[t[i]] = true |
| 75 |
end |
| 76 |
end |
| 77 |
end |
| 78 |
local script_list = {} |
| 79 |
local script_mapping do |
| 80 |
-- We could extract these from PropertyValueAliases.txt... |
| 81 |
local script_aliases = { |
| 82 |
Adlam = "adlm", Caucasian_Albanian = "aghb", Ahom = "ahom", Arabic = "arab", |
| 83 |
Imperial_Aramaic = "armi", Armenian = "armn", Avestan = "avst", |
| 84 |
Balinese = "bali", Bamum = "bamu", Bassa_Vah = "bass", Batak = "batk", |
| 85 |
Bengali = "beng", Bhaiksuki = "bhks", Bopomofo = "bopo", Brahmi = "brah", |
| 86 |
Braille = "brai", Buginese = "bugi", Buhid = "buhd", Chakma = "cakm", |
| 87 |
Canadian_Aboriginal = "cans", Carian = "cari", Cham = "cham", |
| 88 |
Cherokee = "cher", Coptic = "copt", Cypriot = "cprt", Cyrillic = "cyrl", |
| 89 |
Devanagari = "deva", Dogra = "dogr", Deseret = "dsrt", Duployan = "dupl", |
| 90 |
Egyptian_Hieroglyphs = "egyp", Elbasan = "elba", Elymaic = "elym", |
| 91 |
Ethiopic = "ethi", Georgian = "geor", Glagolitic = "glag", |
| 92 |
Gunjala_Gondi = "gong", Masaram_Gondi = "gonm", Gothic = "goth", |
| 93 |
Grantha = "gran", Greek = "grek", Gujarati = "gujr", Gurmukhi = "guru", |
| 94 |
Hangul = "hang", Han = "hani", Hanunoo = "hano", Hatran = "hatr", |
| 95 |
Hebrew = "hebr", Hiragana = "hira", Anatolian_Hieroglyphs = "hluw", |
| 96 |
Pahawh_Hmong = "hmng", Nyiakeng_Puachue_Hmong = "hmnp", |
| 97 |
Katakana_Or_Hiragana = "hrkt", Old_Hungarian = "hung", Old_Italic = "ital", |
| 98 |
Javanese = "java", Kayah_Li = "kali", Katakana = "kana", |
| 99 |
Kharoshthi = "khar", Khmer = "khmr", Khojki = "khoj", Kannada = "knda", |
| 100 |
Kaithi = "kthi", Tai_Tham = "lana", Lao = "laoo", Latin = "latn", |
| 101 |
Lepcha = "lepc", Limbu = "limb", Linear_A = "lina", Linear_B = "linb", |
| 102 |
Lisu = "lisu", Lycian = "lyci", Lydian = "lydi", Mahajani = "mahj", |
| 103 |
Makasar = "maka", Mandaic = "mand", Manichaean = "mani", Marchen = "marc", |
| 104 |
Medefaidrin = "medf", Mende_Kikakui = "mend", Meroitic_Cursive = "merc", |
| 105 |
Meroitic_Hieroglyphs = "mero", Malayalam = "mlym", Modi = "modi", |
| 106 |
Mongolian = "mong", Mro = "mroo", Meetei_Mayek = "mtei", Multani = "mult", |
| 107 |
Myanmar = "mymr", Nandinagari = "nand", Old_North_Arabian = "narb", |
| 108 |
Nabataean = "nbat", Newa = "newa", Nko = "nkoo", Nushu = "nshu", |
| 109 |
Ogham = "ogam", Ol_Chiki = "olck", Old_Turkic = "orkh", Oriya = "orya", |
| 110 |
Osage = "osge", Osmanya = "osma", Palmyrene = "palm", Pau_Cin_Hau = "pauc", |
| 111 |
Old_Permic = "perm", Phags_Pa = "phag", Inscriptional_Pahlavi = "phli", |
| 112 |
Psalter_Pahlavi = "phlp", Phoenician = "phnx", Miao = "plrd", |
| 113 |
Inscriptional_Parthian = "prti", Rejang = "rjng", Hanifi_Rohingya = "rohg", |
| 114 |
Runic = "runr", Samaritan = "samr", Old_South_Arabian = "sarb", |
| 115 |
Saurashtra = "saur", SignWriting = "sgnw", Shavian = "shaw", |
| 116 |
Sharada = "shrd", Siddham = "sidd", Khudawadi = "sind", Sinhala = "sinh", |
| 117 |
Sogdian = "sogd", Old_Sogdian = "sogo", Sora_Sompeng = "sora", |
| 118 |
Soyombo = "soyo", Sundanese = "sund", Syloti_Nagri = "sylo", |
| 119 |
Syriac = "syrc", Tagbanwa = "tagb", Takri = "takr", Tai_Le = "tale", |
| 120 |
New_Tai_Lue = "talu", Tamil = "taml", Tangut = "tang", Tai_Viet = "tavt", |
| 121 |
Telugu = "telu", Tifinagh = "tfng", Tagalog = "tglg", Thaana = "thaa", |
| 122 |
Thai = "thai", Tibetan = "tibt", Tirhuta = "tirh", Ugaritic = "ugar", |
| 123 |
Vai = "vaii", Warang_Citi = "wara", Wancho = "wcho", Old_Persian = "xpeo", |
| 124 |
Cuneiform = "xsux", Yi = "yiii", Zanabazar_Square = "zanb", |
| 125 |
Inherited = "zinh", Common = "zyyy", Unknown = "zzzz", |
| 126 |
} |
| 127 |
local entry = lpeg.Cg(codepoint_range * sep * ((lpeg.R'AZ' + lpeg.R'az' + '_')^1/script_aliases))^-1 * (1-lpeg.P'\n')^0 * '\n' |
| 128 |
-- local entry = lpeg.Cg(codepoint_range * sep * lpeg.Cc(true))^-1 * (1-lpeg.P'\n')^0 * '\n' |
| 129 |
local file = lpeg.Cf( |
| 130 |
lpeg.Ct'' |
| 131 |
* entry^0 |
| 132 |
, multirawset) |
| 133 |
|
| 134 |
local f = io.open(kpse.find_file"Scripts.txt") |
| 135 |
script_mapping = file:match(f:read'*a') |
| 136 |
f:close() |
| 137 |
|
| 138 |
for _, scr in next, script_aliases do |
| 139 |
script_list[#script_list + 1] = scr |
| 140 |
end |
| 141 |
end |
| 142 |
|
| 143 |
table.sort(script_list) |
| 144 |
local script_mark_offset = 0x200000 |
| 145 |
local script_marks = {} |
| 146 |
do |
| 147 |
for i=1,#script_list do |
| 148 |
local codepoint = script_mark_offset - 1 + i |
| 149 |
local scr = script_list[i] |
| 150 |
-- script_mapping[codepoint] = scr |
| 151 |
script_marks[scr] = codepoint |
| 152 |
script_marks[codepoint] = scr |
| 153 |
end |
| 154 |
end |
| 155 |
|
| 156 |
local function load_on_demand(specifications, size) |
| 157 |
return setmetatable({}, { __index = function(t, k) |
| 158 |
local specification = specifications[k] |
| 159 |
if not specification then return end |
| 160 |
local f = define_font(specification, size) |
| 161 |
local fid |
| 162 |
if type(f) == 'table' then |
| 163 |
fid = font.define(f) |
| 164 |
definers.register(f, fid) |
| 165 |
elseif f then |
| 166 |
fid = f |
| 167 |
end |
| 168 |
t[k] = fid |
| 169 |
return fid |
| 170 |
end}) |
| 171 |
end |
| 172 |
|
| 173 |
local function collect_scripts(tfmdata) |
| 174 |
local script_dict = {} |
| 175 |
local hbdata = tfmdata.hb |
| 176 |
if hbdata then |
| 177 |
local face = hbdata.shared.face |
| 178 |
for _, tag in next, { GSUBtag, GPOStag } do |
| 179 |
local script_tags = face:ot_layout_get_script_tags(tag) or {} |
| 180 |
for i = 1, #script_tags do |
| 181 |
script_dict[tostring(script_tags[i]):gsub(" +$", "")] = true |
| 182 |
end |
| 183 |
end |
| 184 |
else |
| 185 |
local features = tfmdata.resources.features |
| 186 |
for _, feature_table in next, features do |
| 187 |
for _, scripts in next, feature_table do |
| 188 |
for script in next, scripts do |
| 189 |
script_dict[script] = true |
| 190 |
end |
| 191 |
end |
| 192 |
end |
| 193 |
script_dict["*"] = nil |
| 194 |
end |
| 195 |
return script_dict |
| 196 |
end |
| 197 |
|
| 198 |
local additional_scripts_tables = { } |
| 199 |
|
| 200 |
local additional_scripts_fonts = setmetatable({}, { |
| 201 |
__index = function(t, fid) |
| 202 |
local f = font.getfont(fid) |
| 203 |
-- table.tofile('myfont2', f) |
| 204 |
local res = f and f.additional_scripts or false |
| 205 |
t[fid] = res |
| 206 |
return res |
| 207 |
end, |
| 208 |
}) |
| 209 |
|
| 210 |
local function is_dominant_script(scripts, script, first, ...) |
| 211 |
if script == first then return true end |
| 212 |
if scripts[first] or not first then return false end |
| 213 |
return is_dominant_script(scripts, script, ...) |
| 214 |
end |
| 215 |
|
| 216 |
local function makecombifont(tfmdata, _, additional_scripts) |
| 217 |
define_font = define_font or luaotfload.define_font -- Lazy loading because this file gets loaded before define_font is defined |
| 218 |
local has_auto |
| 219 |
additional_scripts = tostring(additional_scripts) |
| 220 |
if additional_scripts:sub(1, 5) == "auto+" then |
| 221 |
additional_scripts = additional_scripts:sub(6) |
| 222 |
has_auto = true |
| 223 |
elseif additional_scripts == "auto" then |
| 224 |
has_auto, additional_scripts = true, false |
| 225 |
end |
| 226 |
if additional_scripts then |
| 227 |
local t = additional_scripts_tables[tonumber(additional_scripts) or additional_scripts] |
| 228 |
if not t then error(string.format("Unknown multiscript table %s", additional_scripts)) end |
| 229 |
local lower_t = {} |
| 230 |
for k, v in next, t do if type(k) == "string" then |
| 231 |
local l = string.lower(k) |
| 232 |
if lower_t[l] ~= nil and lower_t[l] ~= v then |
| 233 |
error(string.format("Inconsistent multiscript table %q for script %s", additional_scripts, l)) |
| 234 |
end |
| 235 |
lower_t[l] = v |
| 236 |
end end |
| 237 |
additional_scripts = lower_t |
| 238 |
else |
| 239 |
additional_scripts = {} |
| 240 |
end |
| 241 |
if has_auto then |
| 242 |
local fallback = tfmdata.fallback_lookup |
| 243 |
if fallback then -- FIXME: here be dragons |
| 244 |
local fallbacks = {} |
| 245 |
local current = tfmdata |
| 246 |
local i = 0 |
| 247 |
while current do |
| 248 |
local collected = collect_scripts(current) |
| 249 |
for script in next, collected do |
| 250 |
local scr_fb = fallbacks[script] |
| 251 |
if not scr_fb then |
| 252 |
scr_fb = {} |
| 253 |
fallbacks[script] = scr_fb |
| 254 |
end |
| 255 |
scr_fb[#scr_fb + 1] = current.specification.specification .. ';script=' .. script .. ';-multiscript' |
| 256 |
end |
| 257 |
i = i - 1 |
| 258 |
current = fallback[i] |
| 259 |
end |
| 260 |
current = tfmdata |
| 261 |
i = 0 |
| 262 |
while current do |
| 263 |
local collected = collect_scripts(current) |
| 264 |
for script, scr_fb in next, fallbacks do |
| 265 |
if not collected[script] then |
| 266 |
scr_fb[#scr_fb + 1] = current.specification.specification .. ';-multiscript' |
| 267 |
end |
| 268 |
end |
| 269 |
i = i - 1 |
| 270 |
current = fallback[i] |
| 271 |
end |
| 272 |
for script, scr_fb in next, fallbacks do |
| 273 |
local iso_script = script_to_iso(script) |
| 274 |
if not additional_scripts[iso_script] and is_dominant_script(scr_fb, script, script_to_ot(iso_script)) then |
| 275 |
local main = scr_fb[1] |
| 276 |
table.remove(scr_fb, 1) |
| 277 |
local fbid = luaotfload.add_fallback(scr_fb) |
| 278 |
additional_scripts[iso_script] = main .. ';fallback=' .. fbid |
| 279 |
end |
| 280 |
end |
| 281 |
else |
| 282 |
local spec = tfmdata.specification |
| 283 |
local collected = collect_scripts(tfmdata) |
| 284 |
for script in next, collected do |
| 285 |
local iso_script = script_to_iso(script) |
| 286 |
if not additional_scripts[iso_script] and is_dominant_script(collected, script, script_to_ot(iso_script)) then |
| 287 |
additional_scripts[iso_script] = spec.specification .. ';-multiscript;script=' .. script |
| 288 |
---- FIXME: IMHO the following which just modiefies the spec |
| 289 |
-- would be nicer, but it breaks font patching callbacks |
| 290 |
-- (except if we ignore them, but that would be inconsistent to |
| 291 |
-- other fonts) |
| 292 |
-- local new_raw_features = {} |
| 293 |
-- local new_features = { raw = new_raw_features, normal = new_raw_features } |
| 294 |
-- for f, v in next, spec.features.raw do |
| 295 |
-- new_raw_features[f] = v |
| 296 |
-- end |
| 297 |
-- new_raw_features.multiscript = false |
| 298 |
-- new_raw_features.script = script |
| 299 |
-- local new_normal_features = luaotfload.apply_default_features(new_raw_features) |
| 300 |
-- new_normal_features.sub = nil |
| 301 |
-- new_normal_features.lookup = nil |
| 302 |
-- new_features.normal = normalize(new_normal_features) |
| 303 |
-- local new_spec = {} |
| 304 |
-- for k, v in next, spec do |
| 305 |
-- new_spec[k] = v |
| 306 |
-- end |
| 307 |
-- new_spec.hash = nil |
| 308 |
-- new_spec.features = new_features |
| 309 |
-- additional_scripts[script] = new_spec |
| 310 |
end |
| 311 |
end |
| 312 |
end |
| 313 |
end |
| 314 |
local basescript = tfmdata.properties.script or "dflt" |
| 315 |
tfmdata.additional_scripts = load_on_demand(additional_scripts, tfmdata.size) |
| 316 |
tfmdata.additional_scripts[basescript] = false |
| 317 |
end |
| 318 |
|
| 319 |
local glyph_id = node.id'glyph' |
| 320 |
-- TODO: unset last_script, matching parentheses etc |
| 321 |
local function domultiscript(head, _, _, _, direction) |
| 322 |
head = node.direct.todirect(head) |
| 323 |
local last_fid, last_fonts, last_script |
| 324 |
for cur, cid, fid in traverse_char(head) do |
| 325 |
if fid ~= last_fid then |
| 326 |
last_fid, last_fonts = fid, additional_scripts_fonts[fid] |
| 327 |
end |
| 328 |
if last_fonts then |
| 329 |
local mapped_scr = script_mapping[cid] |
| 330 |
if mapped_scr == "zinh" then |
| 331 |
mapped_scr = last_script |
| 332 |
else |
| 333 |
local additional_scripts = script_extensions[cid] |
| 334 |
if additional_scripts then |
| 335 |
if additional_scripts[last_script] then |
| 336 |
mapped_scr = last_script |
| 337 |
elseif last_fonts[mapped_scr] == nil then |
| 338 |
for i = 1, #additional_scripts do |
| 339 |
local script = additional_scripts[i] |
| 340 |
if last_fonts[script] ~= nil then |
| 341 |
mapped_scr = script |
| 342 |
break |
| 343 |
end |
| 344 |
end |
| 345 |
end |
| 346 |
elseif mapped_scr == "zyyy" then |
| 347 |
mapped_scr = last_script |
| 348 |
end |
| 349 |
end |
| 350 |
last_script = mapped_scr |
| 351 |
local mapped_font = last_fonts[mapped_scr] |
| 352 |
if mapped_font then |
| 353 |
setfont(cur, mapped_font) |
| 354 |
end |
| 355 |
elseif fid == 0 then |
| 356 |
local script_mark = script_marks[cid] |
| 357 |
if script_mark then |
| 358 |
head = delayedremove(head, current) -- Instead of removing them we could also keep them and suppress the missing glyph warning, but this seems cleaner |
| 359 |
last_script = script_mark |
| 360 |
end |
| 361 |
else |
| 362 |
last_script = nil |
| 363 |
end |
| 364 |
end |
| 365 |
finishremove() -- Cleanup |
| 366 |
return head |
| 367 |
end |
| 368 |
|
| 369 |
function luaotfload.add_multiscript(name, fonts) |
| 370 |
if fonts == nil then |
| 371 |
fonts = name |
| 372 |
name = #additional_scripts_fonts + 1 |
| 373 |
else |
| 374 |
name = name:lower() |
| 375 |
end |
| 376 |
additional_scripts_tables[name] = fonts |
| 377 |
return name |
| 378 |
end |
| 379 |
|
| 380 |
function luaotfload.get_script_mark(scr) |
| 381 |
return script_marks[scr] |
| 382 |
end |
| 383 |
|
| 384 |
otffeatures.register { |
| 385 |
name = "multiscript", |
| 386 |
description = "Combine fonts for multiple scripts", |
| 387 |
manipulators = { |
| 388 |
node = makecombifont, |
| 389 |
plug = makecombifont, |
| 390 |
}, |
| 391 |
-- processors = { -- processors would be nice, but they are applied |
| 392 |
-- -- too late for our purposes |
| 393 |
-- node = donotdef, |
| 394 |
-- } |
| 395 |
} |
| 396 |
|
| 397 |
return { |
| 398 |
-- I would omit the table if that wouldn't cause the function to be used as initializer |
| 399 |
process = domultiscript, |
| 400 |
} |
| 401 |
--- vim:sw=2:ts=2:expandtab:tw=71 |