mxVsdxCanvas2D.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675
  1. /**
  2. * Copyright (c) 2006-2017, JGraph Ltd
  3. */
  4. /**
  5. * Class: mxVsdxCanvas2D
  6. *
  7. * Constructor: mxVsdxCanvas2D
  8. *
  9. * Constructs a new abstract canvas.
  10. */
  11. function mxVsdxCanvas2D(zip)
  12. {
  13. mxAbstractCanvas2D.call(this);
  14. };
  15. /**
  16. * Extends mxAbstractCanvas2D
  17. */
  18. mxUtils.extend(mxVsdxCanvas2D, mxAbstractCanvas2D);
  19. /**
  20. * Variable: textEnabled
  21. *
  22. * Specifies if text output should be enabled. Default is true.
  23. */
  24. mxVsdxCanvas2D.prototype.textEnabled = true;
  25. /**
  26. * Function: init
  27. *
  28. * Initialize the canvas for a new vsdx file.
  29. */
  30. mxVsdxCanvas2D.prototype.init = function (zip)
  31. {
  32. this.filesLoading = 0;
  33. this.zip = zip;
  34. };
  35. /**
  36. * Function: createGeoSec
  37. *
  38. * Create a new geo section.
  39. */
  40. mxVsdxCanvas2D.prototype.createGeoSec = function ()
  41. {
  42. if (this.geoSec != null)
  43. {
  44. this.shape.appendChild(this.geoSec);
  45. }
  46. var geoSec = this.xmlDoc.createElement("Section");
  47. geoSec.setAttribute("N", "Geometry");
  48. geoSec.setAttribute("IX", this.geoIndex++);
  49. this.geoSec = geoSec;
  50. this.geoStepIndex = 1;
  51. this.lastX = 0;
  52. this.lastY = 0;
  53. this.lastMoveToX = 0;
  54. this.lastMoveToY = 0;
  55. };
  56. /**
  57. * Function: newShape
  58. *
  59. * Create a new shape.
  60. */
  61. mxVsdxCanvas2D.prototype.newShape = function (shape, xmGeo, xmlDoc)
  62. {
  63. this.geoIndex = 0;
  64. this.shape = shape;
  65. this.xmGeo = xmGeo;
  66. this.xmlDoc = xmlDoc;
  67. this.geoSec = null;
  68. this.shapeImg = null;
  69. this.shapeType = "Shape";
  70. this.createGeoSec();
  71. };
  72. /**
  73. * Function: endShape
  74. *
  75. * End current shape.
  76. */
  77. mxVsdxCanvas2D.prototype.endShape = function ()
  78. {
  79. if (this.shapeImg != null)
  80. {
  81. this.addForeignData(this.shapeImg.type, this.shapeImg.id);
  82. }
  83. };
  84. /**
  85. * Function: newPage
  86. *
  87. * Start a new page.
  88. */
  89. mxVsdxCanvas2D.prototype.newPage = function ()
  90. {
  91. this.images = [];
  92. };
  93. /**
  94. * Function: newPage
  95. *
  96. * Start a new page.
  97. */
  98. mxVsdxCanvas2D.prototype.getShapeType = function ()
  99. {
  100. return this.shapeType;
  101. };
  102. /**
  103. * Function: getShapeGeo
  104. *
  105. * return the current geo section.
  106. */
  107. mxVsdxCanvas2D.prototype.getShapeGeo = function ()
  108. {
  109. return this.geoSec;
  110. };
  111. /**
  112. * Function: createCellElemScaled
  113. *
  114. * Creates a cell element and scale the value.
  115. */
  116. mxVsdxCanvas2D.prototype.createCellElemScaled = function (name, val, formula)
  117. {
  118. return this.createCellElem(name, val / VsdxExport.prototype.CONVERSION_FACTOR, formula);
  119. };
  120. /**
  121. * Function: createCellElem
  122. *
  123. * Creates a cell element.
  124. */
  125. mxVsdxCanvas2D.prototype.createCellElem = function (name, val, formula)
  126. {
  127. var cell = this.xmlDoc.createElement("Cell");
  128. cell.setAttribute("N", name);
  129. cell.setAttribute("V", val);
  130. if (formula) cell.setAttribute("F", formula);
  131. return cell;
  132. };
  133. mxVsdxCanvas2D.prototype.createRowRel = function(type, index, x, y, a, b, c , d)
  134. {
  135. var row = this.xmlDoc.createElement("Row");
  136. row.setAttribute("T", type);
  137. row.setAttribute("IX", index);
  138. row.appendChild(this.createCellElem("X", x));
  139. row.appendChild(this.createCellElem("Y", y));
  140. if (a != null) row.appendChild(this.createCellElem("A", a));
  141. if (b != null) row.appendChild(this.createCellElem("B", b));
  142. if (c != null) row.appendChild(this.createCellElem("C", c));
  143. if (d != null) row.appendChild(this.createCellElem("D", d));
  144. return row;
  145. };
  146. /**
  147. * Function: begin
  148. *
  149. * Extends superclass to create path.
  150. */
  151. mxVsdxCanvas2D.prototype.begin = function()
  152. {
  153. if (this.geoStepIndex > 1) this.createGeoSec();
  154. };
  155. /**
  156. * Function: rect
  157. *
  158. * Private helper function to create SVG elements
  159. */
  160. mxVsdxCanvas2D.prototype.rect = function(x, y, w, h)
  161. {
  162. if (this.geoStepIndex > 1) this.createGeoSec();
  163. var s = this.state;
  164. w = w * s.scale;
  165. h = h * s.scale;
  166. var geo = this.xmGeo;
  167. x = ((x - geo.x + s.dx) * s.scale) /w;
  168. y = ((geo.height - y + geo.y - s.dy) * s.scale) /h;
  169. this.geoSec.appendChild(this.createRowRel("RelMoveTo", this.geoStepIndex++, x, y));
  170. this.geoSec.appendChild(this.createRowRel("RelLineTo", this.geoStepIndex++, x + 1, y));
  171. this.geoSec.appendChild(this.createRowRel("RelLineTo", this.geoStepIndex++, x + 1, y - 1));
  172. this.geoSec.appendChild(this.createRowRel("RelLineTo", this.geoStepIndex++, x, y - 1));
  173. this.geoSec.appendChild(this.createRowRel("RelLineTo", this.geoStepIndex++, x, y));
  174. };
  175. /**
  176. * Function: roundrect
  177. *
  178. * Private helper function to create SVG elements
  179. */
  180. mxVsdxCanvas2D.prototype.roundrect = function(x, y, w, h, dx, dy)
  181. {
  182. this.rect(x, y, w, h);
  183. //TODO this assume dx and dy are equal and only one rounding is needed
  184. this.shape.appendChild(this.createCellElemScaled("Rounding", dx));
  185. };
  186. /**
  187. * Function: ellipse
  188. *
  189. * Private helper function to create SVG elements
  190. */
  191. mxVsdxCanvas2D.prototype.ellipse = function(x, y, w, h)
  192. {
  193. if (this.geoStepIndex > 1) this.createGeoSec();
  194. var s = this.state;
  195. w = w * s.scale;
  196. h = h * s.scale;
  197. var geo = this.xmGeo;
  198. var gh = geo.height * s.scale;
  199. var gw = geo.width * s.scale;
  200. x = (x - geo.x + s.dx) * s.scale;
  201. y = gh + (-y + geo.y - s.dy) * s.scale;
  202. var hr = h/gh;
  203. var wr = w/gw;
  204. this.geoSec.appendChild(this.createRowRel("RelMoveTo", this.geoStepIndex++, x/gw, y/gh - hr * 0.5));
  205. var row = this.createRowRel("RelEllipticalArcTo", this.geoStepIndex++, x/gw, y/gh - hr * 0.5001, wr * 0.5 + x/gw, y/gh - hr, 0);
  206. row.appendChild(this.createCellElem("D", w/h, "Width/Height*"+(wr/hr)));
  207. this.geoSec.appendChild(row);
  208. };
  209. /**
  210. * Function: moveTo
  211. *
  212. * Moves the current path the given point.
  213. *
  214. * Parameters:
  215. *
  216. * x - Number that represents the x-coordinate of the point.
  217. * y - Number that represents the y-coordinate of the point.
  218. */
  219. mxVsdxCanvas2D.prototype.moveTo = function(x, y)
  220. {
  221. this.lastMoveToX = x;
  222. this.lastMoveToY = y;
  223. this.lastX = x;
  224. this.lastY = y;
  225. var geo = this.xmGeo;
  226. var s = this.state;
  227. x = (x - geo.x + s.dx) * s.scale;
  228. y = (geo.height - y + geo.y - s.dy) * s.scale;
  229. var h = geo.height * s.scale;
  230. var w = geo.width * s.scale;
  231. this.geoSec.appendChild(this.createRowRel("RelMoveTo", this.geoStepIndex++, x/w, y/h));
  232. };
  233. /**
  234. * Function: lineTo
  235. *
  236. * Draws a line to the given coordinates.
  237. *
  238. * Parameters:
  239. *
  240. * x - Number that represents the x-coordinate of the endpoint.
  241. * y - Number that represents the y-coordinate of the endpoint.
  242. */
  243. mxVsdxCanvas2D.prototype.lineTo = function(x, y)
  244. {
  245. this.lastX = x;
  246. this.lastY = y;
  247. var geo = this.xmGeo;
  248. var s = this.state;
  249. x = (x - geo.x + s.dx) * s.scale;
  250. y = (geo.height - y + geo.y - s.dy) * s.scale;
  251. var h = geo.height * s.scale;
  252. var w = geo.width * s.scale;
  253. this.geoSec.appendChild(this.createRowRel("RelLineTo", this.geoStepIndex++, x/w, y/h));
  254. };
  255. /**
  256. * Function: quadTo
  257. *
  258. * Adds a quadratic curve to the current path.
  259. *
  260. * Parameters:
  261. *
  262. * x1 - Number that represents the x-coordinate of the control point.
  263. * y1 - Number that represents the y-coordinate of the control point.
  264. * x2 - Number that represents the x-coordinate of the endpoint.
  265. * y2 - Number that represents the y-coordinate of the endpoint.
  266. */
  267. mxVsdxCanvas2D.prototype.quadTo = function(x1, y1, x2, y2)
  268. {
  269. this.lastX = x2;
  270. this.lastY = y2;
  271. var s = this.state;
  272. var geo = this.xmGeo;
  273. var h = geo.height * s.scale;
  274. var w = geo.width * s.scale;
  275. x1 = (x1 - geo.x + s.dx) * s.scale;
  276. y1 = (geo.height - y1 + geo.y - s.dy) * s.scale;
  277. x2 = (x2 - geo.x + s.dx) * s.scale;
  278. y2 = (geo.height - y2 + geo.y - s.dy) * s.scale;
  279. x1 = x1 / w;
  280. y1 = y1 / h;
  281. x2 = x2 / w;
  282. y2 = y2 / h;
  283. this.geoSec.appendChild(this.createRowRel("RelQuadBezTo", this.geoStepIndex++, x2, y2, x1, y1));
  284. };
  285. /**
  286. * Function: curveTo
  287. *
  288. * Adds a bezier curve to the current path.
  289. *
  290. * Parameters:
  291. *
  292. * x1 - Number that represents the x-coordinate of the first control point.
  293. * y1 - Number that represents the y-coordinate of the first control point.
  294. * x2 - Number that represents the x-coordinate of the second control point.
  295. * y2 - Number that represents the y-coordinate of the second control point.
  296. * x3 - Number that represents the x-coordinate of the endpoint.
  297. * y3 - Number that represents the y-coordinate of the endpoint.
  298. */
  299. mxVsdxCanvas2D.prototype.curveTo = function(x1, y1, x2, y2, x3, y3)
  300. {
  301. this.lastX = x3;
  302. this.lastY = y3;
  303. var s = this.state;
  304. var geo = this.xmGeo;
  305. var h = geo.height * s.scale;
  306. var w = geo.width * s.scale;
  307. x1 = (x1 - geo.x + s.dx) * s.scale;
  308. y1 = (geo.height - y1 + geo.y - s.dy) * s.scale;
  309. x2 = (x2 - geo.x + s.dx) * s.scale;
  310. y2 = (geo.height - y2 + geo.y - s.dy) * s.scale;
  311. x3 = (x3 - geo.x + s.dx) * s.scale;
  312. y3 = (geo.height - y3 + geo.y - s.dy) * s.scale;
  313. x1 = x1 / w;
  314. y1 = y1 / h;
  315. x2 = x2 / w;
  316. y2 = y2 / h;
  317. x3 = x3 / w;
  318. y3 = y3 / h;
  319. this.geoSec.appendChild(this.createRowRel("RelCubBezTo", this.geoStepIndex++, x3, y3, x1, y1, x2, y2));
  320. };
  321. /**
  322. * Function: close
  323. *
  324. * Closes the current path.
  325. */
  326. mxVsdxCanvas2D.prototype.close = function()
  327. {
  328. //Closing with a line if last point != last MoveTo point
  329. if (this.lastMoveToX != this.lastX || this.lastMoveToY != this.lastY)
  330. this.lineTo(this.lastMoveToX, this.lastMoveToY);
  331. };
  332. /**
  333. * Function: addForeignData
  334. *
  335. * Add ForeignData to current shape using last image in the images array
  336. */
  337. mxVsdxCanvas2D.prototype.addForeignData = function(type, index)
  338. {
  339. var foreignData = this.xmlDoc.createElement("ForeignData");
  340. foreignData.setAttribute("ForeignType", "Bitmap");
  341. type = type.toUpperCase();
  342. if (type != "BMP")
  343. foreignData.setAttribute("CompressionType", type);
  344. var rel = this.xmlDoc.createElement("Rel");
  345. rel.setAttribute("r:id", "rId" + index);
  346. foreignData.appendChild(rel);
  347. this.shape.appendChild(foreignData);
  348. this.shapeType = "Foreign";
  349. };
  350. /**
  351. * Function: image
  352. *
  353. * Add image to vsdx file as a media (Foreign Object)
  354. */
  355. mxVsdxCanvas2D.prototype.image = function(x, y, w, h, src, aspect, flipH, flipV)
  356. {
  357. //TODO image reusing, if the same image is used more than once, reuse it. Applicable for URLs specifically (but can also be applied to embedded ones)
  358. var imgName = "image" + (this.images.length + 1) + ".";
  359. var type;
  360. if (src.indexOf("data:") == 0)
  361. {
  362. var p = src.indexOf("base64,");
  363. var base64 = src.substring(p + 7); //7 is the length of "base64,"
  364. type = src.substring(11, p-1); //5 is the length of "data:image/"
  365. imgName += type;
  366. this.zip.file("visio/media/" + imgName, base64, {base64: true});
  367. }
  368. else if (window.XMLHttpRequest) //URL src, fetch it
  369. {
  370. src = this.converter.convert(src);
  371. this.filesLoading++;
  372. var that = this;
  373. var p = src.lastIndexOf(".");
  374. type = src.substring(p+1);
  375. imgName += type;
  376. //The old browsers binary workaround doesn't work with jszip and converting to base64 encoding doesn't work also
  377. var xhr = new XMLHttpRequest();
  378. xhr.open('GET', src, true);
  379. xhr.responseType = 'arraybuffer';
  380. xhr.onreadystatechange = function(e)
  381. {
  382. if (this.readyState == 4 && this.status == 200) {
  383. that.zip.file("visio/media/" + imgName, this.response);
  384. that.filesLoading--;
  385. }
  386. };
  387. xhr.send();
  388. }
  389. this.images.push(imgName);
  390. //TODO can a shape has more than one image?
  391. this.shapeImg = {type: type, id: this.images.length};
  392. //TODO support these!
  393. aspect = (aspect != null) ? aspect : true;
  394. flipH = (flipH != null) ? flipH : false;
  395. flipV = (flipV != null) ? flipV : false;
  396. var s = this.state;
  397. w = w * s.scale;
  398. h = h * s.scale;
  399. var geo = this.xmGeo;
  400. x = (x - geo.x + s.dx) * s.scale;
  401. y = (geo.height - y + geo.y - s.dy) * s.scale;
  402. this.shape.appendChild(this.createCellElemScaled("ImgOffsetX", x));
  403. this.shape.appendChild(this.createCellElemScaled("ImgOffsetY", y - h));
  404. this.shape.appendChild(this.createCellElemScaled("ImgWidth", w));
  405. this.shape.appendChild(this.createCellElemScaled("ImgHeight", h));
  406. // var s = this.state;
  407. // x += s.dx;
  408. // y += s.dy;
  409. //
  410. // if (s.alpha < 1 || s.fillAlpha < 1)
  411. // {
  412. // node.setAttribute('opacity', s.alpha * s.fillAlpha);
  413. // }
  414. //
  415. // var tr = this.state.transform || '';
  416. //
  417. // if (flipH || flipV)
  418. // {
  419. // var sx = 1;
  420. // var sy = 1;
  421. // var dx = 0;
  422. // var dy = 0;
  423. //
  424. // if (flipH)
  425. // {
  426. // sx = -1;
  427. // dx = -w - 2 * x;
  428. // }
  429. //
  430. // if (flipV)
  431. // {
  432. // sy = -1;
  433. // dy = -h - 2 * y;
  434. // }
  435. //
  436. // // Adds image tansformation to existing transform
  437. // tr += 'scale(' + sx + ',' + sy + ')translate(' + (dx * s.scale) + ',' + (dy * s.scale) + ')';
  438. // }
  439. //
  440. // if (tr.length > 0)
  441. // {
  442. // node.setAttribute('transform', tr);
  443. // }
  444. //
  445. // if (!this.pointerEvents)
  446. // {
  447. // node.setAttribute('pointer-events', 'none');
  448. // }
  449. };
  450. /**
  451. * Function: text
  452. *
  453. * Paints the given text. Possible values for format are empty string for
  454. * plain text and html for HTML markup. Background and border color as well
  455. * as clipping is not available in plain text labels for VML. HTML labels
  456. * are not available as part of shapes with no foreignObject support in SVG
  457. * (eg. IE9, IE10).
  458. *
  459. * Parameters:
  460. *
  461. * x - Number that represents the x-coordinate of the text.
  462. * y - Number that represents the y-coordinate of the text.
  463. * w - Number that represents the available width for the text or 0 for automatic width.
  464. * h - Number that represents the available height for the text or 0 for automatic height.
  465. * str - String that specifies the text to be painted.
  466. * align - String that represents the horizontal alignment.
  467. * valign - String that represents the vertical alignment.
  468. * wrap - Boolean that specifies if word-wrapping is enabled. Requires w > 0.
  469. * format - Empty string for plain text or 'html' for HTML markup.
  470. * overflow - Specifies the overflow behaviour of the label. Requires w > 0 and/or h > 0.
  471. * clip - Boolean that specifies if the label should be clipped. Requires w > 0 and/or h > 0.
  472. * rotation - Number that specifies the angle of the rotation around the anchor point of the text.
  473. * dir - Optional string that specifies the text direction. Possible values are rtl and lrt.
  474. */
  475. mxVsdxCanvas2D.prototype.text = function(x, y, w, h, str, align, valign, wrap, format, overflow, clip, rotation, dir)
  476. {
  477. if (this.textEnabled && str != null)
  478. {
  479. if (mxUtils.isNode(str))
  480. {
  481. str = mxUtils.getOuterHtml(str);
  482. }
  483. //TODO support HTML text formatting and remaining attributes
  484. var s = this.state;
  485. var geo = this.xmGeo;
  486. var strRect;
  487. // if (h == 0 || w == 0)
  488. // {
  489. // strRect = mxUtils.getSizeForString(str);
  490. // }
  491. // h = h > 0 ? h : strRect.height;
  492. // w = w > 0 ? w : strRect.width;
  493. h = h > 0 ? h : geo.height;
  494. w = w > 0 ? w : geo.width;
  495. w = w * s.scale;
  496. h = h * s.scale;
  497. x = (x - geo.x + s.dx) * s.scale;
  498. y = (geo.height - y + geo.y - s.dy) * s.scale;
  499. var hw = w/2, hh = h/2;
  500. this.shape.appendChild(this.createCellElemScaled("TxtPinX", x));
  501. this.shape.appendChild(this.createCellElemScaled("TxtPinY", y));
  502. this.shape.appendChild(this.createCellElemScaled("TxtWidth", w));
  503. this.shape.appendChild(this.createCellElemScaled("TxtHeight", h));
  504. this.shape.appendChild(this.createCellElemScaled("TxtLocPinX", hw));
  505. this.shape.appendChild(this.createCellElemScaled("TxtLocPinY", hh));
  506. this.shape.appendChild(this.createCellElemScaled("TxtAngle", rotation * Math.PI / 180));
  507. var text = this.xmlDoc.createElement("Text");
  508. text.textContent = str + "\n";
  509. this.shape.appendChild(text);
  510. //
  511. // var elem = this.createElement('text');
  512. // elem.setAttribute('x', this.format(x));
  513. // elem.setAttribute('y', this.format(y));
  514. // elem.setAttribute('w', this.format(w));
  515. // elem.setAttribute('h', this.format(h));
  516. // elem.setAttribute('str', str);
  517. //
  518. // if (align != null)
  519. // {
  520. // elem.setAttribute('align', align);
  521. // }
  522. //
  523. // if (valign != null)
  524. // {
  525. // elem.setAttribute('valign', valign);
  526. // }
  527. //
  528. // elem.setAttribute('wrap', (wrap) ? '1' : '0');
  529. //
  530. // if (format == null)
  531. // {
  532. // format = '';
  533. // }
  534. //
  535. // elem.setAttribute('format', format);
  536. //
  537. // if (overflow != null)
  538. // {
  539. // elem.setAttribute('overflow', overflow);
  540. // }
  541. //
  542. // if (clip != null)
  543. // {
  544. // elem.setAttribute('clip', (clip) ? '1' : '0');
  545. // }
  546. //
  547. // if (rotation != null)
  548. // {
  549. // elem.setAttribute('rotation', rotation);
  550. // }
  551. //
  552. // if (dir != null)
  553. // {
  554. // elem.setAttribute('dir', dir);
  555. // }
  556. //
  557. // this.root.appendChild(elem);
  558. }
  559. };
  560. /**
  561. * Function: stroke
  562. *
  563. * Paints the outline of the current drawing buffer.
  564. */
  565. mxVsdxCanvas2D.prototype.stroke = function()
  566. {
  567. this.geoSec.appendChild(this.createCellElem("NoFill", "1"));
  568. this.geoSec.appendChild(this.createCellElem("NoLine", "0"));
  569. };
  570. /**
  571. * Function: fill
  572. *
  573. * Fills the current drawing buffer.
  574. */
  575. mxVsdxCanvas2D.prototype.fill = function()
  576. {
  577. this.geoSec.appendChild(this.createCellElem("NoFill", "0"));
  578. this.geoSec.appendChild(this.createCellElem("NoLine", "1"));
  579. };
  580. /**
  581. * Function: fillAndStroke
  582. *
  583. * Fills the current drawing buffer and its outline.
  584. */
  585. mxVsdxCanvas2D.prototype.fillAndStroke = function()
  586. {
  587. this.geoSec.appendChild(this.createCellElem("NoFill", "0"));
  588. this.geoSec.appendChild(this.createCellElem("NoLine", "0"));
  589. };