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