summaryrefslogtreecommitdiff
path: root/Master/texmf-dist/tex/context/base/mkiv/meta-nod.mkiv
blob: 9f966349c15b23a8d17943137637a600ec8370c4 (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
%D \module
%D   [       file=meta-nod,
%D        version=2016.11.23,
%D          title=\METAPOST\ Graphics,
%D       subtitle=Nodes,
%D         author={Alan Braslau and 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.

\registerctxluafile{meta-nod}{}

\unprotect

\defineMPinstance
  [nodes]
  [\s!format=metafun,
   \s!extensions=\v!yes,
   \s!initializations=\v!yes,
   \c!method=\s!double]

\defineframed
  [node]
  [\c!frame=\v!off]

\defineframed
  [smallnode]
  [node]
  [\c!foregroundstyle=\small]

% \startMPdefinitions{nodes}
%     loadmodule "node" ;
% \stopMPdefinitions

\protect

%D This module provides a simple \TEX\ layer on top of the nodes library that
%D is preloaded in \METAFUN. More information can be found in the manual and
%D article.
%D
%D We strongly advice to use the \METAPOST\ interface and only provide this
%D \TEX\ variant as proof of concept. There are no plans to extend this module
%D because we see no advantage in using a \TEX\ interface over a \METAPOST\
%D one.
%D
%D \startbuffer
%D \startnodes
%D    \placenode[0,0]{A}
%D    \placenode[1,0]{B}
%D    \connectnodes [0,1] [option=doublearrow]
%D \stopnodes
%D
%D \startnodes[dx=2cm,rotation=45]
%D    \placenode[0,0]{A}
%D    \placenode[1,0]{B}
%D    \placenode[1,1]{C}
%D    \placenode[0,1]{D}
%D    \connectnodes [0,2] [option=doublearrow,label=a,offset=.05]
%D    \connectnodes [1,3] [option=doublearrows]
%D \stopnodes
%D
%D \startnodes[dx=2cm,dy=2cm]
%D    \nodeMPcode{ahlength  := 12pt ; ahangle := 30 ; ahvariant := 1 ;}
%D    \mpcode{ahlength  := 12pt ; ahangle := 30 ; ahvariant := 1 ;}
%D    \placenode[0,1]{\node{A}}
%D    \placenode[1,1]{\node{B}}
%D    \placenode[0,0]{\node{C}}
%D    \placenode[1,0]{\node{D}}
%D    \connectnodes [0,3] [option=arrow,label={ }]
%D    \connectnodes [1,2] [option=arrow]
%D \stopnodes
%D \stopbuffer
%D
%D \typebuffer
%D
%D \startlinecorrection \getbuffer \stoplinecorrection

\unprotect

\installcorenamespace{metanodes}
\installcorenamespace{metanodesoption}
\installcorenamespace{metanodesalternative}
\installcorenamespace{metanodesposition}

% todo: maybe instances

\installparameterhandler \??metanodes {metanodes}
\installsetuphandler     \??metanodes {metanodes}

\setupmetanodes
  [\c!option        =,
   \c!alternative   =,
   \c!offset        =0,
   \c!position      =,
   \c!label         =,
   \c!dx            =2\emwidth,
   \c!dy            =2\emwidth,
   \c!rotation      =90,
   \c!rulethickness =.5pt,
   \c!command       =]

\newtoks\t_every_meta_nodes
\newtoks\t_meta_nodes

\let\stopnodes\relax

%D Hm, we started out simple but it now quickly becomes the usual mess of
%D \TEX, \METAPOST\ and \LUA. Hard to understand.

\newcount\c_meta_nodes_n

\unexpanded\def\startnodes
  {\dosingleempty\meta_nodes_start}

\def\meta_nodes_start[#1]#2\stopnodes
  {\hpack\bgroup
   \iffirstargument
     \setupcurrentmetanodes[#1]%
   \fi
   \edef\p_meta_option{\metanodesparameter\c!option}%
   \edef\p_meta_alternative{\metanodesparameter\c!alternative}%
   \the\t_every_meta_nodes
   \c_meta_nodes_n\zerocount
   \t_meta_nodes\emptytoks
   #2\removeunwantedspaces
   % for alan, will be commented:
   \writestatus{metanodes}{\detokenize\expandafter{\the\t_meta_nodes}}%
   \clf_grph_nodes_initialize
   \startMPcode
     mfun_node_init(%
        \the\dimexpr\metanodesparameter\c!dx\relax,%
        \the\dimexpr\metanodesparameter\c!dy\relax,%
        \metanodesparameter\c!rotation%
     ) ;
     \the\t_meta_nodes ;
     mfun_node_flush ;
   \stopMPcode
   \clf_grph_nodes_reset
   \egroup}

\unexpanded\def\grph_nodes_node
  {\dodoubleempty\grph_nodes_node_two}

\def\grph_nodes_node_two[#1][#2]%
  {\grph_nodes_node_two_indeed[#1][#2]}

\def\grph_nodes_node_two_indeed[#1,#2][#3]#4%
  {\begingroup
   \ifsecondargument
     \setupcurrentmetanodes[#3]%
   \fi
   \edef\p_label{#4}%
   \edef\p_reference{\metanodesparameter\c!reference}%
   \ifx\p_reference\empty\else
      \clf_grph_nodes_register{\p_reference}\c_meta_nodes_n\relax
   \fi
   \normalexpanded{\endgroup\noexpand\etoksapp\t_meta_nodes{%
     mfun_node_make(\number#1,\number#2%
       \ifx\p_label\empty
         ,""%
       \else
         ,"\metanodesparameter\c!command{\p_label}"%
       \fi
     );%
   }}%
   \advance\c_meta_nodes_n\plusone}

\appendtoks
   \let\placenode\grph_nodes_node
\to \t_every_meta_nodes

\unexpanded\def\grph_nodes_fromto
  {\dodoubleempty\grph_nodes_fromto_two}

\def\grph_nodes_fromto_two[#1][#2]%
  {\grph_nodes_fromto_two_indeed[#1][#2]}% get rid of {n,m}

\letvalue{\??metanodesposition            }\empty
\setvalue{\??metanodesposition         top}{.top}
\setvalue{\??metanodesposition      bottom}{.bot}
\setvalue{\??metanodesposition        left}{.lft}
\setvalue{\??metanodesposition       right}{.rt}
\setvalue{\??metanodesposition   upperleft}{.ulft}
\setvalue{\??metanodesposition     topleft}{.ulft}
\setvalue{\??metanodesposition     lefttop}{.ulft}
\setvalue{\??metanodesposition  upperright}{.urt}
\setvalue{\??metanodesposition    topright}{.urt}
\setvalue{\??metanodesposition    righttop}{.urt}
\setvalue{\??metanodesposition   lowerleft}{.llft}
\setvalue{\??metanodesposition  bottomleft}{.llft}
\setvalue{\??metanodesposition  leftbottom}{.llft}
\setvalue{\??metanodesposition  lowerright}{.lrt}
\setvalue{\??metanodesposition bottomright}{.lrt}
\setvalue{\??metanodesposition rightbottom}{.lrt}

\def\grph_nodes_fromto_two_indeed[#1,#2][#3]% we can't group because etoksapp doesn't like that
  {\begingroup
   \ifsecondargument
     \setupcurrentmetanodes[#3]%
   \fi
   \edef\p_label        {\metanodesparameter\c!label}%
   \edef\p_rulethickness{\metanodesparameter\c!rulethickness}%
   \edef\p_command      {\metanodesparameter\c!command}% better get an error early
   \normalexpanded{\endgroup\noexpand\etoksapp\t_meta_nodes{%
     \ifcsname\??metanodesalternative\metanodesparameter\c!alternative\endcsname
       \lastnamedcs
     \else
       \csname\??metanodesalternative\endcsname
     \fi
     \space
     mfun_nodes_fromto\begincsname\??metanodesposition\metanodesparameter\c!position\endcsname(%
       \metanodesparameter\c!offset,%
     % \number#1,\number#2%
       \clf_grph_nodes_resolve{#1},\clf_grph_nodes_resolve{#2}%
       \ifx\p_label\empty
         ,""%
       \else
         ,"\ifx\p_command\empty\p_label\else\p_command{\p_label}\fi"%
       \fi
     )%
     \space
     \ifcsname\??metanodesoption\metanodesparameter\c!option\endcsname
       \lastnamedcs
     \else
       \csname\??metanodesoption\endcsname
     \fi
     \space
     \ifx\p_rulethickness\empty \else
       withpen pencircle scaled \the\dimexpr\p_rulethickness\relax
     \fi
     ;%
   }}}

\setvalue{\??metanodesalternative             }{draw}
\setvalue{\??metanodesalternative        arrow}{drawarrow}
\setvalue{\??metanodesalternative  doublearrow}{drawdblarrow}
\setvalue{\??metanodesalternative doublearrows}{drawdoublearrows}

\setvalue{\??metanodesoption                  }{}
\setvalue{\??metanodesoption            dashed}{dashed evenly}
\setvalue{\??metanodesoption            dotted}{dashed withdots scaled .5}

\appendtoks
   \let\connectnodes\grph_nodes_fromto
\to \t_every_meta_nodes

\unexpanded\def\grph_nodes_code#1%
  {\etoksapp\t_meta_nodes{#1}}

\appendtoks
   \let\nodeMPcode\grph_nodes_code
   \let\mpcode\grph_nodes_code
\to \t_every_meta_nodes

\protect \endinput