export2.html 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  5. <script>
  6. var isLocalStorage = false;
  7. var mxLoadStylesheets = false;
  8. </script>
  9. <!-- CSS for print output is needed for using current window -->
  10. <style type="text/css">
  11. @media print {
  12. table.mxPageSelector { display: none; }
  13. hr.mxPageBreak { display: none; }
  14. }
  15. @media screen {
  16. table.mxPageSelector { position: fixed; right: 10px; top: 10px;font-family: Arial; font-size:10pt; border: solid 1px darkgray;background: white; border-collapse:collapse; }
  17. table.mxPageSelector td { border: solid 1px gray; padding:4px; }
  18. body.mxPage { background: gray; }
  19. }
  20. </style>
  21. <link rel="stylesheet" href="mxgraph/css/common.css" charset="UTF-8" type="text/css">
  22. <script src="js/app.min.js"></script>
  23. <script>
  24. // NOTE: SVG Output fixes missing symbols in AsciiMath
  25. // but produces larger output with clipping problems
  26. Editor.initMath();
  27. // Workaround for varphi vs. phi export in MathJax on Phantom
  28. // see https://github.com/mathjax/MathJax/issues/353
  29. (function()
  30. {
  31. var authInit = MathJax.AuthorInit;
  32. MathJax.AuthorInit = function()
  33. {
  34. authInit();
  35. MathJax.Hub.Register.StartupHook('AsciiMath Jax Config', function()
  36. {
  37. var symbols = MathJax.InputJax.AsciiMath.AM.symbols;
  38. for (var i = 0, m = symbols.length; i < m; i++)
  39. {
  40. if (symbols[i].input === 'phi')
  41. {
  42. symbols[i].output = '\u03C6'
  43. }
  44. else if (symbols[i].input === 'varphi')
  45. {
  46. symbols[i].output = '\u03D5'; i = m
  47. }
  48. }
  49. });
  50. };
  51. })();
  52. function render(data)
  53. {
  54. var graph = new Graph(document.getElementById('graph'));
  55. data.border = parseInt(data.border) || 0;
  56. data.w = parseFloat(data.w) || 0;
  57. data.h = parseFloat(data.h) || 0;
  58. data.scale = parseFloat(data.scale) || 1;
  59. // Parses XML
  60. var xmlDoc = mxUtils.parseXml(data.xml);
  61. var diagrams = null;
  62. var from = 0;
  63. // Handles mxfile
  64. if (xmlDoc.documentElement.nodeName == 'mxfile')
  65. {
  66. diagrams = xmlDoc.documentElement.getElementsByTagName('diagram');
  67. if (diagrams.length > 0)
  68. {
  69. from = Math.max(0, Math.min(parseInt(data.from) || from, diagrams.length - 1));
  70. var diagramNode = diagrams[from];
  71. if (diagramNode != null)
  72. {
  73. xmlDoc = mxUtils.parseXml(graph.decompress(mxUtils.getTextContent(diagramNode)));
  74. }
  75. }
  76. }
  77. /**
  78. * Implements %page% and %pagenumber% placeholders
  79. */
  80. var graphGetGlobalVariable = graph.getGlobalVariable;
  81. graph.getGlobalVariable = function(name)
  82. {
  83. if (name == 'page')
  84. {
  85. return (diagrams == null) ? 'Page-1' :
  86. (diagrams[from].getAttribute('name') || ('Page-' + (from + 1)));
  87. }
  88. else if (name == 'pagenumber')
  89. {
  90. return from + 1;
  91. }
  92. return graphGetGlobalVariable.apply(this, arguments);
  93. };
  94. // Enables math typesetting
  95. var math = xmlDoc.documentElement.getAttribute('math') == '1';
  96. if (math)
  97. {
  98. mxClient.NO_FO = true;
  99. }
  100. // Createa graph instance
  101. graph.foldingEnabled = false;
  102. graph.setEnabled(false);
  103. // Sets background image
  104. var bgImg = xmlDoc.documentElement.getAttribute('backgroundImage');
  105. if (bgImg != null)
  106. {
  107. bgImg = JSON.parse(bgImg);
  108. graph.setBackgroundImage(new mxImage(bgImg.src, bgImg.width, bgImg.height));
  109. }
  110. // Parses XML into graph
  111. var codec = new mxCodec(xmlDoc);
  112. codec.decode(xmlDoc.documentElement, graph.getModel());
  113. // Loads background color
  114. var bg = (data.bg != null && data.bg.length > 0) ? data.bg : xmlDoc.documentElement.getAttribute('background');
  115. // Normalizes values for transparent backgrounds
  116. if (bg == 'none' || bg == '')
  117. {
  118. bg = null;
  119. }
  120. // Checks if export format supports transparent backgrounds
  121. if (bg == null && data.format != 'gif' && data.format != 'png')
  122. {
  123. bg = '#ffffff';
  124. }
  125. // Sets background color on page
  126. if (bg != null)
  127. {
  128. document.body.style.backgroundColor = bg;
  129. }
  130. // Sets initial value for PDF page background
  131. graph.pdfPageVisible = false;
  132. // Handles PDF output where the output should match the page format if the page is visible
  133. if (data.format == 'pdf' && xmlDoc.documentElement.getAttribute('page') == '1' && data.w == 0 && data.h == 0)
  134. {
  135. var pw = xmlDoc.documentElement.getAttribute('pageWidth');
  136. var ph = xmlDoc.documentElement.getAttribute('pageHeight');
  137. graph.pdfPageVisible = true;
  138. if (pw != null && ph != null)
  139. {
  140. graph.pageFormat = new mxRectangle(0, 0, parseFloat(pw), parseFloat(ph));
  141. }
  142. var ps = xmlDoc.documentElement.getAttribute('pageScale');
  143. if (ps != null)
  144. {
  145. graph.pageScale = ps;
  146. }
  147. graph.getPageSize = function()
  148. {
  149. return new mxRectangle(0, 0, this.pageFormat.width * this.pageScale,
  150. this.pageFormat.height * this.pageScale);
  151. };
  152. graph.getPageLayout = function()
  153. {
  154. var size = this.getPageSize();
  155. var bounds = this.getGraphBounds();
  156. if (bounds.width == 0 || bounds.height == 0)
  157. {
  158. return new mxRectangle(0, 0, 1, 1);
  159. }
  160. else
  161. {
  162. // Computes untransformed graph bounds
  163. var x = Math.ceil(bounds.x / this.view.scale - this.view.translate.x);
  164. var y = Math.ceil(bounds.y / this.view.scale - this.view.translate.y);
  165. var w = Math.floor(bounds.width / this.view.scale);
  166. var h = Math.floor(bounds.height / this.view.scale);
  167. var x0 = Math.floor(x / size.width);
  168. var y0 = Math.floor(y / size.height);
  169. var w0 = Math.ceil((x + w) / size.width) - x0;
  170. var h0 = Math.ceil((y + h) / size.height) - y0;
  171. return new mxRectangle(x0, y0, w0, h0);
  172. }
  173. };
  174. // Fits the number of background pages to the graph
  175. graph.view.getBackgroundPageBounds = function()
  176. {
  177. var layout = this.graph.getPageLayout();
  178. var page = this.graph.getPageSize();
  179. return new mxRectangle(this.scale * (this.translate.x + layout.x * page.width),
  180. this.scale * (this.translate.y + layout.y * page.height),
  181. this.scale * layout.width * page.width,
  182. this.scale * layout.height * page.height);
  183. };
  184. }
  185. if (!graph.pdfPageVisible)
  186. {
  187. var b = graph.getGraphBounds();
  188. // Floor is needed to keep rendering crisp
  189. if (data.w > 0 && data.h > 0)
  190. {
  191. var s = Math.min(data.w / b.width, data.h / b.height);
  192. graph.view.scaleAndTranslate(s,
  193. Math.floor(data.border / s - b.x),
  194. Math.floor(data.border / s - b.y));
  195. }
  196. else
  197. {
  198. graph.view.scaleAndTranslate(data.scale, Math.floor(data.border - b.x),
  199. Math.floor(data.border - b.y));
  200. }
  201. }
  202. else
  203. {
  204. // Disables border for PDF page export
  205. data.border = 0;
  206. // Moves to first page in page layout
  207. var layout = graph.getPageLayout();
  208. var page = graph.getPageSize();
  209. var dx = layout.x * page.width;
  210. var dy = layout.y * page.height;
  211. if (dx != 0 || dy != 0)
  212. {
  213. graph.view.setTranslate(Math.floor(-dx), Math.floor(-dy));
  214. }
  215. }
  216. // Gets the diagram bounds and sets the document size
  217. var bounds = (graph.pdfPageVisible) ? graph.view.getBackgroundPageBounds() : graph.getGraphBounds();
  218. bounds.width = Math.ceil(bounds.width + data.border);
  219. bounds.height = Math.ceil(bounds.height + data.border);
  220. // Waits for all images to finish loading
  221. var cache = new Object();
  222. var waitCounter = 1;
  223. // Decrements waitCounter and invokes callback when finished
  224. function decrementWaitCounter()
  225. {
  226. if (--waitCounter < 1)
  227. {
  228. if (typeof window.callPhantom === 'function')
  229. {
  230. window.callPhantom(bounds);
  231. }
  232. else if (window.console != null)
  233. {
  234. console.log('loading complete');
  235. }
  236. }
  237. };
  238. function waitForImages(tagName, attributeName)
  239. {
  240. var imgs = document.body.getElementsByTagName(tagName);
  241. waitCounter += imgs.length;
  242. for (var i = 0; i < imgs.length; i++)
  243. {
  244. // No load events for image elements in Phantom using indirection instead
  245. var src = imgs[i].getAttribute(attributeName);
  246. if (typeof window.callPhantom !== 'function' && window.console != null)
  247. {
  248. console.log((cache[src] == null) ? 'loading' : 'cached', src);
  249. }
  250. if (src != null && src.length > 0 && cache[src] == null)
  251. {
  252. cache[src] = new Image();
  253. cache[src].onload = decrementWaitCounter;
  254. cache[src].onerror = decrementWaitCounter;
  255. cache[src].src = src;
  256. }
  257. else
  258. {
  259. decrementWaitCounter();
  260. }
  261. }
  262. };
  263. // Includes images in SVG and HTML labels
  264. waitForImages('image', 'xlink:href');
  265. waitForImages('img', 'src');
  266. // Waits for MathJax to finish rendering
  267. function renderMath(elt)
  268. {
  269. if (math && window.MathJax != null && window.MathJax.Hub != null)
  270. {
  271. waitCounter++;
  272. Editor.MathJaxRender(elt);
  273. // Asynchronous callback when MathJax has finished
  274. window.MathJax.Hub.Queue(function ()
  275. {
  276. decrementWaitCounter();
  277. });
  278. }
  279. }
  280. // Converts the graph to a vertical sequence of pages for PDF export
  281. if (graph.pdfPageVisible)
  282. {
  283. // Workaround to match available paper size
  284. var printScale = 0.72
  285. var pf = graph.pageFormat || mxConstants.PAGE_FORMAT_A4_PORTRAIT;
  286. var scale = 1 / graph.pageScale;
  287. var autoOrigin = false;
  288. var border = 0;
  289. // Negative coordinates are cropped or shifted if page visible
  290. var gb = graph.getGraphBounds();
  291. var x0 = 0;
  292. var y0 = 0;
  293. // Applies print scale
  294. pf = mxRectangle.fromRectangle(pf);
  295. pf.width = Math.ceil(pf.width * printScale);
  296. pf.height = Math.ceil(pf.height * printScale);
  297. scale *= printScale;
  298. // Starts at first visible page
  299. var layout = graph.getPageLayout();
  300. x0 -= layout.x * pf.width;
  301. y0 -= layout.y * pf.height;
  302. var preview = new mxPrintPreview(graph, scale, pf, border, x0, y0);
  303. preview.printBackgroundImage = true;
  304. preview.autoOrigin = autoOrigin;
  305. preview.backgroundColor = bg;
  306. // Renders print output into this document and removes the graph container
  307. preview.open(null, window);
  308. graph.container.parentNode.removeChild(graph.container);
  309. // Adds shadow
  310. if (mxClient.IS_SVG && xmlDoc.documentElement.getAttribute('shadow') == '1')
  311. {
  312. var svgs = document.getElementsByTagName('svg');
  313. for (var i = 0; i < svgs.length; i++)
  314. {
  315. var svg = svgs[i];
  316. var filter = graph.addSvgShadow(svg, null, true);
  317. filter.setAttribute('id', 'shadow-' + i);
  318. svg.appendChild(filter);
  319. svg.setAttribute('filter', 'url(#' + 'shadow-' + i + ')');
  320. }
  321. border = 7;
  322. }
  323. bounds = new mxRectangle(0, 0, pf.width, pf.height);
  324. renderMath(graph.container.parentNode);
  325. }
  326. else
  327. {
  328. // Adds shadow
  329. if (mxClient.IS_SVG && xmlDoc.documentElement.getAttribute('shadow') == '1')
  330. {
  331. graph.addSvgShadow(graph.view.canvas.ownerSVGElement, null, true);
  332. graph.setShadowVisible(true);
  333. bounds.width += 7;
  334. bounds.height += 7;
  335. }
  336. renderMath(graph.container);
  337. if (data.format != 'pdf')
  338. {
  339. document.body.style.width = Math.ceil(bounds.x + bounds.width) + 'px';
  340. document.body.style.height = (Math.ceil(bounds.y + bounds.height) + 1) + 'px';
  341. }
  342. }
  343. // Immediate return if not waiting for any content
  344. decrementWaitCounter();
  345. };
  346. </script>
  347. </head>
  348. <body style="margin:0px;">
  349. <div id="graph" style="width:100%;height:100%;"></div>
  350. </body>
  351. </html>