summaryrefslogtreecommitdiff
path: root/macros/latex/contrib/l3backend/l3backend-color.dtx
blob: 6875ce757902c31a0c7aed0b296e667a9f0b2018 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
% \iffalse meta-comment
%
%% File: l3backend-color.dtx
%
% Copyright (C) 2019,2020 The LaTeX3 Project
%
% It may be distributed and/or modified under the conditions of the
% LaTeX Project Public License (LPPL), either version 1.3c of this
% license or (at your option) any later version.  The latest version
% of this license is in the file
%
%    https://www.latex-project.org/lppl.txt
%
% This file is part of the "l3backend bundle" (The Work in LPPL)
% and all files in that bundle must be distributed together.
%
% -----------------------------------------------------------------------
%
% The development version of the bundle can be found at
%
%    https://github.com/latex3/latex3
%
% for those people who are interested.
%
%<*driver>
\documentclass[full,kernel]{l3doc}
\begin{document}
  \DocInput{\jobname.dtx}
\end{document}
%</driver>
% \fi
%
% \title{^^A
%   The \textsf{l3backend-color} package\\ Backend color support^^A
% }
%
% \author{^^A
%  The \LaTeX3 Project\thanks
%    {^^A
%      E-mail:
%        \href{mailto:latex-team@latex-project.org}
%          {latex-team@latex-project.org}^^A
%    }^^A
% }
%
% \date{Released 2020-06-29}
%
% \maketitle
%
% \begin{documentation}
%
% \end{documentation}
%
% \begin{implementation}
%
% \section{\pkg{l3backend-color} Implementation}
%
%    \begin{macrocode}
%<*initex|package>
%<@@=color>
%    \end{macrocode}
%
% Color support is split into two parts: a \enquote{general} concept and
% one directly linked to drawings (or rather the split between filling
% and stroking). General color is relatively easy to handle: we have a color
% stack available with all modern drivers, and can use that.
% Whilst \texttt{(x)dvipdfmx} does have its own approach to color specials,
% it is easier to use \texttt{dvips}-like ones for all cases except direct
% PDF output.
%
% \subsection{\texttt{dvips}-style}
%
%    \begin{macrocode}
%<*dvisvgm|dvipdfmx|dvips|xdvipdfmx>
%    \end{macrocode}
%
% \begin{macro}{\@@_backend_pickup:N}
% \begin{macro}{\@@_backend_pickup:w}
%   Allow for \LaTeXe{} color. Here, the possible input values are limited:
%   \texttt{dvips}-style colors can mainly be taken as-is with the exception
%   spot ones (here we need a model and a tint). The \texttt{x}-type expansion
%   is there to cover the case where \pkg{xcolor} is in use.
%    \begin{macrocode}
%<*package>
\cs_new_protected:Npn \@@_backend_pickup:N #1 { }
\AtBeginDocument
  {
    \cs_if_exist:cT { ver@color.sty }
      {
        \cs_set_protected:Npn \@@_backend_pickup:N #1
          {
            \exp_args:NV \tl_if_head_is_space:nTF \current@color
              {
                \tl_set:Nx #1
                   {
                     { spot }
                     { \exp_after:wN \use:n \current@color \c_space_tl 1 }
                   }
              }
              {
                \exp_last_unbraced:Nx \@@_backend_pickup:w
                  { \current@color } \s_@@_stop #1
              }
          }
        \cs_new_protected:Npn \@@_backend_pickup:w #1 ~ #2 \s_@@_stop #3
          { \tl_set:Nn #3 { {#1} {#2} } }
      }
  }
%</package>
%    \end{macrocode}
% \end{macro}
% \end{macro}
%
% \begin{macro}
%   {
%     \@@_backend_cmyk:n  ,
%     \@@_backend_gray:n  ,
%     \@@_backend_rgb:n   ,
%     \@@_backend_spot:n  ,
%     \@@_backend_select:n
%   }
% \begin{macro}{\@@_backend_reset:}
% \begin{macro}{color.fc}
%    Push the data to the stack. In the case of \texttt{dvips} also reset the
%    drawing fill color in raw PostScript.
%    \begin{macrocode}
\cs_new_protected:Npn \@@_backend_cmyk:n #1
  { \@@_backend_select:n { cmyk ~ #1 } }
\cs_new_protected:Npn \@@_backend_gray:n #1
  { \@@_backend_select:n { gray ~ #1 } }
\cs_new_protected:Npn \@@_backend_rgb:n #1
  { \@@_backend_select:n { rgb ~ #1 } }
\cs_new_protected:Npn \@@_backend_spot:n #1
  { \@@_backend_select:n {#1} }
\cs_new_protected:Npn \@@_backend_select:n #1
  {
    \__kernel_backend_literal:n { color~push~ #1 }
%<*dvips>
    \__kernel_backend_postscript:n { /color.fc~{ }~def }
%</dvips>
    \group_insert_after:N \@@_backend_reset:
  }
\cs_new_protected:Npn \@@_backend_reset:
  { \__kernel_backend_literal:n { color~pop } }
%    \end{macrocode}
% \end{macro}
% \end{macro}
% \end{macro}
%
%    \begin{macrocode}
%</dvisvgm|dvipdfmx|dvips|xdvipdfmx>
%    \end{macrocode}
%
% \subsection{\texttt{pdfmode}}
%
%    \begin{macrocode}
%<*pdfmode>
%    \end{macrocode}
%
% \begin{macro}{\@@_backend_pickup:N}
% \begin{macro}{\@@_backend_pickup:w}
%   The current color in driver-dependent format: pick up the package-mode
%   data if available. We end up converting back and forward in this route as
%   we store our color data in \texttt{dvips} format.
%   The \tn{current@color} needs to be \texttt{x}-expanded before
%   \cs{@@_backend_pickup:w} breaks it apart, because for instance
%   \pkg{xcolor} sets it to be instructions to generate a color
%    \begin{macrocode}
%<*package>
\cs_new_protected:Npn \@@_backend_pickup:N #1 { }
\AtBeginDocument
  {
    \cs_if_exist:cT { ver@color.sty }
      {
        \cs_set_protected:Npn \@@_backend_pickup:N #1
          {
            \exp_last_unbraced:Nx \@@_backend_pickup:w
              { \current@color } ~ 0 ~ 0 ~ 0 \s_@@_stop #1
          }
        \cs_new_protected:Npn \@@_backend_pickup:w
          #1 ~ #2 ~ #3 ~ #4 ~ #5 ~ #6 \s_@@_stop #7
          {
            \str_if_eq:nnTF {#2} { g }
              { \tl_set:Nn #7 { { gray } {#1} } }
              {
                \str_if_eq:nnTF {#4} { rg }
                  { \tl_set:Nn #7 { { rgb } { #1 ~ #2 ~ #3 } } }
                  {
                     \str_if_eq:nnTF {#5} { k }
                       { \tl_set:Nn #7 { { cmyk } { #1 ~ #2 ~ #3 ~ #4 } } }
                       {
                         \str_if_eq:nnTF {#2} { cs }
                           {
                             \tl_set:Nx #7 { { spot } { \use_none:n #1 ~ #5 } }
                           }
                           {
                             \tl_set:Nn #7 { { gray } { 0 } }
                           }
                       }
                  }
              }
          }
      }
  }
%</package>
%    \end{macrocode}
% \end{macro}
% \end{macro}
%
% \begin{variable}{\l__kernel_color_stack_int}
%   \pdfTeX{} and \LuaTeX{} have multiple stacks available, and to track
%   which one is in use a variable is required.
%    \begin{macrocode}
\int_new:N \l__kernel_color_stack_int
%    \end{macrocode}
% \end{variable}
%
% \begin{macro}
%   {
%     \@@_backend_cmyk:n ,
%     \@@_backend_gray:n ,
%     \@@_backend_rgb:n  ,
%     \@@_backend_spot:n
%   }
% \begin{macro}{\@@_backend_spot:w}
% \begin{macro}{\@@_backend_select:n}
% \begin{macro}{\@@_backend_reset:}
%   Simply dump the data, but allowing for \LuaTeX{}.
%    \begin{macrocode}
\cs_new_protected:Npn \@@_backend_cmyk:n #1
  { \@@_backend_select:n { #1 ~ k ~ #1 ~ K } }
\cs_new_protected:Npn \@@_backend_gray:n #1
  { \@@_backend_select:n { #1 ~ g ~ #1 ~ G } }
\cs_new_protected:Npn \@@_backend_rgb:n #1
  { \@@_backend_select:n { #1 ~ rg ~ #1 ~ RG } }
\cs_new_protected:Npn \@@_backend_spot:n #1
  { \@@_backend_spot:w #1 \s_@@_stop }
\cs_new_protected:Npn \@@_backend_spot:w #1 ~ #2 \s_@@_stop
  { \@@_backend_select:n { /#1 ~ cs ~ /#1 ~ CS ~ #2 ~ scn ~ #2 ~ SCN } }
\cs_new_protected:Npx \@@_backend_select:n #1
  {
    \cs_if_exist:NTF \tex_pdfextension:D
      { \tex_pdfextension:D colorstack }
      { \tex_pdfcolorstack:D }
        \exp_not:N \l__kernel_color_stack_int push {#1}
      \group_insert_after:N \exp_not:N \@@_backend_reset:
  }
\cs_new_protected:Npx \@@_backend_reset:
  {
    \cs_if_exist:NTF \tex_pdfextension:D
      { \tex_pdfextension:D colorstack }
      { \tex_pdfcolorstack:D }
        \exp_not:N \l__kernel_color_stack_int pop \scan_stop:
  }
%    \end{macrocode}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
%
%    \begin{macrocode}
%</pdfmode>
%    \end{macrocode}
%
%    \begin{macrocode}
%</initex|package>
%    \end{macrocode}
%
% \end{implementation}
%
% \PrintIndex