export2.html 10.0 KB

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