-
Notifications
You must be signed in to change notification settings - Fork 0
/
sfgdemo.c
499 lines (425 loc) · 11.3 KB
/
sfgdemo.c
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
/*
* Copyright t lefering
* parts are (C) Universitaet Passau 1986-1991
* parts are Copyright (C) 1998-2021 Free Software Foundation, Inc.
* parts are Copyright (C) Felix von Leitner from dietlibc
*
* https://notabug.org/mooigraph/sfgraph
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* These are the four essential freedoms with GNU GPL software:
* 1: freedom to run the program, for any purpose
* 2: freedom to study how the program works, and change it to make it do what you wish
* 3: freedom to redistribute copies to help your Free Software friends
* 4: freedom to distribute copies of your modified versions to your Free Software friends
* , ,
* / \
* ((__-^^-,-^^-__))
* `-_---' `---_-'
* `--|o` 'o|--'
* \ ` /
* ): :(
* :o_o:
* "-"
*
* SPDX-License-Identifier: GPL-3.0+
* License-Filename: LICENSE
*/
/* demo program how to use the sfg.c Single File Graph layout file
* compile with gcc sfgdemo.c sfg.c -o sfgdemo
* usage:
* ./sfgdemo
* ./sfgdemo input.txt output.ps
*/
#include <stdio.h>
#include <string.h>
#include "sfg.h"
static char *namein = NULL;
static char *nameout = NULL;
static FILE *fin = NULL;
static FILE *fos = NULL;
static char lbuf[1024];
static void readfile(void);
static void head(void);
static void tail(void);
/* this gets the node data after layout */
static int getnodedata(int num, int level, int pos, int xpos, int ypos, int tx, int ty, int nselfedges, int type,
int indegree, int outdegree, int ly0, int ly1);
/* this gets the node data after layout */
static int getnodedataps(int num, int level, int pos, int xpos, int ypos, int tx, int ty, int nselfedges, int type,
int indegree, int outdegree, int ly0, int ly1);
/* this gets the edge data after layout */
static int getedgedata(int num, int from, int to, int type, int rev);
/* this gets the edge data after layout */
static int getedgedataps(int num, int from, int to, int type, int rev);
int main(int argc, char *argv[])
{
int status = 0;
int nn = 0;
int ne = 0;
if (argc == 1) {
namein = "sfgdemo.txt";
nameout = "sfgdemo.svg";
} else if (argc == 3) {
namein = argv[1];
nameout = argv[2];
} else {
printf("Usage: ./sfgdemo input.txt output.svg or ./sfgdemo\n");
return (0);
}
if (argc == 1) {
printf("demo of sfg Single File Graph layout library version %d\n", sfg_version());
/* init to start */
status = sfg_init();
if (status < 0) { /* error */
}
/* add few nodes starting at 1, no user data, small texlabel area */
for (nn = 1; nn < 4; nn++) {
status = sfg_addnode(nn, 25, 20);
if (status < 0) { /* error */
break;
}
}
/* add few edges with number starting at 1 */
ne++;
status = sfg_addedge(ne, /* from */ 1, /* to */ 2, 0, 0);
if (status < 0) { /* error */
}
ne++;
status = sfg_addedge(ne, /* from */ 1, /* to */ 3, 10, 10);
if (status < 0) { /* error */
}
/* set min (x,y) spacing of nodes */
status = sfg_xspacing(5);
if (status < 0) { /* error */
}
status = sfg_yspacing(15);
if (status < 0) { /* error */
}
/* run layout */
status = sfg_layout();
if (status < 0) { /* error */
}
printf("drawing has size (%d,%d) with %d levels\n", sfg_maxx(), sfg_maxy(), sfg_nlevels());
/* get node data */
status = sfg_node_foreach(getnodedata);
if (status < 0) { /* error */
}
/* get edge data */
status = sfg_edge_foreach(getedgedata);
if (status < 0) { /* error */
}
/* de-init to finish */
status = sfg_deinit();
if (status < 0) { /* error */
}
}
/* now run it on file */
fin = fopen(namein, "r");
fos = fopen(nameout, "w");
if (fin == NULL) {
printf("cannot open file %s for reading\n", namein);
return (0);
}
if (fos == NULL) {
printf("cannot open file %s for writing\n", nameout);
return (0);
}
fflush(stdout);
/* re-init and read input file, then generate postscript */
/* init to start */
status = sfg_init();
if (status < 0) { /* error */
}
readfile();
/* run layout */
status = sfg_layout();
if (status < 0) { /* error */
}
printf("drawing has size (%d,%d) with %d levels and %d nodes with %d edges\n", sfg_maxx(), sfg_maxy(), sfg_nlevels(),
sfg_nnodes(), sfg_nedges());
head();
/* get node data */
status = sfg_node_foreach(getnodedataps);
if (status < 0) { /* error */
}
/* get edge data */
status = sfg_edge_foreach(getedgedataps);
if (status < 0) { /* error */
}
tail();
/* de-init to finish */
status = sfg_deinit();
if (status < 0) { /* error */
}
return (0);
}
/* this gets the node data after layout */
static int getnodedata(int num, int level, int pos, int xpos, int ypos, int tx, int ty, int nselfedges, int type,
int indegree, int outdegree, int ly0, int ly1)
{
/* return(1); to stop the callback */
if (xpos) {
}
if (ypos) {
}
if (tx) {
}
if (ty) {
}
if (nselfedges) {
}
if (type) {
}
if (indegree) {
}
if (outdegree) {
}
if (ly0) {
}
if (ly1) {
}
printf("node %d relative pos (%d,%d) in/outdegree %d:%d final pos (%d,%d)\n", num, pos, level, indegree, outdegree, xpos,
ypos);
/* return 0 to continue callbacks */
return (0);
}
/* this gets the edge data after layout */
static int getedgedata(int num, int from, int to, int type, int rev)
{
/* return(1); to stop the callback */
if (type) {
}
printf("edge %d from node %d to %d reversed=%d\n", num, from, to, rev);
/* return 0 to continue callbacks */
return (0);
}
static void readfile(void)
{
int ch = 0;
int i = 0;
int n = 0;
int fn = 0;
int tn = 0;
int status = 0;
int ne = 0;
for (;;) {
/* fill buffer */
memset(lbuf, 0, 1024);
i = 0;
for (;;) {
ch = fgetc(fin);
if (ch == EOF) {
break;
}
if (ch == '\n') {
break;
}
lbuf[i] = ch;
if (i > 1023) {
break;
}
i++;
}
/* check data in buffer */
if (strlen(lbuf)) {
if (lbuf[0] == '#') {
/* comment line */ } else {
n = sscanf(lbuf, "%d %d", &fn, &tn);
if (n == 1) {
/* node */
printf("parsed node number %d\n", fn);
status = sfg_addnode(fn, 25, 20);
if (status < 0) { /* error */
break;
}
} else if (n == 2) {
/* edge */
printf("parsed edge from node %d to node %d\n", fn, tn);
ne++;
status = sfg_addedge(ne, /* from */ fn, /* to */ tn, 0, 0);
if (status < 0) { /* error */
}
} else {
/* skip line */
}
}
}
if (ch == EOF) {
break;
}
}
/* testing add few single nodes */
if (0) {
status = sfg_addnode(100, 25, 20);
if (status < 0) { /* error */
}
status = sfg_addnode(101, 25, 40);
if (status < 0) { /* error */
}
status = sfg_addnode(102, 25, 30);
if (status < 0) { /* error */
}
}
return;
}
/* this gets the node data after layout, see sfg.h */
static int getnodedataps(int num, int level, int pos, int xpos, int ypos, int tx, int ty, int nselfedges, int type,
int indegree, int outdegree, int ly0, int ly1)
{
/* return(1); to stop the callback */
if (num) {
}
if (level) {
}
if (pos) {
}
if (xpos) {
}
if (ypos) {
}
if (nselfedges) {
/* self edges at the node must be drawn here */
}
if (type) {
}
if (indegree) {
}
if (outdegree) {
}
if (ly0) {
}
if (ly1) {
}
fprintf(fos,
" <rect id=\"node_%d\" height=\"%d\" width=\"%d\" y=\"%d\" x=\"%d\" stroke-linecap=\"null\" stroke-linejoin=\"null\" stroke-dasharray=\"null\" stroke=\"#000000\" fill=\"#7fff00\"/>\n",
num, ty, tx, ypos, xpos);
if (tx && ty) {
fprintf(fos, " <text x=\"%d\" y=\"%d\" class=\"small\">%d</text>\n", xpos, ypos + ty, num);
}
/* return 0 to continue callbacks */
return (0);
}
/* this gets the edge data after layout, see sfg.h */
static int getedgedataps(int num, int from, int to, int type, int rev)
{
int fx = 0;
int fy = 0;
int tx = 0;
int ty = 0;
int typef = 0;
int typet = 0;
int szxf = 0;
int szyf = 0;
int szxt = 0;
int szyt = 0;
int ly = 0;
int ryf = 0;
int ryt = 0;
char *scolor = "";
/* todo this should also guide the dummy nodes to get a better drawing */
if (num) {
}
if (rev) {
/* draw arrow at from-node */
scolor = "#0f0fff";
} else {
/* draw arrow at to-node */
scolor = "#000000";
}
/* return(1); to stop the callback */
/* get type of nodes */
typef = sfg_nodetype(from);
typet = sfg_nodetype(to);
/* get (x,y) pos of nodes */
fx = sfg_nodexpos(from);
fy = sfg_nodeypos(from);
tx = sfg_nodexpos(to);
ty = sfg_nodeypos(to);
/* get sizes of nodes */
szxf = sfg_nodexsize(from);
szyf = sfg_nodeysize(from);
szxt = sfg_nodexsize(to);
szyt = sfg_nodeysize(to);
/* relative y positions of from/to node */
ryf = sfg_noderelypos(from);
ryt = sfg_noderelypos(to);
if (ryf) {
}
if (ryt) {
}
/* type of edge */
if (type == 3) {
/* this horizontal edge must be drawn different */
if (fy + szyf > ty + szyt) {
ly = fy + szyf;
} else {
ly = ty + szyt;
}
ly = ly + 4;
fx = fx + (szxf / 2);
fy = fy + szyf;
tx = tx + (szxt / 2);
ty = ty + szyt;
fprintf(fos,
" <line id=\"edge_%d\" y2=\"%d\" x2=\"%d\" y1=\"%d\" x1=\"%d\" stroke-linecap=\"null\" stroke-linejoin=\"null\" stroke-dasharray=\"null\" stroke-width=\"2\" stroke=\"%s\" fill=\"none\"/>\n",
num, ly, fx, fy, fx, scolor);
fprintf(fos,
" <line id=\"edge_%d\" y2=\"%d\" x2=\"%d\" y1=\"%d\" x1=\"%d\" stroke-linecap=\"null\" stroke-linejoin=\"null\" stroke-dasharray=\"null\" stroke-width=\"2\" stroke=\"%s\" fill=\"none\"/>\n",
num, ly, tx, ly, fx, scolor);
fprintf(fos,
" <line id=\"edge_%d\" y2=\"%d\" x2=\"%d\" y1=\"%d\" x1=\"%d\" stroke-linecap=\"null\" stroke-linejoin=\"null\" stroke-dasharray=\"null\" stroke-width=\"2\" stroke=\"%s\" fill=\"none\"/>\n",
num, ty, tx, ly, tx, scolor);
} else {
/* if from node is regular, tune the edge conn. point */
if (typef == 1) {
fx = fx + (szxf / 2);
fy = fy + szyf;
}
/* if to node is regular, tune the edge conn. point */
if (typet == 1) {
tx = tx + (szxt / 2);
if (szyt) {
}
}
fprintf(fos,
" <line id=\"edge_%d\" y2=\"%d\" x2=\"%d\" y1=\"%d\" x1=\"%d\" stroke-linecap=\"null\" stroke-linejoin=\"null\" stroke-dasharray=\"null\" stroke-width=\"2\" stroke=\"%s\" fill=\"none\"/>\n",
num, ty, tx, fy, fx, scolor);
}
/* return 0 to continue callbacks */
return (0);
}
/* header */
static void head(void)
{
/* make image +5 y pixels larger to allow for horizontal edge */
fprintf(fos,
"<svg width=\"%d\" height=\"%d\" xmlns=\"http://www.w3.org/2000/svg\" xmlns:svg=\"http://www.w3.org/2000/svg\">\n",
sfg_maxx() + 1, sfg_maxy() + 5);
fprintf(fos, "%s\n", " <g>");
fprintf(fos, "%s\n", " <title>sfgdemo</title>");
fprintf(fos, "%s\n", " <style>");
fprintf(fos, "%s\n", " .small { font: italic 15px sans-serif; }");
fprintf(fos, "%s\n", " </style>");
return;
}
/* footer */
static void tail(void)
{
fprintf(fos, "%s\n", " </g>");
fprintf(fos, "%s\n", "</svg>");
return;
}
/* end */