Hello,

could font.getfont get an option to always return a new font table
instead of returning the current one? While for many fonts a cached
table makes sense, sometimes it would be useful to also have access to
the ooriginal table, e.g. to see which characters are `used`, inspect
the result of `font.addcharacters` or inspect other changes which might
be applied to the underlying font from TeX.

I attached a patch implementing this by adding a `nocache` parameter to
`font.getfont`, such that `font.getfont(true, font_id)` always generates
a new table. IMO `font.getfont(font_id, true)` would look nicer, but
that wouldn't be compatible to older LuaTeX versions which always expect
the font is as last parameter.

Best regards,
Marcel
>From 289f7e85fd2048716f411d0730bb5787f2b56b3c Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Marcel=20Fabian=20Kr=C3=BCger?= <t...@2krueger.de>
Date: Thu, 12 Dec 2019 13:52:53 +0100
Subject: [PATCH] Add `nocache` parameter for font.getfont

---
 manual/luatex-fonts.tex                      | 6 ++++--
 source/texk/web2c/luatexdir/font/luafont.c   | 6 +++---
 source/texk/web2c/luatexdir/lua/lfontlib.c   | 7 ++++---
 source/texk/web2c/luatexdir/lua/luatex-api.h | 2 +-
 4 files changed, 12 insertions(+), 9 deletions(-)

diff --git a/manual/luatex-fonts.tex b/manual/luatex-fonts.tex
index e5b6e1cb2..5eeec2034 100644
--- a/manual/luatex-fonts.tex
+++ b/manual/luatex-fonts.tex
@@ -722,13 +722,15 @@ below for the \type {font.each} iterator.
 The two metatable functions implementing the virtual array are:
 
 \startfunctioncall
-<table> f = font.getfont(<number> n)
+<table> f = font.getfont([<boolean> nocache, ]<number> n)
 font.setfont(<number> n, <table> f)
 \stopfunctioncall
 
 Note that at the moment, each access to the \type {font.fonts} or call to \type
 {font.getfont} creates a \LUA\ table for the whole font unless you cached it.
-This process can be quite slow.
+This process can be quite slow. If the optional nocache parameter is set to
+\type {true} for \type {font.getfont}, a new table will be created even if a
+cached table exists.
 
 \startfunctioncall
 <table> p = font.getparameters(<number> n)
diff --git a/source/texk/web2c/luatexdir/font/luafont.c 
b/source/texk/web2c/luatexdir/font/luafont.c
index 14efbf8a7..1d31847e5 100644
--- a/source/texk/web2c/luatexdir/font/luafont.c
+++ b/source/texk/web2c/luatexdir/font/luafont.c
@@ -432,11 +432,11 @@ static void write_lua_math_parameters(lua_State * L, int 
f)
     lua_rawset(L, -3);
 }
 
-int font_to_lua(lua_State * L, int f)
+int font_to_lua(lua_State * L, int f, boolean bypass_cache)
 {
     int k;
     charinfo *co;
-    if (font_cache_id(f) > 0) {
+    if (!bypass_cache && font_cache_id(f) > 0) {
         /*tex Fetch the table from the registry if it was saved there by 
|font_from_lua|. */
         lua_rawgeti(L, LUA_REGISTRYINDEX, font_cache_id(f));
         return 1;
@@ -523,7 +523,7 @@ int font_to_lua(lua_State * L, int f)
         }
     }
     lua_rawset(L, -3);
-    if (font_cache_id(f) == 0) {
+    if (!bypass_cache && font_cache_id(f) == 0) {
         /*tex Renew the cache. */
         int r;
         lua_pushvalue(L, -1);
diff --git a/source/texk/web2c/luatexdir/lua/lfontlib.c 
b/source/texk/web2c/luatexdir/lua/lfontlib.c
index b3ac2aa53..392f3ca77 100644
--- a/source/texk/web2c/luatexdir/lua/lfontlib.c
+++ b/source/texk/web2c/luatexdir/lua/lfontlib.c
@@ -44,7 +44,7 @@ static int font_read_tfm(lua_State * L)
             if (strlen(cnom)) {
                 internal_font_number f = get_fontid();
                 if (read_tfm_info(f, cnom, s)) {
-                    int k = font_to_lua(L, f);
+                    int k = font_to_lua(L, f, false);
                     delete_font(f);
                     return k;
                 } else {
@@ -118,7 +118,7 @@ static int tex_each_font_next(lua_State * L)
         return 1;
     } else {
         lua_pushinteger(L, i);
-        if (!font_to_lua(L, i))
+        if (!font_to_lua(L, i, false))
             lua_pushnil(L);
         return 2;
     }
@@ -250,8 +250,9 @@ static int nextfontid(lua_State * L)
 
 static int getfont(lua_State * L)
 {
+    boolean bypass_cache = lua_gettop(L) == 2 && lua_toboolean(L, -2);
     int i = luaL_checkinteger(L, -1);
-    if (i && is_valid_font(i) && font_to_lua(L, i))
+    if (i && is_valid_font(i) && font_to_lua(L, i, bypass_cache))
         return 1;
     lua_pushnil(L);
     return 1;
diff --git a/source/texk/web2c/luatexdir/lua/luatex-api.h 
b/source/texk/web2c/luatexdir/lua/luatex-api.h
index bebf088af..44b765f62 100644
--- a/source/texk/web2c/luatexdir/lua/luatex-api.h
+++ b/source/texk/web2c/luatexdir/lua/luatex-api.h
@@ -155,7 +155,7 @@ extern int luaopen_stats(lua_State * L);
 extern int luaopen_font(lua_State * L);
 extern int luaopen_vf(lua_State * L);
 extern int font_parameters_to_lua(lua_State * L, int f);
-extern int font_to_lua(lua_State * L, int f);
+extern int font_to_lua(lua_State * L, int f, int bypass_cache); /* third arg 
is boolean */
 extern int font_from_lua(lua_State * L, int f); /* return is boolean */
 extern int characters_from_lua(lua_State * L, int f); /* return is boolean */
 
-- 
2.24.1

_______________________________________________
dev-luatex mailing list
dev-luatex@ntg.nl
https://mailman.ntg.nl/mailman/listinfo/dev-luatex

Reply via email to