meta-nod.mkxl /size: 7793 b    last modification: 2023-12-21 09:44
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 is
41%D preloaded in \METAFUN. More information can be found in the manual and article.
42%D
43%D We strongly advice to use the \METAPOST\ interface and only provide this \TEX\
44%D variant as proof of concept. There are no plans to extend this module because we
45%D see no advantage in using a \TEX\ interface over a \METAPOST\ one.
46%D
47%D \startbuffer
48%D \startnodes
49%D    \placenode[0,0]{A}
50%D    \placenode[1,0]{B}
51%D    \connectnodes [0,1] [option=doublearrow]
52%D \stopnodes
53%D
54%D \startnodes[dx=2cm,rotation=45]
55%D    \placenode[0,0]{A}
56%D    \placenode[1,0]{B}
57%D    \placenode[1,1]{C}
58%D    \placenode[0,1]{D}
59%D    \connectnodes [0,2] [option=doublearrow,label=a,offset=.05]
60%D    \connectnodes [1,3] [option=doublearrows]
61%D \stopnodes
62%D
63%D \startnodes[dx=2cm,dy=2cm]
64%D    \nodeMPcode{ahlength  := 12pt ; ahangle := 30 ; ahvariant := 1 ;}
65%D    \mpcode{ahlength  := 12pt ; ahangle := 30 ; ahvariant := 1 ;}
66%D    \placenode[0,1]{\node{A}}
67%D    \placenode[1,1]{\node{B}}
68%D    \placenode[0,0]{\node{C}}
69%D    \placenode[1,0]{\node{D}}
70%D    \connectnodes [0,3] [option=arrow,label={ }]
71%D    \connectnodes [1,2] [option=arrow]
72%D \stopnodes
73%D \stopbuffer
74%D
75%D \typebuffer
76%D
77%D \startlinecorrection \getbuffer \stoplinecorrection
78
79\unprotect
80
81\installcorenamespace{metanodes}
82\installcorenamespace{metanodesoption}
83\installcorenamespace{metanodesalternative}
84\installcorenamespace{metanodesposition}
85
86% todo: maybe instances
87
88\installparameterhandler \??metanodes {metanodes}
89\installsetuphandler     \??metanodes {metanodes}
90
91\setupmetanodes
92  [\c!option        =,
93   \c!alternative   =,
94   \c!offset        =0,
95   \c!position      =,
96   \c!label         =,
97   \c!dx            =2\emwidth,
98   \c!dy            =2\emwidth,
99   \c!rotation      =90,
100   \c!rulethickness =.5pt,
101   \c!command       =]
102
103\newtoks\t_every_meta_nodes
104\newtoks\t_meta_nodes
105
106%D Hm, we started out simple but it now quickly becomes the usual mess of \TEX,
107%D \METAPOST\ and \LUA. Hard to understand.
108
109\newinteger\c_meta_nodes_n
110
111\permanent\protected\lettonothing\stopnodes
112
113\permanent\tolerant\protected\def\startnodes[#1]#:#2\stopnodes
114  {\hpack\bgroup
115   \setupcurrentmetanodes[#1]%
116   \edef\p_meta_option{\metanodesparameter\c!option}%
117   \edef\p_meta_alternative{\metanodesparameter\c!alternative}%
118   \expand\t_every_meta_nodes
119   \c_meta_nodes_n\zerocount
120   \t_meta_nodes\emptytoks
121   #2\removeunwantedspaces
122   % for alan, will be commented:
123   \writestatus{metanodes}{\detokenize\expandafter{\the\t_meta_nodes}}%
124   \clf_grph_nodes_initialize
125   \startMPcode
126     mfun_node_init(%
127        \the\dimexpr\metanodesparameter\c!dx\relax,%
128        \the\dimexpr\metanodesparameter\c!dy\relax,%
129        \metanodesparameter\c!rotation%
130     ) ;
131     \the\t_meta_nodes ;
132     mfun_node_flush ;
133   \stopMPcode
134   \clf_grph_nodes_reset
135   \egroup}
136
137\permanent\tolerant\protected\def\grph_nodes_node[#1]#*[#2]%
138  {\grph_nodes_node_indeed[#1][#2]}
139
140\def\grph_nodes_node_indeed[#1,#2][#3]#4%
141  {\begingroup
142   \setupcurrentmetanodes[#3]%
143   \edef\p_label{#4}%
144   \edef\p_reference{\metanodesparameter\c!reference}%
145   \ifempty\p_reference\else
146      \clf_grph_nodes_register{\p_reference}\c_meta_nodes_n\relax
147   \fi
148   \normalexpanded{\endgroup\noexpand\etoksapp\t_meta_nodes{%
149     mfun_node_make(\number#1,\number#2%
150       \ifempty\p_label
151         ,""%
152       \else
153         ,"\metanodesparameter\c!command{\p_label}"%
154       \fi
155     );%
156   }}%
157   \advanceby\c_meta_nodes_n\plusone}
158
159\permanent\protected\lettonothing\placenode
160
161\appendtoks
162   \enforced\let\placenode\grph_nodes_node
163\to \t_every_meta_nodes
164
165\permanent\tolerant\protected\def\grph_nodes_fromto[#1]#*[#2]%
166  {\grph_nodes_fromto_indeed[#1][#2]}% get rid of {n,m} % todo: we can use #_ or so
167
168\letcsname\??metanodesposition            \endcsname\empty
169\defcsname\??metanodesposition         top\endcsname{.top}
170\defcsname\??metanodesposition      bottom\endcsname{.bot}
171\defcsname\??metanodesposition        left\endcsname{.lft}
172\defcsname\??metanodesposition       right\endcsname{.rt}
173\defcsname\??metanodesposition   upperleft\endcsname{.ulft}
174\defcsname\??metanodesposition     topleft\endcsname{.ulft}
175\defcsname\??metanodesposition     lefttop\endcsname{.ulft}
176\defcsname\??metanodesposition  upperright\endcsname{.urt}
177\defcsname\??metanodesposition    topright\endcsname{.urt}
178\defcsname\??metanodesposition    righttop\endcsname{.urt}
179\defcsname\??metanodesposition   lowerleft\endcsname{.llft}
180\defcsname\??metanodesposition  bottomleft\endcsname{.llft}
181\defcsname\??metanodesposition  leftbottom\endcsname{.llft}
182\defcsname\??metanodesposition  lowerright\endcsname{.lrt}
183\defcsname\??metanodesposition bottomright\endcsname{.lrt}
184\defcsname\??metanodesposition rightbottom\endcsname{.lrt}
185
186\def\grph_nodes_fromto_indeed[#1,#2][#3]% we can't group because etoksapp doesn't like that
187  {\begingroup
188   \setupcurrentmetanodes[#3]%
189   \edef\p_label        {\metanodesparameter\c!label}%
190   \edef\p_rulethickness{\metanodesparameter\c!rulethickness}%
191   \edef\p_command      {\metanodesparameter\c!command}% better get an error early
192   \normalexpanded{\endgroup\noexpand\etoksapp\t_meta_nodes{%
193     \ifcsname\??metanodesalternative\metanodesparameter\c!alternative\endcsname
194       \lastnamedcs
195     \else
196       \csname\??metanodesalternative\endcsname
197     \fi
198     \space
199     mfun_nodes_fromto\begincsname\??metanodesposition\metanodesparameter\c!position\endcsname(%
200       \metanodesparameter\c!offset,%
201     % \number#1,\number#2%
202       \clf_grph_nodes_resolve{#1},\clf_grph_nodes_resolve{#2}%
203       \ifempty\p_label
204         ,""%
205       \else
206         ,"\ifempty\p_command\p_label\else\p_command{\p_label}\fi"%
207       \fi
208     )%
209     \space
210     \ifcsname\??metanodesoption\metanodesparameter\c!option\endcsname
211       \lastnamedcs
212     \else
213       \csname\??metanodesoption\endcsname
214     \fi
215     \space
216     \ifempty\p_rulethickness \else
217       withpen pencircle scaled \the\dimexpr\p_rulethickness\relax
218     \fi
219     ;%
220   }}}
221
222\defcsname\??metanodesalternative             \endcsname{draw}
223\defcsname\??metanodesalternative        arrow\endcsname{drawarrow}
224\defcsname\??metanodesalternative  doublearrow\endcsname{drawdblarrow}
225\defcsname\??metanodesalternative doublearrows\endcsname{drawdoublearrows}
226
227\defcsname\??metanodesoption                  \endcsname{}
228\defcsname\??metanodesoption            dashed\endcsname{dashed evenly}
229\defcsname\??metanodesoption            dotted\endcsname{dashed withdots scaled .5}
230
231\permanent\protected\lettonothing\connectnodes
232\permanent\protected\lettonothing\mpcode
233\permanent\protected\lettonothing\nodeMPcode
234
235\appendtoks
236   \enforced\let\connectnodes\grph_nodes_fromto
237\to \t_every_meta_nodes
238
239\permanent\protected\def\grph_nodes_code#1%
240  {\etoksapp\t_meta_nodes{#1}}
241
242\appendtoks
243   \enforced\let\nodeMPcode\grph_nodes_code
244   \enforced\let\mpcode    \grph_nodes_code
245\to \t_every_meta_nodes
246
247\protect \endinput
248