summaryrefslogtreecommitdiff
path: root/Master/texmf-dist/tex/context/base/spac-pag.mkiv
blob: 9babab29a274856db41ecce093d22827911d20e9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
%D \module
%D   [       file=spac-pag,
%D        version=2009.10.16, % 1997.03.31, was core-spa.tex
%D          title=\CONTEXT\ Spacing Macros,
%D       subtitle=Pages,
%D         author=Hans Hagen,
%D           date=\currentdate,
%D      copyright={PRAGMA / Hans Hagen \& Ton Otten}]
%C
%C This module is part of the \CONTEXT\ macro||package and is
%C therefore copyrighted by \PRAGMA. See mreadme.pdf for
%C details.

\writestatus{loading}{ConTeXt Spacing Macros / Pages}

\unprotect

%D A quick way to determine left|/|middle|/|right states
%D (experimental).

\setvalue{\??as\v!left  }{0}
\setvalue{\??as\v!middle}{1}
\setvalue{\??as\v!right }{2}

\def\setalignmentswitch#1%
  {\chardef\alignmentswitch0\csname\??as#1\endcsname\relax}

\newcount   \noftrackedpagestates
\newif      \ifpagestatemismatch
\newcount   \realpagestateno
\newconstant\frozenpagestate

\def\dotrackpagestate#1#2%
  {\ifdoublesided \ifinpagebody \else
     \doforcedtrackpagestate{#1}{#2}%
   \fi \fi}

\def\doforcedtrackpagestate#1#2%
  {\ifcase\frozenpagestate
     \global\advance\noftrackedpagestates\plusone
     \global\advance#2\plusone
     \lazysavetaggedtwopassdata{#1}{\number\noftrackedpagestates}{\number#2}{\noexpand\realfolio}%
     %\llap{\infofont\number\noftrackedpagestates/\number#2}% tracing
   \fi}

\def\doifrightpagestateelse#1#2%
  {\ifcase\frozenpagestate
     \pagestatemismatchfalse
     \realpagestateno\realfolio
     \ifinpagebody
       \ifdoublesided
         \ifodd\realpageno\relax
           \twopassdatafoundtrue \else \twopassdatafoundfalse
         \fi
       \else
         \twopassdatafoundtrue
       \fi
     \else\ifdoublesided
       \findtwopassdata{#1}{\number#2}%
       \iftwopassdatafound
         \realpagestateno\twopassdata\relax
         \ifnum\twopassdata=\realpageno \else
           \pagestatemismatchtrue
         \fi
         \ifodd\twopassdata\relax
           \twopassdatafoundtrue \else \twopassdatafoundfalse
         \fi
       \else
         \ifodd\realpageno\relax
           \twopassdatafoundtrue \else \twopassdatafoundfalse
         \fi
       \fi
     \else
       \twopassdatafoundtrue
     \fi\fi
   \else
     \ifodd\realpagestateno\relax
       \twopassdatafoundtrue \else \twopassdatafoundfalse
     \fi
   \fi
   \iftwopassdatafound
     \@EA\firstoftwoarguments
   \else
     \@EA\secondoftwoarguments
   \fi}

\def\doifforcedrightpagestateelse#1#2%
  {\ifcase\frozenpagestate
     \pagestatemismatchfalse
     \realpagestateno\realfolio
     \findtwopassdata{#1}{\number#2}%
     \iftwopassdatafound
       \realpagestateno\twopassdata\relax
       \ifnum\twopassdata=\realpageno \else
         \pagestatemismatchtrue
       \fi
       \ifodd\twopassdata\relax
         \twopassdatafoundtrue \else \twopassdatafoundfalse
       \fi
     \else
       \ifodd\realpageno\relax
         \twopassdatafoundtrue \else \twopassdatafoundfalse
       \fi
     \fi
   \else
     \ifodd\realpagestateno\relax
       \twopassdatafoundtrue \else \twopassdatafoundfalse
     \fi
   \fi
   \iftwopassdatafound
     \@EA\firstoftwoarguments
   \else
     \@EA\secondoftwoarguments
   \fi}

\def\freezepagestate {\frozenpagestate\plusone  }
\def\defrostpagestate{\frozenpagestate\zerocount}

% we can make more of these on top, but how to deal with mixed frozen states

\definetwopasslist\s!paragraph \newcount \nofraggedparagraphs

\def\signalrightpage  {\dotrackpagestate      \s!paragraph\nofraggedparagraphs}
\def\doifrightpageelse{\doifrightpagestateelse\s!paragraph\nofraggedparagraphs}

\newcount\pagesignallevel

\unexpanded\def\startsignalrightpage % one may do a \postsignalrightplace
  {\advance\pagesignallevel\plusone
   \presignalrightpage
   \let\signalrightpage\relax
   \let\presignalrightpage\relax
   \let\startsignalrightpage\relax
   \doifrightpageelse\donothing\donothing
   \freezepagestate}

\unexpanded\def\stopsignalrightpage
  {\ifcase\pagesignallevel\or\postsignalrightpage\fi
   \advance\pagesignallevel\minusone}

% \def\setraggedparagraphmode
%   {\signalrightpage\doifrightpageelse} % move it there

\ifx\swapmargins\undefined \let\swapmargins\undefined \fi % todo

\def\doifswappedrightpageelse#1#2% alleen in box construction !
  {\doifrightpageelse
     {#1}
     {\scratchcounter\realpageno
      \realpageno\realpagestateno\relax
      \swapmargins
      \realpageno\scratchcounter
      #2}}

\newbox\signaledrightpage % this way we can avoid interference, i.e. postpone placement

\def\presignalrightpage {\global\setbox\signaledrightpage\hbox{\signalrightpage}}
\def\postsignalrightpage{\ifvoid\signaledrightpage\else\box\signaledrightpage\fi}

% The next feature is is used in:
%
% \definenumber[test][way=bypage]
%
% \def\Test
%   {\incrementnumber[test]\rawnumber[test]/%
%    \incrementnumber[test]\rawnumber[test]/%
%    \incrementnumber[test]\rawnumber[test]\space
%    \checkpagechange{oeps}\changedpage{oeps}\space
%    \ifpagechanged TRUE\else FALSE\fi}
%
% \Test\page \Test\par \Test\page \Test\par \Test\page \Test\page
%
% (adapted from cont-new.tex:)

\newif\ifpagechanged \let\lastchangedpage\empty

\def\docheckpagestatechange#1#2#3%
  {\pagechangedfalse
   \doforcedtrackpagestate{#2}{#3}%
   \findtwopassdata{#2}{\number#3}%
   \iftwopassdatafound
     \ifnum\twopassdata>0\getvalue{#2:p:#1}\relax
       \pagechangedtrue
     \fi
   \fi
   \ifpagechanged
     \letgvalue{#2:p:#1}\twopassdata
     \globallet\lastchangedpage\twopassdata
   \else
     \globallet\lastchangedpage\realfolio
   \fi}

\def\changedpagestate#1#2%
  {\executeifdefined{#2:p:#1}{0}}

\def\checkpagechange#1{\docheckpagestatechange{#1}\s!paragraph\nofraggedparagraphs}
\def\changedpage    #1{\changedpagestate{#1}\s!paragraph}

\protect \endinput