summaryrefslogtreecommitdiff
path: root/Master/texmf-dist/tex/generic/pgf/graphdrawing/lua/pgf/gd/bindings/BindingToPGF.lua
blob: 90519e23d5503c4b7789bed1b211973543946c06 (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
-- Copyright 2012 by Till Tantau
--
-- This file may be distributed an/or modified
--
-- 1. under the LaTeX Project Public License and/or
-- 2. under the GNU Public License
--
-- See the file doc/generic/pgf/licenses/LICENSE for more information

-- @release $Header$



-- Imports
local Storage = require "pgf.gd.lib.Storage"


---
-- This class, which is a subclass of |Binding|, binds the graph
-- drawing system to the \pgfname\ display system by overriding (that
-- is, implementing) the methods of the |Binding| class. As a typical
-- example, consider the implementation of the function |renderVertex|:
--
--\begin{codeexample}[code only, tikz syntax=false]
--function BindingToPGF:renderVertex(v)
--  local info = assert(self.infos[v], "thou shalt not modify the syntactic digraph")
--  tex.print(
--    string.format(
--      "\\pgfgdcallbackrendernode{%s}{%fpt}{%fpt}{%fpt}{%fpt}{%fpt}{%fpt}{%s}",
--      'not yet positionedPGFINTERNAL' .. v.name,
--      info.x_min,
--      info.x_max,
--      info.y_min,
--      info.y_max,
--      v.pos.x,
--      v.pos.y,
--      info.box_count))
--end
--\end{codeexample}
--
-- As can be seen, the main job of this function is to call a function
-- on the \TeX\ layer that is called |\pgfgdcallbackrendernode|, which gets
-- several parameters like the name of the to-be-rendered node or the
-- (new) position for the node. For almost all methods of the
-- |Binding| class there is a corresponding ``callback'' macro on the
-- \TeX\ layer, all of which are implemented in the \pgfname\ library
-- |graphdrawing|. For details on these callbacks, 
-- please consult the code of that file and of the class
-- |BindingToPGF| (they are not documented here since they are local
-- to the binding and should not be called by anyone other than the
-- binding class).

local BindingToPGF = {
  storage = Storage.newTableStorage () -- overwrite default storage
}
BindingToPGF.__index = BindingToPGF
setmetatable(BindingToPGF, require "pgf.gd.bindings.Binding") -- subclass of Binding


-- Namespace
require("pgf.gd.bindings").BindingToPGF = BindingToPGF

-- Imports
local lib = require "pgf.gd.lib"

local Coordinate = require "pgf.gd.model.Coordinate"
local Path = require "pgf.gd.model.Path"

-- The implementation

-- Forward
local table_in_pgf_syntax
local animations_in_pgf_syntax
local path_in_pgf_syntax
local coordinate_in_pgf_syntax




-- Scope handling

function BindingToPGF:resumeGraphDrawingCoroutine(text)
  tex.print(text)
  tex.print("\\pgfgdresumecoroutinetrue") 
end


-- Declarations

function BindingToPGF:declareCallback(t)
  tex.print("\\pgfgdcallbackdeclareparameter{" .. t.key .. "}{" .. (t.type or "nil") .. "}")
end



-- Rendering

function BindingToPGF:renderStart()
  tex.print("\\pgfgdcallbackbeginshipout")
end

function BindingToPGF:renderStop()
  tex.print("\\pgfgdcallbackendshipout")
end


-- Rendering collections

function BindingToPGF:renderCollection(collection)
  tex.print("\\pgfgdcallbackrendercollection{".. collection.kind .. "}{"
	    .. table_in_pgf_syntax(collection.generated_options) .. "}")
end

function BindingToPGF:renderCollectionStartKind(kind, layer)
  tex.print("\\pgfgdcallbackrendercollectionkindstart{" .. kind .. "}{" .. tostring(layer) .. "}")
end

function BindingToPGF:renderCollectionStopKind(kind, layer)
  tex.print("\\pgfgdcallbackrendercollectionkindstop{" .. kind .. "}{" .. tostring(layer) .. "}")
end

-- Printing points

local function to_pt(x)
  return string.format("%.12fpt", x)
end


-- Managing vertices (pgf nodes)

local boxes = {}
local box_count = 0

function BindingToPGF:everyVertexCreation(v)
  local info = self.storage[v]
  
  -- Save the box!
  box_count = box_count + 1
  boxes[box_count] = node.copy_list(tex.box[info.tex_box_number])
  
  -- Special tex stuff, should not be considered by gd algorithm
  info.box_count = box_count
end

function BindingToPGF:renderVertex(v)
  local info = assert(self.storage[v], "thou shalt not modify the syntactic digraph")
  tex.print(
    string.format(
      "\\pgfgdcallbackrendernode{%s}{%.12fpt}{%.12fpt}{%.12fpt}{%.12fpt}{%.12fpt}{%.12fpt}{%s}{%s}",
      'not yet positionedPGFINTERNAL' .. v.name,
      info.x_min,
      info.x_max,
      info.y_min,
      info.y_max,
      v.pos.x,
      v.pos.y,
      info.box_count,
      animations_in_pgf_syntax(v.animations)))
end

function BindingToPGF:retrieveBox(index, box_num)
  tex.box[box_num] = assert(boxes[index], "no box stored at given index")
  boxes[index] = nil -- remove from memory
end

function BindingToPGF:renderVerticesStart()
  tex.print("\\pgfgdcallbackbeginnodeshipout")
end

function BindingToPGF:renderVerticesStop()
  tex.print("\\pgfgdcallbackendnodeshipout")
end


local function rigid(x)
  if type(x) == "function" then
    return x()
  else
    return x
  end
end

  
-- Managing edges

function BindingToPGF:renderEdge(e)
  local info = assert(self.storage[e], "thou shalt not modify the syntactic digraph")
  
  local function get_anchor(e, anchor)
    local a = e.options[anchor]
    if a and a ~= "" then
      return "." .. a
    else
      return ""
    end
  end
  
  local callback = {
    '\\pgfgdcallbackedge',
    '{', e.tail.name .. get_anchor(e, "tail anchor"), '}',
    '{', e.head.name .. get_anchor(e, "head anchor"), '}',
    '{', e.direction,  '}',
    '{', info.pgf_options or "",  '}',
    '{', info.pgf_edge_nodes or "", '}',
    '{', table_in_pgf_syntax(e.generated_options), '}',
    '{'
  }
  
  local i = 1
  while i <= #e.path do
    local c = e.path[i]
    assert (type(c) == "string", "illegal path operand")
    
    if c == "lineto" then
      i = i + 1
      local d = rigid(e.path[i])
      callback [#callback + 1] = '--(' .. to_pt(d.x) .. ',' .. to_pt(d.y) .. ')'
      i = i + 1
    elseif c == "moveto" then
      i = i + 1
      local d = rigid(e.path[i])
      callback [#callback + 1] = '(' .. to_pt(d.x) .. ',' .. to_pt(d.y) .. ')'
      i = i + 1
    elseif c == "closepath" then
      callback [#callback + 1] = '--cycle'
      i = i + 1
    elseif c == "curveto" then
      local d1, d2, d3 = rigid(e.path[i+1]), rigid(e.path[i+2]), rigid(e.path[i+3])
      i = i + 3
      callback [#callback + 1] = '..controls(' .. to_pt(d1.x) .. ',' .. to_pt(d1.y) .. ')and('
                                               .. to_pt(d2.x) .. ',' .. to_pt(d2.y) .. ')..'
      callback [#callback + 1] = '(' .. to_pt(d3.x) .. ',' .. to_pt(d3.y) .. ')'
      i = i + 1
    else				     
      error("illegal operation in edge path")
    end
  end

  callback [#callback + 1] = '}'
  callback [#callback + 1] = '{' .. animations_in_pgf_syntax(e.animations) .. '}'
  
  -- hand TikZ code over to TeX
  tex.print(table.concat(callback))
end
      
      
function BindingToPGF:renderEdgesStart()
  tex.print("\\pgfgdcallbackbeginedgeshipout")
end

function BindingToPGF:renderEdgesStop()
  tex.print("\\pgfgdcallbackendedgeshipout")
end


-- Vertex creation

function BindingToPGF:createVertex(init)
  -- Now, go back to TeX...
 coroutine.yield(
   table.concat({
      "\\pgfgdcallbackcreatevertex{", init.name, "}",
      "{", init.shape, "}",
      "{", table_in_pgf_syntax(init.generated_options), ",", init.pgf_options or "", "}",
      "{", (init.text or ""), "}"
    }))
  -- ... and come back with a new node!
end



-- Local helpers

function table_in_pgf_syntax (t)
  local prefix = "/graph drawing/"
  local suffix = "/.try"
  return table.concat( lib.imap( t, function(table)
	   if table.value then
	     return prefix .. table.key .. suffix .. "={" .. tostring(table.value) .. "}"
	   else
	     return prefix .. table.key .. suffix
	   end
	 end), ",")
end


function animations_in_pgf_syntax (a)
  return
    table.concat(
      lib.imap(
	a,
	function(animation) 
	  return "\\pgfanimateattribute{" .. animation.attribute .. "}{whom=pgf@gd," ..
	    table.concat(
	      lib.imap (
		animation.entries,
		function (entry)
		  return "entry={" .. entry.t .. "s}{" .. to_pgf(entry.value) .. "}"
		end
	      ), ",") ..
	    "," ..
	    table.concat(
	      lib.imap(
		animation.options or {},
		function(table)
		  if table.value then
		    return table.key .. "={" .. to_pgf(table.value) .. "}"
		  else
		    return table.key
		  end
	      end), ",")
	    .. "}"
	end)
    )
end


function to_pgf(x)
  if type (x) == "table" then
    if getmetatable(x) == Coordinate then
      return coordinate_in_pgf_syntax(x)
    elseif getmetatable(x) == Path then
      return path_in_pgf_syntax(x)
    else
      error("illegal table in value of a key to be passed back to pgf")
    end
  else
    return tostring(x)
  end
end

function path_in_pgf_syntax (p)

  local s = {}
  
  local i = 1
  while i <= #p do
    local c = p[i]
    assert (type(c) == "string", "illegal path operand")
    
    if c == "lineto" then
      i = i + 1
      local d = rigid(p[i])
      s [#s + 1] = '\\pgfpathlineto{\\pgfqpoint{' .. to_pt(d.x) .. '}{' .. to_pt(d.y) .. '}}'
      i = i + 1
    elseif c == "moveto" then
      i = i + 1
      local d = rigid(p[i])
      s [#s + 1] = '\\pgfpathmoveto{\\pgfqpoint{' .. to_pt(d.x) .. '}{' .. to_pt(d.y) .. '}}'
      i = i + 1
    elseif c == "closepath" then
      s [#s + 1] = '\\pgfpathclose'
      i = i + 1
    elseif c == "curveto" then
      local d1, d2, d3 = rigid(p[i+1]), rigid(p[i+2]), rigid(p[i+3])
      i = i + 3
      s [#s + 1] = '\\pgfpathcurveto{\\pgfqpoint{' .. to_pt(d1.x) .. '}{' .. to_pt(d1.y) .. '}}{\\pgfqpoint{'
	.. to_pt(d2.x) .. '}{' .. to_pt(d2.y) .. '}}{\\pgfqpoint{' 
	.. to_pt(d3.x) .. '}{' .. to_pt(d3.y) .. '}}'
      i = i + 1
    else				     
      error("illegal operation in edge path")
    end
  end

  return table.concat(s)
end

function coordinate_in_pgf_syntax(c)
  return '\\pgfqpoint{'..to_pt(c.x) .. '}{'.. to_pt(c.y) .. '}'
end


return BindingToPGF