export2.html 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  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. function render(data)
  28. {
  29. var graph = new Graph(document.getElementById('graph'));
  30. data.border = parseInt(data.border) || 0;
  31. data.w = parseFloat(data.w) || 0;
  32. data.h = parseFloat(data.h) || 0;
  33. data.scale = parseFloat(data.scale) || 1;
  34. // Parses XML
  35. var xmlDoc = mxUtils.parseXml(data.xml);
  36. var diagrams = null;
  37. var from = 0;
  38. // Handles mxfile
  39. if (xmlDoc.documentElement.nodeName == 'mxfile')
  40. {
  41. diagrams = xmlDoc.documentElement.getElementsByTagName('diagram');
  42. if (diagrams.length > 0)
  43. {
  44. from = Math.max(0, Math.min(parseInt(data.from) || from, diagrams.length - 1));
  45. var diagramNode = diagrams[from];
  46. if (diagramNode != null)
  47. {
  48. xmlDoc = mxUtils.parseXml(graph.decompress(mxUtils.getTextContent(diagramNode)));
  49. }
  50. }
  51. }
  52. /**
  53. * Implements %page% and %pagenumber% placeholders
  54. */
  55. var graphGetGlobalVariable = graph.getGlobalVariable;
  56. graph.getGlobalVariable = function(name)
  57. {
  58. if (name == 'page')
  59. {
  60. return (diagrams == null) ? 'Page-1' :
  61. (diagrams[from].getAttribute('name') || ('Page-' + (from + 1)));
  62. }
  63. else if (name == 'pagenumber')
  64. {
  65. return from + 1;
  66. }
  67. return graphGetGlobalVariable.apply(this, arguments);
  68. };
  69. // Enables math typesetting
  70. var math = xmlDoc.documentElement.getAttribute('math') == '1';
  71. if (math)
  72. {
  73. mxClient.NO_FO = true;
  74. }
  75. // Createa graph instance
  76. graph.foldingEnabled = false;
  77. graph.setEnabled(false);
  78. // Sets background image
  79. var bgImg = xmlDoc.documentElement.getAttribute('backgroundImage');
  80. if (bgImg != null)
  81. {
  82. bgImg = JSON.parse(bgImg);
  83. graph.setBackgroundImage(new mxImage(bgImg.src, bgImg.width, bgImg.height));
  84. }
  85. // Parses XML into graph
  86. var codec = new mxCodec(xmlDoc);
  87. codec.decode(xmlDoc.documentElement, graph.getModel());
  88. // Loads background color
  89. var bg = (data.bg != null && data.bg.length > 0) ? data.bg : xmlDoc.documentElement.getAttribute('background');
  90. // Normalizes values for transparent backgrounds
  91. if (bg == 'none' || bg == '')
  92. {
  93. bg = null;
  94. }
  95. // Checks if export format supports transparent backgrounds
  96. if (bg == null && data.format != 'gif' && data.format != 'png')
  97. {
  98. bg = '#ffffff';
  99. }
  100. // Sets background color on page
  101. if (bg != null)
  102. {
  103. document.body.style.backgroundColor = bg;
  104. }
  105. // Sets initial value for PDF page background
  106. graph.pdfPageVisible = false;
  107. // Handles PDF output where the output should match the page format if the page is visible
  108. if (data.format == 'pdf' && xmlDoc.documentElement.getAttribute('page') == '1' && data.w == 0 && data.h == 0)
  109. {
  110. var pw = xmlDoc.documentElement.getAttribute('pageWidth');
  111. var ph = xmlDoc.documentElement.getAttribute('pageHeight');
  112. graph.pdfPageVisible = true;
  113. if (pw != null && ph != null)
  114. {
  115. graph.pageFormat = new mxRectangle(0, 0, parseFloat(pw), parseFloat(ph));
  116. }
  117. var ps = xmlDoc.documentElement.getAttribute('pageScale');
  118. if (ps != null)
  119. {
  120. graph.pageScale = ps;
  121. }
  122. graph.getPageSize = function()
  123. {
  124. return new mxRectangle(0, 0, this.pageFormat.width * this.pageScale,
  125. this.pageFormat.height * this.pageScale);
  126. };
  127. graph.getPageLayout = function()
  128. {
  129. var size = this.getPageSize();
  130. var bounds = this.getGraphBounds();
  131. if (bounds.width == 0 || bounds.height == 0)
  132. {
  133. return new mxRectangle(0, 0, 1, 1);
  134. }
  135. else
  136. {
  137. // Computes untransformed graph bounds
  138. var x = Math.ceil(bounds.x / this.view.scale - this.view.translate.x);
  139. var y = Math.ceil(bounds.y / this.view.scale - this.view.translate.y);
  140. var w = Math.floor(bounds.width / this.view.scale);
  141. var h = Math.floor(bounds.height / this.view.scale);
  142. var x0 = Math.floor(x / size.width);
  143. var y0 = Math.floor(y / size.height);
  144. var w0 = Math.ceil((x + w) / size.width) - x0;
  145. var h0 = Math.ceil((y + h) / size.height) - y0;
  146. return new mxRectangle(x0, y0, w0, h0);
  147. }
  148. };
  149. // Fits the number of background pages to the graph
  150. graph.view.getBackgroundPageBounds = function()
  151. {
  152. var layout = this.graph.getPageLayout();
  153. var page = this.graph.getPageSize();
  154. return new mxRectangle(this.scale * (this.translate.x + layout.x * page.width),
  155. this.scale * (this.translate.y + layout.y * page.height),
  156. this.scale * layout.width * page.width,
  157. this.scale * layout.height * page.height);
  158. };
  159. }
  160. if (!graph.pdfPageVisible)
  161. {
  162. var b = graph.getGraphBounds();
  163. // Floor is needed to keep rendering crisp
  164. if (data.w > 0 && data.h > 0)
  165. {
  166. var s = Math.min(data.w / b.width, data.h / b.height);
  167. graph.view.scaleAndTranslate(s,
  168. Math.floor(data.border / s - b.x),
  169. Math.floor(data.border / s - b.y));
  170. }
  171. else
  172. {
  173. graph.view.scaleAndTranslate(data.scale, Math.floor(data.border - b.x),
  174. Math.floor(data.border - b.y));
  175. }
  176. }
  177. else
  178. {
  179. // Disables border for PDF page export
  180. data.border = 0;
  181. // Moves to first page in page layout
  182. var layout = graph.getPageLayout();
  183. var page = graph.getPageSize();
  184. var dx = layout.x * page.width;
  185. var dy = layout.y * page.height;
  186. if (dx != 0 || dy != 0)
  187. {
  188. graph.view.setTranslate(Math.floor(-dx), Math.floor(-dy));
  189. }
  190. }
  191. // Gets the diagram bounds and sets the document size
  192. var bounds = (graph.pdfPageVisible) ? graph.view.getBackgroundPageBounds() : graph.getGraphBounds();
  193. bounds.width = Math.ceil(bounds.width + data.border);
  194. bounds.height = Math.ceil(bounds.height + data.border);
  195. // Waits for all images to finish loading
  196. var cache = new Object();
  197. var waitCounter = 1;
  198. // Decrements waitCounter and invokes callback when finished
  199. function decrementWaitCounter()
  200. {
  201. if (--waitCounter < 1)
  202. {
  203. if (typeof window.callPhantom === 'function')
  204. {
  205. window.callPhantom(bounds);
  206. }
  207. else if (window.console != null)
  208. {
  209. console.log('loading complete');
  210. }
  211. }
  212. };
  213. function waitForImages(tagName, attributeName)
  214. {
  215. var imgs = document.body.getElementsByTagName(tagName);
  216. waitCounter += imgs.length;
  217. for (var i = 0; i < imgs.length; i++)
  218. {
  219. // No load events for image elements in Phantom using indirection instead
  220. var src = imgs[i].getAttribute(attributeName);
  221. if (typeof window.callPhantom !== 'function' && window.console != null)
  222. {
  223. console.log((cache[src] == null) ? 'loading' : 'cached', src);
  224. }
  225. if (cache[src] == null)
  226. {
  227. cache[src] = new Image();
  228. cache[src].onload = decrementWaitCounter;
  229. cache[src].onerror = decrementWaitCounter;
  230. cache[src].src = src;
  231. }
  232. else
  233. {
  234. decrementWaitCounter();
  235. }
  236. }
  237. };
  238. // Includes images in SVG and HTML labels
  239. waitForImages('image', 'xlink:href');
  240. waitForImages('img', 'src');
  241. // Waits for MathJax to finish rendering
  242. function renderMath(elt)
  243. {
  244. if (math && window.MathJax != null && window.MathJax.Hub != null)
  245. {
  246. waitCounter++;
  247. Editor.MathJaxRender(elt);
  248. // Asynchronous callback when MathJax has finished
  249. window.MathJax.Hub.Queue(function ()
  250. {
  251. decrementWaitCounter();
  252. });
  253. }
  254. }
  255. // Converts the graph to a vertical sequence of pages for PDF export
  256. if (graph.pdfPageVisible)
  257. {
  258. // Workaround to match available paper size
  259. var printScale = 0.72
  260. var pf = graph.pageFormat || mxConstants.PAGE_FORMAT_A4_PORTRAIT;
  261. var scale = 1 / graph.pageScale;
  262. var autoOrigin = false;
  263. var border = 0;
  264. // Negative coordinates are cropped or shifted if page visible
  265. var gb = graph.getGraphBounds();
  266. var border = 0;
  267. var x0 = 0;
  268. var y0 = 0;
  269. // Applies print scale
  270. pf = mxRectangle.fromRectangle(pf);
  271. pf.width = Math.ceil(pf.width * printScale);
  272. pf.height = Math.ceil(pf.height * printScale);
  273. scale *= printScale;
  274. // Starts at first visible page
  275. var layout = graph.getPageLayout();
  276. x0 -= layout.x * pf.width;
  277. y0 -= layout.y * pf.height;
  278. var preview = new mxPrintPreview(graph, scale, pf, border, x0, y0);
  279. preview.printBackgroundImage = true;
  280. preview.autoOrigin = autoOrigin;
  281. preview.backgroundColor = bg;
  282. // Renders print output into this document and removes the graph container
  283. preview.open(null, window);
  284. graph.container.parentNode.removeChild(graph.container);
  285. bounds = new mxRectangle(0, 0, pf.width, pf.height);
  286. renderMath(graph.container.parentNode);
  287. }
  288. else
  289. {
  290. renderMath(graph.container);
  291. if (data.format != 'pdf')
  292. {
  293. document.body.style.width = Math.ceil(bounds.x + bounds.width) + 'px';
  294. document.body.style.height = (Math.ceil(bounds.y + bounds.height) + 1) + 'px';
  295. }
  296. }
  297. // Immediate return if not waiting for any content
  298. decrementWaitCounter();
  299. };
  300. </script>
  301. </head>
  302. <body style="margin:0px;">
  303. <div id="graph" style="width:100%;height:100%;"></div>
  304. </body>
  305. </html>