[latex3-commits] [git/LaTeX3-latex3-luaotfload] dev: Avoid globals and use module returns instead (8ca7c75)

Marcel Fabian Krüger tex at 2krueger.de
Sun Feb 9 15:24:46 CET 2020


Repository : https://github.com/latex3/luaotfload
On branch  : dev
Link       : https://github.com/latex3/luaotfload/commit/8ca7c7501db5e92f970d0efb4fe8be49d806c418

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

commit 8ca7c7501db5e92f970d0efb4fe8be49d806c418
Author: Marcel Fabian Krüger <tex at 2krueger.de>
Date:   Sun Feb 9 15:24:46 2020 +0100

    Avoid globals and use module returns instead


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

8ca7c7501db5e92f970d0efb4fe8be49d806c418
 src/luaotfload-fallback.lua    | 10 +++++++---
 src/luaotfload-init.lua        |  9 +++++++--
 src/luaotfload-main.lua        | 12 ++++++++----
 src/luaotfload-multiscript.lua |  9 +++++++--
 4 files changed, 29 insertions(+), 11 deletions(-)

diff --git a/src/luaotfload-fallback.lua b/src/luaotfload-fallback.lua
index 844941a..e9dd913 100644
--- a/src/luaotfload-fallback.lua
+++ b/src/luaotfload-fallback.lua
@@ -26,7 +26,7 @@ local protect_glyph      = node.direct.protect_glyph
 local otffeatures        = fonts.constructors.newfeatures "otf"
 -- local normalize          = fonts.handlers.otf.features.normalize
 local definers           = fonts.definers
-local define_font        = luaotfload.define_font
+local define_font --     = luaotfload.define_font % This is set when the first font is loaded.
 
 local fallback_table_fontnames = {}
 
@@ -89,8 +89,8 @@ local function makefallbackfont(tfmdata, _, fallback)
 end
 
 local glyph_id = node.id'glyph'
--- TODO: unset last_script, matching parentheses etc
-function dofallback(head, _, _, _, direction)
+-- TODO: inherited fonts (combining accents etc.)
+local function dofallback(head, _, _, _, direction)
   head = node.direct.todirect(head)
   local last_fid, last_fallbacks
   for cur, cid, fid in traverse_char(head) do
@@ -107,6 +107,7 @@ function dofallback(head, _, _, _, direction)
 end
 
 function luaotfload.add_fallback(name, fonts)
+  define_font = define_font or luaotfload.define_font -- Lazy loading because this file get's loaded before define_font is defined
   if fonts == nil then
     fonts = name
     name = #fallback_table_fontnames + 1
@@ -131,4 +132,7 @@ otffeatures.register {
   -- }
 }
 
+return {
+  process = dofallback,
+}
 --- vim:sw=2:ts=2:expandtab:tw=71
diff --git a/src/luaotfload-init.lua b/src/luaotfload-init.lua
index 1f4b035..b4456c5 100644
--- a/src/luaotfload-init.lua
+++ b/src/luaotfload-init.lua
@@ -448,14 +448,19 @@ local init_post_install_callbacks = function ()
 
   --doc]]--
 
+  -- The order is important here: multiscript=auto needs to look at the
+  -- fallback fonts, so they already have to be processed at that stage
+  local fallback = luaotfload.loaders.luaotfload "fallback".process
+  local multiscript = luaotfload.loaders.luaotfload "multiscript".process
+
   -- MK Pass current text direction to simple_font_handler
   local handler = luaotfload.fontloader.nodes.simple_font_handler
   local callback = function(head, groupcode, _, _, direction)
     if not direction then
       direction = tex.get'textdir'
     end
-    domultiscript(head, nil, nil, nil, direction)
-    dofallback(head, nil, nil, nil, direction)
+    multiscript(head, nil, nil, nil, direction)
+    fallback(head, nil, nil, nil, direction)
     return handler(head, groupcode, nil, nil, direction)
   end
   luatexbase.add_to_callback("pre_linebreak_filter",
diff --git a/src/luaotfload-main.lua b/src/luaotfload-main.lua
index 02bfccd..0b31e90 100644
--- a/src/luaotfload-main.lua
+++ b/src/luaotfload-main.lua
@@ -294,6 +294,14 @@ luaotfload.main = function ()
     local initialize = loaders.initialize
 
     local starttime = osgettimeofday ()
+
+    -- Feature detect HarfBuzz. This is done early to allow easy HarfBuzz
+    -- detection in other modules
+    local harfstatus, harfbuzz = pcall(require, 'luaharfbuzz')
+    if harfstatus then
+        luaotfload.harfbuzz = harfbuzz
+    end
+
     local init      = loadmodule "init" --- fontloader initialization
     init (function ()
 
@@ -319,15 +327,11 @@ luaotfload.main = function ()
     loadmodule "letterspace"  --- extra character kerning
     loadmodule "embolden"     --- fake bold
     loadmodule "notdef"       --- missing glyph handling
-    local harfstatus, harfbuzz = pcall(require, 'luaharfbuzz')
     if harfstatus then
-        luaotfload.harfbuzz = harfbuzz
         loadmodule "harf-define"
         loadmodule "harf-plug"
     end
     initialize "auxiliary"    --- additional high-level functionality
-    loadmodule "fallback"  --- ...
-    loadmodule "multiscript"  --- ...
     loadmodule "tounicode"
 
     luaotfload.aux.start_rewrite_fontname () --- to be migrated to fontspec
diff --git a/src/luaotfload-multiscript.lua b/src/luaotfload-multiscript.lua
index c359dbf..b757e26 100644
--- a/src/luaotfload-multiscript.lua
+++ b/src/luaotfload-multiscript.lua
@@ -28,7 +28,7 @@ local remove             = node.direct.remove
 local otffeatures        = fonts.constructors.newfeatures "otf"
 -- local normalize          = fonts.handlers.otf.features.normalize
 local definers           = fonts.definers
-local define_font        = luaotfload.define_font
+local define_font --     = luaotfload.define_font % This is set when the first font is loaded.
 local scripts_lib        = require'luaotfload-scripts'.script
 local script_to_iso      = scripts_lib.to_iso
 local script_to_ot       = scripts_lib.to_ot
@@ -218,6 +218,7 @@ local function is_dominant_script(scripts, script, first, ...)
 end
 
 local function makecombifont(tfmdata, _, additional_scripts)
+  define_font = define_font or luaotfload.define_font -- Lazy loading because this file get's loaded before define_font is defined
   local has_auto
   additional_scripts = tostring(additional_scripts)
   if additional_scripts:sub(1, 5) == "auto+" then
@@ -321,7 +322,7 @@ end
 
 local glyph_id = node.id'glyph'
 -- TODO: unset last_script, matching parentheses etc
-function domultiscript(head, _, _, _, direction)
+local function domultiscript(head, _, _, _, direction)
   head = node.direct.todirect(head)
   local last_fid, last_fonts, last_script
   for cur, cid, fid in traverse_char(head) do
@@ -397,4 +398,8 @@ otffeatures.register {
   -- }
 }
 
+return {
+  -- I would omit the table if that wouldn't cause the function to be used as initializer
+  process = domultiscript,
+}
 --- vim:sw=2:ts=2:expandtab:tw=71





More information about the latex3-commits mailing list.