Skip to content

Commit

Permalink
WIP images option
Browse files Browse the repository at this point in the history
  • Loading branch information
mdaines committed Feb 13, 2024
1 parent f8198db commit f99aa32
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 1 deletion.
2 changes: 1 addition & 1 deletion packages/viz/src/module/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
16 changes: 16 additions & 0 deletions packages/viz/src/viz.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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 = `<?xml version="1.0" encoding="UTF-8"?>
<svg xmlns="http://www.w3.org/2000/svg" width="${image.width}" height="${image.height}"></svg>
`;

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)) {
Expand Down Expand Up @@ -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") {
Expand Down
22 changes: 22 additions & 0 deletions packages/viz/test/render.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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: []
});
Expand Down

0 comments on commit f99aa32

Please sign in to comment.