| 1 |
----------------------------------------------------------------------- |
| 2 |
-- FILE: luaotfload-szss.lua |
| 3 |
-- DESCRIPTION: part of luaotfload / szss |
| 4 |
----------------------------------------------------------------------- |
| 5 |
|
| 6 |
assert(luaotfload_module, "This is a part of luaotfload and should not be loaded independently") { |
| 7 |
name = "luaotfload-szss", |
| 8 |
version = "3.29", --TAGVERSION |
| 9 |
date = "2024-12-03", --TAGDATE |
| 10 |
description = "luaotfload submodule / Special feature handling", |
| 11 |
license = "GPL v2.0", |
| 12 |
author = "Marcel Krüger" |
| 13 |
} |
| 14 |
|
| 15 |
local direct = node.direct |
| 16 |
local otfregister = fonts.constructors.features.otf.register |
| 17 |
|
| 18 |
local copy = direct.copy |
| 19 |
local getdisc = direct.getdisc |
| 20 |
local getnext = direct.getnext |
| 21 |
local insert_after = direct.insert_after |
| 22 |
local is_char = direct.is_char |
| 23 |
local setchar = direct.setchar |
| 24 |
local setdisc = direct.setdisc |
| 25 |
|
| 26 |
local disc_t = node.id'disc' |
| 27 |
|
| 28 |
local szsstable = setmetatable({}, { __index = function(t, i) |
| 29 |
local v = font.getfont(i) |
| 30 |
v = v and v.properties |
| 31 |
v = v and v.transform_sz or false |
| 32 |
t[i] = v |
| 33 |
return v |
| 34 |
end}) |
| 35 |
|
| 36 |
local function szssinitializer(tfmdata, value, features) |
| 37 |
if value == 'auto' then |
| 38 |
value = not tfmdata.characters[0x1E9E] |
| 39 |
end |
| 40 |
local properties = tfmdata.properties |
| 41 |
properties.transform_sz = value |
| 42 |
end |
| 43 |
|
| 44 |
local function szssprocessor(head,font) -- ,attr,direction) |
| 45 |
if not szsstable[font] then return end |
| 46 |
local n = head |
| 47 |
while n do |
| 48 |
local c, id = is_char(n, font) |
| 49 |
if c == 0x1E9E then |
| 50 |
setchar(n, 0x53) |
| 51 |
head, n = insert_after(head, n, copy(n)) |
| 52 |
elseif id == disc_t then |
| 53 |
local pre, post, replace = getdisc(n) |
| 54 |
pre = szssprocessor(pre, font) |
| 55 |
post = szssprocessor(post, font) |
| 56 |
replace = szssprocessor(replace, font) |
| 57 |
setdisc(n, pre, post, replace) |
| 58 |
end |
| 59 |
n = getnext(n) |
| 60 |
end |
| 61 |
return head |
| 62 |
end |
| 63 |
|
| 64 |
otfregister { |
| 65 |
name = 'szss', |
| 66 |
description = 'Replace capital ß with SS', |
| 67 |
default = 'auto', |
| 68 |
initializers = { |
| 69 |
node = szssinitializer, |
| 70 |
plug = szssinitializer, |
| 71 |
}, |
| 72 |
processors = { |
| 73 |
position = 1, |
| 74 |
node = szssprocessor, |
| 75 |
plug = szssprocessor, |
| 76 |
}, |
| 77 |
} |
| 78 |
|
| 79 |
-- harf-only features (for node they are implemented in the fontloader |
| 80 |
|
| 81 |
otfregister { |
| 82 |
name = 'extend', |
| 83 |
description = 'Fake extend', |
| 84 |
default = false, |
| 85 |
manipulators = { |
| 86 |
plug = function(tfmdata, _, value) |
| 87 |
value = tonumber(value) |
| 88 |
if not value then |
| 89 |
error[[Invalid extend value]] |
| 90 |
end |
| 91 |
tfmdata.extend = value * 1000 |
| 92 |
tfmdata.hb.hscale = tfmdata.units_per_em * value |
| 93 |
tfmdata.hb.space = tfmdata.hb.space * value |
| 94 |
local parameters = tfmdata.parameters |
| 95 |
parameters.slant = parameters.slant * value |
| 96 |
parameters.space = parameters.space * value |
| 97 |
parameters.space_stretch = parameters.space_stretch * value |
| 98 |
parameters.space_shrink = parameters.space_shrink * value |
| 99 |
parameters.quad = parameters.quad * value |
| 100 |
parameters.extra_space = parameters.extra_space * value |
| 101 |
local done = {} |
| 102 |
for _, char in next, tfmdata.characters do |
| 103 |
if char.width and not done[char] then |
| 104 |
char.width = char.width * value |
| 105 |
done[char] = true |
| 106 |
end |
| 107 |
end |
| 108 |
end, |
| 109 |
}, |
| 110 |
} |
| 111 |
|
| 112 |
otfregister { |
| 113 |
name = 'slant', |
| 114 |
description = 'Fake slant', |
| 115 |
default = false, |
| 116 |
manipulators = { |
| 117 |
plug = function(tfmdata, _, value) |
| 118 |
value = tonumber(value) |
| 119 |
if not value then |
| 120 |
error[[Invalid slant value]] |
| 121 |
end |
| 122 |
tfmdata.slant = value * 1000 |
| 123 |
local parameters = tfmdata.parameters |
| 124 |
parameters.slant = parameters.slant + value * 65536 |
| 125 |
end, |
| 126 |
}, |
| 127 |
} |
| 128 |
|
| 129 |
otfregister { |
| 130 |
name = 'squeeze', |
| 131 |
description = 'Fake squeeze', |
| 132 |
default = false, |
| 133 |
manipulators = { |
| 134 |
plug = function(tfmdata, _, value) |
| 135 |
value = tonumber(value) |
| 136 |
if not value then |
| 137 |
error[[Invalid squeeze value]] |
| 138 |
end |
| 139 |
tfmdata.squeeze = value * 1000 |
| 140 |
tfmdata.hb.vscale = tfmdata.units_per_em * value |
| 141 |
local parameters = tfmdata.parameters |
| 142 |
parameters.slant = parameters.slant / value |
| 143 |
parameters.x_height = parameters.x_height * value |
| 144 |
parameters[8] = parameters[8] * value |
| 145 |
local done = {} |
| 146 |
for _, char in next, tfmdata.characters do |
| 147 |
if not done[char] then |
| 148 |
if char.height then |
| 149 |
char.height = char.height * value |
| 150 |
end |
| 151 |
if char.depth then |
| 152 |
char.depth = char.depth * value |
| 153 |
end |
| 154 |
done[char] = true |
| 155 |
end |
| 156 |
end |
| 157 |
end, |
| 158 |
}, |
| 159 |
} |
| 160 |
|
| 161 |
-- Legacy TeX Input Method Disguised as Font Ligatures hack. |
| 162 |
-- |
| 163 |
-- Single replacements, keyed by character to replace. Handled separately |
| 164 |
-- because TeX ligaturing mechanism does not support one-to-one replacements. |
| 165 |
local trep = { |
| 166 |
[0x0022] = 0x201D, -- ["] |
| 167 |
[0x0027] = 0x2019, -- ['] |
| 168 |
[0x0060] = 0x2018, -- [`] |
| 169 |
} |
| 170 |
|
| 171 |
-- Ligatures. The value is a character "ligature" table as described in the |
| 172 |
-- manual. |
| 173 |
local tlig ={ |
| 174 |
[0x2013] = { [0x002D] = { char = 0x2014 } }, -- [---] |
| 175 |
[0x002D] = { [0x002D] = { char = 0x2013 } }, -- [--] |
| 176 |
[0x0060] = { [0x0060] = { char = 0x201C } }, -- [``] |
| 177 |
[0x0027] = { [0x0027] = { char = 0x201D } }, -- [''] |
| 178 |
[0x0021] = { [0x0060] = { char = 0x00A1 } }, -- [!`] |
| 179 |
[0x003F] = { [0x0060] = { char = 0x00BF } }, -- [?`] |
| 180 |
[0x002C] = { [0x002C] = { char = 0x201E } }, -- [,,] |
| 181 |
[0x003C] = { [0x003C] = { char = 0x00AB } }, -- [<<] |
| 182 |
[0x003E] = { [0x003E] = { char = 0x00BB } }, -- [>>] |
| 183 |
} |
| 184 |
|
| 185 |
local function tligprocessor(head, font) |
| 186 |
local n = head |
| 187 |
while n do |
| 188 |
local c, id = is_char(n, font) |
| 189 |
local rep = trep[c] |
| 190 |
if rep then |
| 191 |
setchar(n, rep) |
| 192 |
elseif id == disc_t then |
| 193 |
local pre, post, replace = getdisc(n) |
| 194 |
tligprocessor(pre, font) |
| 195 |
tligprocessor(post, font) |
| 196 |
tligprocessor(replace, font) |
| 197 |
end |
| 198 |
n = getnext(n) |
| 199 |
end |
| 200 |
end |
| 201 |
|
| 202 |
otfregister { |
| 203 |
name = 'tlig', |
| 204 |
description = 'Traditional TeX ligatures', |
| 205 |
default = false, |
| 206 |
manipulators = { |
| 207 |
plug = function(tfmdata, _, value) |
| 208 |
local characters = tfmdata.characters |
| 209 |
for codepoint, ligatures in next, tlig do |
| 210 |
local char = characters[codepoint] |
| 211 |
if char then |
| 212 |
char.ligatures = ligatures |
| 213 |
end |
| 214 |
end |
| 215 |
end, |
| 216 |
}, |
| 217 |
processors = { |
| 218 |
position=1, |
| 219 |
plug = tligprocessor, |
| 220 |
}, |
| 221 |
} |
| 222 |
|
| 223 |
--- vim:sw=2:ts=2:expandtab:tw=71 |