Debian Codegraph @

texmf-dist/tex/latex/tikz-feynman/tikzfeynman.patch.3.0.1.lua

raw
1 -- This patched version of the createVertex function simply ensures that the
2 -- path is never empty. This works fine for TikZ version 3.0.1 and 3.0.1a.
3 --
4 -- Later versions (should) have a more thorough patch by Till which is
5 -- incompatible with this override unfortunately.
6 --
7 -- #############################################################################
8 --
9 -- Copyright 2012 by Till Tantau
10 --
11 -- This file may be distributed an/or modified
12 --
13 -- 1. under the LaTeX Project Public License and/or
14 -- 2. under the GNU Public License
15 --
16 -- See the file doc/generic/pgf/licenses/LICENSE for more information
17
18 -- @release $Header: /cvsroot/pgf/pgf/generic/pgf/graphdrawing/lua/pgf/gd/interface/InterfaceToDisplay.lua,v 1.14 2014/03/19 09:20:59 tantau Exp $
19
20 -- Print anything - including nested tables
21 function table_print (tt, indent, done)
22 done = done or {}
23 indent = indent or 0
24 if type(tt) == "table" then
25 for key, value in pairs (tt) do
26 io.write(string.rep (" ", indent)) -- indent it
27 if type (value) == "table" and not done [value] then
28 done [value] = true
29 io.write(string.format("[%s] => table\n", tostring (key)));
30 io.write(string.rep (" ", indent+2)) -- indent it
31 io.write("(\n");
32 table_print (value, indent + 4, done)
33 io.write(string.rep (" ", indent+2)) -- indent it
34 io.write(")\n");
35 else
36 io.write(string.format("[%s] => %s\n",
37 tostring (key), tostring(value)))
38 end
39 end
40 else
41 io.write(tt .. "\n")
42 end
43 end
44
45 ---
46 -- This class provides the interface between a display
47 -- layer (like \tikzname\ or a graph editor) and graph drawing
48 -- system. Another class, |InterfaceToAlgorithms|, binds the algorithm
49 -- layer (which are written in Lua) to the graph drawing system.
50 --
51 -- The functions declared here are independent of the actual display
52 -- layer. Rather, the differences between the layers are encapsulated
53 -- by subclasses of the |Binding| class, see that class for
54 -- details. Thus, when a new display layer is written, the present
55 -- class is \emph{used}, but not \emph{modified}. Instead, only a new
56 -- binding is created and all display layer specific interaction is
57 -- put there.
58 --
59 -- The job of this class is to provide convenient methods that can be
60 -- called by the display layer. For instance, it provides methods for
61 -- starting a graph drawing scope, managing the stack of such scope,
62 -- adding a node to a graph and so on.
63
64 local InterfaceToDisplay = {}
65
66 -- Namespace
67 require("pgf.gd.interface").InterfaceToDisplay = InterfaceToDisplay
68
69
70 -- Imports
71 local InterfaceCore = require "pgf.gd.interface.InterfaceCore"
72 local Scope = require "pgf.gd.interface.Scope"
73
74 local Binding = require "pgf.gd.bindings.Binding"
75
76 local Sublayouts = require "pgf.gd.control.Sublayouts"
77 local LayoutPipeline = require "pgf.gd.control.LayoutPipeline"
78
79 local Digraph = require "pgf.gd.model.Digraph"
80 local Vertex = require "pgf.gd.model.Vertex"
81 local Edge = require "pgf.gd.model.Edge"
82 local Collection = require "pgf.gd.model.Collection"
83
84 local Storage = require "pgf.gd.lib.Storage"
85 local LookupTable = require "pgf.gd.lib.LookupTable"
86 local Event = require "pgf.gd.lib.Event"
87
88 local lib = require "pgf.gd.lib"
89
90
91 -- Forward declarations
92 local get_current_options_table
93 local render_collections
94 local push_on_option_stack
95 local vertex_created
96
97 -- Local objects
98
99 local phase_unique = {} -- a unique handle
100 local collections_unique = {} -- a unique handle
101 local option_cache = nil -- The option cache
102
103
104
105
106 ---
107 -- Initiliaze the binding. This function is called once by the display
108 -- layer at the very beginning. For instance, \tikzname\ does the
109 -- following call:
110 --
111 --\begin{codeexample}[code only, tikz syntax=false]
112 --InterfaceToDisplay.bind(require "pgf.gd.bindings.BindingToPGF")
113 --\end{codeexample}
114 --
115 -- Inside this call, many standard declarations will be executed, that
116 -- is, the declared binding will be used immediately.
117 --
118 -- Subsequently, the |binding| field of the |InterfaceCore| can be used.
119 --
120 -- @param class A subclass of |Binding|.
121
122 function InterfaceToDisplay.bind(class)
123 assert (not InterfaceCore.binding, "binding already initialized")
124
125 -- Create a new object
126 InterfaceCore.binding = setmetatable({}, class)
127
128 -- Load these libraries, which contain many standard declarations:
129 require "pgf.gd.model.library"
130 require "pgf.gd.control.library"
131 end
132
133
134
135
136 ---
137 -- Start a graph drawing scope. Note that this is not the same as
138 -- starting a subgraph / sublayout, which are local to a graph drawing
139 -- scope: When a new graph drawing scope is started, it is pushed on
140 -- top of a stack of graph drawing scopes and all other ``open''
141 -- scopes are no longer directly accessible. All method calls to an
142 -- |Interface...| object will refer to this newly created scope until
143 -- either a new scope is opened or until the current scope is closed
144 -- once more.
145 --
146 -- Each graph drawing scope comes with a syntactic digraph that is
147 -- build using methods like |addVertex| or |addEdge|.
148 --
149 -- @param height The to-be-used height of the options stack. All
150 -- options above this height will be popped prior to attacking the
151 -- options to the syntactic digraph.
152
153 function InterfaceToDisplay.beginGraphDrawingScope(height)
154
155 -- Create a new scope table
156 local scope = Scope.new {}
157
158 -- Setup syntactic digraph:
159 local g = scope.syntactic_digraph
160
161 g.options = get_current_options_table(height)
162 g.syntactic_digraph = g
163 g.scope = scope
164
165 -- Push scope:
166 InterfaceCore.scopes[#InterfaceCore.scopes + 1] = scope
167 end
168
169
170
171 ---
172 -- Arranges the current graph using the specified algorithm and options.
173 --
174 -- This function should be called after the graph drawing scope has
175 -- been opened and the syntactic digraph has been completely
176 -- specified. It will now start running the algorithm specified
177 -- through the |algorithm_phase| options.
178 --
179 -- Internally, this function creates a coroutine that will run the current graph
180 -- drawing algorithm. Coroutines are needed since a graph drawing
181 -- algorithm may choose to create a new node. In this case, the
182 -- algorithm needs to be suspended and control must be returned back
183 -- to the display layer, so that the node can be typeset in order to
184 -- determine the precise size information. Once this is done, control
185 -- must be passed back to the exact point inside the algorithm where
186 -- the node was created. Clearly, all of these actions are exactly
187 -- what coroutines are for.
188 --
189 -- @return Time it took to run the algorithm
190
191 function InterfaceToDisplay.runGraphDrawingAlgorithm()
192
193 -- Time things
194 local start = os.clock()
195
196 -- Setup
197 local scope = InterfaceCore.topScope()
198 assert(not scope.coroutine, "coroutine already created for current gd scope")
199
200 -- The actual drawing function
201 local function run ()
202 if #scope.syntactic_digraph.vertices == 0 then
203 -- Nothing needs to be done
204 return
205 end
206
207 LayoutPipeline.run(scope)
208 end
209
210 scope.coroutine = coroutine.create(run)
211
212 -- Run it:
213 InterfaceToDisplay.resumeGraphDrawingCoroutine()
214
215 -- End timing:
216 local stop = os.clock()
217
218 return stop - start
219 end
220
221
222 ---
223 -- Resume the graph drawing coroutine.
224 --
225 -- This function is the work horse of the coroutine management. It
226 -- gets called whenever control passes back from the display layer to
227 -- the algorithm level. We resume the graph drawing coroutine so that the
228 -- algorithm can start/proceed. The tricky part is when the algorithm
229 -- yields, but is not done. In this case, the code needed for creating
230 -- a new node is passed back to the display layer through the binding,
231 -- which must then execute the code and then resuming the coroutine.
232 --
233 function InterfaceToDisplay.resumeGraphDrawingCoroutine()
234
235 -- Setup
236 local scope = InterfaceCore.topScope()
237 local binding = InterfaceCore.binding
238
239 -- Asserts
240 assert(scope.coroutine, "coroutine not created for current gd scope")
241
242 -- Run
243 local ok, text = coroutine.resume(scope.coroutine)
244 assert(ok, text)
245 if coroutine.status(scope.coroutine) ~= "dead" then
246 -- Ok, ask binding to continue
247 binding:resumeGraphDrawingCoroutine(text)
248 end
249 end
250
251
252
253 --- Ends the current graph drawing scope.
254 --
255 function InterfaceToDisplay.endGraphDrawingScope()
256 assert(#InterfaceCore.scopes > 0, "no gd scope open")
257 InterfaceCore.scopes[#InterfaceCore.scopes] = nil -- pop
258 end
259
260
261
262
263 ---
264 -- Creates a new vertex in the syntactic graph of the current graph
265 -- drawing scope. The display layer should call this function for each
266 -- node of the graph. The |name| must be a unique string identifying
267 -- the node. The newly created vertex will be added to the syntactic
268 -- digraph. The binding function |everyVertexCreation| will then be
269 -- called, allowing the binding to store information regarding the newly
270 -- created vertex.
271 --
272 -- For each vertex an event will be created in the event
273 -- sequence. This event will have the kind |"node"| and its
274 -- |parameter| will be the vertex.
275 --
276 -- @param name Name of the vertex.
277 --
278 -- @param shape The shape of the vertex such as |"circle"| or
279 -- |"rectangle"|. This shape may help a graph drawing algorithm
280 -- figuring out how the node should be placed.
281 --
282 -- @param path A |Path| object representing the vertex's path.
283 --
284 -- @param height The to-be-used height of the options stack. All
285 -- options above this height will be popped prior to attacking the
286 -- options to the syntactic digraph.
287 --
288 -- @param binding_infos These options are passed to and are specific
289 -- to the current |Binding|.
290 --
291 -- @param anchors A table of anchors (mapping anchor positions to
292 -- |Coordinates|).
293
294
295 function InterfaceToDisplay.createVertex(name, shape, path, height, binding_infos, anchors)
296
297 -- The path should never be empty, so we create a trivial path in the provided
298 -- path is empty. This occurs with the ‘coordinate‘ shape for example.
299 if #path == 0 then
300 path:appendMoveto(0, 0)
301 path:appendClosepath()
302 end
303
304 -- Setup
305 local scope = InterfaceCore.topScope()
306 local binding = InterfaceCore.binding
307
308 -- Does vertex already exist?
309 local v = scope.node_names[name]
310 assert (not v or not v.created_on_display_layer, "node already created")
311
312 -- Create vertex
313 if not v then
314 v = Vertex.new {
315 name = name,
316 shape = shape,
317 kind = "node",
318 path = path,
319 options = get_current_options_table(height),
320 anchors = anchors,
321 }
322
323 vertex_created(v,scope)
324 else
325 assert(v.kind == "subgraph node", "subgraph node expected")
326 v.shape = shape
327 v.path = path
328 v.anchors = anchors
329 end
330
331 v.created_on_display_layer = true
332
333 -- Call binding
334 binding.storage[v] = binding_infos
335 binding:everyVertexCreation(v)
336 end
337
338
339 -- This is a helper function
340 function vertex_created(v,scope)
341
342 -- Create Event
343 local e = InterfaceToDisplay.createEvent ("node", v)
344 v.event = e
345
346 -- Create name lookup
347 scope.node_names[v.name] = v
348
349 -- Add vertex to graph
350 scope.syntactic_digraph:add {v}
351
352 -- Add to collections
353 for _,c in ipairs(v.options.collections) do
354 LookupTable.addOne(c.vertices, v)
355 end
356
357 end
358
359
360
361 ---
362 -- Creates a new vertex in the syntactic graph of the current graph
363 -- drawing scope that is a subgraph vertex. Such a vertex
364 -- ``surrounds'' the vertices of a subgraph. The special property of a
365 -- subgraph node opposed to a normal node is that it is created only
366 -- after the subgraph has been laid out. However, the difference to a
367 -- collection like |hyper| is that the node is availble immediately as
368 -- a normal node in the sense that you can connect edges to it.
369 --
370 -- What happens internally is that subgraph nodes get ``registered''
371 -- immediately both on the display level and on the algorithm level,
372 -- but the actual node is only created inside the layout pipeline
373 -- using a callback of the binding. The present function is used to
374 -- perform this registering. The node creation happens when the
375 -- innermost layout in which the subgraph node is declared has
376 -- finished. For each subgraph node, a collection is created that
377 -- contains all vertices (and edges) being part of the subgraph. For
378 -- this reason, this method is a |push...| method, since it pushes
379 -- something on the options stack.
380 --
381 -- The |init| parameter will be used during the creation of the node,
382 -- see |Binding:createVertex| for details on the fields. Note that
383 -- |init.text| is often not displayed for such ``vast'' nodes as those
384 -- created for whole subgraphs, but a shape may use it nevertheless
385 -- (for instance, one might display this text at the top of the node
386 -- or, in case of a \textsc{uml} package, in a special box above the
387 -- actual node).
388 --
389 -- The |init.generated_options| will be augmented by additional
390 -- key--value pairs when the vertex is created:
391 --
392 -- \begin{itemize}
393 -- \item The key |subgraph point cloud| will have as its value a
394 -- string that is be a list of points (without
395 -- separating commas) like |"(10pt,20pt)(0pt,0pt)(30pt,40pt)"|, always in
396 -- this syntax. The list will contain all points inside the
397 -- subgraph. In particular, a bounding box around these points will
398 -- encompass all nodes and bend points of the subgraph.
399 -- The bounding box of this point cloud is guaranteed to be centered on
400 -- the origin.
401 -- \item The key |subgraph bounding box width| will have as its value
402 -- the width of a bounding box (in \TeX\ points, as a string with the
403 -- suffix |"pt"|).
404 -- \item The key |subgraph bounding box height| stores the height of a
405 -- bounding box.
406 -- \end{itemize}
407 --
408 -- @param name The name of the node.
409 -- @param height Height of the options stack. Note that this method
410 -- pushes something (namely a collection) on the options stack.
411 -- @param info A table passed to |Binding:createVertex|, see that function.
412 --
413 function InterfaceToDisplay.pushSubgraphVertex(name, height, info)
414
415 -- Setup
416 local scope = InterfaceCore.topScope()
417 local binding = InterfaceCore.binding
418
419 -- Does vertex already exist?
420 assert (not scope.node_names[name], "node already created")
421
422 -- Create vertex
423 local v = Vertex.new {
424 name = name,
425 kind = "subgraph node",
426 options = get_current_options_table(height-1)
427 }
428
429 vertex_created(v,scope)
430
431 -- Store info
432 info.generated_options = info.generated_options or {}
433 info.name = name
434 v.subgraph_info = info
435
436 -- Create collection and link it to v
437 local _, _, entry = InterfaceToDisplay.pushOption(InterfaceCore.subgraph_node_kind, nil, height)
438 v.subgraph_collection = entry.value
439 v.subgraph_collection.subgraph_node = v
440
441 -- Find parent collection in options stack:
442 local collections = v.options.collections
443 for i=#collections,1,-1 do
444 if collections[i].kind == InterfaceCore.sublayout_kind then
445 v.subgraph_collection.parent_layout = collections[i]
446 break
447 end
448 end
449 end
450
451
452
453 ---
454 -- Add options for an already existing vertex.
455 --
456 -- This function allows you to add options to an already existing
457 -- vertex. The options that will be added are all options on the
458 -- current options stack; they will overwrite existing options of the
459 -- same name. For collections, the vertex stays in all collections it
460 -- used to, it is only added to all collections that are currently on
461 -- the options stack.
462 --
463 -- @param name Name of the vertex.
464 -- @param height The option stack height.
465
466 function InterfaceToDisplay.addToVertexOptions(name, height)
467
468 -- Setup
469 local scope = InterfaceCore.topScope()
470
471 -- Does vertex already exist?
472 local v = assert (scope.node_names[name], "node is missing, cannot add options")
473
474 v.options = get_current_options_table(height, v.options)
475
476 -- Add to collections
477 for _,c in ipairs(v.options.collections) do
478 LookupTable.addOne(c.vertices, v)
479 end
480
481 end
482
483
484
485
486
487 ---
488 -- Creates a new edge in the syntactic graph of the current graph
489 -- drawing scope. The display layer should call this function for each
490 -- edge that is created. Both the |from| vertex and the |to| vertex
491 -- must exist (have been created through |createVertex|) prior to your
492 -- being able to call this function.
493 --
494 -- After the edge has been created, the binding layer's function
495 -- |everyEdgeCreation| will be called, allowing the binding layer to
496 -- store information about the edge.
497 --
498 -- For each edge an event is created, whose kind is |"edge"| and whose
499 -- |parameter| is a two-element array whose first entry is the edge's
500 -- arc in the syntactic digraph and whose second entry is the position
501 -- of the edge in the arc's array of syntactic edges.
502 --
503 -- @param tail Name of the node the edge begins at.
504 -- @param head Name of the node the edge ends at.
505 -- @param direction Direction of the edge (e.g. |--| for an undirected edge
506 -- or |->| for a directed edge from the first to the second
507 -- node).
508 -- @param height The option stack height, see for instance |createVertex|.
509 --
510 -- @param binding_infos These options will be stored in the |storage|
511 -- of the vertex at the field index by the binding.
512
513 function InterfaceToDisplay.createEdge(tail, head, direction, height, binding_infos)
514
515 -- Setup
516 local scope = InterfaceCore.topScope()
517 local binding = InterfaceCore.binding
518
519 -- Does vertex already exist?
520 local h = scope.node_names[head]
521 local t = scope.node_names[tail]
522 assert (h and t, "attempting to create edge between nodes that are not in the graph")
523
524 -- Create Arc object
525 local arc = scope.syntactic_digraph:connect(t, h)
526
527 -- Create Edge object
528 local edge = Edge.new {
529 head = h,
530 tail = t,
531 direction = direction,
532 options = get_current_options_table(height)
533 }
534
535 -- Add to arc
536 arc.syntactic_edges[#arc.syntactic_edges+1] = edge
537
538 -- Create Event
539 local e = InterfaceToDisplay.createEvent ("edge", { arc, #arc.syntactic_edges })
540 edge.event = e
541
542 -- Make part of collections
543 for _,c in ipairs(edge.options.collections) do
544 LookupTable.addOne(c.edges, edge)
545 end
546
547 -- Call binding
548 binding.storage[edge] = binding_infos
549 binding:everyEdgeCreation(edge)
550
551 end
552
553
554
555
556
557 ---
558 -- Push an option to the stack of options.
559 --
560 -- As a graph is parsed, a stack of ``current options''
561 -- is created. To add something to this table, the display layers may
562 -- call the method |pushOption|. To pop something from this stack,
563 -- just set the |height| value during the next push to the position to
564 -- which you actually wish to push something; everything above and
565 -- including this position will be popped from the stack.
566 --
567 -- When an option is pushed, several additional options may also be
568 -- pushed, namely whenever the option has a |use| field set. These
569 -- additional options may, in turn, also push new options. Because of
570 -- this, this function returns a new stack height, representing the
571 -- resulting stack height.
572 --
573 -- In addition to this stack height, this function returns a Boolean
574 -- value indicating whether a ``main algorithm phase was set.'' This
575 -- happens whenever a key is executed (directly or indirectly through
576 -- the |use| field) that selects an algorithm for the ``main''
577 -- algorithm phase. This information may help the caller to setup the
578 -- graph drawing scopes correctly.
579 --
580 -- @param key A parameter (must be a string).
581 -- @param value A value (can be anything). If it is a string, it will
582 -- be converted to whatever the key expects.
583 -- @param height A stack height at which to insert the key. Everything
584 -- above this height will be removed.
585 --
586 -- @return A new stack height
587 -- @return A Boolean that is |true| iff the main algorithm phase was
588 -- set by the option or one option |use|d by it.
589 -- @return The newly created entry on the stack. If more entries are
590 -- created through the use of the |use| field, the original entry is
591 -- returned nevertheless.
592
593
594 function InterfaceToDisplay.pushOption(key, value, height)
595 assert(type(key) == "string", "illegal key")
596
597 local key_record = assert(InterfaceCore.keys[key], "unknown key")
598 local main_phase_set = false
599
600 if value == nil and key_record.default then
601 value = key_record.default
602 end
603
604 -- Find out what kind of key we are pushing:
605
606 if key_record.algorithm then
607 -- Push a phase
608 if type(InterfaceCore.algorithm_classes[key]) == "function" then
609 -- Call the constructor function
610 InterfaceCore.algorithm_classes[key] = InterfaceCore.algorithm_classes[key]()
611 end
612
613 local algorithm = InterfaceCore.algorithm_classes[key]
614
615 assert (algorithm, "algorithm class not found")
616
617 push_on_option_stack(phase_unique,
618 { phase = value or key_record.phase, algorithm = algorithm },
619 height)
620
621 if key_record.phase == "main" then
622 main_phase_set = true
623 end
624
625 elseif key_record.layer then
626 -- Push a collection
627 local stack = InterfaceCore.option_stack
628 local scope = InterfaceCore.topScope()
629
630 -- Get the stack above "height":
631 local options = get_current_options_table(height-1)
632
633 -- Create the collection event
634 local event = InterfaceToDisplay.createEvent ("collection", key)
635
636 -- Create collection object:
637 local collection = Collection.new { kind = key, options = options, event = event }
638
639 -- Store in collections table of current scope:
640 local collections = scope.collections[key] or {}
641 collections[#collections + 1] = collection
642 scope.collections[key] = collections
643
644 -- Build collection tree
645 collection:registerAsChildOf(options.collections[#options.collections])
646
647 -- Push on stack
648 push_on_option_stack(collections_unique, collection, height)
649
650 else
651
652 -- A normal key
653 push_on_option_stack(key, InterfaceCore.convert(value, InterfaceCore.keys[key].type), height)
654
655 end
656
657 local newly_created = InterfaceCore.option_stack[#InterfaceCore.option_stack]
658
659 -- Now, push use keys:
660 local use = key_record.use
661 if key_record.use then
662 local flag
663 for _,u in ipairs(InterfaceCore.keys[key].use) do
664 local use_k = u.key
665 local use_v = u.value
666 if type(use_k) == "function" then
667 use_k = use_k(value)
668 end
669 if type(use_v) == "function" then
670 use_v = use_v(value)
671 end
672 height, flag = InterfaceToDisplay.pushOption(use_k, use_v, height+1)
673 main_phase_set = main_phase_set or flag
674 end
675 end
676
677 return height, main_phase_set, newly_created
678 end
679
680
681 ---
682 -- Push a layout on the stack of options. As long as this layout is on
683 -- the stack, all vertices and edges will be part of this layout. For
684 -- details on layouts, please see |Sublayouts|.
685 --
686 -- @param height A stack height at which to insert the key. Everything
687 -- above this height will be removed.
688
689 function InterfaceToDisplay.pushLayout(height)
690 InterfaceToDisplay.pushOption(InterfaceCore.sublayout_kind, nil, height)
691 end
692
693
694
695 ---
696 -- Creates an event and adds it to the event string of the current scope.
697 --
698 -- @param kind Name/kind of the event.
699 -- @param parameters Parameters of the event.
700 --
701 -- @return The newly pushed event
702 --
703 function InterfaceToDisplay.createEvent(kind, param)
704 local scope = InterfaceCore.topScope()
705 local n = #scope.events + 1
706 local e = Event.new { kind = kind, parameters = param, index = n }
707 scope.events[n] = e
708
709 return e
710 end
711
712
713
714 ---
715 -- This method allows you to query the table of all declared keys. It
716 -- contains them both as an array and also as a table index by the
717 -- keys's names. In particular, you can then iterate over it using
718 -- |ipairs| and you can check whether a key is defined by accessing
719 -- the table at the key's name. Each entry of the table is the
720 -- original table passed to |InterfaceToAlgorithms.declare|.
721 --
722 -- @return A lookup table of all declared keys.
723
724 function InterfaceToDisplay.getDeclaredKeys()
725 return InterfaceCore.keys
726 end
727
728
729
730
731 ---
732 -- Renders the graph.
733 --
734 -- This function is called after the graph has been laid out by the
735 -- graph drawing algorithms. It will trigger a sequence of calls to
736 -- the binding layer that will, via callbacks, start rendering the
737 -- whole graph.
738 --
739 -- In detail, this function calls:
740 --
741 --\begin{codeexample}[code only, tikz syntax=false]
742 --local binding = InterfaceCore.binding
743 --
744 --binding:renderStart()
745 --render_vertices()
746 --render_edges()
747 --render_collections()
748 --binding:renderStop()
749 --\end{codeexample}
750 --
751 -- Here, the |render_...| functions are local, internal functions that are,
752 -- nevertheless, documented here.
753 --
754 -- @param name Returns the algorithm class that has been declared using
755 -- |declare| under the given name.
756
757 function InterfaceToDisplay.renderGraph()
758 local scope = InterfaceCore.topScope()
759 local syntactic_digraph = scope.syntactic_digraph
760
761 local binding = InterfaceCore.binding
762
763 binding:renderStart()
764 render_vertices(syntactic_digraph.vertices)
765 render_edges(syntactic_digraph.arcs)
766 render_collections(scope.collections)
767 binding:renderStop()
768 end
769
770
771
772
773
774 ---
775 -- Render the vertices after the graph drawing algorithm has
776 -- finished. This function is local and internal and included only for
777 -- documenting the call graph.
778 --
779 -- When the graph drawing algorithm is done, the interface will start
780 -- rendering the vertices by calling appropriate callbacks of the
781 -- binding layer.
782 --
783 -- Consider the following code:
784 --\begin{codeexample}[code only]
785 --\graph [... layout] {
786 -- a -- b -- c -- d;
787 --};
788 --\end{codeexample}
789 --
790 -- In this case, after the graph drawing algorithm has run, the
791 -- present function will call:
792 --
793 --\begin{codeexample}[code only, tikz syntax=false]
794 --local binding = InterfaceCore.binding
795 --
796 --binding:renderVerticesStart()
797 --binding:renderVertex(vertex_a)
798 --binding:renderVertex(vertex_b)
799 --binding:renderVertex(vertex_c)
800 --binding:renderVertex(vertex_d)
801 --binding:renderVerticesStop()
802 --\end{codeexample}
803 --
804 -- @param vertices An array of all vertices in the syntactic digraph.
805
806 function render_vertices(vertices)
807 InterfaceCore.binding:renderVerticesStart()
808 for _,vertex in ipairs(vertices) do
809 InterfaceCore.binding:renderVertex(vertex)
810 end
811 InterfaceCore.binding:renderVerticesStop()
812 end
813
814
815 ---
816 -- Render the collections whose layer is not |0|. This local, internal
817 -- function is called to render the different collection kinds.
818 --
819 -- Collection kinds rendered in the order provided by the |layer|
820 -- field passed to |declare| during the declaration of the colleciton
821 -- kind, see also |declare_collection|. If several collection kinds
822 -- have the same layer, they are rendered in lexicographical ordering
823 -- (to ensure that they are always rendered in the same order).
824 --
825 -- Consider the following code:
826 --\begin{codeexample}[code only, tikz syntax=false]
827 --declare { key = "hyper", layer = 1 }
828 --\end{codeexample}
829 -- you can say on the \tikzname\ layer
830 --\begin{codeexample}[code only]
831 --\graph {
832 -- a, b, c, d;
833 -- { [hyper] a, b, c }
834 -- { [hyper] b, c, d }
835 --};
836 --\end{codeexample}
837 --
838 -- In this case, after the graph drawing algorithm has run, the
839 -- present function will call:
840 --
841 --\begin{codeexample}[code only, tikz syntax=false]
842 --local binding = InterfaceCore.binding
843 --
844 --binding:renderCollectionStartKind("hyper", 1)
845 --binding:renderCollection(collection_containing_abc)
846 --binding:renderCollection(collection_containing_bcd)
847 --binding:renderCollectionStopKind("hyper", 1)
848 --\end{codeexample}
849 --
850 -- @param collections The |collections| table of the current scope.
851
852 function render_collections(collections)
853 local kinds = InterfaceCore.collection_kinds
854 local binding = InterfaceCore.binding
855
856 for i=1,#kinds do
857 local kind = kinds[i].kind
858 local layer = kinds[i].layer
859
860 if layer ~= 0 then
861 binding:renderCollectionStartKind(kind, layer)
862 for _,c in ipairs(collections[kind] or {}) do
863 binding:renderCollection(c)
864 end
865 binding:renderCollectionStopKind(kind, layer)
866 end
867 end
868 end
869
870
871 ---
872 -- Render the syntactic edges of a graph after the graph drawing
873 -- algorithm has finished. This function is local and internal and included only
874 -- for documenting the call graph.
875 --
876 -- When the graph drawing algorithm is done, the interface will first
877 -- rendering the vertices using |render_vertices|, followed by calling
878 -- this function, which in turn calls appropriate callbacks to the
879 -- binding layer.
880 --
881 -- Consider the following code:
882 --\begin{codeexample}[code only]
883 -- \graph [... layout] {
884 -- a -- b -- c -- d;
885 -- };
886 --\end{codeexample}
887 --
888 -- In this case, after the graph drawing algorithm has run, the
889 -- present function will call:
890 --
891 --\begin{codeexample}[code only, tikz syntax=false]
892 -- local binding = InterfaceCore.binding
893 --
894 -- binding:renderEdgesStart()
895 -- binding:renderEdge(edge_from_a_to_b)
896 -- binding:renderEdge(edge_from_b_to_c)
897 -- binding:renderEdge(edge_from_c_to_d)
898 -- binding:renderEdgesStop()
899 --\end{codeexample}
900 --
901 -- @param arcs The array of arcs of the syntactic digraph.
902
903 function render_edges(arcs)
904 InterfaceCore.binding:renderEdgesStart()
905 for _,a in ipairs(arcs) do
906 for _,e in ipairs (a.syntactic_edges) do
907 InterfaceCore.binding:renderEdge(e)
908 end
909 end
910 InterfaceCore.binding:renderEdgesStop()
911 end
912
913
914 local aliases = InterfaceCore.option_aliases
915 local option_initial = InterfaceCore.option_initial
916
917 local option_metatable = {
918 __index =
919 function (t, key)
920 local k = aliases[key]
921 if k then
922 local v = (type(k) == "string" and t[k]) or (type(k) == "function" and k(t)) or nil
923 if v ~= nil then
924 return v
925 end
926 end
927 return option_initial[key]
928 end
929 }
930
931
932 ---
933 -- Get the current options table.
934 --
935 -- An option table can be accessed like a normal table; however, there
936 -- is a global fallback for this table. If an index is not defined,
937 -- the value of this index in the global fallback table is used. (This
938 -- reduces the overall amount of option keys that need to be stored
939 -- with object.)
940 --
941 -- (This function is local and internal and included only for documentation
942 -- purposes.)
943 --
944 -- @param height The stack height for which the option table is
945 -- required.
946 -- @param table If non |nil|, the options will be added to this
947 -- table.
948 --
949 -- @return The option table as described above.
950
951 function get_current_options_table (height, table)
952 local stack = InterfaceCore.option_stack
953 assert (height >= 0 and height <= #stack, "height value out of bounds")
954
955 if height == InterfaceCore.option_cache_height and not table then
956 return option_cache
957 else
958 -- Clear superfluous part of stack
959 for i=#stack,height+1,-1 do
960 stack[i] = nil
961 end
962
963 -- Build options table
964 local cache
965 if not table then
966 cache = setmetatable(
967 {
968 algorithm_phases = setmetatable({}, InterfaceCore.option_initial.algorithm_phases),
969 collections = {}
970 }, option_metatable)
971 else
972 cache = lib.copy(table)
973 cache.algorithm_phases = lib.copy(cache.algorithm_phases)
974 cache.collections = lib.copy(cache.collections)
975 end
976
977 local algorithm_phases = cache.algorithm_phases
978 local collections = cache.collections
979 local keys = InterfaceCore.keys
980
981 local function handle (k, v)
982 if k == phase_unique then
983 algorithm_phases[v.phase] = v.algorithm
984 local phase_stack = v.phase .. " stack"
985 local t = rawget(algorithm_phases, phase_stack)
986 if not t then
987 t = algorithm_phases[phase_stack]
988 assert(type(t) == "table", "unknown phase")
989 t = lib.copy(t)
990 algorithm_phases[phase_stack] = t
991 end
992 t[#t + 1] = v.algorithm
993 elseif k == collections_unique then
994 LookupTable.addOne(collections, v)
995 else
996 cache[k] = v
997 end
998 end
999
1000 for _,s in ipairs(stack) do
1001 handle (s.key, s.value)
1002 end
1003
1004 -- Cache it, if this was not added:
1005 if not table then
1006 InterfaceCore.option_cache_height = height
1007 option_cache = cache
1008 end
1009
1010 return cache
1011 end
1012 end
1013
1014
1015
1016 -- A helper function
1017
1018 function push_on_option_stack(key, value, height)
1019 local stack = InterfaceCore.option_stack
1020
1021 assert (type(height) == "number" and height > 0 and height <= #stack + 1,
1022 "height value out of bounds")
1023
1024 -- Clear superfluous part of stack
1025 for i=#stack,height+1,-1 do
1026 stack[i] = nil
1027 end
1028
1029 stack[height] = { key = key, value = value }
1030 InterfaceCore.option_cache_height = nil -- invalidate cache
1031 end
1032
1033
1034
1035 -- Done
1036
1037 return InterfaceToDisplay