| 1 |
#!/usr/bin/env texlua |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
g |
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
local ProvidesLuaModule = { |
| 11 |
name = "luaotfload-tool", |
| 12 |
version = "3.29", |
| 13 |
date = "2024-12-03", |
| 14 |
description = "luaotfload-tool / database functionality", |
| 15 |
license = "GPL v2.0" |
| 16 |
} |
| 17 |
|
| 18 |
function luaotfload_module(module) end |
| 19 |
|
| 20 |
|
| 21 |
luaotfload = luaotfload or { } |
| 22 |
local version = ProvidesLuaModule.version |
| 23 |
luaotfload.version = ProvidesLuaModule.version |
| 24 |
luaotfload.self = "luaotfload-tool" |
| 25 |
luaotfload.fontloader = _G |
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
kpse.set_program_name "luatex" |
| 47 |
|
| 48 |
local iowrite = io.write |
| 49 |
local kpsefind_file = kpse.find_file |
| 50 |
local mathfloor = math.floor |
| 51 |
local next = next |
| 52 |
local osdate = os.date |
| 53 |
local ostype = os.type |
| 54 |
local stringexplode = string.explode |
| 55 |
local stringformat = string.format |
| 56 |
local stringlower = string.lower |
| 57 |
local stringrep = string.rep |
| 58 |
local tableconcat = table.concat |
| 59 |
local texiowrite_nl = texio.write_nl |
| 60 |
local texiowrite = texio.write |
| 61 |
local tonumber = tonumber |
| 62 |
local type = type |
| 63 |
|
| 64 |
do |
| 65 |
local runtime = _G.jit and { "jit" , jit.version } |
| 66 |
or { "stock", _VERSION } |
| 67 |
local minimum = {110, 0} |
| 68 |
local revn = tonumber (status.luatex_revision) or 0 |
| 69 |
|
| 70 |
if status.luatex_version < minimum[1] |
| 71 |
or status.luatex_version == minimum[1] and tonumber(status.luatex_revision) < minimum[2] then |
| 72 |
texio.write_nl ("term and log", |
| 73 |
string.format ("\tFATAL ERROR\n\z |
| 74 |
\tLuaotfload requires a Luatex version >= %d.%d.%d.\n\z |
| 75 |
\tPlease update your TeX distribution!\n\n", |
| 76 |
math.floor(minimum[1] / 100), minimum[1] % 100, minimum[2])) |
| 77 |
error "version check failed" |
| 78 |
end |
| 79 |
luaotfload.runtime = runtime |
| 80 |
end |
| 81 |
|
| 82 |
local C, Ct, P, S = lpeg.C, lpeg.Ct, lpeg.P, lpeg.S |
| 83 |
local lpegmatch = lpeg.match |
| 84 |
|
| 85 |
string.quoted = string.quoted or function (str) |
| 86 |
return string.format("%q",str) |
| 87 |
end |
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
config = config or { } |
| 98 |
local config = config |
| 99 |
config.luaotfload = config.luaotfload or { } |
| 100 |
|
| 101 |
config.lualibs = config.lualibs or { } |
| 102 |
config.lualibs.verbose = false |
| 103 |
config.lualibs.prefer_merged = true |
| 104 |
config.lualibs.load_extended = true |
| 105 |
|
| 106 |
require "lualibs" |
| 107 |
|
| 108 |
local iosavedata = io.savedata |
| 109 |
local lfsisdir = lfs.isdir |
| 110 |
local lfsisfile = lfs.isfile |
| 111 |
local stringsplit = string.split |
| 112 |
local tablekeys = table.keys |
| 113 |
local tableserialize = table.serialize |
| 114 |
local tablesortedkeys = table.sortedkeys |
| 115 |
local tabletohash = table.tohash |
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
local function dummy_function ( ) end |
| 131 |
local backup = { |
| 132 |
write = texio.write, |
| 133 |
write_nl = texio.write_nl, |
| 134 |
utilities = utilities, |
| 135 |
} |
| 136 |
|
| 137 |
texio.write, texio.write_nl = dummy_function, dummy_function |
| 138 |
require "fontloader-basics-gen.lua" |
| 139 |
texio.write, texio.write_nl = backup.write, backup.write_nl |
| 140 |
utilities = backup.utilities |
| 141 |
|
| 142 |
pdf = pdf or { } |
| 143 |
|
| 144 |
require "fontloader-data-con" |
| 145 |
require "fontloader-font-ini" |
| 146 |
require "fontloader-font-con" |
| 147 |
require "fontloader-fonts-enc" |
| 148 |
require "fontloader-font-cid" |
| 149 |
require "fontloader-font-map" |
| 150 |
require "fontloader-font-oti" |
| 151 |
require "fontloader-font-otr" |
| 152 |
require "fontloader-font-ott" |
| 153 |
require "fontloader-font-cff" |
| 154 |
require "fontloader-font-ttf" |
| 155 |
require "fontloader-font-dsp" |
| 156 |
require "fontloader-font-oup" |
| 157 |
require "fontloader-font-onr" |
| 158 |
require "fontloader-font-def" |
| 159 |
|
| 160 |
fonts = fonts or { } |
| 161 |
local fontsnames = fonts.names or { } |
| 162 |
fonts.names = fontsnames |
| 163 |
|
| 164 |
local require_init = { } |
| 165 |
|
| 166 |
local function loadmodule (name) |
| 167 |
local v = require ("luaotfload-" .. name) |
| 168 |
if v then |
| 169 |
local mod = { } |
| 170 |
local tv = type (v) |
| 171 |
if tv == "table" then |
| 172 |
mod.name = name |
| 173 |
mod.init = v.init |
| 174 |
require_init [#require_init + 1] = mod |
| 175 |
elseif tv == "function" then |
| 176 |
mod.name = name |
| 177 |
mod.init = v |
| 178 |
require_init [#require_init + 1] = mod |
| 179 |
end |
| 180 |
end |
| 181 |
end |
| 182 |
|
| 183 |
require "alt_getopt" |
| 184 |
|
| 185 |
loadmodule "log" |
| 186 |
loadmodule "parsers" |
| 187 |
loadmodule "configuration" |
| 188 |
loadmodule "database" |
| 189 |
loadmodule "resolvers" |
| 190 |
|
| 191 |
local logreport |
| 192 |
|
| 193 |
local function init_modules () |
| 194 |
t. |
| 195 |
local todo = #require_init |
| 196 |
local ret = true |
| 197 |
for i = 1, todo do |
| 198 |
local mod = require_init[i] |
| 199 |
local name = mod.name |
| 200 |
local init = mod.init |
| 201 |
if type (init) ~= "function" then |
| 202 |
error ("luaotfload broken; module " |
| 203 |
.. name .. " missing initializers!") |
| 204 |
end |
| 205 |
local v = mod.init () |
| 206 |
if v == true then |
| 207 |
|
| 208 |
elseif type (v) == "table" then |
| 209 |
luaotfload[name] = v |
| 210 |
else |
| 211 |
error ("luaotfload broken; initialization of module " |
| 212 |
.. name .. " returned " .. tostring (v) .. ".") |
| 213 |
return false |
| 214 |
end |
| 215 |
end |
| 216 |
logreport = luaotfload.log.report |
| 217 |
return ret |
| 218 |
end |
| 219 |
|
| 220 |
|
| 221 |
local help_messages = { |
| 222 |
["luaotfload-tool"] = [[ |
| 223 |
|
| 224 |
Usage: %s [OPTIONS...] |
| 225 |
|
| 226 |
Luaotfload font management and diagnostic utility. |
| 227 |
This program is part of the Luaotfload package. |
| 228 |
|
| 229 |
Valid options are: |
| 230 |
|
| 231 |
------------------------------------------------------------------------------- |
| 232 |
VERBOSITY AND DIAGNOSTICS |
| 233 |
|
| 234 |
-q --quiet don't output anything |
| 235 |
-v --verbose=LEVEL be more verbose (print the searched directories) |
| 236 |
-v, -vv .. -vvvvvvvvv set loglevel in unary |
| 237 |
--log=stdout redirect log output to stdout |
| 238 |
|
| 239 |
-V --version print version and exit |
| 240 |
-h --help print this message |
| 241 |
--diagnose=CHECK run a self test procedure; one of "files", |
| 242 |
"environment", "index", "permissions", or |
| 243 |
"repository" |
| 244 |
|
| 245 |
------------------------------------------------------------------------------- |
| 246 |
DATABASE |
| 247 |
|
| 248 |
-u --update update the database |
| 249 |
-n --no-reload suppress db update |
| 250 |
--no-strip keep redundant information in db |
| 251 |
-f --force force re-indexing all fonts |
| 252 |
-L --local scan font files in $PWD |
| 253 |
-c --no-compress do not gzip index file (text version only) |
| 254 |
-l --flush-lookups empty lookup cache of font requests |
| 255 |
-D --dry-run skip loading of fonts, just scan |
| 256 |
--formats=[+|-]EXTENSIONS set, add, or subtract file formats |
| 257 |
-p --prefer-texmf prefer fonts in the TEXMF over system fonts |
| 258 |
--max-fonts=N process at most N font files |
| 259 |
|
| 260 |
--find="font name" query the database for a font name |
| 261 |
-F --fuzzy look for approximate matches if --find fails |
| 262 |
--limit=n limit display of fuzzy matches to <n> |
| 263 |
(default: n = 1) |
| 264 |
|
| 265 |
-i --info display basic font metadata |
| 266 |
-I --inspect display detailed font metadata |
| 267 |
|
| 268 |
--list=<criterion> output list of entries by field <criterion> |
| 269 |
--list=<criterion>:<value> restrict to entries with <criterion>=<value> |
| 270 |
--fields=<f1>,<f2>,…,<fn> which fields <f> to print with --list |
| 271 |
-b --show-blacklist show blacklisted files |
| 272 |
|
| 273 |
--bisect=<directive> control database bisection: valid |
| 274 |
directives are "start", "stop", "run", "status", |
| 275 |
"good", "bad" |
| 276 |
|
| 277 |
The font database will be saved to |
| 278 |
%s |
| 279 |
%s |
| 280 |
|
| 281 |
------------------------------------------------------------------------------- |
| 282 |
FONT CACHE |
| 283 |
|
| 284 |
--cache=<directive> operate on font cache, where <directive> is |
| 285 |
"show", "purge", or "erase" |
| 286 |
|
| 287 |
The font cache will be written to |
| 288 |
%s |
| 289 |
|
| 290 |
]], |
| 291 |
mkluatexfontdb = [[ |
| 292 |
FATAL ERROR |
| 293 |
As of Luaotfload v2.5, legacy behavior is not supported anymore. Please |
| 294 |
update your scripts and/or habits! Kthxbye. |
| 295 |
]], |
| 296 |
short = [[ |
| 297 |
Usage: luaotfload-tool [--help] [--version] [--verbose=<lvl>] |
| 298 |
[--update] [--force] [--prefer-texmf] [--local] |
| 299 |
[--dry-run] [--formats=<extension list>] |
| 300 |
[--find=<font name>] [--fuzzy] [--info] [--inspect] |
| 301 |
[--list=<criterion>] [--fields=<field list>] |
| 302 |
[--cache=<directive>] [--flush-lookups] |
| 303 |
[--show-blacklist] [--diagnose=<procedure>] |
| 304 |
[--no-compress] [--no-strip] [--local] |
| 305 |
[--max-fonts=<n>] [--bisect=<directive>] |
| 306 |
|
| 307 |
Enter 'luaotfload-tool --help' for a larger list of options. |
| 308 |
]] |
| 309 |
} |
| 310 |
|
| 311 |
local function help_msg (version) |
| 312 |
local template = help_messages[version] |
| 313 |
local paths = config.luaotfload.paths |
| 314 |
local names_plain = paths.index_path_lua |
| 315 |
local names_gzip = names_plain .. ".gz" |
| 316 |
local names_bin = paths.index_path_luc |
| 317 |
|
| 318 |
iowrite(stringformat(template, |
| 319 |
luaotfload.self, |
| 320 |
names_gzip, |
| 321 |
names_bin, |
| 322 |
caches.getwritablepath (config.luaotfload.paths.cache_dir, ""))) |
| 323 |
end |
| 324 |
|
| 325 |
local about = [[ |
| 326 |
%s: |
| 327 |
Luaotfload font management and diagnostic utility. |
| 328 |
License: GNU GPL v2.0. |
| 329 |
Report problems to <https://github.com/latex3/luaotfload/issues> |
| 330 |
]] |
| 331 |
|
| 332 |
local function version_msg ( ) |
| 333 |
local out = function (...) texiowrite_nl (stringformat (...)) end |
| 334 |
local uname = os.uname () |
| 335 |
local meta = fonts.names.getmetadata () |
| 336 |
|
| 337 |
local runtime = luaotfload.runtime |
| 338 |
local notes = config.luaotfload.status |
| 339 |
local notes = notes and notes.notes or { } |
| 340 |
|
| 341 |
out (about, luaotfload.self) |
| 342 |
out ("%s version: %q", luaotfload.self, version) |
| 343 |
if notes.description then |
| 344 |
out ("Luaotfload: %q", notes.description) |
| 345 |
end |
| 346 |
out ("Revision: %q", notes.revision) |
| 347 |
out ("Lua interpreter: %s; version %q", runtime[1], runtime[2]) |
| 348 |
|
| 349 |
out ("Luatex version: %d.%d", math.floor(status.luatex_version / 100), status.luatex_version % 100) |
| 350 |
out ("Platform: type=%s name=%s", os.type, os.name) |
| 351 |
|
| 352 |
local uname_vars = tablesortedkeys (uname) |
| 353 |
for i = 1, #uname_vars do |
| 354 |
local var = uname_vars[i] |
| 355 |
out (" + %8s: %s", var, uname[var]) |
| 356 |
end |
| 357 |
if meta == false then |
| 358 |
out("No database metadata available.") |
| 359 |
else |
| 360 |
out ("Index: version=%q created=%q modified=%q", |
| 361 |
meta.version or "too old", |
| 362 |
meta.created or "ages ago", |
| 363 |
meta.modified or "ages ago") |
| 364 |
end |
| 365 |
out "" |
| 366 |
end |
| 367 |
|
| 368 |
|
| 369 |
|
| 370 |
|
| 371 |
local head_adornchars = { |
| 372 |
[1] = "*", [2] = "=", [3] = "~", [4] = "-", [5] = "·", |
| 373 |
} |
| 374 |
|
| 375 |
local textwidth = 80 |
| 376 |
local wd_leftcolumn = mathfloor(textwidth * .25) |
| 377 |
local key_fmt = stringformat([[%%%ds]], wd_leftcolumn) |
| 378 |
local val_fmt = [[%s]] |
| 379 |
local fieldseparator = ":" |
| 380 |
local info_fmt = key_fmt .. fieldseparator .. " " .. val_fmt |
| 381 |
|
| 382 |
local currentdepth = 0 |
| 383 |
local counterstack = { } |
| 384 |
local counterformat = "%d" |
| 385 |
|
| 386 |
local function format_counter (stack) |
| 387 |
local acc = { } |
| 388 |
for lvl=1, #stack do |
| 389 |
acc[#acc+1] = stringformat(counterformat, stack[lvl]) |
| 390 |
end |
| 391 |
return tableconcat(acc, ".") |
| 392 |
end |
| 393 |
|
| 394 |
local function print_heading (title, level) |
| 395 |
if not title then return end |
| 396 |
local structuredata |
| 397 |
if currentdepth == level then |
| 398 |
counterstack[#counterstack] = counterstack[#counterstack] + 1 |
| 399 |
elseif currentdepth < level then |
| 400 |
counterstack[#counterstack+1] = 1 |
| 401 |
else |
| 402 |
local diff = currentdepth - level |
| 403 |
while diff > 0 do |
| 404 |
counterstack[#counterstack] = nil |
| 405 |
diff = diff - 1 |
| 406 |
end |
| 407 |
counterstack[#counterstack] = counterstack[#counterstack] + 1 |
| 408 |
end |
| 409 |
currentdepth = level |
| 410 |
|
| 411 |
texiowrite_nl "" |
| 412 |
if not level or level > #head_adornchars then |
| 413 |
level = #head_adornchars |
| 414 |
end |
| 415 |
local adornchar = head_adornchars[level] |
| 416 |
|
| 417 |
local counter = format_counter(counterstack) |
| 418 |
|
| 419 |
local s = adornchar .. adornchar .. " " |
| 420 |
.. counter .. " " |
| 421 |
.. title .. " " |
| 422 |
texiowrite_nl (s .. stringrep(adornchar, textwidth-utf.len(s))) |
| 423 |
end |
| 424 |
|
| 425 |
local baseindent = " " |
| 426 |
|
| 427 |
|
| 428 |
|
| 429 |
|
| 430 |
|
| 431 |
|
| 432 |
|
| 433 |
|
| 434 |
|
| 435 |
local function show_info_table (t, depth) |
| 436 |
depth = depth or 0 |
| 437 |
local indent = stringrep (baseindent, depth) |
| 438 |
local keys = tablesortedkeys (t) |
| 439 |
for n = 1, #keys do |
| 440 |
local key = keys [n] |
| 441 |
local val = t [key] |
| 442 |
if key == "subfontindex" then |
| 443 |
val = val - 1 |
| 444 |
end |
| 445 |
if type (val) == "table" then |
| 446 |
texiowrite_nl (indent .. stringformat (info_fmt, key, "<table>")) |
| 447 |
show_info_table (val, depth + 1) |
| 448 |
else |
| 449 |
texiowrite_nl (indent .. stringformat (info_fmt, key, val)) |
| 450 |
end |
| 451 |
end |
| 452 |
end |
| 453 |
|
| 454 |
local function show_info_items (fontinfo) |
| 455 |
print_heading (fontinfo.fullname, 1) |
| 456 |
texiowrite_nl "" |
| 457 |
show_info_table (fontinfo) |
| 458 |
texiowrite_nl "" |
| 459 |
end |
| 460 |
|
| 461 |
local p_spacechar = S" \n\r\t\v" |
| 462 |
local p_wordchar = (1 - p_spacechar) |
| 463 |
local p_whitespace = p_spacechar^1 |
| 464 |
local p_word = C(p_wordchar^1) |
| 465 |
local p_words = Ct(p_word * (p_whitespace * p_word)^0) |
| 466 |
|
| 467 |
|
| 468 |
local function reflow (text, width) |
| 469 |
local words |
| 470 |
local t_text = type (text) |
| 471 |
if t_text == "string" then |
| 472 |
words = lpegmatch(p_words, text) |
| 473 |
if #words < 2 then |
| 474 |
return { text } |
| 475 |
end |
| 476 |
elseif t_text == "table" then |
| 477 |
words = text |
| 478 |
if #text < 2 then |
| 479 |
return text |
| 480 |
end |
| 481 |
end |
| 482 |
|
| 483 |
local space = " " |
| 484 |
local utflen = utf.len |
| 485 |
local reflowed = { } |
| 486 |
|
| 487 |
local first = words[1] |
| 488 |
local linelen = #first |
| 489 |
local line = { first } |
| 490 |
|
| 491 |
for i=2, #words do |
| 492 |
local word = words[i] |
| 493 |
local lword = utflen(word) |
| 494 |
linelen = linelen + lword + 1 |
| 495 |
if linelen > width then |
| 496 |
reflowed[#reflowed+1] = tableconcat(line) |
| 497 |
linelen = #word |
| 498 |
line = { word } |
| 499 |
else |
| 500 |
line[#line+1] = space |
| 501 |
line[#line+1] = word |
| 502 |
end |
| 503 |
end |
| 504 |
reflowed[#reflowed+1] = tableconcat(line) |
| 505 |
return reflowed |
| 506 |
end |
| 507 |
|
| 508 |
|
| 509 |
local function print_field (key, val) |
| 510 |
val = tostring(val) |
| 511 |
local lhs = stringformat(key_fmt, key) .. fieldseparator .. " " |
| 512 |
local wd_lhs = #lhs |
| 513 |
local lines = reflow(val, textwidth - wd_lhs) |
| 514 |
|
| 515 |
texiowrite_nl(lhs) |
| 516 |
texiowrite(lines[1]) |
| 517 |
if #lines > 1 then |
| 518 |
local indent = stringrep(" ", wd_lhs) |
| 519 |
for i=2, #lines do |
| 520 |
texiowrite_nl(indent) |
| 521 |
texiowrite (lines[i]) |
| 522 |
end |
| 523 |
end |
| 524 |
end |
| 525 |
|
| 526 |
|
| 527 |
local general_fields = { |
| 528 |
|
| 529 |
{ "fullname", "l", "font name" }, |
| 530 |
{ "version", "l", "font version" }, |
| 531 |
{ "width", "l", "width" }, |
| 532 |
{ "averagewidth", "l", "average width" }, |
| 533 |
{ "panosewidth", "l", "panose width" }, |
| 534 |
{ "weight", "l", "weight indicator" }, |
| 535 |
{ "pfmweight", "l", "numerical weight" }, |
| 536 |
{ "panoseweight", "l", "panose weight" }, |
| 537 |
{ "designsize", "l", "design size" }, |
| 538 |
{ "minsize", "l", "design size min" }, |
| 539 |
{ "maxsize", "l", "design size max" }, |
| 540 |
{ "units", "l", "units per em" }, |
| 541 |
{ "ascender", "l", "ascender height" }, |
| 542 |
{ "descender", "l", "descender height" }, |
| 543 |
{ "capheight", "l", "capital height" }, |
| 544 |
} |
| 545 |
|
| 546 |
local function display_general (fullinfo) |
| 547 |
texiowrite_nl "" |
| 548 |
print_heading("General Information", 2) |
| 549 |
texiowrite_nl "" |
| 550 |
for i=1, #general_fields do |
| 551 |
local field = general_fields[i] |
| 552 |
local key, mode, desc = unpack(field) |
| 553 |
local val |
| 554 |
if mode == "l" then |
| 555 |
val = fullinfo[key] |
| 556 |
elseif mode == "S" then |
| 557 |
local data = fullinfo[key] |
| 558 |
if type (data) == "table" then |
| 559 |
if #data > 0 then |
| 560 |
for n = 1, #data do |
| 561 |
local nth = data[n] |
| 562 |
if nth.lang == 1033 then |
| 563 |
val = nth.name |
| 564 |
goto found |
| 565 |
end |
| 566 |
end |
| 567 |
val = next (data).name |
| 568 |
else |
| 569 |
val = "" |
| 570 |
end |
| 571 |
::found:: |
| 572 |
else |
| 573 |
val = data |
| 574 |
end |
| 575 |
elseif mode == "n" then |
| 576 |
local v = fullinfo[key] |
| 577 |
if v then |
| 578 |
val = #fullinfo[key] |
| 579 |
end |
| 580 |
elseif mode == "d" then |
| 581 |
if ostype == "unix" then |
| 582 |
val = osdate("%F %T", fullinfo[key]) |
| 583 |
else |
| 584 |
so |
| 585 |
|
| 586 |
|
| 587 |
val = osdate("%Y-%m-%d %H:%M:%S", fullinfo[key]) |
| 588 |
end |
| 589 |
end |
| 590 |
if not val then |
| 591 |
val = "<none>" |
| 592 |
end |
| 593 |
print_field(desc, val) |
| 594 |
end |
| 595 |
end |
| 596 |
|
| 597 |
local function print_features (features) |
| 598 |
for tag, data in next, features do |
| 599 |
print_heading(tag, 4) |
| 600 |
for script, languages in next, data do |
| 601 |
local field = stringformat(key_fmt, script).. fieldseparator .. " " |
| 602 |
local wd_field = #field |
| 603 |
local lines = reflow(tablekeys(languages), textwidth - wd_field) |
| 604 |
local indent = stringrep(" ", wd_field) |
| 605 |
texiowrite_nl(field) |
| 606 |
texiowrite(lines[1]) |
| 607 |
if #lines > 1 then |
| 608 |
for i=1, #lines do |
| 609 |
texiowrite_nl(indent .. lines[i]) |
| 610 |
end |
| 611 |
end |
| 612 |
end |
| 613 |
end |
| 614 |
end |
| 615 |
|
| 616 |
local function display_feature_set (set) |
| 617 |
print_features(set) |
| 618 |
end |
| 619 |
|
| 620 |
local function display_features_type (id, feat) |
| 621 |
if feat and next (feat) then |
| 622 |
print_heading(id, 3) |
| 623 |
display_feature_set(feat) |
| 624 |
return true |
| 625 |
end |
| 626 |
return false |
| 627 |
end |
| 628 |
|
| 629 |
local function display_features (features) |
| 630 |
texiowrite_nl "" |
| 631 |
print_heading("Features", 2) |
| 632 |
|
| 633 |
local status = 0 |
| 634 |
if not display_features_type ("GSUB Features", features.gsub) then |
| 635 |
status = status + 1 |
| 636 |
end |
| 637 |
if not display_features_type ("GPOS Features", features.gpos) then |
| 638 |
status = status + 2 |
| 639 |
end |
| 640 |
if status == 3 then |
| 641 |
texiowrite_nl("font defines neither gsub nor gpos features") |
| 642 |
elseif status == 2 then |
| 643 |
texiowrite_nl("font defines no gpos feature") |
| 644 |
elseif status == 1 then |
| 645 |
texiowrite_nl("font defines no gsub feature") |
| 646 |
end |
| 647 |
end |
| 648 |
|
| 649 |
local function show_full_info (path, subfont) |
| 650 |
local rawinfo, warn = fonts.handlers.otf.readers.loadfont (path, subfont) |
| 651 |
if not rawinfo then |
| 652 |
texiowrite_nl(stringformat([[cannot open font %s]], path)) |
| 653 |
return |
| 654 |
end |
| 655 |
display_general(rawinfo.metadata) |
| 656 |
display_features(rawinfo.resources.features) |
| 657 |
end |
| 658 |
|
| 659 |
local function subfont_by_name (lst, askedname, n) |
| 660 |
for n = 1, #lst do |
| 661 |
local font = lst[n] |
| 662 |
if fonts.names.sanitize_fontname (font.fullname) == askedname then |
| 663 |
return font |
| 664 |
end |
| 665 |
end |
| 666 |
return false |
| 667 |
end |
| 668 |
|
| 669 |
|
| 670 |
|
| 671 |
|
| 672 |
|
| 673 |
|
| 674 |
|
| 675 |
|
| 676 |
|
| 677 |
local function show_font_info (basename, askedname, detail, subfont) |
| 678 |
local filenames = fonts.names.data().files |
| 679 |
local index = filenames.base[basename] |
| 680 |
local fullname = filenames.full[index] |
| 681 |
askedname = fonts.names.sanitize_fontname (askedname) |
| 682 |
if not fullname then |
| 683 |
fullname = resolvers.findfile(basename) |
| 684 |
end |
| 685 |
if fullname then |
| 686 |
local shortinfo = fonts.handlers.otf.readers.getinfo (fullname, { |
| 687 |
subfont = subfont, |
| 688 |
platformnames = true, |
| 689 |
rawfamilynames = true, |
| 690 |
}) |
| 691 |
local nfonts = #shortinfo |
| 692 |
if nfonts > 0 then |
| 693 |
local subfont |
| 694 |
if askedname then |
| 695 |
logreport (true, 1, "resolve", |
| 696 |
[[%s is part of the font collection %s]], |
| 697 |
askedname, basename) |
| 698 |
subfont = subfont_by_name(shortinfo, askedname) |
| 699 |
end |
| 700 |
if subfont then |
| 701 |
show_info_items(subfont) |
| 702 |
if detail == true then |
| 703 |
show_full_info(fullname, subfont) |
| 704 |
end |
| 705 |
else |
| 706 |
logreport (true, 1, "resolve", |
| 707 |
[[%s is a font collection]], basename) |
| 708 |
for subfont = 1, nfonts do |
| 709 |
logreport (true, 1, "resolve", |
| 710 |
[[Showing info for font no. %d]], |
| 711 |
subfont - 1) |
| 712 |
show_info_items(shortinfo[subfont]) |
| 713 |
if detail == true then |
| 714 |
show_full_info(fullname, subfont) |
| 715 |
end |
| 716 |
end |
| 717 |
end |
| 718 |
else |
| 719 |
show_info_items(shortinfo) |
| 720 |
if detail == true then |
| 721 |
show_full_info(fullname, subfont) |
| 722 |
end |
| 723 |
end |
| 724 |
else |
| 725 |
logreport (true, 1, "resolve", "Font %s not found", filename) |
| 726 |
end |
| 727 |
end |
| 728 |
|
| 729 |
|
| 730 |
|
| 731 |
|
| 732 |
|
| 733 |
|
| 734 |
|
| 735 |
local action_sequence = { |
| 736 |
"config" , "loglevel" , "help" , "version" , |
| 737 |
"dumpconf" , "diagnose" , "blacklist" , "cache" , |
| 738 |
"flush" , "bisect" , "generate" , "list" , |
| 739 |
"query" , "aliases" , |
| 740 |
} |
| 741 |
|
| 742 |
local action_pending = tabletohash(action_sequence, false) |
| 743 |
|
| 744 |
action_pending.config = true |
| 745 |
action_pending.loglevel = true |
| 746 |
action_pending.generate = false |
| 747 |
|
| 748 |
local actions = { } |
| 749 |
|
| 750 |
function actions.loglevel (job) |
| 751 |
local lvl = job.log_level |
| 752 |
if lvl then |
| 753 |
luaotfload.log.set_loglevel(lvl) |
| 754 |
logreport ("info", 3, "util", "Setting the log level to %d.", lvl) |
| 755 |
logreport ("log", 2, "util", "Lua=%q", _VERSION) |
| 756 |
end |
| 757 |
return true, true |
| 758 |
end |
| 759 |
|
| 760 |
function actions.config (job) |
| 761 |
local defaults = luaotfload.default_config |
| 762 |
local vars = config.actions.read (job.extra_config) |
| 763 |
config.luaotfload = config.actions.apply (defaults, vars) |
| 764 |
config.luaotfload = config.actions.apply (config.luaotfload, job.config) |
| 765 |
|
| 766 |
|
| 767 |
|
| 768 |
if not config.actions.reconfigure () then |
| 769 |
return false, false |
| 770 |
end |
| 771 |
return true, true |
| 772 |
end |
| 773 |
|
| 774 |
function actions.version (job) |
| 775 |
version_msg() |
| 776 |
return true, false |
| 777 |
end |
| 778 |
|
| 779 |
function actions.dumpconf (job) |
| 780 |
config.actions.dump () |
| 781 |
return true, false |
| 782 |
end |
| 783 |
|
| 784 |
function actions.help (job) |
| 785 |
help_msg (job.help_version or "luaotfload-tool") |
| 786 |
return true, false |
| 787 |
end |
| 788 |
|
| 789 |
function actions.blacklist (job) |
| 790 |
fonts.names.read_blacklist() |
| 791 |
local n = 0 |
| 792 |
for n, entry in next, tablesortedkeys(fonts.names.blacklist) do |
| 793 |
iowrite (stringformat("(%d %s)\n", n, entry)) |
| 794 |
end |
| 795 |
return true, false |
| 796 |
end |
| 797 |
|
| 798 |
function actions.generate (job) |
| 799 |
local _ = fonts.names.update ({ }, job.force_reload, job.dry_run) |
| 800 |
local namedata = fonts.names.data () |
| 801 |
if namedata then |
| 802 |
logreport ("info", 2, "db", |
| 803 |
"Fonts in the database: %i", |
| 804 |
#namedata.mappings) |
| 805 |
return true, true |
| 806 |
end |
| 807 |
return false, false |
| 808 |
end |
| 809 |
|
| 810 |
|
| 811 |
|
| 812 |
|
| 813 |
|
| 814 |
local bisect_status_path = caches.getwritablepath ("bisect", "") |
| 815 |
local bisect_status_file = bisect_status_path .."/" .. "luaotfload-bisect-status.lua" |
| 816 |
local bisect_status_fmt = [[ |
| 817 |
--[==[------------------------------------------------------------------------- |
| 818 |
This file is generated by Luaotfload. It can be safely deleted. |
| 819 |
Creation date: %s. |
| 820 |
-------------------------------------------------------------------------]==]-- |
| 821 |
|
| 822 |
%s |
| 823 |
|
| 824 |
--- vim:ft=lua:ts=8:et:sw=2 |
| 825 |
]] |
| 826 |
|
| 827 |
|
| 828 |
|
| 829 |
|
| 830 |
|
| 831 |
|
| 832 |
|
| 833 |
|
| 834 |
local function write_bisect_status (data) |
| 835 |
local payload = tableserialize (data, true) |
| 836 |
local status = stringformat (bisect_status_fmt, |
| 837 |
osdate ("%Y-%m-%d %H:%M:%S", os.time ()), |
| 838 |
payload) |
| 839 |
if status and iosavedata (bisect_status_file, status) then |
| 840 |
logreport ("info", 4, "bisect", |
| 841 |
"Bisection state written to %s.", |
| 842 |
bisect_status_file) |
| 843 |
return true |
| 844 |
end |
| 845 |
logreport ("info", 0, "bisect", |
| 846 |
"Failed to write bisection state to %s.", |
| 847 |
bisect_status_file) |
| 848 |
return false |
| 849 |
end |
| 850 |
|
| 851 |
|
| 852 |
|
| 853 |
|
| 854 |
|
| 855 |
|
| 856 |
|
| 857 |
|
| 858 |
local function read_bisect_status () |
| 859 |
logreport ("info", 4, "bisect", |
| 860 |
"Testing for status file: %q.", |
| 861 |
bisect_status_file) |
| 862 |
if not lfsisfile (bisect_status_file) then |
| 863 |
logreport ("info", 2, "bisect", |
| 864 |
"No such file: %q.", bisect_status_file) |
| 865 |
logreport ("info", 0, "bisect", |
| 866 |
"Not in bisect mode.") |
| 867 |
return false |
| 868 |
end |
| 869 |
logreport ("info", 4, "bisect", |
| 870 |
"Reading status file: %q.", bisect_status_file) |
| 871 |
local success, status = pcall (dofile, bisect_status_file) |
| 872 |
if not success then |
| 873 |
logreport ("info", 0, "bisect", |
| 874 |
"Could not read status file.") |
| 875 |
return false |
| 876 |
end |
| 877 |
return status |
| 878 |
end |
| 879 |
|
| 880 |
|
| 881 |
|
| 882 |
|
| 883 |
|
| 884 |
|
| 885 |
|
| 886 |
|
| 887 |
local function bisect_start () |
| 888 |
if lfsisfile (bisect_status_file) then |
| 889 |
logreport ("info", 0, "bisect", |
| 890 |
"Bisect session in progress.", |
| 891 |
bisect_status_file) |
| 892 |
logreport ("info", 0, "bisect", |
| 893 |
"Use --bisect=stop to erase it before starting over.") |
| 894 |
return false, false |
| 895 |
end |
| 896 |
logreport ("info", 2, "bisect", |
| 897 |
"Starting bisection of font database %q.", |
| 898 |
bisect_status_file) |
| 899 |
local n = fonts.names.count_font_files () |
| 900 |
local pivot = mathfloor (n / 2) |
| 901 |
local data = { { 1, n, pivot } } |
| 902 |
logreport ("info", 0, "bisect", |
| 903 |
"Initializing pivot to %d.", pivot) |
| 904 |
if write_bisect_status (data) then |
| 905 |
return true, false |
| 906 |
end |
| 907 |
return false, false |
| 908 |
end |
| 909 |
|
| 910 |
|
| 911 |
|
| 912 |
|
| 913 |
|
| 914 |
|
| 915 |
|
| 916 |
local function bisect_stop () |
| 917 |
logreport ("info", 3, "bisect", |
| 918 |
"Erasing bisection state at %s.", |
| 919 |
bisect_status_file) |
| 920 |
if lfsisfile (bisect_status_file) then |
| 921 |
local success, msg = os.remove (bisect_status_file) |
| 922 |
if not success then |
| 923 |
logreport ("info", 2, "bisect", |
| 924 |
"Failed to erase file %s (%s).", |
| 925 |
bisect_status_file, msg) |
| 926 |
end |
| 927 |
end |
| 928 |
if lfsisdir (bisect_status_path) then |
| 929 |
local success, msg = os.remove (bisect_status_path) |
| 930 |
if not success then |
| 931 |
logreport ("info", 2, "bisect", |
| 932 |
"Failed to erase directory %s (%s).", |
| 933 |
bisect_status_path, msg) |
| 934 |
end |
| 935 |
end |
| 936 |
if lfsisfile (bisect_status_file) then |
| 937 |
return false, false |
| 938 |
end |
| 939 |
return true, false |
| 940 |
end |
| 941 |
|
| 942 |
|
| 943 |
|
| 944 |
|
| 945 |
|
| 946 |
|
| 947 |
|
| 948 |
|
| 949 |
local function bisect_terminate (nsteps, culprit) |
| 950 |
logreport ("info", 1, "bisect", |
| 951 |
"Bisection completed after %d steps.", nsteps) |
| 952 |
logreport ("info", 0, "bisect", |
| 953 |
"Bad file: %s.", fonts.names.nth_font_filename (culprit)) |
| 954 |
logreport ("info", 0, "bisect", |
| 955 |
"Run with --bisect=stop to finish bisection.") |
| 956 |
return true, false |
| 957 |
end |
| 958 |
|
| 959 |
|
| 960 |
|
| 961 |
|
| 962 |
|
| 963 |
|
| 964 |
|
| 965 |
local function list_remainder (lo, hi) |
| 966 |
local fonts = fonts.names.font_slice (lo, hi) |
| 967 |
logreport ("info", 0, "bisect", "%d fonts left.", hi - lo + 1) |
| 968 |
for i = 1, #fonts do |
| 969 |
logreport ("info", 1, "bisect", " · %2d: %s", lo, fonts[i]) |
| 970 |
lo = lo + 1 |
| 971 |
end |
| 972 |
end |
| 973 |
|
| 974 |
|
| 975 |
|
| 976 |
|
| 977 |
|
| 978 |
|
| 979 |
o we |
| 980 |
hat. |
| 981 |
|
| 982 |
|
| 983 |
|
| 984 |
local function bisect_set (outcome) |
| 985 |
local status = read_bisect_status () |
| 986 |
if not status then |
| 987 |
return false, false |
| 988 |
end |
| 989 |
|
| 990 |
local nsteps = #status |
| 991 |
local previous = status[nsteps] |
| 992 |
if previous == true then |
| 993 |
|
| 994 |
|
| 995 |
|
| 996 |
nsteps = nsteps - 1 |
| 997 |
return bisect_terminate (nsteps, status[nsteps][1]) |
| 998 |
end |
| 999 |
|
| 1000 |
local lo, hi, pivot = unpack (previous) |
| 1001 |
|
| 1002 |
logreport ("info", 3, "bisect", |
| 1003 |
"Previous step %d: lo=%d, hi=%d, pivot=%d.", |
| 1004 |
nsteps, lo, hi, pivot) |
| 1005 |
|
| 1006 |
if outcome == "bad" then |
| 1007 |
hi = pivot |
| 1008 |
if lo >= hi then |
| 1009 |
status[nsteps + 1] = { lo, lo, lo } |
| 1010 |
status[nsteps + 2] = true |
| 1011 |
write_bisect_status (status) |
| 1012 |
return bisect_terminate (nsteps, lo) |
| 1013 |
end |
| 1014 |
pivot = mathfloor ((lo + hi) / 2) |
| 1015 |
logreport ("info", 0, "bisect", |
| 1016 |
"Continuing with the lower segment: lo=%d, hi=%d, pivot=%d.", |
| 1017 |
lo, hi, pivot) |
| 1018 |
elseif outcome == "good" then |
| 1019 |
lo = pivot + 1 |
| 1020 |
if lo >= hi then |
| 1021 |
status[nsteps + 1] = { lo, lo, lo } |
| 1022 |
write_bisect_status (status) |
| 1023 |
status[nsteps + 2] = true |
| 1024 |
return bisect_terminate (nsteps, lo) |
| 1025 |
end |
| 1026 |
pivot = mathfloor ((lo + hi) / 2) |
| 1027 |
logreport ("info", 0, "bisect", |
| 1028 |
"Continuing with the upper segment: lo=%d, hi=%d, pivot=%d.", |
| 1029 |
lo, hi, pivot) |
| 1030 |
else en |
| 1031 |
logreport ("info", 0, "bisect", |
| 1032 |
"What the hell?", lo, hi, pivot) |
| 1033 |
return false, false |
| 1034 |
end |
| 1035 |
|
| 1036 |
status[nsteps + 1] = { lo, hi, pivot } |
| 1037 |
write_bisect_status (status) |
| 1038 |
if hi - lo <= 10 then |
| 1039 |
list_remainder (lo, hi) |
| 1040 |
end |
| 1041 |
return true, false |
| 1042 |
end |
| 1043 |
|
| 1044 |
|
| 1045 |
|
| 1046 |
|
| 1047 |
|
| 1048 |
|
| 1049 |
|
| 1050 |
local function bisect_status () |
| 1051 |
local status = read_bisect_status () |
| 1052 |
if not status then |
| 1053 |
return false, false |
| 1054 |
end |
| 1055 |
local nsteps = #status |
| 1056 |
if nsteps > 1 then |
| 1057 |
for i = nsteps - 1, 1, -1 do |
| 1058 |
local step = status[i] |
| 1059 |
logreport ("info", 2, "bisect", "Step %d: lo=%d, hi=%d, pivot=%d.", |
| 1060 |
i, unpack (step)) |
| 1061 |
end |
| 1062 |
end |
| 1063 |
local current = status[nsteps] |
| 1064 |
logreport ("info", 0, "bisect", "Step %d: lo=%d, hi=%d, pivot=%d.", |
| 1065 |
nsteps, unpack (current)) |
| 1066 |
return true, false |
| 1067 |
end |
| 1068 |
|
| 1069 |
|
| 1070 |
|
| 1071 |
|
| 1072 |
|
| 1073 |
|
| 1074 |
|
| 1075 |
|
| 1076 |
|
| 1077 |
|
| 1078 |
|
| 1079 |
local function bisect_run () |
| 1080 |
local status = read_bisect_status () |
| 1081 |
if not status then |
| 1082 |
return false, false |
| 1083 |
end |
| 1084 |
local nsteps = #status |
| 1085 |
local currentstep = nsteps + 1 |
| 1086 |
local current = status[nsteps] |
| 1087 |
if current == true then |
| 1088 |
current = status[nsteps - 1] |
| 1089 |
end |
| 1090 |
local lo, hi, pivot = unpack (current) |
| 1091 |
logreport ("info", 3, "bisect", "Previous step %d: lo=%d, hi=%d, pivot=%d.", |
| 1092 |
nsteps, lo, hi, pivot) |
| 1093 |
logreport ("info", 1, "bisect", "Step %d: Testing fonts from %d to %d.", |
| 1094 |
currentstep, lo, pivot) |
| 1095 |
config.luaotfload.misc.bisect = { lo, pivot } |
| 1096 |
return true, true |
| 1097 |
end |
| 1098 |
|
| 1099 |
local bisect_modes = { |
| 1100 |
start = bisect_start, |
| 1101 |
good = function () return bisect_set "good" end, |
| 1102 |
bad = function () return bisect_set "bad" end, |
| 1103 |
stop = bisect_stop, |
| 1104 |
status = bisect_status, |
| 1105 |
run = bisect_run, |
| 1106 |
} |
| 1107 |
|
| 1108 |
function actions.bisect (job) |
| 1109 |
local mode = job.bisect |
| 1110 |
local runner = bisect_modes[mode] |
| 1111 |
if not runner then |
| 1112 |
logreport ("info", 0, "bisect", "Unknown directive %q.", mode) |
| 1113 |
return false, false |
| 1114 |
end |
| 1115 |
return runner (job) |
| 1116 |
end |
| 1117 |
|
| 1118 |
function actions.flush (job) |
| 1119 |
local success = fonts.names.flush_lookup_cache() |
| 1120 |
if success then |
| 1121 |
local success = fonts.names.save_lookups() |
| 1122 |
if success then |
| 1123 |
logreport ("info", 2, "cache", "Lookup cache emptied") |
| 1124 |
return true, true |
| 1125 |
end |
| 1126 |
end |
| 1127 |
return false, false |
| 1128 |
end |
| 1129 |
|
| 1130 |
local function cache_directives () |
| 1131 |
|
| 1132 |
return { |
| 1133 |
["purge"] = fonts.names.purge_cache, |
| 1134 |
["erase"] = fonts.names.erase_cache, |
| 1135 |
["show"] = fonts.names.show_cache, |
| 1136 |
} |
| 1137 |
end |
| 1138 |
|
| 1139 |
function actions.cache (job) |
| 1140 |
local directive = cache_directives()[job.cache] |
| 1141 |
if not directive or type(directive) ~= "function" then |
| 1142 |
logreport ("info", 2, "cache", |
| 1143 |
"Invalid font cache directive %s.", job.cache) |
| 1144 |
return false, false |
| 1145 |
end |
| 1146 |
if directive() then |
| 1147 |
return true, true |
| 1148 |
end |
| 1149 |
return false, false |
| 1150 |
end |
| 1151 |
|
| 1152 |
function actions.query (job) |
| 1153 |
|
| 1154 |
require "luaotfload-features" |
| 1155 |
|
| 1156 |
local query = job.query |
| 1157 |
|
| 1158 |
local tmpspec = fonts.definers.analyze (query, 655360) |
| 1159 |
|
| 1160 |
local foundname, subfont, success, needle |
| 1161 |
|
| 1162 |
if tmpspec.lookup == "name" then |
| 1163 |
if fonts.definers.resolvers.name (tmpspec) then |
| 1164 |
needle = tmpspec.resolved |
| 1165 |
end |
| 1166 |
elseif tmpspec.lookup == "anon" then |
| 1167 |
if fonts.definers.resolvers.anon (tmpspec) then |
| 1168 |
needle = tmpspec.resolved or tmpspec.name |
| 1169 |
end |
| 1170 |
elseif tmpspec.lookup == "file" then |
| 1171 |
needle = tmpspec.name |
| 1172 |
end |
| 1173 |
subfont = tmpspec.sub |
| 1174 |
|
| 1175 |
if needle then |
| 1176 |
foundname, _, success = fonts.names.lookup_font_file (tmpspec.name) |
| 1177 |
end |
| 1178 |
|
| 1179 |
if success then |
| 1180 |
logreport (false, 0, "resolve", "Font %q found!", query) |
| 1181 |
if subfont then |
| 1182 |
logreport (false, 0, "resolve", |
| 1183 |
"Resolved file name %q, subfont nr. %q", |
| 1184 |
foundname, subfont - 1) |
| 1185 |
else |
| 1186 |
logreport (false, 0, "resolve", |
| 1187 |
"Resolved file name %q", foundname) |
| 1188 |
end |
| 1189 |
if job.show_info then |
| 1190 |
logreport (false, 3, "resolve", "Dump extra info.") |
| 1191 |
show_font_info (foundname, query, job.full_info, subfont) |
| 1192 |
iowrite "\n" |
| 1193 |
end |
| 1194 |
else |
| 1195 |
logreport (false, 0, "resolve", "Cannot find %q in index.", query) |
| 1196 |
if job.fuzzy == true then |
| 1197 |
logreport (false, 0, "resolve", |
| 1198 |
"Looking for close matches, this may take a while ...") |
| 1199 |
local _success = fonts.names.find_closest(query, job.fuzzy_limit) |
| 1200 |
else |
| 1201 |
logreport (false, 0, "resolve", |
| 1202 |
"Hint: use the --fuzzy option to display suggestions.", |
| 1203 |
query) |
| 1204 |
end |
| 1205 |
end |
| 1206 |
return true, true |
| 1207 |
end |
| 1208 |
|
| 1209 |
|
| 1210 |
|
| 1211 |
|
| 1212 |
|
| 1213 |
|
| 1214 |
local function get_fields (entry, fields) |
| 1215 |
local acc = {} |
| 1216 |
for n, field in ipairs(fields) do |
| 1217 |
local chain = stringsplit (field, "->") |
| 1218 |
local tmp = entry |
| 1219 |
for i = 1, #chain do |
| 1220 |
tmp = tmp [chain [i]] |
| 1221 |
if not tmp then |
| 1222 |
|
| 1223 |
break |
| 1224 |
end |
| 1225 |
end |
| 1226 |
acc[n] = tmp or false |
| 1227 |
end |
| 1228 |
return acc |
| 1229 |
end |
| 1230 |
|
| 1231 |
local separator = "\t" csv |
| 1232 |
|
| 1233 |
local function format_fields (fields, acc, n) |
| 1234 |
if not acc then |
| 1235 |
return format_fields(fields, { }, 1) |
| 1236 |
end |
| 1237 |
|
| 1238 |
local field = fields[n] |
| 1239 |
if field ~= nil then |
| 1240 |
if field == false then |
| 1241 |
acc[#acc+1] = "<none>" |
| 1242 |
else |
| 1243 |
acc[#acc+1] = tostring(field) |
| 1244 |
end |
| 1245 |
return format_fields(fields, acc, n+1) |
| 1246 |
end |
| 1247 |
return tableconcat(acc, separator) |
| 1248 |
end |
| 1249 |
|
| 1250 |
local function set_primary_field (fields, addme, acc, n) |
| 1251 |
if not acc then |
| 1252 |
return set_primary_field(fields, addme, { addme }, 1) |
| 1253 |
end |
| 1254 |
|
| 1255 |
local field = fields[n] |
| 1256 |
if field then |
| 1257 |
if field ~= addme then |
| 1258 |
acc[#acc+1] = field |
| 1259 |
end |
| 1260 |
return set_primary_field(fields, addme, acc, n+1) |
| 1261 |
end |
| 1262 |
return acc |
| 1263 |
end |
| 1264 |
|
| 1265 |
local function sane_pattern (p) |
| 1266 |
local l = lpeg or require'lpeg' |
| 1267 |
local special = l.Cg(l.P'*' * l.Cc'.*') |
| 1268 |
local pattern = l.Cs(l.Cc'^' * (special + (1 - special)^1/fonts.names.sanitize_fontname)^0 * l.Cc'$') |
| 1269 |
function sane_pattern (p) |
| 1270 |
return pattern:match(p) |
| 1271 |
end |
| 1272 |
return sane_pattern(p) |
| 1273 |
end |
| 1274 |
|
| 1275 |
function actions.list (job) |
| 1276 |
local criterion = job.criterion |
| 1277 |
local asked_fields = job.asked_fields |
| 1278 |
local name_index = fonts.names.data () |
| 1279 |
|
| 1280 |
if asked_fields then |
| 1281 |
local splitcomma = luaotfload.parsers.splitcomma |
| 1282 |
asked_fields = lpegmatch(splitcomma, asked_fields) |
| 1283 |
end |
| 1284 |
|
| 1285 |
if not asked_fields then |
| 1286 |
|
| 1287 |
asked_fields = { "plainname", "version", } |
| 1288 |
end |
| 1289 |
|
| 1290 |
if not name_index then |
| 1291 |
name_index = fonts.names.load() |
| 1292 |
end |
| 1293 |
|
| 1294 |
local mappings = name_index.mappings |
| 1295 |
local nmappings = #mappings |
| 1296 |
|
| 1297 |
if criterion == "*" then |
| 1298 |
logreport (false, 1, "list", "All %d entries", nmappings) |
| 1299 |
for i=1, nmappings do |
| 1300 |
local entry = mappings[i] |
| 1301 |
local fields = get_fields(entry, asked_fields) |
| 1302 |
|
| 1303 |
local formatted = format_fields(fields) |
| 1304 |
texiowrite_nl(formatted) |
| 1305 |
end |
| 1306 |
|
| 1307 |
else |
| 1308 |
criterion = stringexplode(criterion, ":") |
| 1309 |
local asked_value = criterion[2] |
| 1310 |
criterion = criterion[1] |
| 1311 |
asked_fields = set_primary_field(asked_fields, criterion) |
| 1312 |
|
| 1313 |
logreport (false, 1, "list", "By %s", criterion) |
| 1314 |
|
| 1315 |
|
| 1316 |
local targets = { } |
| 1317 |
if asked_value then |
| 1318 |
local sane_asked_pattern = sane_pattern(asked_value) |
| 1319 |
logreport (false, 2, "list", "Restricting to value %s", asked_value) |
| 1320 |
for i=1, nmappings do |
| 1321 |
local entry = mappings[i] |
| 1322 |
local entryvalue = tostring(entry[criterion]) |
| 1323 |
if entryvalue |
| 1324 |
and (entryvalue == asked_value or entryvalue:match(sane_asked_pattern)) |
| 1325 |
then |
| 1326 |
targets[#targets+1] = entry |
| 1327 |
end |
| 1328 |
end |
| 1329 |
|
| 1330 |
else |
| 1331 |
local categories, by_category = { }, { } |
| 1332 |
for i=1, nmappings do |
| 1333 |
local entry = mappings[i] |
| 1334 |
local tmp = entry |
| 1335 |
local chain = stringsplit (criterion, "->") |
| 1336 |
for i = 1, #chain - 1 do |
| 1337 |
tmp = tmp [chain [i]] |
| 1338 |
if not tmp then |
| 1339 |
break |
| 1340 |
end |
| 1341 |
end |
| 1342 |
local value = tmp and tmp [chain [#chain]] or "<none>" |
| 1343 |
if value then |
| 1344 |
|
| 1345 |
local entries = by_category[value] |
| 1346 |
if not entries then |
| 1347 |
entries = { entry } |
| 1348 |
categories[#categories+1] = value |
| 1349 |
else |
| 1350 |
entries[#entries+1] = entry |
| 1351 |
end |
| 1352 |
by_category[value] = entries |
| 1353 |
end |
| 1354 |
end |
| 1355 |
table.sort(categories) |
| 1356 |
|
| 1357 |
for i=1, #categories do |
| 1358 |
local entries = by_category[categories[i]] |
| 1359 |
for j=1, #entries do |
| 1360 |
targets[#targets+1] = entries[j] |
| 1361 |
end |
| 1362 |
end |
| 1363 |
end |
| 1364 |
local ntargets = #targets |
| 1365 |
logreport (false, 2, "list", "%d entries", ntargets) |
| 1366 |
|
| 1367 |
|
| 1368 |
for i=1, ntargets do |
| 1369 |
local entry = targets[i] |
| 1370 |
local fields = get_fields(entry, asked_fields) |
| 1371 |
local formatted = format_fields(fields) |
| 1372 |
texiowrite_nl(formatted) |
| 1373 |
end |
| 1374 |
end |
| 1375 |
|
| 1376 |
texiowrite_nl "" |
| 1377 |
|
| 1378 |
return true, true |
| 1379 |
end |
| 1380 |
|
| 1381 |
function actions.diagnose (job) |
| 1382 |
|
| 1383 |
local diagnose = require "luaotfload-diagnostics.lua" |
| 1384 |
return diagnose (job) |
| 1385 |
end |
| 1386 |
|
| 1387 |
function actions.aliases (job) |
| 1388 |
|
| 1389 |
local name_index = fonts.names.data() or fonts.names.load() |
| 1390 |
local mappings = name_index.mappings |
| 1391 |
local fontnames = name_index.fontnames.texmf |
| 1392 |
local families = name_index.families.texmf |
| 1393 |
local formats = { 'ttf', 'otf', } |
| 1394 |
for _, format in ipairs(formats) do |
| 1395 |
for name, mapping in pairs(fontnames[format]) do |
| 1396 |
mapping = mappings[mapping] |
| 1397 |
print(string.format('%s %s', mapping.basename, name)) |
| 1398 |
end |
| 1399 |
end |
| 1400 |
local function best_match(options, target) |
| 1401 |
if not options then return end |
| 1402 |
if options.default then return mappings[options.default] end |
| 1403 |
local best, best_diff = nil, math.huge |
| 1404 |
for _, option in ipairs(options) do |
| 1405 |
local diff = math.abs(option[1]-target) |
| 1406 |
if diff < best_diff then |
| 1407 |
best, best_diff = option[4], diff |
| 1408 |
end |
| 1409 |
end |
| 1410 |
return mappings[best] |
| 1411 |
end |
| 1412 |
for _, format in ipairs(formats) do |
| 1413 |
for name, family in pairs(families[format]) do |
| 1414 |
local r = best_match(family.r, 655360, mappings) |
| 1415 |
local b = best_match(family.b, 655360, mappings) |
| 1416 |
local i = best_match(family.i, 655360, mappings) |
| 1417 |
local bi = best_match(family.bi, 655360, mappings) |
| 1418 |
r = r or b or i or bi |
| 1419 |
if r then |
| 1420 |
b, i, bi = b or r, i or r, bi or r |
| 1421 |
print(string.format('%s %s\n%s %s-b\n%s %s-i\n%s %s-bi', |
| 1422 |
r.basename, name, |
| 1423 |
b.basename, name, |
| 1424 |
i.basename, name, |
| 1425 |
bi.basename, name)) |
| 1426 |
end |
| 1427 |
end |
| 1428 |
end |
| 1429 |
return true |
| 1430 |
end |
| 1431 |
|
| 1432 |
|
| 1433 |
|
| 1434 |
local finalizers = { } |
| 1435 |
|
| 1436 |
|
| 1437 |
|
| 1438 |
local function finalize () |
| 1439 |
local success = true |
| 1440 |
for _, fun in next, finalizers do |
| 1441 |
if type (fun) == "function" then |
| 1442 |
if fun () == false then success = false end |
| 1443 |
end |
| 1444 |
end |
| 1445 |
return success |
| 1446 |
end |
| 1447 |
|
| 1448 |
|
| 1449 |
|
| 1450 |
|
| 1451 |
|
| 1452 |
|
| 1453 |
|
| 1454 |
|
| 1455 |
|
| 1456 |
|
| 1457 |
|
| 1458 |
local function process_cmdline ( ) |
| 1459 |
local result = { |
| 1460 |
force_reload = nil, |
| 1461 |
full_info = false, |
| 1462 |
criterion = "", |
| 1463 |
query = "", |
| 1464 |
log_level = nil, |
| 1465 |
bisect = nil, |
| 1466 |
config = { db = { }, misc = { }, run = { live = false }, paths = { } }, |
| 1467 |
} |
| 1468 |
|
| 1469 |
local long_options = { |
| 1470 |
aliases = 0, |
| 1471 |
["bisect"] = 1, |
| 1472 |
cache = 1, |
| 1473 |
conf = 1, |
| 1474 |
diagnose = 1, |
| 1475 |
dumpconf = 0, |
| 1476 |
["dry-run"] = "D", |
| 1477 |
["flush-lookups"] = "l", |
| 1478 |
fields = 1, |
| 1479 |
find = 1, |
| 1480 |
force = "f", |
| 1481 |
formats = 1, |
| 1482 |
fuzzy = "F", |
| 1483 |
help = "h", |
| 1484 |
info = "i", |
| 1485 |
inspect = "I", |
| 1486 |
limit = 1, |
| 1487 |
list = 1, |
| 1488 |
["local"] = "L", |
| 1489 |
log = 1, |
| 1490 |
["max-fonts"] = 1, |
| 1491 |
["no-compress"] = "c", |
| 1492 |
["no-reload"] = "n", |
| 1493 |
["no-strip"] = 0, |
| 1494 |
["skip-read"] = "R", |
| 1495 |
["prefer-texmf"] = "p", |
| 1496 |
["print-conf"] = 0, |
| 1497 |
quiet = "q", |
| 1498 |
["show-blacklist"] = "b", |
| 1499 |
stats = "S", |
| 1500 |
update = "u", |
| 1501 |
verbose = 1, |
| 1502 |
version = "V", |
| 1503 |
} |
| 1504 |
|
| 1505 |
local short_options = "bcDfFiIlLnpqRSuvVh" |
| 1506 |
|
| 1507 |
local options, _, optarg = |
| 1508 |
alt_getopt.get_ordered_opts (arg, short_options, long_options) |
| 1509 |
|
| 1510 |
local nopts = #options |
| 1511 |
for n=1, nopts do |
| 1512 |
local v = options[n] |
| 1513 |
if v == "q" then |
| 1514 |
result.log_level = 0 |
| 1515 |
elseif v == "u" then |
| 1516 |
action_pending["generate"] = true |
| 1517 |
elseif v == "v" then |
| 1518 |
local lvl = result.log_level |
| 1519 |
if not lvl or lvl < 1 then |
| 1520 |
lvl = 1 |
| 1521 |
else |
| 1522 |
lvl = lvl + 1 |
| 1523 |
end |
| 1524 |
result.log_level = lvl |
| 1525 |
elseif v == "V" then |
| 1526 |
action_pending["version"] = true |
| 1527 |
elseif v == "h" then |
| 1528 |
action_pending["help"] = true |
| 1529 |
elseif v == "f" then |
| 1530 |
result.update = true |
| 1531 |
result.force_reload = 1 |
| 1532 |
elseif v == "verbose" then |
| 1533 |
local lvl = optarg[n] |
| 1534 |
if lvl then |
| 1535 |
lvl = tonumber(lvl) |
| 1536 |
result.log_level = lvl |
| 1537 |
end |
| 1538 |
elseif v == "log" then |
| 1539 |
local str = optarg[n] |
| 1540 |
if str then |
| 1541 |
finalizers = luaotfload.log.set_logout(str, finalizers) |
| 1542 |
end |
| 1543 |
elseif v == "find" then |
| 1544 |
action_pending["query"] = true |
| 1545 |
result.query = optarg[n] |
| 1546 |
elseif v == "F" then |
| 1547 |
result.fuzzy = true |
| 1548 |
elseif v == "limit" then |
| 1549 |
local lim = optarg[n] |
| 1550 |
if lim then |
| 1551 |
result.fuzzy_limit = tonumber(lim) |
| 1552 |
end |
| 1553 |
elseif v == "i" then |
| 1554 |
result.show_info = true |
| 1555 |
elseif v == "I" then |
| 1556 |
result.show_info = true |
| 1557 |
result.full_info = true |
| 1558 |
elseif v == "l" then |
| 1559 |
action_pending["flush"] = true |
| 1560 |
elseif v == "L" then |
| 1561 |
action_pending["generate"] = true |
| 1562 |
result.config.db.scan_local = true |
| 1563 |
elseif v == "list" then |
| 1564 |
action_pending["list"] = true |
| 1565 |
result.criterion = optarg[n] |
| 1566 |
elseif v == "fields" then |
| 1567 |
result.asked_fields = optarg[n] |
| 1568 |
elseif v == "cache" then |
| 1569 |
action_pending["cache"] = true |
| 1570 |
result.cache = optarg[n] |
| 1571 |
elseif v == "D" then |
| 1572 |
result.dry_run = true |
| 1573 |
elseif v == "p" then |
| 1574 |
fonts.names.set_location_precedence { |
| 1575 |
"local", "texmf", "system" |
| 1576 |
} |
| 1577 |
elseif v == "b" then |
| 1578 |
action_pending["blacklist"] = true |
| 1579 |
elseif v == "diagnose" then |
| 1580 |
action_pending["diagnose"] = true |
| 1581 |
result.asked_diagnostics = optarg[n] |
| 1582 |
elseif v == "formats" then |
| 1583 |
result.config.db.formats = optarg[n] |
| 1584 |
|
| 1585 |
elseif v == "n" then |
| 1586 |
result.config.db.update_live = false |
| 1587 |
elseif v == "S" then |
| 1588 |
result.config.misc.statistics = true |
| 1589 |
elseif v == "R" then |
| 1590 |
|
| 1591 |
result.config.db.skip_read = true |
| 1592 |
elseif v == "c" then |
| 1593 |
result.config.db.compress = false |
| 1594 |
elseif v == "no-strip" then |
| 1595 |
result.config.db.strip = false |
| 1596 |
elseif v == "max-fonts" then |
| 1597 |
local n = optarg[n] |
| 1598 |
if n then |
| 1599 |
n = tonumber(n) |
| 1600 |
if n and n > 0 then |
| 1601 |
result.config.db.max_fonts = n |
| 1602 |
end |
| 1603 |
end |
| 1604 |
elseif v == "bisect" then |
| 1605 |
result.bisect = optarg[n] |
| 1606 |
action_pending.bisect = true |
| 1607 |
elseif v == "conf" then |
| 1608 |
local confname = optarg[n] |
| 1609 |
if confname then |
| 1610 |
local extra = stringexplode (optarg[n], ",+") |
| 1611 |
if extra then |
| 1612 |
local extra_config = result.extra_config |
| 1613 |
if extra_config then |
| 1614 |
table.append (extra_config, extra) |
| 1615 |
else |
| 1616 |
result.extra_config = extra |
| 1617 |
end |
| 1618 |
end |
| 1619 |
end |
| 1620 |
elseif v == "dumpconf" then |
| 1621 |
action_pending["dumpconf"] = true |
| 1622 |
elseif v == "print-conf" then |
| 1623 |
result.print_config = true |
| 1624 |
elseif v == "aliases" then |
| 1625 |
action_pending["aliases"] = true |
| 1626 |
end |
| 1627 |
end |
| 1628 |
|
| 1629 |
if nopts == 0 then |
| 1630 |
action_pending["help"] = true |
| 1631 |
result.help_version = "short" |
| 1632 |
end |
| 1633 |
return result |
| 1634 |
end |
| 1635 |
|
| 1636 |
local function main ( ) |
| 1637 |
if init_modules () == false then return -42 end |
| 1638 |
|
| 1639 |
local retval = 0 |
| 1640 |
local job = process_cmdline() |
| 1641 |
|
| 1642 |
|
| 1643 |
|
| 1644 |
|
| 1645 |
for i=1, #action_sequence do |
| 1646 |
local actionname = action_sequence[i] |
| 1647 |
local exit = false |
| 1648 |
if action_pending[actionname] then |
| 1649 |
logreport ("log", 3, "util", "Preparing for task", "%s", actionname) |
| 1650 |
|
| 1651 |
local action = actions[actionname] |
| 1652 |
local success, continue = action(job) |
| 1653 |
|
| 1654 |
if not success then |
| 1655 |
logreport (false, 0, "util", |
| 1656 |
"Failed to execute task.", "%s", actionname) |
| 1657 |
retval = -1 |
| 1658 |
exit = true |
| 1659 |
elseif not continue then |
| 1660 |
logreport (false, 3, "util", |
| 1661 |
"Task completed, exiting.", "%s", actionname) |
| 1662 |
exit = true |
| 1663 |
else |
| 1664 |
logreport (false, 3, "util", |
| 1665 |
"Task completed successfully.", "%s", actionname) |
| 1666 |
end |
| 1667 |
end |
| 1668 |
if exit then break end |
| 1669 |
end |
| 1670 |
|
| 1671 |
if finalize () == false then |
| 1672 |
retval = -1 |
| 1673 |
end |
| 1674 |
|
| 1675 |
|
| 1676 |
return retval |
| 1677 |
end |
| 1678 |
|
| 1679 |
return main() |
| 1680 |
|
| 1681 |
|