export2.html 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  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 = parseInt(data.w) || 0;
  32. data.h = parseInt(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. graph.view.scaleAndTranslate(Math.min(data.w / b.width, data.h / b.height),
  166. Math.floor(-b.x + data.border), Math.floor(-b.y + data.border));
  167. }
  168. else
  169. {
  170. graph.view.setTranslate(Math.floor(-b.x + data.border), Math.floor(-b.y + data.border));
  171. }
  172. }
  173. else
  174. {
  175. // Disables border for PDF page export
  176. data.border = 0;
  177. // Moves to first page in page layout
  178. var layout = graph.getPageLayout();
  179. var page = graph.getPageSize();
  180. var dx = layout.x * page.width;
  181. var dy = layout.y * page.height;
  182. if (dx != 0 || dy != 0)
  183. {
  184. graph.view.setTranslate(Math.floor(-dx), Math.floor(-dy));
  185. }
  186. }
  187. // Gets the diagram bounds and sets the document size
  188. var bounds = (graph.pdfPageVisible) ? graph.view.getBackgroundPageBounds() : graph.getGraphBounds();
  189. bounds.width += data.border;
  190. bounds.height += data.border;
  191. // Waits for all images to finish loading
  192. var cache = new Object();
  193. var waitCounter = 1;
  194. // Decrements waitCounter and invokes callback when finished
  195. function decrementWaitCounter()
  196. {
  197. if (--waitCounter < 1)
  198. {
  199. if (typeof window.callPhantom === 'function')
  200. {
  201. window.callPhantom(bounds);
  202. }
  203. else if (window.console != null)
  204. {
  205. console.log('loading complete');
  206. }
  207. }
  208. };
  209. function waitForImages(tagName, attributeName)
  210. {
  211. var imgs = document.body.getElementsByTagName(tagName);
  212. waitCounter += imgs.length;
  213. for (var i = 0; i < imgs.length; i++)
  214. {
  215. // No load events for image elements in Phantom using indirection instead
  216. var src = imgs[i].getAttribute(attributeName);
  217. if (typeof window.callPhantom !== 'function' && window.console != null)
  218. {
  219. console.log((cache[src] == null) ? 'loading' : 'cached', src);
  220. }
  221. if (cache[src] == null)
  222. {
  223. cache[src] = new Image();
  224. cache[src].onload = decrementWaitCounter;
  225. cache[src].onerror = decrementWaitCounter;
  226. cache[src].src = src;
  227. }
  228. else
  229. {
  230. decrementWaitCounter();
  231. }
  232. }
  233. };
  234. // Includes images in SVG and HTML labels
  235. waitForImages('image', 'xlink:href');
  236. waitForImages('img', 'src');
  237. // Waits for MathJax to finish rendering
  238. function renderMath(elt)
  239. {
  240. if (math && window.MathJax != null && window.MathJax.Hub != null)
  241. {
  242. waitCounter++;
  243. Editor.MathJaxRender(elt);
  244. // Asynchronous callback when MathJax has finished
  245. window.MathJax.Hub.Queue(function ()
  246. {
  247. decrementWaitCounter();
  248. });
  249. }
  250. }
  251. // Converts the graph to a vertical sequence of pages for PDF export
  252. if (graph.pdfPageVisible)
  253. {
  254. // Workaround to match available paper size
  255. var printScale = 0.72
  256. var pf = graph.pageFormat || mxConstants.PAGE_FORMAT_A4_PORTRAIT;
  257. var scale = 1 / graph.pageScale;
  258. var autoOrigin = false;
  259. var border = 0;
  260. // Negative coordinates are cropped or shifted if page visible
  261. var gb = graph.getGraphBounds();
  262. var border = 0;
  263. var x0 = 0;
  264. var y0 = 0;
  265. // Applies print scale
  266. pf = mxRectangle.fromRectangle(pf);
  267. pf.width = Math.ceil(pf.width * printScale);
  268. pf.height = Math.ceil(pf.height * printScale);
  269. scale *= printScale;
  270. // Starts at first visible page
  271. var layout = graph.getPageLayout();
  272. x0 -= layout.x * pf.width;
  273. y0 -= layout.y * pf.height;
  274. var preview = new mxPrintPreview(graph, scale, pf, border, x0, y0);
  275. preview.printBackgroundImage = true;
  276. preview.autoOrigin = autoOrigin;
  277. preview.backgroundColor = bg;
  278. // Renders print output into this document and removes the graph container
  279. preview.open(null, window);
  280. graph.container.parentNode.removeChild(graph.container);
  281. bounds = new mxRectangle(0, 0, pf.width, pf.height);
  282. renderMath(graph.container.parentNode);
  283. }
  284. else
  285. {
  286. renderMath(graph.container);
  287. if (data.format != 'pdf')
  288. {
  289. document.body.style.width = Math.ceil(bounds.x + bounds.width) + 'px';
  290. document.body.style.height = (Math.ceil(bounds.y + bounds.height) + 1) + 'px';
  291. }
  292. }
  293. // Immediate return if not waiting for any content
  294. decrementWaitCounter();
  295. };
  296. </script>
  297. </head>
  298. <body style="margin:0px;">
  299. <div id="graph" style="width:100%;height:100%;"></div>
  300. </body>
  301. </html>