summaryrefslogtreecommitdiff
path: root/metapost/context/base/mpiv/mp-lmtx.mpxl
blob: 42aa54f9ff3c1e912aabffa08f88ffc585dc868b (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
%D \module
%D   [       file=mp-luas.lmtx,
%D        version=2019.06.23,
%D          title=\CONTEXT\ \METAPOST\ graphics,
%D       subtitle=\LUA,
%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.

% This is an experimental module where I test some new interface methods;
% for real advanced graphics use the luapost module.

if known context_lmtx : endinput ; fi ;

boolean context_lmtx ; context_lmtx := true ;

presetparameters "grid" [
    nx = 1, dx = 1,
    ny = 1, dy = 1,
] ;

def lmt_grid = applyparameters "grid" "lmt_do_grid" enddef ;

vardef lmt_do_grid =
    image (
        save nx; nx := getparameter "grid" "nx" ;
        save ny; ny := getparameter "grid" "ny" ;
        save dx; dx := getparameter "grid" "dx" ;
        save dy; dy := getparameter "grid" "dy" ;
        for i = 0 step dx until nx :
            draw ((0,0) -- (0,ny)) shifted (i,0) ;
        endfor ;
        for i = 0 step dy until ny :
            draw ((0,0) -- (nx,0)) shifted (0,i) ;
        endfor ;
    )
enddef ;

def lmt_axis = applyparameters "axis" "lmt_do_axis" enddef ;

presetparameters "axis" [
    nx = 1, dx = 1, tx = 0, sx = 1, startx = 0,
    ny = 1, dy = 1, ty = 0, sy = 1, starty = 0,

    samples      = { },
    list         = { },
    connect      = false,
    list         = [ close = false ],
    samplecolors = { "" },
    axiscolor    = "",
    textcolor    = "",
] ;

% vardef lmt_do_axis =
%     image (
%         save nx; nx := getparameter "axis" "nx" ;
%         save ny; ny := getparameter "axis" "ny" ;
%         save dx; dx := getparameter "axis" "dx" ;
%         save dy; dy := getparameter "axis" "dy" ;
%         save tx; tx := getparameter "axis" "tx" ;
%         save ty; ty := getparameter "axis" "ty" ;
%         save c, startx, starty ; string c ;
%         c := getparameter "axis" "axiscolor" ;
%         startx := getparameter "axis" "startx" ;
%         starty := getparameter "axis" "starty" ;
%         draw (startx,starty) -- (startx,ny) withcolor c ;
%         draw (startx,starty) -- (nx,starty) withcolor c ;
%         for i = 0 step dx until nx :
%             if (i > startx) or (startx = 0) :
%                 draw ((0,0) -- (0,-2)) shifted (i,starty) withcolor c ;
%             fi ;
%         endfor ;
%         for i = 0 step dy until ny :
%             if (i > starty) or (starty = 0) :
%                 draw ((0,0) -- (-2,0)) shifted (startx,i) withcolor c ;
%             fi ;
%         endfor ;
%         if tx <> 0 :
%             c := getparameter "axis" "textcolor" ;
%             for i = 0 step tx until nx :
%                 if (i > startx) or (startx = 0) :
%                     draw
%                         textext("\strut " & decimal i) ysized 2 shifted (i,-4+starty)
%                         withcolor c;
%                 fi ;
%             endfor ;
%         fi ;
%         if ty <> 0 :
%             c := getparameter "axis" "textcolor" ;
%             for i = 0 step ty until ny :
%                 if (i > starty) or (starty = 0) :
%                     draw
%                         textext.lft("\strut " & decimal i) ysized 2 shifted (-3+startx,i)
%                         withcolor c;
%                 fi ;
%             endfor ;
%         fi ;
%
%         if (getparametercount "axis" "samples") > 0 :
%             if getparameter "axis" "connect" :
%                 for s = 1 upto getparametercount "axis" "samples" :
%                     c := getparameter "axis" "samplecolors" s ;
%                     draw for i = 1 upto getparametercount "axis" "samples" s :
%                         if (i > 1) : -- fi (i, getparameter "axis" "samples" s i)
%                     endfor
%                     withcolor c ;
%                 endfor ;
%             else :
%                 for s = 1 upto getparametercount "axis" "samples" :
%                     c := getparameter "axis" "samplecolors" s ;
%                     for i = 1 upto getparametercount "axis" "samples" s :
%                         draw (i, getparameter "axis" "samples" s i)
%                         withcolor c ;
%                     endfor ;
%                 endfor ;
%             fi ;
%         fi ;
%
%         if (getparametercount "axis" "list") > 0 :
%
%             path p ; numeric ts ; pair a, d ;
%
%             ts := (getparameter "axis" "sy") / 20 ;
%
%             for s = 1 upto getparametercount "axis" "list" :
%
%                 c := getparameter "axis" "list" s "color" ;
%
%                 p := for i = 1 upto getparametercount "axis" "list" s "points" :
%                     if (i > 1) : -- fi (getparameter "axis" "list" s "points" i)
%                 endfor
%                 if (getparameterdefault "axis" "list" s "close" false) : -- cycle fi ;
%
%                 draw p withcolor c ;
%
%                 if (getparametercount "axis" "list" s "labels") > 0 :
%                     for i = 1 upto getparametercount "axis" "list" s "labels" :
%                         draw
%                             textext(getparameterdefault "axis" "list" s "labels" i "")
%                             ysized ts
%                             shifted unitvector(direction (i-eps) of p) % todo
%                             shifted point i of p ;
%                     endfor ;
%                 fi ;
%
%               % if (getparametercount "axis" "list" s "texts") > 0 :
%               %     for i = 1 upto getparametercount "axis" "list" s "texts" :
%               %         n := i + 0.5 ;
%               %         a := point n of p ;
%               %         d := direction n of p ;
%               %         draw textext.d(getparameterdefault "axis" "list" s "texts" i "")
%               %             if d < left : rotated 180 shifted (0,-5) else : shifted (0,5) fi
%               %             ysized ts
%               %             shifted a
%               %             rotatedaround(a,angle(d)) ;
%               %     endfor ;
%               % fi ;
%
%                 pushparameters "axis" "list" s "texts" ;
%                 if (getparametercount) > 0 :
%                     for i = 1 upto getparametercount :
%                         n := i + 0.5 ;
%                         a := point n of p ;
%                         d := direction n of p ;
%                         draw textext.d(getparameterdefault i "")
%                             if d < left : rotated 180 shifted (0,-5) else : shifted (0,5) fi
%                             ysized ts
%                             shifted a
%                             rotatedaround(a,angle(d)) ;
%                     endfor ;
%                 fi ;
%                 popparameters ;
%
%             endfor ;
%         fi ;
%
%     )
%         xyscaled(getparameter "axis" "sx",getparameter "axis" "sy")
% enddef ;

vardef lmt_do_axis =
    image (
        pushparameters "axis" ;

            save nx; nx := getparameter "nx" ;
            save ny; ny := getparameter "ny" ;
            save dx; dx := getparameter "dx" ;
            save dy; dy := getparameter "dy" ;
            save tx; tx := getparameter "tx" ;
            save ty; ty := getparameter "ty" ;
            save c, startx, starty ; string c ;
            c := getparameter "axiscolor" ;
            startx := getparameter "startx" ;
            starty := getparameter "starty" ;
            draw (startx,starty) -- (startx,ny) withcolor c ;
            draw (startx,starty) -- (nx,starty) withcolor c ;
            for i = 0 step dx until nx :
                if (i > startx) or (startx = 0) :
                    draw ((0,0) -- (0,-2)) shifted (i,starty) withcolor c ;
                fi ;
            endfor ;
            for i = 0 step dy until ny :
                if (i > starty) or (starty = 0) :
                    draw ((0,0) -- (-2,0)) shifted (startx,i) withcolor c ;
                fi ;
            endfor ;
            if tx <> 0 :
                c := getparameter "textcolor" ;
                for i = 0 step tx until nx :
                    if (i > startx) or (startx = 0) :
                        draw
                            textext("\strut " & decimal i) ysized 2 shifted (i,-4+starty)
                            withcolor c;
                    fi ;
                endfor ;
            fi ;
            if ty <> 0 :
                c := getparameter "textcolor" ;
                for i = 0 step ty until ny :
                    if (i > starty) or (starty = 0) :
                        draw
                            textext.lft("\strut " & decimal i) ysized 2 shifted (-3+startx,i)
                            withcolor c;
                    fi ;
                endfor ;
            fi ;

            if (getparametercount "samples") > 0 :
                if getparameter "connect" :
                    for s = 1 upto getparametercount "samples" :
                        c := getparameter "samplecolors" s ;
                        draw for i = 1 upto getparametercount "samples" s :
                            if (i > 1) : -- fi (i, getparameter "samples" s i)
                        endfor
                        withcolor c ;
                    endfor ;
                else :
                    for s = 1 upto getparametercount "samples" :
                        c := getparameter "samplecolors" s ;
                        for i = 1 upto getparametercount "samples" s :
                            draw (i, getparameter "samples" s i)
                            withcolor c ;
                        endfor ;
                    endfor ;
                fi ;
            fi ;

            if (getparametercount "list") > 0 :

                path p ; numeric ts ; pair a, d ;

                ts := (getparameter "sy") / 20 ;

                pushparameters "list" ;
                    for s = 1 upto getparametercount :
                        pushparameters s ;

                            c := getparameter "color" ;

                            % p := for i = 1 upto getparametercount "points":
                            %     if (i > 1) : -- fi (getparameter "points" i)
                            % endfor
                            % if (getparameterdefault "close" false) : -- cycle fi ;

                            % this can become:

                            % p := if (getparameterdefault "close" false) :
                            %   % getparameterpath "points" "--" true ;
                            %     getparameterpath "points" true ;
                            % else :
                            %   % getparameterpath "points" "--" false ;
                            %     getparameterpath "points" ;
                            % fi ;

                            % p := getparameterpath "points" if (getparameterdefault "close" false) : true fi ;

                            p := getparameterpath "points" (getparameterdefault "close" false) ;
                          % p := getparameterpath "points" getparameterdefault "close" false ;

                            draw p withcolor c ;

                            pushparameters "labels" ;
                                if (getparametercount) > 0 :
                                    for i = 1 upto getparametercount:
                                        n := i - 1 ;
                                        a := point n of p ;
                                        d := direction n of p ;
                                        draw
                                            textext(getparametertext i true)
                                            ysized ts
                                            shifted (a + .5 * unitvector(d) rotated 90) ;
                                    endfor ;
                                fi ;
                            popparameters ;

                            pushparameters "texts" ;
                                if (getparametercount) > 0 :
                                    for i = 1 upto getparametercount :
                                        n := i + 0.5 ;
                                        a := point n of p ;
                                        d := direction n of p ;
                                        draw textext.d(getparametertext i true)
                                            if d < left : rotated 180 shifted (0,-5) else : shifted (0,5) fi
                                            ysized ts
                                            shifted a
                                            rotatedaround(a,angle(d)) ;
                                    endfor ;
                                fi ;
                            popparameters ;

                        popparameters ;
                    endfor ;
                popparameters ;
            fi ;

        popparameters ;

    )
        xyscaled(getparameter "axis" "sx",getparameter "axis" "sy")
enddef ;

presetparameters "outline" [
    content       = "",
    kind          = "draw",
    fillcolor     = "",
    drawcolor     = "",
    rulethickness = 1/10,
    align         = "",
    style         = "",
    width         = 0,
] ;

def lmt_outline = applyparameters "outline" "lmt_do_outline" enddef ;

vardef lmt_do_outline =
    image ( normaldraw image (
        save kind  ; string kind   ; kind  := getparameter "outline" "kind"  ;
        save align ; string align  ; align := getparameter "outline" "align" ;
        save style ; string style  ; style := getparameter "outline" "style" ;
        save width ; numeric width ; width := getparameter "outline" "width" ;
        if kind = "draw" :
            kind := "d" ;
        elseif kind = "fill" :
            kind := "f" ;
        elseif kind = "both" :
            kind := "b" ;
        elseif kind = "reverse" :
            kind := "r" ;
        fi ;
        currentoutlinetext := currentoutlinetext + 1 ;
        lua.mp.mf_outline_text(
            currentoutlinetext,
            if align = "" :
                getparameter "outline" "content",
            else :
                "\framed[align={" & align & "}"
                if width > 0 :
                    & ",width=" & decimal width & "bp"
                fi
                if style <> "" :
                    & ",foregroundstyle=" & style
                fi
                & ",offset=none,frame=off]{"
                & (getparameter "outline" "content")
                & "}",
            fi,
            kind
        ) ;
        save currentpen; pen currentpen ;
        pickup pencircle scaled getparameter "outline" "rulethickness"  ;
        if kind = "f" :
            mfun_do_outline_text_set_f (
                withcolor getparameter "outline" "fillcolor"
            );
        elseif kind = "d" :
            mfun_do_outline_text_set_d (
                withcolor getparameter "outline" "drawcolor"
            );
        elseif kind = "b" :
            mfun_do_outline_text_set_b (
                withcolor getparameter "outline" "fillcolor"
            ) (
                withcolor getparameter "outline" "drawcolor"
            );
        elseif kind = "u" :
            mfun_do_outline_text_set_f (
                withcolor getparameter "outline" "fillcolor"
            );
        elseif kind = "r" :
            mfun_do_outline_text_set_r (
                withcolor getparameter "outline" "drawcolor"
            ) (
                withcolor getparameter "outline" "fillcolor"
            ) ;
        elseif kind = "p" :
            mfun_do_outline_text_set_p ;
        else :
            mfun_do_outline_text_set_n (
                % what to use here
            );
        fi ;
        lua.mp.mf_get_outline_text(currentoutlinetext) ;
    ) )
enddef ;

presetparameters "followtext" [
    content       = "",
    spread        = true,
    trace         = false,
    reverse       = false,
    autoscaleup   = "no",
    autoscaledown = "no",
    path          = (fullcircle),
] ;

def lmt_followtext = applyparameters "followtext" "lmt_do_followtext" enddef ;

vardef lmt_do_followtext =
    image (
        save s_u ; string s_u ; s_u := getparameter "followtext" "autoscaleup" ;
        save s_d ; string s_d ; s_d := getparameter "followtext" "autoscaledown" ;
        save followtextalternative   ; followtextalternative   := if getparameter "followtext" "spread" : 1 else : 0 fi ;
        save tracingfollowtext       ; tracingfollowtext       := if getparameter "followtext" "trace"  : 1 else : 0 fi ;
        save autoscaleupfollowtext   ; autoscaleupfollowtext   := if s_u = "yes" : 1 elseif s_u = "max" : 2 else : 0 fi ;
        save autoscaledownfollowtext ; autoscaledownfollowtext := if s_d = "yes" : 1 elseif s_d = "max" : 2 else : 0 fi ;
        draw followtext (
            if (getparameter "followtext" "reverse") : reverse fi getparameter "followtext" "path",
                getparameter "followtext" "content"
        ) ;
    )
enddef ;

presetparameters "arrow" [
    path        = origin,
    kind        = "fill",
    dimple      = 1/5,
    scale       = 3/4,
    length      = 4,
    angle       = 45,
    location    = "end",    % middle both
    alternative = "normal", % dimpled curved
    percentage  = 50,
    headonly    = false,
] ;

def lmt_arrow = applyparameters "arrow" "lmt_do_arrow" enddef ;

vardef lmt_do_arrow =
    image (
        save a ; string a ; a := getparameter "arrow" "alternative" ;
        save l ; string l ; l := getparameter "arrow" "location" ;
        save k ; string k ; k := getparameter "arrow" "kind" ;
        save p ; path   p ; p := getparameter "arrow" "path" ;
        save ahvariant ; ahvariant := if a = "dimpled" : 1 elseif a = "curved" : 2 else : 0 fi ;
        save ahdimple  ; ahdimple  := getparameter "arrow" "dimple" ;
        save ahscale   ; ahscale   := getparameter "arrow" "scale" ;
        save ahangle   ; ahangle   := getparameter "arrow" "angle" ;
        save ahlength  ; ahlength  := getparameter "arrow" "length" ;
        if not getparameter "arrow" "headonly" :
            draw p ;
        fi ;
        if k = "draw" : draw elseif k = "both" : filldraw else : fill fi
        if l = "middle" :
            midarrowhead p ;
        elseif l = "percentage" :
            arrowheadonpath (p, (getparameter "arrow" "percentage")/100) ;
        elseif l = "both" :
            arrowhead p ;
            if k = "draw" : draw elseif k = "both" : filldraw else : fill fi
            arrowhead reverse p ;
        else :
            arrowhead p ;
        fi ;
    )
enddef ;

% from dum

presetparameters "placeholder" [
    color       = "red",
    width       = 1,
    height      = 1,
    reduction   = 0,
    alternative = "circle",
] ;

def lmt_placeholder = applyparameters "placeholder" "lmt_do_placeholder" enddef ;

def lmt_do_placeholder =
    begingroup ;
    save w, h, d, r, p, c, b, s, q, a ;
    numeric w, h, d, r ; path p ; string s, a ;
    s := getparameter "placeholder" "color" ;
    w := getparameter "placeholder" "width" ;
    h := getparameter "placeholder" "height" ;
    r := getparameter "placeholder" "reduction" ;
    a := getparameter "placeholder" "alternative" ;
    d := max(w,h) ;
    if cmykcolor resolvedcolor(s) :
        cmykcolor c, b ; b := (0,0,0,0)
    else :
        color c, b ; b := (1,1,1)
    fi ;
    c := resolvedcolor(s) ;
    p := unitsquare xyscaled (w,h) ;
    fill p withcolor r[.5c,b] ;
    if a = "square" :
        vardef q = fullsquare enddef ;
    elseif a = "triangle" :
        vardef q = fulltriangle rotated (90 * round(uniformdeviate(4))) enddef ;
    else :
        vardef q = fullcircle enddef ;
    fi ;
    for i := 1 upto 60 :
        fill q
            scaled (d/5 randomized (d/5))
            shifted (center p randomized (d))
            withcolor r[c randomized(.3,.9),b] ;
    endfor ;
    clip currentpicture to p ;
    endgroup ;
enddef ;

% maybe:

vardef lmt_connected(text t) =
    save p ; path p ;
    p := origin t ;
    subpath (1,length(p)) of p
enddef;

def lmt_connection expr t =
    -- t
enddef;