Debian Codegraph @

texmf-dist/source/latex/l3backend/l3backend-opacity.dtx

raw
1 % \iffalse meta-comment
2 %
3 %% File: l3backend-opacity.dtx
4 %
5 % Copyright (C) 2021-2026 The LaTeX Project
6 %
7 % It may be distributed and/or modified under the conditions of the
8 % LaTeX Project Public License (LPPL), either version 1.3c of this
9 % license or (at your option) any later version. The latest version
10 % of this license is in the file
11 %
12 % https://www.latex-project.org/lppl.txt
13 %
14 % This file is part of the "l3backend bundle" (The Work in LPPL)
15 % and all files in that bundle must be distributed together.
16 %
17 % -----------------------------------------------------------------------
18 %
19 % The development version of the bundle can be found at
20 %
21 % https://github.com/latex3/latex3
22 %
23 % for those people who are interested.
24 %
25 %<*driver>
26 \documentclass[full,kernel]{l3doc}
27 \begin{document}
28 \DocInput{\jobname.dtx}
29 \end{document}
30 %</driver>
31 % \fi
32 %
33 % \title{^^A
34 % The \pkg{l3backend-opacity} module\\ Backend opacity support^^A
35 % }
36 %
37 % \author{^^A
38 % The \LaTeX{} Project\thanks
39 % {^^A
40 % E-mail:
41 % \href{mailto:[email protected]}
42 % {[email protected]}^^A
43 % }^^A
44 % }
45 %
46 % \date{Released 2026-02-18}
47 %
48 % \maketitle
49 %
50 % \begin{documentation}
51 %
52 % \end{documentation}
53 %
54 % \begin{implementation}
55 %
56 % \section{\pkg{l3backend-opacity} implementation}
57 %
58 % \begin{macrocode}
59 %<*package>
60 %<@@=opacity>
61 % \end{macrocode}
62 %
63 % Although opacity is not color, it needs to be managed in a somewhat
64 % similar way: using a dedicated stack if possible. Depending on the backend,
65 % that may not be possible. There is also the need to cover fill/stroke setting
66 % as well as more general running opacity. It is easiest to describe the value
67 % used in terms of opacity, although commonly this is referred to as
68 % transparency.
69 %
70 % \begin{macrocode}
71 %<*dvips>
72 % \end{macrocode}
73 %
74 % \begin{macro}{\@@_backend_select:n}
75 % \begin{macro}{\@@_backend_fill:n, \@@_backend_stroke:n}
76 % \begin{macro}{\@@_backend:nnn}
77 % \begin{macro}
78 % {\@@_backend_reset:, \@@_backend_reset_fill:, \@@_backend_reset_stroke:}
79 % No stack so set values directly. The need to deal with Distiller and
80 % Ghostscript separately means we use a common auxiliary: the two
81 % systems require different PostScript for transparency. This is
82 % of course not quite as efficient as doing one test for setting all
83 % transparency, but it keeps things clearer here. Thanks to Alex Grahn
84 % for the detail on testing for GhostScript.
85 % \begin{macrocode}
86 \cs_new_protected:Npn \@@_backend_select:n #1
87 {
88 \@@_backend:nnn {#1} { fill } { ca }
89 \@@_backend:nnn {#1} { stroke } { CA }
90 }
91 \cs_new_protected:Npn \@@_backend_fill:n #1
92 {
93 \@@_backend:nnn
94 { #1 }
95 { fill }
96 { ca }
97 }
98 \cs_new_protected:Npn \@@_backend_stroke:n #1
99 {
100 \@@_backend:nnn
101 { #1 }
102 { stroke }
103 { CA }
104 }
105 \cs_new_protected:Npn \@@_backend:nnn #1#2#3
106 {
107 \__kernel_backend_postscript:n
108 {
109 product ~ (Ghostscript) ~ search
110 {
111 pop ~ pop ~ pop ~
112 #1 ~ .set #2 constantalpha
113 }
114 {
115 pop ~
116 mark ~
117 /#3 ~ #1
118 /SetTransparency ~
119 pdfmark
120 }
121 ifelse
122 }
123 }
124 \cs_new_protected:Npn \@@_backend_reset:
125 {
126 \@@_backend_reset_fill:
127 \@@_backend_reset_stroke:
128 }
129 \cs_new_protected:Npn \@@_backend_reset_fill:
130 {
131 \@@_backend:nnn
132 { 1 }
133 { fill }
134 { ca }
135 }
136 \cs_new_protected:Npn \@@_backend_reset_stroke:
137 {
138 \@@_backend:nnn
139 { 1 }
140 { stroke }
141 { CA }
142 }
143 % \end{macrocode}
144 % \end{macro}
145 % \end{macro}
146 % \end{macro}
147 % \end{macro}
148 %
149 % \begin{macrocode}
150 %</dvips>
151 % \end{macrocode}
152 %
153 % \begin{macrocode}
154 %<*dvipdfmx|luatex|pdftex|xetex>
155 % \end{macrocode}
156 %
157 % \begin{variable}{\c_@@_backend_stack_int}
158 % Set up a stack, where that is applicable.
159 % \begin{macrocode}
160 \bool_lazy_and:nnT
161 { \cs_if_exist_p:N \pdfmanagement_if_active_p: }
162 { \pdfmanagement_if_active_p: }
163 {
164 %<*luatex|pdftex>
165 \__kernel_color_backend_stack_init:Nnn \c_@@_backend_stack_int
166 { page ~ direct } { /opacity 1 ~ gs }
167 %</luatex|pdftex>
168 \pdfmanagement_add:nnn { Page / Resources / ExtGState }
169 { opacity 1 } { << /ca ~ 1 /CA ~ 1 >> }
170 }
171 % \end{macrocode}
172 % \end{variable}
173 %
174 % \begin{variable}{\l_@@_backend_fill_tl, \l_@@_backend_stroke_tl}
175 % We use |tl| here for speed: at the backend, this should be reasonable.
176 % Both need to start off fully opaque.
177 % \begin{macrocode}
178 \tl_new:N \l_@@_backend_fill_tl
179 \tl_new:N \l_@@_backend_stroke_tl
180 \tl_set:Nn \l_@@_backend_fill_tl { 1 }
181 \tl_set:Nn \l_@@_backend_stroke_tl { 1 }
182 % \end{macrocode}
183 % \end{variable}
184 %
185 % \begin{macro}{\@@_backend_select:n}
186 % \begin{macro}
187 % {\@@_backend_reset:, \@@_backend_reset_fill:, \@@_backend_reset_stroke:}
188 % Much the same as color.
189 % \begin{macrocode}
190 \cs_new_protected:Npn \@@_backend_select:n #1
191 {
192 \tl_set:Nn \l_@@_backend_fill_tl {#1}
193 \tl_set:Nn \l_@@_backend_stroke_tl {#1}
194 \pdfmanagement_add:nnn { Page / Resources / ExtGState }
195 { opacity #1 }
196 { << /ca ~ #1 /CA ~ #1 >> }
197 %<*dvipdfmx|xetex>
198 \__kernel_backend_literal_pdf:n
199 %</dvipdfmx|xetex>
200 %<*luatex|pdftex>
201 \__kernel_color_backend_stack_push:nn \c_@@_backend_stack_int
202 %</luatex|pdftex>
203 { /opacity #1 ~ gs }
204 }
205 \cs_new_protected:Npn \@@_backend_reset:
206 {
207 %<*dvipdfmx|xetex>
208 \__kernel_backend_literal_pdf:n
209 { /opacity1 ~ gs }
210 %</dvipdfmx|xetex>
211 %<*luatex|pdftex>
212 \__kernel_color_backend_stack_pop:n \c_@@_backend_stack_int
213 %</luatex|pdftex>
214 }
215 \cs_new_eq:NN \@@_backend_reset_fill: \@@_backend_reset:
216 \cs_new_eq:NN \@@_backend_reset_stroke: \@@_backend_reset:
217 % \end{macrocode}
218 % \end{macro}
219 % \end{macro}
220 %
221 % \begin{macro}{\@@_backend_fill:n, \@@_backend_stroke:n}
222 % \begin{macro}{\@@_backend_fill_stroke:nn}
223 % For separate fill and stroke, we need to work out if we need to do
224 % more work or if we can stick to a single setting.
225 % \begin{macrocode}
226 \cs_new_protected:Npn \@@_backend_fill:n #1
227 {
228 \exp_args:Nno \@@_backend_fill_stroke:nn
229 { #1 }
230 { \l_@@_backend_stroke_tl }
231 }
232 \cs_new_protected:Npn \@@_backend_stroke:n #1
233 {
234 \exp_args:No \@@_backend_fill_stroke:nn
235 { \l_@@_backend_fill_tl }
236 { #1 }
237 }
238 \cs_new_protected:Npn \@@_backend_fill_stroke:nn #1#2
239 {
240 \str_if_eq:nnTF {#1} {#2}
241 { \@@_backend_select:n {#1} }
242 {
243 \tl_set:Nn \l_@@_backend_fill_tl {#1}
244 \tl_set:Nn \l_@@_backend_stroke_tl {#2}
245 \pdfmanagement_add:nnn { Page / Resources / ExtGState }
246 { opacity.fill #1 }
247 { << /ca ~ #1 >> }
248 \pdfmanagement_add:nnn { Page / Resources / ExtGState }
249 { opacity.stroke #2 }
250 { << /CA ~ #2 >> }
251 %<*dvipdfmx|xetex>
252 \__kernel_backend_literal_pdf:n
253 %</dvipdfmx|xetex>
254 %<*luatex|pdftex>
255 \__kernel_color_backend_stack_push:nn \c_@@_backend_stack_int
256 %</luatex|pdftex>
257 { /opacity.fill #1 ~ gs /opacity.stroke #2 ~ gs }
258 }
259 }
260 % \end{macrocode}
261 % \end{macro}
262 % \end{macro}
263 %
264 % \begin{macro}{\@@_backend_select:n, \@@_backend_fill_stroke:nn}
265 % \begin{macro}
266 % {\@@_backend_reset:, \@@_backend_reset_fill:, \@@_backend_reset_stroke:}
267 % Redefine them to stubs if pdfmanagement is either not loaded or
268 % deactivated.
269 % \begin{macrocode}
270 \bool_lazy_and:nnF
271 { \cs_if_exist_p:N \pdfmanagement_if_active_p: }
272 { \pdfmanagement_if_active_p: }
273 {
274 \cs_gset_protected:Npn \@@_backend_select:n #1 { }
275 \cs_gset_protected:Npn \@@_backend_fill_stroke:nn #1#2 { }
276 \cs_gset_protected:Npn \@@_backend_reset: { }
277 \cs_gset_eq:NN \@@_backend_reset_fill: \@@_backend_reset:
278 \cs_gset_eq:NN \@@_backend_reset_stroke: \@@_backend_reset:
279 }
280 % \end{macrocode}
281 % \end{macro}
282 % \end{macro}
283 %
284 % \begin{macrocode}
285 %</dvipdfmx|luatex|pdftex|xetex>
286 % \end{macrocode}
287 %
288 % \begin{macrocode}
289 %<*dvisvgm>
290 % \end{macrocode}
291 %
292 % \begin{macro}{\@@_backend_select:n, \@@_backend_fill:n, \@@_backend_stroke:n}
293 % \begin{macro}{\@@_backend:nn}
294 % \begin{macro}
295 % {\@@_backend_reset:, \@@_backend_reset_fill:, \@@_backend_reset_stroke:}
296 % Once again, we use a scope here. There is a general opacity function for
297 % SVG, but that is of course not set up using the stack.
298 % \begin{macrocode}
299 \cs_new_protected:Npn \@@_backend_select:n #1
300 { \@@_backend:nn {#1} { } }
301 \cs_new_protected:Npn \@@_backend_fill:n #1
302 { \@@_backend:nn {#1} { fill- } }
303 \cs_new_protected:Npn \@@_backend_stroke:n #1
304 { \@@_backend:nn {#1} { stroke- } }
305 \cs_new_protected:Npn \@@_backend:nn #1#2
306 { \__kernel_backend_scope:e { #2 opacity = " #1 " } }
307 \cs_new_protected:Npn \@@_backend_reset: { }
308 \cs_new_eq:NN \@@_backend_reset_fill: \@@_backend_reset:
309 \cs_new_eq:NN \@@_backend_reset_stroke: \@@_backend_reset:
310 % \end{macrocode}
311 % \end{macro}
312 % \end{macro}
313 % \end{macro}
314 %
315 % \begin{macrocode}
316 %</dvisvgm>
317 % \end{macrocode}
318 %
319 % \begin{macrocode}
320 %</package>
321 % \end{macrocode}
322 %
323 % \subsection{Font handling integration}
324 %
325 % In \LuaTeX{} we want to use these functions also for transparent fonts
326 % to avoid interference between both uses of transparency.
327 %
328 % \begin{macrocode}
329 %<*lua>
330 % \end{macrocode}
331 %
332 % First we need to check if pdfmanagement is active from Lua.
333 % \begin{macrocode}
334 local pdfmanagement_active do
335 local pdfmanagement_if_active_p = token.create'pdfmanagement_if_active_p:'
336 local cmd = pdfmanagement_if_active_p.cmdname
337 if cmd == 'undefined_cs' then
338 pdfmanagement_active = false
339 else
340 token.put_next(pdfmanagement_if_active_p)
341 pdfmanagement_active = token.scan_int() ~= 0
342 end
343 end
344
345 if pdfmanagement_active and luaotfload and luaotfload.set_transparent_colorstack then
346 luaotfload.set_transparent_colorstack(function() return token.create'c_@@_backend_stack_int'.index end)
347
348 local transparent_register = {
349 token.create'pdfmanagement_add:nnn',
350 token.new(0, 1),
351 'Page/Resources/ExtGState',
352 token.new(0, 2),
353 token.new(0, 1),
354 '',
355 token.new(0, 2),
356 token.new(0, 1),
357 '<</ca ',
358 '',
359 '/CA ',
360 '',
361 '>>',
362 token.new(0, 2),
363 }
364 luatexbase.add_to_callback('luaotfload.parse_transparent', function(value)
365 value = (octet * -1):match(value)
366 if not value then
367 tex.error'Invalid transparency value'
368 return
369 end
370 value = value:sub(1, -2)
371 local result = 'opacity' .. value
372 tex.runtoks(function()
373 transparent_register[6], transparent_register[10], transparent_register[12] = result, value, value
374 tex.sprint(-2, transparent_register)
375 end)
376 return '/' .. result .. ' gs'
377 end, 'l3opacity')
378 end
379 % \end{macrocode}
380 %
381 % \begin{macrocode}
382 %</lua>
383 % \end{macrocode}
384 %
385 %
386 % \end{implementation}
387 %
388 % \PrintIndex