page-box.mkvi /size: 10 Kb    last modification: 2021-10-28 13:50
1%D \module
2%D   [       file=page-box,
3%D        version=2011.12.05, % moved from other page-* files
4%D          title=\CONTEXT\ Page Macros,
5%D       subtitle=Page Boxing,
6%D         author=Hans Hagen,
7%D           date=\currentdate,
8%D      copyright={PRAGMA ADE \& \CONTEXT\ Development Team}]
9%C
10%C This module is part of the \CONTEXT\ macro||package and is
11%C therefore copyrighted by \PRAGMA. See mreadme.pdf for
12%C details.
13
14\writestatus{loading}{ConTeXt Page Macros / Boxing}
15
16\unprotect
17
18%D The often two step approach (_indeed) saves skipping tokens
19%D which is nicer for tracing.
20
21%D Centering the paper area on the print area is determined
22%D by the \type {top}, \type {bottom}, \type {left} and \type
23%D {right} parameters.
24
25\def\page_boxes_apply_center#box% we could test for a difference (saves packing)
26  {\printpaperwidth \v_page_target_scale\printpaperwidth \relax
27   \printpaperheight\v_page_target_scale\printpaperheight\relax
28   \ifdim\printpaperheight>\ht#box\relax
29     \page_boxes_apply_center_indeed#box%
30   \else\ifdim\printpaperwidth>\wd#box\relax
31     \page_boxes_apply_center_indeed#box%
32   \fi\fi}
33
34\def\page_boxes_apply_center_indeed_l_r#box%
35  {\v_page_target_left
36   \v_page_target_left_fill
37   \box#box%
38   \v_page_target_right_fill
39   \v_page_target_right}
40
41\def\page_boxes_apply_center_indeed_r_l#box%
42  {\v_page_target_right
43   \v_page_target_right_fill
44   \box#box%
45   \v_page_target_left_fill
46   \v_page_target_left}
47
48\def\page_boxes_apply_center_indeed#box% we could test for a difference (saves packing)
49  {\setbox#box\vpack to \printpaperheight
50     {\v_page_target_top
51      \v_page_target_top_fill
52      \hpack to \printpaperwidth
53        {\ifconditional\c_page_target_print_doublesided
54           \doifbothsides
55             {\page_boxes_apply_center_indeed_l_r#box}%
56             {\page_boxes_apply_center_indeed_l_r#box}%
57             {\page_boxes_apply_center_indeed_r_l#box}%
58         \else
59              \page_boxes_apply_center_indeed_l_r#box%
60         \fi}%
61      \par
62      \v_page_target_bottom_fill
63      \v_page_target_bottom}}
64
65\def\page_boxes_apply_offset % #box
66  {\ifzeropt\topoffset
67     \ifzeropt\backoffset
68       \doubleexpandafter\gobbleoneargument
69     \else
70       \doubleexpandafter\page_boxes_apply_offset_indeed
71     \fi
72   \else
73     \expandafter\page_boxes_apply_offset_indeed
74   \fi}
75
76\def\page_boxes_apply_offset_indeed#box%
77  {\scratchwidth \wd#box%
78   \scratchheight\ht#box%
79   \scratchdepth \dp#box%
80   \setbox#box\vpack
81     {\offinterlineskip
82      \vskip\topoffset
83      \hskip\doifbothsides\backoffset\backoffset{-\backoffset}%
84      \box#box}%
85   \wd#box\scratchwidth
86   \ht#box\scratchheight
87   \dp#box\scratchdepth}
88
89\def\page_boxes_apply_replicate
90  {\ifnum\layoutparameter\c!nx>\plusone
91     \expandafter\page_boxes_apply_replicate_indeed
92   \else\ifnum\layoutparameter\c!ny>\plusone
93     \doubleexpandafter\page_boxes_apply_replicate_indeed
94   \else
95     \doubleexpandafter\gobbleoneargument
96   \fi\fi}
97
98\def\page_boxes_apply_replicate_indeed#box%
99  {\setbox#box\vpack
100     {\offinterlineskip
101      \dorecurse{\layoutparameter\c!ny}
102        {\hpack{\dorecurse{\layoutparameter\c!nx}{\copy#box\kern\layoutparameter\c!dx}\unskip}%
103         \vskip\layoutparameter\c!dy}
104      \unskip}}
105
106\def\page_boxes_apply_orientate_paper#box%
107  {\ifnum\number\c_page_target_paper_orientation\number\c_page_target_paper_reverse>\zerocount
108     \page_boxes_apply_orientate_indeed\c_page_target_paper_orientation\c_page_target_paper_reverse{#box}%
109   \fi}
110
111\def\page_boxes_apply_orientate_print#box%
112  {\ifnum\number\c_page_target_print_orientation\number\c_page_target_print_reverse>\zerocount
113     \page_boxes_apply_orientate_indeed\c_page_target_print_orientation\c_page_target_print_reverse{#box}%
114   \fi}
115
116\def\page_boxes_apply_orientate_indeed#odd#even#box%
117  {\setbox#box\vpack
118     {\edef\somerotation{\number\ifdoublesided\ifodd\realpageno#odd\else#even\fi\else#odd\fi}%
119      \dorotatebox\somerotation\hpack{\box#box}}}
120
121\def\page_boxes_apply_mirror#box%
122  {\setbox#box\vpack{\mirror{\box#box}}}
123
124\def\page_boxes_apply_mirror_paper#box{\ifconditional\c_page_target_paper_mirror\page_boxes_apply_mirror{#box}\fi}
125\def\page_boxes_apply_mirror_print#box{\ifconditional\c_page_target_print_mirror\page_boxes_apply_mirror{#box}\fi}
126
127\let\page_boxes_apply_negate\negatecolorbox
128
129\def\page_boxes_apply_negate_print#box%
130  {\ifconditional\c_page_target_print_negate
131     \page_boxes_apply_negate#box%
132   \else\ifconditional\c_page_target_paper_negate
133     \page_boxes_apply_negate#box%
134   \fi\fi}
135
136\let\p_page_layouts_scale\relax
137\let\p_page_layouts_sx   \relax
138\let\p_page_layouts_sy   \relax
139
140\def\page_boxes_apply_scale#box%
141  {\edef\p_page_layouts_scale{\layoutparameter\c!scale}%
142   \ifdim\p_page_layouts_scale\points=\onepoint
143     \edef\p_page_layouts_sx{\layoutparameter\c!sx}%
144     \edef\p_page_layouts_sy{\layoutparameter\c!sy}%
145     \ifdim\p_page_layouts_sx\points=\onepoint
146       \ifdim\layoutparameter\c!sy\points=\onepoint
147         % no scaling done (and no copying either)
148       \else
149         \page_boxes_apply_scale_indeed\p_page_layouts_sx\p_page_layouts_sy{#box}%
150       \fi
151     \else
152       \page_boxes_apply_scale_indeed\p_page_layouts_sx\p_page_layouts_sy{#box}%
153     \fi
154   \else
155     \page_boxes_apply_scale_indeed\p_page_layouts_scale\p_page_layouts_scale{#box}%
156   \fi}
157
158\def\page_boxes_apply_scale_indeed#sx#sy#box%
159  {\setbox#box\vpack{\scale[\c!sx=#sx,\c!sy=#sy]{\box#box}}% can be a fast one
160   \paperwidth #sx\paperwidth
161   \paperheight#sy\paperheight}
162
163% \setuppagenumbering[alternative=doublesided]
164% \setupcolors[state=start]
165% \setuppapersize[A4][A4,oversized]
166% \setuplayout[location=middle,clipoffset=5mm]
167% \setupbackgrounds
168%   [page]
169%   [frame=on,rulethickness=1mm,
170%    backgroundoffset=10mm,background=color,backgroundcolor=red]
171% \starttext \dorecurse{10}{\input tufte \par} \stoptext
172
173% best use private variables
174
175\newconditional\c_page_boxes_clip_print \settrue\c_page_boxes_clip_print
176
177\def\page_boxes_apply_clip_print_left  % box
178  {\ifconditional\c_page_boxes_clip_print
179     \expandafter\page_boxes_apply_clip_print_indeed\expandafter\conditionalfalse
180   \else
181     \expandafter\gobbleoneargument
182   \fi}
183
184\def\page_boxes_apply_clip_print_right % box
185  {\ifconditional\c_page_boxes_clip_print
186     \expandafter\page_boxes_apply_clip_print_indeed\expandafter\conditionaltrue
187   \else
188     \expandafter\gobbleoneargument
189   \fi}
190
191\def\page_boxes_apply_clip_print_indeed#right#box% can be made more efficient, see other clipper
192  {\scratchoffset\pagebackgroundoffset % can be zero which is valid % maybe another variable
193   \scratchwidth \wd#box%
194   \scratchheight\ht#box%
195   \scratchdepth \dp#box%
196   \setbox#box\hpack
197     {\advance\scratchheight\dimexpr\scratchdepth+2\scratchoffset\relax
198      \advance\scratchwidth \scratchoffset
199      \ifconditional#right\relax
200         \scratchdimen-\scratchoffset
201         \kern\scratchdimen
202      \else
203         \scratchdimen\zeropoint
204      \fi
205      \lower\scratchoffset\hpack
206        {\clip
207           [\c!hoffset=\scratchdimen,
208            \c!voffset=-\scratchoffset,
209              \c!width=\scratchwidth,
210             \c!height=\scratchheight]%
211           {\box#box}}}%
212   \wd#box\scratchwidth
213   \ht#box\scratchheight
214   \dp#box\scratchdepth}
215
216\def\page_boxes_apply_clip_paper
217  {\scratchoffset\layoutparameter\c!clipoffset\relax
218   \ifdim\scratchoffset>\zeropoint
219      \expandafter\page_boxes_apply_clip_paper_indeed
220   \else
221      \expandafter\gobbleoneargument
222   \fi}
223
224\def\page_boxes_apply_clip_paper_indeed#box%
225  {\scratchwidth \wd#box%
226   \scratchheight\ht#box%
227   \scratchdepth \dp#box%
228   \setbox#box\hpack
229     {\advance\scratchheight\dimexpr\scratchdepth+2\scratchoffset\relax
230      \advance\scratchwidth \scratchoffset
231      \doifbothsides
232        {\advance\scratchwidth\scratchoffset
233         \scratchdimen-\scratchoffset
234         \kern\scratchdimen}%
235        {\scratchdimen\zeropoint}
236        {\scratchdimen-\scratchoffset
237         \kern\scratchdimen}%
238      \lower\scratchoffset\hpack
239        {\clip
240           [\c!hoffset=\scratchdimen,
241            \c!voffset=-\scratchoffset,
242              \c!width=\scratchwidth,
243             \c!height=\scratchheight]%
244           {\box#box}}}%
245   \wd#box\scratchwidth
246   \ht#box\scratchheight
247   \dp#box\scratchdepth}
248
249% nearly always some displacement so no speedup test needed
250
251\def\page_boxes_apply_offsets#1%
252  {\setbox#1\vpack to \paperheight
253     {\hsize\paperwidth
254      \vkern\topspace
255      \hkern\doifbothsides\backspace\backspace{\dimexpr\paperwidth-\backspace-\makeupwidth\relax}%
256      \box#1}%
257   \dp#1\zeropoint}
258
259% \def\page_boxes_apply_offset#box%
260%   {\scratchwidth \wd#box%
261%    \scratchheight\ht#box%
262%    \scratchdepth \dp#box%
263%    \setbox#box\vpack
264%      {\offinterlineskip
265%       \vkern\topoffset
266%       \hkern\doifbothsides\backoffset\backoffset{-\backoffset}%
267%       \box#box}%
268%    \wd#box\scratchwidth
269%    \ht#box\scratchheight
270%    \dp#box\scratchdepth}
271
272% \let\page_boxes_apply_clip_paper      \gobbleoneargument
273% \let\page_boxes_apply_clip_print_left \gobbleoneargument
274% \let\page_boxes_apply_clip_print_right\gobbleoneargument
275
276%D This is rather specialized:
277
278\newconditional\c_page_areas_enabled
279\newbox        \b_page_areas_registered
280
281\def\enabletextarearegistration
282  {\global\settrue\c_page_areas_enabled}
283
284\def\page_areas_registered_box
285  {\ifconditional\c_page_areas_enabled
286     \expandafter\page_areas_registered_box_indeed
287   \else
288     \expandafter\page_areas_registered_box_forget
289   \fi}
290
291\def\page_areas_registered_box_forget#1#2#3% #1=lower-dp #2=correct-ht #3=box
292  {\box#3}
293
294\def\page_areas_registered_box_indeed#1#2#3% #1=lower-dp #2=correct-ht #3=box
295  {\anch_mark_flow_box{#3}}
296
297\def\page_areas_register_boxed#1%
298  {\begingroup
299   \setbox\scratchbox\vpack{#1}%
300   \wd\scratchbox\makeupwidth % somehow a space creeps in
301   \vpack{\page_areas_registered_box00\scratchbox}% 0 0 will go
302   \endgroup}
303
304% \def\page_areas_register_direct#1%
305%   {\xypos{pbd:\realfolio:b}% we could save bytes by only saving the y
306%    \endgraf
307%    \begingroup
308%    \scratchdimen\dimexpr\MPy{pbd:\realfolio:b}-\MPy{pbd:\realfolio:e}\relax
309%    \setbox\scratchbox\emptyhbox
310%    \wd\scratchbox\makeupwidth
311%    \ht\scratchbox\scratchdimen
312%    \vsmash{\page_areas_registered_box00\scratchbox}%
313%    \endgroup
314%    #1%
315%    \endgraf
316%    \xypos{pbd:\realfolio:e}}
317
318\def\page_areas_register_direct#1#2%
319  {\scratchdepth\dp#2%
320   \dp#2\strutdepth
321   \anch_mark_flow_only{#2}%
322   \dp#2\scratchdepth
323   #1#2}
324
325\protect \endinput
326