[latex3-commits] [git/LaTeX3-latex3-luaotfload] psname-dev: Allow psname based subfont selection (d976086)

Marcel Fabian Krüger tex at 2krueger.de
Sat Nov 2 23:21:38 CET 2019


Repository : https://github.com/latex3/luaotfload
On branch  : psname-dev
Link       : https://github.com/latex3/luaotfload/commit/d976086375f0488ee6b4ba8376560879eee56665

>---------------------------------------------------------------

commit d976086375f0488ee6b4ba8376560879eee56665
Author: Marcel Fabian Krüger <tex at 2krueger.de>
Date:   Sat Nov 2 03:14:51 2019 +0100

    Allow psname based subfont selection


>---------------------------------------------------------------

d976086375f0488ee6b4ba8376560879eee56665
 src/luaotfload-database.lua  | 38 +++++++++++++++++++++++++++++++++++---
 src/luaotfload-features.lua  | 15 +++++----------
 src/luaotfload-parsers.lua   |  4 +++-
 src/luaotfload-resolvers.lua |  5 +++++
 4 files changed, 48 insertions(+), 14 deletions(-)

diff --git a/src/luaotfload-database.lua b/src/luaotfload-database.lua
index 14b3c18..cbd60fc 100644
--- a/src/luaotfload-database.lua
+++ b/src/luaotfload-database.lua
@@ -3104,6 +3104,39 @@ end
 
 --[[doc--
 
+    Get the subfont index corresponding to a given psname in a
+    font collection
+
+--doc]]--
+
+local function lookup_subfont_index(filepath, psname)
+    assert(name_index)
+    -- if not name_index then name_index = load_names () end
+    local filestatus = name_index.status[filepath]
+    local mappings = name_index.mappings
+    if filestatus then
+        for subfont, idx in next, filestatus.index do
+            if mappings[idx].psname == psname then
+                return subfont or 1
+            end
+        end
+    end
+
+    -- If that didn't work, we do a manual search
+    psname = sanitize_fontname(psname)
+    local err, info = read_font_file (filepath)
+    if #info == 0 then return 1 end
+    for i = 1, #info do
+        for _, names in next, info[i].platformnames do
+            if psname == sanitize_fontname(names.postscriptname) then
+                return i
+            end
+        end
+    end
+end
+
+--[[doc--
+
     collect_font_filenames -- Scan the three search path categories for
     font files. This constitutes the first pass of the update mode.
 
@@ -3713,8 +3746,6 @@ end
 --- PHG: we need to investigate these, maybe they’re useful as early
 ---      hooks
 
-local ignoredfile = function () return false end
-
 local reportmissingbase = function ()
     logreport ("info", 0, "db", --> bug‽
                "Font name database not found but expected by fontloader.")
@@ -3743,7 +3774,7 @@ local resolve = function (name, subfont)
 end
 
 local api = {
-    ignoredfile       = ignoredfile,
+    ignoredfile       = function() return false end,
     reportmissingbase = reportmissingbase,
     reportmissingname = reportmissingname,
     getfilename       = getfilename,
@@ -3776,6 +3807,7 @@ local export = {
     count_font_files            = count_font_files,
     nth_font_filename           = nth_font_filename,
     font_slice                  = font_slice,
+    lookup_subfont_index        = lookup_subfont_index,
     --- font cache
     purge_cache                 = purge_cache,
     erase_cache                 = erase_cache,
diff --git a/src/luaotfload-features.lua b/src/luaotfload-features.lua
index f182532..d4e843d 100644
--- a/src/luaotfload-features.lua
+++ b/src/luaotfload-features.lua
@@ -26,6 +26,7 @@ local lpeg              = require "lpeg"
 local lpegmatch         = lpeg.match
 local P                 = lpeg.P
 local R                 = lpeg.R
+local S                 = lpeg.S
 local C                 = lpeg.C
 
 local table             = table
@@ -338,10 +339,9 @@ end
 local extract_subfont
 do
     local eof         = P(-1)
-    local digit       = R"09"
     --- Theoretically a valid subfont address can be up to ten
-    --- digits long.
-    local sub_expr    = P"(" * C(digit^1) * P")" * eof
+    --- digits long. Additionally we allow names
+    local sub_expr    = P"(" * C((1 - S"()")^1) * P")" * eof
     local full_path   = C(P(1 - sub_expr)^1)
     extract_subfont   = full_path * sub_expr
 end
@@ -371,7 +371,7 @@ local handle_request = function (specification)
         local fullpath, sub = lpegmatch(extract_subfont,
                                         specification.specification)
         if fullpath and sub then
-            specification.sub  = tonumber(sub)
+            specification.sub  = tonumber(sub) or sub
             specification.name = fullpath
         else
             specification.name = specification.specification
@@ -428,12 +428,7 @@ local handle_request = function (specification)
     --- investigated it any further (luatex-fonts-ext), so it will
     --- just stay here.
     features.normal = normalize (request.features)
-    local subfont = tonumber (request.sub)
-    if subfont and subfont >= 0 then
-        specification.sub = subfont + 1
-    else
-        specification.sub = false
-    end
+    specification.sub = request.sub or specification.sub or false
 
     if request.features and request.features.mode
           and fonts.readers[request.features.mode] then
diff --git a/src/luaotfload-parsers.lua b/src/luaotfload-parsers.lua
index b827319..af92905 100644
--- a/src/luaotfload-parsers.lua
+++ b/src/luaotfload-parsers.lua
@@ -603,7 +603,9 @@ local combolist         = Ct(combodef1 * (comborowsep * combodef)^1)
 --- Note to self: subfonts apparently start at index 0. Tested with
 --- Cambria.ttc that includes “Cambria Math” at 0 and “Cambria” at 1.
 --- Other values cause luatex to segfault.
-local subfont           = P"(" * Cg((1 - S"()")^1, "sub") * P")"
+local subfont           = P"(" * Cg(R'09'^1 / function (s)
+                            return tonumber(s) + 1
+                          end + (1 - S"()")^1, "sub") * P")"
 
 --- lookups -----------------------------------------------------------
 local fontname          = C((1-S":(/")^1)  --- like luatex-fonts
diff --git a/src/luaotfload-resolvers.lua b/src/luaotfload-resolvers.lua
index 4be5008..589774d 100644
--- a/src/luaotfload-resolvers.lua
+++ b/src/luaotfload-resolvers.lua
@@ -242,6 +242,7 @@ local function resolve_kpse (specification)
     end
 end
 
+local lookup_subfont_index = fonts.names.lookup_subfont_index
 local function wrap_resolver(resolver)
     return function (specification)
         local filename, sub, forced = resolver(specification)
@@ -254,6 +255,10 @@ local function wrap_resolver(resolver)
             specification.filename = filename
             specification.name = filename
             specification.sub = sub or specification.sub
+            if type(specification.sub) == "string" then
+                specification.sub =
+                    lookup_subfont_index(filename, specification.sub)
+            end
             specification.forced = specification.forced or forced
             if not specification.forced then
                 local suffix = stringlower (filesuffix (filename))





More information about the latex3-commits mailing list