| 1 |
-- |
| 2 |
-- This is file `l3backend-luatex.lua', |
| 3 |
-- generated with the docstrip utility. |
| 4 |
-- |
| 5 |
-- The original source files were: |
| 6 |
-- |
| 7 |
-- l3backend-color.dtx (with options: `lua') |
| 8 |
-- l3backend-opacity.dtx (with options: `lua') |
| 9 |
-- |
| 10 |
-- Copyright (C) 2023-2026 The LaTeX Project |
| 11 |
-- |
| 12 |
-- It may be distributed and/or modified under the conditions of |
| 13 |
-- the LaTeX Project Public License (LPPL), either version 1.3c of |
| 14 |
-- this license or (at your option) any later version. The latest |
| 15 |
-- version of this license is in the file: |
| 16 |
-- |
| 17 |
-- https://www.latex-project.org/lppl.txt |
| 18 |
-- |
| 19 |
-- This file is part of the "l3backend bundle" (The Work in LPPL) |
| 20 |
-- and all files in that bundle must be distributed together. |
| 21 |
-- |
| 22 |
-- File: l3backend-color.dtx |
| 23 |
local l = lpeg |
| 24 |
local spaces = l.P' '^0 |
| 25 |
local digit16 = l.R('09', 'af', 'AF') |
| 26 |
|
| 27 |
local octet = digit16 * digit16 / function(s) |
| 28 |
return string.format('%.3g ', tonumber(s, 16) / 255) |
| 29 |
end |
| 30 |
|
| 31 |
if luaotfload and luaotfload.set_transparent_colorstack then |
| 32 |
local htmlcolor = l.Cs(octet * octet * octet * -1 * l.Cc'rg') |
| 33 |
local color_export = { |
| 34 |
token.create'tex_endlocalcontrol:D', |
| 35 |
token.create'tex_hpack:D', |
| 36 |
token.new(0, 1), |
| 37 |
token.create'color_export:nnN', |
| 38 |
token.new(0, 1), |
| 39 |
'', |
| 40 |
token.new(0, 2), |
| 41 |
token.new(0, 1), |
| 42 |
'backend', |
| 43 |
token.new(0, 2), |
| 44 |
token.create'l_tmpa_tl', |
| 45 |
token.create'exp_after:wN', |
| 46 |
token.create'__color_select:nn', |
| 47 |
token.create'l_tmpa_tl', |
| 48 |
token.new(0, 2), |
| 49 |
} |
| 50 |
local group_end = token.create'group_end:' |
| 51 |
local value = (1 - l.P'}')^0 |
| 52 |
luatexbase.add_to_callback('luaotfload.parse_color', function (value) |
| 53 |
local html = htmlcolor:match(value) |
| 54 |
if html then return html end |
| 55 |
|
| 56 |
local l3color_prop = token.get_macro(string.format('l__color_named_%s_prop', value)) |
| 57 |
if l3color_prop == nil or l3color_prop == '' then |
| 58 |
local legacy_color_macro = token.create(string.format('\\color@%s', value)) |
| 59 |
if legacy_color_macro.cmdname ~= 'undefined_cs' then |
| 60 |
token.put_next(legacy_color_macro) |
| 61 |
return token.scan_argument() |
| 62 |
end |
| 63 |
end |
| 64 |
|
| 65 |
tex.runtoks(function() |
| 66 |
token.get_next() |
| 67 |
color_export[6] = value |
| 68 |
tex.sprint(-2, color_export) |
| 69 |
end) |
| 70 |
local list = token.scan_list() |
| 71 |
if not list.head or list.head.next |
| 72 |
or list.head.subtype ~= node.subtype'pdf_colorstack' then |
| 73 |
error'Unexpected backend behavior' |
| 74 |
end |
| 75 |
local cmd = list.head.data |
| 76 |
node.free(list) |
| 77 |
return cmd |
| 78 |
end, 'l3color') |
| 79 |
end |
| 80 |
-- File: l3backend-opacity.dtx |
| 81 |
local pdfmanagement_active do |
| 82 |
local pdfmanagement_if_active_p = token.create'pdfmanagement_if_active_p:' |
| 83 |
local cmd = pdfmanagement_if_active_p.cmdname |
| 84 |
if cmd == 'undefined_cs' then |
| 85 |
pdfmanagement_active = false |
| 86 |
else |
| 87 |
token.put_next(pdfmanagement_if_active_p) |
| 88 |
pdfmanagement_active = token.scan_int() ~= 0 |
| 89 |
end |
| 90 |
end |
| 91 |
|
| 92 |
if pdfmanagement_active and luaotfload and luaotfload.set_transparent_colorstack then |
| 93 |
luaotfload.set_transparent_colorstack(function() return token.create'c__opacity_backend_stack_int'.index end) |
| 94 |
|
| 95 |
local transparent_register = { |
| 96 |
token.create'pdfmanagement_add:nnn', |
| 97 |
token.new(0, 1), |
| 98 |
'Page/Resources/ExtGState', |
| 99 |
token.new(0, 2), |
| 100 |
token.new(0, 1), |
| 101 |
'', |
| 102 |
token.new(0, 2), |
| 103 |
token.new(0, 1), |
| 104 |
'<</ca ', |
| 105 |
'', |
| 106 |
'/CA ', |
| 107 |
'', |
| 108 |
'>>', |
| 109 |
token.new(0, 2), |
| 110 |
} |
| 111 |
luatexbase.add_to_callback('luaotfload.parse_transparent', function(value) |
| 112 |
value = (octet * -1):match(value) |
| 113 |
if not value then |
| 114 |
tex.error'Invalid transparency value' |
| 115 |
return |
| 116 |
end |
| 117 |
value = value:sub(1, -2) |
| 118 |
local result = 'opacity' .. value |
| 119 |
tex.runtoks(function() |
| 120 |
transparent_register[6], transparent_register[10], transparent_register[12] = result, value, value |
| 121 |
tex.sprint(-2, transparent_register) |
| 122 |
end) |
| 123 |
return '/' .. result .. ' gs' |
| 124 |
end, 'l3opacity') |
| 125 |
end |