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