page-sel.mklx /size: 15 Kb    last modification: 2021-10-28 13:51
1%D \module
2%D   [       file=page-sel, % moved from page-imp
3%D        version=1998.01.15,
4%D          title=\CONTEXT\ Page Macros,
5%D       subtitle=Page Selection,
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%D This code relates to old texexec features and one can wonder if it needs
15%D to be in the core. So, this could become runtime loaded code. Some of
16%D the alternatives need checking.
17
18\writestatus{loading}{ConTeXt Page Macros / Page Selection}
19
20\unprotect
21
22\startcontextdefinitioncode
23
24%D One can (mis)use this mechanism to (re)arrange pages of already produced files.
25%D
26%D \starttyping
27%D \insertpages[file.pdf][1,3][n=30,width=18cm]
28%D \stoptyping
29%D
30%D The pages are inserted in the text area, and even pages are repositioned
31%D according to the width. In this example empty pages are added after page 1 and 3.
32%D
33%D Selecting pages can be accomplished by:
34%D
35%D \starttyping
36%D \filterpages[file.pdf][1,3,5][n=30,width=18cm]
37%D \stoptyping
38%D
39%D One may pass \type {odd} or \type {even} instead of a comma separated list. A
40%D third alternative is:
41%D
42%D \starttyping
43%D \copypages[file.pdf][n=30,scale=950]
44%D \stoptyping
45%D
46%D This macros inserts the page, according to the settings provided.
47
48%D Beware: width is not the width of the image, but height can be used to control
49%D its dimensions.
50
51\installcorenamespace{withpages}
52
53\installsetuponlycommandhandler \??withpages {withpages}
54%  \installdirectcommandhandler \??withpages {withpages}
55
56\newcount\c_page_selectors_n
57
58\permanent\tolerant\protected\def\insertpages[#filename]#spacer[#emptylist]#spacer[#settings]%
59  {\doifelseassignment{#emptylist}
60     {\page_selectors_insert_indeed[#filename][][#emptylist]}
61     {\page_selectors_insert_indeed[#filename][#emptylist][#settings]}}
62
63\def\page_selectors_insert_indeed[#filename][#emptylist][#settings]%
64  {\bgroup
65   \dontcomplain
66   \getfiguredimensions[#filename]%
67   \setupcurrentwithpages
68     [\c!width=\zeropoint,
69      \c!n=\noffigurepages,
70      \c!category=,
71      #settings]%
72   \global\c_page_selectors_n\directwithpagesparameter\c!n\relax
73   \scratchwidth\directwithpagesparameter\c!width\relax
74   \doifinset0{#emptylist}
75     {\emptyhbox\page}%
76   \dorecurse\c_page_selectors_n
77     {\page_selectors_filter_a_page{#filename}\recurselevel
78      \doifinset\recurselevel{#emptylist}
79        {\emptyhbox\page}}%
80   \egroup}
81
82\let\page_selectors_filter_step_indeed\gobbleoneargument
83\let\page_selectors_filter_step       \gobbleoneargument
84
85\permanent\tolerant\protected\def\filterpages[#filename]#spacer[#selection]#spacer[#settings]% % \noffigurepages not yet supported
86  {\bgroup
87   \dontcomplain
88   \getfiguredimensions[#filename]%
89   \setupcurrentwithpages
90     [\c!width=\zeropoint,
91      \c!n=\noffigurepages,
92      \c!category=,
93      #settings]%
94   \global\c_page_selectors_n\directwithpagesparameter\c!n\relax
95   \scratchwidth\directwithpagesparameter\c!width\relax
96   \edef\p_selection{#selection}%
97   \ifx\p_selection\v!even
98     \dorecurse\c_page_selectors_n
99       {\ifodd\recurselevel\else
100          \page_selectors_filter_a_page{#filename}\recurselevel
101        \fi}%
102   \orelse\ifx\p_selection\v!odd
103     \dorecurse\c_page_selectors_n
104       {\ifodd\recurselevel\relax
105          \page_selectors_filter_a_page{#filename}\recurselevel
106        \fi}%
107   \else
108     \def\page_selectors_filter_step_indeed#page%
109       {\ifnum#page>\c_page_selectors_n\else
110          \page_selectors_filter_a_page{#filename}{#page}%
111        \fi}%
112     \def\page_selectors_filter_step#step%
113       {\dowithrange{#step}\page_selectors_filter_step_indeed}%
114     \processcommacommand[\p_selection]\page_selectors_filter_step
115   \fi
116   \egroup}
117
118\def\page_selectors_filter_a_page#filename#page%
119  {%\startpagemakeup
120   \hpack to \textwidth
121     {\ifdim\scratchwidth>\zeropoint
122        \rightorleftpageaction{\scratchwidth\zeropoint}{\hfill}%
123      \fi
124      \setbox\scratchbox\hpack
125        {\hskip-\scratchwidth
126         \edef\p_category{\directwithpagesparameter\c!category}% \useexternalfigure[foo][width=\textwidth]
127         \ifempty\p_category
128           \externalfigure[#filename][\c!page=#page,\c!height=\textheight]%
129         \else
130           \externalfigure[#filename][\p_category][\c!page=#page]%
131         \fi
132         \hss}%
133      \wd\scratchbox\zeropoint
134      \box\scratchbox}%
135   \page}%\stoppagemakeup}
136
137\permanent\tolerant\protected\def\copypages[#filename]#spacer[#settings]#spacer[#figuresettings]%
138  {\bgroup
139   \getfiguredimensions[#filename]%
140   \setupcurrentwithpages
141     [\c!marking=\v!off,
142      \c!offset=\zeropoint,
143      \c!n=\noffigurepages,
144      \c!category=,
145      #settings]%
146   \global\c_page_selectors_n\directwithpagesparameter\c!n\relax
147   \scratchoffset\directwithpagesparameter\c!offset\relax
148   \dorecurse\c_page_selectors_n
149     {\vbox to \textheight
150        {\hsize\textwidth
151         \centeredbox
152           {\doifelse{\directwithpagesparameter\c!marking}\v!on\cuthbox\hpack % only place where cuthbox is used
153              {\ifdim\scratchoffset>\zeropoint\relax
154                 \advance\vsize -2\scratchoffset
155                 \advance\hsize -2\scratchoffset
156                 \externalfigure[#filename][\c!page=\recurselevel,#figuresettings,\c!scale=,\c!factor=\v!max,\c!offset=\v!overlay]%
157               \else
158                 \externalfigure[#filename][\c!page=\recurselevel,#figuresettings,\c!offset=\v!overlay]%
159               \fi}}}
160      \page}%
161   \egroup}
162
163%D \macros
164%D   {combinepages}
165%D
166%D Yet another way of postprocessing is handles by \type {\combinepages}. This macro
167%D builds a matrix of pages from a file, for example:
168%D
169%D \starttyping
170%D \setuppapersize
171%D   [A4][A4] % or [A4,landscape][A4,landscape]
172%D
173%D \setuplayout
174%D   [header=0pt,footer=1cm,
175%D    backspace=1cm,topspace=1cm,
176%D    width=middle,height=middle]
177%D
178%D \setupfootertexts
179%D   [presentation---\currentdate\space---\space\pagenumber]
180%D
181%D \starttext
182%D   \combinepages[slides][nx=2,ny=3,frame=on]
183%D \stoptext
184%D \stoptyping
185%D
186%D One can influence the way the pages are combined. (This will be explained some
187%D time.)
188
189\installcorenamespace{combinepagesalternative}
190
191\permanent\tolerant\protected\def\combinepages[#filename]#spacer[#settings]% a=perpag b=free
192  {\bgroup
193   \dontcomplain
194   \getfiguredimensions[#filename]%
195   \setupcurrentwithpages
196     [\c!alternative=\v!a,
197      \c!n=\noffigurepages,\c!nx=\plustwo,\c!ny=\plustwo,
198      \c!start=\plusone,\c!stop=\maxcard,
199      \c!distance=\bodyfontsize,
200      \c!bottom=\vfill,\c!top=\vss,\c!left=\hss,\c!right=\hss,
201      \c!before=\page,\c!after=\page,\c!inbetween=\blank,
202      \c!frame=,\c!background=,\c!backgroundcolor=,
203      \c!name={#filename},
204      \c!category=,
205      #settings]%
206   \global\c_page_selectors_n\directwithpagesparameter\c!n\relax
207   \directwithpagesparameter\c!before
208   \scratchnx\directwithpagesparameter\c!nx
209   \scratchny\directwithpagesparameter\c!ny
210   \scratchdistance\directwithpagesparameter\c!distance\relax
211   \scratchwidth\dimexpr(\textwidth-\scratchnx\scratchdistance+\scratchdistance)/\scratchnx\relax
212   \scratchheight\dimexpr(\textheight-\scratchny\scratchdistance+\scratchdistance)/\scratchny\relax
213   \expandnamespaceparameter\??combinepagesalternative\directwithpagesparameter\c!alternative\v!b
214   \directwithpagesparameter\c!after
215   \egroup}
216
217\defcsname\??combinepagesalternative\v!a\endcsname % use hpacks
218  {\global\combinedpagescounter\directwithpagesparameter\c!start\relax
219   \doloop
220     {\vbox to \textheight
221        {\dorecurse\scratchny
222           {\hbox to \textwidth
223              {\dorecurse\scratchnx
224                 {\vbox to \scratchheight
225                    {\hsize\scratchwidth
226                     \vsize\scratchheight
227                     \directwithpagesparameter\c!top
228                     \hbox to \hsize
229                       {\directwithpagesparameter\c!left
230                        \ifnum\combinedpagescounter>\directwithpagesparameter\c!stop\relax
231                          \global\c_page_selectors_n\zerocount
232                        \orelse\ifnum\combinedpagescounter>\c_page_selectors_n \else
233                          \externalfigure
234                            [\directwithpagesparameter\c!name]
235                            [\c!object=\v!no,
236                             \c!page=\number\combinedpagescounter,
237                             \c!factor=\v!max,
238                             \c!background=\directwithpagesparameter\c!background,
239                             \c!backgroundcolor=\directwithpagesparameter\c!backgroundcolor,
240                             \c!frame=\directwithpagesparameter\c!frame]%
241                        \fi
242                        \directwithpagesparameter\c!right}%
243                     \directwithpagesparameter\c!bottom}%
244                  \global\advance\combinedpagescounter\plusone
245                  \hfil}%
246               \hfilneg}
247            \vfil}%
248         \vfilneg}%
249         \page
250         \ifnum\combinedpagescounter>\c_page_selectors_n
251           \exitloop
252         \fi}}
253
254\defcsname\??combinepagesalternative\v!c\endcsname
255  {\global\combinedpagescounter\directwithpagesparameter\c!start\relax
256   \doloop
257     {\vbox to \textheight
258        {\hbox to \textwidth
259           {\dorecurse\scratchnx
260              {\directwithpagesparameter\c!left
261               \vbox to \textheight
262                 {\hsize\scratchwidth
263                  \dorecurse\scratchny
264                    {\directwithpagesparameter\c!top
265                     \hbox to \hsize
266                       {\vbox to \scratchheight
267                          {\hsize\scratchwidth
268                           \vsize\scratchheight
269                           \ifnum\combinedpagescounter>\directwithpagesparameter\c!stop\relax
270                             \global\c_page_selectors_n\zerocount
271                           \orunless\ifnum\combinedpagescounter>\c_page_selectors_n
272                             \externalfigure
273                               [\directwithpagesparameter\c!name]
274                               [\c!object=\v!no,
275                                \c!page=\number\combinedpagescounter,
276                                \c!factor=\v!max,
277                                \c!background=\directwithpagesparameter\c!background,
278                                \c!backgroundcolor=\directwithpagesparameter\c!backgroundcolor,
279                                \c!frame=\directwithpagesparameter\c!frame]%
280                           \fi}}
281                      \global\advance\combinedpagescounter\plusone
282                      \directwithpagesparameter\c!bottom}}%
283                \hfil}%
284            \hfilneg}}%
285      \page
286      \ifnum\combinedpagescounter>\c_page_selectors_n
287        \exitloop
288      \fi}}
289
290\letcsname\??combinepagesalternative\v!horizontal\expandafter\endcsname\csname\??combinepagesalternative\v!a\endcsname
291\letcsname\??combinepagesalternative\v!vertical  \expandafter\endcsname\csname\??combinepagesalternative\v!c\endcsname
292
293\defcsname\??combinepagesalternative\v!b\endcsname
294  {\global\combinedpagescounter\directwithpagesparameter\c!start\relax
295   \doloop
296     {\startbaselinecorrection
297        \hbox to \textwidth
298          {\dorecurse\scratchnx
299             {\global\advance\combinedpagescounter\plusone
300              \ifnum\combinedpagescounter>\c_page_selectors_n \else
301                 \normalexpanded{\externalfigure
302                   [\directwithpagesparameter\c!name]
303                   [\c!page=\number\combinedpagescounter,
304                    \c!width=\the\scratchwidth,
305                    \c!background=\directwithpagesparameter\c!background,
306                    \c!backgroundcolor=\directwithpagesparameter\c!backgroundcolor,
307                    \c!frame=\directwithpagesparameter\c!frame]}%
308                 \hfill
309              \fi}%
310              \hfillneg}%
311      \stopbaselinecorrection
312      \ifnum\combinedpagescounter<\c_page_selectors_n\relax
313        \directwithpagesparameter\c!inbetween
314      \else
315        \exitloop
316      \fi}}
317
318% This macro cuts a page into n parts that can be pasted together.
319
320\permanent\tolerant\protected\def\slicepages[#filename]#spacer[#oddsettings]#spacer[#evensettings]%
321  {\ifparameter#evensettings\or
322     \page_selectors_slice_indeed[#filename][#oddsettings][#evensettings]%
323   \else
324     \page_selectors_slice_indeed[#filename][#oddsettings][#oddsettings]%
325   \fi}
326
327\mutable\let\slicedpagenumber\!!zerocount
328\mutable\let\slicedpagestepx \!!zerocount
329\mutable\let\slicedpagestepy \!!zerocount
330
331\def\page_selectors_slice_indeed[#filename][#oddsettings][#evensettings]%
332  {\bgroup
333   \dontcomplain
334   \glet\slicedpagenumber\!!zerocount
335   \getfiguredimensions[#filename]%
336   \setupcurrentwithpages
337     [\c!offset=\zeropoint,
338      \c!hoffset=\zeropoint,
339      \c!voffset=\zeropoint,
340      \c!width=\figurewidth,
341      \c!height=\figureheight,
342      \c!n=\noffigurepages,
343      \c!category=,
344      #oddsettings]%
345   \global\c_page_selectors_n\directwithpagesparameter\c!n\relax
346   \ifnum\c_page_selectors_n>\zerocount
347     \definepapersize
348       [\s!dummy]
349       [\c!height=\directwithpagesparameter\c!height,
350        \c!width=\directwithpagesparameter\c!width]%
351     \setuppapersize
352       [\s!dummy]
353       [\s!dummy]%
354     \setuplayout
355       [\c!backspace=\zeropoint,\c!topspace=\zeropoint,
356        \c!height=\v!middle,\c!width=\v!middle,
357        \c!textdistance=\zeropoint,
358        \c!header=\zeropoint,\c!footer=\zeropoint]%
359   \fi
360   \dorecurse\noffigurepages
361     {\glet\slicedpagenumber\recurselevel
362      \ifnum\c_page_selectors_n>\plusone
363        \dorecurse\c_page_selectors_n
364          {\let\slicedpagestepx\recurselevel
365           \dorecurse\c_page_selectors_n
366             {\let\slicedpagestepy\recurselevel
367              \clip
368                [\c!nx=\c_page_selectors_n,\c!ny=\c_page_selectors_n,\c!x=\slicedpagestepx,\c!y=\slicedpagestepy]%
369                {\scale
370                   [\c!scale=\number\c_page_selectors_n000]%
371                   {\externalfigure[#filename][\c!page=\slicedpagenumber]}}%
372              \page}}%
373      \else
374        \ifodd\slicedpagenumber\relax
375          \setupcurrentwithpages[#oddsettings]%
376        \else
377          \setupcurrentwithpages[#evensettings]%
378        \fi
379        \hskip\directwithpagesparameter\c!offset\relax
380        \clip
381          [\c!hoffset=\directwithpagesparameter\c!hoffset,\c!voffset=\directwithpagesparameter\c!voffset,
382           \c!height=\directwithpagesparameter\c!height,\c!width=\directwithpagesparameter\c!width]
383          {\externalfigure[#filename][\c!page=\slicedpagenumber]}%
384        \page
385      \fi}
386   \egroup}
387
388% \starttext \slicepages[slice1.pdf][n=3] \stoptext
389
390\permanent\protected\def\trimpages[#1]% was for a over decade in p-pdf-51.tex
391  {\begingroup
392   \getdummyparameters
393     [\c!file=dummy,%
394      \c!hoffset=\zeropoint,
395      \c!voffset=\zeropoint,
396      \c!width=17cm,
397      \c!height=24cm,
398      \c!x=\zeropoint,
399      \c!y=\zeropoint,
400      #1]%
401   \getfiguredimensions
402     [\dummyparameter\c!file]
403     [\c!object=\v!no]%
404   \dorecurse\noffigurepages
405     {\scale
406        [\c!width=\paperwidth,
407         \c!height=\paperheight]%
408        {\offset
409           [\c!x=\dummyparameter\c!x,
410            \c!y=\dummyparameter\c!y]
411           {\clip
412              [\c!hoffset=\dummyparameter\c!hoffset,
413               \c!voffset=\dummyparameter\c!voffset,
414               \c!width=\dummyparameter\c!width,
415               \c!height=\dummyparameter\c!height]
416              {% we correct by default, if not needed, introduce option
417               \setbox\nextbox\hbox
418                 {\externalfigure[\dummyparameter\c!file][\c!page=##1]}%
419               \ifdim\wd\nextbox>\ht\nextbox
420                 \rotate[\c!rotation=90]{\box\nextbox}%
421               \else
422                 \box\nextbox
423               \fi}}}}%
424   \endgroup}
425
426\stopcontextdefinitioncode
427
428\protect \endinput
429