page-inj.mklx /size: 7759 b    last modification: 2023-12-21 09:44
1%D \module
2%D   [     file=page-inj,
3%D      version=2013.02.10,
4%D        title=\CONTEXT\ Page Module,
5%D     subtitle=Injections,
6%D       author=Wolfgang Schuster \& Hans Hagen,
7%D         date=\currentdate,
8%D    copyright=Wolfgang Schuster \& Hans Hagen,
9%D      license=see context related readme files (gpl)]
10
11%D This module is based on an idea and prototype by Wolfgang but instead of
12%D injectioning into a page constructor we injection it into the regular shipout
13%D handler because that way we get page numbering. It's a sort of variation on
14%D postponing but the content does not end up in the regular page flow, so it's
15%D closer to page figures but without the usual page builder.
16%D
17%D This module is following the mkvi way of rendering setups and configuration and
18%D is somewhat experimental. Probably more alternatives will be added. This could
19%D become a delayed loaded module at some point (no real consequences).
20
21\unprotect
22
23\registerctxluafile{page-inj}{}
24
25\installcorenamespace {pageinjection}
26\installcorenamespace {pageinjectionalternative}
27\installcorenamespace {pageinjectionrenderings}
28
29\installframedcommandhandler \??pageinjection            {pageinjection}            \??pageinjection
30\installcommandhandler       \??pageinjectionalternative {pageinjectionalternative} \??pageinjectionalternative
31
32\let\page_boxes_flush_before\clf_flushpageinjectionsbefore
33\let\page_boxes_flush_after \clf_flushpageinjectionsafter
34
35\lettonothing\p_page_injections_n
36\lettonothing\p_page_injections_page
37\lettonothing\p_page_injections_state
38
39\def\page_injections_flush_indeed
40  {\begingroup
41   \setbox\scratchbox\normalhpack
42     {\inheritedpageinjectionframed{\texsetup\p_page_injectionalternative_rederingsetup}}%
43   \printpaperwidth \wd\scratchbox
44   \printpaperheight\ht\scratchbox
45   \scrn_canvas_synchronize_only
46   \invokepagehandler\v!normal{\box\scratchbox}%
47   \endgroup
48   \scrn_canvas_synchronize_reset}
49
50\def\page_injections_flush_saved#name#parameters%
51  {\begingroup
52   \cdef\currentpageinjection{#name}%
53   \getdummyparameters[#parameters]%
54   \edef\currentpageinjectionalternative          {\pageinjectionparameter           \c!alternative   }%
55   \edef\p_page_injectionalternative_rederingsetup{\pageinjectionalternativeparameter\c!renderingsetup}%
56   \page_injections_nextpage
57   \page_injections_flush_indeed
58   \endgroup}
59
60\permanent\tolerant\protected\def\pageinjection[#1]#*[#2]% name parameters | settings parameters | name | parameters
61  {\begingroup
62   \ifhastok={#1}%
63     \ifhastok={#2}%
64        \page_injections_direct_settings_parameters{#1}{#2}%
65     \else
66        \page_injections_direct_parameters{#1}%
67     \fi
68   \else
69     \ifhastok={#2}%
70        \page_injections_direct_name_parameters{#1}{#2}%
71     \else
72        \page_injections_direct_name{#1}%
73     \fi
74   \fi}
75
76\def\page_injections_direct_settings_parameters#settings% #parameters%
77  {\lettonothing\currentpageinjection
78   \checkpageinjectionparent
79   \setupcurrentpageinjection[#settings]%
80   \page_injections_direct_indeed}
81
82\def\page_injections_direct_parameters% #parameters%
83  {\lettonothing\currentpageinjection
84   \checkpageinjectionparent
85   \page_injections_direct_indeed}
86
87\def\page_injections_direct_name_parameters#name% #parameters%
88  {\cdef\currentpageinjection{#name}%
89   \checkpageinjectionparent
90   \page_injections_direct_indeed}
91
92\def\page_injections_direct_name#name%
93  {\cdef\currentpageinjection{#name}%
94   \checkpageinjectionparent
95   \page_injections_direct_indeed{}}
96
97\def\page_injections_direct_indeed% #parameters%
98  {\edef\p_page_injections_state{\pageinjectionparameter\c!state}%
99   \edef\p_page_injections_page {\pageinjectionparameter\c!page }%
100   \ifcstok{\pageinjectionparameter\c!delay}\v!yes
101     \expandafter\page_injections_direct_indeed_yes
102   \else
103     \expandafter\page_injections_direct_indeed_nop
104   \fi}
105
106\def\page_injections_direct_indeed_yes#parameters%
107  {\clf_savepageinjections
108         name {\currentpageinjection}%
109        state {\p_page_injections_state}%
110     userdata {\normalunexpanded{#parameters}}%
111   \relax
112   \endgroup}
113
114\def\page_injections_direct_indeed_nop#parameters%
115  {\ifempty\p_page_injections_page
116     \page
117   \else
118     \page[\p_page_injections_page]%
119   \fi
120   \getdummyparameters[#parameters]%
121   \page_injections_place
122   \endgroup}
123
124\def\page_injections_place
125  {\edef\currentpageinjectionalternative          {\pageinjectionparameter           \c!alternative   }%
126   \edef\p_page_injectionalternative_rederingsetup{\pageinjectionalternativeparameter\c!renderingsetup}%
127   \page_injections_nextpage
128   \ifx\currentpageinjectionalternative\v!none \else % increment counter but don’t generate output
129     \forgetparindent
130     \dontcomplain
131     \setconstant\shipoutfinalizemethod\zerocount % this is messy
132     \page_injections_flush_indeed
133   \fi}
134
135\def\page_injections_nextpage
136  {\ifcstok{\pageinjectionparameter\c!pagestate}\v!stop\else
137     \edef\p_page_injections_n{\pageinjectionparameter\c!n}%
138     \ifempty\p_page_injections_n
139        \let\p_page_injections_n\plusone
140     \fi
141     \dorecurse\p_page_injections_n
142       {\incrementcounter[\s!realpage]%
143        \incrementcounter[\s!userpage]}%
144   \fi}
145
146\definepageinjectionalternative[\v!figure][\c!renderingsetup=\??pageinjectionrenderings:\v!figure]
147\definepageinjectionalternative[\v!buffer][\c!renderingsetup=\??pageinjectionrenderings:\v!buffer]
148\definepageinjectionalternative[\v!setups][\c!renderingsetup=\??pageinjectionrenderings:\s!setup ]
149
150\startsetups[\??pageinjectionrenderings:\v!figure]
151    \dontleavehmode\externalfigure[\dummyparameter\c!name][\c!factor=\v!max]
152\stopsetups
153
154\startsetups[\??pageinjectionrenderings:\s!setup]
155    \setups[\dummyparameter\c!setups]% kind of redundant but fits in
156\stopsetups
157
158\startsetups[\??pageinjectionrenderings:\v!buffer]
159    \getbuffer[\dummyparameter\c!name]%
160\stopsetups
161
162% \setuppaper
163%   [\c!method=\v!pageinjection]
164
165\setuppageinjection
166  [      \c!width=\paperwidth,
167        \c!height=\paperheight,
168         \c!frame=\v!off,
169        \c!offset=\v!overlay,
170         \c!delay=\v!no,
171         \c!state=\v!next,
172   \c!alternative=\v!figure]
173
174\definepageinjection[\v!previouspage][\c!delay=\v!yes,\c!state=\v!previous]
175\definepageinjection[\v!nextpage]    [\c!delay=\v!yes,\c!state=\v!next]
176\definepageinjection[\v!here]        % direct
177\definepageinjection[\v!empty]       [\c!alternative=\v!none]
178
179%definepageinjection[\v!chapter]     [\v!previouspage][\c!pagestate=\v!stop]
180
181\protect \endinput
182
183% \enabletrackers[pagebuilder.injections]
184%
185% \setuppageinjection[offset=2em]
186%
187% \definepageinjection[chapter][previouspage][pagestate=stop]
188% \definepageinjection[later]  [delay=yes,state=+2]
189%
190% \useMPlibrary[dum]
191%
192% \starttext
193%
194% \startchapter[title={Insert pages before/after the current page}]
195%
196% 	\dorecurse{10}{one: \input ward\par}
197%
198% 	\pageinjection[previouspage][name=dummy-1]
199%
200% 	\dorecurse{20}{two: \input ward\par} \page
201%
202% 	\pageinjection[nextpage][name=dummy-2]
203%  	\pageinjection[later]   [name=dummy-4]
204%  	\pageinjection[delay=yes,state=+2][name=dummy-5]
205%
206% 	\dorecurse{100}{three: \input ward\par}
207%
208% \stopchapter
209%
210% \startbuffer[whatever]
211%     \dorecurse{10}{whatever #1 }
212% \stopbuffer
213%
214% \startchapter[title={Insert pages at the current location}]
215%
216% 	\dorecurse{10}{\input ward\par}
217%
218% 	\pageinjection[direct][name=dummy-3]
219% 	\pageinjection[direct][alternative=buffer,name=whatever]
220%
221% 	\dorecurse{10}{\input ward\par}
222%
223% \stopchapter
224%
225% \setuphead
226%   [chapter]
227%   [beforesection={\pageinjection[empty]},
228%    insidesection={\pageinjection[chapter][name=\structureuservariable{image}]}]
229%
230% \startchapter[title={Combine both mechanism}][name=dummy-4]
231%
232% 	\dorecurse{10}{\input ward\par}
233%
234% \stopchapter
235%
236% \stoptext
237