summaryrefslogtreecommitdiff
path: root/metapost/context/base/mp-page.mp
blob: a4f0a86ef06b4a48ae4764dadc6a2df0ce5fd2d6 (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
%D \module
%D   [       file=mp-page.mp,
%D        version=1999.03.10,
%D          title=\CONTEXT\ \METAPOST\ graphics,
%D       subtitle=page enhancements,
%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.

%D This module is rather preliminary and subjected to
%D changes.

if unknown context_tool : input mp-tool ; fi ;
if   known context_page : endinput      ; fi ;

boolean context_page ; context_page := true ;

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 ;

def SaveTextAreas =
  path    SavedTextAreas  []  ;
  path    SavedTextColumns[]  ;
  numeric NOfSavedTextAreas   ;
  numeric NOfSavedTextColumns ;
  for i=1 upto NOfTextAreas :
    SavedTextAreas[i] := TextAreas[i] ;
  endfor ;
  for i=1 upto NOfTextColumns :
    SavedTextColumns[i] := TextColumns[i] ;
  endfor ;
  NOfSavedTextAreas   := NOfTextAreas ;
  NOfSavedTextColumns := NOfTextColumns ;
enddef ;

def ResetTextAreas =
  path TextAreas[], TextColumns[] ;
  numeric NOfTextAreas   ; NOfTextAreas   := 0 ;
  numeric NOfTextColumns ; NOfTextColumns := 0 ;
  numeric nofmultipars   ; nofmultipars   := 0 ;
  TextAreas[0] := TextColumns[0] := origin -- cycle ;
enddef ;

ResetTextAreas ; SaveTextAreas ; ;

def RegisterTextArea (expr x, y, w, h, d) =
  begingroup ; save p ; path p ;
    p := unitsquare xyscaled(w,h+d) shifted (x,y-d) ;
    if NOfTextAreas>0 :
      % if needed, concatenate areas
      if (round(llcorner TextAreas[NOfTextAreas]) = round(ulcorner p)) and
         (round(lrcorner TextAreas[NOfTextAreas]) = round(urcorner p)) :
        p := ulcorner TextAreas[NOfTextAreas] --
             urcorner TextAreas[NOfTextAreas] --
             lrcorner p --
             llcorner p --
             cycle ;
      else :
        NOfTextAreas := NOfTextAreas + 1 ;
      fi ;
    else :
      NOfTextAreas := NOfTextAreas + 1 ;
    fi ;
    TextAreas[NOfTextAreas] := p ;
    if NOfTextColumns>0 :
      if (round(xpart llcorner TextColumns[NOfTextColumns]) = round(xpart ulcorner p)) and
         (round(xpart lrcorner TextColumns[NOfTextColumns]) = round(xpart urcorner p)) :
        p := ulcorner TextColumns[NOfTextColumns] --
             urcorner TextColumns[NOfTextColumns] --
             lrcorner p --
             llcorner p --
             cycle ;
      else :
        NOfTextColumns := NOfTextColumns + 1 ;
      fi ;
    else :
      NOfTextColumns := NOfTextColumns + 1 ;
    fi ;
    TextColumns[NOfTextColumns] := p ;
  endgroup ;
enddef ;

%D We store a local area in slot zero.

def RegisterLocalTextArea (expr x, y, w, h, d) =
  TextAreas[0] := TextColumns[0] := unitsquare xyscaled(w,h+d) shifted (x,y-d) ;
enddef ;

def ResetLocalTextArea =
  TextAreas[0] := TextColumns[0] := origin -- cycle ;
enddef ;

ResetLocalTextArea ;

vardef InsideTextArea (expr _i_, _xy_) =
  ( (round(xpart _xy_) >= round(xpart llcorner TextAreas[_i_])) and
    (round(xpart _xy_) <= round(xpart lrcorner TextAreas[_i_])) and
    (round(ypart _xy_) >= round(ypart llcorner TextAreas[_i_])) and
    (round(ypart _xy_) <= round(ypart urcorner TextAreas[_i_])) )
enddef ;

vardef InsideSavedTextArea (expr _i_, _xy_) =
  ( (round(xpart _xy_) >= round(xpart llcorner SavedTextAreas[_i_])) and
    (round(xpart _xy_) <= round(xpart lrcorner SavedTextAreas[_i_])) and
    (round(ypart _xy_) >= round(ypart llcorner SavedTextAreas[_i_])) and
    (round(ypart _xy_) <= round(ypart urcorner SavedTextAreas[_i_])) )
enddef ;

vardef InsideSomeTextArea (expr _xy_) =
  save ok ; boolean ok ; ok := false ;
  for i := 1 upto NOfTextAreas :
    if InsideTextArea(i,_xy_) : ok := true ; fi ;
    exitif ok ;
  endfor ;
  ok
enddef ;

vardef InsideSomeSavedTextArea (expr _xy_) =
  save ok ; boolean ok ; ok := false ;
  for i := 1 upto NOfSavedTextAreas :
    if InsideSavedTextArea(i,_xy_) : ok := true ; fi ;
    exitif ok ;
  endfor ;
  ok
enddef ;

vardef TextAreaX (expr x) =
  numeric _TextAreaX_ ; _TextAreaX_ := 0 ;
  for i := 1 upto NOfTextAreas :
    if (round(x) >= round(xpart llcorner TextAreas[i])) and
       (round(x) <= round(xpart lrcorner TextAreas[i])) :
      _TextAreaX_ := xpart llcorner TextAreas[i] ;
    fi ;
  endfor ;
  _TextAreaX_
enddef ;

vardef TextAreaY (expr y) =
  numeric _TextAreaY_ ; _TextAreaY_ := 0 ;
  for i := 1 upto NOfTextAreas :
    if (round(y) >= round(ypart llcorner TextAreas[NOfTextAreas])) and
       (round(y) <= round(ypart ulcorner TextAreas[NOfTextAreas])) :
      _TextAreaY_ := ypart llcorner TextAreas[NOfTextAreas] ;
    fi ;
  endfor ;
  _TextAreaY_
enddef ;

vardef TextAreaXY (expr x, y) =
  pair _TextAreaXY_ ; _TextAreaXY_ := origin ;
  for i := 1 upto NOfTextAreas :
    if (round(x) >= round(xpart llcorner TextAreas[i])) and
       (round(x) <= round(xpart lrcorner TextAreas[i])) and
       (round(y) >= round(ypart llcorner TextAreas[i])) and
       (round(y) <= round(ypart ulcorner TextAreas[i])) :
      _TextAreaXY_ := llconer TextAreas[i] ;
    fi ;
  endfor ;
  _TextAreaXY_
enddef ;

vardef TextAreaW (expr x) =
  numeric _TextAreaW_ ; _TextAreaW_ := 0 ;
  for i := 1 upto NOfTextAreas :
    if (round(x) >= round(xpart llcorner TextAreas[i])) and
       (round(x) <= round(xpart lrcorner TextAreas[i])) :
      _TextAreaW_ := bbwidth(TextAreas[i]) ;
    fi ;
  endfor ;
  _TextAreaW_
enddef ;

vardef TextAreaH (expr y) =
  numeric _TextAreaH_ ; _TextAreaH_ := 0 ;
  for i := 1 upto NOfTextAreas :
    if (round(y) >= round(ypart llcorner TextAreas[i])) and
       (round(y) <= round(ypart ulcorner TextAreas[i])) :
      _TextAreaH_ := bbheight(TextAreas[i]) ;
    fi ;
  endfor ;
  _TextAreaH_
enddef ;

vardef TextAreaWH (expr x, y) =
  pair _TextAreaWH_ ; _TextAreaWH_ := origin ;
  for i := 1 upto NOfTextAreas :
    if (round(x) >= round(xpart llcorner TextAreas[i])) and
       (round(x) <= round(xpart lrcorner TextAreas[i])) and
       (round(y) >= round(ypart llcorner TextAreas[i])) and
       (round(y) <= round(ypart ulcorner TextAreas[i])) :
      _TextAreaWH_ := (bbwidth(TextAreas[i]),bbheight(TextAreas[i])) ;
    fi ;
  endfor ;
  _TextAreaWH_
enddef ;

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 [][] ;  path Page ;
numeric HorPos ; numeric Hstep [] ; numeric Hsize [] ;
numeric VerPos ; 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 LoadPageState =
%   scantokens "input mp-state.tmp" ;
% enddef ;

def SwapPageState =
  if not OnRightPage :
           BackSpace := PaperWidth-MakeupWidth-BackSpace ;
            CutSpace := PaperWidth-MakeupWidth-CutSpace ;
                   i := LeftMarginWidth ;
     LeftMarginWidth := RightMarginWidth ;
    RightMarginWidth := i ;
                   i := LeftMarginDistance ;
  LeftMarginDistance := RightMarginDistance ;
 RightMarginDistance := i ;
                   i := LeftEdgeWidth ;
       LeftEdgeWidth := RightEdgeWidth ;
      RightEdgeWidth := i ;
                   i := LeftEdgeDistance ;
    LeftEdgeDistance := RightEdgeDistance ;
   RightEdgeDistance := i ;

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

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 =

  if PageStateAvailable :
    LoadPageState ;
    SwapPageState ;
  fi ;

  SetPageAreas ;
  BoundPageAreas ;

enddef ;

def StopPage =

  BoundPageAreas ;

enddef ;

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 ;

% New:

def position_anchor_bar(expr p_b_self, p_e_self, y_b_self, y_e_self, h_b_self, d_e_self,
        distance, linewidth, linecolor) =
    StartPage ;
      path p ; p :=
      if p_b_self=p_e_self :
        (xpart ulcorner Field[Text][Text],y_b_self+h_b_self) --
        (xpart llcorner Field[Text][Text],y_e_self-d_e_self) ;
      elseif RealPageNumber=p_b_self :
        (xpart ulcorner Field[Text][Text],y_b_self+h_b_self) --
        (llcorner Field[Text][Text]) ;
      elseif RealPageNumber=p_e_self :
        (ulcorner Field[Text][Text]) --
        (xpart llcorner Field[Text][Text],y_e_self-d_e_self) ;
      else :
        (ulcorner Field[Text][Text]) --
        (llcorner Field[Text][Text]) ;
      fi ;
      p := p shifted (-llcorner Field[Text][Text]-(distance,0)) ;
      interim linecap := butt ;
      draw p
        withpen pencircle scaled linewidth
        withcolor linecolor ;
    StopPage ;
enddef ;

% Crop stuff

vardef crop_marks_lines (expr box, length, offset) =
    save p ; picture p ;
    p := image (
        draw ((llcorner box) -- (llcorner box) shifted (-length,0)) shifted (-offset,0) ;
        draw ((llcorner box) -- (llcorner box) shifted (0,-length)) shifted (0,-offset) ;
        draw ((lrcorner box) -- (lrcorner box) shifted ( length,0)) shifted ( offset,0) ;
        draw ((lrcorner box) -- (lrcorner box) shifted (0,-length)) shifted (0,-offset) ;
        draw ((ulcorner box) -- (ulcorner box) shifted (-length,0)) shifted (-offset,0) ;
        draw ((ulcorner box) -- (ulcorner box) shifted (0, length)) shifted (0, offset) ;
        draw ((urcorner box) -- (urcorner box) shifted ( length,0)) shifted ( offset,0) ;
        draw ((urcorner box) -- (urcorner box) shifted (0, length)) shifted (0, offset) ;
    ) ;
    setbounds p to box ;
    p
enddef ;

vardef crop_marks_cmyk =
    save p ; picture p ; p := image (
        fill ulcircle   scaled 12.5 withcolor (1,0,0,0) ;
        fill urcircle   scaled 12.5 withcolor (0,1,0,0) ;
        fill lrcircle   scaled 12.5 withcolor (0,0,1,0) ;
        fill llcircle   scaled 12.5 withcolor (0,0,0,1) ;
        draw (-10,0) -- (10,0) ;
        draw (0,-10) -- (0,10) ;
        draw fullcircle scaled 12.5 ;
    ) ;
    setbounds p to fullsquare scaled 20 ;
    p
enddef ;

vardef crop_marks_gray =
    save p ; picture p ; p := image (
        fill ulcircle   scaled 12.5 withcolor (0.00) ;
        fill urcircle   scaled 12.5 withcolor (0.25) ;
        fill lrcircle   scaled 12.5 withcolor (0.50) ;
        fill llcircle   scaled 12.5 withcolor (0.75) ;
        draw (-10,0) -- (10,0) ;
        draw (0,-10) -- (0,10) ;
        draw (-6,0) -- (6,0) withcolor white ;
        draw (0,-6) -- (0,6) withcolor white  ;
        draw fullcircle scaled 12.5 ;
    ) ;
    setbounds p to fullsquare scaled 20 ;
    p
enddef ;

vardef crop_marks_cmykrgb =
    save p ; picture p ; p := image (
        fill ulcircle   scaled 15 withcolor (1,0,0) ;
        fill urcircle   scaled 15 withcolor (0,1,0) ;
        fill lrcircle   scaled 15 withcolor (0,0,1) ;
        fill llcircle   scaled 15 withcolor (.5,.5,.5) ;
        fill ulcircle   scaled 10 withcolor (1,0,0,0) ;
        fill urcircle   scaled 10 withcolor (0,1,0,0) ;
        fill lrcircle   scaled 10 withcolor (0,0,1,0) ;
        fill llcircle   scaled 10 withcolor (0,0,0,1) ;
        draw (-10,0) -- (10,0) ;
        draw (0,-10) -- (0,10) ;
        draw fullcircle scaled 10 ;
        draw fullcircle scaled 15 ;
    ) ;
    setbounds p to fullsquare scaled 20 ;
    p
enddef ;

vardef crop_color(expr c, h, w, dx, dy, ts) =
    image (
        save p ; path p ;
        for i=1 upto 6 :
            p := fullsquare
                xscaled w
                yscaled h
                shifted (dx,dy-i*h) ;
            fill p
                withcolor (crop_colors[i]*c) ;
            draw textext("\format{'@0.2f'," & decimal crop_colors[i] & "}")
                scaled ts
                shifted center p withcolor white ;
        endfor ;
    )
enddef ;

vardef crop_gray(expr c, h, w, dx, dy, ts) =
    image (
        save p ; path p ;
        for i=.05 step .05 until 1 :
                p := fullsquare
                xscaled w
                yscaled h
                shifted (20*(i-1)*w+dx,dy) ;
            fill p
                withcolor (i*c) ;
            draw textext("\format{'@0.2f'," & decimal i & "}")
                scaled ts
                shifted center p withcolor white ;
        endfor ;
    )
enddef ;

% draw crop_marks_cmyk shifted llcorner more ;
% draw crop_marks_cmyk shifted lrcorner more ;
% draw crop_marks_cmyk shifted ulcorner more ;
% draw crop_marks_cmyk shifted urcorner more ;

def page_marks_add_color(expr width, height, length, offset) = % todo: namespace

    path page ; page := fullsquare xscaled width yscaled height ;
    path more ; more := page enlarged (offset+length/2,offset+length/2) ;

    numeric crop_colors[] ;
    crop_colors[1] := 1 ;
    crop_colors[2] := 0.95 ;
    crop_colors[3] := 0.75 ;
    crop_colors[4] := 0.50 ;
    crop_colors[5] := 0.25 ;
    crop_colors[6] := 0.05 ;

    numeric h ; h := height / 20 ;
    numeric w ; w := width  / 20 ;
    numeric d ; d := offset + length/2 ;

    draw crop_color((1,0,0,0),h,length,xpart ulcorner page - d, 10h,length/20) ;
    draw crop_color((0,1,0,0),h,length,xpart ulcorner page - d,3.5h,length/20) ;
    draw crop_color((0,0,1,0),h,length,xpart ulcorner page - d, -3h,length/20) ;

    draw crop_color((0,1,1,0),h,length,xpart urcorner page + d, 10h,length/20) ;
    draw crop_color((1,0,1,0),h,length,xpart urcorner page + d,3.5h,length/20) ;
    draw crop_color((1,1,0,0),h,length,xpart urcorner page + d, -3h,length/20) ;

    draw crop_gray((0,0,0,1),length,  w,-xpart llcorner page,-ypart llcorner page + d            ,w/20) ;
    draw crop_gray((1,0,0,0),length/3,w,-xpart llcorner page, ypart llcorner page - d + 1length/3,w/20) ;
    draw crop_gray((0,1,0,0),length/3,w,-xpart llcorner page, ypart llcorner page - d - 0length/3,w/20) ;
    draw crop_gray((0,0,1,0),length/3,w,-xpart llcorner page, ypart llcorner page - d - 1length/3,w/20) ;

    setbounds currentpicture to page ;

enddef ;

def page_marks_add_marking(expr width, height, length, offset) = % todo: namespace

    path page ; page := fullsquare xscaled width yscaled height ;
    path more ; more := page enlarged (offset+length/2,offset+length/2) ;

    draw crop_marks_gray shifted center(topboundary    more) shifted (0, offset+length);
    draw crop_marks_cmyk shifted center(bottomboundary more) shifted (0,-offset-length);

    setbounds currentpicture to page ;

enddef ;

def page_marks_add_lines(expr width, height, length, offset) = % todo: namespace

    path page ; page := fullsquare xscaled width yscaled height ;
    path more ; more := page enlarged (offset+length/2,offset+length/2) ;

    draw crop_marks_lines(page,length,offset) ;

    setbounds currentpicture to page ;

enddef ;

def page_marks_add_number(expr width, height, length, offset, n) = % todo: namespace

    path page ; page := fullsquare xscaled width yscaled height ;
    path more ; more := page enlarged (offset+length/2,offset+length/2) ;

    for s=llcorner more, lrcorner more, ulcorner more, urcorner more :
        draw textext(decimal n) shifted s ;
    endfor ;

    setbounds currentpicture to page ;

enddef ;

endinput ;