mxVsdxCanvas2D.js 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943
  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()
  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: onFilesLoaded
  37. *
  38. * Called after all pending files have finished loading.
  39. */
  40. mxVsdxCanvas2D.prototype.onFilesLoaded = function ()
  41. {
  42. // hook for subclassers
  43. };
  44. /**
  45. * Function: createElt
  46. *
  47. * Create a new geo section.
  48. */
  49. mxVsdxCanvas2D.prototype.createElt = function (name)
  50. {
  51. return (this.xmlDoc.createElementNS != null) ? this.xmlDoc.createElementNS(VsdxExport.prototype.XMLNS, name) :
  52. this.xmlDoc.createElement(name);
  53. };
  54. /**
  55. * Function: createGeoSec
  56. *
  57. * Create a new geo section.
  58. */
  59. mxVsdxCanvas2D.prototype.createGeoSec = function ()
  60. {
  61. if (this.geoSec != null)
  62. {
  63. this.shape.appendChild(this.geoSec);
  64. }
  65. var geoSec = this.createElt("Section");
  66. geoSec.setAttribute("N", "Geometry");
  67. geoSec.setAttribute("IX", this.geoIndex++);
  68. this.geoSec = geoSec;
  69. this.geoStepIndex = 1;
  70. this.lastX = 0;
  71. this.lastY = 0;
  72. this.lastMoveToX = 0;
  73. this.lastMoveToY = 0;
  74. };
  75. /**
  76. * Function: newShape
  77. *
  78. * Create a new shape.
  79. */
  80. mxVsdxCanvas2D.prototype.newShape = function (shape, cellState, xmlDoc)
  81. {
  82. this.geoIndex = 0;
  83. this.shape = shape;
  84. this.cellState = cellState;
  85. this.xmGeo = cellState.cell.geometry;
  86. this.xmlDoc = xmlDoc;
  87. this.geoSec = null;
  88. this.shapeImg = null;
  89. this.shapeType = "Shape";
  90. this.createGeoSec();
  91. };
  92. /**
  93. * Function: newEdge
  94. *
  95. * Create a new edge.
  96. */
  97. mxVsdxCanvas2D.prototype.newEdge = function (shape, cellState, xmlDoc)
  98. {
  99. this.shape = shape;
  100. this.cellState = cellState;
  101. this.xmGeo = cellState.cellBounds;
  102. var s = this.state;
  103. this.xmlDoc = xmlDoc;
  104. };
  105. /**
  106. * Function: endShape
  107. *
  108. * End current shape.
  109. */
  110. mxVsdxCanvas2D.prototype.endShape = function ()
  111. {
  112. if (this.shapeImg != null)
  113. {
  114. this.addForeignData(this.shapeImg.type, this.shapeImg.id);
  115. }
  116. };
  117. /**
  118. * Function: newPage
  119. *
  120. * Start a new page.
  121. */
  122. mxVsdxCanvas2D.prototype.newPage = function ()
  123. {
  124. this.images = [];
  125. };
  126. /**
  127. * Function: newPage
  128. *
  129. * Start a new page.
  130. */
  131. mxVsdxCanvas2D.prototype.getShapeType = function ()
  132. {
  133. return this.shapeType;
  134. };
  135. /**
  136. * Function: getShapeGeo
  137. *
  138. * return the current geo section.
  139. */
  140. mxVsdxCanvas2D.prototype.getShapeGeo = function ()
  141. {
  142. return this.geoSec;
  143. };
  144. /**
  145. * Function: createCellElemScaled
  146. *
  147. * Creates a cell element and scale the value.
  148. */
  149. mxVsdxCanvas2D.prototype.createCellElemScaled = function (name, val, formula)
  150. {
  151. return this.createCellElem(name, val / VsdxExport.prototype.CONVERSION_FACTOR, formula);
  152. };
  153. /**
  154. * Function: createCellElem
  155. *
  156. * Creates a cell element.
  157. */
  158. mxVsdxCanvas2D.prototype.createCellElem = function (name, val, formula)
  159. {
  160. var cell = this.createElt("Cell");
  161. cell.setAttribute("N", name);
  162. cell.setAttribute("V", val);
  163. if (formula) cell.setAttribute("F", formula);
  164. return cell;
  165. };
  166. mxVsdxCanvas2D.prototype.createRowScaled = function(type, index, x, y, a, b, c , d, xF, yF, aF, bF, cF, dF)
  167. {
  168. return this.createRowRel(type, index, x / VsdxExport.prototype.CONVERSION_FACTOR, y / VsdxExport.prototype.CONVERSION_FACTOR,
  169. a / VsdxExport.prototype.CONVERSION_FACTOR, b / VsdxExport.prototype.CONVERSION_FACTOR,
  170. c / VsdxExport.prototype.CONVERSION_FACTOR, d / VsdxExport.prototype.CONVERSION_FACTOR,
  171. xF, yF, aF, bF, cF, dF);
  172. };
  173. mxVsdxCanvas2D.prototype.createRowRel = function(type, index, x, y, a, b, c , d, xF, yF, aF, bF, cF, dF)
  174. {
  175. var row = this.createElt("Row");
  176. row.setAttribute("T", type);
  177. row.setAttribute("IX", index);
  178. row.appendChild(this.createCellElem("X", x, xF));
  179. row.appendChild(this.createCellElem("Y", y, yF));
  180. if (a != null) row.appendChild(this.createCellElem("A", a, aF));
  181. if (b != null) row.appendChild(this.createCellElem("B", b, bF));
  182. if (c != null) row.appendChild(this.createCellElem("C", c, cF));
  183. if (d != null) row.appendChild(this.createCellElem("D", d, dF));
  184. return row;
  185. };
  186. /**
  187. * Function: begin
  188. *
  189. * Extends superclass to create path.
  190. */
  191. mxVsdxCanvas2D.prototype.begin = function()
  192. {
  193. if (this.geoStepIndex > 1) this.createGeoSec();
  194. };
  195. /**
  196. * Function: rect
  197. *
  198. * Private helper function to create SVG elements
  199. */
  200. mxVsdxCanvas2D.prototype.rect = function(x, y, w, h)
  201. {
  202. if (this.geoStepIndex > 1) this.createGeoSec();
  203. var s = this.state;
  204. w = w * s.scale;
  205. h = h * s.scale;
  206. var geo = this.xmGeo;
  207. x = ((x - geo.x + s.dx) * s.scale) /w;
  208. y = ((geo.height - y + geo.y - s.dy) * s.scale) /h;
  209. this.geoSec.appendChild(this.createRowRel("RelMoveTo", this.geoStepIndex++, x, y));
  210. this.geoSec.appendChild(this.createRowRel("RelLineTo", this.geoStepIndex++, x + 1, y));
  211. this.geoSec.appendChild(this.createRowRel("RelLineTo", this.geoStepIndex++, x + 1, y - 1));
  212. this.geoSec.appendChild(this.createRowRel("RelLineTo", this.geoStepIndex++, x, y - 1));
  213. this.geoSec.appendChild(this.createRowRel("RelLineTo", this.geoStepIndex++, x, y));
  214. };
  215. /**
  216. * Function: roundrect
  217. *
  218. * Private helper function to create SVG elements
  219. */
  220. mxVsdxCanvas2D.prototype.roundrect = function(x, y, w, h, dx, dy)
  221. {
  222. this.rect(x, y, w, h);
  223. //TODO this assume dx and dy are equal and only one rounding is needed
  224. this.shape.appendChild(this.createCellElemScaled("Rounding", dx));
  225. };
  226. /**
  227. * Function: ellipse
  228. *
  229. * Private helper function to create SVG elements
  230. */
  231. mxVsdxCanvas2D.prototype.ellipse = function(x, y, w, h)
  232. {
  233. if (this.geoStepIndex > 1) this.createGeoSec();
  234. var s = this.state;
  235. w = w * s.scale;
  236. h = h * s.scale;
  237. var geo = this.xmGeo;
  238. var gh = geo.height * s.scale;
  239. var gw = geo.width * s.scale;
  240. x = (x - geo.x + s.dx) * s.scale;
  241. y = gh + (-y + geo.y - s.dy) * s.scale;
  242. var xWr = (x + w/2) / gw;
  243. var yHr = (y - h/2) / gh;
  244. var aWr = x / gw;
  245. var bHr = (y - h/2) / gh;
  246. var cWr = (x + w/2) / gw;
  247. var dHr = y / gh;
  248. this.geoSec.appendChild(this.createRowScaled("Ellipse", this.geoStepIndex++, x + w/2, y - h/2, x, y - h/2, x + w/2, y
  249. , "Width*" + xWr, "Height*" + yHr, "Width*" + aWr, "Height*" + bHr, "Width*" + cWr, "Height*" + dHr));
  250. };
  251. /**
  252. * Function: moveTo
  253. *
  254. * Moves the current path the given point.
  255. *
  256. * Parameters:
  257. *
  258. * x - Number that represents the x-coordinate of the point.
  259. * y - Number that represents the y-coordinate of the point.
  260. */
  261. mxVsdxCanvas2D.prototype.moveTo = function(x, y)
  262. {
  263. //MoveTo inside a geo usually produce incorrect fill
  264. if (this.geoStepIndex > 1) this.createGeoSec();
  265. this.lastMoveToX = x;
  266. this.lastMoveToY = y;
  267. this.lastX = x;
  268. this.lastY = y;
  269. var geo = this.xmGeo;
  270. var s = this.state;
  271. x = (x - geo.x + s.dx) * s.scale;
  272. y = (geo.height - y + geo.y - s.dy) * s.scale;
  273. var h = geo.height * s.scale;
  274. var w = geo.width * s.scale;
  275. this.geoSec.appendChild(this.createRowRel("RelMoveTo", this.geoStepIndex++, x/w, y/h));
  276. };
  277. /**
  278. * Function: lineTo
  279. *
  280. * Draws a line to the given coordinates.
  281. *
  282. * Parameters:
  283. *
  284. * x - Number that represents the x-coordinate of the endpoint.
  285. * y - Number that represents the y-coordinate of the endpoint.
  286. */
  287. mxVsdxCanvas2D.prototype.lineTo = function(x, y)
  288. {
  289. this.lastX = x;
  290. this.lastY = y;
  291. var geo = this.xmGeo;
  292. var s = this.state;
  293. x = (x - geo.x + s.dx) * s.scale;
  294. y = (geo.height - y + geo.y - s.dy) * s.scale;
  295. var h = geo.height * s.scale;
  296. var w = geo.width * s.scale;
  297. this.geoSec.appendChild(this.createRowRel("RelLineTo", this.geoStepIndex++, x/w, y/h));
  298. };
  299. /**
  300. * Function: quadTo
  301. *
  302. * Adds a quadratic curve to the current path.
  303. *
  304. * Parameters:
  305. *
  306. * x1 - Number that represents the x-coordinate of the control point.
  307. * y1 - Number that represents the y-coordinate of the control point.
  308. * x2 - Number that represents the x-coordinate of the endpoint.
  309. * y2 - Number that represents the y-coordinate of the endpoint.
  310. */
  311. mxVsdxCanvas2D.prototype.quadTo = function(x1, y1, x2, y2)
  312. {
  313. this.lastX = x2;
  314. this.lastY = y2;
  315. var s = this.state;
  316. var geo = this.xmGeo;
  317. var h = geo.height * s.scale;
  318. var w = geo.width * s.scale;
  319. x1 = (x1 - geo.x + s.dx) * s.scale;
  320. y1 = (geo.height - y1 + geo.y - s.dy) * s.scale;
  321. x2 = (x2 - geo.x + s.dx) * s.scale;
  322. y2 = (geo.height - y2 + geo.y - s.dy) * s.scale;
  323. x1 = x1 / w;
  324. y1 = y1 / h;
  325. x2 = x2 / w;
  326. y2 = y2 / h;
  327. this.geoSec.appendChild(this.createRowRel("RelQuadBezTo", this.geoStepIndex++, x2, y2, x1, y1));
  328. };
  329. /**
  330. * Function: curveTo
  331. *
  332. * Adds a bezier curve to the current path.
  333. *
  334. * Parameters:
  335. *
  336. * x1 - Number that represents the x-coordinate of the first control point.
  337. * y1 - Number that represents the y-coordinate of the first control point.
  338. * x2 - Number that represents the x-coordinate of the second control point.
  339. * y2 - Number that represents the y-coordinate of the second control point.
  340. * x3 - Number that represents the x-coordinate of the endpoint.
  341. * y3 - Number that represents the y-coordinate of the endpoint.
  342. */
  343. mxVsdxCanvas2D.prototype.curveTo = function(x1, y1, x2, y2, x3, y3)
  344. {
  345. this.lastX = x3;
  346. this.lastY = y3;
  347. var s = this.state;
  348. var geo = this.xmGeo;
  349. var h = geo.height * s.scale;
  350. var w = geo.width * s.scale;
  351. x1 = (x1 - geo.x + s.dx) * s.scale;
  352. y1 = (geo.height - y1 + geo.y - s.dy) * s.scale;
  353. x2 = (x2 - geo.x + s.dx) * s.scale;
  354. y2 = (geo.height - y2 + geo.y - s.dy) * s.scale;
  355. x3 = (x3 - geo.x + s.dx) * s.scale;
  356. y3 = (geo.height - y3 + geo.y - s.dy) * s.scale;
  357. x1 = x1 / w;
  358. y1 = y1 / h;
  359. x2 = x2 / w;
  360. y2 = y2 / h;
  361. x3 = x3 / w;
  362. y3 = y3 / h;
  363. this.geoSec.appendChild(this.createRowRel("RelCubBezTo", this.geoStepIndex++, x3, y3, x1, y1, x2, y2));
  364. };
  365. /**
  366. * Function: close
  367. *
  368. * Closes the current path.
  369. */
  370. mxVsdxCanvas2D.prototype.close = function()
  371. {
  372. //Closing with a line if last point != last MoveTo point
  373. if (this.lastMoveToX != this.lastX || this.lastMoveToY != this.lastY)
  374. this.lineTo(this.lastMoveToX, this.lastMoveToY);
  375. };
  376. /**
  377. * Function: addForeignData
  378. *
  379. * Add ForeignData to current shape using last image in the images array
  380. */
  381. mxVsdxCanvas2D.prototype.addForeignData = function(type, index)
  382. {
  383. var foreignData = this.createElt("ForeignData");
  384. foreignData.setAttribute("ForeignType", "Bitmap");
  385. type = type.toUpperCase();
  386. if (type != "BMP")
  387. foreignData.setAttribute("CompressionType", type);
  388. var rel = this.createElt("Rel");
  389. rel.setAttribute("r:id", "rId" + index);
  390. foreignData.appendChild(rel);
  391. this.shape.appendChild(foreignData);
  392. this.shapeType = "Foreign";
  393. };
  394. /**
  395. * Function: image
  396. *
  397. * Add image to vsdx file as a media (Foreign Object)
  398. */
  399. mxVsdxCanvas2D.prototype.image = function(x, y, w, h, src, aspect, flipH, flipV)
  400. {
  401. //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)
  402. var imgName = "image" + (this.images.length + 1) + ".";
  403. var type;
  404. if (src.indexOf("data:") == 0)
  405. {
  406. var p = src.indexOf("base64,");
  407. var base64 = src.substring(p + 7); //7 is the length of "base64,"
  408. type = src.substring(11, p-1); //5 is the length of "data:image/"
  409. imgName += type;
  410. this.zip.file("visio/media/" + imgName, base64, {base64: true});
  411. }
  412. else if (window.XMLHttpRequest) //URL src, fetch it
  413. {
  414. src = this.converter.convert(src);
  415. this.filesLoading++;
  416. var that = this;
  417. var p = src.lastIndexOf(".");
  418. type = src.substring(p+1);
  419. imgName += type;
  420. //The old browsers binary workaround doesn't work with jszip and converting to base64 encoding doesn't work also
  421. var xhr = new XMLHttpRequest();
  422. xhr.open('GET', src, true);
  423. xhr.responseType = 'arraybuffer';
  424. xhr.onreadystatechange = function(e)
  425. {
  426. if (this.readyState == 4)
  427. {
  428. if (this.status == 200)
  429. {
  430. that.zip.file("visio/media/" + imgName, this.response);
  431. }
  432. that.filesLoading--;
  433. if (that.filesLoading == 0)
  434. {
  435. that.onFilesLoaded();
  436. }
  437. }
  438. };
  439. xhr.send();
  440. }
  441. this.images.push(imgName);
  442. //TODO can a shape has more than one image?
  443. //We add one to the id as rId1 is reserved for the edges master
  444. this.shapeImg = {type: type, id: this.images.length + 1};
  445. //TODO support these!
  446. aspect = (aspect != null) ? aspect : true;
  447. flipH = (flipH != null) ? flipH : false;
  448. flipV = (flipV != null) ? flipV : false;
  449. var s = this.state;
  450. w = w * s.scale;
  451. h = h * s.scale;
  452. var geo = this.xmGeo;
  453. x = (x - geo.x + s.dx) * s.scale;
  454. y = (geo.height - y + geo.y - s.dy) * s.scale;
  455. this.shape.appendChild(this.createCellElemScaled("ImgOffsetX", x));
  456. this.shape.appendChild(this.createCellElemScaled("ImgOffsetY", y - h));
  457. this.shape.appendChild(this.createCellElemScaled("ImgWidth", w));
  458. this.shape.appendChild(this.createCellElemScaled("ImgHeight", h));
  459. // var s = this.state;
  460. // x += s.dx;
  461. // y += s.dy;
  462. //
  463. // if (s.alpha < 1 || s.fillAlpha < 1)
  464. // {
  465. // node.setAttribute('opacity', s.alpha * s.fillAlpha);
  466. // }
  467. //
  468. // var tr = this.state.transform || '';
  469. //
  470. // if (flipH || flipV)
  471. // {
  472. // var sx = 1;
  473. // var sy = 1;
  474. // var dx = 0;
  475. // var dy = 0;
  476. //
  477. // if (flipH)
  478. // {
  479. // sx = -1;
  480. // dx = -w - 2 * x;
  481. // }
  482. //
  483. // if (flipV)
  484. // {
  485. // sy = -1;
  486. // dy = -h - 2 * y;
  487. // }
  488. //
  489. // // Adds image tansformation to existing transform
  490. // tr += 'scale(' + sx + ',' + sy + ')translate(' + (dx * s.scale) + ',' + (dy * s.scale) + ')';
  491. // }
  492. //
  493. // if (tr.length > 0)
  494. // {
  495. // node.setAttribute('transform', tr);
  496. // }
  497. //
  498. // if (!this.pointerEvents)
  499. // {
  500. // node.setAttribute('pointer-events', 'none');
  501. // }
  502. };
  503. /**
  504. * Function: text
  505. *
  506. * Paints the given text. Possible values for format are empty string for
  507. * plain text and html for HTML markup. Background and border color as well
  508. * as clipping is not available in plain text labels for VML. HTML labels
  509. * are not available as part of shapes with no foreignObject support in SVG
  510. * (eg. IE9, IE10).
  511. *
  512. * Parameters:
  513. *
  514. * x - Number that represents the x-coordinate of the text.
  515. * y - Number that represents the y-coordinate of the text.
  516. * w - Number that represents the available width for the text or 0 for automatic width.
  517. * h - Number that represents the available height for the text or 0 for automatic height.
  518. * str - String that specifies the text to be painted.
  519. * align - String that represents the horizontal alignment.
  520. * valign - String that represents the vertical alignment.
  521. * wrap - Boolean that specifies if word-wrapping is enabled. Requires w > 0.
  522. * format - Empty string for plain text or 'html' for HTML markup.
  523. * overflow - Specifies the overflow behaviour of the label. Requires w > 0 and/or h > 0.
  524. * clip - Boolean that specifies if the label should be clipped. Requires w > 0 and/or h > 0.
  525. * rotation - Number that specifies the angle of the rotation around the anchor point of the text.
  526. * dir - Optional string that specifies the text direction. Possible values are rtl and lrt.
  527. */
  528. mxVsdxCanvas2D.prototype.text = function(x, y, w, h, str, align, valign, wrap, format, overflow, clip, rotation, dir)
  529. {
  530. var that = this;
  531. if (this.textEnabled && str != null)
  532. {
  533. if (mxUtils.isNode(str))
  534. {
  535. str = mxUtils.getOuterHtml(str);
  536. }
  537. //TODO support HTML text formatting and remaining attributes
  538. if (format == 'html')
  539. {
  540. if (mxUtils.getValue(this.cellState.style, 'nl2Br', '1') != '0')
  541. {
  542. // Removes newlines from HTML and converts breaks to newlines
  543. // to match the HTML output in plain text
  544. str = str.replace(/\n/g, '').replace(/<br\s*.?>/g, '\n');
  545. }
  546. // Removes HTML tags
  547. if (this.html2txtDiv == null)
  548. this.html2txtDiv = document.createElement('div');
  549. this.html2txtDiv.innerHTML = str;
  550. str = mxUtils.extractTextWithWhitespace(this.html2txtDiv.childNodes);
  551. }
  552. var s = this.state;
  553. var geo = this.xmGeo;
  554. w = w * s.scale;
  555. h = h * s.scale;
  556. var charSect = this.createElt("Section");
  557. charSect.setAttribute('N', 'Character');
  558. var pSect = this.createElt("Section");
  559. pSect.setAttribute('N', 'Paragraph');
  560. var text = this.createElt("Text");
  561. var rgb2hex = function (rgb){
  562. rgb = rgb.match(/^rgba?[\s+]?\([\s+]?(\d+)[\s+]?,[\s+]?(\d+)[\s+]?,[\s+]?(\d+)[\s+]?/i);
  563. return (rgb && rgb.length === 4) ? "#" +
  564. ("0" + parseInt(rgb[1],10).toString(16)).slice(-2) +
  565. ("0" + parseInt(rgb[2],10).toString(16)).slice(-2) +
  566. ("0" + parseInt(rgb[3],10).toString(16)).slice(-2) : '';
  567. };
  568. var rowIndex = 0, pIndex = 0;
  569. var calcW = 0, calcH = 0, lastW = 0, lastH = 0, lineH = 0;
  570. var createTextRow = function(styleMap, charSect, pSect, textEl, txt)
  571. {
  572. var fontSize = styleMap['fontSize'];
  573. var fontFamily = styleMap['fontFamily'];
  574. var strRect = mxUtils.getSizeForString(txt, fontSize, fontFamily);
  575. var wrapped = false;
  576. if (wrap && strRect.width > w)
  577. {
  578. strRect = mxUtils.getSizeForString(txt, fontSize, fontFamily, w);
  579. wrapped = true;
  580. }
  581. if (styleMap['blockElem'])
  582. {
  583. lastW += strRect.width;
  584. calcW = Math.min(Math.max(calcW, lastW), w);
  585. lastW = 0;
  586. lastH = Math.max(lastH, strRect.height);
  587. calcH += lastH + lineH;
  588. lineH = lastH;
  589. lastH = 0;
  590. }
  591. else
  592. {
  593. lastW += strRect.width;
  594. calcW = Math.min(Math.max(calcW, lastW), w);
  595. lastH = Math.max(lastH, strRect.height);
  596. calcH = Math.max(calcH, lastH);
  597. }
  598. var charRow = that.createElt("Row");
  599. charRow.setAttribute('IX', rowIndex);
  600. if (styleMap['fontColor']) charRow.appendChild(that.createCellElem("Color", styleMap['fontColor']));
  601. if (fontSize) charRow.appendChild(that.createCellElemScaled("Size", fontSize * 0.97)); //the magic number 0.97 is needed such that text do not overflow
  602. if (fontFamily) charRow.appendChild(that.createCellElem("Font", fontFamily));
  603. //0x00 No format
  604. //0x01 Specifies that the text run has a bold character property.
  605. //0x02 Specifies that the text run has an italic character property.
  606. //0x04 Specifies that the text run has an underline character property.
  607. //0x08 Specifies that the text run has a small caps character property.
  608. var style = 0;
  609. if (styleMap['bold']) style |= 0x11;
  610. if (styleMap['italic']) style |= 0x22;
  611. if (styleMap['underline']) style |= 0x4;
  612. charRow.appendChild(that.createCellElem("Style", style));
  613. charRow.appendChild(that.createCellElem("Case", "0"));
  614. charRow.appendChild(that.createCellElem("Pos", "0"));
  615. charRow.appendChild(that.createCellElem("FontScale", "1"));
  616. charRow.appendChild(that.createCellElem("Letterspace", "0"));
  617. charSect.appendChild(charRow);
  618. // var pRow = that.createElt("Row");
  619. // pRow.setAttribute('IX', pIndex);
  620. //
  621. // var align = 1; //center is default
  622. //
  623. // switch(styleMap['align'])
  624. // {
  625. // case 'left': align = 0; break;
  626. // case 'center': align = 1; break;
  627. // case 'right': align = 2; break;
  628. // }
  629. // pRow.appendChild(that.createCellElem("HorzAlign", align));
  630. // pRow.appendChild(that.createCellElem("SpLine", "-1.2"));
  631. // pSect.appendChild(pRow);
  632. // var pp = that.createElt("pp");
  633. // pp.setAttribute('IX', pIndex++);
  634. // textEl.appendChild(pp);
  635. var cp = that.createElt("cp");
  636. cp.setAttribute('IX', rowIndex++);
  637. textEl.appendChild(cp);
  638. var txtNode = that.xmlDoc.createTextNode(txt + (styleMap['blockElem']? "\n" : ""));
  639. textEl.appendChild(txtNode);
  640. };
  641. var processNodeChildren = function(ch, pStyle)
  642. {
  643. pStyle = pStyle || {};
  644. for (var i=0; i<ch.length; i++)
  645. {
  646. if (ch[i].nodeType == 3)
  647. { //#text
  648. var styleMap = {
  649. fontColor: pStyle['fontColor'] || that.cellState.style["fontColor"],
  650. fontSize: pStyle['fontSize'] || that.cellState.style["fontSize"],
  651. fontFamily: pStyle['fontFamily'] || that.cellState.style["fontFamily"],
  652. align: pStyle['align'] || that.cellState.style["align"],
  653. bold: pStyle['bold'],
  654. italic: pStyle['italic'],
  655. underline: pStyle['underline']
  656. };
  657. createTextRow(styleMap, charSect, pSect, text, ch[i].textContent);
  658. }
  659. else if (ch[i].nodeType == 1)
  660. { //element
  661. var nodeName = ch[i].nodeName.toUpperCase();
  662. var chLen = ch[i].childNodes.length;
  663. var style = window.getComputedStyle(ch[i], null);
  664. var styleMap = {
  665. bold: style.getPropertyValue('font-weight') == 'bold' || pStyle['bold'],
  666. italic: style.getPropertyValue('font-style') == 'italic' || pStyle['italic'],
  667. underline: style.getPropertyValue('text-decoration').indexOf('underline') >= 0 || pStyle['underline'],
  668. align: style.getPropertyValue('text-align'),
  669. fontColor: rgb2hex(style.getPropertyValue('color')),
  670. fontSize: parseFloat(style.getPropertyValue('font-size')),
  671. fontFamily: style.getPropertyValue('font-family').replace(/"/g, ''), //remove quotes
  672. blockElem: style.getPropertyValue('display') == 'block' || nodeName == "BR" || nodeName == "LI"
  673. };
  674. // if (nodeName == "OL" || nodeName == "UL")
  675. // {
  676. // var pRow = that.createElt("Row");
  677. // pRow.setAttribute('IX', pIndex);
  678. //
  679. // pRow.appendChild(that.createCellElem("HorzAlign", "0"));
  680. // pRow.appendChild(that.createCellElem("Bullet", "1"));
  681. // pSect.appendChild(pRow);
  682. //
  683. // var pp = that.createElt("pp");
  684. // pp.setAttribute('IX', pIndex++);
  685. // text.appendChild(pp);
  686. // }
  687. if (chLen > 0)
  688. {
  689. createTextRow(styleMap, charSect, pSect, text, ""); //to handle block elements if any
  690. processNodeChildren(ch[i].childNodes, styleMap);
  691. }
  692. else
  693. {
  694. createTextRow(styleMap, charSect, pSect, text, ch[i].textContent);
  695. }
  696. }
  697. }
  698. };
  699. if (format == 'html' && mxClient.IS_SVG)
  700. {
  701. //Get the actual HTML label node
  702. var ch = this.cellState.text.node.getElementsByTagName('div')[mxClient.NO_FO? 0 : 1].childNodes;
  703. processNodeChildren(ch, {});
  704. }
  705. else
  706. {
  707. //If it is not HTML or SVG, we fall back to removing html format
  708. var styleMap = {
  709. fontColor: that.cellState.style["fontColor"],
  710. fontSize: that.cellState.style["fontSize"],
  711. fontFamily: that.cellState.style["fontFamily"]
  712. };
  713. createTextRow(styleMap, charSect, pSect, text, str);
  714. }
  715. var wShift = 0;
  716. var hShift = 0;
  717. switch(align)
  718. {
  719. case "right": wShift = calcW/2; break;
  720. case "center": wShift = 0; break;
  721. case "left": wShift = -calcW/2; break;
  722. }
  723. switch(valign)
  724. {
  725. case "top": hShift = calcH/2; break;
  726. case "middle": hShift = 0; break;
  727. case "bottom": hShift = -calcH/2; break;
  728. }
  729. h = Math.max(h, calcH);
  730. w = Math.max(w, calcW);
  731. x = (x - geo.x + s.dx) * s.scale;
  732. y = (geo.height - y + geo.y - s.dy) * s.scale;
  733. var hw = w/2, hh = h/2;
  734. this.shape.appendChild(this.createCellElemScaled("TxtPinX", x));
  735. this.shape.appendChild(this.createCellElemScaled("TxtPinY", y));
  736. this.shape.appendChild(this.createCellElemScaled("TxtWidth", w));
  737. this.shape.appendChild(this.createCellElemScaled("TxtHeight", h));
  738. this.shape.appendChild(this.createCellElemScaled("TxtLocPinX", hw + wShift));
  739. this.shape.appendChild(this.createCellElemScaled("TxtLocPinY", hh + hShift));
  740. if (rotation != 0)
  741. this.shape.appendChild(this.createCellElemScaled("TxtAngle", (360 - rotation) * Math.PI / 180));
  742. this.shape.appendChild(charSect);
  743. // this.shape.appendChild(pSect);
  744. this.shape.appendChild(text);
  745. // if (overflow != null)
  746. // {
  747. // elem.setAttribute('overflow', overflow);
  748. // }
  749. //
  750. // if (clip != null)
  751. // {
  752. // elem.setAttribute('clip', (clip) ? '1' : '0');
  753. // }
  754. //
  755. // if (dir != null)
  756. // {
  757. // elem.setAttribute('dir', dir);
  758. // }
  759. }
  760. };
  761. /**
  762. * Function: rotate
  763. *
  764. * Sets the rotation of the canvas. Note that rotation cannot be concatenated.
  765. */
  766. mxVsdxCanvas2D.prototype.rotate = function(theta, flipH, flipV, cx, cy)
  767. {
  768. //Vsdx has flipX/Y support separate from rotation
  769. if (theta != 0)
  770. {
  771. var s = this.state;
  772. cx += s.dx;
  773. cy += s.dy;
  774. cx *= s.scale;
  775. cy *= s.scale;
  776. this.shape.appendChild(this.createCellElem("Angle", (360 - theta) * Math.PI / 180));
  777. s.rotation = s.rotation + theta;
  778. s.rotationCx = cx;
  779. s.rotationCy = cy;
  780. }
  781. };
  782. /**
  783. * Function: stroke
  784. *
  785. * Paints the outline of the current drawing buffer.
  786. */
  787. mxVsdxCanvas2D.prototype.stroke = function()
  788. {
  789. this.geoSec.appendChild(this.createCellElem("NoFill", "1"));
  790. this.geoSec.appendChild(this.createCellElem("NoLine", "0"));
  791. };
  792. /**
  793. * Function: fill
  794. *
  795. * Fills the current drawing buffer.
  796. */
  797. mxVsdxCanvas2D.prototype.fill = function()
  798. {
  799. this.geoSec.appendChild(this.createCellElem("NoFill", "0"));
  800. this.geoSec.appendChild(this.createCellElem("NoLine", "1"));
  801. };
  802. /**
  803. * Function: fillAndStroke
  804. *
  805. * Fills the current drawing buffer and its outline.
  806. */
  807. mxVsdxCanvas2D.prototype.fillAndStroke = function()
  808. {
  809. this.geoSec.appendChild(this.createCellElem("NoFill", "0"));
  810. this.geoSec.appendChild(this.createCellElem("NoLine", "0"));
  811. };