r3/src/gvc.c

111 lines
2.5 KiB
C
Raw Normal View History

2014-05-17 19:34:51 -04:00
/*
* gvz.c
* Copyright (C) 2014 c9s <c9s@c9smba.local>
*
* Distributed under terms of the MIT license.
*/
#include <gvc.h>
#include <stdio.h>
#include <stdlib.h>
#include "r3.h"
#include "zmalloc.h"
2014-05-17 19:34:51 -04:00
2014-06-01 02:53:12 -04:00
void r3_tree_build_ag_nodes(Agraph_t * g, Agnode_t * ag_parent_node, const node * n, int node_cnt) {
2014-05-17 20:13:58 -04:00
edge * e;
Agnode_t *agn_child;
Agedge_t *agn_edge;
for ( int i = 0 ; i < n->edge_len ; i++ ) {
e = n->edges[i];
node_cnt++;
2014-06-01 02:53:12 -04:00
char *nodename = NULL;
2014-06-01 13:35:58 -04:00
if ( e && e->child && e->child->combined_pattern ) {
asprintf(&nodename,"%s", e->child->combined_pattern);
2014-06-01 02:53:12 -04:00
} else {
asprintf(&nodename,"#%d", node_cnt);
}
agn_child = agnode(g, nodename, 1);
2014-05-17 20:13:58 -04:00
agn_edge = agedge(g, ag_parent_node, agn_child, 0, 1);
agsafeset(agn_edge, "label", e->pattern, "");
2014-05-20 11:48:35 -04:00
if (e->child && e->child->endpoint) {
agsafeset(agn_child, "shape", "doublecircle", "");
}
2014-05-17 20:13:58 -04:00
r3_tree_build_ag_nodes(g, agn_child, e->child, node_cnt);
}
}
/**
* Render a tree to tree graph image via graphviz (dot)
2014-05-17 20:13:58 -04:00
*/
2014-06-03 06:41:55 -04:00
int r3_tree_render(const node * tree, const char *layout, const char * format, FILE *fp)
2014-05-17 20:13:58 -04:00
{
Agraph_t *g;
/* set up a graphviz context - but only once even for multiple graphs */
2014-06-03 06:41:55 -04:00
GVC_t *gvc = NULL;
gvc = gvContext();
2014-05-17 20:13:58 -04:00
/* Create a simple digraph */
g = agopen("g", Agdirected, 0);
// create self node
Agnode_t *ag_root = agnode(g, "{root}", 1);
r3_tree_build_ag_nodes(g, ag_root, tree, 0);
2014-06-03 06:41:55 -04:00
gvLayout(gvc, g, layout);
gvRender(gvc, g, format, fp);
2014-05-17 20:13:58 -04:00
gvFreeLayout(gvc, g);
agclose(g);
return 0;
}
2014-06-03 06:41:55 -04:00
/**
* Render a tree to tree graph image via graphviz (dot)
*/
int r3_tree_render_dot(const node * tree, const char *layout, FILE *fp)
{
return r3_tree_render(tree, layout, "dot", fp);
}
2014-05-17 20:13:58 -04:00
/**
* Render a tree to tree graph image via graphviz (dot)
2014-05-17 20:13:58 -04:00
*/
2014-06-01 02:41:45 -04:00
int r3_tree_render_file(const node * tree, const char * format, const char * filename)
2014-05-17 19:34:51 -04:00
{
Agraph_t *g;
2014-06-03 06:41:55 -04:00
GVC_t *gvc = NULL;
gvc = gvContext();
/*
// set up a graphviz context - but only once even for multiple graphs
2014-05-17 19:34:51 -04:00
static GVC_t *gvc;
if (!gvc) {
gvc = gvContext();
}
2014-06-03 06:41:55 -04:00
*/
2014-05-17 19:34:51 -04:00
/* Create a simple digraph */
g = agopen("g", Agdirected, 0);
2014-05-17 20:13:58 -04:00
// create self node
Agnode_t *ag_root = agnode(g, "{root}", 1);
r3_tree_build_ag_nodes(g, ag_root, tree, 0);
2014-05-17 19:34:51 -04:00
gvLayout(gvc, g, "dot");
gvRenderFilename(gvc, g, format, filename);
gvFreeLayout(gvc, g);
agclose(g);
return 0;
}