export2.html 9.2 KB

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