Skip to content

feat: add a simple standalone version of graphviz #296

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: trunk
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 13 additions & 12 deletions packages/graphviz/src-cpp/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,21 @@ add_executable(graphvizlib
set_target_properties(graphvizlib PROPERTIES COMPILE_FLAGS "${CPP_FLAGS}")
set_target_properties(graphvizlib PROPERTIES LINK_FLAGS "${LINK_FLAGS}")

add_executable(graphvizlib-standalone
main-standalone.cpp
util.hpp
util.cpp
)

set_target_properties(graphvizlib-standalone PROPERTIES COMPILE_FLAGS "")
set_target_properties(graphvizlib-standalone PROPERTIES LINK_FLAGS "--no-entry -s STANDALONE_WASM -s EXPORTED_FUNCTIONS=\"['_free', '_malloc', '_layout', '_lastError']\"")

# link libraries
function(add_graphviz_library library)
find_library(GRAPHVIZ_${library} NAMES ${library})
if(GRAPHVIZ_${library})
target_link_libraries(graphvizlib PRIVATE ${GRAPHVIZ_${library}})
target_link_libraries(graphvizlib-standalone PRIVATE ${GRAPHVIZ_${library}})
else()
message(FATAL_ERROR "Could not find library ${library}!")
endif()
Expand Down Expand Up @@ -90,18 +100,9 @@ target_link_libraries(graphvizlib PRIVATE
${EXPAT_LIBRARIES}
)

# add_executable(graphvizlib-web
# ${SRCS}
# )

# set_target_properties(graphvizlib-web PROPERTIES LINK_FLAGS "${LINK_FLAGS} -s ENVIRONMENT=web")

# target_link_libraries(graphvizlib-web PRIVATE
# gvplugin_core
# gvplugin_dot_layout
# gvplugin_neato_layout
# ortho
# )
target_link_libraries(graphvizlib-standalone PRIVATE
${EXPAT_LIBRARIES}
)

# add_executable(graphvizlib-worker
# ${SRCS}
Expand Down
84 changes: 84 additions & 0 deletions packages/graphviz/src-cpp/main-standalone.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
#include "util.hpp"

#include <gvc.h>
#include <gvplugin.h>
#include <graphviz_version.h>

#include <emscripten.h>

extern gvplugin_library_t gvplugin_dot_layout_LTX_library;
extern gvplugin_library_t gvplugin_neato_layout_LTX_library;
#ifdef HAVE_LIBGD
extern gvplugin_library_t gvplugin_gd_LTX_library;
#endif
#ifdef HAVE_PANGOCAIRO
extern gvplugin_library_t gvplugin_pango_LTX_library;
#ifdef HAVE_WEBP
extern gvplugin_library_t gvplugin_webp_LTX_library;
#endif
#endif
extern gvplugin_library_t gvplugin_core_LTX_library;

lt_symlist_t lt_preloaded_symbols[] = {
{"gvplugin_dot_layout_LTX_library", &gvplugin_dot_layout_LTX_library},
{"gvplugin_neato_layout_LTX_library", &gvplugin_neato_layout_LTX_library},
#ifdef HAVE_PANGOCAIRO
{"gvplugin_pango_LTX_library", &gvplugin_pango_LTX_library},
#ifdef HAVE_WEBP
{"gvplugin_webp_LTX_library", &gvplugin_webp_LTX_library},
#endif
#endif
#ifdef HAVE_LIBGD
{"gvplugin_gd_LTX_library", &gvplugin_gd_LTX_library},
#endif
{"gvplugin_core_LTX_library", &gvplugin_core_LTX_library},
{0, 0}};

StringBuffer lastErrorStr;

int vizErrorf(char *buf)
{
lastErrorStr = buf;
return 0;
}

extern int Y_invert;
int origYInvert = Y_invert;
extern int Nop;
int origNop = Nop;

StringBuffer layout_result;

extern "C"
{
const char *layout(const char *src, const char *format, const char *engine)
{
layout_result = "";

GVC_t *gvc = gvContextPlugins(lt_preloaded_symbols, true);

Agraph_t *graph = agmemread(src);
if (graph)
{
char *data = NULL;
unsigned int length;

gvLayout(gvc, graph, engine);
gvRenderData(gvc, graph, format, &data, &length);
layout_result = data;
gvFreeRenderData(data);
gvFreeLayout(gvc, graph);
agclose(graph);
}

gvFinalize(gvc);
gvFreeContext(gvc);

return layout_result;
}

const char *lastError()
{
return lastErrorStr;
}
}
Loading