From 69f51727fba1c348d78348d9f163cf884e7ab558 Mon Sep 17 00:00:00 2001 From: Philipp Gesang Date: Thu, 9 May 2013 13:45:14 +0200 Subject: add querying of font metadata to luaotfload-tool --- luaotfload-tool.lua | 147 ++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 143 insertions(+), 4 deletions(-) (limited to 'luaotfload-tool.lua') diff --git a/luaotfload-tool.lua b/luaotfload-tool.lua index 0cd19b1..6662291 100755 --- a/luaotfload-tool.lua +++ b/luaotfload-tool.lua @@ -32,6 +32,8 @@ local stringformat = string.format local texiowrite_nl = texio.write_nl local stringlower = string.lower +local C, Ct, P = lpeg.C, lpeg.Ct, lpeg.P +local lpegmatch = lpeg.match local loader_file = "luatexbase.loader.lua" local loader_path = assert(kpse.find_file(loader_file, "lua"), @@ -64,8 +66,6 @@ local config = config config.luaotfload = config.luaotfload or { } do -- we don’t have file.basename and the likes yet, so inline parser ftw - local C, P = lpeg.C, lpeg.P - local lpegmatch = lpeg.match local slash = P"/" local dot = P"." local noslash = 1 - slash @@ -228,7 +228,7 @@ set. --]]-- local action_sequence = { - "loglevel", "help", "version", "flush", "generate", "query" + "loglevel", "help", "version", "flush", "generate", "list", "query" } local action_pending = table.tohash(action_sequence, false) @@ -286,7 +286,7 @@ actions.query = function (job) lookup = "name", specification = "name:" .. query, optsize = 0, - } + } local foundname, subfont, success = fonts.names.resolve(nil, nil, tmpspec) @@ -317,6 +317,137 @@ actions.query = function (job) return true, true end +--- --list= +--- --list=: +--- +--- --list= --fields=,,,... + +local get_fields get_fields = function (entry, fields, acc, n) + if not acc then + return get_fields(entry, fields, { }, 1) + end + + local field = fields[n] + if field then + local value = entry[field] + acc[#acc+1] = value or false + return get_fields(entry, fields, acc, n+1) + end + return acc +end + +local comma = P"," +local noncomma = 1-comma +local split_comma = Ct((C(noncomma^1) + comma)^1) + +local texiowrite_nl = texio.write_nl +local tableconcat = table.concat +local stringexplode = string.explode + +local separator = "\t" --- could be “,” for csv + +local format_fields format_fields = function (fields, acc, n) + if not acc then + return format_fields(fields, { }, 1) + end + + local field = fields[n] + if field ~= nil then + if field == false then + acc[#acc+1] = "" + else + acc[#acc+1] = tostring(field) + end + return format_fields(fields, acc, n+1) + end + return tableconcat(acc, separator) +end + +local set_primary_field +set_primary_field = function (fields, addme, acc, n) + if not acc then + return set_primary_field(fields, addme, { addme }, 1) + end + + local field = fields[n] + if field then + if field ~= addme then + acc[#acc+1] = field + end + return set_primary_field(fields, addme, acc, n+1) + end + return acc +end + +actions.list = function (job) + local criterion = job.criterion + + local asked_fields = job.asked_fields + if asked_fields then + asked_fields = lpegmatch(split_comma, asked_fields) + else + --- some defaults + asked_fields = { "fullname", "version", } + end + + if not names.data then + names.data = names.load() + end + + local mappings = names.data.mappings + local nmappings = #mappings + + if criterion == "*" then + logs.names_report(false, 1, "list", "all %d entries", nmappings) + for i=1, nmappings do + local entry = mappings[i] + local fields = get_fields(entry, asked_fields) + --- we could collect these instead ... + local formatted = format_fields(fields) + texiowrite_nl(formatted) + end + + else + criterion = stringexplode(criterion, ":") --> { field, value } + local asked_value = criterion[2] + criterion = criterion[1] + asked_fields = set_primary_field(asked_fields, criterion) + + logs.names_report(false, 1, "list", "by %s", criterion) + + --- firstly, build a list of fonts to operate on + local targets, ntargets + + if asked_value then + logs.names_report(false, 2, "list", "restricting to value %s", asked_value) + targets = { } + for i=1, nmappings do + local entry = mappings[i] + if entry[criterion] + and tostring(entry[criterion]) == asked_value + then + targets[#targets+1] = entry + end + end + ntargets = #targets + else --- all + targets = mappings + ntargets = nmappings + end + logs.names_report(false, 2, "list", "%d entries", ntargets) + + --- now, output the collection + for i=1, ntargets do + local entry = targets[i] + local fields = get_fields(entry, asked_fields) + local formatted = format_fields(fields) + texiowrite_nl(formatted) + end + end + + return true, true +end + --[[-- Command-line processing. mkluatexfontdb.lua relies on the script alt_getopt to process argv and @@ -330,6 +461,7 @@ alt_getopt. local process_cmdline = function ( ) -- unit -> jobspec local result = { -- jobspec force_reload = nil, + criterion = "", query = "", log_level = 1, --- 2 is approx. the old behavior } @@ -337,12 +469,14 @@ local process_cmdline = function ( ) -- unit -> jobspec local long_options = { alias = 1, ["flush-cache"] = "c", + fields = 1, find = 1, force = "f", fuzzy = "F", help = "h", info = "i", limit = 1, + list = 1, log = 1, quiet = "q", update = "u", @@ -401,6 +535,11 @@ local process_cmdline = function ( ) -- unit -> jobspec config.luaotfload.self = optarg[n] elseif v == "c" then action_pending["flush"] = true + elseif v == "list" then + action_pending["list"] = true + result.criterion = optarg[n] + elseif v == "fields" then + result.asked_fields = optarg[n] end end -- cgit v1.2.3 From 822273918a0475abb91418e63dccff021e1d7983 Mon Sep 17 00:00:00 2001 From: Philipp Gesang Date: Thu, 9 May 2013 14:02:05 +0200 Subject: sort output for unspecified fields --- luaotfload-tool.lua | 37 +++++++++++++++++++++++++++++-------- 1 file changed, 29 insertions(+), 8 deletions(-) (limited to 'luaotfload-tool.lua') diff --git a/luaotfload-tool.lua b/luaotfload-tool.lua index 6662291..3e10820 100755 --- a/luaotfload-tool.lua +++ b/luaotfload-tool.lua @@ -416,11 +416,9 @@ actions.list = function (job) logs.names_report(false, 1, "list", "by %s", criterion) --- firstly, build a list of fonts to operate on - local targets, ntargets - - if asked_value then + local targets = { } + if asked_value then --- only those whose value matches logs.names_report(false, 2, "list", "restricting to value %s", asked_value) - targets = { } for i=1, nmappings do local entry = mappings[i] if entry[criterion] @@ -429,11 +427,34 @@ actions.list = function (job) targets[#targets+1] = entry end end - ntargets = #targets - else --- all - targets = mappings - ntargets = nmappings + + else --- whichever have the field, sorted + local categories, by_category = { }, { } + for i=1, nmappings do + local entry = mappings[i] + local value = entry[criterion] + if value then + --value = tostring(value) + local entries = by_category[value] + if not entries then + entries = { entry } + categories[#categories+1] = value + else + entries[#entries+1] = entry + end + by_category[value] = entries + end + end + table.sort(categories) + + for i=1, #categories do + local entries = by_category[categories[i]] + for j=1, #entries do + targets[#targets+1] = entries[j] + end + end end + local ntargets = #targets logs.names_report(false, 2, "list", "%d entries", ntargets) --- now, output the collection -- cgit v1.2.3 From ab06ec4422bc2e18ee5528a0ae7abd95a031c694 Mon Sep 17 00:00:00 2001 From: Philipp Gesang Date: Thu, 9 May 2013 15:11:33 +0200 Subject: update manpage and usage info --- luaotfload-tool.lua | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) (limited to 'luaotfload-tool.lua') diff --git a/luaotfload-tool.lua b/luaotfload-tool.lua index 3e10820..8d7f6f5 100755 --- a/luaotfload-tool.lua +++ b/luaotfload-tool.lua @@ -129,11 +129,14 @@ This tool is part of the luaotfload package. Valid options are: -v --verbose=LEVEL be more verbose (print the searched directories) -vv print the loaded fonts -vvv print all steps of directory searching + --log=stdout redirect log output to stdout + -V --version print version and exit -h --help print this message --alias= force behavior of “luaotfload-tool” or legacy “mkluatexfontdb” + ------------------------------------------------------------------------------- DATABASE @@ -147,7 +150,9 @@ This tool is part of the luaotfload package. Valid options are: (default: n = 1) -i --info display font metadata - --log=stdout redirect log output to stdout + --list= output list of entries by field + --list=: restrict to entries with = + --fields=,,…, which fields to print with --list The font database will be saved to %s @@ -158,7 +163,7 @@ The font database will be saved to Usage: %s [OPTION]... -Rebuild the LuaTeX font database. +Rebuild or update the LuaTeX font database. Valid options: -f --force force re-indexing all fonts -- cgit v1.2.3 From e6b989e65849ea7294500a6e156371696f7cbf52 Mon Sep 17 00:00:00 2001 From: Philipp Gesang Date: Thu, 9 May 2013 17:45:50 +0200 Subject: set higher loglevels for database tracing --- luaotfload-tool.lua | 1 + 1 file changed, 1 insertion(+) (limited to 'luaotfload-tool.lua') diff --git a/luaotfload-tool.lua b/luaotfload-tool.lua index 8d7f6f5..0d3b229 100755 --- a/luaotfload-tool.lua +++ b/luaotfload-tool.lua @@ -572,6 +572,7 @@ local process_cmdline = function ( ) -- unit -> jobspec if config.luaotfload.self == "mkluatexfontdb" then action_pending["generate"] = true result.log_level = math.max(2, result.log_level) + logs.set_logout"stdout" end return result end -- cgit v1.2.3