Skip to content

Commit 2e34ce5

Browse files
committed
feat: Add support for yInvert and nop
Signed-off-by: Gordon Smith <[email protected]>
1 parent f745e6e commit 2e34ce5

File tree

6 files changed

+44
-1
lines changed

6 files changed

+44
-1
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,8 @@ _ext_ optional "extra params":
163163
}
164164
```
165165
* _wasmFolder_: An optional `string` specifying the location of wasm file.
166+
* _yInvert_: An optional boolean flag to invert the y coordinate in generic output formats (dot, xdot, plain, plain-ext). This is equivalent to specifying -y when invoking Graphviz from the command-line.
167+
* _nop_: An optional number to specify "No layout" mode for the neato engine. This is equivalent to specifying the -n option when invoking Graphviz from the command-line.
166168

167169
For example passing a web hosted Image to GraphViz:
168170
```JavaScript

cpp/graphviz/graphvizlib/main.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#include "main.hpp"
22

33
#include <gvc.h>
4+
#include <globals.h>
45

56
extern gvplugin_library_t gvplugin_core_LTX_library;
67
extern gvplugin_library_t gvplugin_dot_layout_LTX_library;
@@ -59,6 +60,17 @@ const char *Main::lastError()
5960
return errorMessage;
6061
}
6162

63+
void Main::setYInvert(int yInvert)
64+
{
65+
Y_invert = yInvert;
66+
}
67+
68+
void Main::setNop(int nop)
69+
{
70+
if (nop != 0)
71+
Nop = nop;
72+
}
73+
6274
// Patch for invalid osage function ---
6375
// https://gitlab.com/graphviz/graphviz/issues/1544
6476
#include "types.h"

cpp/graphviz/graphvizlib/main.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,6 @@ class Main
44
static const char *layout(const char *dot, const char *format, const char *engine);
55
static const char *lastError();
66
static void createFile(const char *path, const char *data);
7+
static void setYInvert(int yInvert);
8+
static void setNop(int nop);
79
};

cpp/graphviz/graphvizlib/main.idl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,7 @@ interface Main {
22
[Const] static DOMString layout([Const] DOMString dot, [Const] DOMString format, [Const] DOMString engine);
33
[Const] static DOMString lastError();
44
void createFile([Const] DOMString file, [Const] DOMString data);
5+
void setYInvert(long yInvert);
6+
void setNop(long nop);
57
};
68

src/graphviz.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ interface Ext {
2020
images?: Image[];
2121
files?: File[];
2222
wasmFolder?: string;
23+
yInvert?: boolean;
24+
nop?: number;
2325
}
2426

2527
function imageToFile(image: Image): File {
@@ -49,6 +51,8 @@ export const graphviz = {
4951
if (!dotSource) return Promise.resolve("");
5052
return loadWasm(graphvizlib, ext?.wasmFolder).then(wasm => {
5153
createFiles(wasm, ext);
54+
wasm.Main.prototype.setYInvert(ext?.yInvert ? 1 : 0);
55+
wasm.Main.prototype.setNop(ext?.nop ? ext?.nop : 0);
5256
const retVal = wasm.Main.prototype.layout(dotSource, outputFormat, layoutEngine);
5357
if (!retVal) {
5458
throw new Error(wasm.Main.prototype.lastError());
@@ -87,6 +91,8 @@ class GraphvizSync {
8791
layout(dotSource: string, outputFormat: Format = "svg", layoutEngine: Engine = "dot", ext?: Ext): string {
8892
if (!dotSource) return "";
8993
createFiles(this._wasm, ext);
94+
this._wasm.Main.prototype.setYInvert(ext?.yInvert ? 1 : 0);
95+
this._wasm.Main.prototype.setNop(ext?.nop ? ext?.nop : 0);
9096
const retVal = this._wasm.Main.prototype.layout(dotSource, outputFormat, layoutEngine);
9197
if (!retVal) {
9298
throw new Error(this._wasm.Main.prototype.lastError());

test/graphviz.spec.js

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ describe("graphvizSync", function () {
136136
graphviz = gv;
137137
expect(graphviz).to.exist;
138138
});
139-
})
139+
});
140140
it("circo", function () {
141141
const svg = graphviz.circo(dot, "svg");
142142
expect(svg).to.be.a("string");
@@ -209,3 +209,22 @@ describe("bad dot", function () {
209209
expect(success).to.be.false;
210210
});
211211
});
212+
213+
describe("yInvert", function () {
214+
let graphviz;
215+
let plain1;
216+
let plain2;
217+
it("create", function () {
218+
return hpccWasm.graphvizSync().then(gv => {
219+
graphviz = gv;
220+
expect(graphviz).to.exist;
221+
});
222+
});
223+
it("compare", function () {
224+
const plain1 = graphviz.dot(dot, "plain");
225+
const plain2 = graphviz.dot(dot, "plain", { yInvert: false });
226+
const plain3 = graphviz.dot(dot, "plain", { yInvert: true });
227+
expect(plain1).to.equal(plain2);
228+
expect(plain1).to.not.equal(plain3);
229+
});
230+
});

0 commit comments

Comments
 (0)