mxXmlCanvas2D.js 28 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217
  1. /**
  2. * Copyright (c) 2006-2015, JGraph Ltd
  3. * Copyright (c) 2006-2015, Gaudenz Alder
  4. */
  5. /**
  6. * Class: mxXmlCanvas2D
  7. *
  8. * Base class for all canvases. The following methods make up the public
  9. * interface of the canvas 2D for all painting in mxGraph:
  10. *
  11. * - <save>, <restore>
  12. * - <scale>, <translate>, <rotate>
  13. * - <setAlpha>, <setFillAlpha>, <setStrokeAlpha>, <setFillColor>, <setGradient>,
  14. * <setStrokeColor>, <setStrokeWidth>, <setDashed>, <setDashPattern>, <setLineCap>,
  15. * <setLineJoin>, <setMiterLimit>
  16. * - <setFontColor>, <setFontBackgroundColor>, <setFontBorderColor>, <setFontSize>,
  17. * <setFontFamily>, <setFontStyle>
  18. * - <setShadow>, <setShadowColor>, <setShadowAlpha>, <setShadowOffset>
  19. * - <rect>, <roundrect>, <ellipse>, <image>, <text>
  20. * - <begin>, <moveTo>, <lineTo>, <quadTo>, <curveTo>
  21. * - <stroke>, <fill>, <fillAndStroke>
  22. *
  23. * <mxAbstractCanvas2D.arcTo> is an additional method for drawing paths. This is
  24. * a synthetic method, meaning that it is turned into a sequence of curves by
  25. * default. Subclassers may add native support for arcs.
  26. *
  27. * Constructor: mxXmlCanvas2D
  28. *
  29. * Constructs a new abstract canvas.
  30. */
  31. function mxXmlCanvas2D(root)
  32. {
  33. mxAbstractCanvas2D.call(this);
  34. /**
  35. * Variable: root
  36. *
  37. * Reference to the container for the SVG content.
  38. */
  39. this.root = root;
  40. // Writes default settings;
  41. this.writeDefaults();
  42. };
  43. /**
  44. * Extends mxAbstractCanvas2D
  45. */
  46. mxUtils.extend(mxXmlCanvas2D, mxAbstractCanvas2D);
  47. /**
  48. * Variable: textEnabled
  49. *
  50. * Specifies if text output should be enabled. Default is true.
  51. */
  52. mxXmlCanvas2D.prototype.textEnabled = true;
  53. /**
  54. * Variable: compressed
  55. *
  56. * Specifies if the output should be compressed by removing redundant calls.
  57. * Default is true.
  58. */
  59. mxXmlCanvas2D.prototype.compressed = true;
  60. /**
  61. * Function: writeDefaults
  62. *
  63. * Writes the rendering defaults to <root>:
  64. */
  65. mxXmlCanvas2D.prototype.writeDefaults = function()
  66. {
  67. var elem;
  68. // Writes font defaults
  69. elem = this.createElement('fontfamily');
  70. elem.setAttribute('family', mxConstants.DEFAULT_FONTFAMILY);
  71. this.root.appendChild(elem);
  72. elem = this.createElement('fontsize');
  73. elem.setAttribute('size', mxConstants.DEFAULT_FONTSIZE);
  74. this.root.appendChild(elem);
  75. // Writes shadow defaults
  76. elem = this.createElement('shadowcolor');
  77. elem.setAttribute('color', mxConstants.SHADOWCOLOR);
  78. this.root.appendChild(elem);
  79. elem = this.createElement('shadowalpha');
  80. elem.setAttribute('alpha', mxConstants.SHADOW_OPACITY);
  81. this.root.appendChild(elem);
  82. elem = this.createElement('shadowoffset');
  83. elem.setAttribute('dx', mxConstants.SHADOW_OFFSET_X);
  84. elem.setAttribute('dy', mxConstants.SHADOW_OFFSET_Y);
  85. this.root.appendChild(elem);
  86. };
  87. /**
  88. * Function: format
  89. *
  90. * Returns a formatted number with 2 decimal places.
  91. */
  92. mxXmlCanvas2D.prototype.format = function(value)
  93. {
  94. return parseFloat(parseFloat(value).toFixed(2));
  95. };
  96. /**
  97. * Function: createElement
  98. *
  99. * Creates the given element using the owner document of <root>.
  100. */
  101. mxXmlCanvas2D.prototype.createElement = function(name)
  102. {
  103. return this.root.ownerDocument.createElement(name);
  104. };
  105. /**
  106. * Function: save
  107. *
  108. * Saves the drawing state.
  109. */
  110. mxXmlCanvas2D.prototype.save = function()
  111. {
  112. if (this.compressed)
  113. {
  114. mxAbstractCanvas2D.prototype.save.apply(this, arguments);
  115. }
  116. this.root.appendChild(this.createElement('save'));
  117. };
  118. /**
  119. * Function: restore
  120. *
  121. * Restores the drawing state.
  122. */
  123. mxXmlCanvas2D.prototype.restore = function()
  124. {
  125. if (this.compressed)
  126. {
  127. mxAbstractCanvas2D.prototype.restore.apply(this, arguments);
  128. }
  129. this.root.appendChild(this.createElement('restore'));
  130. };
  131. /**
  132. * Function: scale
  133. *
  134. * Scales the output.
  135. *
  136. * Parameters:
  137. *
  138. * scale - Number that represents the scale where 1 is equal to 100%.
  139. */
  140. mxXmlCanvas2D.prototype.scale = function(value)
  141. {
  142. var elem = this.createElement('scale');
  143. elem.setAttribute('scale', value);
  144. this.root.appendChild(elem);
  145. };
  146. /**
  147. * Function: translate
  148. *
  149. * Translates the output.
  150. *
  151. * Parameters:
  152. *
  153. * dx - Number that specifies the horizontal translation.
  154. * dy - Number that specifies the vertical translation.
  155. */
  156. mxXmlCanvas2D.prototype.translate = function(dx, dy)
  157. {
  158. var elem = this.createElement('translate');
  159. elem.setAttribute('dx', this.format(dx));
  160. elem.setAttribute('dy', this.format(dy));
  161. this.root.appendChild(elem);
  162. };
  163. /**
  164. * Function: rotate
  165. *
  166. * Rotates and/or flips the output around a given center. (Note: Due to
  167. * limitations in VML, the rotation cannot be concatenated.)
  168. *
  169. * Parameters:
  170. *
  171. * theta - Number that represents the angle of the rotation (in degrees).
  172. * flipH - Boolean indicating if the output should be flipped horizontally.
  173. * flipV - Boolean indicating if the output should be flipped vertically.
  174. * cx - Number that represents the x-coordinate of the rotation center.
  175. * cy - Number that represents the y-coordinate of the rotation center.
  176. */
  177. mxXmlCanvas2D.prototype.rotate = function(theta, flipH, flipV, cx, cy)
  178. {
  179. var elem = this.createElement('rotate');
  180. if (theta != 0 || flipH || flipV)
  181. {
  182. elem.setAttribute('theta', this.format(theta));
  183. elem.setAttribute('flipH', (flipH) ? '1' : '0');
  184. elem.setAttribute('flipV', (flipV) ? '1' : '0');
  185. elem.setAttribute('cx', this.format(cx));
  186. elem.setAttribute('cy', this.format(cy));
  187. this.root.appendChild(elem);
  188. }
  189. };
  190. /**
  191. * Function: setAlpha
  192. *
  193. * Sets the current alpha.
  194. *
  195. * Parameters:
  196. *
  197. * value - Number that represents the new alpha. Possible values are between
  198. * 1 (opaque) and 0 (transparent).
  199. */
  200. mxXmlCanvas2D.prototype.setAlpha = function(value)
  201. {
  202. if (this.compressed)
  203. {
  204. if (this.state.alpha == value)
  205. {
  206. return;
  207. }
  208. mxAbstractCanvas2D.prototype.setAlpha.apply(this, arguments);
  209. }
  210. var elem = this.createElement('alpha');
  211. elem.setAttribute('alpha', this.format(value));
  212. this.root.appendChild(elem);
  213. };
  214. /**
  215. * Function: setFillAlpha
  216. *
  217. * Sets the current fill alpha.
  218. *
  219. * Parameters:
  220. *
  221. * value - Number that represents the new fill alpha. Possible values are between
  222. * 1 (opaque) and 0 (transparent).
  223. */
  224. mxXmlCanvas2D.prototype.setFillAlpha = function(value)
  225. {
  226. if (this.compressed)
  227. {
  228. if (this.state.fillAlpha == value)
  229. {
  230. return;
  231. }
  232. mxAbstractCanvas2D.prototype.setFillAlpha.apply(this, arguments);
  233. }
  234. var elem = this.createElement('fillalpha');
  235. elem.setAttribute('alpha', this.format(value));
  236. this.root.appendChild(elem);
  237. };
  238. /**
  239. * Function: setStrokeAlpha
  240. *
  241. * Sets the current stroke alpha.
  242. *
  243. * Parameters:
  244. *
  245. * value - Number that represents the new stroke alpha. Possible values are between
  246. * 1 (opaque) and 0 (transparent).
  247. */
  248. mxXmlCanvas2D.prototype.setStrokeAlpha = function(value)
  249. {
  250. if (this.compressed)
  251. {
  252. if (this.state.strokeAlpha == value)
  253. {
  254. return;
  255. }
  256. mxAbstractCanvas2D.prototype.setStrokeAlpha.apply(this, arguments);
  257. }
  258. var elem = this.createElement('strokealpha');
  259. elem.setAttribute('alpha', this.format(value));
  260. this.root.appendChild(elem);
  261. };
  262. /**
  263. * Function: setFillColor
  264. *
  265. * Sets the current fill color.
  266. *
  267. * Parameters:
  268. *
  269. * value - Hexadecimal representation of the color or 'none'.
  270. */
  271. mxXmlCanvas2D.prototype.setFillColor = function(value)
  272. {
  273. if (value == mxConstants.NONE)
  274. {
  275. value = null;
  276. }
  277. if (this.compressed)
  278. {
  279. if (this.state.fillColor == value)
  280. {
  281. return;
  282. }
  283. mxAbstractCanvas2D.prototype.setFillColor.apply(this, arguments);
  284. }
  285. var elem = this.createElement('fillcolor');
  286. elem.setAttribute('color', (value != null) ? value : mxConstants.NONE);
  287. this.root.appendChild(elem);
  288. };
  289. /**
  290. * Function: setGradient
  291. *
  292. * Sets the gradient. Note that the coordinates may be ignored by some implementations.
  293. *
  294. * Parameters:
  295. *
  296. * color1 - Hexadecimal representation of the start color.
  297. * color2 - Hexadecimal representation of the end color.
  298. * x - X-coordinate of the gradient region.
  299. * y - y-coordinate of the gradient region.
  300. * w - Width of the gradient region.
  301. * h - Height of the gradient region.
  302. * direction - One of <mxConstants.DIRECTION_NORTH>, <mxConstants.DIRECTION_EAST>,
  303. * <mxConstants.DIRECTION_SOUTH> or <mxConstants.DIRECTION_WEST>.
  304. * alpha1 - Optional alpha of the start color. Default is 1. Possible values
  305. * are between 1 (opaque) and 0 (transparent).
  306. * alpha2 - Optional alpha of the end color. Default is 1. Possible values
  307. * are between 1 (opaque) and 0 (transparent).
  308. */
  309. mxXmlCanvas2D.prototype.setGradient = function(color1, color2, x, y, w, h, direction, alpha1, alpha2)
  310. {
  311. if (color1 != null && color2 != null)
  312. {
  313. mxAbstractCanvas2D.prototype.setGradient.apply(this, arguments);
  314. var elem = this.createElement('gradient');
  315. elem.setAttribute('c1', color1);
  316. elem.setAttribute('c2', color2);
  317. elem.setAttribute('x', this.format(x));
  318. elem.setAttribute('y', this.format(y));
  319. elem.setAttribute('w', this.format(w));
  320. elem.setAttribute('h', this.format(h));
  321. // Default direction is south
  322. if (direction != null)
  323. {
  324. elem.setAttribute('direction', direction);
  325. }
  326. if (alpha1 != null)
  327. {
  328. elem.setAttribute('alpha1', alpha1);
  329. }
  330. if (alpha2 != null)
  331. {
  332. elem.setAttribute('alpha2', alpha2);
  333. }
  334. this.root.appendChild(elem);
  335. }
  336. };
  337. /**
  338. * Function: setStrokeColor
  339. *
  340. * Sets the current stroke color.
  341. *
  342. * Parameters:
  343. *
  344. * value - Hexadecimal representation of the color or 'none'.
  345. */
  346. mxXmlCanvas2D.prototype.setStrokeColor = function(value)
  347. {
  348. if (value == mxConstants.NONE)
  349. {
  350. value = null;
  351. }
  352. if (this.compressed)
  353. {
  354. if (this.state.strokeColor == value)
  355. {
  356. return;
  357. }
  358. mxAbstractCanvas2D.prototype.setStrokeColor.apply(this, arguments);
  359. }
  360. var elem = this.createElement('strokecolor');
  361. elem.setAttribute('color', (value != null) ? value : mxConstants.NONE);
  362. this.root.appendChild(elem);
  363. };
  364. /**
  365. * Function: setStrokeWidth
  366. *
  367. * Sets the current stroke width.
  368. *
  369. * Parameters:
  370. *
  371. * value - Numeric representation of the stroke width.
  372. */
  373. mxXmlCanvas2D.prototype.setStrokeWidth = function(value)
  374. {
  375. if (this.compressed)
  376. {
  377. if (this.state.strokeWidth == value)
  378. {
  379. return;
  380. }
  381. mxAbstractCanvas2D.prototype.setStrokeWidth.apply(this, arguments);
  382. }
  383. var elem = this.createElement('strokewidth');
  384. elem.setAttribute('width', this.format(value));
  385. this.root.appendChild(elem);
  386. };
  387. /**
  388. * Function: setDashed
  389. *
  390. * Enables or disables dashed lines.
  391. *
  392. * Parameters:
  393. *
  394. * value - Boolean that specifies if dashed lines should be enabled.
  395. * value - Boolean that specifies if the stroke width should be ignored
  396. * for the dash pattern. Default is false.
  397. */
  398. mxXmlCanvas2D.prototype.setDashed = function(value, fixDash)
  399. {
  400. if (this.compressed)
  401. {
  402. if (this.state.dashed == value)
  403. {
  404. return;
  405. }
  406. mxAbstractCanvas2D.prototype.setDashed.apply(this, arguments);
  407. }
  408. var elem = this.createElement('dashed');
  409. elem.setAttribute('dashed', (value) ? '1' : '0');
  410. if (fixDash != null)
  411. {
  412. elem.setAttribute('fixDash', (fixDash) ? '1' : '0');
  413. }
  414. this.root.appendChild(elem);
  415. };
  416. /**
  417. * Function: setDashPattern
  418. *
  419. * Sets the current dash pattern. Default is '3 3'.
  420. *
  421. * Parameters:
  422. *
  423. * value - String that represents the dash pattern, which is a sequence of
  424. * numbers defining the length of the dashes and the length of the spaces
  425. * between the dashes. The lengths are relative to the line width - a length
  426. * of 1 is equals to the line width.
  427. */
  428. mxXmlCanvas2D.prototype.setDashPattern = function(value)
  429. {
  430. if (this.compressed)
  431. {
  432. if (this.state.dashPattern == value)
  433. {
  434. return;
  435. }
  436. mxAbstractCanvas2D.prototype.setDashPattern.apply(this, arguments);
  437. }
  438. var elem = this.createElement('dashpattern');
  439. elem.setAttribute('pattern', value);
  440. this.root.appendChild(elem);
  441. };
  442. /**
  443. * Function: setLineCap
  444. *
  445. * Sets the line cap. Default is 'flat' which corresponds to 'butt' in SVG.
  446. *
  447. * Parameters:
  448. *
  449. * value - String that represents the line cap. Possible values are flat, round
  450. * and square.
  451. */
  452. mxXmlCanvas2D.prototype.setLineCap = function(value)
  453. {
  454. if (this.compressed)
  455. {
  456. if (this.state.lineCap == value)
  457. {
  458. return;
  459. }
  460. mxAbstractCanvas2D.prototype.setLineCap.apply(this, arguments);
  461. }
  462. var elem = this.createElement('linecap');
  463. elem.setAttribute('cap', value);
  464. this.root.appendChild(elem);
  465. };
  466. /**
  467. * Function: setLineJoin
  468. *
  469. * Sets the line join. Default is 'miter'.
  470. *
  471. * Parameters:
  472. *
  473. * value - String that represents the line join. Possible values are miter,
  474. * round and bevel.
  475. */
  476. mxXmlCanvas2D.prototype.setLineJoin = function(value)
  477. {
  478. if (this.compressed)
  479. {
  480. if (this.state.lineJoin == value)
  481. {
  482. return;
  483. }
  484. mxAbstractCanvas2D.prototype.setLineJoin.apply(this, arguments);
  485. }
  486. var elem = this.createElement('linejoin');
  487. elem.setAttribute('join', value);
  488. this.root.appendChild(elem);
  489. };
  490. /**
  491. * Function: setMiterLimit
  492. *
  493. * Sets the miter limit. Default is 10.
  494. *
  495. * Parameters:
  496. *
  497. * value - Number that represents the miter limit.
  498. */
  499. mxXmlCanvas2D.prototype.setMiterLimit = function(value)
  500. {
  501. if (this.compressed)
  502. {
  503. if (this.state.miterLimit == value)
  504. {
  505. return;
  506. }
  507. mxAbstractCanvas2D.prototype.setMiterLimit.apply(this, arguments);
  508. }
  509. var elem = this.createElement('miterlimit');
  510. elem.setAttribute('limit', value);
  511. this.root.appendChild(elem);
  512. };
  513. /**
  514. * Function: setFontColor
  515. *
  516. * Sets the current font color. Default is '#000000'.
  517. *
  518. * Parameters:
  519. *
  520. * value - Hexadecimal representation of the color or 'none'.
  521. */
  522. mxXmlCanvas2D.prototype.setFontColor = function(value)
  523. {
  524. if (this.textEnabled)
  525. {
  526. if (value == mxConstants.NONE)
  527. {
  528. value = null;
  529. }
  530. if (this.compressed)
  531. {
  532. if (this.state.fontColor == value)
  533. {
  534. return;
  535. }
  536. mxAbstractCanvas2D.prototype.setFontColor.apply(this, arguments);
  537. }
  538. var elem = this.createElement('fontcolor');
  539. elem.setAttribute('color', (value != null) ? value : mxConstants.NONE);
  540. this.root.appendChild(elem);
  541. }
  542. };
  543. /**
  544. * Function: setFontBackgroundColor
  545. *
  546. * Sets the current font background color.
  547. *
  548. * Parameters:
  549. *
  550. * value - Hexadecimal representation of the color or 'none'.
  551. */
  552. mxXmlCanvas2D.prototype.setFontBackgroundColor = function(value)
  553. {
  554. if (this.textEnabled)
  555. {
  556. if (value == mxConstants.NONE)
  557. {
  558. value = null;
  559. }
  560. if (this.compressed)
  561. {
  562. if (this.state.fontBackgroundColor == value)
  563. {
  564. return;
  565. }
  566. mxAbstractCanvas2D.prototype.setFontBackgroundColor.apply(this, arguments);
  567. }
  568. var elem = this.createElement('fontbackgroundcolor');
  569. elem.setAttribute('color', (value != null) ? value : mxConstants.NONE);
  570. this.root.appendChild(elem);
  571. }
  572. };
  573. /**
  574. * Function: setFontBorderColor
  575. *
  576. * Sets the current font border color.
  577. *
  578. * Parameters:
  579. *
  580. * value - Hexadecimal representation of the color or 'none'.
  581. */
  582. mxXmlCanvas2D.prototype.setFontBorderColor = function(value)
  583. {
  584. if (this.textEnabled)
  585. {
  586. if (value == mxConstants.NONE)
  587. {
  588. value = null;
  589. }
  590. if (this.compressed)
  591. {
  592. if (this.state.fontBorderColor == value)
  593. {
  594. return;
  595. }
  596. mxAbstractCanvas2D.prototype.setFontBorderColor.apply(this, arguments);
  597. }
  598. var elem = this.createElement('fontbordercolor');
  599. elem.setAttribute('color', (value != null) ? value : mxConstants.NONE);
  600. this.root.appendChild(elem);
  601. }
  602. };
  603. /**
  604. * Function: setFontSize
  605. *
  606. * Sets the current font size. Default is <mxConstants.DEFAULT_FONTSIZE>.
  607. *
  608. * Parameters:
  609. *
  610. * value - Numeric representation of the font size.
  611. */
  612. mxXmlCanvas2D.prototype.setFontSize = function(value)
  613. {
  614. if (this.textEnabled)
  615. {
  616. if (this.compressed)
  617. {
  618. if (this.state.fontSize == value)
  619. {
  620. return;
  621. }
  622. mxAbstractCanvas2D.prototype.setFontSize.apply(this, arguments);
  623. }
  624. var elem = this.createElement('fontsize');
  625. elem.setAttribute('size', value);
  626. this.root.appendChild(elem);
  627. }
  628. };
  629. /**
  630. * Function: setFontFamily
  631. *
  632. * Sets the current font family. Default is <mxConstants.DEFAULT_FONTFAMILY>.
  633. *
  634. * Parameters:
  635. *
  636. * value - String representation of the font family. This handles the same
  637. * values as the CSS font-family property.
  638. */
  639. mxXmlCanvas2D.prototype.setFontFamily = function(value)
  640. {
  641. if (this.textEnabled)
  642. {
  643. if (this.compressed)
  644. {
  645. if (this.state.fontFamily == value)
  646. {
  647. return;
  648. }
  649. mxAbstractCanvas2D.prototype.setFontFamily.apply(this, arguments);
  650. }
  651. var elem = this.createElement('fontfamily');
  652. elem.setAttribute('family', value);
  653. this.root.appendChild(elem);
  654. }
  655. };
  656. /**
  657. * Function: setFontStyle
  658. *
  659. * Sets the current font style.
  660. *
  661. * Parameters:
  662. *
  663. * value - Numeric representation of the font family. This is the sum of the
  664. * font styles from <mxConstants>.
  665. */
  666. mxXmlCanvas2D.prototype.setFontStyle = function(value)
  667. {
  668. if (this.textEnabled)
  669. {
  670. if (value == null)
  671. {
  672. value = 0;
  673. }
  674. if (this.compressed)
  675. {
  676. if (this.state.fontStyle == value)
  677. {
  678. return;
  679. }
  680. mxAbstractCanvas2D.prototype.setFontStyle.apply(this, arguments);
  681. }
  682. var elem = this.createElement('fontstyle');
  683. elem.setAttribute('style', value);
  684. this.root.appendChild(elem);
  685. }
  686. };
  687. /**
  688. * Function: setShadow
  689. *
  690. * Enables or disables shadows.
  691. *
  692. * Parameters:
  693. *
  694. * value - Boolean that specifies if shadows should be enabled.
  695. */
  696. mxXmlCanvas2D.prototype.setShadow = function(value)
  697. {
  698. if (this.compressed)
  699. {
  700. if (this.state.shadow == value)
  701. {
  702. return;
  703. }
  704. mxAbstractCanvas2D.prototype.setShadow.apply(this, arguments);
  705. }
  706. var elem = this.createElement('shadow');
  707. elem.setAttribute('enabled', (value) ? '1' : '0');
  708. this.root.appendChild(elem);
  709. };
  710. /**
  711. * Function: setShadowColor
  712. *
  713. * Sets the current shadow color. Default is <mxConstants.SHADOWCOLOR>.
  714. *
  715. * Parameters:
  716. *
  717. * value - Hexadecimal representation of the color or 'none'.
  718. */
  719. mxXmlCanvas2D.prototype.setShadowColor = function(value)
  720. {
  721. if (this.compressed)
  722. {
  723. if (value == mxConstants.NONE)
  724. {
  725. value = null;
  726. }
  727. if (this.state.shadowColor == value)
  728. {
  729. return;
  730. }
  731. mxAbstractCanvas2D.prototype.setShadowColor.apply(this, arguments);
  732. }
  733. var elem = this.createElement('shadowcolor');
  734. elem.setAttribute('color', (value != null) ? value : mxConstants.NONE);
  735. this.root.appendChild(elem);
  736. };
  737. /**
  738. * Function: setShadowAlpha
  739. *
  740. * Sets the current shadows alpha. Default is <mxConstants.SHADOW_OPACITY>.
  741. *
  742. * Parameters:
  743. *
  744. * value - Number that represents the new alpha. Possible values are between
  745. * 1 (opaque) and 0 (transparent).
  746. */
  747. mxXmlCanvas2D.prototype.setShadowAlpha = function(value)
  748. {
  749. if (this.compressed)
  750. {
  751. if (this.state.shadowAlpha == value)
  752. {
  753. return;
  754. }
  755. mxAbstractCanvas2D.prototype.setShadowAlpha.apply(this, arguments);
  756. }
  757. var elem = this.createElement('shadowalpha');
  758. elem.setAttribute('alpha', value);
  759. this.root.appendChild(elem);
  760. };
  761. /**
  762. * Function: setShadowOffset
  763. *
  764. * Sets the current shadow offset.
  765. *
  766. * Parameters:
  767. *
  768. * dx - Number that represents the horizontal offset of the shadow.
  769. * dy - Number that represents the vertical offset of the shadow.
  770. */
  771. mxXmlCanvas2D.prototype.setShadowOffset = function(dx, dy)
  772. {
  773. if (this.compressed)
  774. {
  775. if (this.state.shadowDx == dx && this.state.shadowDy == dy)
  776. {
  777. return;
  778. }
  779. mxAbstractCanvas2D.prototype.setShadowOffset.apply(this, arguments);
  780. }
  781. var elem = this.createElement('shadowoffset');
  782. elem.setAttribute('dx', dx);
  783. elem.setAttribute('dy', dy);
  784. this.root.appendChild(elem);
  785. };
  786. /**
  787. * Function: rect
  788. *
  789. * Puts a rectangle into the drawing buffer.
  790. *
  791. * Parameters:
  792. *
  793. * x - Number that represents the x-coordinate of the rectangle.
  794. * y - Number that represents the y-coordinate of the rectangle.
  795. * w - Number that represents the width of the rectangle.
  796. * h - Number that represents the height of the rectangle.
  797. */
  798. mxXmlCanvas2D.prototype.rect = function(x, y, w, h)
  799. {
  800. var elem = this.createElement('rect');
  801. elem.setAttribute('x', this.format(x));
  802. elem.setAttribute('y', this.format(y));
  803. elem.setAttribute('w', this.format(w));
  804. elem.setAttribute('h', this.format(h));
  805. this.root.appendChild(elem);
  806. };
  807. /**
  808. * Function: roundrect
  809. *
  810. * Puts a rounded rectangle into the drawing buffer.
  811. *
  812. * Parameters:
  813. *
  814. * x - Number that represents the x-coordinate of the rectangle.
  815. * y - Number that represents the y-coordinate of the rectangle.
  816. * w - Number that represents the width of the rectangle.
  817. * h - Number that represents the height of the rectangle.
  818. * dx - Number that represents the horizontal rounding.
  819. * dy - Number that represents the vertical rounding.
  820. */
  821. mxXmlCanvas2D.prototype.roundrect = function(x, y, w, h, dx, dy)
  822. {
  823. var elem = this.createElement('roundrect');
  824. elem.setAttribute('x', this.format(x));
  825. elem.setAttribute('y', this.format(y));
  826. elem.setAttribute('w', this.format(w));
  827. elem.setAttribute('h', this.format(h));
  828. elem.setAttribute('dx', this.format(dx));
  829. elem.setAttribute('dy', this.format(dy));
  830. this.root.appendChild(elem);
  831. };
  832. /**
  833. * Function: ellipse
  834. *
  835. * Puts an ellipse into the drawing buffer.
  836. *
  837. * Parameters:
  838. *
  839. * x - Number that represents the x-coordinate of the ellipse.
  840. * y - Number that represents the y-coordinate of the ellipse.
  841. * w - Number that represents the width of the ellipse.
  842. * h - Number that represents the height of the ellipse.
  843. */
  844. mxXmlCanvas2D.prototype.ellipse = function(x, y, w, h)
  845. {
  846. var elem = this.createElement('ellipse');
  847. elem.setAttribute('x', this.format(x));
  848. elem.setAttribute('y', this.format(y));
  849. elem.setAttribute('w', this.format(w));
  850. elem.setAttribute('h', this.format(h));
  851. this.root.appendChild(elem);
  852. };
  853. /**
  854. * Function: image
  855. *
  856. * Paints an image.
  857. *
  858. * Parameters:
  859. *
  860. * x - Number that represents the x-coordinate of the image.
  861. * y - Number that represents the y-coordinate of the image.
  862. * w - Number that represents the width of the image.
  863. * h - Number that represents the height of the image.
  864. * src - String that specifies the URL of the image.
  865. * aspect - Boolean indicating if the aspect of the image should be preserved.
  866. * flipH - Boolean indicating if the image should be flipped horizontally.
  867. * flipV - Boolean indicating if the image should be flipped vertically.
  868. */
  869. mxXmlCanvas2D.prototype.image = function(x, y, w, h, src, aspect, flipH, flipV)
  870. {
  871. src = this.converter.convert(src);
  872. // LATER: Add option for embedding images as base64.
  873. var elem = this.createElement('image');
  874. elem.setAttribute('x', this.format(x));
  875. elem.setAttribute('y', this.format(y));
  876. elem.setAttribute('w', this.format(w));
  877. elem.setAttribute('h', this.format(h));
  878. elem.setAttribute('src', src);
  879. elem.setAttribute('aspect', (aspect) ? '1' : '0');
  880. elem.setAttribute('flipH', (flipH) ? '1' : '0');
  881. elem.setAttribute('flipV', (flipV) ? '1' : '0');
  882. this.root.appendChild(elem);
  883. };
  884. /**
  885. * Function: begin
  886. *
  887. * Starts a new path and puts it into the drawing buffer.
  888. */
  889. mxXmlCanvas2D.prototype.begin = function()
  890. {
  891. this.root.appendChild(this.createElement('begin'));
  892. this.lastX = 0;
  893. this.lastY = 0;
  894. };
  895. /**
  896. * Function: moveTo
  897. *
  898. * Moves the current path the given point.
  899. *
  900. * Parameters:
  901. *
  902. * x - Number that represents the x-coordinate of the point.
  903. * y - Number that represents the y-coordinate of the point.
  904. */
  905. mxXmlCanvas2D.prototype.moveTo = function(x, y)
  906. {
  907. var elem = this.createElement('move');
  908. elem.setAttribute('x', this.format(x));
  909. elem.setAttribute('y', this.format(y));
  910. this.root.appendChild(elem);
  911. this.lastX = x;
  912. this.lastY = y;
  913. };
  914. /**
  915. * Function: lineTo
  916. *
  917. * Draws a line to the given coordinates.
  918. *
  919. * Parameters:
  920. *
  921. * x - Number that represents the x-coordinate of the endpoint.
  922. * y - Number that represents the y-coordinate of the endpoint.
  923. */
  924. mxXmlCanvas2D.prototype.lineTo = function(x, y)
  925. {
  926. var elem = this.createElement('line');
  927. elem.setAttribute('x', this.format(x));
  928. elem.setAttribute('y', this.format(y));
  929. this.root.appendChild(elem);
  930. this.lastX = x;
  931. this.lastY = y;
  932. };
  933. /**
  934. * Function: quadTo
  935. *
  936. * Adds a quadratic curve to the current path.
  937. *
  938. * Parameters:
  939. *
  940. * x1 - Number that represents the x-coordinate of the control point.
  941. * y1 - Number that represents the y-coordinate of the control point.
  942. * x2 - Number that represents the x-coordinate of the endpoint.
  943. * y2 - Number that represents the y-coordinate of the endpoint.
  944. */
  945. mxXmlCanvas2D.prototype.quadTo = function(x1, y1, x2, y2)
  946. {
  947. var elem = this.createElement('quad');
  948. elem.setAttribute('x1', this.format(x1));
  949. elem.setAttribute('y1', this.format(y1));
  950. elem.setAttribute('x2', this.format(x2));
  951. elem.setAttribute('y2', this.format(y2));
  952. this.root.appendChild(elem);
  953. this.lastX = x2;
  954. this.lastY = y2;
  955. };
  956. /**
  957. * Function: curveTo
  958. *
  959. * Adds a bezier curve to the current path.
  960. *
  961. * Parameters:
  962. *
  963. * x1 - Number that represents the x-coordinate of the first control point.
  964. * y1 - Number that represents the y-coordinate of the first control point.
  965. * x2 - Number that represents the x-coordinate of the second control point.
  966. * y2 - Number that represents the y-coordinate of the second control point.
  967. * x3 - Number that represents the x-coordinate of the endpoint.
  968. * y3 - Number that represents the y-coordinate of the endpoint.
  969. */
  970. mxXmlCanvas2D.prototype.curveTo = function(x1, y1, x2, y2, x3, y3)
  971. {
  972. var elem = this.createElement('curve');
  973. elem.setAttribute('x1', this.format(x1));
  974. elem.setAttribute('y1', this.format(y1));
  975. elem.setAttribute('x2', this.format(x2));
  976. elem.setAttribute('y2', this.format(y2));
  977. elem.setAttribute('x3', this.format(x3));
  978. elem.setAttribute('y3', this.format(y3));
  979. this.root.appendChild(elem);
  980. this.lastX = x3;
  981. this.lastY = y3;
  982. };
  983. /**
  984. * Function: close
  985. *
  986. * Closes the current path.
  987. */
  988. mxXmlCanvas2D.prototype.close = function()
  989. {
  990. this.root.appendChild(this.createElement('close'));
  991. };
  992. /**
  993. * Function: text
  994. *
  995. * Paints the given text. Possible values for format are empty string for
  996. * plain text and html for HTML markup. Background and border color as well
  997. * as clipping is not available in plain text labels for VML. HTML labels
  998. * are not available as part of shapes with no foreignObject support in SVG
  999. * (eg. IE9, IE10).
  1000. *
  1001. * Parameters:
  1002. *
  1003. * x - Number that represents the x-coordinate of the text.
  1004. * y - Number that represents the y-coordinate of the text.
  1005. * w - Number that represents the available width for the text or 0 for automatic width.
  1006. * h - Number that represents the available height for the text or 0 for automatic height.
  1007. * str - String that specifies the text to be painted.
  1008. * align - String that represents the horizontal alignment.
  1009. * valign - String that represents the vertical alignment.
  1010. * wrap - Boolean that specifies if word-wrapping is enabled. Requires w > 0.
  1011. * format - Empty string for plain text or 'html' for HTML markup.
  1012. * overflow - Specifies the overflow behaviour of the label. Requires w > 0 and/or h > 0.
  1013. * clip - Boolean that specifies if the label should be clipped. Requires w > 0 and/or h > 0.
  1014. * rotation - Number that specifies the angle of the rotation around the anchor point of the text.
  1015. * dir - Optional string that specifies the text direction. Possible values are rtl and lrt.
  1016. */
  1017. mxXmlCanvas2D.prototype.text = function(x, y, w, h, str, align, valign, wrap, format, overflow, clip, rotation, dir)
  1018. {
  1019. if (this.textEnabled && str != null)
  1020. {
  1021. if (mxUtils.isNode(str))
  1022. {
  1023. str = mxUtils.getOuterHtml(str);
  1024. }
  1025. var elem = this.createElement('text');
  1026. elem.setAttribute('x', this.format(x));
  1027. elem.setAttribute('y', this.format(y));
  1028. elem.setAttribute('w', this.format(w));
  1029. elem.setAttribute('h', this.format(h));
  1030. elem.setAttribute('str', str);
  1031. if (align != null)
  1032. {
  1033. elem.setAttribute('align', align);
  1034. }
  1035. if (valign != null)
  1036. {
  1037. elem.setAttribute('valign', valign);
  1038. }
  1039. elem.setAttribute('wrap', (wrap) ? '1' : '0');
  1040. if (format == null)
  1041. {
  1042. format = '';
  1043. }
  1044. elem.setAttribute('format', format);
  1045. if (overflow != null)
  1046. {
  1047. elem.setAttribute('overflow', overflow);
  1048. }
  1049. if (clip != null)
  1050. {
  1051. elem.setAttribute('clip', (clip) ? '1' : '0');
  1052. }
  1053. if (rotation != null)
  1054. {
  1055. elem.setAttribute('rotation', rotation);
  1056. }
  1057. if (dir != null)
  1058. {
  1059. elem.setAttribute('dir', dir);
  1060. }
  1061. this.root.appendChild(elem);
  1062. }
  1063. };
  1064. /**
  1065. * Function: stroke
  1066. *
  1067. * Paints the outline of the current drawing buffer.
  1068. */
  1069. mxXmlCanvas2D.prototype.stroke = function()
  1070. {
  1071. this.root.appendChild(this.createElement('stroke'));
  1072. };
  1073. /**
  1074. * Function: fill
  1075. *
  1076. * Fills the current drawing buffer.
  1077. */
  1078. mxXmlCanvas2D.prototype.fill = function()
  1079. {
  1080. this.root.appendChild(this.createElement('fill'));
  1081. };
  1082. /**
  1083. * Function: fillAndStroke
  1084. *
  1085. * Fills the current drawing buffer and its outline.
  1086. */
  1087. mxXmlCanvas2D.prototype.fillAndStroke = function()
  1088. {
  1089. this.root.appendChild(this.createElement('fillstroke'));
  1090. };