[latex3-commits] [git/LaTeX3-latex3-latex3] l3color-luaotfload: Add luaotfload integration for l3color and l3opacity (5094cc3eb)

Marcel Fabian Krüger tex at 2krueger.de
Sun Feb 26 20:44:23 CET 2023


Repository : https://github.com/latex3/latex3
On branch  : l3color-luaotfload
Link       : https://github.com/latex3/latex3/commit/5094cc3ebf726d5f56cc6fc6354ff5e1fa606dda

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

commit 5094cc3ebf726d5f56cc6fc6354ff5e1fa606dda
Author: Marcel Fabian Krüger <tex at 2krueger.de>
Date:   Sun Feb 26 15:32:07 2023 +0100

    Add luaotfload integration for l3color and l3opacity


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

5094cc3ebf726d5f56cc6fc6354ff5e1fa606dda
 l3backend/l3backend-color.dtx   | 79 +++++++++++++++++++++++++++++++++++++++++
 l3backend/l3backend-opacity.dtx | 71 ++++++++++++++++++++++++++++++++++++
 l3backend/l3backend.ins         |  8 +++++
 3 files changed, 158 insertions(+)

diff --git a/l3backend/l3backend-color.dtx b/l3backend/l3backend-color.dtx
index de29926f0..95a67ce3d 100644
--- a/l3backend/l3backend-color.dtx
+++ b/l3backend/l3backend-color.dtx
@@ -1264,6 +1264,85 @@
 %</package>
 %    \end{macrocode}
 %
+% \subsection{Font handling integration}
+%
+% In \LuaTeX{} these colors should also be usable to color fonts, so
+% \texttt{luaotfload} color handling is extended to include these.
+%
+%    \begin{macrocode}
+%<*luatex>
+%    \end{macrocode}
+%
+%    \begin{macrocode}
+%<*lua>
+%    \end{macrocode}
+%
+%    \begin{macrocode}
+local l = lpeg
+local spaces = l.P' '^0
+local digit16 = l.R('09', 'af', 'AF')
+
+local octet = digit16 * digit16 / function(s)
+  return string.format('%.3g ', tonumber(s, 16) / 255)
+end
+
+if luaotfload and luaotfload.set_transparent_colorstack then
+  local htmlcolor = l.Cs(octet * octet * octet * -1 * l.Cc'rg')
+  local color_export = {
+    token.create'endlocalcontrol',
+    token.create'tex_hpack:D',
+    token.new(0, 1),
+    token.create'color_export:nnN',
+    token.new(0, 1),
+    '',
+    token.new(0, 2),
+    token.new(0, 1),
+    'backend',
+    token.new(0, 2),
+    token.create'l_tmpa_tl',
+    token.create'exp_after:wN',
+    token.create'__color_select:nn',
+    token.create'l_tmpa_tl',
+    token.new(0, 2),
+  }
+  local group_end = token.create'group_end:'
+  local value = (1 - l.P'}')^0
+  luatexbase.add_to_callback('luaotfload.parse_color', function (value)
+% Also allow HTML colors to preserve compatibility
+    local html = htmlcolor:match(value)
+    if html then return html end
+
+    tex.runtoks(function()
+      token.get_next()
+      color_export[6] = value
+      tex.sprint(-2, color_export)
+    end)
+    local list = token.scan_list()
+    if not list.head or list.head.next
+        or list.head.subtype ~= node.subtype'pdf_colorstack' then
+      error'Unexpected backend behavior'
+    end
+    local cmd = list.head.data
+    node.free(list)
+    return cmd
+  end, 'l3color')
+end
+%    \end{macrocode}
+%
+%    \begin{macrocode}
+%</lua>
+%    \end{macrocode}
+%
+%    \begin{macrocode}
+%<*package>
+\lua_load_module:n {l3backend-luatex}
+%</package>
+%    \end{macrocode}
+%
+%    \begin{macrocode}
+%</luatex>
+%    \end{macrocode}
+%
 % \end{implementation}
 %
 % \PrintIndex
diff --git a/l3backend/l3backend-opacity.dtx b/l3backend/l3backend-opacity.dtx
index 5d1f8778c..07a3c40d8 100644
--- a/l3backend/l3backend-opacity.dtx
+++ b/l3backend/l3backend-opacity.dtx
@@ -287,6 +287,77 @@
 %</package>
 %    \end{macrocode}
 %
+% \subsection{Font handling integration}
+%
+% In \LuaTeX{} we want to use these functions also for transparent fonts
+% to avoid interference between both uses of transparency.
+%
+%    \begin{macrocode}
+%<*luatex>
+%    \end{macrocode}
+%
+%    \begin{macrocode}
+%<*lua>
+%    \end{macrocode}
+%
+% First we need to check if pdfmanagement is active from Lua.
+%    \begin{macrocode}
+local pdfmanagement_active do
+  local pdfmanagement_if_active_p = token.create'pdfmanagement_if_active_p:'
+  local cmd = pdfmanagement_if_active_p.cmdname
+  if cmd == 'undefined_cs' then
+    pdfmanagement_active = false
+  else
+    token.put_next(pdfmanagement_if_active_p)
+    pdfmanagement_active = token.scan_int() ~= 0
+  end
+end
+
+if pdfmanagement_active and luaotfload and luaotfload.set_transparent_colorstack then
+  luaotfload.set_transparent_colorstack(token.create'c__opacity_backend_stack_int'.index)
+
+  local transparent_register = {
+    token.create'pdfmanagement_add:nnn',
+    token.new(0, 1),
+      'Page/Resources/ExtGState',
+    token.new(0, 2),
+    token.new(0, 1),
+      '',
+    token.new(0, 2),
+    token.new(0, 1),
+      '<</ca ',
+      '',
+      '/CA ',
+      '',
+      '>>',
+    token.new(0, 2),
+  }
+  luatexbase.add_to_callback('luaotfload.parse_transparent', function(value)
+    value = (octet * -1):match(value)
+    if not value then
+      tex.error'Invalid transparency value'
+      return
+    end
+    value = value:sub(1, -2)
+    local result = 'opacity' .. value
+    tex.runtoks(function()
+      transparent_register[6], transparent_register[10], transparent_register[12] = result, value, value
+      tex.sprint(-2, transparent_register)
+    end)
+    return '/' .. result .. ' gs'
+  end, 'l3opacity')
+end
+%    \end{macrocode}
+%
+%    \begin{macrocode}
+%</lua>
+%    \end{macrocode}
+%
+%    \begin{macrocode}
+%</luatex>
+%    \end{macrocode}
+%
+%
 % \end{implementation}
 %
 % \PrintIndex
diff --git a/l3backend/l3backend.ins b/l3backend/l3backend.ins
index cfa8ece98..510cdff6d 100644
--- a/l3backend/l3backend.ins
+++ b/l3backend/l3backend.ins
@@ -110,6 +110,14 @@ and all files in that bundle must be distributed together.
         \from{l3backend-opacity.dtx} {package,luatex}
       }
   }
+\generate
+  {
+    \file{l3backend-luatex.lua}
+      {
+        \from{l3backend-color.dtx}   {lua,luatex}
+        \from{l3backend-opacity.dtx} {lua,luatex}
+      }
+  }
 \generate
   {
     \file{l3backend-pdftex.def}





More information about the latex3-commits mailing list.