| 1 |
-- *** UTILITY FUNCTIONS *** |
| 2 |
local function get_locals (tab) |
| 3 |
local tb = {} |
| 4 |
for lib, keys in pairs(tab) do |
| 5 |
keys = string.explode(keys) |
| 6 |
for _, k in ipairs(keys) do |
| 7 |
tb[k] = _G[lib][k] |
| 8 |
end |
| 9 |
end |
| 10 |
return tb |
| 11 |
end |
| 12 |
|
| 13 |
-- str |
| 14 |
-- string manipulation |
| 15 |
local str = get_locals {string = "explode gsub match format lower upper"} |
| 16 |
|
| 17 |
-- Removes space at the beginning and end and conflates multiple spaces. |
| 18 |
function str.trim (s) |
| 19 |
s = str.match(s, "^%s*(.-)%s*$") |
| 20 |
s = str.gsub(s, "%s+", " ") |
| 21 |
return s |
| 22 |
end |
| 23 |
|
| 24 |
-- Extracts a pattern from a string, i.e. removes it and returns it. |
| 25 |
-- If "full", then the entire pattern is removed, not only the captures. |
| 26 |
function str.extract (s, pat, full) |
| 27 |
local cap = str.match(s, pat) |
| 28 |
if cap then |
| 29 |
if full then |
| 30 |
s = str.gsub(s, pat, "") |
| 31 |
else |
| 32 |
s = str.gsub(s, cap, "") |
| 33 |
end |
| 34 |
end |
| 35 |
return cap, s |
| 36 |
end |
| 37 |
-- /str |
| 38 |
|
| 39 |
|
| 40 |
-- lp |
| 41 |
-- advanced string manipulation |
| 42 |
lpeg.locale(lpeg) |
| 43 |
local lp = get_locals {lpeg = "match P C S Ct V alnum"} |
| 44 |
lp.space = lpeg.space^0 |
| 45 |
-- /lp |
| 46 |
|
| 47 |
|
| 48 |
-- tab |
| 49 |
-- table manipulation |
| 50 |
local tab = get_locals {table = "insert remove sort"} |
| 51 |
|
| 52 |
-- Adds val to tab, creating it if necessary. |
| 53 |
function tab.update (tb, val) |
| 54 |
tb = tb or {} |
| 55 |
tab.insert(tb, val) |
| 56 |
return tb |
| 57 |
end |
| 58 |
|
| 59 |
-- Adds subtable (empty, or val) to tb at entry key, unless it already exists. |
| 60 |
function tab.subtable (tb, key, val) |
| 61 |
tb[key] = tb[key] or val or {} |
| 62 |
end |
| 63 |
|
| 64 |
-- Writes a table to an extenal file. |
| 65 |
local function write_key (key, ind) |
| 66 |
return ind .. '["' .. key .. '"] = ' |
| 67 |
end |
| 68 |
|
| 69 |
function tab.write (tb, f, ind) |
| 70 |
for a, b in pairs (tb) do |
| 71 |
if type(a) == "string" then |
| 72 |
a = '"' .. a .. '"' |
| 73 |
end |
| 74 |
a = "[" .. a .. "]" |
| 75 |
if type(b) == "table" then |
| 76 |
f:write(ind, a, " = {") |
| 77 |
tab.write(b, f, ind .. " ") |
| 78 |
f:write(ind, "},") |
| 79 |
else |
| 80 |
if type(b) == "boolean" then |
| 81 |
b = b and "true" or "false" |
| 82 |
elseif type(b) == "string" then |
| 83 |
b = '"' .. b .. '"' |
| 84 |
end |
| 85 |
f:write(ind, a, " = ", b, ",") |
| 86 |
end |
| 87 |
end |
| 88 |
end |
| 89 |
|
| 90 |
-- Returns a full copy of a table. Not copying of metatables necessary for the |
| 91 |
-- moment. |
| 92 |
function tab.copy (tb) |
| 93 |
local t = {} |
| 94 |
for k, v in pairs (tb) do |
| 95 |
if type(v) == "table" then |
| 96 |
v = tab.copy(v) |
| 97 |
end |
| 98 |
t[k] = v |
| 99 |
end |
| 100 |
return t |
| 101 |
end |
| 102 |
|
| 103 |
-- Sorts two tables containing modifiers (Italic, etc.). |
| 104 |
function tab.sortmods (a, b) |
| 105 |
local A, B = "", "" |
| 106 |
for _, x in ipairs (a) do |
| 107 |
A = A .. " " .. x |
| 108 |
end |
| 109 |
for _, x in ipairs (b) do |
| 110 |
B = B .. " " .. x |
| 111 |
end |
| 112 |
return A < B |
| 113 |
end |
| 114 |
|
| 115 |
-- Turns an array into a hash. |
| 116 |
function tab.tohash(tb) |
| 117 |
local t = {} |
| 118 |
for _, k in ipairs(tb) do |
| 119 |
t[k] = true |
| 120 |
end |
| 121 |
return t |
| 122 |
end |
| 123 |
-- /tab |
| 124 |
|
| 125 |
-- lfs |
| 126 |
-- files etc. |
| 127 |
local lfs = get_locals {lfs = "dir isdir isfile mkdir", kpse = "expand_var show_path find_file"} |
| 128 |
|
| 129 |
-- Returns anything after the last dot, i.e. an extension. |
| 130 |
function lfs.extension (s) |
| 131 |
return str.lower(str.match(s, "%.([^%.]*)$")) |
| 132 |
end |
| 133 |
|
| 134 |
local extensions = { |
| 135 |
otf = "opentype", |
| 136 |
ttf = "truetype", |
| 137 |
ttc = "truetype", |
| 138 |
} |
| 139 |
function lfs.type (s) |
| 140 |
return extensions[lfs.extension(s)] |
| 141 |
end |
| 142 |
|
| 143 |
local kpse_extensions = { |
| 144 |
otf = "opentype fonts", |
| 145 |
ttf = "truetype fonts", |
| 146 |
ttc = "truetype fonts", |
| 147 |
} |
| 148 |
function lfs.kpse (s) |
| 149 |
return kpse_extensions[lfs.extension(s)] |
| 150 |
end |
| 151 |
|
| 152 |
-- Returns anything after the last slash, i.e. a pathless file. |
| 153 |
function lfs.nopath (f) |
| 154 |
return str.match(f, "[^/]*$") |
| 155 |
end |
| 156 |
|
| 157 |
-- Creates a directory; the arguments are the successive subdirectories. |
| 158 |
function lfs.ensure_dir (...) |
| 159 |
local arg, path = {...} |
| 160 |
for _, d in ipairs(arg) do |
| 161 |
if path then |
| 162 |
path = path .. "/" .. d |
| 163 |
else |
| 164 |
path = d |
| 165 |
end |
| 166 |
path = str.gsub(path, "//", "/") |
| 167 |
if not lfs.isdir(path) then |
| 168 |
lfs.mkdir(path) |
| 169 |
end |
| 170 |
end |
| 171 |
return path |
| 172 |
end |
| 173 |
|
| 174 |
-- Turns "foo/blahblah/../" into "foo/" (such going into and leaving |
| 175 |
-- directories happens with kpse). Also puts everything to lowercase. |
| 176 |
function lfs.smooth_file (f) |
| 177 |
f = str.gsub(f, "/.-/%.%./", "/") |
| 178 |
f = str.gsub(f, "^%a", str.lower) |
| 179 |
return f |
| 180 |
end |
| 181 |
-- /lfs |
| 182 |
|
| 183 |
|
| 184 |
-- wri |
| 185 |
-- messages |
| 186 |
local wri, write_nl = {}, texio.write_nl |
| 187 |
|
| 188 |
function wri.report (s, ...) |
| 189 |
write_nl(str.format(s, unpack(arg))) |
| 190 |
end |
| 191 |
|
| 192 |
function wri.error (s, ...) |
| 193 |
tex.error(str.format(s, unpack(arg))) |
| 194 |
end |
| 195 |
-- /wri |
| 196 |
|
| 197 |
|
| 198 |
-- various |
| 199 |
-- the last one is, of course, not the least |
| 200 |
local io = get_locals {io = "open lines"} |
| 201 |
local os = get_locals {os = "date"} |
| 202 |
local num = get_locals {math = "abs tan rad floor pi", tex = "sp"} |
| 203 |
local fl = get_locals {fontloader = "open close to_table info", font = "read_tfm"} |
| 204 |
-- /various |
| 205 |
|
| 206 |
|
| 207 |
-- *** END OF UTILITY FUNCTIONS *** |
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
--- *** CREATING THE LIBRARY *** --- |
| 212 |
local settings |
| 213 |
if lfs.find_file"foundry-settings.lua" then |
| 214 |
settings = require"foundry-settings.lua" |
| 215 |
else |
| 216 |
settings = {normal = {}, features = {}} |
| 217 |
end |
| 218 |
local font_families = {} |
| 219 |
local normal_names = {} |
| 220 |
for _, name in ipairs(settings.normal) do |
| 221 |
normal_names[name] = true |
| 222 |
end |
| 223 |
local local_path = lfs.expand_var("$TEXMFLOCAL") |
| 224 |
local foundry_path = lfs.ensure_dir (local_path, "tex", "luatex", "foundry") |
| 225 |
local library_file = foundry_path .. "/" .. "readable.txt" |
| 226 |
--local library_file = "c:/texlive/texmf-local/tex/plain/pitex/readable.txt" |
| 227 |
--local library_file = "readable.txt" |
| 228 |
|
| 229 |
-- Analyze a font file and return a name and a table |
| 230 |
-- with modifiers. |
| 231 |
local function extract_font (file, names) |
| 232 |
local fi, subname |
| 233 |
-- Trying to open a font in ttc, using the names returned by fontloader.info |
| 234 |
if names then |
| 235 |
local name = names.fullname |
| 236 |
if name then |
| 237 |
fi = fl.open (file, name) |
| 238 |
end |
| 239 |
if not fi then |
| 240 |
name = names.fontname |
| 241 |
if name then |
| 242 |
fi = fl.open |
| 243 |
end |
| 244 |
if not fi then |
| 245 |
fl.error("Can't open %s", file) |
| 246 |
return |
| 247 |
end |
| 248 |
end |
| 249 |
subname = name |
| 250 |
else |
| 251 |
fi = fl.open(file) |
| 252 |
end |
| 253 |
-- Getting the most precise information. Not necessarily the best |
| 254 |
-- solution, but since the user can modify the library, it's not so bad. |
| 255 |
local fam, name = fi.names[1].names.preffamilyname or fi.names[1].names.family or fi.familyname, fi.fontname |
| 256 |
local spec = fi.names[1].names.prefmodifiers or fi.names[1].names.subfamily or "" |
| 257 |
local subfam, _spec |
| 258 |
local t = { [0] = file } |
| 259 |
-- Removing mods like Regular, Book, etc. |
| 260 |
for name in pairs(normal_names) do |
| 261 |
spec = str.gsub(spec, name, "") |
| 262 |
end |
| 263 |
if subname then |
| 264 |
tab.insert(t, "[font = " .. subname .. "]") |
| 265 |
end |
| 266 |
if spec ~= "" then |
| 267 |
spec = str.explode(spec) |
| 268 |
for _, s in ipairs(spec) do |
| 269 |
tab.insert(t, s) |
| 270 |
end |
| 271 |
end |
| 272 |
fl.close(fi) |
| 273 |
return fam, t |
| 274 |
end |
| 275 |
|
| 276 |
-- Searches directories for font files, and pass them to |
| 277 |
-- extract_font. The fonts are collected in a table. |
| 278 |
-- The fonts_done table is updated when the library is read, |
| 279 |
-- so when a font is missing and one needs to recheck files, |
| 280 |
-- only those that arent in the libraries are considered. |
| 281 |
local fonts_done = {} |
| 282 |
local function check_fonts (rep, tb) |
| 283 |
for f in lfs.dir (rep) do |
| 284 |
if f ~= "." and f ~= ".." then |
| 285 |
f = str.gsub(rep, "/$", "") .. "/" .. f |
| 286 |
if lfs.isdir(f) then |
| 287 |
check_fonts(f, tb) |
| 288 |
elseif lfs.isfile(f) and not fonts_done[lfs.nopath(f)] then |
| 289 |
local e = lfs.extension(f) |
| 290 |
if e == "ttf" or e == "otf" then |
| 291 |
local fam, file = extract_font(f) |
| 292 |
if fam then |
| 293 |
tab.subtable(tb, fam) |
| 294 |
tab.insert(tb[fam], file) |
| 295 |
end |
| 296 |
elseif e == "ttc" then |
| 297 |
local info = fl.info(f) |
| 298 |
for _, i in ipairs(info) do |
| 299 |
local fam, file = extract_font(f, i) |
| 300 |
if fam then |
| 301 |
tab.subtable(tb, fam) |
| 302 |
tab.insert(tb[fam], file) |
| 303 |
end |
| 304 |
end |
| 305 |
end |
| 306 |
end |
| 307 |
end |
| 308 |
end |
| 309 |
end |
| 310 |
|
| 311 |
-- Writes the library to an external file. |
| 312 |
-- Type is "a" if what's going on is recheck_fonts. |
| 313 |
local function write_lib (fams, file, type) |
| 314 |
local read_table = {} |
| 315 |
for fam, tb in pairs(fams) do |
| 316 |
tab.insert(read_table, fam) |
| 317 |
for _, ttb in ipairs(tb) do |
| 318 |
tab.sort(ttb) |
| 319 |
end |
| 320 |
tab.sort(tb, tab.sortmods) |
| 321 |
end |
| 322 |
tab.sort(read_table) |
| 323 |
local readable = io.open(file, type) |
| 324 |
for n, fam in ipairs(read_table) do |
| 325 |
local log |
| 326 |
if type == "a" then |
| 327 |
log = true |
| 328 |
if n == 1 then |
| 329 |
wri.report("\nAdding new font(s):") |
| 330 |
readable:write("\n\n% Added automatically " .. os.date() .. "\n\n") |
| 331 |
end |
| 332 |
wri.report(fam) |
| 333 |
end |
| 334 |
readable:write(fam .. " :") |
| 335 |
for n, f in ipairs(fams[fam]) do |
| 336 |
log = log and " " |
| 337 |
readable:write("\n ") |
| 338 |
for _, t in ipairs(f) do |
| 339 |
log = log and log .. " " .. t |
| 340 |
readable:write(" " .. t) |
| 341 |
end |
| 342 |
log = log and log .. " " .. '"' .. f[0] .. '"' |
| 343 |
readable:write(" " .. '"' .. lfs.nopath(f[0]) .. '",') |
| 344 |
if log then wri.report(log) end |
| 345 |
if n == #fams[fam] then |
| 346 |
readable:write("\n\n") |
| 347 |
end |
| 348 |
end |
| 349 |
end |
| 350 |
readable:close() |
| 351 |
end |
| 352 |
|
| 353 |
|
| 354 |
-- If there is no library, we create it. |
| 355 |
local font_paths = lfs.show_path("opentype fonts") |
| 356 |
font_paths = str.gsub(font_paths, "\\", "/") |
| 357 |
font_paths = str.gsub(font_paths, "/+", "/") |
| 358 |
font_paths = str.gsub(font_paths, "!!", "") |
| 359 |
font_paths = str.explode(font_paths, ";+") |
| 360 |
|
| 361 |
if not lfs.find_file(library_file) then |
| 362 |
wri.report("I must create the library; please wait, that can take some time.") |
| 363 |
for _, rep in ipairs(font_paths) do |
| 364 |
check_fonts(rep, font_families) |
| 365 |
end |
| 366 |
write_lib(font_families, library_file, "w") |
| 367 |
end |
| 368 |
|
| 369 |
-- Reads the library file, turning it into a table. |
| 370 |
local explode_semicolon = lp.P{ |
| 371 |
lp.Ct(lp.V"data"^1), |
| 372 |
data = lp.C(((1 - lp.S";[") + (lp.S"[" * (1 - lp.S"]")^0 * lp.S"]" ))^1) / str.trim * (lp.S";" + -1), |
| 373 |
} |
| 374 |
local explode_comma = lp.P{ |
| 375 |
lp.Ct(lp.V"data"^1), |
| 376 |
data = lp.C(((1 - lp.S",[") + (lp.S"[" * (1 - lp.S"]")^0 * lp.S"]" ))^1) / str.trim * (lp.S"," + -1), |
| 377 |
} |
| 378 |
|
| 379 |
local function load_library (lib) |
| 380 |
local LIB = "" |
| 381 |
local lib_file = lfs.find_file(lib) |
| 382 |
if not lib_file then |
| 383 |
wri.error("I can't find library %s.", lib) |
| 384 |
return |
| 385 |
end |
| 386 |
|
| 387 |
for l in io.lines(lib_file) do |
| 388 |
if not str.match(l, "^%s*%%") then |
| 389 |
if str.match(l, "^%s*$") then |
| 390 |
LIB = LIB .. ";" |
| 391 |
else |
| 392 |
LIB = LIB .. " " .. l |
| 393 |
end |
| 394 |
end |
| 395 |
end |
| 396 |
LIB = str.gsub(LIB, ";%s*;", ";;") |
| 397 |
LIB = str.gsub(LIB, ";+", ";") |
| 398 |
LIB = str.gsub(LIB, "^;", "") |
| 399 |
LIB = str.gsub(LIB, "%s+", " ") |
| 400 |
|
| 401 |
LIB = lp.match(explode_semicolon, LIB) |
| 402 |
|
| 403 |
local newlib = {} |
| 404 |
for _, t in ipairs(LIB) do |
| 405 |
local fam, files = str.match(t, "(.-):(.*)") |
| 406 |
local current_mods |
| 407 |
if files then |
| 408 |
files = lp.match(explode_comma, files) |
| 409 |
fam = str.explode(fam, ",") |
| 410 |
local root |
| 411 |
for n, f in ipairs (fam) do |
| 412 |
f = str.trim(f) |
| 413 |
if n == 1 then |
| 414 |
root = f |
| 415 |
if type(newlib[f]) == "string" then |
| 416 |
wri.error("Name `%s' is already used as an alias for `%s'; it is now overwritten to denote a family", f, newlib[f]) |
| 417 |
newlib[f] = {} |
| 418 |
else |
| 419 |
newlib[f] = newlib[f] or {} |
| 420 |
end |
| 421 |
else |
| 422 |
if newlib[f] then |
| 423 |
wri.error("The name `%s' is already used. I ignore it as an alias for `%s'", f, root) |
| 424 |
else |
| 425 |
newlib[f] = root |
| 426 |
end |
| 427 |
end |
| 428 |
end |
| 429 |
for _, f in ipairs(files) do |
| 430 |
local reset |
| 431 |
reset, f = str.extract(f, "^%.%.", true) |
| 432 |
if reset then current_mods = nil end |
| 433 |
local mods, file, feats = str.match(f, '([^"]*)"(.*)"') |
| 434 |
if mods then |
| 435 |
fonts_done[lfs.nopath(file)] = true |
| 436 |
feats, mods = str.extract(mods, "%[([^%]]-)]", true) |
| 437 |
mods = str.explode(mods) |
| 438 |
if current_mods then |
| 439 |
for _, t in ipairs(current_mods) do |
| 440 |
tab.insert(mods, t) |
| 441 |
end |
| 442 |
if current_mods.feats then |
| 443 |
feats = feats or "" |
| 444 |
feats = current_mods.feats .. "," .. feats |
| 445 |
end |
| 446 |
end |
| 447 |
local sizes, real_mods = {}, {} |
| 448 |
for n, s in ipairs(mods) do |
| 449 |
if tonumber(s) then |
| 450 |
tab.insert(sizes, tonumber(s)) |
| 451 |
else |
| 452 |
tab.insert(real_mods, s) |
| 453 |
end |
| 454 |
end |
| 455 |
sizes = #sizes > 0 and sizes or {0} |
| 456 |
tab.sort(real_mods) |
| 457 |
local T = newlib[root] |
| 458 |
for _, t in ipairs(real_mods) do |
| 459 |
t = str.trim(t) |
| 460 |
if t ~= "" then |
| 461 |
T[t] = T[t] or {} |
| 462 |
T = T[t] |
| 463 |
end |
| 464 |
end |
| 465 |
T.__files = T.__files or {} |
| 466 |
for _, s in ipairs(sizes) do |
| 467 |
T.__files[s] = {str.trim(file), feats} |
| 468 |
end |
| 469 |
else |
| 470 |
feats, mods = str.extract(f, "%[([^%]]-)]", true) |
| 471 |
if current_mods then |
| 472 |
for _, mod in ipairs(str.explode(mods)) do |
| 473 |
tab.insert(current_mods, mod) |
| 474 |
end |
| 475 |
if feats then |
| 476 |
current_mods.feats = current_mods.feats and current_mods.feats .. "," .. feats or feats |
| 477 |
end |
| 478 |
else |
| 479 |
current_mods = str.explode(mods) |
| 480 |
current_mods.feats = feats |
| 481 |
end |
| 482 |
end |
| 483 |
end |
| 484 |
end |
| 485 |
end |
| 486 |
return newlib |
| 487 |
end |
| 488 |
|
| 489 |
local library = {} |
| 490 |
library.default = load_library(library_file) |
| 491 |
|
| 492 |
-- Same as above, but used when rechecking (if a font isn't found in libraries). |
| 493 |
local function recheck_fonts () |
| 494 |
local tb = {} |
| 495 |
for _, rep in ipairs(font_paths) do |
| 496 |
check_fonts(rep, tb) |
| 497 |
end |
| 498 |
write_lib(tb, library_file, "a") |
| 499 |
library.default = load_library(library_file) |
| 500 |
end |
| 501 |
|
| 502 |
|
| 503 |
-- This is public. |
| 504 |
function new_library (lib) |
| 505 |
local l = load_library(lib) |
| 506 |
if l then |
| 507 |
tab.insert(library, l) |
| 508 |
end |
| 509 |
end |
| 510 |
|
| 511 |
--- *** END OF LIBRARY MANAGEMENT *** --- |
| 512 |
|
| 513 |
|
| 514 |
|
| 515 |
--- *** FONT CREATION *** --- |
| 516 |
|
| 517 |
|
| 518 |
-- Creates a new substitution for trep. |
| 519 |
local function add_sub (f, num, sub) |
| 520 |
num, sub = f.map.map[num], f.map.map[sub] |
| 521 |
if f.glyphs[num] and f.glyphs[sub] then |
| 522 |
local x = f.glyphs[num] |
| 523 |
tab.subtable(x, "lookups") |
| 524 |
x.lookups.tex_trep = { { type = "substitution", specification = {variant = f.glyphs[sub].name} } } |
| 525 |
end |
| 526 |
end |
| 527 |
|
| 528 |
-- Creates a new ligature for tlig. |
| 529 |
local function add_lig (f, lig, ...) |
| 530 |
lig = f.map.map[lig] |
| 531 |
if lig then |
| 532 |
arg = {...} |
| 533 |
local components |
| 534 |
for _, c in ipairs(arg) do |
| 535 |
c = f.map.map[c] |
| 536 |
c = c and f.glyphs[c] |
| 537 |
if c then |
| 538 |
c = c.name |
| 539 |
components = components and components .. " " .. c or c |
| 540 |
else |
| 541 |
components = nil |
| 542 |
break |
| 543 |
end |
| 544 |
end |
| 545 |
if components then |
| 546 |
local x = f.glyphs[lig] |
| 547 |
tab.subtable(x, "lookups") |
| 548 |
tab.subtable(x.lookups, "tex_tlig") |
| 549 |
tab.insert(x.lookups.tex_tlig, { type = "ligature", |
| 550 |
specification = {char = x.name, components = components} }) |
| 551 |
end |
| 552 |
end |
| 553 |
end |
| 554 |
|
| 555 |
-- Loops over all the constitutents of ligatures, creating intermediary |
| 556 |
-- ligatures if necessary. E.g. "f f i" is broken into: |
| 557 |
-- f + f = ff.lig |
| 558 |
-- ff.lig + i = ffi.lig |
| 559 |
-- Then when loading the font if the intermediary ligatures do not exist |
| 560 |
-- (e.g. "1/" in "1 / 4") a phantom character is added to the font; which might |
| 561 |
-- be dangerous (e.g. "1/" will create a node without character if no "4" follows). |
| 562 |
-- The ".lig" suffix is arbitrary but all glyphs marked as ligatures are also registered |
| 563 |
-- with such a name, so if there's an "f f" ligature in a font, no matter its name, "ff.lig" |
| 564 |
-- will point to it. |
| 565 |
local function ligature (comp, tb, phantoms) |
| 566 |
local i = str.gsub(comp[1], "%.lig$", "") .. comp[2] .. ".lig" |
| 567 |
phantoms[i] = true |
| 568 |
tab.insert(tb.all_ligs, i) |
| 569 |
tab.subtable(tb, comp[1]) |
| 570 |
tb[comp[1]][comp[2]] = { char = i, type = 0 } -- The type could be something else. |
| 571 |
tab.remove(comp, 1) |
| 572 |
tab.remove(comp, 1) |
| 573 |
if #comp > 0 then |
| 574 |
tab.insert(comp, 1, i) |
| 575 |
ligature(comp, tb, phantoms) |
| 576 |
end |
| 577 |
end |
| 578 |
|
| 579 |
local function get_lookups (t, lookup_table) |
| 580 |
if t then |
| 581 |
for _, tb in pairs(t) do |
| 582 |
local _tb = { tags = {} } |
| 583 |
if tb.features then |
| 584 |
for _, feats in pairs(tb.features) do |
| 585 |
local _tag = {} |
| 586 |
if feats.scripts then |
| 587 |
for _, scr in pairs(feats.scripts) do |
| 588 |
_tag[scr.script] = {} |
| 589 |
for _, lang in pairs(scr.langs) do |
| 590 |
tab.insert (_tag[scr.script], str.trim(lang)) |
| 591 |
end |
| 592 |
end |
| 593 |
end |
| 594 |
for _, sub in pairs(tb.subtables) do |
| 595 |
tab.insert(_tb, sub.name) |
| 596 |
end |
| 597 |
_tb.tags[feats.tag] = _tag |
| 598 |
end |
| 599 |
end |
| 600 |
if tb.name then |
| 601 |
local tp = tb.type or "no_type" |
| 602 |
tab.subtable(lookup_table, tp) |
| 603 |
lookup_table[tp][tb.name] = _tb |
| 604 |
end |
| 605 |
end |
| 606 |
end |
| 607 |
end |
| 608 |
|
| 609 |
function create_font (filename, extension, path, subfont, write) |
| 610 |
|
| 611 |
local data = fl.open(filename, subfont) |
| 612 |
fontfile = fl.to_table(data) |
| 613 |
fl.close(data) |
| 614 |
|
| 615 |
local lookups = {} |
| 616 |
|
| 617 |
local name_touni = { } |
| 618 |
local max_char = 0 |
| 619 |
for chr, gly in pairs(fontfile.map.map) do |
| 620 |
max_char = chr > max_char and chr or max_char |
| 621 |
-- Some glyphs have the same name in some fonts, |
| 622 |
-- e.g. the several hyphens. |
| 623 |
local name = fontfile.glyphs[gly].name |
| 624 |
while name_touni[name] do |
| 625 |
name = name .. "_" |
| 626 |
end |
| 627 |
name_touni[name] = chr |
| 628 |
end |
| 629 |
|
| 630 |
if fontfile.gsub then |
| 631 |
tab.insert(fontfile.gsub, |
| 632 |
{ type = "gsub_single", |
| 633 |
name = "tex_trep", |
| 634 |
subtables = { {name = "tex_trep"} }, |
| 635 |
features = { { tag = "trep"} } }) |
| 636 |
tab.insert(fontfile.gsub, |
| 637 |
{ type = "gsub_ligature", |
| 638 |
name = "tex_tlig", |
| 639 |
subtables = { {name = "tex_tlig"} }, |
| 640 |
features = { { tag = "tlig"} } }) |
| 641 |
for _, tb in ipairs(fontfile.gsub) do |
| 642 |
for __, ttb in ipairs(tb.subtables) do |
| 643 |
if tb.type == "gsub_contextchain" then |
| 644 |
lookups[tb.name] = lookups[tb.name] or { type = tb.type } |
| 645 |
tab.insert(lookups[tb.name], ttb.name) |
| 646 |
end |
| 647 |
lookups[ttb.name] = { type = tb.type } |
| 648 |
lookups["-" .. ttb.name] = { type = tb.type } |
| 649 |
end |
| 650 |
end |
| 651 |
end |
| 652 |
|
| 653 |
if fontfile.gpos then |
| 654 |
for _, tb in ipairs(fontfile.gpos) do |
| 655 |
for __, ttb in ipairs(tb.subtables) do |
| 656 |
lookups[ttb.name] = { type = tb.type } |
| 657 |
lookups["-" .. ttb.name] = { type = tb.type } |
| 658 |
end |
| 659 |
end |
| 660 |
end |
| 661 |
|
| 662 |
if fontfile.kerns then |
| 663 |
for _, class in ipairs(fontfile.kerns) do |
| 664 |
local max = 0 |
| 665 |
for a in pairs (class.seconds) do |
| 666 |
max = max < a and a or max |
| 667 |
end |
| 668 |
if type(class.lookup) == "string" then |
| 669 |
lookups[class.lookup] = { type = "gpos_pair", firsts = class.firsts, seconds = class.seconds, offsets = class.offsets, max = max} |
| 670 |
else |
| 671 |
for _, lk in ipairs(class.lookup) do |
| 672 |
lookups[lk] = { type = "gpos_pair", firsts = class.firsts, seconds = class.seconds, offsets = class.offsets, max = max} |
| 673 |
end |
| 674 |
end |
| 675 |
end |
| 676 |
end |
| 677 |
|
| 678 |
add_sub(fontfile, 96, 8216) -- ` to quoteleft |
| 679 |
add_sub(fontfile, 39, 8217) -- ' to apostrophe (quoteright) |
| 680 |
|
| 681 |
add_lig(fontfile, 8220, 8216, 8216) -- quoteleft + quoteleft to quotedblleft |
| 682 |
add_lig(fontfile, 8221, 8217, 8217) -- quoteright + quoteright to quotedblright |
| 683 |
add_lig(fontfile, 8211, 45, 45) -- -- to endash |
| 684 |
add_lig(fontfile, 8212, 8211, 45) -- --- (i.e. endash + -) to emdash |
| 685 |
add_lig(fontfile, 161, 63, 96) -- ?` to inverted question mark |
| 686 |
add_lig(fontfile, 161, 63, 8216) -- The same, with `turned to quoteleft. |
| 687 |
add_lig(fontfile, 191, 33, 96) -- !` to inverted exclamation mark |
| 688 |
add_lig(fontfile, 191, 33, 8216) -- Idem. |
| 689 |
|
| 690 |
local characters, phantom_ligatures = {}, {} |
| 691 |
for chr, gly in pairs(fontfile.map.map) do |
| 692 |
local glyph, char = fontfile.glyphs[gly], {} |
| 693 |
|
| 694 |
char.index = gly |
| 695 |
char.name = glyph.name |
| 696 |
char.width = glyph.width |
| 697 |
if glyph.boundingbox then |
| 698 |
char.depth = -glyph.boundingbox[2] |
| 699 |
char.height = glyph.boundingbox[4] |
| 700 |
end |
| 701 |
if glyph.italic_correction then |
| 702 |
char.italic = glyph.italic_correction |
| 703 |
elseif glyph.width and glyph.boundingbox then |
| 704 |
char.italic = glyph.boundingbox[3] - glyph.width |
| 705 |
end |
| 706 |
tab.subtable(characters, chr) |
| 707 |
characters[chr] = char |
| 708 |
|
| 709 |
if glyph.lookups then |
| 710 |
for lk, tb in pairs(glyph.lookups) do |
| 711 |
local _lk = "-" .. lk |
| 712 |
if lookups[lk] and lookups[_lk] then |
| 713 |
for _, l in ipairs(tb) do |
| 714 |
if l.type == "substitution" then |
| 715 |
|
| 716 |
tab.subtable(lookups[lk], "pairs") |
| 717 |
lookups[lk].pairs[glyph.name] = l.specification.variant |
| 718 |
|
| 719 |
tab.subtable(lookups[_lk], "pairs") |
| 720 |
lookups[_lk].pairs[l.specification.variant] = glyph.name |
| 721 |
|
| 722 |
elseif l.type == "ligature" then |
| 723 |
|
| 724 |
local comp, lig = str.explode(l.specification.components), l.specification.char |
| 725 |
local lig = "" |
| 726 |
for _, c in ipairs(comp) do |
| 727 |
lig = lig .. c |
| 728 |
end |
| 729 |
|
| 730 |
tab.subtable(lookups, lk) |
| 731 |
tab.subtable(lookups[lk], "ligs", {all_ligs = {}}) |
| 732 |
ligature(comp, lookups[lk].ligs, phantom_ligatures) |
| 733 |
name_touni[lig .. ".lig"] = chr |
| 734 |
|
| 735 |
end |
| 736 |
end |
| 737 |
end |
| 738 |
end |
| 739 |
end |
| 740 |
|
| 741 |
if glyph.kerns then |
| 742 |
for _, kern in pairs(glyph.kerns) do |
| 743 |
local lks = type(kern.lookup) == "table" and kern.lookup or {kern.lookup} |
| 744 |
for _, lk in ipairs(lks) do |
| 745 |
tab.subtable(lookups[lk], "kerns") |
| 746 |
tab.subtable(lookups[lk].kerns, glyph.name) |
| 747 |
lookups[lk].kerns[glyph.name][kern.char] = kern.off |
| 748 |
end |
| 749 |
end |
| 750 |
end |
| 751 |
|
| 752 |
end |
| 753 |
|
| 754 |
for lig in pairs(phantom_ligatures) do |
| 755 |
if not name_touni[lig] then |
| 756 |
max_char = max_char + 1 |
| 757 |
name_touni[lig] = max_char |
| 758 |
characters[max_char] = {name = lig} |
| 759 |
end |
| 760 |
end |
| 761 |
|
| 762 |
local lookup_table = {} |
| 763 |
get_lookups(fontfile.gsub, lookup_table) |
| 764 |
get_lookups(fontfile.gpos, lookup_table) |
| 765 |
get_lookups(fontfile.lookups, lookup_table, true) |
| 766 |
|
| 767 |
if fontfile.lookups then |
| 768 |
for name, lk in pairs(fontfile.lookups) do |
| 769 |
local tb, format = {}, lk.format |
| 770 |
for _, rule in ipairs(lk.rules) do |
| 771 |
local ttb = { lookups = rule.lookups } |
| 772 |
for pos, seq in pairs(rule[format]) do |
| 773 |
ttb[pos] = {} |
| 774 |
for _, glyfs in ipairs(seq) do |
| 775 |
glyfs = str.explode(glyfs) |
| 776 |
glyfs = tab.tohash(glyfs) |
| 777 |
tab.insert(ttb[pos], glyfs) |
| 778 |
end |
| 779 |
end |
| 780 |
tab.insert(tb, ttb) |
| 781 |
end |
| 782 |
lookups[name] = tb |
| 783 |
end |
| 784 |
end |
| 785 |
|
| 786 |
local loaded_font = { |
| 787 |
|
| 788 |
direction = 0, |
| 789 |
filename = filename, |
| 790 |
format = extension, |
| 791 |
fullname = fontfile.names[1].names.fullname, |
| 792 |
name = fontfile.fontname, |
| 793 |
psname = fontfile.fontname, |
| 794 |
type = "real", |
| 795 |
units_per_em = fontfile.units_per_em, |
| 796 |
|
| 797 |
auto_expand = true, |
| 798 |
|
| 799 |
cidinfo = fontfile.cidinfo, |
| 800 |
|
| 801 |
-- Used only to adjust absoluteslant. |
| 802 |
italicangle = -fontfile.italicangle, |
| 803 |
name_to_unicode = name_touni, |
| 804 |
max_char = max_char, |
| 805 |
lookups = lookups, |
| 806 |
lookup_table = lookup_table, |
| 807 |
characters = characters |
| 808 |
} |
| 809 |
|
| 810 |
if write then |
| 811 |
local f = io.open(path, "w") |
| 812 |
f:write("return {") |
| 813 |
tab.write(loaded_font, f, "\n") |
| 814 |
f:write("}") |
| 815 |
f:close() |
| 816 |
end |
| 817 |
|
| 818 |
return loaded_font |
| 819 |
|
| 820 |
end |
| 821 |
|
| 822 |
-- GETTING A FONT |
| 823 |
|
| 824 |
-- Finds a font file, and returns the original |
| 825 |
-- file and the Lua version. |
| 826 |
local name_luafile, get_features |
| 827 |
local function is_font (name, mods, size) |
| 828 |
|
| 829 |
local lib, library_filename |
| 830 |
for l, t in ipairs(library) do |
| 831 |
if t[name] then |
| 832 |
library_filename = t[name] |
| 833 |
lib = t |
| 834 |
break |
| 835 |
end |
| 836 |
end |
| 837 |
if not library_filename then |
| 838 |
library_filename = library.default[name] |
| 839 |
lib = library.default |
| 840 |
end |
| 841 |
|
| 842 |
if library_filename then |
| 843 |
if type(library_filename) == "string" then |
| 844 |
library_filename = lib[library_filename] |
| 845 |
end |
| 846 |
tab.sort(mods) |
| 847 |
local T = library_filename |
| 848 |
for _, t in ipairs(mods) do |
| 849 |
local found |
| 850 |
for tag in pairs(T) do |
| 851 |
if str.match(tag, "^" .. t) then |
| 852 |
T = T[tag] |
| 853 |
found = true |
| 854 |
break |
| 855 |
end |
| 856 |
end |
| 857 |
if not found then |
| 858 |
T = nil |
| 859 |
break |
| 860 |
end |
| 861 |
end |
| 862 |
|
| 863 |
if T and T.__files then |
| 864 |
local file, feats |
| 865 |
local diff = 10000 |
| 866 |
for s, f in pairs(T.__files) do |
| 867 |
if num.abs(s - size) < diff then |
| 868 |
diff = num.abs(s - size) |
| 869 |
file, feats = f[1], f[2] |
| 870 |
end |
| 871 |
end |
| 872 |
file, _file = lfs.find_file(file, lfs.kpse(file)), file |
| 873 |
if file then |
| 874 |
file = str.gsub(file, "\\", "/") |
| 875 |
else |
| 876 |
return 1, _file |
| 877 |
end |
| 878 |
local features = {} |
| 879 |
if feats then |
| 880 |
get_features(feats, features) |
| 881 |
end |
| 882 |
local lua = name_luafile(file, features.font) |
| 883 |
lua = lfs.isfile(lua) and lua |
| 884 |
return file, lua, feats |
| 885 |
end |
| 886 |
end |
| 887 |
end |
| 888 |
|
| 889 |
-- Returns the full path to the Lua version of the font. |
| 890 |
-- "sub" is a font in ttc. |
| 891 |
function name_luafile (file, sub) |
| 892 |
local lua |
| 893 |
sub = sub and "_" .. sub or "" |
| 894 |
sub = str.gsub(sub, " ", "_") |
| 895 |
if str.match(file, "/") then |
| 896 |
lua = str.match(file, ".*/(.-)%....$") |
| 897 |
else |
| 898 |
lua = str.match(file, "(.-)%....$") |
| 899 |
end |
| 900 |
lua = lua .. sub |
| 901 |
return foundry_path .. "/" .. lua .. ".lua" |
| 902 |
end |
| 903 |
|
| 904 |
local function apply_size (font, size, letterspacing, parameters) |
| 905 |
if (size < 0) then size = (- 655.36) * size end |
| 906 |
local to_size = size / font.units_per_em |
| 907 |
font.size = size |
| 908 |
font.designsize = size |
| 909 |
font.to_size = to_size |
| 910 |
local italic = font.italicangle or 0 |
| 911 |
local space, stretch, shrink, extra |
| 912 |
if parameters == "mono" then -- Creates a monospaced font with space equal to the |
| 913 |
-- width of an "m" and no stretch or shrink. |
| 914 |
space = font.characters[109].width * to_size |
| 915 |
stretch, shrink, extra = 0, 0, 0 |
| 916 |
else |
| 917 |
parameters = parameters and str.explode(parameters) or {} |
| 918 |
space = (parameters[1] or 0.25) * size |
| 919 |
stretch = (parameters[2] or 0.166666) * size |
| 920 |
shrink = (parameters[3] or 0.111111) * size |
| 921 |
extra = (parameters[4] or 0.111111) * size |
| 922 |
end |
| 923 |
font.parameters = { |
| 924 |
slant = size * num.floor(num.tan(italic * num.pi/180)), -- \fontdimen 1 |
| 925 |
space = space, -- \fontdimen 2 |
| 926 |
space_stretch = stretch, -- \fontdimen 3 |
| 927 |
space_shrink = shrink, -- \fontdimen 4 |
| 928 |
x_height = size * 0.4, -- \fontdimen 5 |
| 929 |
quad = size, -- \fontdimen 6 |
| 930 |
extra_space = extra -- \fontdimen 7 |
| 931 |
} |
| 932 |
|
| 933 |
letterspacing = letterspacing or 1 |
| 934 |
for c, t in pairs(font.characters) do |
| 935 |
if t.width then |
| 936 |
t.width, t.height, t.depth = t.width * to_size * letterspacing, t.height * to_size, t.depth * to_size |
| 937 |
t.expansion_factor = 1000 |
| 938 |
if t.italic then |
| 939 |
t.italic = t.italic * to_size |
| 940 |
end |
| 941 |
end |
| 942 |
end |
| 943 |
|
| 944 |
return font |
| 945 |
end |
| 946 |
|
| 947 |
|
| 948 |
local get_mods = lp.Ct((lp.space * lp.S"/" * lp.C(lp.alnum^1))^0) |
| 949 |
local get_feats = lp.Ct((lp.C((1 - lp.S",;")^1) * (lp.S",;" + -1))^1) |
| 950 |
function get_features (features, tb) |
| 951 |
features = lp.match(get_feats, features) or {} |
| 952 |
for _, f in ipairs(features) do |
| 953 |
if str.match(f, "=") then |
| 954 |
local key, val = str.match(f, "%s*(.-)%s*=%s*(.*)") |
| 955 |
val = str.trim(val) |
| 956 |
if val == "false" then |
| 957 |
tb[key] = nil |
| 958 |
else |
| 959 |
tb[key] = val |
| 960 |
end |
| 961 |
else |
| 962 |
f = str.trim(f) |
| 963 |
neg, f = str.extract(f, "^%-") |
| 964 |
if neg then |
| 965 |
tb[f] = nil |
| 966 |
else |
| 967 |
_, f = str.extract(f, "^%+") |
| 968 |
tb[f] = true |
| 969 |
end |
| 970 |
end |
| 971 |
end |
| 972 |
end |
| 973 |
|
| 974 |
local lookup_functions = {} |
| 975 |
|
| 976 |
function lookup_functions.gsub_single (tb, f) |
| 977 |
local name_touni = f.name_to_unicode |
| 978 |
for a, b in pairs(tb.pairs) do |
| 979 |
local _a, _b = name_touni[a], name_touni[b] |
| 980 |
f.max_char = f.max_char + 1 |
| 981 |
f.characters[f.max_char] = f.characters[_a] |
| 982 |
f.characters[_a] = f.characters[_b] |
| 983 |
name_touni[a], name_touni[b] = f.max_char, _a |
| 984 |
end |
| 985 |
return f |
| 986 |
end |
| 987 |
|
| 988 |
function lookup_functions.gsub_ligature (tb, f) |
| 989 |
local name_touni = f.name_to_unicode |
| 990 |
tb.ligs.all_ligs = nil |
| 991 |
for a, tb in pairs(tb.ligs) do |
| 992 |
a = name_touni[a] |
| 993 |
tab.subtable(f.characters[a], "ligatures") |
| 994 |
for b, ttb in pairs(tb) do |
| 995 |
b, c = name_touni[b], name_touni[ttb.char] |
| 996 |
f.characters[a].ligatures[b] = {char = c, type = ttb.type} |
| 997 |
end |
| 998 |
end |
| 999 |
return f |
| 1000 |
end |
| 1001 |
|
| 1002 |
local function kern_pairs (tb, firsts, seconds, offset) |
| 1003 |
for _, c1 in ipairs(str.explode(firsts)) do |
| 1004 |
for __, c2 in ipairs(str.explode(seconds)) do |
| 1005 |
tab.subtable(tb, c1) |
| 1006 |
tb[c1][c2] = offset |
| 1007 |
end |
| 1008 |
end |
| 1009 |
end |
| 1010 |
|
| 1011 |
local function kern_classes (firsts, seconds, offsets, max) |
| 1012 |
local kerns = {} |
| 1013 |
for f, F in pairs (firsts) do |
| 1014 |
for s, S in pairs (seconds) do |
| 1015 |
local off = offsets[(f-1) * max + s] |
| 1016 |
if off then |
| 1017 |
kern_pairs (kerns, F, S, off) |
| 1018 |
end |
| 1019 |
end |
| 1020 |
end |
| 1021 |
return kerns |
| 1022 |
end |
| 1023 |
|
| 1024 |
local function apply_kerns (f, kerns, to_size) |
| 1025 |
local name_touni = f.name_to_unicode |
| 1026 |
to_size = to_size or 1 |
| 1027 |
for c1, ttb in pairs(kerns) do |
| 1028 |
for c2, off in pairs(ttb) do |
| 1029 |
tab.subtable(f.characters[name_touni[c1]], "kerns") |
| 1030 |
f.characters[name_touni[c1]].kerns[name_touni[c2]] = off * to_size |
| 1031 |
end |
| 1032 |
end |
| 1033 |
end |
| 1034 |
|
| 1035 |
function lookup_functions.gpos_pair (tb, f) |
| 1036 |
-- These are the big kern classes. |
| 1037 |
if tb.offsets then |
| 1038 |
local name_touni = f.name_to_unicode |
| 1039 |
for n, off in pairs(tb.offsets) do |
| 1040 |
tb.offsets[n] = off * f.to_size |
| 1041 |
end |
| 1042 |
local kerns = kern_classes(tb.firsts, tb.seconds, tb.offsets, tb.max) |
| 1043 |
apply_kerns(f, kerns) |
| 1044 |
end |
| 1045 |
-- These are the ones retrieved from individual glyphs. |
| 1046 |
if tb.kerns then |
| 1047 |
apply_kerns(f, tb.kerns, f.to_size) |
| 1048 |
end |
| 1049 |
return f |
| 1050 |
end |
| 1051 |
|
| 1052 |
function lookup_functions.gsub_contextchain (tb, f) |
| 1053 |
local name_touni = f.name_to_unicode |
| 1054 |
local T = f.contextchain or {} |
| 1055 |
for _, llk in ipairs(tb) do |
| 1056 |
if fontfile.lookups then |
| 1057 |
local sub, Sub = fontfile.lookups[llk].rules[1].lookups |
| 1058 |
local chain = fontfile.lookups[llk].rules[1].coverage |
| 1059 |
local cur, current = str.explode(chain.current[1]), {} |
| 1060 |
local aft, after = chain.after and str.explode(chain.after[1]) or {}, {} |
| 1061 |
for _, c in ipairs(cur) do |
| 1062 |
c = name_touni[c] |
| 1063 |
if sub then |
| 1064 |
Sub = {} |
| 1065 |
for n, x in ipairs(sub) do |
| 1066 |
Sub[n] = name_touni[fontfile.glyphs[f.characters[c].index].lookups[x .. "_s"][1].specification.variant] |
| 1067 |
end |
| 1068 |
end |
| 1069 |
local t = { lookup = Sub} |
| 1070 |
tab.subtable(T, C) |
| 1071 |
for __, a in ipairs(aft) do |
| 1072 |
tab.subtable(t, "after") |
| 1073 |
t.after[name_touni[a]] = true |
| 1074 |
end |
| 1075 |
tab.insert(T[c], t) |
| 1076 |
end |
| 1077 |
end |
| 1078 |
end |
| 1079 |
f.contextchain = T |
| 1080 |
return f |
| 1081 |
end |
| 1082 |
|
| 1083 |
local function _isactive (tb, ft, sc, lg) |
| 1084 |
for t in pairs(tb.tags) do |
| 1085 |
if ft[t] then |
| 1086 |
if t[sc] then |
| 1087 |
for _, lang in pairs(tb[sc]) do |
| 1088 |
if lang == lg then |
| 1089 |
return true |
| 1090 |
end |
| 1091 |
end |
| 1092 |
else |
| 1093 |
return true |
| 1094 |
end |
| 1095 |
end |
| 1096 |
end |
| 1097 |
end |
| 1098 |
|
| 1099 |
|
| 1100 |
local lookup_types = { |
| 1101 |
"gsub_single", |
| 1102 |
"gsub_ligature", |
| 1103 |
"gpos_pair" |
| 1104 |
} |
| 1105 |
|
| 1106 |
local function activate_lookups (font, features, script, lang) |
| 1107 |
for _, type in ipairs(lookup_types) do |
| 1108 |
if font.lookup_table and font.lookup_table[type] then |
| 1109 |
for l, tb in pairs(font.lookup_table[type]) do |
| 1110 |
if _isactive(tb, features, script, lang) then |
| 1111 |
for _, lk in ipairs(tb) do |
| 1112 |
local lt = font.lookups[lk] |
| 1113 |
if lt then |
| 1114 |
font = lookup_functions[type](lt, font) |
| 1115 |
end |
| 1116 |
end |
| 1117 |
end |
| 1118 |
end |
| 1119 |
end |
| 1120 |
end |
| 1121 |
return font |
| 1122 |
end |
| 1123 |
|
| 1124 |
local function load_font (name, size, id, done) |
| 1125 |
local loaded_font = lfs.find_file(name, "tfm") |
| 1126 |
if loaded_font then |
| 1127 |
loaded_font = fl.read_tfm(loaded_font, size) |
| 1128 |
else |
| 1129 |
local original = str.trim(str.match(name, "[^:]*")) |
| 1130 |
local family, mods, feats |
| 1131 |
family, name = str.extract(name, "([^/:]*)") |
| 1132 |
family = str.trim(family) |
| 1133 |
mods, name = str.extract(name, "[^:]*") |
| 1134 |
mods = lp.match(get_mods, mods) or {} |
| 1135 |
feats = str.extract(name, ":(.*)") or "" |
| 1136 |
local features = tab.copy(settings.features) |
| 1137 |
get_features(feats, features) |
| 1138 |
|
| 1139 |
local at_size |
| 1140 |
if features.size then |
| 1141 |
at_size = features.size |
| 1142 |
else |
| 1143 |
at_size = size |
| 1144 |
at_size = at_size > 0 and at_size or at_size * - 655.36 |
| 1145 |
at_size = at_size / 65536 |
| 1146 |
end |
| 1147 |
local source, lua, add_feats = is_font(family, mods, at_size) |
| 1148 |
if add_feats then get_features(add_feats, features) end |
| 1149 |
|
| 1150 |
if type(source) == "string" then |
| 1151 |
local cache = features.cache or "yes" |
| 1152 |
if lua then |
| 1153 |
if cache == "no" or cache == "rewrite" then |
| 1154 |
loaded_font = create_font(source, lfs.type(source), lua, features.font, cache == "rewrite") |
| 1155 |
else |
| 1156 |
loaded_font = dofile(lua) |
| 1157 |
end |
| 1158 |
else |
| 1159 |
lua = name_luafile(source, features.font) |
| 1160 |
loaded_font = create_font(source, lfs.type(source), lua, features.font, cache ~= "no") |
| 1161 |
end |
| 1162 |
else |
| 1163 |
if not done then |
| 1164 |
if type(source) == "number" then |
| 1165 |
wri.error("The library says `%s' matches `%s', but I can't find that file anywhere. Clean up your library!", original, lua) |
| 1166 |
else |
| 1167 |
recheck_fonts() |
| 1168 |
return load_font(original, size, id, true) |
| 1169 |
end |
| 1170 |
else |
| 1171 |
wri.error("I can't find `%s'. I return a default font to avoid further errors.", original) |
| 1172 |
end |
| 1173 |
end |
| 1174 |
|
| 1175 |
if loaded_font then |
| 1176 |
|
| 1177 |
local expansion = features.expansion and str.explode(features.expansion) or {} |
| 1178 |
loaded_font.stretch = expansion[1] or 0 |
| 1179 |
loaded_font.shrink = expansion[2] or 0 |
| 1180 |
loaded_font.step = expansion[3] or 0 |
| 1181 |
|
| 1182 |
local extend = features.extend or 1 |
| 1183 |
loaded_font.extend = extend * 1000 |
| 1184 |
local slant |
| 1185 |
if features.absoluteslant then |
| 1186 |
local italic = loaded_font.italicangle or 0 |
| 1187 |
slant = features.absoluteslant - italic |
| 1188 |
else |
| 1189 |
slant = features.slant or 0 |
| 1190 |
end |
| 1191 |
loaded_font.slant = num.tan(num.rad(slant)) * 1000 |
| 1192 |
|
| 1193 |
loaded_font = apply_size(loaded_font, size, features.letterspacing, features.space) |
| 1194 |
loaded_font = activate_lookups(loaded_font, features, features.script, features.lang) |
| 1195 |
|
| 1196 |
loaded_font.name = loaded_font.name .. id |
| 1197 |
loaded_font.fullname = loaded_font.fullname .. id |
| 1198 |
local embedding = features.embedding or "subset" |
| 1199 |
if embedding ~= "no" and embedding ~= "subset" and embedding ~= "full" then |
| 1200 |
wri.error("Invalid value `%s' for the `embedding' feature. Value should be `no', `subset' or `full'.", embedding) |
| 1201 |
embedding = "subset" |
| 1202 |
end |
| 1203 |
loaded_font.embedding = embedding |
| 1204 |
else |
| 1205 |
loaded_font = fl.read_tfm(lfs.find_file("cmr10", "tfm"), size) |
| 1206 |
end |
| 1207 |
end |
| 1208 |
return loaded_font |
| 1209 |
end |
| 1210 |
|
| 1211 |
callback.register("define_font", load_font) |