Skip to content

Commit

Permalink
Implement Emscripten code for multiple output formats
Browse files Browse the repository at this point in the history
  • Loading branch information
mdaines committed Sep 24, 2024
1 parent 03dc8fb commit 8d8189d
Showing 1 changed file with 28 additions and 31 deletions.
59 changes: 28 additions & 31 deletions packages/viz/src/module/viz.c
Original file line number Diff line number Diff line change
Expand Up @@ -158,48 +158,45 @@ void viz_free_graph(Agraph_t *g) {
}

EMSCRIPTEN_KEEPALIVE
char *viz_render_graph(Agraph_t *graph, const char *format, const char *engine) {
GVC_t *context = NULL;
char *data = NULL;
unsigned int length = 0;
int layout_error = 0;
int render_error = 0;
GVC_t *viz_create_context() {
return gvContextPlugins(lt_preloaded_symbols, 0);
}

// Initialize context
EMSCRIPTEN_KEEPALIVE
void viz_free_context(GVC_t *context) {
gvFinalize(context);
gvFreeContext(context);
}

context = gvContextPlugins(lt_preloaded_symbols, 0);
EMSCRIPTEN_KEEPALIVE
int viz_layout(GVC_t *context, Agraph_t *graph, const char *engine) {
return gvLayout(context, graph, engine);
}

// Reset errors
EMSCRIPTEN_KEEPALIVE
void viz_free_layout(GVC_t *context, Agraph_t *graph) {
gvFreeLayout(context, graph);
}

EMSCRIPTEN_KEEPALIVE
void viz_reset_errors() {
agseterrf(viz_errorf);
agseterr(AGWARN);
agreseterrors();
}

// Layout

layout_error = gvLayout(context, graph, engine);

// Render (if layout was successful)

if (!layout_error) {
render_error = gvRenderData(context, graph, format, &data, &length);

if (render_error) {
gvFreeRenderData(data);
data = NULL;
}
}
EMSCRIPTEN_KEEPALIVE
char *viz_render(GVC_t *context, Agraph_t *graph, const char *format) {
char *data = NULL;
unsigned int length = 0;
int render_error = 0;

// Free the layout and context
render_error = gvRenderData(context, graph, format, &data, &length);

if (graph) {
gvFreeLayout(context, graph);
if (render_error) {
gvFreeRenderData(data);
data = NULL;
}

gvFinalize(context);
gvFreeContext(context);

// Return the result (if successful, the rendered graph; otherwise, null)

return data;
}

0 comments on commit 8d8189d

Please sign in to comment.