meta-nod.mkiv /size: 7545 b    last modification: 2020-07-01 14:35
1
%D \module
2
%D [ file=meta-nod,
3
%D version=2016.11.23,
4
%D title=\METAPOST\ Graphics,
5
%D subtitle=Nodes,
6
%D author={Alan Braslau and Hans Hagen},
7
%D date=\currentdate,
8
%D copyright={PRAGMA ADE \& \CONTEXT\ Development Team}]
9
%C
10
%C This module is part of the \CONTEXT\ macro||package and is
11
%C therefore copyrighted by \PRAGMA. See mreadme.pdf for
12
%C details.
13 14
\registerctxluafile
{
meta
-
nod
}{}
15 16
\unprotect
17 18
\defineMPinstance
19
[
nodes
]
20
[
\s!format
=
metafun
,
21
\s!extensions
=
\v!yes
,
22
\s!initializations
=
\v!yes
,
23
\c!method
=
\s!double
]
24 25
\defineframed
26
[
node
]
27
[
\c!frame
=
\v!off
]
28 29
\defineframed
30
[
smallnode
]
31
[
node
]
32
[
\c!foregroundstyle
=
\small
]
33 34
% \startMPdefinitions{nodes}
35
% loadmodule "node" ;
36
% \stopMPdefinitions
37 38
\protect
39 40
%D This module provides a simple \TEX\ layer on top of the nodes library that
41
%D is preloaded in \METAFUN. More information can be found in the manual and
42
%D article.
43
%D
44
%D We strongly advice to use the \METAPOST\ interface and only provide this
45
%D \TEX\ variant as proof of concept. There are no plans to extend this module
46
%D because we see no advantage in using a \TEX\ interface over a \METAPOST\
47
%D one.
48
%D
49
%D \startbuffer
50
%D \startnodes
51
%D \placenode[0,0]{A}
52
%D \placenode[1,0]{B}
53
%D \connectnodes [0,1] [option=doublearrow]
54
%D \stopnodes
55
%D
56
%D \startnodes[dx=2cm,rotation=45]
57
%D \placenode[0,0]{A}
58
%D \placenode[1,0]{B}
59
%D \placenode[1,1]{C}
60
%D \placenode[0,1]{D}
61
%D \connectnodes [0,2] [option=doublearrow,label=a,offset=.05]
62
%D \connectnodes [1,3] [option=doublearrows]
63
%D \stopnodes
64
%D
65
%D \startnodes[dx=2cm,dy=2cm]
66
%D \nodeMPcode{ahlength := 12pt ; ahangle := 30 ; ahvariant := 1 ;}
67
%D \mpcode{ahlength := 12pt ; ahangle := 30 ; ahvariant := 1 ;}
68
%D \placenode[0,1]{\node{A}}
69
%D \placenode[1,1]{\node{B}}
70
%D \placenode[0,0]{\node{C}}
71
%D \placenode[1,0]{\node{D}}
72
%D \connectnodes [0,3] [option=arrow,label={ }]
73
%D \connectnodes [1,2] [option=arrow]
74
%D \stopnodes
75
%D \stopbuffer
76
%D
77
%D \typebuffer
78
%D
79
%D \startlinecorrection \getbuffer \stoplinecorrection
80 81
\unprotect
82 83
\installcorenamespace
{
metanodes
}
84
\installcorenamespace
{
metanodesoption
}
85
\installcorenamespace
{
metanodesalternative
}
86
\installcorenamespace
{
metanodesposition
}
87 88
% todo: maybe instances
89 90
\installparameterhandler
\??metanodes
{
metanodes
}
91
\installsetuphandler
\??metanodes
{
metanodes
}
92 93
\setupmetanodes
94
[
\c!option
=
,
95
\c!alternative
=
,
96
\c!offset
=
0
,
97
\c!position
=
,
98
\c!label
=
,
99
\c!dx
=
2
\emwidth
,
100
\c!dy
=
2
\emwidth
,
101
\c!rotation
=
9
0
,
102
\c!rulethickness
=
.
5
pt
,
103
\c!command
=]
104 105
\newtoks
\t_every_meta_nodes
106
\newtoks
\t_meta_nodes
107 108
\let
\stopnodes
\relax
109 110
%D Hm, we started out simple but it now quickly becomes the usual mess of
111
%D \TEX, \METAPOST\ and \LUA. Hard to understand.
112 113
\newcount
\c_meta_nodes_n
114 115
\unexpanded
\def
\startnodes
116
{
\dosingleempty
\meta_nodes_start
}
117 118
\def
\meta_nodes_start
[#
1
]#
2
\stopnodes
119
{
\hpack
\bgroup
120
\iffirstargument
121
\setupcurrentmetanodes
[#
1
]
%
122
\fi
123
\edef
\p_meta_option
{
\metanodesparameter
\c!option
}
%
124
\edef
\p_meta_alternative
{
\metanodesparameter
\c!alternative
}
%
125
\the
\t_every_meta_nodes
126
\c_meta_nodes_n
\zerocount
127
\t_meta_nodes
\emptytoks
128
#
2
\removeunwantedspaces
129
% for alan, will be commented:
130
\writestatus
{
metanodes
}{
\detokenize\expandafter
{
\the
\t_meta_nodes
}}
%
131
\clf_grph_nodes_initialize
132
\startMPcode
133
mfun_node_init
(
%
134
\the
\dimexpr
\metanodesparameter
\c!dx
\relax
,
%
135
\the
\dimexpr
\metanodesparameter
\c!dy
\relax
,
%
136
\metanodesparameter
\c!rotation
%
137
)
;
138
\the
\t_meta_nodes
;
139
mfun_node_flush
;
140
\stopMPcode
141
\clf_grph_nodes_reset
142
\egroup
}
143 144
\unexpanded
\def
\grph_nodes_node
145
{
\dodoubleempty
\grph_nodes_node_two
}
146 147
\def
\grph_nodes_node_two
[#
1
][#
2
]
%
148
{
\grph_nodes_node_two_indeed
[#
1
][#
2
]
}
149 150
\def
\grph_nodes_node_two_indeed
[#
1
,
#
2
][#
3
]#
4
%
151
{
\begingroup
152
\ifsecondargument
153
\setupcurrentmetanodes
[#
3
]
%
154
\fi
155
\edef
\p_label
{
#
4
}
%
156
\edef
\p_reference
{
\metanodesparameter
\c!reference
}
%
157
\ifx
\p_reference
\empty
\else
158
\clf_grph_nodes_register
{
\p_reference
}
\c_meta_nodes_n
\relax
159
\fi
160
\normalexpanded
{
\endgroup\noexpand\etoksapp
\t_meta_nodes
{
%
161
mfun
_
node
_
make
(
\number
#
1
,
\number
#
2
%
162
\ifx
\p_label
\empty
163
,
""
%
164
\else
165
,
"
\metanodesparameter
\c!command
{
\p_label
}
"
%
166
\fi
167
)
;
%
168
}}
%
169
\advance
\c_meta_nodes_n
\plusone
}
170 171
\appendtoks
172
\let
\placenode
\grph_nodes_node
173
\to
\t_every_meta_nodes
174 175
\unexpanded
\def
\grph_nodes_fromto
176
{
\dodoubleempty
\grph_nodes_fromto_two
}
177 178
\def
\grph_nodes_fromto_two
[#
1
][#
2
]
%
179
{
\grph_nodes_fromto_two_indeed
[#
1
][#
2
]
}
% get rid of {n,m}
180 181
\letvalue
{
\??metanodesposition
}
\empty
182
\setvalue
{
\??metanodesposition
top
}{
.
top
}
183
\setvalue
{
\??metanodesposition
bottom
}{
.
bot
}
184
\setvalue
{
\??metanodesposition
left
}{
.
lft
}
185
\setvalue
{
\??metanodesposition
right
}{
.
rt
}
186
\setvalue
{
\??metanodesposition
upperleft
}{
.
ulft
}
187
\setvalue
{
\??metanodesposition
topleft
}{
.
ulft
}
188
\setvalue
{
\??metanodesposition
lefttop
}{
.
ulft
}
189
\setvalue
{
\??metanodesposition
upperright
}{
.
urt
}
190
\setvalue
{
\??metanodesposition
topright
}{
.
urt
}
191
\setvalue
{
\??metanodesposition
righttop
}{
.
urt
}
192
\setvalue
{
\??metanodesposition
lowerleft
}{
.
llft
}
193
\setvalue
{
\??metanodesposition
bottomleft
}{
.
llft
}
194
\setvalue
{
\??metanodesposition
leftbottom
}{
.
llft
}
195
\setvalue
{
\??metanodesposition
lowerright
}{
.
lrt
}
196
\setvalue
{
\??metanodesposition
bottomright
}{
.
lrt
}
197
\setvalue
{
\??metanodesposition
rightbottom
}{
.
lrt
}
198 199
\def
\grph_nodes_fromto_two_indeed
[#
1
,
#
2
][#
3
]
% we can't group because etoksapp doesn't like that
200
{
\begingroup
201
\ifsecondargument
202
\setupcurrentmetanodes
[#
3
]
%
203
\fi
204
\edef
\p_label
{
\metanodesparameter
\c!label
}
%
205
\edef
\p_rulethickness
{
\metanodesparameter
\c!rulethickness
}
%
206
\edef
\p_command
{
\metanodesparameter
\c!command
}
% better get an error early
207
\normalexpanded
{
\endgroup\noexpand\etoksapp
\t_meta_nodes
{
%
208
\ifcsname
\??metanodesalternative
\metanodesparameter
\c!alternative
\endcsname
209
\lastnamedcs
210
\else
211
\csname
\??metanodesalternative
\endcsname
212
\fi
213
\space
214
mfun
_
nodes
_
fromto
\begincsname
\??metanodesposition
\metanodesparameter
\c!position
\endcsname
(
%
215
\metanodesparameter
\c!offset
,
%
216
% \number#1,\number#2%
217
\clf_grph_nodes_resolve
{
#
1
}
,
\clf_grph_nodes_resolve
{
#
2
}
%
218
\ifx
\p_label
\empty
219
,
""
%
220
\else
221
,
"
\ifx
\p_command
\empty
\p_label
\else
\p_command
{
\p_label
}
\fi
"
%
222
\fi
223
)
%
224
\space
225
\ifcsname
\??metanodesoption
\metanodesparameter
\c!option
\endcsname
226
\lastnamedcs
227
\else
228
\csname
\??metanodesoption
\endcsname
229
\fi
230
\space
231
\ifx
\p_rulethickness
\empty
\else
232
withpen
pencircle
scaled
\the\dimexpr
\p_rulethickness
\relax
233
\fi
234
;
%
235
}}}
236 237
\setvalue
{
\??metanodesalternative
}{
draw
}
238
\setvalue
{
\??metanodesalternative
arrow
}{
drawarrow
}
239
\setvalue
{
\??metanodesalternative
doublearrow
}{
drawdblarrow
}
240
\setvalue
{
\??metanodesalternative
doublearrows
}{
drawdoublearrows
}
241 242
\setvalue
{
\??metanodesoption
}{}
243
\setvalue
{
\??metanodesoption
dashed
}{
dashed
evenly
}
244
\setvalue
{
\??metanodesoption
dotted
}{
dashed
withdots
scaled
.
5
}
245 246
\appendtoks
247
\let
\connectnodes
\grph_nodes_fromto
248
\to
\t_every_meta_nodes
249 250
\unexpanded
\def
\grph_nodes_code
#
1
%
251
{
\etoksapp
\t_meta_nodes
{
#
1
}}
252 253
\appendtoks
254
\let
\nodeMPcode
\grph_nodes_code
255
\let
\mpcode
\grph_nodes_code
256
\to
\t_every_meta_nodes
257 258
\protect
\endinput
259