mp-page.mpxl /size: 11 Kb    last modification: 2021-10-28 13:50
1%D \module
2%D   [       file=mp-page.mpiv,
3%D        version=1999.03.10,
4%D          title=\CONTEXT\ \METAPOST\ graphics,
5%D       subtitle=page enhancements,
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 module is rather preliminary and subjected to changes.In the process of
15%D moving to \METAFUN2\ this might change.
16
17if known metafun_loaded_page : endinput ; fi ;
18
19newinternal boolean metafun_loaded_page ; metafun_loaded_page := true ; immutable metafun_loaded_page ;
20
21def LoadPageState = enddef ; % just in case some old style uses it
22
23% Next we implement the the page area model. First some constants. We use a
24% matrix approach as we do at the TeX end but we could have gone for a period
25% separated variant. Too late.
26
27LeftEdge             :=  -4 ; Top             := -40 ;
28LeftEdgeSeparator    :=  -3 ; TopSeparator    := -30 ;
29LeftMargin           :=  -2 ; Header          := -20 ;
30LeftMarginSeparator  :=  -1 ; HeaderSeparator := -10 ;
31Text                 :=   0 ; Text            :=   0 ;
32RightMarginSeparator :=  +1 ; FooterSeparator := +10 ;
33RightMargin          :=  +2 ; Footer          := +20 ;
34RightEdgeSeparator   :=  +3 ; BottomSeparator := +30 ;
35RightEdge            :=  +4 ; Bottom          := +40 ;
36
37numeric HorPos ; HorPos := 0 ;
38numeric VerPos ; VerPos := 0 ;
39
40immutable % permanent
41    Text, HorPos, VerPos,
42    LeftEdge, LeftEdgeSeparator, LeftMargin, LeftMarginSeparator,
43    RightMarginSeparator, RightMargin, RightEdgeSeparator, RightEdge,
44    Top, TopSeparator, Header, HeaderSeparator,
45    FooterSeparator, Footer, BottomSeparator, Bottom ;
46
47% Because metapost > 1.50 has dynamic memory management and is less efficient than
48% before we now delay calculations ... (on a document with 150 pages the time spent
49% in mp was close to 5 seconds which was only due to initialising the page related
50% areas, something that was hardly noticeable before. At least now we're back to
51% half a second for such a case.
52
53% We could go for just setting them (:=):
54
55path    mfun_page_area[][] ;
56pair    mfun_page_location[][] ;
57path    mfun_page_field[][] ;
58numeric mfun_page_vsize[] ;
59numeric mfun_page_hsize[] ;
60numeric mfun_page_vstep[] ;
61numeric mfun_page_hstep[] ;
62
63newinternal mfun_page_done ; mfun_page_done := 0 ;
64
65def mfun_page_check_vsize =
66    mfun_page_vsize[Top]             := TopHeight ;
67    mfun_page_vsize[TopSeparator]    := TopDistance ;
68    mfun_page_vsize[Header]          := HeaderHeight ;
69    mfun_page_vsize[HeaderSeparator] := HeaderDistance ;
70    mfun_page_vsize[Text]            := TextHeight ;
71    mfun_page_vsize[FooterSeparator] := FooterDistance ;
72    mfun_page_vsize[Footer]          := FooterHeight ;
73    mfun_page_vsize[BottomSeparator] := BottomDistance ;
74    mfun_page_vsize[Bottom]          := BottomHeight ;
75enddef ;
76
77def mfun_page_check_hsize =
78    mfun_page_hsize[LeftEdge]             := LeftEdgeWidth ;
79    mfun_page_hsize[LeftEdgeSeparator]    := LeftEdgeDistance ;
80    mfun_page_hsize[LeftMargin]           := LeftMarginWidth ;
81    mfun_page_hsize[LeftMarginSeparator]  := LeftMarginDistance ;
82    mfun_page_hsize[Text]                 := MakeupWidth ;
83    mfun_page_hsize[RightMarginSeparator] := RightMarginDistance ;
84    mfun_page_hsize[RightMargin]          := RightMarginWidth ;
85    mfun_page_hsize[RightEdgeSeparator]   := RightEdgeDistance ;
86    mfun_page_hsize[RightEdge]            := RightEdgeWidth ;
87enddef ;
88
89def mfun_page_check_vstep =
90    mfun_page_vstep[TopSeparator]    := PaperHeight-TopSpace ;
91    mfun_page_vstep[Top]             := mfun_page_vstep[TopSeparator]   +mfun_page_vsize[TopSeparator] ;
92    mfun_page_vstep[Header]          := mfun_page_vstep[TopSeparator]   -mfun_page_vsize[Header] ;
93    mfun_page_vstep[HeaderSeparator] := mfun_page_vstep[Header]         -mfun_page_vsize[HeaderSeparator] ;
94    mfun_page_vstep[Text]            := mfun_page_vstep[HeaderSeparator]-mfun_page_vsize[Text] ;
95    mfun_page_vstep[FooterSeparator] := mfun_page_vstep[Text]           -mfun_page_vsize[FooterSeparator] ;
96    mfun_page_vstep[Footer]          := mfun_page_vstep[FooterSeparator]-mfun_page_vsize[Footer] ;
97    mfun_page_vstep[BottomSeparator] := mfun_page_vstep[Footer]         -mfun_page_vsize[BottomSeparator] ;
98    mfun_page_vstep[Bottom]          := mfun_page_vstep[BottomSeparator]-mfun_page_vsize[Bottom] ;
99enddef ;
100
101def mfun_page_check_hstep =
102    mfun_page_hstep[Text]                 := BackSpace ;
103    mfun_page_hstep[LeftMarginSeparator]  := mfun_page_hstep[Text]                -mfun_page_hsize[LeftMarginSeparator] ;
104    mfun_page_hstep[RightMarginSeparator] := mfun_page_hstep[Text]                +mfun_page_hsize[Text] ;
105    mfun_page_hstep[LeftMargin]           := mfun_page_hstep[LeftMarginSeparator] -mfun_page_hsize[LeftMargin] ;
106    mfun_page_hstep[RightMargin]          := mfun_page_hstep[RightMarginSeparator]+mfun_page_hsize[RightMarginSeparator] ;
107    mfun_page_hstep[LeftEdgeSeparator]    := mfun_page_hstep[LeftMargin]          -mfun_page_hsize[LeftEdgeSeparator] ;
108    mfun_page_hstep[LeftEdge]             := mfun_page_hstep[LeftEdgeSeparator]   -mfun_page_hsize[LeftEdge] ;
109    mfun_page_hstep[RightEdgeSeparator]   := mfun_page_hstep[RightMargin]         +mfun_page_hsize[RightMargin] ;
110    mfun_page_hstep[RightEdge]            := mfun_page_hstep[RightEdgeSeparator]  +mfun_page_hsize[RightEdgeSeparator] ;
111enddef ;
112
113def mfun_check_page_dimensions =
114    if mfun_page_done <> RealPageNumber :
115        if LayoutHasChanged :
116            mfun_page_check_vsize ;
117            mfun_page_check_hsize ;
118            mfun_page_check_vstep ;
119            mfun_page_check_hstep ;
120        fi ;
121        mfun_page_done := RealPageNumber ;
122    fi ;
123enddef;
124
125
126def mfun_check_page_area =
127    mfun_check_page_dimensions ;
128    for VerPos=Top step 10 until Bottom:
129        for HorPos=LeftEdge step 1 until RightEdge:
130            mfun_page_area[HorPos][VerPos] := unitsquare xscaled mfun_page_hsize[HorPos] yscaled mfun_page_vsize[VerPos] ;
131            mfun_page_area[VerPos][HorPos] := mfun_page_area[HorPos][VerPos] ;
132        endfor ;
133    endfor ;
134enddef ;
135
136def mfun_check_page_location =
137    mfun_check_page_dimensions ;
138    for VerPos=Top step 10 until Bottom:
139        for HorPos=LeftEdge step 1 until RightEdge:
140            mfun_page_location[HorPos][VerPos] := (mfun_page_hstep[HorPos],mfun_page_vstep[VerPos]) ;
141            mfun_page_location[VerPos][HorPos] := mfun_page_location[HorPos][VerPos] ;
142        endfor ;
143    endfor ;
144enddef ;
145
146def mfun_check_page_field =
147    mfun_check_page_dimensions ;
148    for VerPos=Top step 10 until Bottom:
149        for HorPos=LeftEdge step 1 until RightEdge:
150            mfun_page_field[HorPos][VerPos] := unitsquare xscaled mfun_page_hsize[HorPos] yscaled mfun_page_vsize[VerPos] shifted (mfun_page_hstep[HorPos],mfun_page_vstep[VerPos]) ;
151            mfun_page_field[VerPos][HorPos] := mfun_page_field[HorPos][VerPos] ;
152        endfor ;
153    endfor ;
154enddef ;
155
156def Area     = hide(mfun_check_page_area       ;) mfun_page_area     enddef ;
157def Location = hide(mfun_check_page_location   ;) mfun_page_location enddef ;
158def Field    = hide(mfun_check_page_field      ;) mfun_page_field    enddef ;
159def Vsize    = hide(mfun_check_page_dimensions ;) mfun_page_vsize    enddef ;
160def Hsize    = hide(mfun_check_page_dimensions ;) mfun_page_hsize    enddef ;
161def Vstep    = hide(mfun_check_page_dimensions ;) mfun_page_vstep    enddef ;
162def Hstep    = hide(mfun_check_page_dimensions ;) mfun_page_hstep    enddef ;
163
164immutable % permanent
165    Area, Location, Field, Vsize, Hsize, Vstep, Hstep ;
166
167vardef FrontPageWidth  = PaperWidth enddef ;
168vardef BackPageWidth   = PaperWidth enddef ;
169vardef CoverWidth      = 2 * PaperWidth + SpineWidth enddef ;
170vardef CoverHeight     = PaperHeight enddef ;
171
172vardef FrontPageHeight = PaperHeight enddef ;
173vardef BackPageHeight  = PaperHeight enddef ;
174vardef SpineHeight     = PaperHeight enddef ;
175
176path mfun_page_page, mfun_page_cover, mfun_page_spine, mfun_page_back, mfun_page_front ;
177
178def mfun_check_page  = mfun_page_page  := unitsquare xscaled PaperWidth     yscaled PaperHeight ; enddef ;
179def mfun_check_cover = mfun_page_cover := unitsquare xscaled CoverWidth     yscaled CoverHeight ; enddef ;
180def mfun_check_spine = mfun_page_spine := unitsquare xscaled SpineWidth     yscaled CoverHeight shifted (BackPageWidth,0) ; enddef ;
181def mfun_check_back  = mfun_page_back  := unitsquare xscaled BackPageWidth  yscaled CoverHeight ; enddef ;
182def mfun_check_front = mfun_page_front := unitsquare xscaled FrontPageWidth yscaled CoverHeight shifted (BackPageWidth+SpineWidth,0) ; enddef ;
183
184def Page      = hide(mfun_check_page  ;) mfun_page_page  enddef ;
185def CoverPage = hide(mfun_check_cover ;) mfun_page_cover enddef ;
186def Spine     = hide(mfun_check_spine ;) mfun_page_spine enddef ;
187def BackPage  = hide(mfun_check_back  ;) mfun_page_back  enddef ;
188def FrontPage = hide(mfun_check_front ;) mfun_page_front enddef ;
189
190% pages
191
192def StartPage =
193    begingroup ;
194    setbounds currentpicture to Page ;
195enddef ;
196
197def StopPage =
198    setbounds currentpicture to Page ;
199    endgroup ;
200enddef ;
201
202% cover pages
203
204def StartCover =
205    begingroup ;
206    setbounds currentpicture to CoverPage enlarged PaperBleed ;
207enddef ;
208
209def StopCover =
210    setbounds currentpicture to CoverPage enlarged PaperBleed ;
211    endgroup ;
212enddef ;
213
214immutable % permanent
215    FrontPageWidth, BackPageWidth, CoverWidth, FrontPageHeight, BackPageHeight, CoverHeight,
216    SpineHeight, Page, CoverPage, Spine, BackPage, FrontPage,
217    StartPage, StopPage, StartCover, StopCover ;
218
219% overlays:
220
221def OverlayBox =
222    (unitsquare xyscaled (OverlayWidth,OverlayHeight))
223enddef ;
224
225def BoundToOverlayBox =
226    setbounds currentpicture to OverlayBox;
227enddef ;
228
229immutable % permanent
230    OverlayBox ;
231
232permanent
233    BoundToOverlayBox ;
234
235% handy
236
237def innerenlarged =
238    if OnRightPage : leftenlarged  else : rightenlarged fi
239enddef ;
240
241def outerenlarged =
242    if OnRightPage : rightenlarged else : leftenlarged  fi
243enddef ;
244
245permanent
246    innerenlarged, outerenlarged ;
247
248% obsolete
249
250% def llEnlarged (expr p,d) = (llcorner p shifted (-d,-d)) enddef ;
251% def lrEnlarged (expr p,d) = (lrcorner p shifted (+d,-d)) enddef ;
252% def urEnlarged (expr p,d) = (urcorner p shifted (+d,+d)) enddef ;
253% def ulEnlarged (expr p,d) = (ulcorner p shifted (-d,+d)) enddef ;
254
255% def Enlarged (expr p, d) =
256%     (
257%         llEnlarged (p,d) --
258%         lrEnlarged (p,d) --
259%         urEnlarged (p,d) --
260%         ulEnlarged (p,d) --
261%         cycle
262%     )
263% enddef ;
264
265% for the moment we put these here:
266
267string  RuleDirection ; RuleDirection := "" ;
268string  RuleOption    ; RuleOption    := "" ;
269numeric RuleWidth     ; RuleWidth     := 0 ;
270numeric RuleHeight    ; RuleHeight    := 0 ;
271numeric RuleDepth     ; RuleDepth     := 0 ;
272numeric RuleH         ; RuleH         := 0 ;
273numeric RuleV         ; RuleV         := 0 ;
274numeric RuleThickness ; RuleThickness := 0 ;
275numeric RuleFactor    ; RuleFactor    := 0 ;
276numeric RuleOffset    ; RuleOffset    := 0 ;
277                    def RuleColor      = (.5white) enddef ; % yet undecided, might become a string
278
279def FakeWord(expr RuleWidth, RuleHeight, RuleDepth, RuleThickness) (text RuleColor) =
280    fill unitsquare
281        xscaled RuleWidth
282        yscaled (RuleDepth-RuleThickness/2)
283        withcolor RuleColor ;
284    fill unitsquare
285        xscaled RuleWidth
286        yscaled (RuleHeight-RuleDepth-RuleThickness/2)
287        shifted (0,RuleDepth+RuleThickness)
288        withcolor RuleColor ;
289enddef ;
290
291def FakeRule(expr RuleWidth, RuleHeight, RuleDepth, RuleThickness) (text RuleColor) =
292    fill unitsquare
293        xscaled RuleWidth
294        yscaled RuleHeight
295        withcolor RuleColor ;
296enddef ;
297
298mutable
299    RuleDirection, RuleOption, RuleWidth, RuleHeight, RuleDepth, RuleH, RuleV, RuleThickness,
300    RuleFactor, RuleOffset, RuleColor;
301
302permanent
303    FakeWord, FakeRule ;
304