Debian Codegraph @

texmf-dist/doc/latex/pdftoolbox/Documentation/pdfgraphics-internal-docs.tex

raw
1 \subsection{Colors}
2
3 There are some useful macros in the {\tt pdfGraphics/colors.tex}, here we describe them.
4
5 \bwarning
6 These macros and file require a clean-up.
7 Unfortunately many other macros are dependent on them, and I am scared to significantly alter anything.
8 One day, though.
9 \eppbox
10
11 \macroexp{\_rgb_encode {<rgb code>}/cr
12 \_rgb_encodebg {<rgb code>}/cr
13 \_rgb_encodefg {<rgb code>}/cr
14 \_cmyk_encode {<cmyk code>}/cr
15 \_cmyk_encodebg {<cmyk code>}/cr
16 \_cmyk_encodefg {<cmyk code>}}
17 Gets the code for the specified color for the foreground or background or both.
18 \emacroexp
19
20 \macroexp{\_setcolor_code {<pdf code>}}
21 Sets the current color using {\it pdf code} (which can be obtained using one of the above macros).
22 Essentially just pushing {\it pdf code} onto the color stack.
23 After the current group, calls \gotomacro\_pdfcolor_restore.
24 \emacroexp
25
26 \macroexp{\_pdfcolor_restore}
27 Restores the color (pops from the color stack).
28 \emacroexp
29
30 \macroexp{\_color_set {<color space>}{<color code>}/cr
31 \_colorbg_set {<color space>}{<color code>}/cr
32 \_colorfg_set {<color space>}{<color code>}}
33 Sets the current color using {\it color code} according to {\it color space} (either {\tt rgb} or {\tt cmyk}).
34 \emacroexp
35
36 \macroexp{\_color_defined {<name>}/cr
37 \_colorbg_defined {<name>}/cr
38 \_colorfg_defined {<name>}}
39 Sets the current color according to the color {\it name} (see \gotomacro\definecolor).
40 \emacroexp
41
42 \macroexp{\_getcolorparam <macro>{<place>}<color>}
43 Gets the pdf code for {\it color} (which may be of the form \inlinecode|rgb{...}|, \inlinecode|cmyk{...}|, or \inlinecode|{name}|), and calls {\it macro} with it as a parameter.
44 {\it place} is either {\tt fg}, {\tt bg}, or left empty.
45 \emacroexp
46
47 \macroexp{\_setcolor {<place>}{<color>}}
48 Sets the current color according to {\it place} and {\it color}.
49 {\it place} is either {\tt fg}, {\tt bg}, or left empty.
50 \emacroexp
51
52 \macroexp{\_getcolor {<place>}{<color>}}
53 Expands to the pdf code for {\it color} ({\it place} is either {\tt fg}, {\tt bg}, or left empty).
54 \emacroexp
55
56 \subsection{Colorboxes}
57
58 \pdftoolbox{} provides a relatively simple interface for creating colorboxes like \macro\bppbox.
59 The main macro is \macro\_splitcontentbox\anchormacro\_splitcontentbox, whose usage is
60
61 \getmacrousage{\_splitcontentbox {<buffer>}<macroT><macroS><macroM><macroE>}
62
63 Which repetitively splits the box \macro\_contentbox\anchormacro\_contentbox{} into \macro\_splitbox{} to fill the remaining material on a page or in the box itself.
64 Then the split box is passed to {\it macroX} for pretty formatting.
65 {\it macroT} is if the material fits entirely on a single page, otherwise the first box uses {\it macroS}, the
66 last box uses {\it macroE}, and all intermediate boxes use {\it macroM}.
67 {\it buffer} is the total amount of vertical buffering that {\it macro} adds to the box it prints.
68
69 So to create your own prettyprint-box (ppbox), you create two macros, say \macro\beginpp{} and \macro\endpp.
70 In \macro\beginpp{} you add the code which should go before the ppbox and starts getting content for \macro\_contentbox.
71 For example, it could be as simple as:
72
73 \blisting
74 \def\beginpp#1#2{%
75 \def\_colorcontentbox{%
76 \hbox{\coloredbox{#1}{\_setcolor{}{#2}\box\_splitbox}}%
77 }%
78 \par\kern.5cm\null\par%
79 \setbox\_contentbox=\vbox\bgroup
80 \hsize=\dimexpr\hsize-\bufferwidth * 2\relax%
81 }
82
83 \def\endpp{%
84 \egroup%
85 \_splitcontentbox{\bufferwidth * 2}%
86 \_colorcontentbox\_colorcontentbox\_colorcontentbox\_colorcontentbox%
87 \kern.5cm\relax%
88 }
89 \elisting
90
91 This creates a ppbox which is simply a wrapper around \gotomacro\coloredbox.
92 It colors the background in \inlinecode|#1| and the foreground in \inlinecode|#2|.
93
94 In depth, here's how it works:
95 \benum
96 \item First, \macro\beginpp{} defines \macro\_colorcontentbox{} to simply place \macro\_splitbox{} into a \macro\coloredbox{} of color \inlinecode|#1|, and sets the foreground color to \inlinecode|#2|.
97 \item Then it adds some space before the start of the first ppbox.
98 The reason for the \inlinecode|\null\par| is to move the kern from the list of recent contributions to the main vertical list (see, e.g. the \TeX book for more information on \TeX's output routines).
99 \item Then \macro\beginpp{} begins reading content for \macro\_contentbox.
100 It alters \macro\hsize{} to compensate for the buffer added by \macro\coloredbox.
101 \item When \macro\endpp{} is called, it first stops the capture of \macro\_contentbox{} with \macro\egroup.
102 \item Then it calls \inlinecode|\_splitcontentbox{\bufferwidth * 2}\_colorcontentbox|, which splits the captured material (in \macro\_contentbox) and places each \macro\_splitbox{} in \macro\_colorcontentbox, which was
103 defined in \macro\beginpp.
104 \inlinecode|\bufferwidth * 2| corresponds to the amount of vertical buffering \macro\_colorcontentbox{} adds to \macro\_splitbox.
105 \item \macro\endpp{} adds buffering after the final ppbox.
106 \eenum
107
108 \subsubsection{The Mechanism}
109
110 In this section we describe the mechanism through which \pdftoolbox{} creates colorboxes.
111 A good reference for this section, which discusses the mechanism through which \TeX{} creates pages (the output routine) is ``The Advanced \TeX book'' by David Salomon.
112
113 We begin with a simple macro which expands to the amount of space left in the page.
114 \listfile{ptb-colorboxes}[9-15]
115 \macro\pagetotal{} is the height of the main vertical list (MVL).
116 \macro\pagegoal{} is the goal height of the MVL.
117 It is generally equal to \macro\vsize, but when footnotes or similar are generated, their heights are
118 subtracted from \macro\pagegoal.
119 At the beginning of each page, \macro\pagegoal{} is set to \macro\maxdimen{} (hence the use of the
120 \macro\ifdim).
121
122 \pdftoolbox{} stores the content to be split across pages in \inlinecode|\box\_contentbox|.
123
124 The following macro \macro\_getsplitdim{} gives the dimension that \macro\_contentbox{} should be split into.
125 Its parameter is the size of the extra vertical glue that is added to each box.
126 \listfile{ptb-colorboxes}[17-41]
127 \benum
128 \item First we check if there is enough space to place the entirety of the box into the page.
129 If so set \macro\_splitdim{} to the height of the box, and set \macro\_lastbox{} to true since we wil be placing the entirity of the box.
130 \item Otherwise, check if there is any space left on the current page (recall that \inlinecode|#1| is the
131 amount of vertical space added.)
132 If there is, then set \macro\_splitdim{} to the amount of space left.
133 \item Otherwise, we perform the following checks:
134 \benum
135 \item If \macro\_recheck{} is false, then we add \inlinecode|\null\par| to the page.
136 This just moves all recent contributions to the MVL.
137 The reason for this is that we need to get rid of all the material that came before the
138 colorbox, and we then recheck the dimension, and set \macro\_recheck{} to true.
139 \item Otherwise, the MVL is up-to-date, and there is still not enough room.
140 So we try to fill in the rest of the vertical space and \macro\break.
141 If for whatever reason this doesn't work, we add \macro\vfilneg{} to remove the glue added.
142 \eenum
143 In either case we get the dimension again.
144 \eenum
145
146 Now, the main macro is \macro\_splitcontentbox:
147 \listfile{ptb-colorboxes}[46-77]
148 It first sets \macro\splittopskip{} to $0{\tt pt}$ so that no extra glue is added to the top of \macro\vsplit.
149 Then \macro\boxmaxdepth{} is also set to $0{\tt pt}$ so that the depth of the split boxes will be $0{\tt pt}$
150 and we can deal only with height.
151 We also turn off interlineskip so there is no extra glue added around the split boxes (these do not affect
152 the contents of \macro\_contentbox{} since it has already been boxed).
153
154 Now we repeat until \macro\_contentbox{} is empty:
155 \benum
156 \item we get the amount of space to split the box into via \macro\_getsplitdim{} (explained above);
157 \item we split \macro\_contentbox{} into \macro\_splitbox{} of this dimension;
158 \item \inlinecode|#2| is the output routine of the colorbox, it places the contents of \macro\_splitbox{}
159 into whatever format the user specifies.
160 We set \inlinecode|\box0| to this;
161 \item we add the box to the page, and set a penalty of $0$ so that the page can be broken at that point
162 if necessary.
163 \eenum
164
165 The definition of \macro\bppbox{} and \macro\eppbox{} are a little enlightening:
166 \listfile{ptb-colorboxes}[79-116]
167 What's of interest here is how \macro\_bppbox{} defines the different \macro\_colorcontentbox s.
168 Firstly, \macro\_getdotsnlines{} is a macro which defines \macro\_line{\it side} and \macro\_dot{\it corner} according to the stream of $8$ characters which follow it (until \macro\_nul).
169 Now, \macro\_colorcontentboxT{} is defined as you'd expect.
170 And \macro\_colorcontentboxS{} is defined so that it preserves the top corners that are input as well as all but the bottom side.
171 \macro\_colorcontentboxM{} and \macro\_colorcontentboxE{} are defined similarly.
172
173 \subsection{Illustrating} \anchor{internal-pdfdraw}
174
175 \bwarning
176 This is a complicated and messy part of \pdftoolbox.
177 Documentation will be added once it is cleaned up.
178 \eppbox
179
180 \subsection{Listings} \anchor{internal-listings}
181
182 \subsubsection{The Mechanism}
183
184 We first begin by discussing the mechanism for how listings work in \pdftoolbox.
185 Credit where credit is due; the mechanism is greatly inspired and copied from Petr Ol\v s\'ak's \url{https://ctan.org/pkg/optex?lang=en}{Op\TeX}, though the implementation may differ.
186
187 The mechanism is relatively simple: all \pdftoolbox{} does is the following:
188 \benum
189 \item call \inlinecode|\the\everylisting| and whatever is given in the remaining line after \macro\blisting;
190 \item call \macro\setupverb{} which changes the catcode of special characters and \inlinecode|^^M| to $12$;
191 \item capture the entirety of the listing from \macro\blisting{} to \macro\elisting;
192 \item set line spacing (via \macro\_setuplstlines);
193 \item call \inlinecode|\the\_commandcapture| which simply sets up macros which are called in \inlinecode|\the\_listingcommands|, which is called immediately afterward (see below);
194 \item call \inlinecode|\the\_commandexecute| which executes the commands added in \inlinecode|\the\_listingcommands| on the listing, and then \inlinecode|\the\_macrocallmanager| which alters the definitions of the macros in
195 \macro\_listingcommands{} to their proper definitions (explanation later);
196 \item at this point, the listing is set up so that everything is ready for printing;
197 \item the line manager is set up (which handles printing each line in the listing);
198 \item the additional vertical buffering added by \macro\syntaxoutbox{} is computed into \macro\syntaxoutboxbuf{} by \macro\syntaxoutboxsetbuf;
199 \item the listing is processed through a ppbox dictated by \macro\syntaxoutbox.
200 \eenum
201
202 This is a deceptive description of how this process works.
203 But what's important is \macro\_commandexecute, which has all the commands for setting up syntax highlighting and the verbatim environment.
204 This is not a token list to messed with by the user directly, it should be done indirectly through \macro\_listingcommands, which in turn should be altered indirectly through ptb-syntax files.
205
206 But if no syntax is set, the definition of \macro\_commandexecute{} is essentially:
207
208 \blisting
209 \_commandexecute={
210 \_execute{\_r_replace{ }{\w \w}}
211 \_execute{\_r_replace{^^M}{\w\n\n\w}}
212 }
213 \elisting
214
215 \macro\_execute{} is defined simply to \inlinecode|\def\_execute#1{#1\_code}| right before execution, where \macro\_code{} is the captured listing.
216 And \macro\_r_replace{} is defined to simply be \inlinecode|\def\_r_replace#1#2#3{\replace{#3}{#1}{#2}}|.
217 So when \macro\_commandexecute{} is called, the result is simply two lines: \inlinecode|\replace{\_code}{ }{\w \w}| and \inlinecode|\replace{\_code}{^^M}{\w\n\n\w}|.
218
219 \macroexp{\replace <macro>{<pattern>}{<replacement>}}
220 Replaces (the expansion of) {\it pattern} with (the expansion of) {\it replacement} in the definition of {\it macro}.
221 \emacroexp
222
223 So the result is that now \macro\_code{} contains the listing, but where each space is now swapped with \inlinecode|\w \w| and each line ending with \inlinecode|\w\n\n\w|.
224 \macro\_code{} is actually defined to be the listing wrapped in \inlinecode|\n\w...\w\n|, so the result is that every line in the listing is wrapped in \inlinecode|\n...\n| and every word is wrapped in \inlinecode|\w...\w|.
225
226 Now suppose you wanted to replace all occurrences of {\tt hi} with {\tt hello}.
227 You'd need to add the line \inlinecode|\_execute{\_r_replace{\w hi\w}{\w hello\w}}| to \macro\_commandexecute{} (note that the pattern and replacement are wrapped in \macro\w s).
228 You can do so with the following command:
229
230 \macroexp{\_add_command_replace {<pattern>}{<replacement>}}
231 Adds \getmacrousage{\_execute{\_r_replace{<pattern>}{<replacement>}}} to \macro\_commandexecute.
232 \emacroexp
233
234 But this is unwieldy, so the actual mechanism used is as follows:
235 \benum
236 \item \macro\_commandcapture{} sets the definitions of \macro\replace{} and \gotomacro\replacefromto{} (see below) to\hfill\break\macro\_add_command_replace{} and \macro\_add_command_replacefromto{}.
237 \item \macro\_listingcommands{} contains uses of \macro\replace{} and \macro\replacefromto{}, which are executed.
238 This adds the required lines to \macro\_commandexecute.
239 \item \macro\_commandexecute{} is executed.
240 \eenum
241
242 Now to explain \gotomacro\loadsyntax{} and \gotomacro\setsyntax: \getmacrousageB{\loadsyntax {<language>}} reads from the ptb-syntaxptb-syntax file {\tt ptb-syntax-{\it language}.tex}, which should define a token list
243 \macro\_{\it language}{\tt _listingcommands}.
244 Then \macro\setsyntax{} simply sets \macro\_listingcommands{} to \macro\_{\it language}{\tt _listingcommands}.
245
246 \subsubsection{Usage}
247
248 Now notice an issue: \macro\replace{} (and \macro\replacefromto) both expand their arguments.
249 What if the arguments expand to invalid code?
250 This is the purpose of \macro\_macrocallmanager{} and two basic macros: \macro\call{} and \macro\mlcall.
251 They are set to \macro\relax{} so they aren't expanded in \macro\replace{} and friends, and \macro\_macrocallmanager{} sets them to their proper definitions:
252
253 \macroexp{\call <macro>{<parameters>}{<last>}}
254 Simply calls \getmacrousageB{\macro <parameters>{<last>}}.
255 \emacroexp
256
257 \macroexp{\mlcall <macro>{<parameters>}{<last>}}
258 If {\it last} is equal to \inlinecode|x1\n\n x2\n\n...\n\n XN|, expands to \inlinecode|\call <macro>{<parameters>}{x1}\n\n...\n\n\call <macro>{<parameters>}{xN}|.
259 \emacroexp
260
261 Since \macro\call{} and friends are redefined only after \macro\_commandexecute{} is executed, to call a macro without it expanding you can use them.
262 For example, to replace {\tt int} with {\tt int} colored red, you can use \inlinecode|\replace{\w int\w}{\w\call localcolor{{red}}{int}\w}|.
263
264 Two shortcuts are provided: \macro\c{} and \macro\mc{}.
265 \getmacrousageB{\c{<color>}{<text>}} will set the color of {\it text} (in a \macro\call), and \getmacrousageB{\mc{<color>}{<text>}} will also set the color but in a \macro\mlcall.
266
267 Now what is \macro\replacefromto?
268
269 \macroexp{\replacefromto <macro>{<start>}{<end>}{<replacement>}}
270 Replacement is a macro definition with a single pattern (e.g. \inlinecode|{#1}|).
271 \macro\replacefrom{} matches \getmacrousage{<start>#1<end>} in the expansion of {\it macro} and replaces it with {\it replacement}, this redefines {\it macro}.
272
273 So for example if \macro\X{} expands to \inlinecode|(.)(.)|, \inlinecode|\replacefrom\X(){[#1]}| will redefine \macro\X{} to be \inlinecode|[.][.]|.
274 \emacroexp
275
276 \subsubsection{An Example}
277
278 \pdftoolbox{} provides syntax highlight for the C language in {\tt ptb-syntax-C.tex}, whose content is:
279
280 \listfile{ptb-syntax-C}
281
282 Let us now explain each part of the file:
283
284 \listfile{ptb-syntax-C}[1-9]
285
286 This defines two new token lists, \macro\lstCcolor{} (for use within the file), and \macro\_C_listingcommands,
287 which as explained previously is what sets the listing commands for the {\tt C} syntax.
288 \macro\lstCcolor{} has C-specific colors, changing it allows you to change the colors of C-specific
289 highlighting.
290 Note that all changes in this file must be global.
291
292 \listfile{ptb-syntax-C}[11-12]
293
294 Begins the definition of \macro\_C_listing_commands{} by first defining C-specific colors.
295
296 \listfile{ptb-syntax-C}[13-17]
297
298 \benum
299 \item Swaps \macro\"{} with \inlinecode|{\"}|, so that the quotation in \macro\"{} (backslash-quote) is
300 not replaced by the following lines.
301 \item Colors between \inlinecode|"| and \inlinecode|"| with a multiline coloring of color {\tt quote}.
302 \item Colors between \inlinecode|//| and \macro\n{} (the end of the line) with the color of a comment.
303 Notice that it adds back in the \macro\n; otherwise this will mess up the line-reading.
304 \item Replaces between \inlinecode|/*| and \inlinecode|*/| with a multline coloring of color
305 {\tt lst-comment}.
306 \item Replaces from \inlinecode|#| to the end of line with a coloring of color {\tt preprocessor}.
307 (Again adding back in \macro\n.)
308 \eenum
309
310 \listfile{ptb-syntax-C}[18-22]
311
312 Sets replacement for \TeX-reserved characters (open and close curly braces, percent).
313
314 \listfile{ptb-syntax-C}[23-25]
315
316 Replaces non-character letters with a coloring and word break (since, e.g. {\tt x.y} is not a single word).
317 Note the use of \gotomacro\map.
318
319 \listfile{ptb-syntax-C}[26-32]
320
321 Replaces keywords with color {\tt keyword}.
322
323 \listfile{ptb-syntax-C}[33-34]
324
325 Colors numbers with {\tt lst-number}.
326
327 \subsubsection{Changing the Output}
328
329 \pdftoolbox{} outputs the listing in colorboxes according to \macro\syntaxoutbox.
330 The default is just as follows:
331
332 \listfile{ptb-listings}[121-127]
333
334 This just places the content in a colored box of color {\tt lst-bg} with a text color of {\tt lst-fg}, along with placing a header and footer.
335 We must also set \macro\syntaxoutboxbuf, which is the total amount of vertical buffering added by
336 \macro\syntaxoutbox{} to its contents.
337 This is done by \macro\syntaxoutboxsetbuf{} which must define a macro \macro\syntaxoutboxbuf{} to be the total amount of extra vertical space \macro\syntaxoutbox{} adds.
338
339 But we can also do, for example:
340
341 \blisting
342 \letcolor{lst-stroke}{lst-fg}
343 \def\syntaxoutbox#1{%
344 \hbox{\curvedcolorbox{lst-stroke}{lst-bg}{\_setcolor{}{lst-fg}#1}{-.-.-.-.}}%
345 }
346 \def\syntaxoutboxsetbuf{%
347 \edef\syntaxoutboxbuf{\the\dimexpr\_actual_curve_buf * 2\relax}%
348 }
349 \elisting
350
351 \bgroup
352 \letcolor{lst-stroke}{lst-fg}
353 \def\syntaxoutbox#1{%
354 \hbox{\curvedcolorbox{lst-stroke}{lst-bg}{\_setcolor{}{lst-fg}#1}{-.-.-.-.}}%
355 }
356 \def\syntaxoutboxsetbuf{%
357 \edef\syntaxoutboxbuf{\the\dimexpr\_actual_curve_buf * 2\relax}%
358 }
359
360 Now listings will have a curve colored box:
361
362 \blisting
363 \def\X#1#2{%
364 \ifnum#1>0 %
365 {\X{\numexpr#1-1\relax}{#2}}^{\X{\numexpr#1-1\relax}{#2}}_{\X{\numexpr#1-1\relax}{#2}}%
366 \else%
367 \vcenter{\hbox{$\m@th\scriptscriptstyle#2$}}%
368 \fi%
369 }
370
371 $$\X{4}\blacksquare$$
372 \elisting
373 \egroup
374
375 (The code outputs the following by the way:)
376
377 \bgroup\catcode`_=8
378 \def\X#1#2{%
379 \ifnum#1>0 %
380 {\X{\numexpr#1-1\relax}{#2}}^{\X{\numexpr#1-1\relax}{#2}}_{\X{\numexpr#1-1\relax}{#2}}%
381 \else%
382 \vcenter{\hbox{$\m@th\scriptscriptstyle#2$}}%
383 \fi%
384 }
385
386 $$\X{4}\blacksquare$$
387 \egroup
388
389 You can also alter how each line is output.
390 This is done by \anchormacro\_syntax_linemanager, which takes a single input and outputs the line to print in the listing.
391 The default definition is
392 \listfile{ptb-listings}[212-229]
393 But a simpler definition would be just
394 \blisting
395 \def\_syntax_linemanager#1{{
396 \def\_regA{#1}%
397 \def\_regB{\w}%
398 \unless\ifx\_regA\_regB%
399 \hbox{#1}%
400 \fi%
401 }}
402 \elisting
403 Which will simply output the line as-is; without line numbering or splitting over multiple lines.
404 The reason for the conditional is because \pdftoolbox{} will pass an empty line containing only \macro\w{} to \macro\_syntax_linemanager{} at the last line (this is the only line to do so).
405
406 \bnote
407 The passing of \macro\w{} as the final line was unintended behavior, but it could be useful and was therefore kept.
408 \eppbox
409