| 1 |
----------------------------------------------------------------------- |
| 2 |
-- FILE: luaotfload-notdef.lua |
| 3 |
-- DESCRIPTION: part of luaotfload / notdef |
| 4 |
----------------------------------------------------------------------- |
| 5 |
|
| 6 |
assert(luaotfload_module, "This is a part of luaotfload and should not be loaded independently") { |
| 7 |
name = "luaotfload-notdef", |
| 8 |
version = "3.29", --TAGVERSION |
| 9 |
date = "2024-12-03", --TAGDATE |
| 10 |
description = "luaotfload submodule / notdef", |
| 11 |
license = "GPL v2.0", |
| 12 |
author = "Marcel Krüger" |
| 13 |
} |
| 14 |
|
| 15 |
local harfbuzz = luaotfload.harfbuzz |
| 16 |
local flush_node = node.direct.flush_node |
| 17 |
local getfont = font.getfont |
| 18 |
local getnext = node.direct.getnext |
| 19 |
local getwhd = node.direct.getwhd |
| 20 |
local insert = table.insert |
| 21 |
local insert_after = node.direct.insert_after |
| 22 |
local kern_id = node.id'kern' |
| 23 |
local disc_id = node.id'disc' |
| 24 |
local nodenew = node.direct.new |
| 25 |
local nodecopy = node.direct.copy |
| 26 |
local otfregister = fonts.constructors.features.otf.register |
| 27 |
local protect_glyph = node.direct.protect_glyph |
| 28 |
local remove = node.direct.remove |
| 29 |
local setfont = node.direct.setfont |
| 30 |
local traverse_char = node.direct.traverse_char |
| 31 |
local traverse_id = node.direct.traverse_id |
| 32 |
local setchar = node.direct.setchar |
| 33 |
local setdisc = node.direct.setdisc |
| 34 |
local getdisc = node.direct.getdisc |
| 35 |
local getwidth = node.direct.getwidth |
| 36 |
local setkern = node.direct.setkern |
| 37 |
local setattributelist = node.direct.setattributelist |
| 38 |
local getattributelist = node.direct.getattributelist |
| 39 |
local setmove = luaotfload.fontloader.nodes.injections.setmove |
| 40 |
|
| 41 |
-- According to DerivedCoreProperties.txt, Default_Ignorable_Code_Point |
| 42 |
-- is generated from: |
| 43 |
-- Other_Default_Ignorable_Code_Point |
| 44 |
-- + Cf (Format characters) |
| 45 |
-- + Variation_Selector |
| 46 |
-- - White_Space |
| 47 |
-- - FFF9..FFFB (Interlinear annotation format characters) |
| 48 |
-- - 13430..13438 (Egyptian hieroglyph format characters) |
| 49 |
-- - Prepended_Concatenation_Mark (Exceptional format characters that should be visible) |
| 50 |
-- Based on HarfBuzz, we add the exclusion |
| 51 |
-- - Lo (Letter, Other) |
| 52 |
-- This affects Hangul fillers. |
| 53 |
local ignorable_codepoints do |
| 54 |
local sep = lpeg.P' '^0 * ';' * lpeg.P' '^0 |
| 55 |
local codepoint = lpeg.S'0123456789ABCDEF'^4/function(c)return tonumber(c, 16)end |
| 56 |
local codepoint_range = codepoint * ('..' * codepoint + lpeg.Cc(false)) |
| 57 |
local function multirawset(table, key1, key2, value) |
| 58 |
for key = key1,(key2 or key1) do |
| 59 |
rawset(table, key, value) |
| 60 |
end |
| 61 |
return table |
| 62 |
end |
| 63 |
local entry = lpeg.Cg(codepoint * ';' * (1-lpeg.P';')^0 * ';Cf;' * lpeg.Cc(true))^-1 * (1-lpeg.P'\n')^0 * '\n' |
| 64 |
local file = lpeg.Cf( |
| 65 |
lpeg.Ct'' |
| 66 |
* entry^0 |
| 67 |
, rawset) |
| 68 |
local f = io.open(kpse.find_file"UnicodeData.txt") |
| 69 |
ignorable_codepoints = file:match(f:read'*a') |
| 70 |
f:close() |
| 71 |
entry = lpeg.Cg(codepoint_range * sep * ('Other_Default_Ignorable_Code_Point' * lpeg.Cc(true) |
| 72 |
+ 'Variation_Selector' * lpeg.Cc(true) |
| 73 |
+ 'White_Space' * lpeg.Cc(nil) |
| 74 |
+ 'Prepended_Concatenation_Mark' * lpeg.Cc(nil) |
| 75 |
) * ' # ' * (1-lpeg.P'Lo'))^-1 * (1-lpeg.P'\n')^0 * '\n' |
| 76 |
file = lpeg.Cf( |
| 77 |
lpeg.Carg(1) |
| 78 |
* entry^0 |
| 79 |
, multirawset) |
| 80 |
f = io.open(kpse.find_file"PropList.txt") |
| 81 |
ignorable_codepoints = file:match(f:read'*a', 1, ignorable_codepoints) |
| 82 |
f:close() |
| 83 |
for i = 0xFFF9,0xFFFB do |
| 84 |
ignorable_codepoints[i] = nil |
| 85 |
end |
| 86 |
for i = 0x13430,0x13438 do |
| 87 |
ignorable_codepoints[i] = nil |
| 88 |
end |
| 89 |
end |
| 90 |
|
| 91 |
ignorable_codepoints[0xAD] = nil |
| 92 |
|
| 93 |
local function setnotdef(tfmdata, factor) |
| 94 |
local desc = tfmdata.shared.rawdata.descriptions |
| 95 |
-- So we have to find the .notdef glyph. We only know that it has GID |
| 96 |
-- 0, but we need it's Unicode mapping. Normally it isn't mapped in |
| 97 |
-- the font, so we auto-assigned the first private slot: |
| 98 |
local guess = desc[0xF0000] |
| 99 |
if guess and guess.index == 0 then |
| 100 |
tfmdata.notdefcode = 0xF0000 |
| 101 |
return |
| 102 |
end |
| 103 |
-- If this didn't happen, it might be mapped to one of the |
| 104 |
-- replacement characters: |
| 105 |
for code = 0xFFFC,0xFFFF do |
| 106 |
guess = desc[code] |
| 107 |
if guess and guess.index == 0 then |
| 108 |
tfmdata.notdefcode = code |
| 109 |
return |
| 110 |
end |
| 111 |
end |
| 112 |
-- Oh no, we couldn't find it. Maybe we can find it by name? |
| 113 |
local code = tfmdata.resources.unicodes[".notdef"] |
| 114 |
-- Better safe than sorry |
| 115 |
guess = code and desc[code] |
| 116 |
if guess and guess.index == 0 then |
| 117 |
tfmdata.notdefcode = code |
| 118 |
return |
| 119 |
end |
| 120 |
-- So the font didn't do the obvious things and then it lied to us. |
| 121 |
-- At this point we should think about sending an automated complain |
| 122 |
-- to the font author, but we probably can't trust the contact |
| 123 |
-- information either. |
| 124 |
-- We will fall back to brute force now: |
| 125 |
for code, char in pairs(desc) do |
| 126 |
if char.index == 0 then |
| 127 |
tfmdata.notdefcode = code |
| 128 |
return |
| 129 |
end |
| 130 |
end |
| 131 |
-- If we ever reach this point, something odd happened. Either there |
| 132 |
-- are no glyphs at all (then LuaTeX will complain anyway, so let's |
| 133 |
-- ignore that case) or someone tried to use this with a legacy font. |
| 134 |
-- In that case there most likely isn't a `.notdef` glyph anyway and |
| 135 |
-- inserting glyph 0 would insert a random character, so `notdefcode` |
| 136 |
-- better stays `nil`. |
| 137 |
end |
| 138 |
|
| 139 |
local glyph_id = node.id'glyph' |
| 140 |
local function donotdef(head, font, _, _, _) |
| 141 |
local tfmdata = getfont(font) |
| 142 |
local notdef, chars = tfmdata.unscaled.notdefcode, tfmdata.characters |
| 143 |
if not notdef then return end |
| 144 |
for cur, cid, fid in traverse_char(head) do if fid == font then |
| 145 |
local w, h, d = getwhd(cur) |
| 146 |
if w == 0 and h == 0 and d == 0 and not chars[cid] and not ignorable_codepoints[cid] then |
| 147 |
local notdefnode = nodenew(glyph_id, 256) |
| 148 |
setfont(notdefnode, font, notdef) |
| 149 |
insert_after(cur, cur, notdefnode) |
| 150 |
protect_glyph(cur) |
| 151 |
end |
| 152 |
end end |
| 153 |
end |
| 154 |
|
| 155 |
otfregister { |
| 156 |
name = "notdef", |
| 157 |
description = "Add notdef glyphs", |
| 158 |
default = 1, |
| 159 |
initializers = { |
| 160 |
node = setnotdef, |
| 161 |
}, |
| 162 |
processors = { |
| 163 |
node = donotdef, |
| 164 |
} |
| 165 |
} |
| 166 |
|
| 167 |
local font_invisible_replacement = setmetatable({}, {__index = function(t, fid) |
| 168 |
local fontdata = font.getfont(fid) |
| 169 |
local replacement = fontdata.shared.features.invisible |
| 170 |
if replacement == "remove" then |
| 171 |
t[fid] = false |
| 172 |
return false |
| 173 |
end |
| 174 |
replacement = tonumber(replacement) or 32 |
| 175 |
local char = fontdata.characters[replacement] |
| 176 |
if char then |
| 177 |
t[fid] = {replacement, -char.width} |
| 178 |
return t[fid] |
| 179 |
else |
| 180 |
t[fid] = false |
| 181 |
return false |
| 182 |
end |
| 183 |
end}) |
| 184 |
|
| 185 |
local push, pop do |
| 186 |
local function checkprop(n) |
| 187 |
local p = node.direct.getproperty(n) |
| 188 |
return p and p.zwnj |
| 189 |
end |
| 190 |
local list = {} |
| 191 |
function push(head) |
| 192 |
head = node.direct.todirect(head) |
| 193 |
local l = {} |
| 194 |
list[#list+1] = l |
| 195 |
for n, id in node.direct.traverse(head) do |
| 196 |
if checkprop(n) then |
| 197 |
head = node.direct.remove(head, n) |
| 198 |
l[#l+1] = n |
| 199 |
elseif id == disc_id then |
| 200 |
local pre, post, replace = getdisc(n) |
| 201 |
for nn in node.direct.traverse(pre) do |
| 202 |
if checkprop(nn) then |
| 203 |
local after |
| 204 |
pre, after = node.direct.remove(pre, nn) |
| 205 |
l[#l+1] = {nn, n, 'pre'} |
| 206 |
end |
| 207 |
end |
| 208 |
for nn in node.direct.traverse(post) do |
| 209 |
if checkprop(nn) then |
| 210 |
post = node.direct.remove(post, nn) |
| 211 |
l[#l+1] = {nn, n, 'post'} |
| 212 |
end |
| 213 |
end |
| 214 |
for nn in node.direct.traverse(replace) do |
| 215 |
if checkprop(nn) then |
| 216 |
replace = node.direct.remove(replace, nn) |
| 217 |
l[#l+1] = {nn, n, 'replace'} |
| 218 |
end |
| 219 |
end |
| 220 |
setdisc(n, pre, post, replace) |
| 221 |
end |
| 222 |
end |
| 223 |
return head |
| 224 |
end |
| 225 |
local getfield, setfield = node.direct.getfield, node.direct.setfield |
| 226 |
local function pop(head) |
| 227 |
head = node.direct.todirect(head) |
| 228 |
local l = list[#list] |
| 229 |
list[#list] = nil |
| 230 |
for i = #l,1,-1 do |
| 231 |
local e = l[i] |
| 232 |
local n = tonumber(e) |
| 233 |
local disc, thishead |
| 234 |
if n then |
| 235 |
thishead = head |
| 236 |
else |
| 237 |
disc, n = e[2], e[1] |
| 238 |
thishead = getfield(disc, e[3]) |
| 239 |
end |
| 240 |
local prev, next = node.direct.getboth(n) |
| 241 |
if prev or not next then |
| 242 |
thishead = node.direct.insert_after(thishead, prev, n) |
| 243 |
else |
| 244 |
thishead = node.direct.insert_before(thishead, next, n) |
| 245 |
end |
| 246 |
if disc then |
| 247 |
setfield(disc, e[3], thishead) |
| 248 |
else |
| 249 |
head = thishead |
| 250 |
end |
| 251 |
end |
| 252 |
return head |
| 253 |
end |
| 254 |
fonts.handlers.otf.handlers.marked_push = push |
| 255 |
fonts.handlers.otf.handlers.marked_pop = pop |
| 256 |
end |
| 257 |
local sequence1 = { |
| 258 |
features = {["semiignored-node"] = {["*"] = {["*"] = true}}}, |
| 259 |
flags = {false, false, false, false}, |
| 260 |
name = "semiignored-node", |
| 261 |
order = {"semiignored-node"}, |
| 262 |
type = "marked_push", |
| 263 |
} |
| 264 |
local sequence2 = { |
| 265 |
features = {["semiignored-node"] = {["*"] = {["*"] = true}}}, |
| 266 |
flags = {false, false, false, false}, |
| 267 |
name = "semiignored-node", |
| 268 |
order = {"semiignored-node"}, |
| 269 |
type = "marked_pop", |
| 270 |
} |
| 271 |
local function pushpopinitialiser(tfmdata, value, features) |
| 272 |
local resources = tfmdata.resources |
| 273 |
local sequences = resources and resources.sequences |
| 274 |
local first_gpos, last_gpos |
| 275 |
if sequences then |
| 276 |
local alreadydone |
| 277 |
for i=1,#sequences do |
| 278 |
local sequence = sequences[i] |
| 279 |
if sequence1 == sequence then |
| 280 |
return |
| 281 |
elseif sequence.type:sub(1,5) == "gpos_" then |
| 282 |
if not first_gpos then |
| 283 |
first_gpos = i |
| 284 |
end |
| 285 |
last_gpos = i |
| 286 |
end |
| 287 |
end |
| 288 |
if first_gpos then |
| 289 |
insert(sequences, last_gpos+1, sequence2) |
| 290 |
insert(sequences, first_gpos, sequence1) |
| 291 |
end |
| 292 |
end |
| 293 |
end |
| 294 |
|
| 295 |
otfregister { |
| 296 |
name = 'semiignored-node', |
| 297 |
description = 'Allow adding nodes which break ligatures but do not affect kerning', |
| 298 |
default = true, -- Should basically never be disabled manually |
| 299 |
initializers = { |
| 300 |
node = pushpopinitialiser, |
| 301 |
-- plug = ? -- TODO: Manually handle in luaotfload-harf-plug.lua |
| 302 |
}, |
| 303 |
} |
| 304 |
|
| 305 |
local delayed_remove do |
| 306 |
local delayed |
| 307 |
function delayed_remove(n) |
| 308 |
flush_node(delayed) |
| 309 |
delayed = n |
| 310 |
end |
| 311 |
end |
| 312 |
|
| 313 |
local function ignorablehandler(head, fid, ...) -- FIXME: The arguments are probably wrong |
| 314 |
local fontparam = font_invisible_replacement[fid] |
| 315 |
local replacement = fontparam and fontparam[1] |
| 316 |
local font_kern = fontparam and fontparam[2] |
| 317 |
for n, c, f in traverse_char(head) do if f == fid then |
| 318 |
local lookup = ignorable_codepoints[c] |
| 319 |
if lookup then |
| 320 |
if replacement then |
| 321 |
setchar(n, replacement) |
| 322 |
if font_kern then |
| 323 |
local k = nodenew(kern_id) |
| 324 |
setkern(k, font_kern) |
| 325 |
setattributelist(k, getattributelist(n)) |
| 326 |
head = insert_after(head, n, k) |
| 327 |
end |
| 328 |
else |
| 329 |
local after |
| 330 |
head, after = remove(head, n) |
| 331 |
delayed_remove(n) |
| 332 |
end |
| 333 |
end |
| 334 |
end end |
| 335 |
delayed_remove() |
| 336 |
for n in traverse_id(disc_id, head) do |
| 337 |
local a, b, c = getdisc(n) |
| 338 |
setdisc(ignorablehandler(a, fid), ignorablehandler(b, fid), ignorablehandler(c, fid)) |
| 339 |
end |
| 340 |
return head |
| 341 |
end |
| 342 |
|
| 343 |
if harfbuzz then |
| 344 |
local harf_settings = luaotfload.harf |
| 345 |
local preserve_flag = harfbuzz.Buffer.FLAG_PRESERVE_DEFAULT_IGNORABLES or 0 |
| 346 |
local remove_flag = harfbuzz.Buffer.FLAG_REMOVE_DEFAULT_IGNORABLES or 0 |
| 347 |
local dotted_circle_flag = harfbuzz.Buffer.FLAG_DO_NOT_INSERT_DOTTED_CIRCLE or 0 |
| 348 |
harf_settings.default_buf_flags = (harf_settings.default_buf_flags & ~remove_flag) | preserve_flag | dotted_circle_flag |
| 349 |
local function dottedcircleinitialize(tfmdata, value) |
| 350 |
if not tfmdata.hb then return end |
| 351 |
local hb = tfmdata.hb |
| 352 |
hb.buf_flags = hb.buf_flags & ~dotted_circle_flag |
| 353 |
end |
| 354 |
otfregister { |
| 355 |
name = 'dottedcircle', |
| 356 |
description = 'Insert dotted circle to fix invalid clusters', |
| 357 |
default = true, |
| 358 |
initializers = { |
| 359 |
plug = dottedcircleinitialize, |
| 360 |
}, |
| 361 |
} |
| 362 |
end |
| 363 |
otfregister { |
| 364 |
name = 'invisible', |
| 365 |
description = 'Remove invisible control characters', |
| 366 |
default = true, |
| 367 |
processors = { |
| 368 |
node = ignorablehandler, |
| 369 |
plug = ignorablehandler, |
| 370 |
}, |
| 371 |
} |
| 372 |
|
| 373 |
--- vim:sw=2:ts=2:expandtab:tw=71 |