summaryrefslogtreecommitdiff
path: root/Master/texmf-dist/metapost/context/base/mpiv/mp-asnc.mpiv
blob: fba182a64851435c8b061f84f39051b87b6ed406 (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
%D \module
%D   [       file=mp-asnc.mpiv,
%D        version=2012.02.19, % was mp-core: 1999.08.01, anchoring
%D          title=\CONTEXT\ \METAPOST\ graphics,
%D       subtitle=anchored background macros,
%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.

if known context_asnc : endinput ; fi ;

boolean context_asnc ; context_asnc := true ;

% will be replaced

numeric sync_n[], sync_p[][], sync_w[][], sync_h[][], sync_d[][], sync_t[][] ;
pair sync_xy[][] ; color sync_c[][] ;

def ResetSyncTasks =
    path SyncPaths[] ; numeric SyncTasks[], NOfSyncPaths, CurrentSyncClass ;
    NOfSyncPaths := CurrentSyncClass := 0 ;
    if unknown SyncLeftOffset : numeric SyncLeftOffset ; SyncLeftOffset := 0          ; fi ;
    if unknown SyncWidth      : numeric SyncWidth      ; SyncWidth      := 0          ; fi ;
    if unknown SyncThreshold  : numeric SyncThreshold  ; SyncThreshold  := LineHeight ; fi ;
    if unknown SyncColor      : color   SyncColor      ; SyncColor      := .5white    ; fi ;
    if (SyncLeftOffset = 0) and (SyncWidth = 0) :
        SyncWidth := if known TextWidth : TextWidth else : -1cm fi ;
    fi ;
enddef ;

ResetSyncTasks ;

vardef SyncBox(expr n, i, leftoffset, width, topoffset, bottomoffset) =
    save o ; pair o ; o := (xpart llcorner PlainTextArea,ypart sync_xy[n][i]) ;
    o shifted (leftoffset,sync_h[n][i]+topoffset) --
    o shifted (width+leftoffset,sync_h[n][i]+topoffset) --
    o shifted (width+leftoffset,bottomoffset) --
    o shifted (leftoffset,bottomoffset) -- cycle
enddef ;

def SetSyncColor(expr n, i, c) =
    sync_c[n][i] := c ;
enddef ;

def SetSyncThreshold(expr n, i, th) =
    sync_th[n][i] := th ;
enddef ;

vardef TheSyncColor(expr n, i) =
    if known sync_c[n][i] : sync_c[n][i] else : SyncColor fi
enddef ;

vardef TheSyncThreshold(expr n, i) =
    if known sync_th[n][i] : sync_th[n][i] else : SyncThreshold fi
enddef ;

vardef PrepareSyncTasks(expr n, collapse, extendtop, prestartnext) =
    ResetSyncTasks ;
    if known sync_n[n] :
        CurrentSyncClass := n ;
        save ok, l, d ; boolean ok ; ok := false ; NOfSyncPaths := l := 0 ;
        for i=1 upto sync_n[n] :
            if RealPageNumber > sync_p[n][i] :
                l := i ;
            elseif RealPageNumber = sync_p[n][i] :
                NOfSyncPaths := NOfSyncPaths + 1 ;
                if not ok :
                    if i>1 :
                        if sync_t[n][i-1] = sync_t[n][i] :
                            SyncPaths[NOfSyncPaths] := SyncBox(n, i, SyncLeftOffset, SyncWidth, PaperHeight, -PaperHeight) ;
                            SyncTasks[NOfSyncPaths] := i ;
                        else :
                            SyncPaths[NOfSyncPaths] := SyncBox(n, i-1, SyncLeftOffset, SyncWidth, PaperHeight, -PaperHeight) ;
                            SyncTasks[NOfSyncPaths] := i-1 ;
                            NOfSyncPaths := NOfSyncPaths + 1 ;
                            SyncPaths[NOfSyncPaths] := SyncBox(n, i, SyncLeftOffset, SyncWidth, 0, -PaperHeight) ;
                            SyncTasks[NOfSyncPaths] := i ;
                        fi ;
                    else :
                        SyncPaths[NOfSyncPaths] := SyncBox(n, i, SyncLeftOffset, SyncWidth, 0, -PaperHeight) ;
                        SyncTasks[NOfSyncPaths] := i ;
                    fi ;
                else :
                    SyncPaths[NOfSyncPaths] := SyncBox(n, i, SyncLeftOffset, SyncWidth, 0, -PaperHeight) ;
                    SyncTasks[NOfSyncPaths] := i ;
                fi ;
                ok := true ;
            fi ;
        endfor ;
        if (NOfSyncPaths = 0) and (l > 0) :
            NOfSyncPaths := 1 ;
            SyncPaths[NOfSyncPaths] := SyncBox(n, l, SyncLeftOffset, SyncWidth, PaperHeight, -PaperHeight) ;
            SyncTasks[NOfSyncPaths] := l ;
        fi ;
        if NOfSyncPaths > 0 :
            for i = 1 upto NOfSyncPaths-1 :
                SyncPaths[i] := topboundary SyncPaths[i] -- reverse topboundary SyncPaths[i+1] -- cycle ;
            endfor ;
            if unknown SyncThresholdMethod :
                numeric SyncThresholdMethod ; SyncThresholdMethod := 2 ;
            fi ;
            if extendtop :
                if SyncThresholdMethod = 1 :
                    if NOfSyncPaths>1 :
                        d := ypart (ulcorner PlainTextArea - sync_xy[n][SyncTasks[2]]) ;
                        if (SyncTasks[2]>1) and (d > 0pt) and (d <= TheSyncThreshold(n,sync_t[n][SyncTasks[2]])) and (sync_p[n][SyncTasks[2]] = RealPageNumber) :
                            SyncPaths[2] := SyncPaths[2] topenlarged PaperHeight ;
                        fi ;
                    fi ;
                else :
                    for i = 1 upto NOfSyncPaths :
                        d := ypart (ulcorner PlainTextArea - sync_xy[n][SyncTasks[i]]) ;
                        if (d > 0) and (d <= TheSyncThreshold(n,sync_t[n][SyncTasks[i]])) and (sync_p[n][SyncTasks[i]] = RealPageNumber) :
                            SyncPaths[i] := SyncPaths[i] topenlarged PaperHeight ;
                        fi ;
                    endfor ;
                fi ;
            fi ;
            if prestartnext :
                if NOfSyncPaths>1 :
                    if SyncTasks[NOfSyncPaths] < sync_n[n] : % there is a next one
                        d := ypart (ulcorner PlainTextArea - sync_xy[n][SyncTasks[NOfSyncPaths]+1]) ;
                        if (d > 0) and (d <= TheSyncThreshold(n, sync_t[n][SyncTasks[i]])) and (sync_p[n][SyncTasks[NOfSyncPaths]+1] = RealPageNumber+1) :
                            SyncPaths[NOfSyncPaths+1] :=
                                (xpart ulcorner SyncPaths[NOfSyncPaths],ypart llcorner PlainTextArea) --
                                (xpart urcorner SyncPaths[NOfSyncPaths],ypart llcorner PlainTextArea) --
                                lrcorner SyncPaths[NOfSyncPaths] --
                                llcorner SyncPaths[NOfSyncPaths] -- cycle ;
                            SyncTasks[NOfSyncPaths+1] := SyncTasks[NOfSyncPaths]+1 ;
                            NOfSyncPaths := NOfSyncPaths + 1 ;
                        fi ;
                    fi ;
                fi ;
            else :
                if NOfSyncPaths>1 :
                    d := ypart (sync_xy[n][SyncTasks[NOfSyncPaths]] - llcorner PlainTextArea) ;
                    if (d < TheSyncThreshold(n, SyncTasks[NOfSyncPaths])) :
                        NOfSyncPaths := NOfSyncPaths - 1 ;
                        SyncPaths[NOfSyncPaths] := SyncPaths[NOfSyncPaths] bottomenlarged PaperHeight ;
                    fi ;
                fi ;
            fi ;
            if (NOfSyncPaths>1) and collapse :
                save j ; numeric j ; j := 1 ;
                for i = 2 upto NOfSyncPaths :
                    if sync_t[n][SyncTasks[i]] = sync_t[n][SyncTasks[j]] :
                        SyncPaths[j] := boundingbox image (draw SyncPaths[i] ; draw SyncPaths[j] ; ) ;
                        SyncTasks[j] := SyncTasks[i] ;
                    else :
                        j := j + 1 ;
                        SyncPaths[j] := SyncPaths[i] ;
                        SyncTasks[j] := SyncTasks[i] ;
                    fi ;
                endfor ;
                NOfSyncPaths := j ;
            fi ;
        fi ;
    fi ;
enddef ;

def SyncTask(expr n) =
    if known SyncTasks[n] : SyncTasks[n] else : 0 fi
enddef ;

def FlushSyncTasks =
    for i = 1 upto NOfSyncPaths :
        ProcessSyncTask(SyncPaths[i], TheSyncColor(CurrentSyncClass,sync_t[CurrentSyncClass][SyncTasks[i]])) ;
    endfor ;
enddef ;

def ProcessSyncTask(expr p, c) =
    fill p withcolor c ;
enddef ;