[latex3-commits] [git/LaTeX3-latex3-xxetex] main: refactor: move implementation to Lua (e800e9b)

Henri Menke henri at henrimenke.de
Thu Jan 26 09:46:30 CET 2023


Repository : https://github.com/latex3/xxetex
On branch  : main
Link       : https://github.com/latex3/xxetex/commit/e800e9b32b2561d92ab107ce4e7f5d01bce438f1

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

commit e800e9b32b2561d92ab107ce4e7f5d01bce438f1
Author: Henri Menke <henri at henrimenke.de>
Date:   Thu Jan 26 09:46:30 2023 +0100

    refactor: move implementation to Lua


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

e800e9b32b2561d92ab107ce4e7f5d01bce438f1
 XeTeXemulate.sty | 157 -------------------------------------------------------
 test.tex         |   3 +-
 xxetex.lua       | 155 ++++++++++++++++++++++++++++++++++++++++++++++++++++++
 xxetex.tex       |  11 ++++
 4 files changed, 167 insertions(+), 159 deletions(-)

diff --git a/XeTeXemulate.sty b/XeTeXemulate.sty
deleted file mode 100644
index a65d4db..0000000
--- a/XeTeXemulate.sty
+++ /dev/null
@@ -1,157 +0,0 @@
-\ifcsname ProvidesPackage\endcsname
-  \NeedsTeXFormat{LaTeX2e}
-  \ProvidesPackage{XeTeXemulate}[2018/09/03 v0.0 Emulate the XeTeX font primitives in LuaTeX]
-\fi
-
-\def\XeTeXemulate{%
-  \chardef\XeTeXversion=0
-  \def\XeTeXrevision{.99999}%
-  % Circumvent \outer error
-  \csname newcount\endcsname\XeTeXtracingfonts \XeTeXtracingfonts=0
-  \csname newcount\endcsname\XeTeXuseglyphmetrics \XeTeXuseglyphmetrics=1
-  %
-  \let\XeTeXmathcode\Umathcode
-  % 
-  \protected\def\XeTeXfonttype{%
-    \directlua{
-      local fid = token.scan_int()
-      local tfmdata = font.getfont(fid)
-      local fonttype = tfmdata.format == "opentype" and 2 or 0
-      tex.print([[\numexpr]] .. fonttype .. [[\relax]])
-    }\fontid
-  }%
-  %
-  \protected\def\XeTeXfirstfontchar{%
-    \directlua{
-      local fid = token.scan_int()
-      local tfmdata = font.getfont(fid)
-      local min
-      for slot in pairs(tfmdata.characters) do
-          min = min and (slot < min and slot or min) or slot
-      end
-      tex.print([[\numexpr]] .. min .. [[\relax]])
-    }\fontid
-  }%
-  %
-  \protected\def\XeTeXlastfontchar{%
-    \directlua{
-      local fid = token.scan_int()
-      local tfmdata = font.getfont(fid)
-      local max
-      for slot in pairs(tfmdata.characters) do
-          max = max and (slot > max and slot < 2^16 and slot or max) or slot
-      end
-      tex.print([[\numexpr]] .. max .. [[\relax]])
-    }\fontid
-  }%
-  %
-  \def\XeTeXglyph{%
-    \directlua{
-      local index = token.scan_int()
-      local tfmdata = font.getfont(font.current())
-      if tfmdata.format \string~= "opentype" then
-          tex.error([[Cannot use \string\XeTeXglyph]] .. [[ with ]] ..
-                    tfmdata.name .. [[; not a native platform font]])
-      end
-      for slot, char in pairs(tfmdata.characters) do
-          if char.index == index then
-              tex.print(utf.char(char.unicode))
-              return
-          end
-      end
-    }}%
-  %
-  \protected\def\XeTeXcountglyphs{%
-    \directlua{
-      local fid = token.scan_int()
-      local tfmdata = font.getfont(fid)
-      local count = 0
-      if tfmdata.format == "opentype" then
-          for _ in pairs(tfmdata.characters) do
-              count = count + 1
-          end
-      end
-      tex.print([[\numexpr]] .. count .. [[\relax]])
-    }\fontid
-  }%
-  %
-  \def\XeTeXglyphname{%
-    \directlua{
-      local fid = token.scan_int()
-      local index = token.scan_int()
-      local tfmdata = font.getfont(fid)
-      if tfmdata.format \string~= "opentype" then
-          tex.error([[Cannot use \string\XeTeXglyphname]] .. [[ with ]] ..
-                    tfmdata.name .. [[; not a native platform font]])
-      end
-      local count = 0
-      for slot, char in pairs(tfmdata.shared.rawdata.descriptions) do
-          if char.index == index then
-              tex.print(char.name)
-              return
-          end
-      end
-    }\fontid
-  }%
-  %
-  \protected\def\XeTeXglyphindex{%
-    \directlua{
-      local k = string.gsub(token.scan_string(),'"','')
-      token.scan_keyword(' ') % remove optional space
-      local tfmdata = font.getfont(font.current())
-      if tfmdata.format \string~= "opentype" then
-          tex.error([[Cannot use \string\XeTeXglyphindex]] .. [[ with ]] ..
-                    tfmdata.name .. [[; not a native platform font]])
-      end
-      local index = 0
-      for slot, char in pairs(tfmdata.shared.rawdata.descriptions) do
-          if char.name == k then
-              index = char.index
-              break
-          end
-      end
-      tex.print([[\numexpr]] .. index .. [[\relax]])
-    }}%
-  %
-  \protected\def\XeTeXcharglyph{%
-    \directlua{
-      local id = token.scan_int()
-      local tfmdata = font.getfont(font.current())
-      if tfmdata.format \string~= "opentype" then
-          tex.error([[Cannot use \string\XeTeXcharglyph]] .. [[ with ]] ..
-                    tfmdata.name .. [[; not a native platform font]])
-      end
-      local index = tfmdata.characters[id].index
-      tex.print([[\numexpr]] .. index .. [[\relax]])
-    }}%
-  %
-  \protected\def\XeTeXglyphbounds{%
-    \directlua{
-      local edge = token.scan_int()
-      local slot = token.scan_int()
-      local tfmdata = font.getfont(font.current())
-      if tfmdata.format \string~= "opentype" then
-          tex.error([[Cannot use \string\XeTeXglyphname]] .. [[ with ]] ..
-                    tfmdata.name .. [[; not a native platform font]])
-      end
-      for _,char in pairs(tfmdata.shared.rawdata.descriptions) do
-          if char and char.index == slot then
-              local bbox = char.boundingbox
-              local dimen = 0
-              if edge == 1 then
-                  dimen = bbox and bbox[1] / 100 * 2^16 or 0
-              elseif edge == 2 then
-                  dimen = tfmdata.characters[char.unicode].height
-              elseif edge == 3 then
-                  dimen = bbox and (char.width - bbox[3]) / 100 * 2^16 or 0
-              elseif edge == 4 then
-                  dimen = tfmdata.characters[char.unicode].depth
-              end
-              tex.print([[\dimexpr]] .. tex.round(dimen) .. [[sp\relax]])
-              break
-          end
-      end
-    }}%
-}
-
-\endinput
\ No newline at end of file
diff --git a/test.tex b/test.tex
index f7e782a..794c8e5 100644
--- a/test.tex
+++ b/test.tex
@@ -1,7 +1,6 @@
 \ifdefined\directlua
-  \input XeTeXemulate.sty
+  \input xxetex.tex
   \input luaotfload.sty
-  \XeTeXemulate
 \fi
 
 \font\1="[lmroman10-italic.otf]" at 10pt\1
diff --git a/xxetex.lua b/xxetex.lua
new file mode 100644
index 0000000..75fe925
--- /dev/null
+++ b/xxetex.lua
@@ -0,0 +1,155 @@
+local fontid = token.create("fontid")
+
+local function XeTeXfonttype()
+    token.put_next(fontid)
+    local fid = token.scan_int()
+    local tfmdata = font.getfont(fid)
+    local fonttype = tfmdata.format == "opentype" and 2 or 0
+    tex.print([[\numexpr]] .. fonttype .. [[\relax]])
+end
+
+local function XeTeXfirstfontchar()
+    token.put_next(fontid)
+    local fid = token.scan_int()
+    local tfmdata = font.getfont(fid)
+    local min
+    for slot in pairs(tfmdata.characters) do
+        min = min and (slot < min and slot or min) or slot
+    end
+    tex.print([[\numexpr]] .. min .. [[\relax]])
+end
+
+local function XeTeXlastfontchar()
+    token.put_next(fontid)
+    local fid = token.scan_int()
+    local tfmdata = font.getfont(fid)
+    local max
+    for slot in pairs(tfmdata.characters) do
+        max = max and (slot > max and slot < 2^16 and slot or max) or slot
+    end
+    tex.print([[\numexpr]] .. max .. [[\relax]])
+end
+
+local function XeTeXglyph()
+    local index = token.scan_int()
+    local tfmdata = font.getfont(font.current())
+    if tfmdata.format ~= "opentype"
+        and tfmdata.format ~= "truetype" then
+        tex.error([[Cannot use \XeTeXglyph with ]] ..
+            tfmdata.name .. tfmdata.format  ..
+            [[; not opentype or truetype]])
+    end
+    for slot, char in pairs(tfmdata.characters) do
+        if char.index == index then
+            tex.print(utf.char(char.unicode))
+            return
+        end
+    end
+end
+
+local function XeTeXcountglyphs()
+    token.put_next(fontid)
+    local fid = token.scan_int()
+    local tfmdata = font.getfont(fid)
+    local count = 0
+    if tfmdata.format == "opentype" then
+        for _ in pairs(tfmdata.characters) do
+            count = count + 1
+        end
+    end
+    tex.print([[\numexpr]] .. count .. [[\relax]])
+end
+
+local function XeTeXglyphname()
+    token.put_next(fontid)
+    local fid = token.scan_int()
+    local index = token.scan_int()
+    local tfmdata = font.getfont(fid)
+    if tfmdata.format ~= "opentype" then
+        tex.error([[Cannot use \XeTeXglyphname with ]] ..
+            tfmdata.name .. [[; not a native platform font]])
+    end
+    local count = 0
+    for slot, char in pairs(tfmdata.shared.rawdata.descriptions) do
+        if char.index == index then
+            tex.print(char.name)
+            return
+        end
+    end
+end
+
+local function XeTeXglyphindex()
+    local k = string.gsub(token.scan_string(),'"','')
+    token.scan_keyword(' ') -- remove optional space
+    local tfmdata = font.getfont(font.current())
+    if tfmdata.format ~= "opentype" then
+        tex.error([[Cannot use \XeTeXglyphindex with ]] ..
+            tfmdata.name .. [[; not a native platform font]])
+    end
+    local index = 0
+    for slot, char in pairs(tfmdata.shared.rawdata.descriptions) do
+        if char.name == k then
+            index = char.index
+            break
+        end
+    end
+    tex.print([[\numexpr]] .. index .. [[\relax]])
+end
+
+local function XeTeXcharglyph()
+    local id = token.scan_int()
+    local tfmdata = font.getfont(font.current())
+    if tfmdata.format ~= "opentype" then
+        tex.error([[Cannot use \XeTeXcharglyph with ]] ..
+            tfmdata.name .. [[; not a native platform font]])
+    end
+    local index = tfmdata.characters[id].index
+    tex.print([[\numexpr]] .. index .. [[\relax]])
+end
+
+local function XeTeXglyphbounds()
+    local edge = token.scan_int()
+    local slot = token.scan_int()
+    local tfmdata = font.getfont(font.current())
+    if tfmdata.format ~= "opentype" then
+        tex.error([[Cannot use \XeTeXglyphname with ]] ..
+            tfmdata.name .. [[; not a native platform font]])
+    end
+    for _,char in pairs(tfmdata.shared.rawdata.descriptions) do
+        if char and char.index == slot then
+            local bbox = char.boundingbox
+            local dimen = 0
+            if edge == 1 then
+                dimen = bbox and bbox[1] / 100 * 2^16 or 0
+            elseif edge == 2 then
+                dimen = tfmdata.characters[char.unicode].height
+            elseif edge == 3 then
+                dimen = bbox and (char.width - bbox[3]) / 100 * 2^16 or 0
+            elseif edge == 4 then
+                dimen = tfmdata.characters[char.unicode].depth
+            end
+            tex.print([[\dimexpr]] .. tex.round(dimen) .. [[sp\relax]])
+            break
+        end
+    end
+end
+
+local lft = lua.get_functions_table()
+lft[#lft + 1] = XeTeXfonttype
+token.set_lua("XeTeXfonttype", #lft, "global")
+lft[#lft + 1] = XeTeXfirstfontchar
+token.set_lua("XeTeXfirstfontchar", #lft, "global")
+lft[#lft + 1] = XeTeXlastfontchar
+token.set_lua("XeTeXlastfontchar", #lft, "global")
+lft[#lft + 1] = XeTeXglyph
+token.set_lua("XeTeXglyph", #lft, "global", "protected")
+lft[#lft + 1] = XeTeXcountglyphs
+token.set_lua("XeTeXcountglyphs", #lft, "global")
+lft[#lft + 1] = XeTeXglyphname
+token.set_lua("XeTeXglyphname", #lft, "global", "protected")
+lft[#lft + 1] = XeTeXglyphindex
+token.set_lua("XeTeXglyphindex", #lft, "global")
+lft[#lft + 1] = XeTeXcharglyph
+token.set_lua("XeTeXcharglyph", #lft, "global")
+lft[#lft + 1] = XeTeXglyphbounds
+token.set_lua("XeTeXglyphbounds", #lft, "global")
diff --git a/xxetex.tex b/xxetex.tex
new file mode 100644
index 0000000..d3b4738
--- /dev/null
+++ b/xxetex.tex
@@ -0,0 +1,11 @@
+\chardef\XeTeXversion=0
+\def\XeTeXrevision{.99999}%
+
+\newcount\XeTeXtracingfonts \XeTeXtracingfonts=0
+\newcount\XeTeXuseglyphmetrics \XeTeXuseglyphmetrics=1
+
+\let\XeTeXmathcode\Umathcode
+
+\directlua{dofile("xxetex.lua")}
+
+\endinput
\ No newline at end of file





More information about the latex3-commits mailing list.