Debian Codegraph @

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

raw
1 % \iffalse meta-comment
2 %
3 %% File: l3backend-color.dtx
4 %
5 % Copyright (C) 2019-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-color} module\\ Backend color 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-color} implementation}
57 %
58 % \begin{macrocode}
59 %<*package>
60 %<@@=color>
61 % \end{macrocode}
62 %
63 % Color support is split into parts: collecting data from \LaTeXe{}, the color
64 % stack, general color, separations, and color for drawings. We have different
65 % approaches in each backend, and have some choices to make about
66 % \texttt{dvipdfmx}/\XeTeX{} in particular. Whilst it is in some ways
67 % convenient to use the same approach in multiple backends, the fact that
68 % \texttt{dvipdfmx}/\XeTeX{} is PDF-based means it (largely) sticks closer to
69 % direct PDF output.
70 %
71 % \subsection{The color stack}
72 %
73 % For PDF-based engines, we have a color stack available inside the specials.
74 % This is used for concepts beyond color itself: it is needed to manage the
75 % graphics state generally. Although \texttt{dvipdfmx}/\XeTeX{} have multiple
76 % color stacks in recent releases, the way these interact with the original
77 % single stack and with other graphic state operations means that currently it
78 % is not feasible to use the multiple stacks.
79 %
80 % \subsubsection{Common code}
81 %
82 % \begin{macrocode}
83 %<*luatex|pdftex>
84 % \end{macrocode}
85 %
86 % \begin{variable}{\l_@@_backend_stack_int}
87 % For tracking which stack is in use where multiple stacks are used:
88 % currently just \pdfTeX{}/\LuaTeX{} but at some future stage may also cover
89 % \texttt{dvipdfmx}/\XeTeX{}.
90 % \begin{macrocode}
91 \int_new:N \l_@@_backend_stack_int
92 % \end{macrocode}
93 % \end{variable}
94 %
95 % \begin{macrocode}
96 %</luatex|pdftex>
97 % \end{macrocode}
98 %
99 % \subsubsection{\LuaTeX and \pdfTeX{}}
100 %
101 % \begin{macrocode}
102 %<*luatex|pdftex>
103 % \end{macrocode}
104 %
105 % \begin{macro}{\__kernel_color_backend_stack_init:Nnn}
106 % \begin{macrocode}
107 \cs_new_protected:Npn \__kernel_color_backend_stack_init:Nnn #1#2#3
108 {
109 \int_const:Nn #1
110 {
111 %<*luatex>
112 \tex_pdffeedback:D colorstackinit ~
113 %</luatex>
114 %<*pdftex>
115 \tex_pdfcolorstackinit:D
116 %</pdftex>
117 \tl_if_blank:nF {#2} { #2 ~ }
118 {#3}
119 }
120 }
121 % \end{macrocode}
122 % \end{macro}
123 %
124 % \begin{macro}{\__kernel_color_backend_stack_push:nn}
125 % \begin{macro}{\__kernel_color_backend_stack_pop:n}
126 % \begin{macrocode}
127 \cs_new_protected:Npn \__kernel_color_backend_stack_push:nn #1#2
128 {
129 %<*luatex>
130 \tex_pdfextension:D colorstack ~
131 %</luatex>
132 %<*pdftex>
133 \tex_pdfcolorstack:D
134 %</pdftex>
135 \int_eval:n {#1} ~ push ~ {#2}
136 }
137 \cs_new_protected:Npn \__kernel_color_backend_stack_pop:n #1
138 {
139 %<*luatex>
140 \tex_pdfextension:D colorstack ~
141 %</luatex>
142 %<*pdftex>
143 \tex_pdfcolorstack:D
144 %</pdftex>
145 \int_eval:n {#1} ~ pop \scan_stop:
146 }
147 % \end{macrocode}
148 % \end{macro}
149 % \end{macro}
150 %
151 % \begin{macrocode}
152 %</luatex|pdftex>
153 % \end{macrocode}
154 %
155 % \subsection{General color}
156 %
157 % \subsubsection{\texttt{dvips}-style}
158 %
159 % \begin{macrocode}
160 %<*dvips|dvisvgm>
161 % \end{macrocode}
162 %
163 % \begin{macro}
164 % {
165 % \@@_backend_select_cmyk:n ,
166 % \@@_backend_select_gray:n ,
167 % \@@_backend_select_named:n ,
168 % \@@_backend_select_rgb:n ,
169 % \@@_backend_select:n
170 % }
171 % \begin{macro}{\@@_backend_reset:}
172 % Push the data to the stack. In the case of \texttt{dvips} also saves the
173 % drawing color in raw PostScript. The |spot| model is for handling data
174 % in classical format.
175 % \begin{macrocode}
176 \cs_new_protected:Npn \@@_backend_select_cmyk:n #1
177 { \@@_backend_select:n { cmyk ~ #1 } }
178 \cs_new_protected:Npn \@@_backend_select_gray:n #1
179 { \@@_backend_select:n { gray ~ #1 } }
180 \cs_new_protected:Npn \@@_backend_select_named:n #1
181 { \@@_backend_select:n { ~ #1 } }
182 \cs_new_protected:Npn \@@_backend_select_rgb:n #1
183 { \@@_backend_select:n { rgb ~ #1 } }
184 \cs_new_protected:Npn \@@_backend_select:n #1
185 {
186 \__kernel_backend_literal:n { color~push~ #1 }
187 %<*dvips>
188 \__kernel_backend_postscript:n { /color.sc ~ { } ~ def }
189 %</dvips>
190 }
191 \cs_new_protected:Npn \@@_backend_reset:
192 { \__kernel_backend_literal:n { color~pop } }
193 % \end{macrocode}
194 % \end{macro}
195 % \end{macro}
196 %
197 % \begin{macrocode}
198 %</dvips|dvisvgm>
199 % \end{macrocode}
200 %
201 % \subsubsection{\LuaTeX{} and \pdfTeX{}}
202 %
203 % \begin{macrocode}
204 %<*luatex|pdftex>
205 % \end{macrocode}
206 %
207 % \begin{variable}{\l_@@_backend_fill_tl, \l_@@_backend_stroke_tl}
208 % \begin{macrocode}
209 \tl_new:N \l_@@_backend_fill_tl
210 \tl_new:N \l_@@_backend_stroke_tl
211 \tl_set:Nn \l_@@_backend_fill_tl { 0 ~ g }
212 \tl_set:Nn \l_@@_backend_stroke_tl { 0 ~ G }
213 % \end{macrocode}
214 % \end{variable}
215 %
216 % \begin{macro}
217 % {
218 % \@@_backend_select_cmyk:n ,
219 % \@@_backend_select_gray:n ,
220 % \@@_backend_select_rgb:n
221 % }
222 % \begin{macro}{\@@_backend_select:nn}
223 % \begin{macro}{\@@_backend_reset:}
224 % Store the values then pass to the stack.
225 % \begin{macrocode}
226 \cs_new_protected:Npn \@@_backend_select_cmyk:n #1
227 { \@@_backend_select:nn { #1 ~ k } { #1 ~ K } }
228 \cs_new_protected:Npn \@@_backend_select_gray:n #1
229 { \@@_backend_select:nn { #1 ~ g } { #1 ~ G } }
230 \cs_new_protected:Npn \@@_backend_select_rgb:n #1
231 { \@@_backend_select:nn { #1 ~ rg } { #1 ~ RG } }
232 \cs_new_protected:Npn \@@_backend_select:nn #1#2
233 {
234 \tl_set:Nn \l_@@_backend_fill_tl {#1}
235 \tl_set:Nn \l_@@_backend_stroke_tl {#2}
236 \__kernel_color_backend_stack_push:nn \l_@@_backend_stack_int { #1 ~ #2 }
237 }
238 \cs_new_protected:Npn \@@_backend_reset:
239 { \__kernel_color_backend_stack_pop:n \l_@@_backend_stack_int }
240 % \end{macrocode}
241 % \end{macro}
242 % \end{macro}
243 % \end{macro}
244 %
245 % \begin{macrocode}
246 %</luatex|pdftex>
247 % \end{macrocode}
248 %
249 % \subsubsection{\texttt{dvipmdfx}/\XeTeX{}}
250 %
251 % These backends have the most possible approaches: it recognizes both
252 % \texttt{dvips}-based color specials and its own format, plus one can
253 % include PDF statements directly. Recent releases also have a color stack
254 % approach similar to \pdfTeX{}. Of the stack methods, the dedicated
255 % the most versatile is the latter as it can cover all of the use cases
256 % we have. However, at present this interacts problematically with any color
257 % on the original stack. We therefore stick to a single-stack approach here.
258 %
259 % \begin{macrocode}
260 %<*dvipdfmx|xetex>
261 % \end{macrocode}
262 %
263 % \begin{macro}
264 % {
265 % \@@_backend_select:n ,
266 % \@@_backend_select_cmyk:n ,
267 % \@@_backend_select_gray:n ,
268 % \@@_backend_select_rgb:n
269 % }
270 % \begin{macro}{\@@_backend_reset:}
271 % Using the single stack is relatively easy as there is only one route.
272 % \begin{macrocode}
273 \cs_new_protected:Npn \@@_backend_select:n #1
274 { \__kernel_backend_literal:n { pdf : bc ~ [ #1 ] } }
275 \cs_new_eq:NN \@@_backend_select_cmyk:n \@@_backend_select:n
276 \cs_new_eq:NN \@@_backend_select_gray:n \@@_backend_select:n
277 \cs_new_eq:NN \@@_backend_select_rgb:n \@@_backend_select:n
278 \cs_new_protected:Npn \@@_backend_reset:
279 { \__kernel_backend_literal:n { pdf : ec } }
280 % \end{macrocode}
281 % \end{macro}
282 % \end{macro}
283 %
284 % \begin{macro}{\@@_backend_select_named:n}
285 % For classical named colors, the only value we should get is |Black|.
286 % \begin{macrocode}
287 \cs_new_protected:Npn \@@_backend_select_named:n #1
288 {
289 \str_if_eq:nnTF {#1} { Black }
290 { \@@_backend_select_gray:n { 0 } }
291 { \msg_error:nnn { color } { unknown-named-color } {#1} }
292 }
293 \msg_new:nnn { color } { unknown-named-color }
294 { Named~color~'#1'~is~not~known. }
295 % \end{macrocode}
296 % \end{macro}
297 %
298 % \begin{macrocode}
299 %</dvipdfmx|xetex>
300 % \end{macrocode}
301 %
302 % \subsection{Separations}
303 %
304 % Here, life gets interesting and we need essentially one approach per
305 % backend.
306 %
307 % \begin{macrocode}
308 %<*dvipdfmx|luatex|pdftex|xetex|dvips>
309 % \end{macrocode}
310 %
311 % But we start with some functionality needed for both PostScript and
312 % PDF based backends.
313 %
314 % \begin{variable}{\g_@@_backend_colorant_prop}
315 % \begin{macrocode}
316 \prop_new:N \g_@@_backend_colorant_prop
317 % \end{macrocode}
318 % \end{variable}
319 %
320 % \begin{macro}[EXP]{\@@_backend_devicen_colorants:n}
321 % \begin{macro}[EXP]{\@@_backend_devicen_colorants:w}
322 % \begin{macrocode}
323 \cs_new:Npe \@@_backend_devicen_colorants:n #1
324 {
325 \exp_not:N \tl_if_blank:nF {#1}
326 {
327 \c_space_tl
328 << ~
329 /Colorants ~
330 << ~
331 \exp_not:N \@@_backend_devicen_colorants:w #1 ~
332 \exp_not:N \q_recursion_tail \c_space_tl
333 \exp_not:N \q_recursion_stop
334 >> ~
335 >>
336 }
337 }
338 \cs_new:Npn \@@_backend_devicen_colorants:w #1 ~
339 {
340 \quark_if_recursion_tail_stop:n {#1}
341 \prop_if_in:NnT \g_@@_backend_colorant_prop {#1}
342 {
343 #1 ~
344 \prop_item:Nn \g_@@_backend_colorant_prop {#1} ~
345 }
346 \@@_backend_devicen_colorants:w
347 }
348 % \end{macrocode}
349 % \end{macro}
350 % \end{macro}
351 %
352 % \begin{macrocode}
353 %</dvipdfmx|luatex|pdftex|xetex|dvips>
354 % \end{macrocode}
355 %
356 % \begin{macrocode}
357 %<*dvips>
358 % \end{macrocode}
359 %
360 % \begin{macro}{\@@_backend_select_separation:nn, \@@_backend_select_devicen:nn}
361 % \begin{macrocode}
362 \cs_new_protected:Npn \@@_backend_select_separation:nn #1#2
363 { \@@_backend_select:n { separation ~ #1 ~ #2 } }
364 \cs_new_eq:NN \@@_backend_select_devicen:nn \@@_backend_select_separation:nn
365 % \end{macrocode}
366 % \end{macro}
367 %
368 % \begin{macro}{\@@_backend_select_iccbased:nn}
369 % No support.
370 % \begin{macrocode}
371 \cs_new_protected:Npn \@@_backend_select_iccbased:nn #1#2 { }
372 % \end{macrocode}
373 % \end{macro}
374 %
375 % \begin{macro}
376 % {
377 % \@@_backend_separation_init:nnnnn,
378 % \@@_backend_separation_init:neenn
379 % }
380 % \begin{macro}
381 % {\@@_backend_separation_init_aux:nnnnnn}
382 % \begin{macro}[EXP]
383 % {
384 % \@@_backend_separation_init_/DeviceCMYK:nnn ,
385 % \@@_backend_separation_init_/DeviceGray:nnn ,
386 % \@@_backend_separation_init_/DeviceRGB:nnn
387 % }
388 % \begin{macro}[EXP]{\@@_backend_separation_init_Device:Nn}
389 % \begin{macro}[EXP]{\@@_backend_separation_init:nnn}
390 % \begin{macro}[EXP]{\@@_backend_separation_init_count:n}
391 % \begin{macro}[EXP]{\@@_backend_separation_init_count:w}
392 % \begin{macro}[EXP]{\@@_backend_separation_init:nnnn}
393 % \begin{macro}[EXP]{\@@_backend_separation_init:w}
394 % \begin{macro}[EXP]{\@@_backend_separation_init:n}
395 % \begin{macro}[EXP]{\@@_backend_separation_init:nw}
396 % \begin{macro}{\@@_backend_separation_init_CIELAB:nnn}
397 % Initializing here means creating a small header set up plus massaging
398 % some data. This comes about as we have to deal with PDF-focussed data,
399 % which makes most sense \enquote{higher-up}. The approach is based on
400 % ideas from \url{https://tex.stackexchange.com/q/560093} plus using
401 % the PostScript manual for other aspects.
402 % \begin{macrocode}
403 \cs_new_protected:Npe \@@_backend_separation_init:nnnnn #1#2#3#4#5
404 {
405 \bool_if:NT \g__kernel_backend_header_bool
406 {
407 \exp_not:N \exp_args:Ne \__kernel_backend_first_shipout:n
408 {
409 \exp_not:N \@@_backend_separation_init_aux:nnnnnn
410 { \exp_not:N \int_use:N \g_@@_model_int }
411 {#1} {#2} {#3} {#4} {#5}
412 }
413 \prop_gput:Nee \exp_not:N \g_@@_backend_colorant_prop
414 { / \exp_not:N \str_convert_pdfname:n {#1} }
415 {
416 << ~
417 /setcolorspace ~ {} ~
418 >> ~ begin ~
419 color \exp_not:N \int_use:N \g_@@_model_int \c_space_tl
420 end
421 }
422 }
423 }
424 \cs_generate_variant:Nn \@@_backend_separation_init:nnnnn { nee }
425 \cs_new_protected:Npn \@@_backend_separation_init_aux:nnnnnn #1#2#3#4#5#6
426 {
427 \__kernel_backend_literal:e
428 {
429 !
430 TeXDict ~ begin ~
431 /color #1
432 {
433 [ ~
434 /Separation ~ ( \str_convert_pdfname:n {#2} ) ~
435 [ ~ #3 ~ ] ~
436 {
437 \cs_if_exist_use:cF { @@_backend_separation_init_ #3 :nnn }
438 { \@@_backend_separation_init:nnn }
439 {#4} {#5} {#6}
440 }
441 ] ~ setcolorspace
442 } ~ def ~
443 end
444 }
445 }
446 \cs_new:cpn { @@_backend_separation_init_ /DeviceCMYK :nnn } #1#2#3
447 { \@@_backend_separation_init_Device:Nn 4 {#3} }
448 \cs_new:cpn { @@_backend_separation_init_ /DeviceGray :nnn } #1#2#3
449 { \@@_backend_separation_init_Device:Nn 1 {#3} }
450 \cs_new:cpn { @@_backend_separation_init_ /DeviceRGB :nnn } #1#2#3
451 { \@@_backend_separation_init_Device:Nn 2 {#3} }
452 \cs_new:Npn \@@_backend_separation_init_Device:Nn #1#2
453 {
454 #2 ~
455 \prg_replicate:nn {#1}
456 { #1 ~ index ~ mul ~ #1 ~ 1 ~ roll ~ }
457 \int_eval:n { #1 + 1 } ~ -1 ~ roll ~ pop
458 }
459 % \end{macrocode}
460 % For the generic case, we cannot use |/FunctionType 2| unfortunately, so
461 % we have to code that idea up in PostScript. Here, we will therefore assume
462 % that a range is \emph{always} given. First, we count values in each argument:
463 % at the backend level, we can assume there are always well-behaved with
464 % spaces present.
465 % \begin{macrocode}
466 \cs_new:Npn \@@_backend_separation_init:nnn #1#2#3
467 {
468 \exp_args:Ne \@@_backend_separation_init:nnnn
469 { \@@_backend_separation_init_count:n {#2} }
470 {#1} {#2} {#3}
471 }
472 \cs_new:Npn \@@_backend_separation_init_count:n #1
473 { \int_eval:n { 0 \@@_backend_separation_init_count:w #1 ~ \s_@@_stop } }
474 \cs_new:Npn \@@_backend_separation_init_count:w #1 ~ #2 \s_@@_stop
475 {
476 +1
477 \tl_if_blank:nF {#2}
478 { \@@_backend_separation_init_count:w #2 \s_@@_stop }
479 }
480 % \end{macrocode}
481 % Now we implement the algorithm. In the terms in the PostScript manual,
482 % we have $\mathbf{N} = 1$ and $\mathbf{Domain} = [0~1]$, with
483 % $\mathbf{Range}$ as |#2|, $\mathbf{C0}$ as |#3| and $\mathbf{C1}$
484 % as |#4|, with the number of output components in |#1|. So all we have
485 % to do is implement $y_{i} = \mathbf{C0}_{i} + x(\mathbf{C1}_{i} -
486 % \mathbf{C0}_{i})$ with lots of stack manipulation, then check the
487 % ranges. That's done by adding everything to the stack first, then using
488 % the fact we know all of the offsets. As manipulating the stack is tricky,
489 % we start by re-formatting the $\mathbf{C0}$ and $\mathbf{C1}$ arrays to
490 % be interleaved, and add a \texttt{0} to each pair: this is used
491 % to keep the stack of constant length while we are doing the first pass of
492 % mathematics. We then working through that list, calculating from the
493 % last to the first value before tidying up by removing all of the input
494 % values. We do that by first copying all of the final $y$ values to the
495 % end of the stack, then rolling everything so we can pop the now-unneeded
496 % material.
497 % \begin{macrocode}
498 \cs_new:Npn \@@_backend_separation_init:nnnn #1#2#3#4
499 {
500 \@@_backend_separation_init:w #3 ~ \s_@@_stop #4 ~ \s_@@_stop
501 \prg_replicate:nn {#1}
502 {
503 pop ~ 1 ~ index ~ neg ~ 1 ~ index ~ add ~
504 \int_eval:n { 3 * #1 } ~ index ~ mul ~
505 2 ~ index ~ add ~
506 \int_eval:n { 3 * #1 } ~ #1 ~ roll ~
507 }
508 \int_step_function:nnnN {#1} { -1 } { 1 }
509 \@@_backend_separation_init:n
510 \int_eval:n { 4 * #1 + 1 } ~ #1 ~ roll ~
511 \prg_replicate:nn { 3 * #1 + 1 } { pop ~ }
512 \tl_if_blank:nF {#2}
513 { \@@_backend_separation_init:nw {#1} #2 ~ \s_@@_stop }
514 }
515 \cs_new:Npn \@@_backend_separation_init:w
516 #1 ~ #2 \s_@@_stop #3 ~ #4 \s_@@_stop
517 {
518 #1 ~ #3 ~ 0 ~
519 \tl_if_blank:nF {#2}
520 { \@@_backend_separation_init:w #2 \s_@@_stop #4 \s_@@_stop }
521 }
522 \cs_new:Npn \@@_backend_separation_init:n #1
523 { \int_eval:n { #1 * 2 } ~ index ~ }
524 % \end{macrocode}
525 % Finally, we deal with the range limit if required. This is handled
526 % by splitting the range into pairs. It's then just a question of doing
527 % the comparisons, this time dropping everything except the desired
528 % result.
529 % \begin{macrocode}
530 \cs_new:Npn \@@_backend_separation_init:nw #1#2 ~ #3 ~ #4 \s_@@_stop
531 {
532 #2 ~ #3 ~
533 2 ~ index ~ 2 ~ index ~ lt ~
534 { ~ pop ~ exch ~ pop ~ } ~
535 { ~
536 2 ~ index ~ 1 ~ index ~ gt ~
537 { ~ exch ~ pop ~ exch ~ pop ~ } ~
538 { ~ pop ~ pop ~ } ~
539 ifelse ~
540 }
541 ifelse ~
542 #1 ~ 1 ~ roll ~
543 \tl_if_blank:nF {#4}
544 { \@@_backend_separation_init:nw {#1} #4 \s_@@_stop }
545 }
546 % \end{macrocode}
547 % CIELAB support uses the detail from the PostScript reference, page 227;
548 % other than that block of PostScript, this is the same as for PDF-based
549 % routes.
550 % \begin{macrocode}
551 \cs_new_protected:Npn \@@_backend_separation_init_CIELAB:nnn #1#2#3
552 {
553 \@@_backend_separation_init:neenn
554 {#2}
555 {
556 /CIEBasedABC ~
557 << ~
558 /RangeABC ~ [ ~ \c_@@_model_range_CIELAB_tl \c_space_tl ] ~
559 /DecodeABC ~
560 [ ~
561 { ~ 16 ~ add ~ 116 ~ div ~ } ~ bind ~
562 { ~ 500 ~ div ~ } ~ bind ~
563 { ~ 200 ~ div ~ } ~ bind ~
564 ] ~
565 /MatrixABC ~ [ ~ 1 ~ 1 ~ 1 ~ 1 ~ 0 ~ 0 ~ 0 ~ 0 ~ -1 ~ ] ~
566 /DecodeLMN ~
567 [ ~
568 { ~
569 dup ~ 6 ~ 29 ~ div ~ ge ~
570 { ~ dup ~ dup ~ mul ~ mul ~ ~ } ~
571 { ~ 4 ~ 29 ~ div ~ sub ~ 108 ~ 841 ~ div ~ mul ~ } ~
572 ifelse ~
573 0.9505 ~ mul ~
574 } ~ bind ~
575 { ~
576 dup ~ 6 ~ 29 ~ div ~ ge ~
577 { ~ dup ~ dup ~ mul ~ mul ~ } ~
578 { ~ 4 ~ 29 ~ div ~ sub ~ 108 ~ 841 ~ div ~ mul ~ } ~
579 ifelse ~
580 } ~ bind ~
581 { ~
582 dup ~ 6 ~ 29 ~ div ~ ge ~
583 { ~ dup ~ dup ~ mul ~ mul ~ } ~
584 { ~ 4 ~ 29 ~ div ~ sub ~ 108 ~ 841 ~ div ~ mul ~ } ~
585 ifelse ~
586 1.0890 ~ mul ~
587 } ~ bind
588 ] ~
589 /WhitePoint ~
590 [ ~ \tl_use:c { c_@@_model_whitepoint_CIELAB_ #1 _tl } ~ ] ~
591 >>
592 }
593 { \c_@@_model_range_CIELAB_tl }
594 { 100 ~ 0 ~ 0 }
595 {#3}
596 }
597 % \end{macrocode}
598 % \end{macro}
599 % \end{macro}
600 % \end{macro}
601 % \end{macro}
602 % \end{macro}
603 % \end{macro}
604 % \end{macro}
605 % \end{macro}
606 % \end{macro}
607 % \end{macro}
608 % \end{macro}
609 % \end{macro}
610 %
611 % \begin{macro}{\@@_backend_devicen_init:nnn}
612 % Trivial as almost all of the work occurs in the shared code.
613 % \begin{macrocode}
614 \cs_new_protected:Npn \@@_backend_devicen_init:nnn #1#2#3
615 {
616 \__kernel_backend_literal:e
617 {
618 !
619 TeXDict ~ begin ~
620 /color \int_use:N \g_@@_model_int
621 {
622 [ ~
623 /DeviceN ~
624 [ ~ #1 ~ ] ~
625 #2 ~
626 { ~ #3 ~ } ~
627 \@@_backend_devicen_colorants:n {#1}
628 ] ~ setcolorspace
629 } ~ def ~
630 end
631 }
632 }
633 % \end{macrocode}
634 % \end{macro}
635 %
636 % \begin{macro}{\@@_backend_iccbased_init:nnn}
637 % No support at present.
638 % \begin{macrocode}
639 \cs_new_protected:Npn \@@_backend_iccbased_init:nnn #1#2#3 { }
640 % \end{macrocode}
641 % \end{macro}
642 %
643 % \begin{macrocode}
644 %</dvips>
645 % \end{macrocode}
646 %
647 % \begin{macrocode}
648 %<*dvisvgm>
649 % \end{macrocode}
650 %
651 % \begin{macro}
652 % {
653 % \@@_backend_select_separation:nn,
654 % \@@_backend_select_devicen:nn
655 % }
656 % No support at present.
657 % \begin{macrocode}
658 \cs_new_protected:Npn \@@_backend_select_separation:nn #1#2 { }
659 \cs_new_eq:NN \@@_backend_select_devicen:nn \@@_backend_select_separation:nn
660 % \end{macrocode}
661 % \end{macro}
662 %
663 % \begin{macro}
664 % {\@@_backend_separation_init:nnnnn, \@@_backend_separation_init_CIELAB:nnn}
665 % No support at present.
666 % \begin{macrocode}
667 \cs_new_protected:Npn \@@_backend_separation_init:nnnnn #1#2#3#4#5 { }
668 \cs_new_protected:Npn \@@_backend_separation_init_CIELAB:nnnnnn #1#2#3 { }
669 % \end{macrocode}
670 % \end{macro}
671 %
672 % \begin{macro}{\@@_backend_select_iccbased:nn}
673 % As detailed in \url{https://www.w3.org/TR/css-color-4/#at-profile},
674 % we can apply a color profile using CSS. As we have a local file, we use
675 % a relative URL.
676 % \begin{macrocode}
677 \cs_new_protected:Npn \@@_backend_select_iccbased:nn #1#2
678 {
679 \__kernel_backend_literal_svg:e
680 {
681 <style>
682 @color-profile ~
683 \str_if_eq:nnTF {#2} { cmyk }
684 { device-cmyk }
685 { --color \int_use:N \g_@@_model_int }
686 \c_space_tl
687 {
688 src:("#1")
689 }
690 </style>
691 }
692 }
693 % \end{macrocode}
694 % \end{macro}
695 %
696 % \begin{macrocode}
697 %</dvisvgm>
698 % \end{macrocode}
699 %
700 % \begin{macrocode}
701 %<*dvipdfmx|luatex|pdftex|xetex>
702 % \end{macrocode}
703 %
704 % \begin{macro}
705 % {
706 % \@@_backend_select_separation:nn,
707 % \@@_backend_select_devicen:nn ,
708 % \@@_backend_select_iccbased:nn
709 % }
710 % \begin{macrocode}
711 %<*dvipdfmx|xetex>
712 \cs_new_protected:Npn \@@_backend_select_separation:nn #1#2
713 { \__kernel_backend_literal:e { pdf : bc ~ \pdf_object_ref:n {#1} ~ [ #2 ] } }
714 %</dvipdfmx|xetex>
715 %<*luatex|pdftex>
716 \cs_new_protected:Npn \@@_backend_select_separation:nn #1#2
717 { \@@_backend_select:nn { /#1 ~ cs ~ #2 ~ scn } { /#1 ~ CS ~ #2 ~ SCN } }
718 %</luatex|pdftex>
719 \cs_new_eq:NN \@@_backend_select_devicen:nn \@@_backend_select_separation:nn
720 \cs_new_eq:NN \@@_backend_select_iccbased:nn \@@_backend_select_separation:nn
721 % \end{macrocode}
722 % \end{macro}
723 %
724 % \begin{macro}{\@@_backend_init_resource:n}
725 % Resource initiation comes up a few times. For \texttt{dvipdfmx}/\XeTeX{},
726 % we skip this as at present it's handled by the backend.
727 % \begin{macrocode}
728 \cs_new_protected:Npn \@@_backend_init_resource:n #1
729 {
730 %<*luatex|pdftex>
731 \bool_lazy_and:nnT
732 { \cs_if_exist_p:N \pdfmanagement_if_active_p: }
733 { \pdfmanagement_if_active_p: }
734 {
735 \use:e
736 {
737 \pdfmanagement_add:nnn
738 { Page / Resources / ColorSpace }
739 { #1 }
740 { \pdf_object_ref_last: }
741 }
742 }
743 %</luatex|pdftex>
744 }
745 % \end{macrocode}
746 % \end{macro}
747 %
748 % \begin{macro}{\@@_backend_separation_init:nnnnn}
749 % \begin{macro}{\@@_backend_separation_init:nn}
750 % \begin{macro}{\@@_backend_separation_init_CIELAB:nnn}
751 % Initializing the PDF structures needs two parts: creating an object
752 % containing the \enquote{real} name of the Separation, then adding a reference
753 % to that to each page. We use a separate object for the tint transformation
754 % following the model in the PDF reference. The object here for the color
755 % needs to be named as that way it's accessible to
756 % \texttt{dvipdfmx}/\XeTeX{}.
757 % \begin{macrocode}
758 \cs_new_protected:Npn \@@_backend_separation_init:nnnnn #1#2#3#4#5
759 {
760 \pdf_object_unnamed_write:ne { dict }
761 {
762 /FunctionType ~ 2
763 /Domain ~ [0 ~ 1]
764 \tl_if_blank:nF {#3} { /Range ~ [#3] }
765 /C0 ~ [#4] ~
766 /C1 ~ [#5] /N ~ 1
767 }
768 \exp_args:Ne \@@_backend_separation_init:nn
769 { \str_convert_pdfname:n {#1} } {#2}
770 \@@_backend_init_resource:n { color \int_use:N \g_@@_model_int }
771 }
772 \cs_new_protected:Npn \@@_backend_separation_init:nn #1#2
773 {
774 \use:e
775 {
776 \pdf_object_new:n { color \int_use:N \g_@@_model_int }
777 \pdf_object_write:nnn { color \int_use:N \g_@@_model_int } { array }
778 { /Separation /#1 ~ #2 ~ \pdf_object_ref_last: }
779 }
780 \prop_gput:Nne \g_@@_backend_colorant_prop { /#1 }
781 { \pdf_object_ref_last: }
782 }
783 % \end{macrocode}
784 % For CIELAB colors, we need one object per document for the illuminant,
785 % plus initialization of the color space referencing that object.
786 % \begin{macrocode}
787 \cs_new_protected:Npn \@@_backend_separation_init_CIELAB:nnn #1#2#3
788 {
789 \pdf_object_if_exist:nF { @@_illuminant_CIELAB_ #1 }
790 {
791 \pdf_object_new:n { @@_illuminant_CIELAB_ #1 }
792 \pdf_object_write:nne { @@_illuminant_CIELAB_ #1 } { array }
793 {
794 /Lab ~
795 <<
796 /WhitePoint ~
797 [ \tl_use:c { c_@@_model_whitepoint_CIELAB_ #1 _tl } ]
798 /Range ~ [ \c_@@_model_range_CIELAB_tl ]
799 >>
800 }
801 }
802 \@@_backend_separation_init:nnnnn
803 {#2}
804 { \pdf_object_ref:n { @@_illuminant_CIELAB_ #1 } }
805 { \c_@@_model_range_CIELAB_tl }
806 { 100 ~ 0 ~ 0 }
807 {#3}
808 }
809 % \end{macrocode}
810 % \end{macro}
811 % \end{macro}
812 % \end{macro}
813 %
814 % \begin{macro}{\@@_backend_devicen_init:nnn}
815 % \begin{macro}[EXP]{\@@_backend_devicen_init:w}
816 % Similar to the Separations case, but with an arbitrary function for
817 % the alternative space work.
818 % \begin{macrocode}
819 \cs_new_protected:Npn \@@_backend_devicen_init:nnn #1#2#3
820 {
821 \pdf_object_unnamed_write:ne { stream }
822 {
823 {
824 /FunctionType ~ 4 ~
825 /Domain ~
826 [ ~
827 \prg_replicate:nn
828 { 0 \@@_backend_devicen_init:w #1 ~ \s_@@_stop }
829 { 0 ~ 1 ~ }
830 ] ~
831 /Range ~
832 [ ~
833 \str_case:nn {#2}
834 {
835 { /DeviceCMYK } { 0 ~ 1 ~ 0 ~ 1 ~ 0 ~ 1 ~ 0 ~ 1 }
836 { /DeviceGray } { 0 ~ 1 }
837 { /DeviceRGB } { 0 ~ 1 ~ 0 ~ 1 ~ 0 ~ 1 }
838 } ~
839 ]
840 }
841 { {#3} }
842 }
843 \use:e
844 {
845 \pdf_object_new:n { color \int_use:N \g_@@_model_int }
846 \pdf_object_write:nnn { color \int_use:N \g_@@_model_int } { array }
847 {
848 /DeviceN ~
849 [ ~ #1 ~ ] ~
850 #2 ~
851 \pdf_object_ref_last:
852 \@@_backend_devicen_colorants:n {#1}
853 }
854 }
855 \@@_backend_init_resource:n { color \int_use:N \g_@@_model_int }
856 }
857 \cs_new:Npn \@@_backend_devicen_init:w #1 ~ #2 \s_@@_stop
858 {
859 + 1
860 \tl_if_blank:nF {#2}
861 { \@@_backend_devicen_init:w #2 \s_@@_stop }
862 }
863 % \end{macrocode}
864 % \end{macro}
865 % \end{macro}
866 %
867 % \begin{macro}{\@@_backend_iccbased_init:nnn}
868 % Lots of data to save here: we only want to do that once per file,
869 % so track it by name.
870 % \begin{macrocode}
871 \cs_new_protected:Npn \@@_backend_iccbased_init:nnn #1#2#3
872 {
873 \pdf_object_if_exist:nF { @@_icc_ #1 }
874 {
875 \pdf_object_new:n { @@_icc_ #1 }
876 \pdf_object_write:nne { @@_icc_ #1 } { fstream }
877 {
878 {
879 /N ~ \exp_not:n { #2 } ~
880 \tl_if_empty:nF { #3 } { /Range~[ #3 ] }
881 }
882 {#1}
883 }
884 }
885 \pdf_object_unnamed_write:ne { array }
886 { /ICCBased ~ \pdf_object_ref:n { @@_icc_ #1 } }
887 \@@_backend_init_resource:n { color \int_use:N \g_@@_model_int }
888 }
889 % \end{macrocode}
890 % \end{macro}
891 %
892 % \begin{macro}{\@@_backend_iccbased_device:nnn}
893 % This is very similar to setting up a color space: the only part we
894 % add to the page resources differently.
895 % \begin{macrocode}
896 \cs_new_protected:Npn \@@_backend_iccbased_device:nnn #1#2#3
897 {
898 \pdf_object_if_exist:nF { @@_icc_ #1 }
899 {
900 \pdf_object_new:n { @@_icc_ #1 }
901 \pdf_object_write:nnn { @@_icc_ #1 } { fstream }
902 {
903 { /N ~ #3 }
904 {#1}
905 }
906 }
907 \pdf_object_unnamed_write:ne { array }
908 { /ICCBased ~ \pdf_object_ref:n { @@_icc_ #1 } }
909 \@@_backend_init_resource:n { Default #2 }
910 }
911 % \end{macrocode}
912 % \end{macro}
913 %
914 % \begin{macrocode}
915 %</dvipdfmx|luatex|pdftex|xetex>
916 % \end{macrocode}
917 %
918 % \subsection{Fill and stroke color}
919 %
920 % Here, \texttt{dvipdfmx}/\XeTeX{} we write direct PDF specials for the fill,
921 % and only use the stack for the stroke color (see above for comments on why
922 % we cannot use multiple stacks with these backends). \LuaTeX{} and \pdfTeX{}
923 % have multiple stacks that can deal with fill and stroke. For \texttt{dvips}
924 % we have to manage fill and stroke color ourselves. We also handle
925 % \texttt{dvisvgm} independently, as there we can create SVG directly.
926 %
927 % \begin{macrocode}
928 %<*dvipdfmx|xetex>
929 % \end{macrocode}
930 %
931 % \begin{macro}
932 % {
933 % \@@_backend_fill:n ,
934 % \@@_backend_fill_cmyk:n ,
935 % \@@_backend_fill_gray:n ,
936 % \@@_backend_fill_rgb:n ,
937 % \@@_backend_stroke:n ,
938 % \@@_backend_stroke_cmyk:n ,
939 % \@@_backend_stroke_gray:n ,
940 % \@@_backend_stroke_rgb:n
941 % }
942 % \begin{macrocode}
943 \cs_new_protected:Npn \@@_backend_fill:n #1
944 { \__kernel_backend_literal:n { pdf : bc ~ fill ~ [ #1 ] } }
945 \cs_new_eq:NN \@@_backend_fill_cmyk:n \@@_backend_fill:n
946 \cs_new_eq:NN \@@_backend_fill_gray:n \@@_backend_fill:n
947 \cs_new_eq:NN \@@_backend_fill_rgb:n \@@_backend_fill:n
948 \cs_new_protected:Npn \@@_backend_stroke:n #1
949 { \__kernel_backend_literal:n { pdf : bc ~ stroke ~ [ #1 ] } }
950 \cs_new_eq:NN \@@_backend_stroke_cmyk:n \@@_backend_stroke:n
951 \cs_new_eq:NN \@@_backend_stroke_gray:n \@@_backend_stroke:n
952 \cs_new_eq:NN \@@_backend_stroke_rgb:n \@@_backend_stroke:n
953 % \end{macrocode}
954 % \end{macro}
955 %
956 % \begin{macro}
957 % {
958 % \@@_backend_fill_separation:nn,
959 % \@@_backend_stroke_separation:nn,
960 % \@@_backend_fill_devicen:nn,
961 % \@@_backend_stroke_devicen:nn
962 % }
963 % \begin{macrocode}
964 \cs_new_protected:Npn \@@_backend_fill_separation:nn #1#2
965 {
966 \__kernel_backend_literal:e
967 { pdf : bc ~ fill ~ \pdf_object_ref:n {#1} ~ [ #2 ] }
968 }
969 \cs_new_protected:Npn \@@_backend_stroke_separation:nn #1#2
970 {
971 \__kernel_backend_literal:e
972 { pdf : bc ~ stroke ~ \pdf_object_ref:n {#1} ~ [ #2 ] }
973 }
974 \cs_new_eq:NN \@@_backend_fill_devicen:nn \@@_backend_fill_separation:nn
975 \cs_new_eq:NN \@@_backend_stroke_devicen:nn \@@_backend_stroke_separation:nn
976 % \end{macrocode}
977 % \end{macro}
978 %
979 % \begin{macro}{\@@_backend_fill_reset:, \@@_backend_stroke_reset:}
980 % \begin{macrocode}
981 \cs_new_eq:NN \@@_backend_fill_reset: \@@_backend_reset:
982 \cs_new_eq:NN \@@_backend_stroke_reset: \@@_backend_reset:
983 % \end{macrocode}
984 % \end{macro}
985 %
986 % \begin{macrocode}
987 %</dvipdfmx|xetex>
988 % \end{macrocode}
989 %
990 % \begin{macrocode}
991 %<*luatex|pdftex>
992 % \end{macrocode}
993 %
994 % \begin{macro}
995 % {
996 % \@@_backend_fill_cmyk:n ,
997 % \@@_backend_fill_gray:n ,
998 % \@@_backend_fill_rgb:n ,
999 % \@@_backend_fill:n ,
1000 % \@@_backend_stroke_cmyk:n ,
1001 % \@@_backend_stroke_gray:n ,
1002 % \@@_backend_stroke_rgb:n ,
1003 % \@@_backend_stroke:n
1004 % }
1005 % Drawing (fill/stroke) color is handled in \texttt{dvipdfmx}/\XeTeX{} in the
1006 % same way as \LuaTeX{}/\pdfTeX{}. We use the same approach as earlier, except the
1007 % color stack is not involved so the generic direct PDF operation is used.
1008 % There is no worry about the nature of strokes: everything is handled
1009 % automatically.
1010 % \begin{macrocode}
1011 \cs_new_protected:Npn \@@_backend_fill_cmyk:n #1
1012 { \@@_backend_fill:n { #1 ~ k } }
1013 \cs_new_protected:Npn \@@_backend_fill_gray:n #1
1014 { \@@_backend_fill:n { #1 ~ g } }
1015 \cs_new_protected:Npn \@@_backend_fill_rgb:n #1
1016 { \@@_backend_fill:n { #1 ~ rg } }
1017 \cs_new_protected:Npn \@@_backend_fill:n #1
1018 {
1019 \tl_set:Nn \l_@@_backend_fill_tl {#1}
1020 \__kernel_color_backend_stack_push:nn \l_@@_backend_stack_int
1021 { #1 ~ \l_@@_backend_stroke_tl }
1022 }
1023 \cs_new_protected:Npn \@@_backend_stroke_cmyk:n #1
1024 { \@@_backend_stroke:n { #1 ~ K } }
1025 \cs_new_protected:Npn \@@_backend_stroke_gray:n #1
1026 { \@@_backend_stroke:n { #1 ~ G } }
1027 \cs_new_protected:Npn \@@_backend_stroke_rgb:n #1
1028 { \@@_backend_stroke:n { #1 ~ RG } }
1029 \cs_new_protected:Npn \@@_backend_stroke:n #1
1030 {
1031 \tl_set:Nn \l_@@_backend_stroke_tl {#1}
1032 \__kernel_color_backend_stack_push:nn \l_@@_backend_stack_int
1033 { \l_@@_backend_fill_tl \c_space_tl #1 }
1034 }
1035 % \end{macrocode}
1036 % \end{macro}
1037 %
1038 % \begin{macro}
1039 % {
1040 % \@@_backend_fill_separation:nn,
1041 % \@@_backend_stroke_separation:nn,
1042 % \@@_backend_fill_devicen:nn,
1043 % \@@_backend_stroke_devicen:nn
1044 % }
1045 % \begin{macrocode}
1046 \cs_new_protected:Npn \@@_backend_fill_separation:nn #1#2
1047 { \@@_backend_fill:n { /#1 ~ cs ~ #2 ~ scn } }
1048 \cs_new_protected:Npn \@@_backend_stroke_separation:nn #1#2
1049 { \@@_backend_stroke:n { /#1 ~ CS ~ #2 ~ SCN } }
1050 \cs_new_eq:NN \@@_backend_fill_devicen:nn \@@_backend_fill_separation:nn
1051 \cs_new_eq:NN \@@_backend_stroke_devicen:nn \@@_backend_stroke_separation:nn
1052 % \end{macrocode}
1053 % \end{macro}
1054 %
1055 % \begin{macro}{\@@_backend_fill_reset:, \@@_backend_stroke_reset:}
1056 % \begin{macrocode}
1057 \cs_new_eq:NN \@@_backend_fill_reset: \@@_backend_reset:
1058 \cs_new_eq:NN \@@_backend_stroke_reset: \@@_backend_reset:
1059 % \end{macrocode}
1060 % \end{macro}
1061 %
1062 % \begin{macrocode}
1063 %</luatex|pdftex>
1064 % \end{macrocode}
1065 %
1066 % \begin{macrocode}
1067 %<*dvips>
1068 % \end{macrocode}
1069 %
1070 % \begin{macro}
1071 % {
1072 % \@@_backend_fill_cmyk:n ,
1073 % \@@_backend_fill_gray:n ,
1074 % \@@_backend_fill_rgb:n ,
1075 % \@@_backend_fill:n ,
1076 % \@@_backend_stroke_cmyk:n ,
1077 % \@@_backend_stroke_gray:n ,
1078 % \@@_backend_stroke_rgb:n
1079 % }
1080 % Fill color here is the same as general color \emph{except} we skip the
1081 % stroke part.
1082 % \begin{macrocode}
1083 \cs_new_protected:Npn \@@_backend_fill_cmyk:n #1
1084 { \@@_backend_fill:n { cmyk ~ #1 } }
1085 \cs_new_protected:Npn \@@_backend_fill_gray:n #1
1086 { \@@_backend_fill:n { gray ~ #1 } }
1087 \cs_new_protected:Npn \@@_backend_fill_rgb:n #1
1088 { \@@_backend_fill:n { rgb ~ #1 } }
1089 \cs_new_protected:Npn \@@_backend_fill:n #1
1090 {
1091 \__kernel_backend_literal:n { color~push~ #1 }
1092 }
1093 \cs_new_protected:Npn \@@_backend_stroke_cmyk:n #1
1094 { \__kernel_backend_postscript:n { /color.sc { #1 ~ setcmykcolor } def } }
1095 \cs_new_protected:Npn \@@_backend_stroke_gray:n #1
1096 { \__kernel_backend_postscript:n { /color.sc { #1 ~ setgray } def } }
1097 \cs_new_protected:Npn \@@_backend_stroke_rgb:n #1
1098 { \__kernel_backend_postscript:n { /color.sc { #1 ~ setrgbcolor } def } }
1099 % \end{macrocode}
1100 % \end{macro}
1101 %
1102 % \begin{macro}
1103 % {
1104 % \@@_backend_fill_separation:nn,
1105 % \@@_backend_stroke_separation:nn,
1106 % \@@_backend_fill_devicen:nn,
1107 % \@@_backend_stroke_devicen:nn
1108 % }
1109 % \begin{macrocode}
1110 \cs_new_protected:Npn \@@_backend_fill_separation:nn #1#2
1111 { \@@_backend_fill:n { separation ~ #1 ~ #2 } }
1112 \cs_new_protected:Npn \@@_backend_stroke_separation:nn #1#2
1113 { \__kernel_backend_postscript:n { /color.sc { separation ~ #1 ~ #2 } def } }
1114 \cs_new_eq:NN \@@_backend_fill_devicen:nn \@@_backend_fill_separation:nn
1115 \cs_new_eq:NN \@@_backend_stroke_devicen:nn \@@_backend_stroke_separation:nn
1116 % \end{macrocode}
1117 % \end{macro}
1118 %
1119 % \begin{macro}{\@@_backend_fill_reset:, \@@_backend_stroke_reset:}
1120 % \begin{macrocode}
1121 \cs_new_eq:NN \@@_backend_fill_reset: \@@_backend_reset:
1122 \cs_new_protected:Npn \@@_backend_stroke_reset: { }
1123 % \end{macrocode}
1124 % \end{macro}
1125 %
1126 % \begin{macrocode}
1127 %</dvips>
1128 % \end{macrocode}
1129 %
1130 % \begin{macrocode}
1131 %<*dvisvgm>
1132 % \end{macrocode}
1133 %
1134 % \begin{macro}
1135 % {
1136 % \@@_backend_fill_cmyk:n ,
1137 % \@@_backend_fill_gray:n ,
1138 % \@@_backend_fill_rgb:n ,
1139 % \@@_backend_fill:n
1140 % }
1141 % Fill color here is the same as general color.
1142 % \begin{macrocode}
1143 \cs_new_protected:Npn \@@_backend_fill_cmyk:n #1
1144 { \@@_backend_fill:n { cmyk ~ #1 } }
1145 \cs_new_protected:Npn \@@_backend_fill_gray:n #1
1146 { \@@_backend_fill:n { gray ~ #1 } }
1147 \cs_new_protected:Npn \@@_backend_fill_rgb:n #1
1148 { \@@_backend_fill:n { rgb ~ #1 } }
1149 \cs_new_protected:Npn \@@_backend_fill:n #1
1150 {
1151 \__kernel_backend_literal:n { color~push~ #1 }
1152 }
1153 % \end{macrocode}
1154 % \end{macro}
1155 %
1156 % \begin{macro}{\@@_backend_stroke_cmyk:n}
1157 % \begin{macro}{\@@_backend_stroke_gray:n, \@@_backend_stroke_gray_aux:n}
1158 % \begin{macro}{\@@_backend_stroke_rgb:n}
1159 % \begin{macro}{\@@_backend_stroke_rgb:w}
1160 % \begin{macro}{\@@_backend:nnn}
1161 % For drawings in SVG, we use scopes for all stroke colors. The backend
1162 % provides the necessary conversion for CMYK but only if that is set as
1163 % the main color: a little bit of gymnastics as a result.
1164 % \begin{macrocode}
1165 \cs_new_protected:Npn \@@_backend_stroke_cmyk:n #1
1166 {
1167 \@@_backend_fill_cmyk:n {#1}
1168 \__kernel_backend_scope:n { stroke = "{?color}" }
1169 \@@_backend_reset:
1170 }
1171 \cs_new_protected:Npn \@@_backend_stroke_gray:n #1
1172 {
1173 \use:e
1174 {
1175 \@@_backend_stroke_gray_aux:n
1176 { \fp_eval:n { 100 * (#1) } }
1177 }
1178 }
1179 \cs_new_protected:Npn \@@_backend_stroke_gray_aux:n #1
1180 { \@@_backend:nnn {#1} {#1} {#1} }
1181 \cs_new_protected:Npn \@@_backend_stroke_rgb:n #1
1182 { \@@_backend_rgb:w #1 \s_@@_stop }
1183 \cs_new_protected:Npn \@@_backend_stroke_rgb:w
1184 #1 ~ #2 ~ #3 \s_@@_stop
1185 {
1186 \use:e
1187 {
1188 \@@_backend:nnn
1189 { \fp_eval:n { 100 * (#1) } }
1190 { \fp_eval:n { 100 * (#2) } }
1191 { \fp_eval:n { 100 * (#3) } }
1192 }
1193 }
1194 \cs_new_protected:Npe \@@_backend:nnn #1#2#3
1195 {
1196 \__kernel_backend_scope:n
1197 {
1198 stroke =
1199 "
1200 rgb
1201 (
1202 #1 \c_percent_str ,
1203 #2 \c_percent_str ,
1204 #3 \c_percent_str
1205 )
1206 "
1207 }
1208 }
1209 % \end{macrocode}
1210 % \end{macro}
1211 % \end{macro}
1212 % \end{macro}
1213 % \end{macro}
1214 % \end{macro}
1215 %
1216 % \begin{macro}
1217 % {
1218 % \@@_backend_fill_separation:nn,
1219 % \@@_backend_stroke_separation:nn,
1220 % \@@_backend_fill_devicen:nn,
1221 % \@@_backend_stroke_devicen:nn
1222 % }
1223 % At present, these are no-ops.
1224 % \begin{macrocode}
1225 \cs_new_protected:Npn \@@_backend_fill_separation:nn #1#2 { }
1226 \cs_new_protected:Npn \@@_backend_stroke_separation:nn #1#2 { }
1227 \cs_new_eq:NN \@@_backend_fill_devicen:nn \@@_backend_fill_separation:nn
1228 \cs_new_eq:NN \@@_backend_stroke_devicen:nn \@@_backend_stroke_separation:nn
1229 % \end{macrocode}
1230 % \end{macro}
1231 %
1232 % \begin{macro}{\@@_backend_fill_reset:, \@@_backend_stroke_reset:}
1233 % \begin{macrocode}
1234 \cs_new_eq:NN \@@_backend_fill_reset: \@@_backend_reset:
1235 \cs_new_protected:Npn \@@_backend_stroke_reset: { }
1236 % \end{macrocode}
1237 % \end{macro}
1238 %
1239 % \begin{macro}{\@@_backend_devicen_init:nnn, \@@_backend_iccbased_init:nnn}
1240 % No support at present.
1241 % \begin{macrocode}
1242 \cs_new_protected:Npn \@@_backend_devicen_init:nnn #1#2#3 { }
1243 \cs_new_protected:Npn \@@_backend_iccbased_init:nnn #1#2#3 { }
1244 % \end{macrocode}
1245 % \end{macro}
1246 %
1247 % \begin{macrocode}
1248 %</dvisvgm>
1249 % \end{macrocode}
1250 %
1251 % \begin{macrocode}
1252 %</package>
1253 % \end{macrocode}
1254 %
1255 % \subsection{Font handling integration}
1256 %
1257 % In \LuaTeX{} these colors should also be usable to color fonts, so
1258 % \texttt{luaotfload} color handling is extended to include these.
1259 %
1260 % \begin{macrocode}
1261 %<*lua>
1262 % \end{macrocode}
1263 %
1264 % \begin{macrocode}
1265 local l = lpeg
1266 local spaces = l.P' '^0
1267 local digit16 = l.R('09', 'af', 'AF')
1268
1269 local octet = digit16 * digit16 / function(s)
1270 return string.format('%.3g ', tonumber(s, 16) / 255)
1271 end
1272
1273 if luaotfload and luaotfload.set_transparent_colorstack then
1274 local htmlcolor = l.Cs(octet * octet * octet * -1 * l.Cc'rg')
1275 local color_export = {
1276 token.create'tex_endlocalcontrol:D',
1277 token.create'tex_hpack:D',
1278 token.new(0, 1),
1279 token.create'color_export:nnN',
1280 token.new(0, 1),
1281 '',
1282 token.new(0, 2),
1283 token.new(0, 1),
1284 'backend',
1285 token.new(0, 2),
1286 token.create'l_tmpa_tl',
1287 token.create'exp_after:wN',
1288 token.create'@@_select:nn',
1289 token.create'l_tmpa_tl',
1290 token.new(0, 2),
1291 }
1292 local group_end = token.create'group_end:'
1293 local value = (1 - l.P'}')^0
1294 luatexbase.add_to_callback('luaotfload.parse_color', function (value)
1295 % Also allow HTML colors to preserve compatibility
1296 local html = htmlcolor:match(value)
1297 if html then return html end
1298
1299 % If no l3color named color with this name is known, check for defined xcolor colors
1300 local l3color_prop = token.get_macro(string.format('l_@@_named_%s_prop', value))
1301 if l3color_prop == nil or l3color_prop == '' then
1302 local legacy_color_macro = token.create(string.format('\\color@%s', value))
1303 if legacy_color_macro.cmdname ~= 'undefined_cs' then
1304 token.put_next(legacy_color_macro)
1305 return token.scan_argument()
1306 end
1307 end
1308
1309 tex.runtoks(function()
1310 token.get_next()
1311 color_export[6] = value
1312 tex.sprint(-2, color_export)
1313 end)
1314 local list = token.scan_list()
1315 if not list.head or list.head.next
1316 or list.head.subtype ~= node.subtype'pdf_colorstack' then
1317 error'Unexpected backend behavior'
1318 end
1319 local cmd = list.head.data
1320 node.free(list)
1321 return cmd
1322 end, 'l3color')
1323 end
1324 % \end{macrocode}
1325 %
1326 % \begin{macrocode}
1327 %</lua>
1328 % \end{macrocode}
1329 %
1330 % \begin{macrocode}
1331 %<*luatex>
1332 % \end{macrocode}
1333 %
1334 % \begin{macrocode}
1335 %<*package>
1336 \lua_load_module:n {l3backend-luatex}
1337 %</package>
1338 % \end{macrocode}
1339 %
1340 % \begin{macrocode}
1341 %</luatex>
1342 % \end{macrocode}
1343 %
1344 % \end{implementation}
1345 %
1346 % \PrintIndex