Skip to content

Commit

Permalink
Add luaotfload integration for l3color and l3opacity
Browse files Browse the repository at this point in the history
  • Loading branch information
zauguin committed Feb 26, 2023
1 parent 8c8839a commit 5094cc3
Show file tree
Hide file tree
Showing 3 changed files with 158 additions and 0 deletions.
79 changes: 79 additions & 0 deletions l3backend/l3backend-color.dtx
Expand Up @@ -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
71 changes: 71 additions & 0 deletions l3backend/l3backend-opacity.dtx
Expand Up @@ -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
8 changes: 8 additions & 0 deletions l3backend/l3backend.ins
Expand Up @@ -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}
Expand Down

0 comments on commit 5094cc3

Please sign in to comment.