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
|
%D \module
%D [ file=mp-grph.mpiv,
%D version=2000.12.14,
%D title=\CONTEXT\ \METAPOST\ graphics,
%D subtitle=graphic text support,
%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 licen-en.pdf for
%C details.
%D Under construction.
if known context_grph : endinput ; fi ;
boolean context_grph ; context_grph := true ;
picture _currentpicture_ ;
numeric _fig_nesting_ ; _fig_nesting_ := 0 ;
def beginfig (expr c) =
_fig_nesting_ := _fig_nesting_ + 1 ;
if _fig_nesting_ = 1 :
begingroup
charcode := c ;
resetfig ;
scantokens extra_beginfig ;
fi ;
enddef ;
def endfig =
; % safeguard
if _fig_nesting_ = 1 :
scantokens extra_endfig ;
shipit ;
endgroup ;
fi ;
_fig_nesting_ := _fig_nesting_ - 1 ;
enddef;
def resetfig =
clearxy ;
clearit ;
clearpen ;
pickup defaultpen ;
interim linecap := linecap ;
interim linejoin := linejoin ;
interim miterlimit := miterlimit ;
save _background_ ; color _background_ ; _background_ := background ;
save background ; color background ; background := _background_ ;
drawoptions () ;
enddef ;
def protectgraphicmacros =
save showtext ;
save beginfig ; let beginfig = begingraphictextfig ;
save endfig ; let endfig = endgraphictextfig ;
save end ; let end = relax ;
interim prologues := prologues ;
resetfig ; % resets currentpicture
enddef ;
numeric currentgraphictext ; currentgraphictext := 0 ;
def data_mpy_file = job_name & "-mpgraph.mpy" enddef ;
def begingraphictextfig (expr n) =
foundpicture := n ;
scratchpicture := nullpicture ;
enddef ;
def endgraphictextfig =
if foundpicture = currentgraphictext :
expandafter endinput
else :
scratchpicture := nullpicture ;
fi ;
enddef ;
def loadfigure primary filename =
mfun_load_figure(filename)
enddef ;
def mfun_load_figure (expr filename) text figureattributes =
begingroup ;
save figurenumber, figurepicture, number, fixedplace ;
numeric figurenumber ; figurenumber := 0 ;
boolean figureshift ; figureshift := true ;
picture figurepicture ; figurepicture := currentpicture ;
def number primary n = hide(figurenumber := n) enddef ;
def fixedplace = hide(figureshift := false) enddef ;
protectgraphicmacros ;
% defaults
interim linecap := rounded ;
interim linejoin := rounded ;
interim miterlimit := 10 ;
%
currentpicture := nullpicture ;
draw fullcircle figureattributes ; % expand number
currentpicture := nullpicture ;
def beginfig (expr n) =
currentpicture := nullpicture ;
if (figurenumber=n) or (figurenumber=0) :
let endfig = endinput ;
fi ;
enddef ;
let endfig = relax ;
readfile(filename) ;
if figureshift :
currentpicture := currentpicture shifted -llcorner currentpicture ;
fi ;
addto figurepicture also currentpicture figureattributes ;
currentpicture := figurepicture ;
endgroup ;
enddef ;
% shared between old and new
boolean mfun_gt_color_fill ;
boolean mfun_gt_color_draw ;
boolean mfun_gt_shade_fill ;
boolean mfun_gt_reverse_fill ;
boolean mfun_gt_outline_fill ;
picture mfun_gt_picture ;
% this is the old version:
def old_graphictext primary t =
hide (
if mfun_trial_run :
let mfun_graphic_text = mfun_no_graphic_text ;
else :
let mfun_graphic_text = mfun_do_graphic_text ;
fi
)
mfun_graphic_text(t)
enddef ;
def mfun_do_graphic_text (expr t) =
% withprescript "gt_stage=final"
begingroup ;
save figurepicture ; picture figurepicture ;
figurepicture := currentpicture ; currentpicture := nullpicture ;
currentgraphictext := currentgraphictext + 1 ;
mfun_finish_graphic_text % picks up directives
enddef ;
def mfun_no_graphic_text (expr t) text rest =
currentgraphictext := currentgraphictext + 1 ;
draw unitsquare
withprescript "gt_stage=trial"
withprescript "gt_index=" & decimal currentgraphictext
withpostscript t
enddef ;
def mfun_finish_graphic_text text rest =
protectgraphicmacros ; % resets currentpicture
interim linecap := butt ; % normally rounded
interim linejoin := mitered ; % normally rounded
interim miterlimit := 10 ; % todo
let normalwithshade = withshade ;
save foundpicture, scratchpicture, str ;
save fill, draw, withshade, reversefill, outlinefill ;
save withfillcolor, withdrawcolor ; % quite important
numeric foundpicture ; picture scratchpicture ; string str ;
def draw expr p =
% the first, naive implementation was:
% addto scratchpicture doublepath p withpen currentpen ;
% but it is better to turn lines into fills
addto scratchpicture contour boundingbox
image (addto currentpicture doublepath p withpen currentpen) ;
enddef ;
def fill expr p =
addto scratchpicture contour p withpen currentpen ;
enddef ;
def mfun_gt_fill = enddef ; boolean mfun_gt_color_fill ; mfun_gt_color_fill := false ;
def mfun_gt_draw = enddef ; boolean mfun_gt_color_draw ; mfun_gt_color_draw := false ;
def mfun_gt_shade = enddef ; boolean mfun_gt_shade_fill ; mfun_gt_shade_fill := false ;
boolean mfun_gt_reverse_fill ; mfun_gt_reverse_fill := false ;
boolean mfun_gt_outline_fill ; mfun_gt_outline_fill := false ;
def reversefill =
hide(mfun_gt_reverse_fill := true )
enddef ;
def outlinefill =
hide(mfun_gt_outline_fill := true )
enddef ;
def withshade primary c =
hide(def mfun_gt_shade = normalwithshade c enddef ; mfun_gt_shade_fill := true )
enddef ;
def withfillcolor primary c =
hide(def mfun_gt_fill = withcolor c enddef ; mfun_gt_color_fill := true )
enddef ;
def withdrawcolor primary c =
hide(def mfun_gt_draw = withcolor c enddef ; mfun_gt_color_draw := true )
enddef ;
scratchpicture := nullpicture ;
addto scratchpicture doublepath origin rest ; % pre-roll
for i within scratchpicture : % Below here is a dirty tricky test!
if (urcorner dashpart i) = origin :
mfun_gt_outline_fill := false ;
fi ;
endfor ;
scratchpicture := nullpicture ;
readfile(data_mpy_file) ;
scratchpicture := (scratchpicture shifted -llcorner scratchpicture) scaled (1/10) ;
if not mfun_gt_color_draw and not mfun_gt_color_fill :
mfun_gt_color_draw := true ;
fi
if mfun_gt_shade_fill :
mfun_gt_color_draw := false ;
mfun_gt_color_fill := false ;
fi ;
currentpicture := figurepicture ;
if mfun_gt_shade_fill :
for i within scratchpicture :
if filled i :
addto currentpicture contour pathpart i _op_ rest mfun_gt_shade ;
fi ;
endfor ;
else :
if mfun_gt_color_draw and not mfun_gt_reverse_fill :
for i within scratchpicture :
if mfun_gt_color_fill and mfun_gt_outline_fill :
addto currentpicture doublepath pathpart i _op_ rest mfun_gt_fill dashed nullpicture ;
fi ;
if filled i :
addto currentpicture doublepath pathpart i _op_ rest mfun_gt_draw ;
fi ;
endfor ;
fi ;
if mfun_gt_color_fill :
for i within scratchpicture :
if filled i :
addto currentpicture contour pathpart i _op_ rest mfun_gt_fill withpen pencircle scaled 0 ;
fi ;
endfor ;
fi ;
if mfun_gt_color_draw and mfun_gt_reverse_fill :
for i within scratchpicture :
if filled i :
addto currentpicture doublepath pathpart i _op_ rest mfun_gt_draw ;
fi ;
endfor ;
fi ;
for i within scratchpicture :
if stroked i :
addto currentpicture doublepath pathpart i _op_ rest mfun_gt_draw ;
fi ;
endfor ;
fi ;
endgroup ;
enddef ;
% and this is the new one:
% boolean mfun_gt_color_fill ;
% boolean mfun_gt_color_draw ;
% boolean mfun_gt_shade_fill ;
% boolean mfun_gt_reverse_fill ;
% picture mfun_gt_picture ;
def mfun_gt_default = % somewhat compatible
scaled 11.5
withpen pencircle scaled .1
enddef ;
def new_graphictext primary t = % use outlinetext instead
begingroup ;
mfun_graphic_text_indeed(t)
enddef ;
def mfun_graphic_text_indeed(expr t) text rest =
interim linecap := butt ; % normally rounded
interim linejoin := mitered ; % normally rounded
% interim miterlimit := 10 ; % todo
%
let normalwithshade = withshade ;
%
save reversefill, outlinefill, withshade, withfillcolor, withdrawcolor ;
%
def mfun_gt_fill = enddef ;
def mfun_gt_draw = enddef ;
def mfun_gt_shade = enddef ;
%
mfun_gt_color_fill := false ;
mfun_gt_color_draw := false ;
mfun_gt_shade_fill := false ;
mfun_gt_reverse_fill := false ;
%
def reversefill = hide(mfun_gt_reverse_fill := true) enddef ;
def outlinefill = enddef ;
def withshade primary c = hide(mfun_gt_shade_fill := true; def mfun_gt_shade = normalwithshade c enddef ;) enddef ;
def withfillcolor primary c = hide(mfun_gt_color_fill := true; def mfun_gt_fill = withcolor c enddef ;) enddef ;
def withdrawcolor primary c = hide(mfun_gt_color_draw := true; def mfun_gt_draw = withcolor c enddef ;) enddef ;
%
mfun_gt_picture := nullpicture ;
addto mfun_gt_picture doublepath origin rest ; % preroll
mfun_gt_picture := nullpicture ;
%
def reversefill = enddef ;
def outlinefill = enddef ;
def withshade primary c = enddef ;
def withfillcolor primary c = enddef ;
def withdrawcolor primary c = enddef ;
%
if mfun_gt_shade_fill :
draw outlinetext.f(t)(mfun_gt_shade) rest;
elseif mfun_gt_color_fill and mfun_gt_color_draw :
if mfun_gt_reverse_fill :
draw outlinetext.r(t)(mfun_gt_default mfun_gt_fill rest)(mfun_gt_default mfun_gt_draw rest) ;
else :
draw outlinetext.b(t)(mfun_gt_default mfun_gt_draw rest)(mfun_gt_default mfun_gt_fill rest);
fi ;
elseif mfun_gt_color_fill :
draw outlinetext.f(t)(mfun_gt_default mfun_gt_fill rest) ;
elseif mfun_gt_color_draw :
draw outlinetext.d(t)(mfun_gt_default mfun_gt_draw rest) ;
else :
draw outlinetext.d(t)(mfun_gt_default rest) ;
fi ;
%
endgroup ;
enddef ;
let graphictext = old_graphictext ;
%%% graphictext = new_graphictext ; % more than 10 times faster
% example
%
% beginfig (1) ;
% graphictext
% "\vbox{\hsize10cm \input tufte }"
% scaled 8
% withdrawcolor blue
% withfillcolor red
% withpen pencircle scaled 2pt ;
% endfig ;
%
% beginfig(1) ;
% loadfigure "gracht.mp" rotated 20 ;
% loadfigure "koe.mp" number 1 scaled 2 ;
% endfig ;
%
% end
|