diff --git a/packages/viz/src/module/Dockerfile b/packages/viz/src/module/Dockerfile index 813532c5..fa1b18f1 100644 --- a/packages/viz/src/module/Dockerfile +++ b/packages/viz/src/module/Dockerfile @@ -27,7 +27,7 @@ COPY --from=graphviz "${PREFIX}" "${PREFIX}" COPY viz.c pre.js . RUN mkdir -p "${OUTPUT}" -RUN emcc -Oz ${DEBUG:+-g2} --closure=0 --no-entry -sMODULARIZE=1 -sMINIMAL_RUNTIME=1 -sFILESYSTEM=0 -sASSERTIONS=0 -sALLOW_MEMORY_GROWTH=1 -sENVIRONMENT=web -sEXPORT_KEEPALIVE=1 -sEXPORTED_FUNCTIONS="['_malloc', '_free']" -s EXPORTED_RUNTIME_METHODS="['ccall', 'UTF8ToString', 'lengthBytesUTF8', 'stringToUTF8', 'getValue']" -sINCOMING_MODULE_JS_API="['wasm']" --pre-js pre.js -o "${OUTPUT}/module.mjs" viz.c -I"${PREFIX}/include" -I"${PREFIX}/include/graphviz" -L"${PREFIX}/lib" -L"${PREFIX}/lib/graphviz" -lgvplugin_dot_layout -lgvplugin_neato_layout -lgvplugin_core -lgvc -lpathplan -lcgraph -lxdot -lcdt -lexpat +RUN emcc -Oz ${DEBUG:+-g2} --closure=0 --no-entry -sMODULARIZE=1 -sMINIMAL_RUNTIME=1 -sASSERTIONS=0 -sALLOW_MEMORY_GROWTH=1 -sENVIRONMENT=web -sEXPORT_KEEPALIVE=1 -sEXPORTED_FUNCTIONS="['_malloc', '_free']" -s EXPORTED_RUNTIME_METHODS="['ccall', 'UTF8ToString', 'lengthBytesUTF8', 'stringToUTF8', 'getValue', 'FS', 'PATH']" -sINCOMING_MODULE_JS_API="['wasm']" --pre-js pre.js -o "${OUTPUT}/module.mjs" viz.c -I"${PREFIX}/include" -I"${PREFIX}/include/graphviz" -L"${PREFIX}/lib" -L"${PREFIX}/lib/graphviz" -lgvplugin_dot_layout -lgvplugin_neato_layout -lgvplugin_core -lgvc -lpathplan -lcgraph -lxdot -lcdt -lexpat FROM scratch AS export diff --git a/packages/viz/src/viz.mjs b/packages/viz/src/viz.mjs index 7656a11f..502b5355 100644 --- a/packages/viz/src/viz.mjs +++ b/packages/viz/src/viz.mjs @@ -60,6 +60,20 @@ function withStringPointer(module, graphPointer, value, callbackFn) { module.ccall("viz_string_free", "number", ["number", "number"], [graphPointer, stringPointer]); } +function createImageFiles(module, options) { + if (options.images) { + for (const image of options.images) { + const path = module.PATH.join("/", image.path); + const data = ` + +`; + + module.FS.createPath("/", module.PATH.dirname(path)); + module.FS.writeFile(path, data); + } + } +} + function setDefaultAttributes(module, graphPointer, data) { if (data.graphAttributes) { for (const [name, value] of Object.entries(data.graphAttributes)) { @@ -157,6 +171,8 @@ function renderInput(module, input, options) { module["agerrMessages"] = []; module["stderrMessages"] = []; + createImageFiles(module, options); + if (typeof input === "string") { graphPointer = readStringInput(module, input, options); } else if (typeof input === "object") { diff --git a/packages/viz/test/render.test.mjs b/packages/viz/test/render.test.mjs index 43858acf..6b3c3165 100644 --- a/packages/viz/test/render.test.mjs +++ b/packages/viz/test/render.test.mjs @@ -325,6 +325,28 @@ stop pos="99,18", width=0.75]; } +`, + errors: [] + }); + }); + + it("accepts an images option", function() { + const result = viz.render("graph { a[image=\"test.png\"] }", { + images: [ + { path: "test.png", width: 300, height: 200 } + ] + }); + + assert.deepStrictEqual(result, { + status: "success", + output: `graph { + graph [bb="0,0,321.03,214.96"]; + node [label="\\N"]; + a [height=2.9856, + image="test.png", + pos="160.51,107.48", + width=4.4587]; +} `, errors: [] });