Debian Codegraph @

texmf-dist/source/latex/luacolor/luacolor.dtx

raw
1 % \iffalse meta-comment
2 %
3 % File: luacolor.dtx
4 % Version: 2023-08-18 v1.18
5 % Info: Color support via LuaTeX's attributes
6 %
7 % Copyright (C)
8 % 2007, 2009-2011 Heiko Oberdiek
9 % 2016-2023 Oberdiek Package Support Group
10 % https://github.com/ho-tex/luacolor/issues
11 %
12 % This work may be distributed and/or modified under the
13 % conditions of the LaTeX Project Public License, either
14 % version 1.3c of this license or (at your option) any later
15 % version. This version of this license is in
16 % https://www.latex-project.org/lppl/lppl-1-3c.txt
17 % and the latest version of this license is in
18 % https://www.latex-project.org/lppl.txt
19 % and version 1.3 or later is part of all distributions of
20 % LaTeX version 2005/12/01 or later.
21 %
22 % This work has the LPPL maintenance status "maintained".
23 %
24 % The Current Maintainers of this work are
25 % Heiko Oberdiek and the Oberdiek Package Support Group
26 % https://github.com/ho-tex/luacolor/issues
27 %
28 % This work consists of the main source file luacolor.dtx
29 % and the derived files
30 % luacolor.sty, luacolor.pdf, luacolor.lua,
31 %
32 % Distribution:
33 % CTAN:macros/latex/contrib/luacolor/luacolor.dtx
34 % CTAN:macros/latex/contrib/luacolor/luacolor.pdf
35 %
36 % Unpacking:
37 % (a) If luacolor.ins is present:
38 % tex luacolor.ins
39 % (b) Without luacolor.ins:
40 % tex luacolor.dtx
41 % (c) If you insist on using LaTeX
42 % latex \let\install=y\input{luacolor.dtx}
43 % (quote the arguments according to the demands of your shell)
44 %
45 % Documentation:
46 % (a) If luacolor.drv is present:
47 % latex luacolor.drv
48 % (b) Without luacolor.drv:
49 % latex luacolor.dtx; ...
50 % The class ltxdoc loads the configuration file ltxdoc.cfg
51 % if available. Here you can specify further options, e.g.
52 % use A4 as paper format:
53 % \PassOptionsToClass{a4paper}{article}
54 %
55 % Programm calls to get the documentation (example):
56 % pdflatex luacolor.dtx
57 % makeindex -s gind.ist luacolor.idx
58 % pdflatex luacolor.dtx
59 % makeindex -s gind.ist luacolor.idx
60 % pdflatex luacolor.dtx
61 %
62 % Installation:
63 % TDS:tex/latex/luacolor/luacolor.sty
64 % TDS:scripts/luacolor/luacolor.lua
65 % TDS:doc/latex/luacolor/luacolor.pdf
66 % TDS:source/latex/luacolor/luacolor.dtx
67 %
68 %<*ignore>
69 \begingroup
70 \catcode123=1 %
71 \catcode125=2 %
72 \def\x{LaTeX2e}%
73 \expandafter\endgroup
74 \ifcase 0\ifx\install y1\fi\expandafter
75 \ifx\csname processbatchFile\endcsname\relax\else1\fi
76 \ifx\fmtname\x\else 1\fi\relax
77 \else\csname fi\endcsname
78 %</ignore>
79 %<*install>
80 \input docstrip.tex
81 \Msg{************************************************************************}
82 \Msg{* Installation}
83 \Msg{* Package: luacolor 2023-08-18 v1.18 Color support via LuaTeX's attributes (HO)}
84 \Msg{************************************************************************}
85
86 \keepsilent
87 \askforoverwritefalse
88
89 \let\MetaPrefix\relax
90 \preamble
91
92 This is a generated file.
93
94 Project: luacolor
95 Version: 2023-08-18 v1.18
96
97 Copyright (C)
98 2007, 2009-2011 Heiko Oberdiek
99 2016-2023 Oberdiek Package Support Group
100
101 This work may be distributed and/or modified under the
102 conditions of the LaTeX Project Public License, either
103 version 1.3c of this license or (at your option) any later
104 version. This version of this license is in
105 https://www.latex-project.org/lppl/lppl-1-3c.txt
106 and the latest version of this license is in
107 https://www.latex-project.org/lppl.txt
108 and version 1.3 or later is part of all distributions of
109 LaTeX version 2005/12/01 or later.
110
111 This work has the LPPL maintenance status "maintained".
112
113 The Current Maintainers of this work are
114 Heiko Oberdiek and the Oberdiek Package Support Group
115 https://github.com/ho-tex/luacolor/issues
116
117
118 This work consists of the main source file luacolor.dtx
119 and the derived files
120 luacolor.sty, luacolor.pdf, luacolor.ins, luacolor.drv,
121 luacolor.lua,
122
123 \endpreamble
124 \let\MetaPrefix\DoubleperCent
125
126 \generate{%
127 \file{luacolor.ins}{\from{luacolor.dtx}{install}}%
128 \file{luacolor.drv}{\from{luacolor.dtx}{driver}}%
129 \usedir{tex/latex/luacolor}%
130 \file{luacolor.sty}{\from{luacolor.dtx}{package}}%
131 }
132 \def\MetaPrefix{-- }
133 \def\defaultpostamble{%
134 \MetaPrefix^^J%
135 \MetaPrefix\space End of File `\outFileName'.%
136 }
137 \def\currentpostamble{\defaultpostamble}%
138 \generate{%
139 \usedir{scripts/luacolor}%
140 % \file{oberdiek.luacolor.lua}{\from{luacolor.dtx}{lua}}%
141 \file{luacolor.lua}{\from{luacolor.dtx}{lua}}%
142 }
143
144 \catcode32=13\relax% active space
145 \let =\space%
146 \Msg{************************************************************************}
147 \Msg{*}
148 \Msg{* To finish the installation you have to move the following}
149 \Msg{* file into a directory searched by TeX:}
150 \Msg{*}
151 \Msg{* luacolor.sty}
152 \Msg{*}
153 \Msg{* And install the following script file:}
154 \Msg{*}
155 \Msg{* luacolor.lua,}
156 \Msg{*}
157 \Msg{* To produce the documentation run the file `luacolor.dtx'}
158 \Msg{* through LaTeX.}
159 \Msg{*}
160 \Msg{* Happy TeXing!}
161 \Msg{*}
162 \Msg{************************************************************************}
163
164 \endbatchfile
165 %</install>
166 %<*ignore>
167 \fi
168 %</ignore>
169 %<*driver>
170 \NeedsTeXFormat{LaTeX2e}
171 \ProvidesFile{luacolor.drv}%
172 [2023-08-18 v1.18 Color support via LuaTeX's attributes (HO)]%
173 \documentclass{ltxdoc}
174 \usepackage{holtxdoc}[2011/11/22]
175 \usepackage{paralist}
176 \makeatletter
177 \g@addto@macro\MakePrivateLetters{%
178 \@makeother\_%
179 }
180 \makeatother
181 \begin{document}
182 \DocInput{luacolor.dtx}%
183 \end{document}
184 %</driver>
185 % \fi
186 %
187 %
188 %
189 % \GetFileInfo{luacolor.drv}
190 %
191 % \title{The \xpackage{luacolor} package}
192 % \date{2023-08-18 v1.18}
193 % \author{Heiko Oberdiek\thanks
194 % {Please report any issues at \url{https://github.com/ho-tex/luacolor/issues}}}
195 %
196 % \maketitle
197 %
198 % \begin{abstract}
199 % Package \xpackage{luacolor} implements color support based
200 % on \LuaTeX's node attributes.
201 % \end{abstract}
202 %
203 % \tableofcontents
204 %
205 % \section{Documentation}
206 %
207 % \subsection{Introduction}
208 %
209 % This package uses a \LuaTeX's attribute register to
210 % to annotate nodes with color information.
211 % If a color is set, then the attribute register
212 % is set to this color and all nodes created in its scope
213 % (current group) are annotated with this attribute.
214 % Now the color property behaves much the same way
215 % as the font property.
216 %
217 % \subsection{Usage}
218 %
219 % Package \xpackage{color} is loaded automatically by this
220 % package \xpackage{luacolor}. If you need a special driver
221 % option or you prefer package \xpackage{xcolor}, then load
222 % it before package \xpackage{luacolor}, for example:
223 % \begin{quote}
224 % |\usepackage[dvipdfmx]{xcolor}|
225 % \end{quote}
226 % The package \xpackage{luacolor} is loaded without options:
227 % \begin{quote}
228 % |\usepackage{luacolor}|
229 % \end{quote}
230 % It is able to detect PDF mode and DVI drivers are
231 % differentiated by its color specials. Therefore the
232 % package do need driver options.
233 %
234 % Then it redefines the color setting commands to set
235 % attributes instead of whatsits for color.
236 %
237 % At last the attribute annotations of the nodes in the
238 % output box must be analyzed to insert the necessary color whatsits.
239 % (With older \LuaTeX\ that lack the appropriate callback function
240 % the package \xpackage{atbegshi} is used to get control
241 % before a box is shipped out.)
242 %
243 % \begin{declcs}{luacolorProcessBox} \M{box}
244 % \end{declcs}
245 % Macro \cs{luacolorProcessBox} processes the box \meta{box}
246 % in the previously described manner. It is automatically
247 % called for pages, but not for XForm objects.
248 % Before passing a box to \cs{pdfxform}, call \cs{luacolorProcessBox}
249 % first.
250 %
251 % \subsection{Limitations}
252 %
253 % \begin{description}
254 % \item[Ligatures with different colored components:]
255 % Package \xpackage{luacolor} sees the ligature after
256 % the paragraph building and page breaking, when a
257 % page is to be shipped out. Therefore it cannot
258 % break ligatures, because the components might occupy
259 % different space. Therefore it is the responsibility
260 % of the ligature forming process to deal with different
261 % colored glyphs that form a ligature. The user can avoid
262 % the problem entirely by explicitly breaking the ligature
263 % at the places where the color changes.
264 % \item \dots
265 % \end{description}
266 %
267 % \StopEventually{
268 % }
269 %
270 % \section{Implementation}
271 %
272 % \begin{macrocode}
273 %<*package>
274 % \end{macrocode}
275 %
276 % \subsection{Catcodes and identification}
277 %
278 % \begin{macrocode}
279 \begingroup\catcode61\catcode48\catcode32=10\relax%
280 \catcode13=5 % ^^M
281 \endlinechar=13 %
282 \catcode123=1 % {
283 \catcode125=2 % }
284 \catcode64=11 % @
285 \def\x{\endgroup
286 \expandafter\edef\csname LuaCol@AtEnd\endcsname{%
287 \endlinechar=\the\endlinechar\relax
288 \catcode13=\the\catcode13\relax
289 \catcode32=\the\catcode32\relax
290 \catcode35=\the\catcode35\relax
291 \catcode61=\the\catcode61\relax
292 \catcode64=\the\catcode64\relax
293 \catcode123=\the\catcode123\relax
294 \catcode125=\the\catcode125\relax
295 }%
296 }%
297 \x\catcode61\catcode48\catcode32=10\relax%
298 \catcode13=5 % ^^M
299 \endlinechar=13 %
300 \catcode35=6 % #
301 \catcode64=11 % @
302 \catcode123=1 % {
303 \catcode125=2 % }
304 \def\TMP@EnsureCode#1#2{%
305 \edef\LuaCol@AtEnd{%
306 \LuaCol@AtEnd
307 \catcode#1=\the\catcode#1\relax
308 }%
309 \catcode#1=#2\relax
310 }
311 \TMP@EnsureCode{34}{12}% "
312 \TMP@EnsureCode{39}{12}% '
313 \TMP@EnsureCode{40}{12}% (
314 \TMP@EnsureCode{41}{12}% )
315 \TMP@EnsureCode{42}{12}% *
316 \TMP@EnsureCode{43}{12}% +
317 \TMP@EnsureCode{44}{12}% ,
318 \TMP@EnsureCode{45}{12}% -
319 \TMP@EnsureCode{46}{12}% .
320 \TMP@EnsureCode{47}{12}% /
321 \TMP@EnsureCode{58}{12}% :
322 \TMP@EnsureCode{60}{12}% <
323 \TMP@EnsureCode{62}{12}% >
324 \TMP@EnsureCode{91}{12}% [
325 \TMP@EnsureCode{93}{12}% ]
326 \TMP@EnsureCode{95}{12}% _ (other!)
327 \TMP@EnsureCode{96}{12}% `
328 \edef\LuaCol@AtEnd{\LuaCol@AtEnd\noexpand\endinput}
329 % \end{macrocode}
330 %
331 % Package identification.
332 % \begin{macrocode}
333 \NeedsTeXFormat{LaTeX2e}
334 \ProvidesPackage{luacolor}%
335 [2023-08-18 v1.18 Color support via LuaTeX's attributes (HO)]
336 % \end{macrocode}
337 %
338 % \subsection{Check for \LuaTeX}
339 %
340 % Without \LuaTeX\ there is no point in using this package.
341 % \begin{macrocode}
342 \RequirePackage{color}
343 % \end{macrocode}
344 %
345 % \begin{macrocode}
346 \ifx\directlua\@undefined
347 \PackageError{luacolor}{%
348 This package may only be run using LuaTeX%
349 }\@ehc
350 \expandafter\LuaCol@AtEnd
351 \fi%
352 % \end{macrocode}
353 %
354 % \subsection{Check for disabled colors}
355 %
356 % \begin{macrocode}
357 \ifcolors@
358 \else
359 \PackageWarningNoLine{luacolor}{%
360 Colors are disabled by option `monochrome'%
361 }%
362 \def\set@color{}%
363 \def\reset@color{}%
364 \def\set@page@color{}%
365 \def\define@color#1#2{}%
366 \expandafter\LuaCol@AtEnd
367 \fi%
368 % \end{macrocode}
369 %
370 % \subsection{Load module and check version}
371 %
372 % \begin{macrocode}
373 \directlua{%
374 require("luacolor")%
375 }
376 % \end{macrocode}
377 % \begin{macrocode}
378 \begingroup
379 \edef\x{\directlua{tex.write("2023-08-18 v1.18")}}%
380 \edef\y{%
381 \directlua{%
382 if oberdiek.luacolor.getversion then %
383 oberdiek.luacolor.getversion()%
384 end%
385 }%
386 }%
387 \ifx\x\y
388 \else
389 \PackageError{luacolor}{%
390 Wrong version of lua module.\MessageBreak
391 Package version: \x\MessageBreak
392 Lua module: \y
393 }\@ehc
394 \fi
395 \endgroup
396 % \end{macrocode}
397 %
398 % \subsection{Find driver}
399 %
400 % \begin{macrocode}
401 \ifnum\outputmode=\@ne
402 \else
403 \begingroup
404 \def\current@color{}%
405 \def\reset@color{}%
406 \setbox\z@=\hbox{%
407 \begingroup
408 \set@color
409 \endgroup
410 }%
411 \edef\reserved@a{%
412 \directlua{%
413 oberdiek.luacolor.dvidetect()%
414 }%
415 }%
416 \ifx\reserved@a\@empty
417 \PackageError{luacolor}{%
418 DVI driver detection failed because of\MessageBreak
419 unrecognized color \string\special
420 }\@ehc
421 \endgroup
422 \expandafter\expandafter\expandafter\LuaCol@AtEnd
423 \else
424 \PackageInfo{luacolor}{%
425 Type of color \string\special: \reserved@a
426 \@gobble}%
427 \fi%
428 \endgroup
429 \fi
430 % \end{macrocode}
431 %
432 % \subsection{Attribute setting}
433 %
434 % \begin{macro}{\LuaCol@Attribute}
435 % \begin{macrocode}
436 \newattribute\LuaCol@Attribute
437 \let\LuaCol@setattribute\setattribute
438 \directlua{%
439 oberdiek.luacolor.setattribute(\number\allocationnumber)%
440 }
441 % \end{macrocode}
442 % \end{macro}
443 %
444 % \begin{macro}{\set@color}
445 % change 2023-08-18: added \cs{reset@color} so that \cs{mathcolor} can gobble it,
446 % issue 7.
447 % \begin{macrocode}
448 \protected\def\set@color{%
449 \LuaCol@setattribute\LuaCol@Attribute{%
450 \directlua{%
451 oberdiek.luacolor.get("\luaescapestring{\current@color}")%
452 }%
453 }%
454 \aftergroup\reset@color
455 }
456 % \end{macrocode}
457 % \end{macro}
458 % \begin{macro}{\reset@color}
459 % \begin{macrocode}
460 \def\reset@color{}
461 % \end{macrocode}
462 % \end{macro}
463 %
464 % \subsection{Whatsit insertion}
465 %
466 % \begin{macro}{\luacolorProcessBox}
467 % \begin{macrocode}
468 \def\luacolorProcessBox#1{%
469 \directlua{%
470 oberdiek.luacolor.process(\number#1)%
471 }%
472 }
473 % \end{macrocode}
474 % \end{macro}
475 %
476 % \begin{macrocode}
477 \directlua{%
478 if luatexbase.callbacktypes.pre_shipout_filter then
479 token.get_next()
480 end
481 }\@secondoftwo\@gobble{
482 \RequirePackage{atbegshi}[2011/01/30]
483 \AtBeginShipout{%
484 \luacolorProcessBox\AtBeginShipoutBox
485 }
486 }
487 % \end{macrocode}
488 %
489 % Set default color.
490 % \begin{macrocode}
491 \set@color
492 % \end{macrocode}
493 %
494 % \subsection{\cs{pdfxform}/\cs{saveboxresource} support}
495 %
496 % \begin{macrocode}
497 \ifnum\outputmode=\@ne
498 \let\LuaCol@org@pdfxform\saveboxresource
499 % \end{macrocode}
500 % First we need some helpers to allow expandable code to parse keyword style arguments:
501 % \begin{macrocode}
502 \def\LuaCol@iii@i@ii#1#2#3{#3{#1}{#2}}
503 \def\LuaCol@ii@i#1#2{{#2#1}}
504 \def\LuaCol@if@keyword#1#2#3{%
505 \expanded{\unexpanded{\LuaCol@iii@i@ii{#2}{#3}}\expandafter}%
506 \directlua{%
507 token.put_next(token.create(token.scan_keyword(token.scan_string())
508 and '@firstoftwo'
509 or '@secondoftwo'))
510 }{#1}%
511 }
512 % \end{macrocode}
513 % The following macro scans a integer and expands to a token equivalent to a chardef
514 % whose value corresponds to the scanned integer. This allows the integer to be passed
515 % around as a undelimited argument.
516 % \begin{macrocode}
517 \def\LuaCol@scan@number{%
518 \directlua{
519 token.put_next(token.new(token.scan_int(), token.command_id'char_given'))
520 }%
521 }
522 % \end{macrocode}
523 % \TeX\ primitives like \cs{saveboxresource} read braced arguments in a special way.
524 % Especially they expand everything until they find a left brace. To simulate this, we
525 % use Lua to expand everything else:
526 % \begin{macrocode}
527 \def\LuaCol@scan@tobrace{%
528 \directlua{
529 local relax, space = token.command_id'relax', token.command_id'spacer'
530 local t
531 repeat
532 t = token.scan_token()
533 until not (t.command == relax or t.command == space)
534 token.put_next(t)
535 }%
536 }
537 \def\LuaCol@scan@boxresource@i#1#2{%
538 \LuaCol@if@keyword{attr}{%
539 \expanded{\unexpanded{\LuaCol@scan@boxresource@iI{#1#2attr}}%
540 \expandafter\expandafter\expandafter}%
541 \LuaCol@scan@tobrace
542 }{%
543 \LuaCol@scan@boxresource@ii{#1#2}%
544 }%
545 }
546 \def\LuaCol@scan@boxresource@iI#1#2{\LuaCol@scan@boxresource@ii{#1{#2}}}
547 \def\LuaCol@scan@boxresource@ii#1{%
548 \LuaCol@if@keyword{resources}{%
549 \expanded{\unexpanded{\LuaCol@scan@boxresource@iiI{#1resources}}%
550 \expandafter\expandafter\expandafter}%
551 \LuaCol@scan@tobrace
552 }{%
553 \LuaCol@scan@boxresource@iii{#1}%
554 }%
555 }
556 \def\LuaCol@scan@boxresource@iiI#1#2{\LuaCol@scan@boxresource@iii{#1{#2}}}
557 \def\LuaCol@scan@boxresource@iii#1{%
558 \LuaCol@if@keyword{margin}{%
559 \expanded{\unexpanded{\LuaCol@scan@boxresource@iv{#1margin }}%
560 \expandafter\expandafter\expandafter}%
561 \LuaCol@scan@number
562 }{%
563 \LuaCol@scan@boxresource@iv{#1}{}%
564 }%
565 }
566 \def\LuaCol@scan@boxresource@iv#1#2{%
567 \expanded{\unexpanded{\LuaCol@scan@boxresource@v{#1#2}}%
568 \expandafter\expandafter\expandafter}%
569 \LuaCol@scan@number
570 }
571 \def\LuaCol@scan@boxresource@v#1#2{%
572 \luacolorProcessBox{#2}%
573 \LuaCol@org@pdfxform#1#2%
574 }
575
576 % \end{macrocode}
577 % This could be written in Lua, but at least upto Lua\TeX~1.11, feeding back too many tokens
578 % from Lua to \TeX\ triggers a segmentation fault.
579 % This is written in Lua so the integer setting is expandable and does not interfere with
580 % a preceding \verb|\immediate|.
581 % \begin{macrocode}
582 \protected\def\saveboxresource{%
583 \LuaCol@if@keyword{type}{%
584 \expandafter
585 \expanded{\unexpanded{\LuaCol@scan@boxresource@i{type }}%
586 \expandafter\expandafter\expandafter}%
587 \LuaCol@scan@number
588 }{%
589 \LuaCol@scan@boxresource@i{}{}%
590 }%
591 }
592 % \end{macrocode}
593 % Legacy alias.
594 % \begin{macrocode}
595 \let\pdfxform\saveboxresource
596 \fi
597 % \end{macrocode}
598 %
599 % \begin{macrocode}
600 \LuaCol@AtEnd%
601 %</package>
602 % \end{macrocode}
603 %
604 % \subsection{Lua module}
605 %
606 % \begin{macrocode}
607 %<*lua>
608 % \end{macrocode}
609 % Box zero contains a \cs{hbox} with the color \cs{special}.
610 % That is analyzed to get the prefix for the color setting
611 % \cs{special}.
612 % \begin{macrocode}
613 oberdiek = oberdiek or {}
614 local luacolor = oberdiek.luacolor or {}
615 oberdiek.luacolor = luacolor
616 % \end{macrocode}
617 % \begin{macro}{getversion()}
618 % \begin{macrocode}
619 function luacolor.getversion()
620 tex.write("2023-08-18 v1.18")
621 end
622 % \end{macrocode}
623 % \end{macro}
624 %
625 % \subsubsection{Driver detection}
626 %
627 % \begin{macrocode}
628 local ifpdf = tonumber(tex.outputmode or tex.pdfoutput) > 0
629 local prefix
630 local prefixes = {
631 dvips = "color ",
632 dvipdfm = "pdf:sc ",
633 truetex = "textcolor:",
634 pctexps = "ps::",
635 }
636 local patterns = {
637 ["^color "] = "dvips",
638 ["^pdf: *begincolor "] = "dvipdfm",
639 ["^pdf: *bcolor "] = "dvipdfm",
640 ["^pdf: *bc "] = "dvipdfm",
641 ["^pdf: *setcolor "] = "dvipdfm",
642 ["^pdf: *scolor "] = "dvipdfm",
643 ["^pdf: *sc "] = "dvipdfm",
644 ["^textcolor:"] = "truetex",
645 ["^ps::"] = "pctexps",
646 }
647 % \end{macrocode}
648 % \begin{macro}{info()}
649 % \begin{macrocode}
650 local function info(msg, term)
651 local target = "log"
652 if term then
653 target = "term and log"
654 end
655 texio.write_nl(target, "Package luacolor info: " .. msg .. ".")
656 texio.write_nl(target, "")
657 end
658 % \end{macrocode}
659 % \end{macro}
660 % \begin{macro}{dvidetect()}
661 % \begin{macrocode}
662 function luacolor.dvidetect()
663 local v = tex.box[0]
664 assert(v.id == node.id("hlist"))
665 for v in node.traverse_id(node.id("whatsit"), v.head) do
666 if v and v.subtype == node.subtype("special") then
667 local data = v.data
668 for pattern, driver in pairs(patterns) do
669 if string.find(data, pattern) then
670 prefix = prefixes[driver]
671 tex.write(driver)
672 return
673 end
674 end
675 info("\\special{" .. data .. "}", true)
676 return
677 end
678 end
679 info("Missing \\special", true)
680 end
681 % \end{macrocode}
682 % \end{macro}
683 %
684 % \subsubsection{Color strings}
685 %
686 % \begin{macrocode}
687 local map = {
688 n = 0,
689 }
690 % \end{macrocode}
691 % \begin{macro}{get()}
692 % \begin{macrocode}
693 function luacolor.get(color)
694 tex.write("" .. luacolor.getvalue(color))
695 end
696 % \end{macrocode}
697 % \end{macro}
698 % \begin{macro}{getvalue()}
699 % \begin{macrocode}
700 function luacolor.getvalue(color)
701 local n = map[color]
702 if not n then
703 n = map.n + 1
704 map.n = n
705 map[n] = color
706 map[color] = n
707 end
708 return n
709 end
710 % \end{macrocode}
711 % \end{macro}
712 %
713 % \subsubsection{Attribute register}
714 %
715 % \begin{macro}{setattribute()}
716 % \begin{macrocode}
717 local attribute
718 function luacolor.setattribute(attr)
719 attribute = attr
720 end
721 % \end{macrocode}
722 % \end{macro}
723 %
724 % \begin{macro}{getattribute()}
725 % \begin{macrocode}
726 function luacolor.getattribute()
727 return attribute
728 end
729 % \end{macrocode}
730 % \end{macro}
731 %
732 % \subsubsection{Whatsit insertion}
733 %
734 % \begin{macrocode}
735 local LIST = 1
736 local LIST_LEADERS = 2
737 local LIST_DISC = 3
738 local COLOR = 4
739 local NOCOLOR = 5
740 local RULE = node.id("rule")
741 local node_types = {
742 [node.id("hlist")] = LIST,
743 [node.id("vlist")] = LIST,
744 [node.id("rule")] = COLOR,
745 [node.id("glyph")] = COLOR,
746 [node.id("disc")] = LIST_DISC,
747 [node.id("whatsit")] = {
748 [node.subtype("pdf_colorstack")] =
749 function(n)
750 return n.stack == 0 and NOCOLOR or nil
751 end,
752 [node.subtype("special")] = COLOR,
753 [node.subtype("pdf_literal")] = COLOR,
754 [node.subtype("pdf_save")] = COLOR,
755 [node.subtype("pdf_restore")] = COLOR, -- probably not needed
756 -- TODO (DPC) [node.subtype("pdf_refximage")] = COLOR,
757 },
758 [node.id("glue")] =
759 function(n)
760 if n.subtype >= 100 then -- leaders
761 if n.leader.id == RULE then
762 return COLOR
763 else
764 return LIST_LEADERS
765 end
766 end
767 end,
768 }
769 % \end{macrocode}
770 % \begin{macro}{get_type()}
771 % \begin{macrocode}
772 local function get_type(n)
773 local ret = node_types[n.id]
774 if type(ret) == 'table' then
775 ret = ret[n.subtype]
776 end
777 if type(ret) == 'function' then
778 ret = ret(n)
779 end
780 return ret
781 end
782 % \end{macrocode}
783 % \end{macro}
784 % \begin{macrocode}
785 local mode = 2 -- luatex.pdfliteral.direct
786 local WHATSIT = node.id("whatsit")
787 local SPECIAL = node.subtype("special")
788 local PDFLITERAL = node.subtype("pdf_literal")
789 local DRY_FALSE = false
790 local DRY_TRUE = true
791 % \end{macrocode}
792 % \begin{macro}{traverse()}
793 % \begin{macrocode}
794 local function traverse(list, color, dry)
795 if not list then
796 return color
797 end
798 local head
799 if get_type(list) == LIST then
800 head = list.head
801 elseif get_type(list) == LIST_DISC then
802 head = list.replace
803 else
804 texio.write_nl("!!! Error: Wrong list type: " .. node.type(list.id))
805 return color
806 end
807 %<debug>texio.write_nl("traverse: " .. node.type(list.id))
808 for n in node.traverse(head) do
809 %<debug>texio.write_nl(" node: " .. node.type(n.id))
810 local t = get_type(n)
811 %<debug>texio.write_nl("TYPE "..tostring(t).. " "..tostring(node.type(node.getid(n))).." ".. tostring(node.getsubtype(n)))
812 if t == LIST or t == LIST_DISC then
813 color = traverse(n, color, dry)
814 elseif t == LIST_LEADERS then
815 local color_after = traverse(n.leader, color, DRY_TRUE)
816 if color == color_after then
817 traverse(n.leader, color, DRY_FALSE or dry)
818 else
819 traverse(n.leader, '', DRY_FALSE or dry)
820 % \end{macrocode}
821 % The color status is unknown here, because the leader box
822 % will or will not be set.
823 % \begin{macrocode}
824 color = ''
825 end
826 elseif t == COLOR then
827 local v = node.has_attribute(n, attribute)
828 if v then
829 local newColor = map[v]
830 if newColor ~= color then
831 color = newColor
832 if dry == DRY_FALSE then
833 local newNode
834 if ifpdf then
835 newNode = node.new(WHATSIT, PDFLITERAL)
836 newNode.mode = mode
837 newNode.data = color
838 else
839 newNode = node.new(WHATSIT, SPECIAL)
840 newNode.data = prefix .. color
841 end
842 head = node.insert_before(head, n, newNode)
843 end
844 end
845 end
846 elseif t == NOCOLOR then
847 color = ''
848 end
849 end
850 if get_type(list) == LIST then
851 list.head = head
852 else
853 list.replace = head
854 end
855 return color
856 end
857 % \end{macrocode}
858 % \end{macro}
859 %
860 % \begin{macro}{process()}
861 % \begin{macrocode}
862 function luacolor.process(box)
863 local color = ""
864 local list = tex.getbox(box)
865 traverse(list, color, DRY_FALSE)
866 end
867
868 if luatexbase.callbacktypes.pre_shipout_filter then
869 luatexbase.add_to_callback("pre_shipout_filter", function(list)
870 traverse(list, "", DRY_FALSE)
871 return true
872 end, "luacolor.process")
873 end
874 % \end{macrocode}
875 % \end{macro}
876 % For recent versions of luaotfload, we can register a callback to
877 % control how coloring glyph is handled for the color feature.
878 % \begin{macrocode}
879 if luaotfload.set_colorhandler then
880 local set_attribute = node.direct.set_attribute
881 luaotfload.set_colorhandler(function(head, n, color)
882 set_attribute(n, attribute, luacolor.getvalue(color))
883 return head, n
884 end)
885 end
886 % \end{macrocode}
887 %
888 % \begin{macrocode}
889 %</lua>
890 % \end{macrocode}
891 % \section{Installation}
892 %
893 % \subsection{Download}
894 %
895 % \paragraph{Package.} This package is available on
896 % CTAN\footnote{\CTANpkg{luacolor}}:
897 % \begin{description}
898 % \item[\CTAN{macros/latex/contrib/luacolor/luacolor.dtx}] The source file.
899 % \item[\CTAN{macros/latex/contrib/luacolor/luacolor.pdf}] Documentation.
900 % \end{description}
901 %
902 %
903 % \paragraph{Bundle.} All the packages of the bundle `luacolor'
904 % are also available in a TDS compliant ZIP archive. There
905 % the packages are already unpacked and the documentation files
906 % are generated. The files and directories obey the TDS standard.
907 % \begin{description}
908 % \item[\CTANinstall{install/macros/latex/contrib/luacolor.tds.zip}]
909 % \end{description}
910 % \emph{TDS} refers to the standard ``A Directory Structure
911 % for \TeX\ Files'' (\CTANpkg{tds}). Directories
912 % with \xfile{texmf} in their name are usually organized this way.
913 %
914 % \subsection{Bundle installation}
915 %
916 % \paragraph{Unpacking.} Unpack the \xfile{luacolor.tds.zip} in the
917 % TDS tree (also known as \xfile{texmf} tree) of your choice.
918 % Example (linux):
919 % \begin{quote}
920 % |unzip luacolor.tds.zip -d ~/texmf|
921 % \end{quote}
922 %
923 % \paragraph{Script installation.}
924 % Check the directory \xfile{TDS:scripts/luacolor/} for
925 % scripts that need further installation steps.
926 %
927 % \subsection{Package installation}
928 %
929 % \paragraph{Unpacking.} The \xfile{.dtx} file is a self-extracting
930 % \docstrip\ archive. The files are extracted by running the
931 % \xfile{.dtx} through \plainTeX:
932 % \begin{quote}
933 % \verb|tex luacolor.dtx|
934 % \end{quote}
935 %
936 % \paragraph{TDS.} Now the different files must be moved into
937 % the different directories in your installation TDS tree
938 % (also known as \xfile{texmf} tree):
939 % \begin{quote}
940 % \def\t{^^A
941 % \begin{tabular}{@{}>{\ttfamily}l@{ $\rightarrow$ }>{\ttfamily}l@{}}
942 % luacolor.sty & tex/latex/luacolor/luacolor.sty\\
943 % luacolor.lua & scripts/luacolor/luacolor.lua\\
944 % luacolor.pdf & doc/latex/luacolor/luacolor.pdf\\
945 % luacolor.dtx & source/latex/luacolor/luacolor.dtx\\
946 % \end{tabular}^^A
947 % }^^A
948 % \sbox0{\t}^^A
949 % \ifdim\wd0>\linewidth
950 % \begingroup
951 % \advance\linewidth by\leftmargin
952 % \advance\linewidth by\rightmargin
953 % \edef\x{\endgroup
954 % \def\noexpand\lw{\the\linewidth}^^A
955 % }\x
956 % \def\lwbox{^^A
957 % \leavevmode
958 % \hbox to \linewidth{^^A
959 % \kern-\leftmargin\relax
960 % \hss
961 % \usebox0
962 % \hss
963 % \kern-\rightmargin\relax
964 % }^^A
965 % }^^A
966 % \ifdim\wd0>\lw
967 % \sbox0{\small\t}^^A
968 % \ifdim\wd0>\linewidth
969 % \ifdim\wd0>\lw
970 % \sbox0{\footnotesize\t}^^A
971 % \ifdim\wd0>\linewidth
972 % \ifdim\wd0>\lw
973 % \sbox0{\scriptsize\t}^^A
974 % \ifdim\wd0>\linewidth
975 % \ifdim\wd0>\lw
976 % \sbox0{\tiny\t}^^A
977 % \ifdim\wd0>\linewidth
978 % \lwbox
979 % \else
980 % \usebox0
981 % \fi
982 % \else
983 % \lwbox
984 % \fi
985 % \else
986 % \usebox0
987 % \fi
988 % \else
989 % \lwbox
990 % \fi
991 % \else
992 % \usebox0
993 % \fi
994 % \else
995 % \lwbox
996 % \fi
997 % \else
998 % \usebox0
999 % \fi
1000 % \else
1001 % \lwbox
1002 % \fi
1003 % \else
1004 % \usebox0
1005 % \fi
1006 % \end{quote}
1007 % If you have a \xfile{docstrip.cfg} that configures and enables \docstrip's
1008 % TDS installing feature, then some files can already be in the right
1009 % place, see the documentation of \docstrip.
1010 %
1011 % \subsection{Refresh file name databases}
1012 %
1013 % If your \TeX~distribution
1014 % (\TeX\,Live, \mikTeX, \dots) relies on file name databases, you must refresh
1015 % these. For example, \TeX\,Live\ users run \verb|texhash| or
1016 % \verb|mktexlsr|.
1017 %
1018 % \subsection{Some details for the interested}
1019 %
1020 % \paragraph{Unpacking with \LaTeX.}
1021 % The \xfile{.dtx} chooses its action depending on the format:
1022 % \begin{description}
1023 % \item[\plainTeX:] Run \docstrip\ and extract the files.
1024 % \item[\LaTeX:] Generate the documentation.
1025 % \end{description}
1026 % If you insist on using \LaTeX\ for \docstrip\ (really,
1027 % \docstrip\ does not need \LaTeX), then inform the autodetect routine
1028 % about your intention:
1029 % \begin{quote}
1030 % \verb|latex \let\install=y\input{luacolor.dtx}|
1031 % \end{quote}
1032 % Do not forget to quote the argument according to the demands
1033 % of your shell.
1034 %
1035 % \paragraph{Generating the documentation.}
1036 % You can use both the \xfile{.dtx} or the \xfile{.drv} to generate
1037 % the documentation. The process can be configured by the
1038 % configuration file \xfile{ltxdoc.cfg}. For instance, put this
1039 % line into this file, if you want to have A4 as paper format:
1040 % \begin{quote}
1041 % \verb|\PassOptionsToClass{a4paper}{article}|
1042 % \end{quote}
1043 % An example follows how to generate the
1044 % documentation with pdf\LaTeX:
1045 % \begin{quote}
1046 %\begin{verbatim}
1047 %pdflatex luacolor.dtx
1048 %makeindex -s gind.ist luacolor.idx
1049 %pdflatex luacolor.dtx
1050 %makeindex -s gind.ist luacolor.idx
1051 %pdflatex luacolor.dtx
1052 %\end{verbatim}
1053 % \end{quote}
1054 %
1055 % \begin{History}
1056 % \begin{Version}{2007/12/12 v1.0}
1057 % \item
1058 % First public version.
1059 % \end{Version}
1060 % \begin{Version}{2009/04/10 v1.1}
1061 % \item
1062 % Fixes for changed syntax of \cs{directlua} in \LuaTeX\ 0.36.
1063 % \end{Version}
1064 % \begin{Version}{2010/03/09 v1.2}
1065 % \item
1066 % Adaptation for package \xpackage{luatex} 2010/03/09 v0.4.
1067 % \end{Version}
1068 % \begin{Version}{2010/12/13 v1.3}
1069 % \item
1070 % Support for \cs{pdfxform} added.
1071 % \item
1072 % Loaded package \xpackage{luatexbase-attr} recognized.
1073 % \item
1074 % Update for \hologo{LuaTeX}: `list' fields renamed to `head' in v0.65.0.
1075 % \end{Version}
1076 % \begin{Version}{2011/03/29 v1.4}
1077 % \item
1078 % Avoid whatsit insertion if option \xoption{monochrome} is used
1079 % (thanks Manuel P\'egouri\'e-Gonnard).
1080 % \end{Version}
1081 % \begin{Version}{2011/04/22 v1.5}
1082 % \item
1083 % Bug fix by Manuel P\'egouri\'e-Gonnard: A typo prevented the
1084 % detection of whatsits and applying color changes for
1085 % \cs{pdfliteral} and \cs{special} nodes that might contain
1086 % typesetting material.
1087 % \item
1088 % Bug fix by Manuel P\'egouri\'e-Gonnard: Now colors are also
1089 % applied to leader boxes.
1090 % \item
1091 % Unnecessary color settings are removed for leaders boxes,
1092 % if after the leader box the color has not changed.
1093 % The costs are a little runtime, leader boxes are processed twice.
1094 % \item
1095 % Additional whatsits that are colored: \texttt{pdf\_refximage}.
1096 % \item
1097 % Workaround for bug with \texttt{node.insert\_before}
1098 % removed for the version after \hologo{LuaTeX} 0.65, because
1099 % bug was fixed in 0.27. (Thanks Manuel P\'egouri\'e-Gonnard.)
1100 % \end{Version}
1101 % \begin{Version}{2011/04/23 v1.6}
1102 % \item
1103 % Bug fix for nested leader boxes.
1104 % \item
1105 % Bug fix for leader boxes that change color, but
1106 % are not set because of missing place.
1107 % \item
1108 % Version check for Lua module added.
1109 % \end{Version}
1110 % \begin{Version}{2011/10/22 v1.7}
1111 % \item
1112 % Lua functions \texttt{getattribute} and \texttt{getvalue}
1113 % added to tell other external Lua functions
1114 % the attribute register number for coloring.
1115 % \end{Version}
1116 % \begin{Version}{2011/11/01 v1.8}
1117 % \item
1118 % Use of \texttt{node.subtype} instead of magic numbers.
1119 % \end{Version}
1120 % \begin{Version}{2016/05/13 v1.9}
1121 % \item
1122 % More use of \texttt{node.subtype} instead of magic numbers.
1123 % \item luatex 85 updates
1124 % \end{Version}
1125 % \begin{Version}{2016/05/16 v1.10}
1126 % \item
1127 % Documentation updates.
1128 % \end{Version}
1129 % \begin{Version}{2018/11/22 v1.11}
1130 % \item
1131 % handle issue 43.
1132 % \item
1133 % removed pre-0.65 stuff
1134 % \end{Version}
1135 % \begin{Version}{2019/07/25 v1.12}
1136 % \item
1137 % removed uses of module function, see PR70
1138 % \end{Version}
1139 % \begin{Version}{2019/11/29 v1.13}
1140 % \item
1141 % Documentation updates.
1142 % \item Use \xpackage{iftex} directly.
1143 % \end{Version}
1144 % \begin{Version}{2020-02-22 v1.14}
1145 % \item Drop use of \textsf{iftex} \textsf{ltxcmds} and \textsf{infwarerr}.
1146 % \item Assume \textsf{ltluatex} preloaded into format (true since 2015).
1147 % \item Patch \verb|\saveboxresource| rather than \verb|\pdfxform| (keep old name as alias).
1148 % \item Grab the number via Lua so that a \verb|\immediate| prefix still works with
1149 % \verb|\saveboxresource|\slash \verb|\pdfxform|.
1150 % \item Added handler for the color feature of luaotfload
1151 % \end{Version}
1152 % \begin{Version}{2020-02-24 v1.15}
1153 % \item Grab all possible arguments for \verb|\saveboxresource|\slash \verb|\pdfxform|
1154 % \end{Version}
1155 % \begin{Version}{2020-04-04 v1.16}
1156 % \item Reset color after \verb|pdf_colorstack| whatsits.
1157 % \end{Version}
1158 % \begin{Version}{2021-02-17 v1.17}
1159 % \item Use \LaTeXe's new \verb|pre_shipout_filter| callback if it's available to
1160 % allow coloring background and foreground layer material
1161 % \end{Version}
1162 % \begin{Version}{2023-08-18 v1.18}
1163 % \item added \verb|\reset@color| to \verb|\set@color| for \verb|\mathcolor|, issue 7.
1164 % \end{Version}
1165 % \end{History}
1166 %
1167 % \PrintIndex
1168 %
1169 % \Finale
1170 \endinput