summaryrefslogtreecommitdiff
path: root/metapost/context/base/mpiv/mp-page.mpiv
blob: a996bfd66270a1858a0be1c6e94ea34739b70db1 (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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
%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.

if known context_page : endinput ; fi ;

boolean context_page ; context_page := true ;

% def LoadPageState =
%     % now always set
% enddef ;
%
% if unknown PageStateAvailable :
%     boolean PageStateAvailable ;
%     PageStateAvailable := false ;
% fi ;
%
% if unknown OnRightPage :
%     boolean OnRightPage ;
%     OnRightPage := true ;
% fi ;
%
% if unknown OnOddPage :
%     boolean OnOddPage ;
%     OnOddPage := true ;
% fi ;
%
% if unknown InPageBody :
%     boolean InPageBody ;
%     InPageBody := false ;
% fi ;
%
% string CurrentLayout ;
%
% CurrentLayout       := "default" ;
%
% PageNumber          := 0 ;
% PaperHeight         := 845.04684pt  ;
% PaperWidth          := 597.50787pt  ;
% PrintPaperHeight    := 845.04684pt  ;
% PrintPaperWidth     := 597.50787pt  ;
% TopSpace            :=  71.12546pt  ;
% BottomSpace         :=   0.0pt      ;
% BackSpace           :=  71.13275pt  ;
% CutSpace            :=   0.0pt      ;
% MakeupHeight        := 711.3191pt   ;
% MakeupWidth         := 426.78743pt  ;
% TopHeight           :=   0.0pt      ;
% TopDistance         :=   0.0pt      ;
% HeaderHeight        :=  56.90294pt  ;
% HeaderDistance      :=   0.0pt      ;
% TextHeight          := 597.51323pt  ;
% FooterDistance      :=   0.0pt      ;
% FooterHeight        :=  56.90294pt  ;
% BottomDistance      :=   0.0pt      ;
% BottomHeight        :=   0.0pt      ;
% LeftEdgeWidth       :=   0.0pt      ;
% LeftEdgeDistance    :=   0.0pt      ;
% LeftMarginWidth     :=   75.58197pt ;
% LeftMarginDistance  :=   11.99829pt ;
% TextWidth           :=  426.78743pt ;
% RightMarginDistance :=   11.99829pt ;
% RightMarginWidth    :=   75.58197pt ;
% RightEdgeDistance   :=    0.0pt     ;
% RightEdgeWidth      :=    0.0pt     ;
%
% PageOffset          :=    0.0pt     ;
% PageDepth           :=    0.0pt     ;
%
% LayoutColumns       :=    0         ;
% LayoutColumnDistance:=    0.0pt     ;
% LayoutColumnWidth   :=    0.0pt     ;
%
% 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 ;
%
% Margin      := LeftMargin  ; % obsolete
% Edge        := LeftEdge    ; % obsolete
% InnerMargin := RightMargin ; % obsolete
% InnerEdge   := RightEdge   ; % obsolete
% OuterMargin := LeftMargin  ; % obsolete
% OuterEdge   := LeftEdge    ; % obsolete
%
% InnerMarginWidth     := 0pt ;
% OuterMarginWidth     := 0pt ;
% InnerMarginDistance  := 0pt ;
% OuterMarginDistance  := 0pt ;
%
% InnerEdgeWidth     := 0pt ;
% OuterEdgeWidth     := 0pt ;
% InnerEdgeDistance  := 0pt ;
% OuterEdgeDistance  := 0pt ;
%
% % path Area[][] ;
% % pair Location[][] ;
% % path Field[][] ;
%
% % numeric Hstep[] ;
% % numeric Hsize[] ;
% % numeric Vstep[] ;
% % numeric Vsize[] ;
%
% path Page ;
%
% numeric HorPos ;
% numeric VerPos ;
%
% % for VerPos=Top step 10 until Bottom:
% %     for HorPos=LeftEdge step 1 until RightEdge:
% %         Area[HorPos][VerPos] := origin--cycle ;
% %         Area[VerPos][HorPos] := Area[HorPos][VerPos] ;
% %         Location[HorPos][VerPos] := origin ;
% %         Location[VerPos][HorPos] := Location[HorPos][VerPos] ;
% %         Field[HorPos][VerPos] := origin--cycle ;
% %         Field[VerPos][HorPos] := Field[HorPos][VerPos] ;
% %     endfor ;
% % endfor ;
%
% % def LoadPageState =
% %   scantokens "input mp-state.tmp" ;
% % enddef ;
%
% numeric mfun_temp ;
%
% def SwapPageState =
%     if not OnRightPage :
%              BackSpace := PaperWidth-MakeupWidth-BackSpace ;
%               CutSpace := PaperWidth-MakeupWidth-CutSpace ;
%              mfun_temp := LeftMarginWidth ;
%        LeftMarginWidth := RightMarginWidth ;
%       RightMarginWidth := mfun_temp ;
%              mfun_temp := LeftMarginDistance ;
%     LeftMarginDistance := RightMarginDistance ;
%    RightMarginDistance := mfun_temp ;
%              mfun_temp := LeftEdgeWidth ;
%          LeftEdgeWidth := RightEdgeWidth ;
%         RightEdgeWidth := mfun_temp ;
%              mfun_temp := LeftEdgeDistance ;
%       LeftEdgeDistance := RightEdgeDistance ;
%      RightEdgeDistance := mfun_temp ;
%
%   % these are now available as ..Width and ..Distance
%
%                 Margin := LeftMargin ;
%                   Edge := LeftEdge ;
%            InnerMargin := RightMargin ;
%              InnerEdge := RightEdge ;
%            OuterMargin := LeftMargin ;
%              OuterEdge := LeftEdge ;
%     else :
%                 Margin := RightMargin ;
%                   Edge := RightEdge ;
%            InnerMargin := LeftMargin ;
%              InnerEdge := LeftEdge ;
%            OuterMargin := RightMargin ;
%              OuterEdge := RightEdge ;
%     fi ;
% enddef ;

% the new way:

def LoadPageState =
    % now always set
enddef ;

if unknown PageStateAvailable :
    boolean PageStateAvailable ;
    PageStateAvailable := false ;
fi ;

string CurrentLayout ; CurrentLayout := "default" ;

% runscript("mp.PaperHeight()") is much faster than lua.mp.PaperHeight() so we use that now:

vardef PaperHeight          = runscript("mp.PaperHeight()")          enddef ;
vardef PaperWidth           = runscript("mp.PaperWidth()")           enddef ;
vardef PrintPaperHeight     = runscript("mp.PrintPaperHeight()")     enddef ;
vardef PrintPaperWidth      = runscript("mp.PrintPaperWidth()")      enddef ;
vardef TopSpace             = runscript("mp.TopSpace()")             enddef ;
vardef BottomSpace          = runscript("mp.BottomSpace()")          enddef ;
vardef BackSpace            = runscript("mp.BackSpace()")            enddef ;
vardef CutSpace             = runscript("mp.CutSpace()")             enddef ;
vardef MakeupHeight         = runscript("mp.MakeupHeight()")         enddef ;
vardef MakeupWidth          = runscript("mp.MakeupWidth()")          enddef ;
vardef TopHeight            = runscript("mp.TopHeight()")            enddef ;
vardef TopDistance          = runscript("mp.TopDistance()")          enddef ;
vardef HeaderHeight         = runscript("mp.HeaderHeight()")         enddef ;
vardef HeaderDistance       = runscript("mp.HeaderDistance()")       enddef ;
vardef TextHeight           = runscript("mp.TextHeight()")           enddef ;
vardef FooterDistance       = runscript("mp.FooterDistance()")       enddef ;
vardef FooterHeight         = runscript("mp.FooterHeight()")         enddef ;
vardef BottomDistance       = runscript("mp.BottomDistance()")       enddef ;
vardef BottomHeight         = runscript("mp.BottomHeight()")         enddef ;
vardef LeftEdgeWidth        = runscript("mp.LeftEdgeWidth()")        enddef ;
vardef LeftEdgeDistance     = runscript("mp.LeftEdgeDistance()")     enddef ;
vardef LeftMarginWidth      = runscript("mp.LeftMarginWidth()")      enddef ;
vardef LeftMarginDistance   = runscript("mp.LeftMarginDistance()")   enddef ;
vardef TextWidth            = runscript("mp.TextWidth()")            enddef ;
vardef RightMarginDistance  = runscript("mp.RightMarginDistance()")  enddef ;
vardef RightMarginWidth     = runscript("mp.RightMarginWidth()")     enddef ;
vardef RightEdgeDistance    = runscript("mp.RightEdgeDistance()")    enddef ;
vardef RightEdgeWidth       = runscript("mp.RightEdgeWidth()")       enddef ;
vardef InnerMarginDistance  = runscript("mp.InnerMarginDistance()")  enddef ;
vardef InnerMarginWidth     = runscript("mp.InnerMarginWidth()")     enddef ;
vardef OuterMarginDistance  = runscript("mp.OuterMarginDistance()")  enddef ;
vardef OuterMarginWidth     = runscript("mp.OuterMarginWidth()")     enddef ;
vardef InnerEdgeDistance    = runscript("mp.InnerEdgeDistance()")    enddef ;
vardef InnerEdgeWidth       = runscript("mp.InnerEdgeWidth()")       enddef ;
vardef OuterEdgeDistance    = runscript("mp.OuterEdgeDistance()")    enddef ;
vardef OuterEdgeWidth       = runscript("mp.OuterEdgeWidth()")       enddef ;
vardef PageOffset           = runscript("mp.PageOffset()")           enddef ;
vardef PageDepth            = runscript("mp.PageDepth()")            enddef ;
vardef LayoutColumns        = runscript("mp.LayoutColumns()")        enddef ;
vardef LayoutColumnDistance = runscript("mp.LayoutColumnDistance()") enddef ;
vardef LayoutColumnWidth    = runscript("mp.LayoutColumnWidth()")    enddef ;

vardef OnRightPage          = runscript("mp.OnRightPage()")          enddef ;
vardef OnOddPage            = runscript("mp.OnOddPage()")            enddef ;
vardef InPageBody           = runscript("mp.InPageBody()")           enddef ;

vardef RealPageNumber       = runscript("mp.RealPageNumber()")       enddef ;
vardef LastPageNumber       = runscript("mp.LastPageNumber()")       enddef ; % duplicates

vardef PageNumber           = runscript("mp.PageNumber()")           enddef ;
vardef NOfPages             = runscript("mp.NOfPages()")             enddef ;

vardef SubPageNumber        = runscript("mp.SubPageNumber()")        enddef ;
vardef NOfSubPages          = runscript("mp.NOfSubPages()")          enddef ;

vardef CurrentColumn        = runscript("mp.CurrentColumn()")        enddef ;
vardef NOfColumns           = runscript("mp.NOfColumns()")           enddef ;

vardef BaseLineSkip         = runscript("mp.BaseLineSkip()")         enddef ;
vardef LineHeight           = runscript("mp.LineHeight()")           enddef ;
vardef BodyFontSize         = runscript("mp.BodyFontSize()")         enddef ;

vardef TopSkip              = runscript("mp.TopSkip()")              enddef ;
vardef StrutHeight          = runscript("mp.StrutHeight()")          enddef ;
vardef StrutDepth           = runscript("mp.StrutDepth()")           enddef ;

vardef CurrentWidth         = runscript("mp.CurrentWidth()")         enddef ;
vardef CurrentHeight        = runscript("mp.CurrentHeight()")        enddef ;

vardef HSize                = runscript("mp.HSize()")                enddef ; % duplicates
vardef VSize                = runscript("mp.VSize()")                enddef ; % duplicates

vardef EmWidth              = runscript("mp.EmWidth()")              enddef ;
vardef ExHeight             = runscript("mp.ExHeight()")             enddef ;

vardef PageFraction         = runscript("mp.PageFraction()")         enddef ;

vardef SpineWidth           = runscript("mp.SpineWidth()")           enddef ;
vardef PaperBleed           = runscript("mp.PaperBleed()")           enddef ;

boolean mfun_swapped ;

def SwapPageState =
    mfun_swapped := true ; % eventually this will go !
enddef ;

extra_beginfig := extra_beginfig & "mfun_swapped := false ;" ;

vardef LeftMarginWidth     = if mfun_swapped and not OnRightPage : runscript("mp.RightMarginWidth()")    else : runscript("mp.LeftMarginWidth()")     fi enddef ;
vardef RightMarginWidth    = if mfun_swapped and not OnRightPage : runscript("mp.LeftMarginWidth()")     else : runscript("mp.RightMarginWidth()")    fi enddef ;
vardef LeftMarginDistance  = if mfun_swapped and not OnRightPage : runscript("mp.RightMarginDistance()") else : runscript("mp.LeftMarginDistance()")  fi enddef ;
vardef RightMarginDistance = if mfun_swapped and not OnRightPage : runscript("mp.LeftMarginDistance()")  else : runscript("mp.RightMarginDistance()") fi enddef ;

vardef LeftEdgeWidth       = if mfun_swapped and not OnRightPage : runscript("mp.RightEdgeWidth()")    else : runscript("mp.LeftEdgeWidth()")     fi enddef ;
vardef RightEdgeWidth      = if mfun_swapped and not OnRightPage : runscript("mp.LeftEdgeWidth()")     else : runscript("mp.RightEdgeWidth()")    fi enddef ;
vardef LeftEdgeDistance    = if mfun_swapped and not OnRightPage : runscript("mp.RightEdgeDistance()") else : runscript("mp.LeftEdgeDistance()")  fi enddef ;
vardef RightEdgeDistance   = if mfun_swapped and not OnRightPage : runscript("mp.LeftEdgeDistance()")  else : runscript("mp.RightEdgeDistance()") fi enddef ;

vardef BackSpace           = if mfun_swapped and not OnRightPage : PaperWidth - MakeupWidth - fi runscript("mp.BackSpace()") enddef ;
vardef CutSpace            = if mfun_swapped and not OnRightPage : PaperWidth - MakeupWidth - fi runscript("mp.CutSpace()")  enddef ;

% better use:

vardef OuterMarginWidth    = if not OnRightPage : runscript("mp.LeftMarginWidth()")     else : runscript("mp.RightMarginWidth()")    fi enddef ;
vardef InnerMarginWidth    = if not OnRightPage : runscript("mp.RightMarginWidth()")    else : runscript("mp.LeftMarginWidth()")     fi enddef ;
vardef OuterMarginDistance = if not OnRightPage : runscript("mp.LeftMarginDistance()")  else : runscript("mp.RightMarginDistance()") fi enddef ;
vardef InnerMarginDistance = if not OnRightPage : runscript("mp.RightMarginDistance()") else : runscript("mp.LeftMarginDistance()")  fi enddef ;

vardef OuterEdgeWidth      = if not OnRightPage : runscript("mp.LeftEdgeWidth()")     else : runscript("mp.RightEdgeWidth()")    fi enddef ;
vardef InnerEdgeWidth      = if not OnRightPage : runscript("mp.RightEdgeWidth()")    else : runscript("mp.LeftEdgeWidth()")     fi enddef ;
vardef OuterEdgeDistance   = if not OnRightPage : runscript("mp.LeftEdgeDistance()")  else : runscript("mp.RightEdgeDistance()") fi enddef ;
vardef InnerEdgeDistance   = if not OnRightPage : runscript("mp.RightEdgeDistance()") else : runscript("mp.LeftEdgeDistance()")  fi enddef ;

vardef OuterSpaceWidth     = if not OnRightPage : runscript("mp.BackSpace()") else : runscript("mp.CutSpace()")  fi enddef ;
vardef InnerSpaceWidth     = if not OnRightPage : runscript("mp.CutSpace()")  else : runscript("mp.BackSpace()") fi enddef ;

% indices

vardef OuterMargin = if not OnRightPage : LeftMargin  else : RightMargin fi enddef ;
vardef InnerMargin = if not OnRightPage : RightMargin else : LeftMargin  fi enddef ;

vardef OuterEdge   = if not OnRightPage : LeftEdge    else : RightEdge   fi enddef ;
vardef InnerEdge   = if not OnRightPage : Rightedge   else : LeftEdge    fi enddef ;

% vardef CurrentLayout = runscript("mp.CurrentLayout()") enddef ;

vardef OverlayWidth      = runscript("mp.OverlayWidth()")     enddef ;
vardef OverlayHeight     = runscript("mp.OverlayHeight()")    enddef ;
vardef OverlayDepth      = runscript("mp.OverlayDepth()")     enddef ;
vardef OverlayLineWidth  = runscript("mp.OverlayLineWidth()") enddef ;
vardef OverlayOffset     = runscript("mp.OverlayOffset()")    enddef ;
vardef OverlayRegion     = runscript("mp.OverlayRegion()")    enddef ;

vardef defaultcolormodel = runscript("mp.mf_default_color_model()") enddef ;

%  def OverlayLineColor  = runscript("mp.OverlayLineColor()")  enddef ;
%  def OverlayColor      = runscript("mp.OverlayColor()")  enddef ;

% Next we implement the the page area model. First some constants.

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 ;

% Margin      := LeftMargin  ; % obsolete
% Edge        := LeftEdge    ; % obsolete
% InnerMargin := RightMargin ; % obsolete
% InnerEdge   := RightEdge   ; % obsolete
% OuterMargin := LeftMargin  ; % obsolete
% OuterEdge   := LeftEdge    ; % obsolete

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

% We used to initialize these variables each (sub)run but at some point MP
% became too slow for this. See later.

% path Area[][] ;
% pair Location[][] ;
% path Field[][] ;
%
% numeric Hstep[] ;
% numeric Hsize[] ;
% numeric Vstep[] ;
% numeric Vsize[] ;
%
% for VerPos=Top step 10 until Bottom:
%     for HorPos=LeftEdge step 1 until RightEdge:
%         Area[HorPos][VerPos] := origin--cycle ;
%         Area[VerPos][HorPos] := Area[HorPos][VerPos] ;
%         Location[HorPos][VerPos] := origin ;
%         Location[VerPos][HorPos] := Location[HorPos][VerPos] ;
%         Field[HorPos][VerPos] := origin--cycle ;
%         Field[VerPos][HorPos] := Field[HorPos][VerPos] ;
%     endfor ;
% endfor ;
%
%
% def SetPageAreas =
%
%     numeric Vsize[], Hsize[], Vstep[], Hstep[] ;
%
%     Vsize[Top]             = TopHeight ;
%     Vsize[TopSeparator]    = TopDistance ;
%     Vsize[Header]          = HeaderHeight ;
%     Vsize[HeaderSeparator] = HeaderDistance ;
%     Vsize[Text]            = TextHeight ;
%     Vsize[FooterSeparator] = FooterDistance ;
%     Vsize[Footer]          = FooterHeight ;
%     Vsize[BottomSeparator] = BottomDistance ;
%     Vsize[Bottom]          = BottomHeight ;
%
%     Vstep[Top]             = Vstep[TopSeparator]   +Vsize[TopSeparator] ;
%     Vstep[TopSeparator]    = PaperHeight-TopSpace ;
%     Vstep[Header]          = Vstep[TopSeparator]   -Vsize[Header] ;
%     Vstep[HeaderSeparator] = Vstep[Header]         -Vsize[HeaderSeparator] ;
%     Vstep[Text]            = Vstep[HeaderSeparator]-Vsize[Text] ;
%     Vstep[FooterSeparator] = Vstep[Text]           -Vsize[FooterSeparator] ;
%     Vstep[Footer]          = Vstep[FooterSeparator]-Vsize[Footer] ;
%     Vstep[BottomSeparator] = Vstep[Footer]         -Vsize[BottomSeparator] ;
%     Vstep[Bottom]          = Vstep[BottomSeparator]-Vsize[Bottom] ;
%
%     Hsize[LeftEdge]             = LeftEdgeWidth ;
%     Hsize[LeftEdgeSeparator]    = LeftEdgeDistance ;
%     Hsize[LeftMargin]           = LeftMarginWidth ;
%     Hsize[LeftMarginSeparator]  = LeftMarginDistance ;
%     Hsize[Text]                 = MakeupWidth ;
%     Hsize[RightMarginSeparator] = RightMarginDistance ;
%     Hsize[RightMargin]          = RightMarginWidth ;
%     Hsize[RightEdgeSeparator]   = RightEdgeDistance ;
%     Hsize[RightEdge]            = RightEdgeWidth ;
%
%     Hstep[LeftEdge]             = Hstep[LeftEdgeSeparator]   -Hsize[LeftEdge] ;
%     Hstep[LeftEdgeSeparator]    = Hstep[LeftMargin]          -Hsize[LeftEdgeSeparator] ;
%     Hstep[LeftMargin]           = Hstep[LeftMarginSeparator] -Hsize[LeftMargin] ;
%     Hstep[LeftMarginSeparator]  = Hstep[Text]                -Hsize[LeftMarginSeparator] ;
%     Hstep[Text]                 = BackSpace ;
%     Hstep[RightMarginSeparator] = Hstep[Text]                +Hsize[Text] ;
%     Hstep[RightMargin]          = Hstep[RightMarginSeparator]+Hsize[RightMarginSeparator] ;
%     Hstep[RightEdgeSeparator]   = Hstep[RightMargin]         +Hsize[RightMargin] ;
%     Hstep[RightEdge]            = Hstep[RightEdgeSeparator]  +Hsize[RightEdgeSeparator] ;
%
%     for VerPos=Top step 10 until Bottom:
%         for HorPos=LeftEdge step 1 until RightEdge:
%             Area[HorPos][VerPos] := unitsquare xscaled Hsize[HorPos] yscaled Vsize[VerPos] ;
%             Area[VerPos][HorPos] := Area[HorPos][VerPos] ;
%             Location[HorPos][VerPos] := (Hstep[HorPos],Vstep[VerPos]) ;
%             Location[VerPos][HorPos] := Location[HorPos][VerPos] ;
%             Field[HorPos][VerPos] := Area[HorPos][VerPos] shifted Location[HorPos][VerPos] ;
%             Field[VerPos][HorPos] := Field[HorPos][VerPos] ;
%         endfor ;
%     endfor ;
%
%     Page := unitsquare xscaled PaperWidth yscaled PaperHeight ;
%
% enddef ;
%
% def BoundPageAreas =
%     % pickup pencircle scaled 0pt ;
%     bboxmargin := 0 ; setbounds currentpicture to Page ;
% enddef ;
%
% def StartPage =
%     begingroup ;
%     if PageStateAvailable :
%         LoadPageState ;
%         SwapPageState ;
%     fi ;
%     SetPageAreas ;
%     BoundPageAreas ;
% enddef ;
%
% def StopPage =
%     BoundPageAreas ;
%     endgroup ;
% enddef ;

% 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.

def SetPageVsize =
    numeric Vsize[] ;
    Vsize[Top]             = TopHeight ;
    Vsize[TopSeparator]    = TopDistance ;
    Vsize[Header]          = HeaderHeight ;
    Vsize[HeaderSeparator] = HeaderDistance ;
    Vsize[Text]            = TextHeight ;
    Vsize[FooterSeparator] = FooterDistance ;
    Vsize[Footer]          = FooterHeight ;
    Vsize[BottomSeparator] = BottomDistance ;
    Vsize[Bottom]          = BottomHeight ;
enddef ;

def SetPageHsize =
    numeric Hsize[] ;
    Hsize[LeftEdge]             = LeftEdgeWidth ;
    Hsize[LeftEdgeSeparator]    = LeftEdgeDistance ;
    Hsize[LeftMargin]           = LeftMarginWidth ;
    Hsize[LeftMarginSeparator]  = LeftMarginDistance ;
    Hsize[Text]                 = MakeupWidth ;
    Hsize[RightMarginSeparator] = RightMarginDistance ;
    Hsize[RightMargin]          = RightMarginWidth ;
    Hsize[RightEdgeSeparator]   = RightEdgeDistance ;
    Hsize[RightEdge]            = RightEdgeWidth ;
enddef ;

def SetPageVstep =
    numeric Vstep[] ;
    Vstep[Top]             = Vstep[TopSeparator]   +Vsize[TopSeparator] ;
    Vstep[TopSeparator]    = PaperHeight-TopSpace ;
    Vstep[Header]          = Vstep[TopSeparator]   -Vsize[Header] ;
    Vstep[HeaderSeparator] = Vstep[Header]         -Vsize[HeaderSeparator] ;
    Vstep[Text]            = Vstep[HeaderSeparator]-Vsize[Text] ;
    Vstep[FooterSeparator] = Vstep[Text]           -Vsize[FooterSeparator] ;
    Vstep[Footer]          = Vstep[FooterSeparator]-Vsize[Footer] ;
    Vstep[BottomSeparator] = Vstep[Footer]         -Vsize[BottomSeparator] ;
    Vstep[Bottom]          = Vstep[BottomSeparator]-Vsize[Bottom] ;
enddef ;

def SetPageHstep =
    numeric Hstep[] ;
    Hstep[LeftEdge]             = Hstep[LeftEdgeSeparator]   -Hsize[LeftEdge] ;
    Hstep[LeftEdgeSeparator]    = Hstep[LeftMargin]          -Hsize[LeftEdgeSeparator] ;
    Hstep[LeftMargin]           = Hstep[LeftMarginSeparator] -Hsize[LeftMargin] ;
    Hstep[LeftMarginSeparator]  = Hstep[Text]                -Hsize[LeftMarginSeparator] ;
    Hstep[Text]                 = BackSpace ;
    Hstep[RightMarginSeparator] = Hstep[Text]                +Hsize[Text] ;
    Hstep[RightMargin]          = Hstep[RightMarginSeparator]+Hsize[RightMarginSeparator] ;
    Hstep[RightEdgeSeparator]   = Hstep[RightMargin]         +Hsize[RightMargin] ;
    Hstep[RightEdge]            = Hstep[RightEdgeSeparator]  +Hsize[RightEdgeSeparator] ;
enddef ;

def SetPageArea =
    path Area[][] ;
    for VerPos=Top step 10 until Bottom:
        for HorPos=LeftEdge step 1 until RightEdge:
            Area[HorPos][VerPos] := unitsquare xscaled Hsize[HorPos] yscaled Vsize[VerPos] ;
            Area[VerPos][HorPos] := Area[HorPos][VerPos] ;
        endfor ;
    endfor ;
enddef ;

def SetPageLocation =
    pair Location[][] ;
    for VerPos=Top step 10 until Bottom:
        for HorPos=LeftEdge step 1 until RightEdge:
            Location[HorPos][VerPos] := (Hstep[HorPos],Vstep[VerPos]) ;
            Location[VerPos][HorPos] := Location[HorPos][VerPos] ;
        endfor ;
    endfor ;
enddef ;

def SetPageField =
    path Field[][] ;
    for VerPos=Top step 10 until Bottom:
        for HorPos=LeftEdge step 1 until RightEdge:
            Field[HorPos][VerPos] := unitsquare xscaled Hsize[HorPos] yscaled Vsize[VerPos] shifted (Hstep[HorPos],Vstep[VerPos]) ;
            Field[VerPos][HorPos] := Field[HorPos][VerPos] ;
        endfor ;
    endfor ;
enddef ;

def mfun_page_Area      = hide(SetPageArea     ;) Area      enddef ;
def mfun_page_Location  = hide(SetPageLocation ;) Location  enddef ;
def mfun_page_Field     = hide(SetPageField    ;) Field     enddef ;
def mfun_page_Vsize     = hide(SetPageVsize    ;) Vsize     enddef ;
def mfun_page_Hsize     = hide(SetPageHsize    ;) Hsize     enddef ;
def mfun_page_Vstep     = hide(SetPageVstep    ;) Vstep     enddef ;
def mfun_page_Hstep     = hide(SetPageHstep    ;) Hstep     enddef ;

def SetAreaVariables =
    let Area      = mfun_page_Area ;
    let Location  = mfun_page_Location ;
    let Field     = mfun_page_Field ;
    let Vsize     = mfun_page_Vsize ;
    let Hsize     = mfun_page_Hsize ;
    let Vstep     = mfun_page_Vstep ;
    let Hstep     = mfun_page_Hstep ;
enddef ;

% we should make Page no path .. from now on don't assume this .. for a while we keek it

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 ;

def SetPagePage      = path Page      ; Page      := unitsquare xscaled PaperWidth      yscaled PaperHeight ; enddef ;
def SetPageCoverPage = path CoverPage ; CoverPage := unitsquare xscaled CoverWidth      yscaled CoverHeight ; enddef ;
def SetPageSpine     = path Spine     ; Spine     := unitsquare xscaled SpineWidth      yscaled CoverHeight shifted (BackPageWidth,0) ; enddef ;
def SetPageBackPage  = path BackPage  ; BackPage  := unitsquare xscaled BackPageWidth   yscaled CoverHeight ; enddef ;
def SetPageFrontPage = path FrontPage ; FrontPage := unitsquare xscaled FrontPageWidth  yscaled CoverHeight shifted (BackPageWidth+SpineWidth,0) ; enddef ;

def mfun_page_Page      = hide(SetPagePage     ;) Page      enddef ;
def mfun_page_CoverPage = hide(SetPageCoverPage;) CoverPage enddef ;
def mfun_page_Spine     = hide(SetPageSpine    ;) Spine     enddef ;
def mfun_page_BackPage  = hide(SetPageBackPage ;) BackPage  enddef ;
def mfun_page_FrontPage = hide(SetPageFrontPage;) FrontPage enddef ;

def SetPageVariables =
    SetAreaVariables ;
    %
    let Page      = mfun_page_Page ;
    let CoverPage = mfun_page_CoverPage ;
    let Spine     = mfun_page_Spine ;
    let BackPage  = mfun_page_BackPage ;
    let FrontPage = mfun_page_FrontPage ;
enddef ;

SetPageVariables ;

let SetPageAreas = SetPageVariables ;  % compatiblity

def BoundPageAreas =
    % pickup pencircle scaled 0pt ;
    bboxmargin := 0 ; setbounds currentpicture to Page ;
enddef ;

def StartPage =
    begingroup ;
    if mfun_first_run :
        if PageStateAvailable :
            LoadPageState ;
            SwapPageState ;
        fi ;
        SetPageVariables ;
    fi ;
    BoundPageAreas ;
enddef ;

def StopPage =
    BoundPageAreas ;
    endgroup ;
enddef ;

% cover pages

def BoundCoverAreas =
    % todo: add cropmarks
    bboxmargin := 0 ; setbounds currentpicture to CoverPage enlarged PaperBleed ;
enddef ;

let SetCoverAreas = SetPageVariables ;  % compatiblity

def StartCover =
    begingroup ;
    if mfun_first_run :
        if PageStateAvailable :
            LoadPageState ;
         %  SwapPageState ;
        fi ;
        SetPageVariables ; % was SetPageAreas ;
        SetCoverAreas ;
    fi ;
    BoundCoverAreas ;
enddef ;

def StopCover =
    BoundCoverAreas ;
    endgroup ;
enddef ;

% overlays:

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

% handy

def innerenlarged =
      hide(LoadPageState)
      if OnRightPage : leftenlarged  else : rightenlarged fi
enddef ;

def outerenlarged =
      hide(LoadPageState)
      if OnRightPage : rightenlarged else : leftenlarged  fi
enddef ;

% 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 ;

% for the moment we put these here:

string  RuleDirection ; RuleDirection := "" ;
string  RuleOption    ; RuleOption    := "" ;
numeric RuleWidth     ; RuleWidth     := 0 ;
numeric RuleHeight    ; RuleHeight    := 0 ;
numeric RuleDepth     ; RuleDepth     := 0 ;
numeric RuleH         ; RuleH         := 0 ;
numeric RuleV         ; RuleV         := 0 ;
numeric RuleThickness ; RuleThickness := 0 ;
numeric RuleFactor    ; RuleFactor    := 0 ;
numeric RuleOffset    ; RuleOffset    := 0 ;
                    def RuleColor      = (.5white) enddef ;

def FakeWord(expr RuleWidth, RuleHeight, RuleDepth, RuleThickness) (text RuleColor) =
    fill unitsquare
        xscaled RuleWidth
        yscaled (RuleDepth-RuleThickness/2)
        withcolor RuleColor ;
    fill unitsquare
        xscaled RuleWidth
        yscaled (RuleHeight-RuleDepth-RuleThickness/2)
        shifted (0,RuleDepth+RuleThickness)
        withcolor RuleColor ;
enddef ;

def FakeRule(expr RuleWidth, RuleHeight, RuleDepth, RuleThickness) (text RuleColor) =
    fill unitsquare
        xscaled RuleWidth
        yscaled RuleHeight
        withcolor RuleColor ;
enddef ;