summaryrefslogtreecommitdiff
path: root/metapost/context/base/mpxl/mp-page.mpxl
blob: 93480e711473c061b40d30c2045cce66c5c8c24b (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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
%D \module
%D   [       file=mp-page.mpiv,
%D        version=1999.03.10,
%D          title=\CONTEXT\ \METAPOST\ graphics,
%D       subtitle=page enhancements,
%D         author=Hans Hagen,
%D           date=\currentdate,
%D      copyright={PRAGMA ADE \& \CONTEXT\ Development Team}]
%C
%C This module is part of the \CONTEXT\ macro||package and is
%C therefore copyrighted by \PRAGMA. See mreadme.pdf for
%C details.

%D This module is rather preliminary and subjected to changes.In the process of
%D moving to \METAFUN2\ this might change.

if known metafun_loaded_page : endinput ; fi ;

newinternal boolean metafun_loaded_page ; metafun_loaded_page := true ; immutable metafun_loaded_page ;

def LoadPageState = enddef ; % just in case some old style uses it

% Next we implement the the page area model. First some constants. We use a
% matrix approach as we do at the TeX end but we could have gone for a period
% separated variant. Too late.

LeftEdge             :=  -4 ; Top             := -40 ;
LeftEdgeSeparator    :=  -3 ; TopSeparator    := -30 ;
LeftMargin           :=  -2 ; Header          := -20 ;
LeftMarginSeparator  :=  -1 ; HeaderSeparator := -10 ;
Text                 :=   0 ; Text            :=   0 ;
RightMarginSeparator :=  +1 ; FooterSeparator := +10 ;
RightMargin          :=  +2 ; Footer          := +20 ;
RightEdgeSeparator   :=  +3 ; BottomSeparator := +30 ;
RightEdge            :=  +4 ; Bottom          := +40 ;

numeric HorPos ; HorPos := 0 ;
numeric VerPos ; VerPos := 0 ;

immutable % permanent
    Text, HorPos, VerPos,
    LeftEdge, LeftEdgeSeparator, LeftMargin, LeftMarginSeparator,
    RightMarginSeparator, RightMargin, RightEdgeSeparator, RightEdge,
    Top, TopSeparator, Header, HeaderSeparator,
    FooterSeparator, Footer, BottomSeparator, Bottom ;

% Because metapost > 1.50 has dynamic memory management and is less efficient than
% before we now delay calculations ... (on a document with 150 pages the time spent
% in mp was close to 5 seconds which was only due to initialising the page related
% areas, something that was hardly noticeable before. At least now we're back to
% half a second for such a case.

% We could go for just setting them (:=):

path    mfun_page_area    [][][] ;
pair    mfun_page_location[][][] ;
path    mfun_page_field   [][][] ;
numeric mfun_page_vsize   [][] ;
numeric mfun_page_hsize   [][] ;
numeric mfun_page_vstep   [][] ;
numeric mfun_page_hstep   [][] ;

numeric mfun_page_odd ; mfun_page_odd := 1 ;

def mfun_page_check_vsize =
    mfun_page_vsize[mfun_page_odd][Top]             := TopHeight ;
    mfun_page_vsize[mfun_page_odd][TopSeparator]    := TopDistance ;
    mfun_page_vsize[mfun_page_odd][Header]          := HeaderHeight ;
    mfun_page_vsize[mfun_page_odd][HeaderSeparator] := HeaderDistance ;
    mfun_page_vsize[mfun_page_odd][Text]            := TextHeight ;
    mfun_page_vsize[mfun_page_odd][FooterSeparator] := FooterDistance ;
    mfun_page_vsize[mfun_page_odd][Footer]          := FooterHeight ;
    mfun_page_vsize[mfun_page_odd][BottomSeparator] := BottomDistance ;
    mfun_page_vsize[mfun_page_odd][Bottom]          := BottomHeight ;
enddef ;

def mfun_page_check_hsize =
    mfun_page_hsize[mfun_page_odd][LeftEdge]             := LeftEdgeWidth ;
    mfun_page_hsize[mfun_page_odd][LeftEdgeSeparator]    := LeftEdgeDistance ;
    mfun_page_hsize[mfun_page_odd][LeftMargin]           := LeftMarginWidth ;
    mfun_page_hsize[mfun_page_odd][LeftMarginSeparator]  := LeftMarginDistance ;
    mfun_page_hsize[mfun_page_odd][Text]                 := MakeupWidth ;
    mfun_page_hsize[mfun_page_odd][RightMarginSeparator] := RightMarginDistance ;
    mfun_page_hsize[mfun_page_odd][RightMargin]          := RightMarginWidth ;
    mfun_page_hsize[mfun_page_odd][RightEdgeSeparator]   := RightEdgeDistance ;
    mfun_page_hsize[mfun_page_odd][RightEdge]            := RightEdgeWidth ;
enddef ;

def mfun_page_check_vstep =
    mfun_page_vstep[mfun_page_odd][TopSeparator]    := PaperHeight-TopSpace ;
    mfun_page_vstep[mfun_page_odd][Top]             := mfun_page_vstep[mfun_page_odd][TopSeparator]   +mfun_page_vsize[mfun_page_odd][TopSeparator] ;
    mfun_page_vstep[mfun_page_odd][Header]          := mfun_page_vstep[mfun_page_odd][TopSeparator]   -mfun_page_vsize[mfun_page_odd][Header] ;
    mfun_page_vstep[mfun_page_odd][HeaderSeparator] := mfun_page_vstep[mfun_page_odd][Header]         -mfun_page_vsize[mfun_page_odd][HeaderSeparator] ;
    mfun_page_vstep[mfun_page_odd][Text]            := mfun_page_vstep[mfun_page_odd][HeaderSeparator]-mfun_page_vsize[mfun_page_odd][Text] ;
    mfun_page_vstep[mfun_page_odd][FooterSeparator] := mfun_page_vstep[mfun_page_odd][Text]           -mfun_page_vsize[mfun_page_odd][FooterSeparator] ;
    mfun_page_vstep[mfun_page_odd][Footer]          := mfun_page_vstep[mfun_page_odd][FooterSeparator]-mfun_page_vsize[mfun_page_odd][Footer] ;
    mfun_page_vstep[mfun_page_odd][BottomSeparator] := mfun_page_vstep[mfun_page_odd][Footer]         -mfun_page_vsize[mfun_page_odd][BottomSeparator] ;
    mfun_page_vstep[mfun_page_odd][Bottom]          := mfun_page_vstep[mfun_page_odd][BottomSeparator]-mfun_page_vsize[mfun_page_odd][Bottom] ;
enddef ;

def mfun_page_check_hstep =
    mfun_page_hstep[mfun_page_odd][Text]                 := BackSpace ;
    mfun_page_hstep[mfun_page_odd][LeftMarginSeparator]  := mfun_page_hstep[mfun_page_odd][Text]                -mfun_page_hsize[mfun_page_odd][LeftMarginSeparator] ;
    mfun_page_hstep[mfun_page_odd][RightMarginSeparator] := mfun_page_hstep[mfun_page_odd][Text]                +mfun_page_hsize[mfun_page_odd][Text] ;
    mfun_page_hstep[mfun_page_odd][LeftMargin]           := mfun_page_hstep[mfun_page_odd][LeftMarginSeparator] -mfun_page_hsize[mfun_page_odd][LeftMargin] ;
    mfun_page_hstep[mfun_page_odd][RightMargin]          := mfun_page_hstep[mfun_page_odd][RightMarginSeparator]+mfun_page_hsize[mfun_page_odd][RightMarginSeparator] ;
    mfun_page_hstep[mfun_page_odd][LeftEdgeSeparator]    := mfun_page_hstep[mfun_page_odd][LeftMargin]          -mfun_page_hsize[mfun_page_odd][LeftEdgeSeparator] ;
    mfun_page_hstep[mfun_page_odd][LeftEdge]             := mfun_page_hstep[mfun_page_odd][LeftEdgeSeparator]   -mfun_page_hsize[mfun_page_odd][LeftEdge] ;
    mfun_page_hstep[mfun_page_odd][RightEdgeSeparator]   := mfun_page_hstep[mfun_page_odd][RightMargin]         +mfun_page_hsize[mfun_page_odd][RightMargin] ;
    mfun_page_hstep[mfun_page_odd][RightEdge]            := mfun_page_hstep[mfun_page_odd][RightEdgeSeparator]  +mfun_page_hsize[mfun_page_odd][RightEdgeSeparator] ;
enddef ;

numeric mfun_last_changed_page ; mfun_last_changed_page := -1 ;
boolean mfun_page_done_odd     ; mfun_page_done_odd     := false ;
boolean mfun_page_done_even    ; mfun_page_done_even    := false ;

def mfun_check_page_dimensions_indeed =
    mfun_page_check_vsize ;
    mfun_page_check_hsize ;
    mfun_page_check_vstep ;
    mfun_page_check_hstep ;
enddef ;

def mfun_check_page_dimensions =
    begingroup ;
    save n ; n := LastChangedLayoutPage ;
    SwapMarginDimensions ; % always
    if mfun_last_changed_page <> n :
        report("page", "layout changed") ;
        mfun_page_done_odd := false ;
        mfun_page_done_even := false ;
        mfun_last_changed_page := n ;
    fi ;
    if odd RealPageNumber :
        mfun_page_odd := 1 ;
        if not mfun_page_done_odd :
            report("page", "checking odd") ;
            mfun_check_page_dimensions_indeed ;
            mfun_page_done_odd := true ;
        fi ;
    else :
        mfun_page_odd := 2 ;
        if not mfun_page_done_even :
            report("page", "checking even") ;
            mfun_check_page_dimensions_indeed ;
            mfun_page_done_even := true ;
        fi ;
    fi ;
    endgroup ;
enddef;

def mfun_check_page_area =
    mfun_check_page_dimensions ;
    for VerPos=Top step 10 until Bottom:
        for HorPos=LeftEdge step 1 until RightEdge:
            mfun_page_area[mfun_page_odd][HorPos][VerPos] := unitsquare xscaled mfun_page_hsize[mfun_page_odd][HorPos] yscaled mfun_page_vsize[where][VerPos] ;
            mfun_page_area[mfun_page_odd][VerPos][HorPos] := mfun_page_area[mfun_page_odd][HorPos][VerPos] ;
        endfor ;
    endfor ;
enddef ;

def mfun_check_page_location =
    mfun_check_page_dimensions ;
    for VerPos=Top step 10 until Bottom:
        for HorPos=LeftEdge step 1 until RightEdge:
            mfun_page_location[mfun_page_odd][HorPos][VerPos] := (mfun_page_hstep[mfun_page_odd][HorPos],mfun_page_vstep[mfun_page_odd][VerPos]) ;
            mfun_page_location[mfun_page_odd][VerPos][HorPos] := mfun_page_location[mfun_page_odd][HorPos][VerPos] ;
        endfor ;
    endfor ;
enddef ;

def mfun_check_page_field =
    mfun_check_page_dimensions ;
    for VerPos=Top step 10 until Bottom:
        for HorPos=LeftEdge step 1 until RightEdge:
            mfun_page_field[mfun_page_odd][HorPos][VerPos] := unitsquare xscaled mfun_page_hsize[mfun_page_odd][HorPos] yscaled mfun_page_vsize[mfun_page_odd][VerPos] shifted (mfun_page_hstep[mfun_page_odd][HorPos],mfun_page_vstep[mfun_page_odd][VerPos]) ;
            mfun_page_field[mfun_page_odd][VerPos][HorPos] := mfun_page_field[mfun_page_odd][HorPos][VerPos] ;
        endfor ;
    endfor ;
enddef ;

def Area     = hide(mfun_check_page_area       ;) mfun_page_area    [mfun_page_odd] enddef ;
def Location = hide(mfun_check_page_location   ;) mfun_page_location[mfun_page_odd] enddef ;
def Field    = hide(mfun_check_page_field      ;) mfun_page_field   [mfun_page_odd] enddef ;
def Vsize    = hide(mfun_check_page_dimensions ;) mfun_page_vsize   [mfun_page_odd] enddef ;
def Hsize    = hide(mfun_check_page_dimensions ;) mfun_page_hsize   [mfun_page_odd] enddef ;
def Vstep    = hide(mfun_check_page_dimensions ;) mfun_page_vstep   [mfun_page_odd] enddef ;
def Hstep    = hide(mfun_check_page_dimensions ;) mfun_page_hstep   [mfun_page_odd] enddef ;

immutable % permanent
    Area, Location, Field, Vsize, Hsize, Vstep, Hstep ;

vardef FrontPageWidth  = PaperWidth enddef ;
vardef BackPageWidth   = PaperWidth enddef ;
vardef CoverWidth      = 2 * PaperWidth + SpineWidth enddef ;
vardef CoverHeight     = PaperHeight enddef ;

vardef FrontPageHeight = PaperHeight enddef ;
vardef BackPageHeight  = PaperHeight enddef ;
vardef SpineHeight     = PaperHeight enddef ;

path mfun_page_page, mfun_page_cover, mfun_page_spine, mfun_page_back, mfun_page_front ;

def mfun_check_page  = mfun_page_page  := unitsquare xscaled PaperWidth     yscaled PaperHeight ; enddef ;
def mfun_check_cover = mfun_page_cover := unitsquare xscaled CoverWidth     yscaled CoverHeight ; enddef ;
def mfun_check_spine = mfun_page_spine := unitsquare xscaled SpineWidth     yscaled CoverHeight shifted (BackPageWidth,0) ; enddef ;
def mfun_check_back  = mfun_page_back  := unitsquare xscaled BackPageWidth  yscaled CoverHeight ; enddef ;
def mfun_check_front = mfun_page_front := unitsquare xscaled FrontPageWidth yscaled CoverHeight shifted (BackPageWidth+SpineWidth,0) ; enddef ;

def Page      = hide(mfun_check_page  ;) mfun_page_page  enddef ;
def CoverPage = hide(mfun_check_cover ;) mfun_page_cover enddef ;
def Spine     = hide(mfun_check_spine ;) mfun_page_spine enddef ;
def BackPage  = hide(mfun_check_back  ;) mfun_page_back  enddef ;
def FrontPage = hide(mfun_check_front ;) mfun_page_front enddef ;

% pages

def StartPage =
    begingroup ;
    setbounds currentpicture to Page ;
enddef ;

def StopPage =
    setbounds currentpicture to Page ;
    endgroup ;
enddef ;

% cover pages

def StartCover =
    begingroup ;
    setbounds currentpicture to CoverPage enlarged PaperBleed ;
enddef ;

def StopCover =
    setbounds currentpicture to CoverPage enlarged PaperBleed ;
    endgroup ;
enddef ;

immutable % permanent
    FrontPageWidth, BackPageWidth, CoverWidth, FrontPageHeight, BackPageHeight, CoverHeight,
    SpineHeight, Page, CoverPage, Spine, BackPage, FrontPage,
    StartPage, StopPage, StartCover, StopCover ;

% overlays:

def OverlayBox =
    (unitsquare xyscaled (OverlayWidth,OverlayHeight))
enddef ;

def BoundToOverlayBox =
    setbounds currentpicture to OverlayBox;
enddef ;

immutable % permanent
    OverlayBox ;

permanent
    BoundToOverlayBox ;

% handy

def innerenlarged =
    if OnRightPage : leftenlarged  else : rightenlarged fi
enddef ;

def outerenlarged =
    if OnRightPage : rightenlarged else : leftenlarged  fi
enddef ;

permanent
    innerenlarged, outerenlarged ;

% obsolete

% def llEnlarged (expr p,d) = (llcorner p shifted (-d,-d)) enddef ;
% def lrEnlarged (expr p,d) = (lrcorner p shifted (+d,-d)) enddef ;
% def urEnlarged (expr p,d) = (urcorner p shifted (+d,+d)) enddef ;
% def ulEnlarged (expr p,d) = (ulcorner p shifted (-d,+d)) enddef ;

% def Enlarged (expr p, d) =
%     (
%         llEnlarged (p,d) --
%         lrEnlarged (p,d) --
%         urEnlarged (p,d) --
%         ulEnlarged (p,d) --
%         cycle
%     )
% enddef ;