summaryrefslogtreecommitdiff
path: root/Master/texmf-dist/source/xelatex/xespotcolor/xespotcolor.dtx
blob: 847374f97858508e70f36794a39bbdc1130b4313 (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
272
273
274
275
276
277
278
279
%% \CharacterTable
%%  {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
%%   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
%%   Digits        \0\1\2\3\4\5\6\7\8\9
%%   Exclamation   \!     Double quote  \"     Hash (number) \#
%%   Dollar        \$     Percent       \%     Ampersand     \&
%%   Acute accent  \'     Left paren    \(     Right paren   \)
%%   Asterisk      \*     Plus          \+     Comma         \,
%%   Minus         \-     Point         \.     Solidus       \/
%%   Colon         \:     Semicolon     \;     Less than     \<
%%   Equals        \=     Greater than  \>     Question mark \?
%%   Commercial at \@     Left bracket  \[     Backslash     \\
%%   Right bracket \]     Circumflex    \^     Underscore    \_
%%   Grave accent  \`     Left brace    \{     Vertical bar  \|
%%   Right brace   \}     Tilde         \~}
%%
%\iffalse
%
% (c) Copyright 2014 Apostolos Syropoulos 
% This program can be redistributed and/or modified under the 
% terms of the LaTeX Project Public License Distributed from 
% http://www.latex-project.org/lppl.txt; either
% version 1.3c of the License, or any later version.
%  
% This work has the LPPL maintenance status `maintained'.
%
% Please report errors or suggestions for improvement to
%
%    Apostolos Syropoulos  (asyropoulos@yahoo.com)
%
%\fi
% \CheckSum{98}
% \iffalse This is a Metacomment
%
%<xespotcolor, >\ProvidesFile{xesporcolor.sty}
%
%<xespotcolor, > [2014/11/12 v1.0 Package `xespotcolor.sty']
%
%    \begin{macrocode}
%<*driver>
\documentclass{ltxdoc}
\GetFileInfo{xspotcolor.drv}
\usepackage{xltxtra}
\usepackage{fullpage}
\begin{document}
\setmainfont[Mapping=tex-text,Script=Greek]{Arno Pro}
\setmonofont{UM Typewriter}
\setsansfont[Mapping=tex-text]{GFS Neohellenic}
   \DocInput{xespotcolor.dtx}
\end{document}
%</driver>
%    \end{macrocode}
% \fi
%\MakeShortVerb{\|}
%\StopEventually{}
%\title{The \textsf{xespotcolor} package}
%\author{Apostolos Syropoulos\\
%        Xanthi, Greece\\
%        \texttt{asyropoulos@yahoo.com}}
% \date{2014/11/12}
%\maketitle
% \begin{abstract}
% A spot color is one that is printed with its own ink. Typically, printers use spot colors
% in the production of books or other printed material. The \textsf{spotcolor} package
% by Jens Elstner is a first attempt to introduce the use of spot colors with pdfLaTeX.
% The \textsf{xespotcolor} package is a reimplementation of this package so to be usable with 
% \XeLaTeX. As such, it has the same user interface and the same capabilities.
%\end{abstract}
%
%\section{Introduction}
%
% Using spot colors with \XeLaTeX\ is very important since most printers use spot colors in the
% production of books and magazines. The \textsf{spotcolor} package makes it possible to use
% spot colors with pdf\LaTeX\ but it cannot be used with \XeLaTeX. In the following I first
% describe how to translate certain pdf\TeX\ code snippets into \XeTeX\ and then I present the
% code of the package. Thus one can view this text as a short tutorial on how to port pdf\TeX\
% code to \XeTeX\ as well as a description of the functionality of the \textsf{xespotcolor} package.
% Since the package is a port of a pdf\TeX\ package, it has the same functionality as the original
% package.
%
% \section{Porting pdf\TeX\ code to \XeTeX}
%
% Translating pdf\TeX\ code, which adds PDF code to the output file, to \XeTeX\ is not a straightforward exercise 
% since pdf\TeX\ provides primitive commands that directly access and modify the structure of the resulting PDF file.
% In the case of \XeTeX\ one has to use |\special| commands that pass code to the driver. In this particular case,
% I had to translate code snippets like the following one:
% \begin{center}
%  |1. \newcount\theCNTa                        |\\
%  |2. \newcount\theCNTb                        |\\
%  |3. \def\obj{ 0 R}%                          |\\
%  |4. \pdfobj{Raw PDF code 1}%                 |\\
%  |5. \theCNTa=\the\pdflastobj%                |\\
%  |6. \pdfobj{Raw PDF code \the\theCNTa \obj}% |\\  
%  |7. \theCNTb=\the\pdflastobj%                |\\
%  |8. \pdfrefobj\theCNTa%                      |\\
%  |9. \pdfrefobj\theCNTb%                      |\\
% \end{center}
% Here pdf\TeX\ creates two PDF objects, where the second contains a reference to the first one. The two counters
% defined in lines 1 and 2 are used to reference these two objects. The macro on line 3 is used to create code
% that references an object. The commands on lines 5 and 7 assign the object reference numbers and these numbers are
% used by the |\pdfrefobj| primitive. After some experimentation and some… Googling, I have found out that the following 
% \XeTeX\ code is a reasonable translation of the previous code snipper:
% \begin{center}
%  |\newcount\CNT                    |\\
%  |\newtoks\TOK                     |\\
%  |\TOK={@TOK \the\CNT}%            |\\
%  |\advance\CNT by1%                |\\
%  |\edef\A{\the\TOK Raw PDF code 1}%|\\
%  |\edef\B{Raw PDF code \the\TOK}%  |\\  
%  |\special{pdf:obj \A}%            |\\
%  |\special{pdf:obj \B}%            |\\
% \end{center}
% The two |\edef|initions are used to do the work done by |\pdfobj|. Note that here there I introduce only 
% one unique object and the first two lines define a counter and a token variable. The token variable uses
% the counter to create a unique identifier, which is passed to the driver. This way the driver will 
% create a number of different objects, if required to do so. The last two commands pass the raw PDF
% code and the unique identifier to the driver.
%
% The original package contains a definition identical to the following one:
% \begin{center}
% |\def\R#1{%                                                           |\\
% |   \edef\act{\noexpand\pdfpageresources={\the\pdfpageresources\space |\\
% |          /ColorSpace<<#1>>}}                                        |\\
% |   \act}                                                             |\\
% \end{center}
% The net effect of this command is to add a specific color space to the page resources of all subsequent pages.
% Unfortunately, when the following code is executed, it adds the particular color space to the current page only: 
% \begin{center}
%  |\def\R#1{%                                              |\\
%  |    \special{pdf:put @resources <</ColorSpace <<#1>>>>}}|\\
% \end{center} 
% In order to add the color space to all subsequent pages, I had to use the |\AddEverypageHook| command of
% package \textsf{everypage}. This command modifies the contents of the ship-out box by adding to it its argument. 
% And this is done for every single page. Also, one should note how the page resources are augmented by the two
% systems. In the case of \XeTeX\ we have to create a PDF dictionary that is merged with the current page resources,
% while in the case of pdf\TeX\ one just ``appends'' what is supposed to be included in the page resources dictionary.
% Let me now proceed with the description of the source code of the package.
%
% \section{The Source Code and Package Usage}
% The first part of the code is the identification part.
%    \begin{macrocode}
%<*xespotcolor>
\NeedsTeXFormat{LaTeX2e}
\ProvidesPackage{xespotcolor}
          [2014/11/12 v.1.0, Package for adding Spot Color support to XeLaTeX.]
%    \end{macrocode}
% The package needs three packages in to operate properly: \textsf{graphics}, \textsf{color}, and \textsf{everypage}.
% The first two packages must be loaded with the \texttt{xetex} option, since auto-detection does not work in this case.
%    \begin{macrocode}
\RequirePackage[xetex]{graphics}
\RequirePackage[xetex]{color}
\RequirePackage{everypage}
%    \end{macrocode}
% The original package defines two ``boolean'' variables that are set when the package is loaded with the \texttt{hks} and 
% the \texttt{pantone} options, respectively. When these variables are true, then the package pre-loads color values in the 
% corresponding color space (i.e., ``hks'' and ``pantone''). These color values are stored in two separate source files.
% These files are part of the original package distribution.
%    \begin{macrocode}
\newif\ifhks\hksfalse
\newif\ifpantone\pantonefalse
\DeclareOption{hks}{\hkstrue}		
\DeclareOption{pantone}{\pantonetrue}	
\ProcessOptions
%    \end{macrocode}
%
% The |\NewSpotColorSpace| command should be used to define a new color space for spot colors. The new color space can be
% any imaginable word! This color space is the place where a new spot color will live. The  \texttt{hks} and 
% the \texttt{pantone} options create two color spaces where the corresponding colors live.
%    \begin{macrocode}
\def\NewSpotColorSpace#1{%
  \expandafter\newtoks\csname #1\endcsname%
  \csname #1\endcsname{}%
}
%    \end{macrocode}
% The |\AddSpotColor| macro should be used to introduce a new spot color. Thus it is one of the first commands on should play 
% with when using this package. This command takes four parameters---the name of a new color 
% space, which has been declared with |\NewSpotColorSpace|, the name of a new color, a name which will be used internally, and a 
% CMYK representation of the new color. For example, here is a typical use of this macro:
% \begin{center}
%|\AddSpotColor{NEWCS}{NEWCOLOR}{Name\SpotSpace in\SpotSpace PDF}{0.5 1.0 0.51 0}|
% \end{center}
% Macro |\SpotSpace| is used to introduce blanks in names. According to the PDF standard a blank is denoted by the
% character sequence |#20| and this is the reason why |\SpotSpace| is defined as follows:
%    \begin{macrocode}
\catcode`\#=12%
\def\SpotSpace{#20}
\catcode`\#=6%
%    \end{macrocode}
%The macro |\csgrab| is quite unsual---it takes two arguments and creates a sequence of them.
%    \begin{macrocode}
\gdef\csgrab#1#2{#2\expandafter{\the#2 #1}}%
%    \end{macrocode}
%Note that this command should never be used by an ordinary package user.
%
% The following variables are used in the definition of macro |\AddSpotColor|. Their functionality has been
% described in the previous section.
%    \begin{macrocode}
\newcount\colorprofilecnt
\newtoks\mycolorprofilename
%    \end{macrocode}
% Macro |\AddSpotColor| first defines a new color profile by assigning a value to |\mycolorprofilename|. The name
% consists of the word |@mycolorprofile| followed by an integer. And since all names must be distinct
% the macro increments the value of the |\colorprofilecnt| counter by one.
%    \begin{macrocode}
\def\AddSpotColor#1#2#3#4{%
  \mycolorprofilename={@mycolorprofile\the\colorprofilecnt}%
  \advance\colorprofilecnt by1%
%    \end{macrocode}
% The following two macros expand to PDF instructions that define the spot color. The PDF instructions are copied verbatim from 
% the original \textsf{spotcolor} package. 
%    \begin{macrocode}
  \edef\mycolorprofile{\the\mycolorprofilename 
          <</C0[0 0 0 0]/FunctionType 2/C1[#4]/Domain[0 1]/N 1>>}%
  \edef\mycolor{[/Separation/#3 /DeviceCMYK \the\mycolorprofilename]}%
%    \end{macrocode}
% The next two lines have been copied verbatim from the original macro definition.
%    \begin{macrocode}
  \edef\tempcs{/#2 \mycolor}%
  \expandafter\csgrab\expandafter{\tempcs}{\csname #1\endcsname}%
%    \end{macrocode}
% In the last part of the macro definition, the driver is instructed to build two objects which should contain the definition of
% the new spot color.
%    \begin{macrocode}  
  \special{pdf:obj \mycolorprofile}% 
  \special{pdf:obj \mycolor}%
}
%    \end{macrocode}
%
% Command |\SetPageColorResource| is used by command |\SetPageColorSpace| to set the color space.
% The |\special| command below sets the page resources only for the current page. Since the color
% space should be visible to every subsequent page, I have opted to use command |\AddEverypageHook| of the 
% \textsf{everypage} package. 
%    \begin{macrocode}
\def\SetPageColorResource#1{%
  \AddEverypageHook{\special{pdf:put @resources <</ColorSpace <<#1>>>>}}%
}%
\def\SetPageColorSpace#1{%
	\expandafter\SetPageColorResource\expandafter{\the\csname #1\endcsname}%
}%
%    \end{macrocode}
% If a user wants to set a spot color as the default color, she should use the |\SpotColor| command:
%    \begin{macrocode}
\def\SpotColor#1#2{%
	\special{pdf:literal /#1 cs /#1 CS #2 sc #2 SC}%
        \aftergroup\reset@color%
}%
%    \end{macrocode}
% The commands |\color@spotcolor| and |\c@lor@@spotcolor| are ``low-level'' commands and should never be used by
% anyone. These commands are used to define new spot colors. They are automatically
% executed everytime a someone uses a command that defines a new spot color:
% \begin{center}
% |\definecolor{Spots}{spotcolor}{SPCOLOR,1.0}|
% \end{center}
% Note that the second argument must always be |spotcolor|. In addition, this command is meaningful only when
% one has defined a few things using command like the following ones:
%\begin{center}
%\begin{tabular}{l}
%|\NewSpotColorSpace{SPCOLORSPACE}|\\
%|\AddSpotColor{SPCOLORSPACE}{SPCOLOR}{Some\SpotSpace Name}{0.5 1.0 0.51 0}|\\
%|\SetPageColorSpace{SPCOLORSPACE}|\\
%\end{tabular}
%\end{center}
% The code that follows has been taken and subsequently modified from \texttt{xetex.def},
% which  is {\em not yet} part of the Standard LaTeX ``Graphics Bundle.'' Note that these command work only
% if the \texttt{xdvipdfm-x} driver has been patched to recognize the \texttt{spot} command. 
%    \begin{macrocode}
\def\color@spotcolor#1#2{\c@lor@@spotcolor#2\@@#1}
\def\c@lor@@spotcolor#1,#2\@@#3{%
  \c@lor@arg{#2}%
  \edef#3{spot #1 #2}%
}
%    \end{macrocode}
% If pantone or hks option specified then load corresponding color tables
%    \begin{macrocode}
\ifhks\input{spotcolorhks}\fi
\ifpantone\input{spotcolorpantone}\fi
%</xespotcolor>
%    \end{macrocode}
% \Finale