From 6309948563cdb3e6842eddfe453a352952c7319a Mon Sep 17 00:00:00 2001 From: Gordon Smith Date: Mon, 20 Jan 2025 14:04:36 +0000 Subject: [PATCH] feat: add a simple standalone version of graphviz Export "layout" and "lastError" Signed-off-by: Gordon Smith --- packages/graphviz/src-cpp/CMakeLists.txt | 25 +++--- packages/graphviz/src-cpp/main-standalone.cpp | 84 +++++++++++++++++++ 2 files changed, 97 insertions(+), 12 deletions(-) create mode 100644 packages/graphviz/src-cpp/main-standalone.cpp diff --git a/packages/graphviz/src-cpp/CMakeLists.txt b/packages/graphviz/src-cpp/CMakeLists.txt index a512527e..c1845c07 100644 --- a/packages/graphviz/src-cpp/CMakeLists.txt +++ b/packages/graphviz/src-cpp/CMakeLists.txt @@ -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() @@ -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} diff --git a/packages/graphviz/src-cpp/main-standalone.cpp b/packages/graphviz/src-cpp/main-standalone.cpp new file mode 100644 index 00000000..e3851bd2 --- /dev/null +++ b/packages/graphviz/src-cpp/main-standalone.cpp @@ -0,0 +1,84 @@ +#include "util.hpp" + +#include +#include +#include + +#include + +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; + } +}