forked from lovasoa/whitebophir
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreateSVG.js
224 lines (216 loc) · 5.11 KB
/
createSVG.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
const fs = require("./fs_promises.js"),
path = require("path"),
wboPencilPoint =
require("../client-data/tools/pencil/wbo_pencil_point.js").wboPencilPoint;
function htmlspecialchars(str) {
if (typeof str !== "string") return "";
return str.replace(/[<>&"']/g, function (c) {
switch (c) {
case "<":
return "<";
case ">":
return ">";
case "&":
return "&";
case '"':
return """;
case "'":
return "'";
}
});
}
function renderPath(el, pathstring) {
return (
"<path " +
(el.id ? 'id="' + htmlspecialchars(el.id) + '" ' : "") +
'stroke-width="' +
(el.size | 0) +
'" ' +
(el.opacity ? 'opacity="' + parseFloat(el.opacity) + '" ' : "") +
'stroke="' +
htmlspecialchars(el.color) +
'" ' +
'd="' +
pathstring +
'" ' +
(el.deltax || el.deltay
? 'transform="translate(' + +el.deltax + "," + +el.deltay + ')"'
: "") +
"/>"
);
}
const Tools = {
/**
* @return {string}
*/
Text: function (el) {
return (
"<text " +
'id="' +
htmlspecialchars(el.id || "t") +
'" ' +
'x="' +
(el.x | 0) +
'" ' +
'y="' +
(el.y | 0) +
'" ' +
'font-size="' +
(el.size | 0) +
'" ' +
'fill="' +
htmlspecialchars(el.color || "#000") +
'" ' +
(el.deltax || el.deltay
? 'transform="translate(' +
(el.deltax || 0) +
"," +
(el.deltay || 0) +
')"'
: "") +
">" +
htmlspecialchars(el.txt || "") +
"</text>"
);
},
/**
* @return {string}
*/
Pencil: function (el) {
if (!el._children) return "";
let pts = el._children.reduce(function (pts, point) {
return wboPencilPoint(pts, point.x, point.y);
}, []);
const pathstring = pts
.map(function (op) {
return op.type + " " + op.values.join(" ");
})
.join(" ");
return renderPath(el, pathstring);
},
/**
* @return {string}
*/
Rectangle: function (el) {
return (
"<rect " +
(el.id ? 'id="' + htmlspecialchars(el.id) + '" ' : "") +
'x="' +
(el.x || 0) +
'" ' +
'y="' +
(el.y || 0) +
'" ' +
'width="' +
(el.x2 - el.x) +
'" ' +
'height="' +
(el.y2 - el.y) +
'" ' +
'stroke="' +
htmlspecialchars(el.color) +
'" ' +
'stroke-width="' +
(el.size | 0) +
'" ' +
(el.deltax || el.deltay
? 'transform="translate(' +
(el.deltax || 0) +
"," +
(el.deltay || 0) +
')"'
: "") +
"/>"
);
},
/**
* @return {string}
*/
Ellipse: function (el) {
const cx = Math.round((el.x2 + el.x) / 2);
const cy = Math.round((el.y2 + el.y) / 2);
const rx = Math.abs(el.x2 - el.x) / 2;
const ry = Math.abs(el.y2 - el.y) / 2;
const pathstring =
"M" +
(cx - rx) +
" " +
cy +
"a" +
rx +
"," +
ry +
" 0 1,0 " +
rx * 2 +
",0" +
"a" +
rx +
"," +
ry +
" 0 1,0 " +
rx * -2 +
",0";
return renderPath(el, pathstring);
},
/**
* @return {string}
*/
"Straight line": function (el) {
const pathstring = "M" + el.x + " " + el.y + "L" + el.x2 + " " + el.y2;
return renderPath(el, pathstring);
},
};
/**
* Writes the given board as an svg to the given writeable stream
* @param {Object[string, BoardElem]} obj
* @param {WritableStream} writeable
*/
async function toSVG(obj, writeable) {
const margin = 400;
const elems = Object.values(obj);
const dim = elems.reduce(
function (dim, elem) {
if (elem._children && elem._children.length) elem = elem._children[0];
return [
Math.max((elem.x + margin + (elem.deltax | 0)) | 0, dim[0]),
Math.max((elem.y + margin + (elem.deltay | 0)) | 0, dim[1]),
];
},
[margin, margin],
);
writeable.write(
'<svg xmlns="http://www.w3.org/2000/svg" version="1.1" ' +
'width="' +
dim[0] +
'" height="' +
dim[1] +
'">' +
'<defs><style type="text/css"><![CDATA[' +
'text {font-family:"Arial"}' +
"path {fill:none;stroke-linecap:round;stroke-linejoin:round;}" +
"rect {fill:none}" +
"]]></style></defs>",
);
await Promise.all(
elems.map(async function (elem) {
await Promise.resolve(); // Do not block the event loop
const renderFun = Tools[elem.tool];
if (renderFun) writeable.write(renderFun(elem));
else console.warn("Missing render function for tool", elem.tool);
}),
);
writeable.write("</svg>");
}
async function renderBoard(file, stream) {
const data = await fs.promises.readFile(file);
var board = JSON.parse(data);
return toSVG(board, stream);
}
if (require.main === module) {
const config = require("./configuration.js");
const HISTORY_FILE =
process.argv[2] || path.join(config.HISTORY_DIR, "board-anonymous.json");
renderBoard(HISTORY_FILE, process.stdout).catch(console.error.bind(console));
} else {
module.exports = { renderBoard: renderBoard };
}