Debian Codegraph @

texmf-dist/source/xelatex/xespotcolor/xespotcolor.dtx

raw
1 %% \CharacterTable
2 %% {Upper-case \A\B\C\D\E\F\G\H\I\J\K\L\M\N\O\P\Q\R\S\T\U\V\W\X\Y\Z
3 %% Lower-case \a\b\c\d\e\f\g\h\i\j\k\l\m\n\o\p\q\r\s\t\u\v\w\x\y\z
4 %% Digits \0\1\2\3\4\5\6\7\8\9
5 %% Exclamation \! Double quote \" Hash (number) \#
6 %% Dollar \$ Percent \% Ampersand \&
7 %% Acute accent \' Left paren \( Right paren \)
8 %% Asterisk \* Plus \+ Comma \,
9 %% Minus \- Point \. Solidus \/
10 %% Colon \: Semicolon \; Less than \<
11 %% Equals \= Greater than \> Question mark \?
12 %% Commercial at \@ Left bracket \[ Backslash \\
13 %% Right bracket \] Circumflex \^ Underscore \_
14 %% Grave accent \` Left brace \{ Vertical bar \|
15 %% Right brace \} Tilde \~}
16 %%
17 %\iffalse
18 %
19 % (c) Copyright 2014-2021 Apostolos Syropoulos
20 % This program can be redistributed and/or modified under the
21 % terms of the LaTeX Project Public License Distributed from
22 % http://www.latex-project.org/lppl.txt; either
23 % version 1.3c of the License, or any later version.
24 %
25 % This work has the LPPL maintenance status `maintained'.
26 %
27 % Please report errors or suggestions for improvement to
28 %
29 % Apostolos Syropoulos ([email protected])
30 %
31 %\fi
32 % \CheckSum{187}
33 % \iffalse This is a Metacomment
34 %
35 %<xespotcolor, >\ProvidesFile{xesporcolor.sty}
36 %
37 %<xespotcolor, > [2021/03/01 v2.1 Package `xespotcolor.sty']
38 %
39 % \begin{macrocode}
40 %<*driver>
41 \documentclass{ltxdoc}
42 \GetFileInfo{xspotcolor.drv}
43 \usepackage{xltxtra}
44 \usepackage{fullpage}
45 \begin{document}
46 \setmainfont[Mapping=tex-text,Script=Greek]{Arno Pro}
47 \setmonofont{UM Typewriter}
48 \setsansfont[Mapping=tex-text]{GFS Neohellenic}
49 \DocInput{xespotcolor.dtx}
50 \end{document}
51 %</driver>
52 % \end{macrocode}
53 % \fi
54 %\MakeShortVerb{\|}
55 %\StopEventually{}
56 %\title{The \textsf{xespotcolor} package:\\ Spot Colors for \XeLaTeX\ \&\ \LaTeX}
57 %\author{Απόστολος Συρόπουλος\\
58 % (Apostolos Syropoulos)\\
59 % Xanthi, Greece\\
60 % \texttt{[email protected]}}
61 % \date{2016/03/22\\updated 2021/03/01}
62 %\maketitle
63 % \begin{abstract}
64 % A spot color is one that is printed with its own ink. Typically, printers use spot colors
65 % in the production of books or other printed material. The \textsf{spotcolor} package
66 % by Jens Elstner is a first attempt to introduce the use of spot colors with pdfLaTeX.
67 % The \textsf{xespotcolor} package is a reimplementation of this package so to be usable with
68 % \XeLaTeX\ and \LaTeX+\texttt{dvipdfmx}. As such, it has the same user interface and the same
69 % capabilities.
70 %\end{abstract}
71 %
72 %\section{Introduction}
73 %
74 % Using spot colors with \XeLaTeX\ is very important since most printers use spot colors in the
75 % production of books and magazines. The \textsf{spotcolor} package makes it possible to use
76 % spot colors with pdf\LaTeX\ but it cannot be used with \XeLaTeX\ or \LaTeX. In what follows I first
77 % describe how to translate certain pdf\TeX\ code snippets into \XeTeX\ or \TeX+\texttt{dvipdfmx}
78 % and then I present the code of the package. Thus one can view this text as a short tutorial on how to port
79 % pdf\TeX\ code to \XeTeX\ or \TeX+\texttt{dvipdfmx} as well as a description of the functionality of the
80 % \textsf{xespotcolor} package. Since the package is a port of a pdf\TeX\ package, it has the same functionality
81 % as the original package.
82 %
83 %
84 % \section{Porting pdf\TeX\ code to \XeTeX\ or \TeX+\texttt{dvipdfmx}}
85 %
86 % Translating pdf\TeX\ code, which adds PDF code to the output file, to \XeTeX\ or \TeX+\texttt{dvipdfmx} is not a
87 % straightforward exercise since pdf\TeX\ provides primitive commands that directly access and modify the structure of
88 % the resulting PDF file. In the case of \XeTeX\ and \TeX\ one has to use |\special| commands that pass code to the driver.
89 % In this particular case, I had to translate code snippets like the following one:
90 % \begin{center}
91 % |1. \newcount\theCNTa |\\
92 % |2. \newcount\theCNTb |\\
93 % |3. \def\obj{ 0 R}% |\\
94 % |4. \pdfobj{Raw PDF code 1}% |\\
95 % |5. \theCNTa=\the\pdflastobj% |\\
96 % |6. \pdfobj{Raw PDF code \the\theCNTa \obj}% |\\
97 % |7. \theCNTb=\the\pdflastobj% |\\
98 % |8. \pdfrefobj\theCNTa% |\\
99 % |9. \pdfrefobj\theCNTb% |\\
100 % \end{center}
101 % Here pdf\TeX\ creates two PDF objects, where the second contains a reference to the first one. The two counters
102 % defined in lines 1 and 2 are used to reference these two objects. The macro on line 3 is used to create code
103 % that references an object. The commands on lines 5 and 7 assign the object reference numbers and these numbers are
104 % used by the |\pdfrefobj| primitive. After some experimentation and some… Googling, I have found out that the following
105 % \XeTeX\ code is a reasonable translation of the previous code snipper:
106 % \begin{center}
107 % |\newcount\CNT |\\
108 % |\newtoks\TOK |\\
109 % |\TOK={@TOK \the\CNT}% |\\
110 % |\edef\A{\the\TOK Raw PDF code 1}% |\\
111 % |\edef\B{Raw PDF code \the\TOK}% |\\
112 % |\special{pdf:obj \A}% |\\
113 % |\special{pdf:obj @TOKB\the\CNT \B}% |\\
114 % |\advance\CNT by1% |\\
115 % \end{center}
116 % The two |\edef|initions are used to do the work done by |\pdfobj|. Note that here there I introduce only
117 % one unique object and the first two lines define a counter and a token variable. The token variable uses
118 % the counter to create a unique identifier, which is passed to the driver. This way the driver will
119 % create a number of different objects, if required to do so. The last two commands pass the raw PDF
120 % code and the unique identifier to the driver.
121 %
122 % The original package contains a definition identical to the following one:
123 % \begin{center}
124 % |\def\R#1{% |\\
125 % | \edef\act{\noexpand\pdfpageresources={\the\pdfpageresources\space |\\
126 % | /ColorSpace<<#1>>}} |\\
127 % | \act} |\\
128 % \end{center}
129 % The net effect of this command is to add a specific color space to the page resources of all subsequent pages.
130 % Unfortunately, when the following code is executed, it adds the particular color space to the current page only:
131 % \begin{center}
132 % |\def\R#1{% |\\
133 % | \special{pdf:put @resources <</ColorSpace <<#1>>>>}}|\\
134 % \end{center}
135 % In order to add the color space to all subsequent pages, I had to use the |\AddEverypageHook| command of
136 % package \textsf{everypage}. This command modifies the contents of the ship-out box by adding to it its argument.
137 % And this is done for every single page. Also, one should note how the page resources are augmented by the two
138 % systems. In the case of \XeTeX\ we have to create a PDF dictionary that is merged with the current page resources,
139 % while in the case of pdf\TeX\ one just ``appends'' what is supposed to be included in the page resources dictionary.
140 % Let me now proceed with the description of the source code of the package.
141 %
142 % \section{The Source Code and Package Usage}
143 % The first part of the code is the identification part.
144 % \begin{macrocode}
145 %<*xespotcolor>
146 \NeedsTeXFormat{LaTeX2e}
147 \ProvidesPackage{xespotcolor}
148 [2021/03/01 v.2.1, Package for adding Spot Color support to LaTeX/XeLaTeX.]
149 % \end{macrocode}
150 % The package needs three packages in order to operate properly: the \textsf{graphics} package, the
151 % \textsf{everypage} package, and either the package \textsf{color} or the package \textsf{xcolor}.
152 % The later is necessary if one wants to use spot colors with \textsf{TikZ} and other packages that
153 % rely on package \textsf{xcolor}. Akira Kakuto has discovered that this package can be used with
154 % either \LaTeX\ or \XeLaTeX\ and the \texttt{dvipdfmx}/\texttt{xdvipdfmx} driver, respectively.
155 % However, in order to properly load the various packages it is necessary to know which typesetting
156 % engine is being used. A simple solution is to use the \textsf{iftex} package:
157 % \begin{macrocode}
158 \RequirePackage{iftex}
159 % \end{macrocode}
160 % Before proceeding it is also necessary to know if the package \textsf{xcolor} has been loaded
161 % (package \textsf{color} is loaded by default). The following command check if \textsf{xcolor}
162 % is loaded. If it is, it defines a new macro.
163 % \begin{macrocode}
164 \@ifpackageloaded{xcolor}{\let\use@xcolor\relax}{}
165 % \end{macrocode}
166 % Now we are ready to process the three package options: \texttt{hks}, \texttt{pantone}, and \texttt{xcolor}.
167 % Here we employ the same ``trick'': if an option is requested, we define a corresponding macro.
168 % \begin{macrocode}
169 \DeclareOption{hks}{\let\use@hks\relax}
170 \DeclareOption{pantone}{\let\use@pantone\relax}
171 \DeclareOption{xcolor}{\let\use@xcolor\relax}
172 \ProcessOptions
173 % \end{macrocode}
174 % The code that follows loads the remaining two packages. If macro |\use@xcolor| is not
175 % defined, then the package should load the \textsf{color} package; otherwise it should
176 % load the \textsf{xcolor} package. The two inner |\if|s are used to load properly the packages
177 % when \XeTeX\ or \TeX\ is used to typeset a document. In addition, to avoid unnecessary package
178 % loadings, there are some tests to make sure the packages are not loaded.
179 % \begin{macrocode}
180 \ifx\use@xcolor\undefined
181 \ifxetex
182 \@ifpackageloaded{graphics}{}{\RequirePackage[xetex]{graphics}}
183 \@ifpackageloaded{color}{}\RequirePackage[xetex]{color}
184 \else
185 \@ifpackageloaded{graphics}{}{\RequirePackage[dvipdfmx]{graphics}}
186 \@ifpackageloaded{color}{}\RequirePackage{color}
187 \fi
188 \else
189 \ifxetex
190 \@ifpackageloaded{graphics}{}{\RequirePackage[xetex]{graphics}}
191 \@ifpackageloaded{xcolor}{}{\RequirePackage[xetex]{xcolor}}
192 \else
193 \@ifpackageloaded{graphics}{}{\RequirePackage[dvipdfmx]{graphics}}
194 \@ifpackageloaded{xcolor}{}{\RequirePackage{xcolor}}
195 \fi\fi
196 % \end{macrocode}
197 % The following command should be executed only when using \textsf{TikZ} and/or \textsf{tcolorbox}. Thus, always
198 % load this package after loading \textsf{TikZ} and/or \textsf{tcolorbox}.
199 % \begin{macrocode}
200 \@ifpackageloaded{tikz}{\global\pgf@sys@pdf@colorspaces@existsfalse}{}
201 \@ifpackageloaded{tcolorbox}{\global\pgf@sys@pdf@colorspaces@existsfalse}{}
202 % \end{macrocode}
203 % The |\NewSpotColorSpace| command should be used to define a new color space for spot colors. The new color
204 % space can be any imaginable word! This color space is the place where a new spot color will live. The
205 % \texttt{hks} and the \texttt{pantone} options create two color spaces where the corresponding colors live.
206 % \begin{macrocode}
207 \def\NewSpotColorSpace#1{%
208 \expandafter\newtoks\csname #1\endcsname%
209 \csname #1\endcsname{}%
210 }
211 % \end{macrocode}
212 % The |\AddSpotColor| macro should be used to introduce a new spot color. Thus it is one of the first commands
213 % on should play with when using this package. This command takes four parameters---the name of a new color
214 % space, which has been declared with |\NewSpotColorSpace|, the name of a new color, a name which will be
215 % used internally, and a CMYK representation of the new color. For example, here is a typical use of this macro:
216 % \begin{center}
217 %|\AddSpotColor{NEWCS}{NEWCOLOR}{emerald\SpotSpace city}{0.83 0 0.04 30}|
218 % \end{center}
219 % CMYK tables usually specify the four color components using percentages, which must be ``translated'' to
220 % numbers between 0 and 1.
221 %
222 % Macro |\SpotSpace| is used to introduce blanks in names. According to the PDF standard a blank is denoted
223 % by the character sequence |#20| and this is the reason why |\SpotSpace| is defined as follows:
224 % \begin{macrocode}
225 \catcode`\#=12%
226 \def\SpotSpace{#20}
227 \catcode`\#=6%
228 % \end{macrocode}
229 %The macro |\csgrab| is quite unsual---it takes two arguments and creates a sequence of them.
230 % \begin{macrocode}
231 \gdef\csgrab#1#2{#2\expandafter{\the#2 #1}}%
232 % \end{macrocode}
233 %Note that this command should never be used by an ordinary package user.
234 %
235 % The following variables are used in the definition of macro |\AddSpotColor|. Their functionality has been
236 % described in the previous section.
237 % \begin{macrocode}
238 \newcount\colorprofilecnt
239 \newtoks\mycolorprofilename
240 % \end{macrocode}
241 % Macro |\AddSpotColor| first defines a new color profile by assigning a value to |\mycolorprofilename|. The
242 % name consists of the word |@mycolorprofile| followed by an integer. This name is used in the construction
243 % of the corresponding PDF object.
244 % \begin{macrocode}
245 \def\AddSpotColor#1#2#3#4{%
246 \mycolorprofilename={@mycolorprofile\the\colorprofilecnt}%
247 % \end{macrocode}
248 % The following two macros expand to PDF instructions that define the spot color. The PDF instructions are copied verbatim from
249 % the original \textsf{spotcolor} package.
250 % \begin{macrocode}
251 \edef\mycolorprofile{\the\mycolorprofilename
252 <</C0[0 0 0 0]/FunctionType 2/C1[#4]/Domain[0 1]/N 1>>}%
253 \edef\mycolor{[/Separation/#3 /DeviceCMYK \the\mycolorprofilename]}%
254 % \end{macrocode}
255 % The next two lines have been copied verbatim from the original macro definition.
256 % \begin{macrocode}
257 \edef\tempcs{/#2 \mycolor}%
258 \expandafter\csgrab\expandafter{\tempcs}{\csname #1\endcsname}%
259 % \end{macrocode}
260 % In the last part of the macro definition, the driver is instructed to build two objects which should
261 % contain the definition of the new spot color. Note that here the macro specifies explicitly the object
262 % reference for the second object.
263 % \begin{macrocode}
264 \special{pdf:obj \mycolorprofile}%
265 \special{pdf:obj @myowncolor\the\colorprofilecnt \mycolor}%
266 % \end{macrocode}
267 % Since all names must be distinct, the macro increments the value of the |\colorprofilecnt| counter by one.
268 % \begin{macrocode}
269 \advance\colorprofilecnt by1%
270 }
271 % \end{macrocode}
272 %
273 % Command |\SetPageColorResource| is used by command |\SetPageColorSpace| to set the color space.
274 % The |\special| command below sets the page resources only for the current page. Since the color
275 % space should be visible to every subsequent page, initially I had opted to use command the |\AddEverypageHook|
276 % command provided by the \textsf{everypage} package. However, since the latest version of \LaTeX\ defines
277 % hooks, I had to use the new \texttt{shipout/background} hook.
278 % \begin{macrocode}
279 \def\SetPageColorResource#1{%
280 \AddToHook{shipout/background}{\put(1in,-1in){%
281 \special{pdf:put @resources <</ColorSpace <<#1>>>>}}}%
282 }%
283 \def\SetPageColorSpace#1{%
284 \expandafter\SetPageColorResource\expandafter{\the\csname #1\endcsname}%
285 }%
286 % \end{macrocode}
287 % If a user wants to set a spot color as the default color, she should use the |\SpotColor| command:
288 % \begin{macrocode}
289 \def\SpotColor#1#2{%
290 \special{pdf:literal /#1 cs /#1 CS #2 sc #2 SC}%
291 \aftergroup\reset@color%
292 }%
293 % \end{macrocode}
294 % Suppose that a user wants to define a new spot color. Then, she has to issue a command like the
295 % following one:
296 % \begin{center}
297 % |\definecolor{Spots}{spotcolor}{SPCOLOR,1.0}|
298 % \end{center}
299 % Note that the second argument of this command must always be |spotcolor|, since this is the color
300 % model we are using. In addition, this command is meaningful only when
301 % one has defined a new color space, a new spot color using commands like the following ones:
302 %\begin{center}
303 %\begin{tabular}{l}
304 %|\NewSpotColorSpace{SPCOLORSPACE}|\\
305 %|\AddSpotColor{SPCOLORSPACE}{SPCOLOR}{Some\SpotSpace Name}{0.5 1.0 0.51 0}|\\
306 %|\SetPageColorSpace{SPCOLORSPACE}|\\
307 %\end{tabular}
308 %\end{center}
309 % Note that we must set the page color space!
310 %
311 % Depending on which color package is in use, different low-level commands are executed in order to
312 % actually define the new color. These new commands are provided by this package. In particular, when the
313 % \textsf{color} package is used, these new commands are the |\color@spotcolor| command and |\c@lor@@spotcolor|
314 % command. On the other hand, when using the \textsf{xcolor} package these new commands are the |\XC@mod@spotcolor|
315 % command and the |\@@xespotcolordef@xspc| command.
316 %
317 % If variable |\use@xcolor| is undefined, this means that the user has opted to use the \textsf{color}
318 % package.
319 % \begin{macrocode}
320 \ifx\use@xcolor\undefined
321 % \end{macrocode}
322 % The code that follows has been taken and subsequently modified from file \texttt{xetex.def},
323 % which is {\em not yet} part of the Standard LaTeX ``Graphics Bundle'' (see the documentation of
324 % the package's source code for more details).
325 % \begin{macrocode}
326 \def\color@spotcolor#1#2{\c@lor@@spotcolor#2\@@#1}
327 \def\c@lor@@spotcolor#1,#2\@@#3{%
328 \c@lor@arg{#2}%
329 \edef#3{spot #1 #2}%
330 }
331 % \end{macrocode}
332 % The code that follows has been copied and subsequently modified from package
333 % \textsf{xspotcolor}. Obviously, the code uses the infrastructure provided by
334 % package \textsf{xcolor} to allow users to define new spot colors (see the
335 % documentation of the source code of package \textsf{xcolor} for more details).
336 % \begin{macrocode}
337 \else
338 \XC@sdef\XC@mod@spotcolor{spotcolor}
339 \def\@@xespotcolordef@xspc#1,#2\@@{spot #1 #2}
340 \let\o@XC@definec@lor@xspc\XC@definec@lor
341 \def\XC@definec@lor[#1]#2[#3]#4#5%
342 {%
343 \expandafter\ifx\csname XC@mod@#4\endcsname\XC@mod@spotcolor
344 \expandafter\xdef\csname\string\color @#2\endcsname
345 {\noexpand\xcolor@{}{\@@xespotcolordef@xspc#5\@@}{spotcolor}{#5}}%
346 \else
347 \o@XC@definec@lor@xspc[#1]{#2}[#3]{#4}{#5}%
348 \fi
349 }%
350 % \end{macrocode}
351 % Since \textsf{TikZ} is widely used today for drawing, the following commands are
352 % redefined to allow the use of spot colors in pictures created with this package.
353 % \begin{macrocode}
354 \gdef\pgfsys@color@spotcolor@stroke#1#2{\special{pdf:literal /#1 CS #2 SC}}
355 \gdef\pgfsys@color@spotcolor@fill#1#2{\special{pdf:literal /#1 cs #2 sc}}
356 \gdef\pgfsys@color@spotcolor#1#2{\pgfsys@color@spotcolor@stroke{#1}{#2}%
357 \pgfsys@color@spotcolor@fill{#1}{#2}}
358 \fi
359 % \end{macrocode}
360 % If the \texttt{pantone} or the \texttt{hks} option have be specified, then the package loads the corresponding
361 % color tables
362 % \begin{macrocode}
363 \ifx\use@hks\undefined\else\input{spotcolorhks}\fi
364 \ifx\use@pantone\undefined\else\input{spotcolorpantone}\fi
365 %</xespotcolor>
366 % \end{macrocode}
367 % \Finale
368 % \section{Simple Usage Example}
369 % The code that follows shows how one should use the package to create color drawings using Tikz.
370 % \begin{flushleft}
371 % |% First define a new color space|\\
372 % |\NewSpotColorSpace{MyPantone}|\\
373 % |% Now define real colors|\\
374 % |\AddSpotColor{MyPantone}{Blue}{Spot\SpotSpace Color\SpotSpace Blue}{0.92 0.24 0 0}|\\
375 % |\AddSpotColor{MyPantone}{Green}{Spot\SpotSpace Color\SpotSpace Green} {0.65 0 0.96 0}|\\
376 % |% Next we need to set the page color space|\\
377 % |\SetPageColorSpace{MyPantone}|\\
378 % |% Finally we can define colors!|\\
379 % |\definecolor{MyGreen}{spotcolor}{Green,0.5}|\\
380 % |\definecolor{MyBlue}{spotcolor}{Blue,0.5}|\\
381 % |% And this how we use these colors in drawings.|\\
382 % |\begin{tikzpicture}|\\
383 % | \filldraw[color=MyGreen] (0.1,0.1) rectangle (1.9,0.9);|\\
384 % | \draw[color=MyBlue, ultra thick] (0,0) rectangle (2,1);|\\
385 % |\end{tikzpicture}|\\
386 % \end{flushleft}
387 % \section*{Acknowledgements}
388 % I would like to thank Akira Kakuto for his comments and suggestions. Also, I would like to thank
389 % иеромонах Пантелеимон (Королев) [hieromonk Panteleimon] for his questions and testing.