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
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
|
if not modules then modules = { } end modules ['x-flow'] = {
version = 1.001,
comment = "companion to m-flow.mkvi",
author = "Hans Hagen, PRAGMA-ADE, Hasselt NL",
copyright = "PRAGMA ADE / ConTeXt Development Team",
license = "see context related readme files"
}
-- when we can resolve mpcolor at the lua end we will use metapost.graphic(....) directly
moduledata.charts = moduledata.charts or { }
local gsub, match, find, format, lower = string.gsub, string.match, string.find, string.format, string.lower
local lpegmatch = lpeg.match
local points = number.points
local variables = interfaces.variables
local defaults = {
chart = {
name = "",
option = "",
backgroundcolor = "",
width = 0,
height = 0,
dx = 0,
dy = 0,
offset = 0,
bodyfont = "",
dot = "",
},
shape = { -- FLOS
rulethickness = 65436,
default = "",
framecolor = "green",
backgroundcolor = "yellow",
},
focus = { -- FLOF
rulethickness = 65436,
framecolor = "red",
backgroundcolor = "yellow",
},
line = { -- FLOL
rulethickness = 65436,
radius = 65436,
color = "blue",
corner = "",
dash = "",
arrow = "",
offset = "",
},
set = { -- FLOX
},
split = {
nx = 3,
ny = 3,
command = "",
marking = "",
before = "",
after = "",
}
}
local validshapes = {
["node"] = { kind = "shape", number = 0 },
["action"] = { kind = "shape", number = 24 },
["procedure"] = { kind = "shape", number = 5 },
["product"] = { kind = "shape", number = 12 },
["decision"] = { kind = "shape", number = 14 },
["archive"] = { kind = "shape", number = 19 },
["loop"] = { kind = "shape", number = 35 },
["wait"] = { kind = "shape", number = 6 },
["subprocedure"] = { kind = "shape", number = 20 },
["singledocument"] = { kind = "shape", number = 32 },
["multidocument"] = { kind = "shape", number = 33 },
["right"] = { kind = "line", number = 66 },
["left"] = { kind = "line", number = 67 },
["up"] = { kind = "line", number = 68 },
["down"] = { kind = "line", number = 69 },
}
local validlabellocations = {
l = "l", left = "l",
r = "r", right = "r",
t = "t", top = "t",
b = "b", bottom = "b",
}
table.setmetatableindex(validshapes,function(t,k)
local l = gsub(lower(k)," ","")
local v = rawget(t,l)
if not v then
local n = tonumber(k)
if n then
v = { kind = "shape", number = n }
else
v = rawget(t,"action")
end
end
t[k] = v
return v
end)
local charts = { }
local data, hash, temp, last_x, last_y, name
function commands.flow_start_chart(chartname)
data = { }
hash = { }
last_x, last_y = 0, 0
name = chartname
end
function commands.flow_stop_chart()
charts[name] = {
data = data,
hash = hash,
last_x = last_x,
last_y = last_y,
}
data, hash, temp = nil, nil, nil
end
function commands.flow_set_current_cell(n)
temp = data[tonumber(n)] or { }
end
function commands.flow_start_cell(settings)
temp = {
labels = { },
comments = { },
exits = { },
connections = { },
settings = settings,
}
end
function commands.flow_stop_cell()
data[#data+1] = temp
hash[temp.name or #data] = temp
end
function commands.flow_set_name(str)
temp.name = str
end
function commands.flow_set_shape(str)
temp.shape = str
end
function commands.flow_set_destination(str)
temp.destination = str
end
function commands.flow_set_text(align,str)
temp.align = align
temp.text = str
end
function commands.flow_set_overlay(str)
temp.overlay = str
end
function commands.flow_set_focus(str)
temp.focus = str
end
function commands.flow_set_figure(str)
temp.figure = str
end
function commands.flow_set_label(location,text)
temp.labels[#temp.labels+1] = {
location = location,
text = text,
}
end
function commands.flow_set_comment(name,str)
temp.comments[#temp.comments+1] = {
location = location,
text = text,
}
end
function commands.flow_set_exit(location,text)
temp.exits[#temp.exits+1] = {
location = location,
text = text,
}
end
function commands.flow_set_include(name,x,y,settings)
data[#data+1] = {
include = name,
x = x,
y = y,
-- settings = settings,
}
end
local function inject(includedata,data,hash)
local subchart = charts[includedata.include]
if not subchart then
return
end
local subdata = subchart.data
if not subdata then
return
end
local xoffset = (includedata.x or 1) - 1
local yoffset = (includedata.y or 1) - 1
local settings = includedata.settings
for i=1,#subdata do
local si = subdata[i]
local t = {
x = si.x + xoffset,
y = si.y + yoffset,
settings = settings,
}
table.setmetatableindex(t,si)
data[#data+1] = t
hash[si.name or #data] = t
end
end
local function expanded(chart)
local expandeddata = { }
local expandedhash = { }
local expandedchart = {
data = expandeddata,
hash = expandedhash,
}
table.setmetatableindex(expandedchart,chart)
local data = chart.data
local hash = chart.hash
for i=1,#data do
local di = data[i]
if di.include then
inject(di,expandeddata,expandedhash)
else
expandeddata[#expandeddata+1] = di
expandedhash[di.name or #expandeddata] = di
end
end
for i=1,#expandeddata do
local cell = expandeddata[i]
local settings = cell.settings
if not settings then
cell.settings = chart.settings
else
table.setmetatableindex(settings,chart.settings)
end
end
return expandedchart
end
local splitter = lpeg.splitat(",")
function commands.flow_set_location(str) -- handle include differently
-- wrong: delay real x,y, only store relative
local x, y = lpegmatch(splitter,str)
if not x or x == "" then
x = last_x
elseif x == "+" then
x = last_x + 1
elseif x == "-" then
x = last_x - 1
elseif find(x,"^[%+%-]") then
x = last_x + (tonumber(x) or 0)
else
x = tonumber(x)
end
if not y or y == "" then
y = last_y
elseif y == "+" then
y = last_y + 1
elseif x == "-" then
y = last_y - 1
elseif find(y,"^[%+%-]") then
y = last_y + (tonumber(y) or 0)
else
y = tonumber(y)
end
temp.x = x
temp.y = y
last_x = x
last_y = y
end
function commands.flow_set_connection(location,displacement,name)
local dx, dy = lpegmatch(splitter,displacement)
dx = tonumber(dx) or 1
dy = tonumber(dy) or 1
temp.connections[#temp.connections+1] = {
location = location,
dx = dx - 1,
dy = dy - 1,
name = name,
}
end
local where = {
l = "left",
r = "right",
t = "top",
b = "bottom",
}
local what = {
["p"] = 1,
["m"] = -1,
["+"] = 1,
["-"] = -1,
}
local function visible(chart,cell)
local x, y = cell.x, cell.y
return
x >= chart.from_x and x <= chart.to_x and
y >= chart.from_y and y <= chart.to_y and cell
end
local function check_cells(chart,xoffset,yoffset,min_x,min_y,max_x,max_y)
local data = chart.data
if not data then
return
end
for i=1,#data do
local cell = data[i]
local x, y = cell.x + xoffset, cell.y + yoffset
if min_x == 0 then
min_x, max_x = x, x
min_y, max_y = y, y
else
if x < min_x then min_x = x end
if y < min_y then min_y = y end
if x > max_x then max_x = x end
if y > max_y then max_y = y end
end
end
return min_x, min_y, max_x, max_y
end
local function process_cells(chart,xoffset,yoffset)
local data = chart.data
if not data then
return
end
for i=1,#data do
local cell = visible(chart,data[i])
if cell then
local shape = cell.shape
if not shape or shape == "" then
shape = settings.shape.default or "none"
end
if shape ~= variables.none then
local settings = cell.settings
local shapedata = validshapes[shape]
context("flow_begin_sub_chart ;")
if shapedata.kind == "line" then
local linesettings = settings.line
context("flow_shape_line_color := \\MPcolor{%s} ;", linesettings.color)
context("flow_shape_fill_color := \\MPcolor{%s} ;", linesettings.backgroundcolor)
context("flow_shape_line_width := %s ; ", points(linesettingsrulethickness))
elseif hasfocus then -- doifcommonelse{FLOWcell,FLOWfocus}@@FLOWfocus
local focussettings = settings.focus
context("flow_shape_line_color := \\MPcolor{%s} ;", focussettings.framecolor)
context("flow_shape_fill_color := \\MPcolor{%s} ;", focussettings.backgroundcolor)
context("flow_shape_line_width := %s ; ", points(focussettings.rulethickness))
else
local shapesettings = settings.shape
context("flow_shape_line_color := \\MPcolor{%s} ;", shapesettings.framecolor)
context("flow_shape_fill_color := \\MPcolor{%s} ;", shapesettings.backgroundcolor)
context("flow_shape_line_width := %s ; " , points(shapesettings.rulethickness))
end
context("bodyfontsize := 10pt ;") -- todo
context("flow_peepshape := false ;") -- todo
context("flow_new_shape(%s,%s,%s) ;",cell.x+xoffset,cell.y+yoffset,shapedata.number)
context("flow_end_sub_chart ;")
end
end
end
end
-- todo : make lpeg for splitter
local function process_connections(chart,xoffset,yoffset)
local data = chart.data
local hash = chart.hash
if not data then
return
end
local settings = chart.settings
for i=1,#data do
local cell = visible(chart,data[i])
if cell then
local connections = cell.connections
for j=1,#connections do
local connection = connections[j]
local othername = connection.name
local othercell = hash[othername]
if othercell then
local cellx, celly = cell.x, cell.y
local otherx, othery, location = othercell.x, othercell.y, connection.location
if otherx > 0 and othery > 0 and cellx > 0 and celly > 0 and connection.location then
-- move to setter
local what_cell, where_cell, what_other, where_other = match(location,"([%+%-pm]-)([lrtb]),?([%+%-pm]-)([lrtb])")
local what_cell = what [what_cell] or 0
local what_other = what [what_other] or 0
local where_cell = where[where_cell] or "left"
local where_other = where[where_other] or "right"
local linesettings = settings.line
context("flow_smooth := %s ;", linesettings.corner == variables.round and "true" or "false")
context("flow_dashline := %s ;", linesettings.dash == variables.yes and "true" or "false")
context("flow_arrowtip := %s ;", linesettings.arrow == variables.yes and "true" or "false")
context("flow_touchshape := %s ;", linesettings.offset == variables.none and "true" or "false")
context("flow_dsp_x := %s ; flow_dsp_y := %s ;",connection.dx or 0, connection.dy or 0)
context("flow_connection_line_color := \\MPcolor{%s} ;",linesettings.color)
context("flow_connection_line_width := 2pt ;",points(linesettings.rulethickness))
context("flow_connect_%s_%s(%s,%s,%s) (%s,%s,%s) ;",where_cell,where_other,cellx,celly,what_cell,otherx,othery,what_other)
context("flow_dsp_x := 0 ; flow_dsp_y := 0 ;")
end
end
end
end
end
end
local texttemplate = "\\setvariables[flowcell:text][x=%s,y=%s,text={%s},align={%s},figure={%s},destination={%s}]"
local function process_texts(chart,xoffset,yoffset)
local data = chart.data
local hash = chart.hash
if not data then
return
end
for i=1,#data do
local cell = visible(chart,data[i])
if cell then
local x = cell.x or 1
local y = cell.y or 1
local text = cell.text
if text and text ~= "" then
local a = cell.align or ""
local f = cell.figure or ""
local d = cell.destination or ""
context('flow_chart_draw_text(%s,%s,textext("%s")) ;',x,y,format(texttemplate,x,y,text,a,f,d))
end
local labels = cell.labels
for i=1,#labels do
local label = labels[i]
local text = label.text
local location = validlabellocations[label.location or ""]
if text and location then
context('flow_chart_draw_label_%s(%s,%s,textext("%s")) ;',location,x,y,text)
end
end
local exits = cell.exits
for i=1,#exits do
local exit = exits[i]
local text = exit.text
local location = validlabellocations[exit.location or ""]
if text and location then
-- maybe make autoexit an option
if location == "l" and x == chart.from_x + 1 or
location == "r" and x == chart.to_x - 1 or
location == "t" and y == chart.to_y - 1 or
location == "b" and y == chart.from_y + 1 then
context('flow_chart_draw_exit_%s(%s,%s,textext("%s")) ;',location,x,y,text)
end
end
end
local comments = cell.comments
for i=1,#comments do
-- invisible
end
end
end
end
local function getchart(settings)
local chartname = settings.chart.name
if not chartname then
print("no name given")
return
end
local chart = charts[chartname]
if not chart then
print("no such chart",chartname)
return
end
chart.settings = settings
table.setmetatableindex(settings,defaults)
chart = expanded(chart)
local _, _, nx, ny = check_cells(chart,0,0,0,0,0,0)
chart.from_x = chart.settings.chart.x or 1
chart.from_y = chart.settings.chart.y or 1
chart.to_x = chart.settings.chart.nx or nx
chart.to_y = chart.settings.chart.ny or ny
chart.nx = chart.to_x - chart.from_x + 1
chart.ny = chart.to_y - chart.from_y + 1
return chart
end
local function makechart(chart)
local settings = chart.settings
context.begingroup()
context.forgetall()
--
local bodyfont = settings.chart.bodyfont
if bodyfont ~= "" then
context.switchtobodyfont { bodyfont }
end
--
context.startMPcode()
context("if unknown context_flow : input mp-char.mpiv ; fi ;")
context("flow_begin_chart(0,%s,%s);",chart.nx,chart.ny)
--
if settings.chart.option == variables.test or settings.chart.dot == variables.yes then
context("flow_show_con_points := true ;")
context("flow_show_mid_points := true ;")
context("flow_show_all_points := true ;")
elseif settings.chart.dot ~= "" then -- no checking done, private option
context("flow_show_%s_points := true ;",settings.chart.dot)
end
--
local backgroundcolor = settings.chart.backgroundcolor
if backgroundcolor and backgroundcolor ~= "" then
context("flow_chart_background_color := \\MPcolor{%s} ;",backgroundcolor)
end
--
local shapewidth = settings.chart.width
local gridwidth = shapewidth + 2*settings.chart.dx
local shapeheight = settings.chart.height
local gridheight = shapeheight + 2*settings.chart.dy
context("flow_grid_width := %s ;", points(gridwidth))
context("flow_grid_height := %s ;", points(gridheight))
context("flow_shape_width := %s ;", points(shapewidth))
context("flow_shape_height := %s ;", points(shapeheight))
--
local radius = settings.line.radius
local rulethickness = settings.line.rulethickness
local dx = settings.chart.dx
local dy = settings.chart.dy
if radius < rulethickness then
radius = 2.5*rulethickness
if radius > dx then
radius = dx
end
if radius > dy then
radius = dy
end
end
context("flow_connection_line_width := %s ;", points(rulethickness))
context("flow_connection_smooth_size := %s ;", points(radius))
context("flow_connection_arrow_size := %s ;", points(radius))
context("flow_connection_dash_size := %s ;", points(radius))
--
local offset = settings.chart.offset -- todo: pass string
if offset == variables.none or offset == variables.overlay or offset == "" then
offset = -2.5 * radius -- or rulethickness?
elseif offset == variables.standard then
offset = radius -- or rulethickness?
end
context("flow_chart_offset := %s ;",points(offset))
--
context("flow_reverse_y := true ;")
process_cells(chart,0,0)
process_connections(chart,0,0)
process_texts(chart,0,0)
-- context("clip_chart(%s,%s,%s,%s) ;",x,y,nx,ny) -- todo: draw lines but not shapes
context("flow_end_chart ;")
context.stopMPcode()
context.endgroup()
end
function commands.flow_make_chart(settings)
local chart = getchart(settings)
if chart then
local settings = chart.settings
if settings.split.state == variables.start then
local nx = chart.settings.split.nx
local ny = chart.settings.split.ny
local x = 1
while true do
local y = 1
while true do
-- FLOTbefore
-- doif @@FLOTmarking on -> cuthbox
-- @@FLOTcommand
chart.from_x = x
chart.from_y = y
chart.to_x = math.min(x + nx - 1,chart.nx)
chart.to_y = math.min(x + ny - 1,chart.ny)
makechart(chart)
-- FLOTafter
y = y + ny
if y > chart.max_y then
break
else
y = y - dy
end
end
x = x + nx
if x > chart.max_x then
break
else
x = x - dx
end
end
else
makechart(chart)
end
end
end
|