forked from yousafgill/draw.io
-
Notifications
You must be signed in to change notification settings - Fork 0
/
export2.html
413 lines (351 loc) · 11.4 KB
/
export2.html
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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<script>
var isLocalStorage = false;
var mxLoadStylesheets = false;
</script>
<!-- CSS for print output is needed for using current window -->
<style type="text/css">
@media print {
table.mxPageSelector { display: none; }
hr.mxPageBreak { display: none; }
}
@media screen {
table.mxPageSelector { position: fixed; right: 10px; top: 10px;font-family: Arial; font-size:10pt; border: solid 1px darkgray;background: white; border-collapse:collapse; }
table.mxPageSelector td { border: solid 1px gray; padding:4px; }
body.mxPage { background: gray; }
}
</style>
<link rel="stylesheet" href="mxgraph/css/common.css" charset="UTF-8" type="text/css">
<script src="js/app.min.js"></script>
<script>
// NOTE: SVG Output fixes missing symbols in AsciiMath
// but produces larger output with clipping problems
Editor.initMath();
// Workaround for varphi vs. phi export in MathJax on Phantom
// see https://github.com/mathjax/MathJax/issues/353
(function()
{
var authInit = MathJax.AuthorInit;
MathJax.AuthorInit = function()
{
authInit();
MathJax.Hub.Register.StartupHook('AsciiMath Jax Config', function()
{
var symbols = MathJax.InputJax.AsciiMath.AM.symbols;
for (var i = 0, m = symbols.length; i < m; i++)
{
if (symbols[i].input === 'phi')
{
symbols[i].output = '\u03C6'
}
else if (symbols[i].input === 'varphi')
{
symbols[i].output = '\u03D5'; i = m
}
}
});
};
})();
function render(data)
{
var graph = new Graph(document.getElementById('graph'));
data.border = parseInt(data.border) || 0;
data.w = parseFloat(data.w) || 0;
data.h = parseFloat(data.h) || 0;
data.scale = parseFloat(data.scale) || 1;
// Parses XML
var xmlDoc = mxUtils.parseXml(data.xml);
var diagrams = null;
var from = 0;
// Handles mxfile
if (xmlDoc.documentElement.nodeName == 'mxfile')
{
diagrams = xmlDoc.documentElement.getElementsByTagName('diagram');
if (diagrams.length > 0)
{
from = Math.max(0, Math.min(parseInt(data.from) || from, diagrams.length - 1));
var diagramNode = diagrams[from];
if (diagramNode != null)
{
xmlDoc = mxUtils.parseXml(graph.decompress(mxUtils.getTextContent(diagramNode)));
}
}
}
/**
* Implements %page% and %pagenumber% placeholders
*/
var graphGetGlobalVariable = graph.getGlobalVariable;
graph.getGlobalVariable = function(name)
{
if (name == 'page')
{
return (diagrams == null) ? 'Page-1' :
(diagrams[from].getAttribute('name') || ('Page-' + (from + 1)));
}
else if (name == 'pagenumber')
{
return from + 1;
}
return graphGetGlobalVariable.apply(this, arguments);
};
// Enables math typesetting
var math = xmlDoc.documentElement.getAttribute('math') == '1';
if (math)
{
mxClient.NO_FO = true;
}
// Createa graph instance
graph.foldingEnabled = false;
graph.setEnabled(false);
// Sets background image
var bgImg = xmlDoc.documentElement.getAttribute('backgroundImage');
if (bgImg != null)
{
bgImg = JSON.parse(bgImg);
graph.setBackgroundImage(new mxImage(bgImg.src, bgImg.width, bgImg.height));
}
// Parses XML into graph
var codec = new mxCodec(xmlDoc);
codec.decode(xmlDoc.documentElement, graph.getModel());
// Loads background color
var bg = (data.bg != null && data.bg.length > 0) ? data.bg : xmlDoc.documentElement.getAttribute('background');
// Normalizes values for transparent backgrounds
if (bg == 'none' || bg == '')
{
bg = null;
}
// Checks if export format supports transparent backgrounds
if (bg == null && data.format != 'gif' && data.format != 'png')
{
bg = '#ffffff';
}
// Sets background color on page
if (bg != null)
{
document.body.style.backgroundColor = bg;
}
// Sets initial value for PDF page background
graph.pdfPageVisible = false;
// Handles PDF output where the output should match the page format if the page is visible
if (data.format == 'pdf' && xmlDoc.documentElement.getAttribute('page') == '1' && data.w == 0 && data.h == 0)
{
var pw = xmlDoc.documentElement.getAttribute('pageWidth');
var ph = xmlDoc.documentElement.getAttribute('pageHeight');
graph.pdfPageVisible = true;
if (pw != null && ph != null)
{
graph.pageFormat = new mxRectangle(0, 0, parseFloat(pw), parseFloat(ph));
}
var ps = xmlDoc.documentElement.getAttribute('pageScale');
if (ps != null)
{
graph.pageScale = ps;
}
graph.getPageSize = function()
{
return new mxRectangle(0, 0, this.pageFormat.width * this.pageScale,
this.pageFormat.height * this.pageScale);
};
graph.getPageLayout = function()
{
var size = this.getPageSize();
var bounds = this.getGraphBounds();
if (bounds.width == 0 || bounds.height == 0)
{
return new mxRectangle(0, 0, 1, 1);
}
else
{
// Computes untransformed graph bounds
var x = Math.ceil(bounds.x / this.view.scale - this.view.translate.x);
var y = Math.ceil(bounds.y / this.view.scale - this.view.translate.y);
var w = Math.floor(bounds.width / this.view.scale);
var h = Math.floor(bounds.height / this.view.scale);
var x0 = Math.floor(x / size.width);
var y0 = Math.floor(y / size.height);
var w0 = Math.ceil((x + w) / size.width) - x0;
var h0 = Math.ceil((y + h) / size.height) - y0;
return new mxRectangle(x0, y0, w0, h0);
}
};
// Fits the number of background pages to the graph
graph.view.getBackgroundPageBounds = function()
{
var layout = this.graph.getPageLayout();
var page = this.graph.getPageSize();
return new mxRectangle(this.scale * (this.translate.x + layout.x * page.width),
this.scale * (this.translate.y + layout.y * page.height),
this.scale * layout.width * page.width,
this.scale * layout.height * page.height);
};
}
if (!graph.pdfPageVisible)
{
var b = graph.getGraphBounds();
// Floor is needed to keep rendering crisp
if (data.w > 0 && data.h > 0)
{
var s = Math.min(data.w / b.width, data.h / b.height);
graph.view.scaleAndTranslate(s,
Math.floor(data.border / s - b.x),
Math.floor(data.border / s - b.y));
}
else
{
graph.view.scaleAndTranslate(data.scale, Math.floor(data.border - b.x),
Math.floor(data.border - b.y));
}
}
else
{
// Disables border for PDF page export
data.border = 0;
// Moves to first page in page layout
var layout = graph.getPageLayout();
var page = graph.getPageSize();
var dx = layout.x * page.width;
var dy = layout.y * page.height;
if (dx != 0 || dy != 0)
{
graph.view.setTranslate(Math.floor(-dx), Math.floor(-dy));
}
}
// Gets the diagram bounds and sets the document size
var bounds = (graph.pdfPageVisible) ? graph.view.getBackgroundPageBounds() : graph.getGraphBounds();
bounds.width = Math.ceil(bounds.width + data.border);
bounds.height = Math.ceil(bounds.height + data.border);
// Waits for all images to finish loading
var cache = new Object();
var waitCounter = 1;
// Decrements waitCounter and invokes callback when finished
function decrementWaitCounter()
{
if (--waitCounter < 1)
{
if (typeof window.callPhantom === 'function')
{
window.callPhantom(bounds);
}
else if (window.console != null)
{
console.log('loading complete');
}
}
};
function waitForImages(tagName, attributeName)
{
var imgs = document.body.getElementsByTagName(tagName);
waitCounter += imgs.length;
for (var i = 0; i < imgs.length; i++)
{
// No load events for image elements in Phantom using indirection instead
var src = imgs[i].getAttribute(attributeName);
if (typeof window.callPhantom !== 'function' && window.console != null)
{
console.log((cache[src] == null) ? 'loading' : 'cached', src);
}
if (src != null && src.length > 0 && cache[src] == null)
{
cache[src] = new Image();
cache[src].onload = decrementWaitCounter;
cache[src].onerror = decrementWaitCounter;
cache[src].src = src;
}
else
{
decrementWaitCounter();
}
}
};
// Includes images in SVG and HTML labels
waitForImages('image', 'xlink:href');
waitForImages('img', 'src');
// Waits for MathJax to finish rendering
function renderMath(elt)
{
if (math && window.MathJax != null && window.MathJax.Hub != null)
{
waitCounter++;
Editor.MathJaxRender(elt);
// Asynchronous callback when MathJax has finished
window.MathJax.Hub.Queue(function ()
{
decrementWaitCounter();
});
}
}
// Converts the graph to a vertical sequence of pages for PDF export
if (graph.pdfPageVisible)
{
// Workaround to match available paper size
var printScale = 0.72
var pf = graph.pageFormat || mxConstants.PAGE_FORMAT_A4_PORTRAIT;
var scale = 1 / graph.pageScale;
var autoOrigin = false;
var border = 0;
// Negative coordinates are cropped or shifted if page visible
var gb = graph.getGraphBounds();
var x0 = 0;
var y0 = 0;
// Applies print scale
pf = mxRectangle.fromRectangle(pf);
pf.width = Math.ceil(pf.width * printScale);
pf.height = Math.ceil(pf.height * printScale);
scale *= printScale;
// Starts at first visible page
var layout = graph.getPageLayout();
x0 -= layout.x * pf.width;
y0 -= layout.y * pf.height;
var preview = new mxPrintPreview(graph, scale, pf, border, x0, y0);
preview.printBackgroundImage = true;
preview.autoOrigin = autoOrigin;
preview.backgroundColor = bg;
// Renders print output into this document and removes the graph container
preview.open(null, window);
graph.container.parentNode.removeChild(graph.container);
// Adds shadow
if (mxClient.IS_SVG && xmlDoc.documentElement.getAttribute('shadow') == '1')
{
var svgs = document.getElementsByTagName('svg');
for (var i = 0; i < svgs.length; i++)
{
var svg = svgs[i];
var filter = graph.addSvgShadow(svg, null, true);
filter.setAttribute('id', 'shadow-' + i);
svg.appendChild(filter);
svg.setAttribute('filter', 'url(#' + 'shadow-' + i + ')');
}
border = 7;
}
bounds = new mxRectangle(0, 0, pf.width, pf.height);
renderMath(graph.container.parentNode);
}
else
{
// Adds shadow
if (mxClient.IS_SVG && xmlDoc.documentElement.getAttribute('shadow') == '1')
{
graph.addSvgShadow(graph.view.canvas.ownerSVGElement, null, true);
graph.setShadowVisible(true);
bounds.width += 7;
bounds.height += 7;
}
renderMath(graph.container);
if (data.format != 'pdf')
{
document.body.style.width = Math.ceil(bounds.x + bounds.width) + 'px';
document.body.style.height = (Math.ceil(bounds.y + bounds.height) + 1) + 'px';
}
}
// Immediate return if not waiting for any content
decrementWaitCounter();
};
</script>
</head>
<body style="margin:0px;">
<div id="graph" style="width:100%;height:100%;"></div>
</body>
</html>