summaryrefslogtreecommitdiff
path: root/tex/context/base/node-bck.mkiv
blob: 41a6da2cb48a3b8e999794bf03b8d566e726812f (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
%D \module
%D   [       file=node-bck,
%D        version=2009.06.08,
%D          title=\CONTEXT\ Node Macros,
%D       subtitle=Backgrounds,
%D         author=Hans Hagen,
%D           date=\currentdate,
%D      copyright=PRAGMA]
%C
%C This module is part of the \CONTEXT\ macro||package and is
%C therefore copyrighted by \PRAGMA. See mreadme.pdf for
%C details.

\writestatus{loading}{ConTeXt Node Support / Backgrounds}

%D This is first attempt to replacing backgrounds in a few
%D tables mechanisms.

\unprotect

\registerctxluafile{node-bck}{1.001}

%D Box helpers:

\def\doaddbackgroundtobox#1[#2]%
  {\begingroup\faststartcolor[#2]%
   \normalexpanded{\noexpand\faststopcolor\endgroup#1
     attr \backgroundattribute   \plusone
     attr \colormodelattribute   \the\attribute\colormodelattribute
     attr \colorattribute        \the\attribute\colorattribute
     attr \transparencyattribute \the\attribute\transparencyattribute}}

\def\backgroundvbox{\doaddbackgroundtobox\vbox}
\def\backgroundvtop{\doaddbackgroundtobox\vtop}
\def\backgroundhbox{\doaddbackgroundtobox\hbox}

\def\colorattr#1%
  {\ifcsname(cs:\currentpalet#1)\endcsname
     \thecolorattr{\currentpalet#1}%
   \else\ifcsname(cs:#1)\endcsname
     \thecolorattr{#1}%
   \fi\fi}

\def\thecolorattr#1%
  {attr \colormodelattribute   \attribute\colormodelattribute
   attr \colorattribute        \csname(cs:#1)\endcsname
   attr \transparencyattribute \csname(ts:#1)\endcsname}

\def\backgroundcolorattr#1%
  {\ifcsname(cs:\currentpalet#1)\endcsname
     \thebackgroundcolorattr{\currentpalet#1}%
   \else\ifcsname(cs:#1)\endcsname
     \thebackgroundcolorattr{#1}%
   \fi\fi}

\def\thebackgroundcolorattr#1%
  {attr \backgroundattribute   \plusone
   attr \colormodelattribute   \attribute\colormodelattribute
   attr \colorattribute        \csname(cs:#1)\endcsname
   attr \transparencyattribute \csname(ts:#1)\endcsname}

% \def\backgroundvbox[#1]{\vbox \backgroundcolorattr{#1}}
% \def\backgroundvtop[#1]{\vtop \backgroundcolorattr{#1}}
% \def\backgroundhbox[#1]{\hbox \backgroundcolorattr{#1}}

%D tabulate:

\def\tabulatenormalcolumn#1%
  {\doiffastoptionalcheckelse{\tabulatenormalcolumnyes#1}{\tabulatenormalcolumnnop#1}}

\def\tabulatenormalcolumnnop#1% overloads anch-pgr
  {&\iftabulateequal\tabulateequalpos\else\tabulatenormalpos\fi&\global\chardef\tabulatetype#1&}

\def\tabulatenormalcolumnyes#1[#2]%
  {&\iftabulateequal\tabulateequalpos\else\tabulatenormalpos\fi&\global\chardef\tabulatetype#1&%
   \iftrialtypesetting\else\settabulatecolor{#2}\fi\ignorespaces}

\newcount\maxtabularcolorcolumn

\def\settabulatecolor#1% we could store the attributes at the cost of a lua call
  {\begingroup
   \global\settrue\tabulatehascolors
   \ifnum\tabulatecolumn>\maxtabularcolorcolumn
     \global\maxtabularcolorcolumn\tabulatecolumn
   \fi
   \setxvalue{\??tt:c:\the\tabulatecolumn}{#1}%
   \attribute\backgroundattribute\plusone
   \faststartcolor[#1]\strut\char0\faststopcolor
   \endgroup}

\def\repeatsettabulatecolor
  {\begingroup
   \ifcsname\??tt:c:\the\tabulatecolumn\endcsname
     \attribute\backgroundattribute\plusone
     \faststartcolor[\csname\??tt:c:\the\tabulatecolumn\endcsname]\strut\char0\faststopcolor
   \fi
   \endgroup}

\def\resettabulatecolors
  {\ifcase\maxtabularcolorcolumn\else
     \doresettabulatecolors
   \fi}

\def\doresettabulatecolors
  {\dorecurse\maxtabularcolorcolumn{\letgvalue{\??tt:c:\recurselevel}\undefined}} % slow

\appendtoks
  \resettabulatecolors
\to \everyaftertabulaterow

\def\splitofftabulatebox % overloads in anch-pgr
  {\dontcomplain
   \global\setbox\tabulatebox % % % global ? % % %
     \vsplit\tablebox\tabulatecolumn to \lineheight
   \setbox\tabulatebox\normalvbox
     {\unvbox\tabulatebox}%
   \setbox\tabulatebox\hbox to \wd\tabulatebox
     {\iftrialtypesetting\else\ifconditional\tabulatehascolors\repeatsettabulatecolor\fi\fi
      \hss\dotabulatehook{\box\tabulatebox}\hss}%
   \ht\tabulatebox\strutht
   \dp\tabulatebox\strutdp
   \box\tabulatebox
   \iftrialtypesetting\else\ifconditional\tablehaspositions\doflushtabulateepos\fi\fi}

\appendtoks
    \global\setfalse\tabulatehascolors
\to \everytabulate

\protect \endinput

% test

\starttext

\backgroundvbox[green] {\input tufte } \par
\backgroundvbox[blue]  {\input ward } \par
\backgroundvbox[red]   {\input knuth } \par
\backgroundhbox[yellow]{\rotate[rotation=45]{hello world}} \par

\starttabulate[||p||]
\NC test \NC test \NC test \NC \NR
\NC test \NC[green] \input tufte  \NC[yellow] test \NC \NR
\NC test \NC test \NC test \NC \NR
\NC test \NC test \NC test \NC \NR
\NC test \NC test \NC test \NC \NR
\NC test \NC test \NC test \NC \NR
\NC[blue] test \NC[red] test \NC test \NC \NR
\NC test \NC test \NC test \NC \NR
\NC test \NC test \NC test \NC \NR
\NC test \NC test \NC test \NC \NR
\NC test \NC test \NC[gray] test \NC \NR
\NC test \NC test \NC test \NC \NR
\NC test \NC test \NC test \NC \NR
\NC[blue] test \NC test \NC test \NC \NR
\NC test \NC test \NC test \NC \NR
\NC test \NC test \NC[magenta] test \NC \NR
\NC test \NC test \NC test \NC \NR
\NC test \NC[cyan] \dorecurse{10}{\input ward }\NC test \NC \NR
\NC test \NC test \NC test \NC \NR
\NC test \NC[yellow] test \NC test \NC \NR
\stoptabulate

\stoptext