summaryrefslogtreecommitdiff
path: root/metapost/context/base/mpiv/mp-asnc.mpiv
blob: 2eb4091242eff061e0990e53c7fd91df7b02a00a (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
%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 ;

%D This is a rather old mechanism that we once needed in an actually nice
%D design. Those were the times that processing a rather complex xml file
%D with pdftex  into a 400 page document took 45 minutes (a few runs) while
%D nowadays with luatex and mkiv we express runtime in seconds.
%D
%D The logic is mostly the same but some work is delegated to Lua so that
%D we save memory and also run faster. As this was not really a used module
%D the interface also was upgraded. As we know the bottlenecks in mp we also
%D work around it a bit.

numeric mfun_sync_count ;
numeric mfun_sync_page ;

vardef StartSync(expr n) =
    numeric CurrentSyncClass ; CurrentSyncClass := n ;
    numeric SyncHOffset      ; SyncHOffset      := 0 ;
    numeric SyncVOffset      ; SyncVOffset      := 0 ;
    numeric SyncWidth        ; SyncWidth        := 0 ;
    path    SyncPaths[] ;
    numeric SyncTasks[] ;
    numeric SyncKinds[] ;
    %
    mfun_sync_page   := RealPageNumber ;
    mfun_sync_count  := 0 ;
enddef ;

def StopSync =
    % maybe some cleanup
enddef ;

vardef CollectSyncDataPage =
    mfun_sync_count := lua.mp.sync_collect(CurrentSyncClass,mfun_sync_page) ;
enddef ;

vardef CollectSyncDataRegion(expr region) =
    mfun_sync_count := lua.mp.sync_collect(CurrentSyncClass,mfun_sync_page,region) ;
enddef ;

vardef MakeSyncPaths =
    if mfun_sync_count > 0 :
        save k, t, b ;
        save l ; l := SyncHOffset ;
        save r ; r := SyncHOffset +  SyncWidth ;
        save y ; y := lua.mp.sync_get_y() + SyncVOffset ;
        for i=1 upto mfun_sync_count :
            k := lua.mp.sync_get_kind(i) ;
            t := lua.mp.sync_get_top(i)    - y ;
            b := lua.mp.sync_get_bottom(i) - y ;
            SyncPaths[i] = ((l,t) -- (r,t) -- (r,b) -- (l,b) -- cycle) ;
            SyncTasks[i] = lua.mp.sync_get_task(i) ;
            SyncKinds[i] = k ;
        endfor ;
    fi ;
enddef ;

% Extend to the top of the text area.

vardef ExtendSyncPaths =
    mfun_sync_count := lua.mp.sync_extend() ;
enddef ;

% Clip to the text area.

vardef PruneSyncPaths =
    mfun_sync_count := lua.mp.sync_prune() ;
enddef ;

% Remove duplicate tasks

vardef CollapseSyncPaths =
    mfun_sync_count := lua.mp.sync_collapse() ;
enddef ;

def SetSyncColor(expr n, i, c) =
    lua.mp.sync_set_color(n,i,c) ;
enddef ;

vardef TheSyncColor(expr n, i) =
    lua.mp.sync_get_color(n,i)
enddef ;

vardef SyncPathColor(expr i) =
    lua.mp.sync_get_color(CurrentSyncClass,SyncTasks[i])
enddef ;

def DrawSyncPaths =
    for i=1 upto NOfSyncPaths :
        draw SyncPaths[i] withcolor SyncPathColor(i) ;
    endfor ;
enddef ;

def FillSyncPaths =
    for i=1 upto NOfSyncPaths :
        fill SyncPaths[i] withcolor SyncPathColor(i) ;
    endfor ;
enddef ;

vardef NOfSyncPaths =
    mfun_sync_count
enddef ;