r3/src/gvc.c

101 lines
2.2 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 "r3_gvc.h"
#include "zmalloc.h"
2014-05-17 19:34:51 -04:00
2014-05-17 20:13:58 -04:00
2014-05-20 11:48:35 -04:00
static char * node_id_str(int id) {
char * name = zmalloc(sizeof(char) * 20);
2014-05-17 20:13:58 -04:00
sprintf(name, "#%d", id);
return name;
}
void r3_tree_build_ag_nodes(Agraph_t * g, Agnode_t * ag_parent_node, 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++;
agn_child = agnode(g, node_id_str(node_cnt) , 1);
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
*/
int r3_tree_render_dot(node * tree)
{
Agraph_t *g;
/* set up a graphviz context - but only once even for multiple graphs */
static GVC_t *gvc;
if (!gvc) {
gvc = gvContext();
}
/* 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);
gvLayout(gvc, g, "dot");
gvRender(gvc, g, "dot", stdout);
gvFreeLayout(gvc, g);
agclose(g);
return 0;
}
/**
* Render a tree to tree graph image via graphviz (dot)
2014-05-17 20:13:58 -04:00
*/
2014-05-17 19:34:51 -04:00
int r3_tree_render_file(node * tree, char * format, char * filename)
{
Agraph_t *g;
/* set up a graphviz context - but only once even for multiple graphs */
static GVC_t *gvc;
if (!gvc) {
gvc = gvContext();
}
/* 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;
}