mxStencil.js 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869
  1. /**
  2. * Copyright (c) 2006-2015, JGraph Ltd
  3. * Copyright (c) 2006-2015, Gaudenz Alder
  4. */
  5. /**
  6. * Class: mxStencil
  7. *
  8. * Implements a generic shape which is based on a XML node as a description.
  9. *
  10. * shape:
  11. *
  12. * The outer element is *shape*, that has attributes:
  13. *
  14. * - "name", string, required. The stencil name that uniquely identifies the shape.
  15. * - "w" and "h" are optional decimal view bounds. This defines your co-ordinate
  16. * system for the graphics operations in the shape. The default is 100,100.
  17. * - "aspect", optional string. Either "variable", the default, or "fixed". Fixed
  18. * means always render the shape with the aspect ratio defined by the ratio w/h.
  19. * Variable causes the ratio to match that of the geometry of the current vertex.
  20. * - "strokewidth", optional string. Either an integer or the string "inherit".
  21. * "inherit" indicates that the strokeWidth of the cell is only changed on scaling,
  22. * not on resizing. Default is "1".
  23. * If numeric values are used, the strokeWidth of the cell is changed on both
  24. * scaling and resizing and the value defines the multiple that is applied to
  25. * the width.
  26. *
  27. * connections:
  28. *
  29. * If you want to define specific fixed connection points on the shape use the
  30. * *connections* element. Each *constraint* element within connections defines
  31. * a fixed connection point on the shape. Constraints have attributes:
  32. *
  33. * - "perimeter", required. 1 or 0. 0 sets the connection point where specified
  34. * by x,y. 1 Causes the position of the connection point to be extrapolated from
  35. * the center of the shape, through x,y to the point of intersection with the
  36. * perimeter of the shape.
  37. * - "x" and "y" are the position of the fixed point relative to the bounds of
  38. * the shape. They can be automatically adjusted if perimeter=1. So, (0,0) is top
  39. * left, (0.5,0.5) the center, (1,0.5) the center of the right hand edge of the
  40. * bounds, etc. Values may be less than 0 or greater than 1 to be positioned
  41. * outside of the shape.
  42. * - "name", optional string. A unique identifier for the port on the shape.
  43. *
  44. * background and foreground:
  45. *
  46. * The path of the graphics drawing is split into two elements, *foreground* and
  47. * *background*. The split is to define which part any shadow applied to the shape
  48. * is derived from (the background). This, generally, means the background is the
  49. * line tracing of the outside of the shape, but not always.
  50. *
  51. * Any stroke, fill or fillstroke of a background must be the first element of the
  52. * foreground element, they must not be used within *background*. If the background
  53. * is empty, this is not required.
  54. *
  55. * Because the background cannot have any fill or stroke, it can contain only one
  56. * *path*, *rect*, *roundrect* or *ellipse* element (or none). It can also not
  57. * include *image*, *text* or *include-shape*.
  58. *
  59. * Note that the state, styling and drawing in mxGraph stencils is very close in
  60. * design to that of HTML 5 canvas. Tutorials on this subject, if you're not
  61. * familiar with the topic, will give a good high-level introduction to the
  62. * concepts used.
  63. *
  64. * State:
  65. *
  66. * Rendering within the foreground and background elements has the concept of
  67. * state. There are two types of operations other than state save/load, styling
  68. * and drawing. The styling operations change the current state, so you can save
  69. * the current state with <save/> and pull the last saved state from the state
  70. * stack using <restore/>.
  71. *
  72. * Styling:
  73. *
  74. * The elements that change colors within the current state all take a hash
  75. * prefixed hex color code ("#FFEA80").
  76. *
  77. * - *strokecolor*, this sets the color that drawing paths will be rendered in
  78. * when a stroke or fillstroke command is issued.
  79. * - *fillcolor*, this sets the color that the inside of closed paths will be
  80. * rendered in when a fill or fillstroke command is issued.
  81. * - *fontcolor*, this sets the color that fonts are rendered in when text is drawn.
  82. *
  83. * *alpha* defines the degree of transparency used between 1.0 for fully opaque
  84. * and 0.0 for fully transparent.
  85. *
  86. * *fillalpha* defines the degree of fill transparency used between 1.0 for fully
  87. * opaque and 0.0 for fully transparent.
  88. *
  89. * *strokealpha* defines the degree of stroke transparency used between 1.0 for
  90. * fully opaque and 0.0 for fully transparent.
  91. *
  92. * *strokewidth* defines the integer thickness of drawing elements rendered by
  93. * stroking. Use fixed="1" to apply the value as-is, without scaling.
  94. *
  95. * *dashed* is "1" for dashing enabled and "0" for disabled.
  96. *
  97. * When *dashed* is enabled the current dash pattern, defined by *dashpattern*,
  98. * is used on strokes. dashpattern is a sequence of space separated "on, off"
  99. * lengths that define what distance to paint the stroke for, then what distance
  100. * to paint nothing for, repeat... The default is "3 3". You could define a more
  101. * complex pattern with "5 3 2 6", for example. Generally, it makes sense to have
  102. * an even number of elements in the dashpattern, but that's not required.
  103. *
  104. * *linejoin*, *linecap* and *miterlimit* are best explained by the Mozilla page
  105. * on Canvas styling (about halfway down). The values are all the same except we
  106. * use "flat" for linecap, instead of Canvas' "butt".
  107. *
  108. * For font styling there are.
  109. *
  110. * - *fontsize*, an integer,
  111. * - *fontstyle*, an ORed bit pattern of bold (1), italic (2) and underline (4),
  112. * i.e bold underline is "5".
  113. * - *fontfamily*, is a string defining the typeface to be used.
  114. *
  115. * Drawing:
  116. *
  117. * Most drawing is contained within a *path* element. Again, the graphic
  118. * primitives are very similar to that of HTML 5 canvas.
  119. *
  120. * - *move* to attributes required decimals (x,y).
  121. * - *line* to attributes required decimals (x,y).
  122. * - *quad* to required decimals (x2,y2) via control point required decimals
  123. * (x1,y1).
  124. * - *curve* to required decimals (x3,y3), via control points required decimals
  125. * (x1,y1) and (x2,y2).
  126. * - *arc*, this doesn't follow the HTML Canvas signatures, instead it's a copy
  127. * of the SVG arc command. The SVG specification documentation gives the best
  128. * description of its behaviors. The attributes are named identically, they are
  129. * decimals and all required.
  130. * - *close* ends the current subpath and causes an automatic straight line to
  131. * be drawn from the current point to the initial point of the current subpath.
  132. *
  133. * Complex drawing:
  134. *
  135. * In addition to the graphics primitive operations there are non-primitive
  136. * operations. These provide an easy method to draw some basic shapes.
  137. *
  138. * - *rect*, attributes "x", "y", "w", "h", all required decimals
  139. * - *roundrect*, attributes "x", "y", "w", "h", all required decimals. Also
  140. * "arcsize" an optional decimal attribute defining how large, the corner curves
  141. * are.
  142. * - *ellipse*, attributes "x", "y", "w", "h", all required decimals.
  143. *
  144. * Note that these 3 shapes and all paths must be followed by either a fill,
  145. * stroke, or fillstroke.
  146. *
  147. * Text:
  148. *
  149. * *text* elements have the following attributes.
  150. *
  151. * - "str", the text string to display, required.
  152. * - "x" and "y", the decimal location (x,y) of the text element, required.
  153. * - "align", the horizontal alignment of the text element, either "left",
  154. * "center" or "right". Optional, default is "left".
  155. * - "valign", the vertical alignment of the text element, either "top", "middle"
  156. * or "bottom". Optional, default is "top".
  157. * - "localized", 0 or 1, if 1 then the "str" actually contains a key to use to
  158. * fetch the value out of mxResources. Optional, default is
  159. * <mxStencil.defaultLocalized>.
  160. * - "vertical", 0 or 1, if 1 the label is rendered vertically (rotated by 90
  161. * degrees). Optional, default is 0.
  162. * - "rotation", angle in degrees (0 to 360). The angle to rotate the text by.
  163. * Optional, default is 0.
  164. * - "align-shape", 0 or 1, if 0 ignore the rotation of the shape when setting
  165. * the text rotation. Optional, default is 1.
  166. *
  167. * If <allowEval> is true, then the text content of the this element can define
  168. * a function which is invoked with the shape as the only argument and returns
  169. * the value for the text element (ignored if the str attribute is not null).
  170. *
  171. * Images:
  172. *
  173. * *image* elements can either be external URLs, or data URIs, where supported
  174. * (not in IE 7-). Attributes are:
  175. *
  176. * - "src", required string. Either a data URI or URL.
  177. * - "x", "y", required decimals. The (x,y) position of the image.
  178. * - "w", "h", required decimals. The width and height of the image.
  179. * - "flipH" and "flipV", optional 0 or 1. Whether to flip the image along the
  180. * horizontal/vertical axis. Default is 0 for both.
  181. *
  182. * If <allowEval> is true, then the text content of the this element can define
  183. * a function which is invoked with the shape as the only argument and returns
  184. * the value for the image source (ignored if the src attribute is not null).
  185. *
  186. * Sub-shapes:
  187. *
  188. * *include-shape* allow stencils to be rendered within the current stencil by
  189. * referencing the sub-stencil by name. Attributes are:
  190. *
  191. * - "name", required string. The unique shape name of the stencil.
  192. * - "x", "y", "w", "h", required decimals. The (x,y) position of the sub-shape
  193. * and its width and height.
  194. *
  195. * Constructor: mxStencil
  196. *
  197. * Constructs a new generic shape by setting <desc> to the given XML node and
  198. * invoking <parseDescription> and <parseConstraints>.
  199. *
  200. * Parameters:
  201. *
  202. * desc - XML node that contains the stencil description.
  203. */
  204. function mxStencil(desc)
  205. {
  206. this.desc = desc;
  207. this.parseDescription();
  208. this.parseConstraints();
  209. };
  210. /**
  211. * Extends mxShape.
  212. */
  213. mxUtils.extend(mxStencil, mxShape);
  214. /**
  215. * Variable: defaultLocalized
  216. *
  217. * Static global variable that specifies the default value for the localized
  218. * attribute of the text element. Default is false.
  219. */
  220. mxStencil.defaultLocalized = false;
  221. /**
  222. * Function: allowEval
  223. *
  224. * Static global switch that specifies if the use of eval is allowed for
  225. * evaluating text content and images. Default is false. Set this to true
  226. * if stencils can not contain user input.
  227. */
  228. mxStencil.allowEval = false;
  229. /**
  230. * Variable: desc
  231. *
  232. * Holds the XML node with the stencil description.
  233. */
  234. mxStencil.prototype.desc = null;
  235. /**
  236. * Variable: constraints
  237. *
  238. * Holds an array of <mxConnectionConstraints> as defined in the shape.
  239. */
  240. mxStencil.prototype.constraints = null;
  241. /**
  242. * Variable: aspect
  243. *
  244. * Holds the aspect of the shape. Default is 'auto'.
  245. */
  246. mxStencil.prototype.aspect = null;
  247. /**
  248. * Variable: w0
  249. *
  250. * Holds the width of the shape. Default is 100.
  251. */
  252. mxStencil.prototype.w0 = null;
  253. /**
  254. * Variable: h0
  255. *
  256. * Holds the height of the shape. Default is 100.
  257. */
  258. mxStencil.prototype.h0 = null;
  259. /**
  260. * Variable: bgNodes
  261. *
  262. * Holds the XML node with the stencil description.
  263. */
  264. mxStencil.prototype.bgNode = null;
  265. /**
  266. * Variable: fgNodes
  267. *
  268. * Holds the XML node with the stencil description.
  269. */
  270. mxStencil.prototype.fgNode = null;
  271. /**
  272. * Variable: strokewidth
  273. *
  274. * Holds the strokewidth direction from the description.
  275. */
  276. mxStencil.prototype.strokewidth = null;
  277. /**
  278. * Function: parseDescription
  279. *
  280. * Reads <w0>, <h0>, <aspect>, <bgNodes> and <fgNodes> from <desc>.
  281. */
  282. mxStencil.prototype.parseDescription = function()
  283. {
  284. // LATER: Preprocess nodes for faster painting
  285. this.fgNode = this.desc.getElementsByTagName('foreground')[0];
  286. this.bgNode = this.desc.getElementsByTagName('background')[0];
  287. this.w0 = Number(this.desc.getAttribute('w') || 100);
  288. this.h0 = Number(this.desc.getAttribute('h') || 100);
  289. // Possible values for aspect are: variable and fixed where
  290. // variable means fill the available space and fixed means
  291. // use w0 and h0 to compute the aspect.
  292. var aspect = this.desc.getAttribute('aspect');
  293. this.aspect = (aspect != null) ? aspect : 'variable';
  294. // Possible values for strokewidth are all numbers and "inherit"
  295. // where the inherit means take the value from the style (ie. the
  296. // user-defined stroke-width). Note that the strokewidth is scaled
  297. // by the minimum scaling that is used to draw the shape (sx, sy).
  298. var sw = this.desc.getAttribute('strokewidth');
  299. this.strokewidth = (sw != null) ? sw : '1';
  300. };
  301. /**
  302. * Function: parseConstraints
  303. *
  304. * Reads the constraints from <desc> into <constraints> using
  305. * <parseConstraint>.
  306. */
  307. mxStencil.prototype.parseConstraints = function()
  308. {
  309. var conns = this.desc.getElementsByTagName('connections')[0];
  310. if (conns != null)
  311. {
  312. var tmp = mxUtils.getChildNodes(conns);
  313. if (tmp != null && tmp.length > 0)
  314. {
  315. this.constraints = [];
  316. for (var i = 0; i < tmp.length; i++)
  317. {
  318. this.constraints.push(this.parseConstraint(tmp[i]));
  319. }
  320. }
  321. }
  322. };
  323. /**
  324. * Function: parseConstraint
  325. *
  326. * Parses the given XML node and returns its <mxConnectionConstraint>.
  327. */
  328. mxStencil.prototype.parseConstraint = function(node)
  329. {
  330. var x = Number(node.getAttribute('x'));
  331. var y = Number(node.getAttribute('y'));
  332. var perimeter = node.getAttribute('perimeter') == '1';
  333. var name = node.getAttribute('name');
  334. return new mxConnectionConstraint(new mxPoint(x, y), perimeter, name);
  335. };
  336. /**
  337. * Function: evaluateTextAttribute
  338. *
  339. * Gets the given attribute as a text. The return value from <evaluateAttribute>
  340. * is used as a key to <mxResources.get> if the localized attribute in the text
  341. * node is 1 or if <defaultLocalized> is true.
  342. */
  343. mxStencil.prototype.evaluateTextAttribute = function(node, attribute, shape)
  344. {
  345. var result = this.evaluateAttribute(node, attribute, shape);
  346. var loc = node.getAttribute('localized');
  347. if ((mxStencil.defaultLocalized && loc == null) || loc == '1')
  348. {
  349. result = mxResources.get(result);
  350. }
  351. return result;
  352. };
  353. /**
  354. * Function: evaluateAttribute
  355. *
  356. * Gets the attribute for the given name from the given node. If the attribute
  357. * does not exist then the text content of the node is evaluated and if it is
  358. * a function it is invoked with <shape> as the only argument and the return
  359. * value is used as the attribute value to be returned.
  360. */
  361. mxStencil.prototype.evaluateAttribute = function(node, attribute, shape)
  362. {
  363. var result = node.getAttribute(attribute);
  364. if (result == null)
  365. {
  366. var text = mxUtils.getTextContent(node);
  367. if (text != null && mxStencil.allowEval)
  368. {
  369. var funct = mxUtils.eval(text);
  370. if (typeof(funct) == 'function')
  371. {
  372. result = funct(shape);
  373. }
  374. }
  375. }
  376. return result;
  377. };
  378. /**
  379. * Function: drawShape
  380. *
  381. * Draws this stencil inside the given bounds.
  382. */
  383. mxStencil.prototype.drawShape = function(canvas, shape, x, y, w, h)
  384. {
  385. var stack = canvas.states.slice();
  386. // TODO: Internal structure (array of special structs?), relative and absolute
  387. // coordinates (eg. note shape, process vs star, actor etc.), text rendering
  388. // and non-proportional scaling, how to implement pluggable edge shapes
  389. // (start, segment, end blocks), pluggable markers, how to implement
  390. // swimlanes (title area) with this API, add icon, horizontal/vertical
  391. // label, indicator for all shapes, rotation
  392. var direction = mxUtils.getValue(shape.style, mxConstants.STYLE_DIRECTION, null);
  393. var aspect = this.computeAspect(shape.style, x, y, w, h, direction);
  394. var minScale = Math.min(aspect.width, aspect.height);
  395. var sw = (this.strokewidth == 'inherit') ?
  396. Number(mxUtils.getNumber(shape.style, mxConstants.STYLE_STROKEWIDTH, 1)) :
  397. Number(this.strokewidth) * minScale;
  398. canvas.setStrokeWidth(sw);
  399. // Draws a transparent rectangle for catching events
  400. if (shape.style != null && mxUtils.getValue(shape.style, mxConstants.STYLE_POINTER_EVENTS, '0') == '1')
  401. {
  402. canvas.setStrokeColor(mxConstants.NONE);
  403. canvas.rect(x, y, w, h);
  404. canvas.stroke();
  405. canvas.setStrokeColor(shape.stroke);
  406. }
  407. this.drawChildren(canvas, shape, x, y, w, h, this.bgNode, aspect, false, true);
  408. this.drawChildren(canvas, shape, x, y, w, h, this.fgNode, aspect, true,
  409. !shape.outline || shape.style == null || mxUtils.getValue(
  410. shape.style, mxConstants.STYLE_BACKGROUND_OUTLINE, 0) == 0);
  411. // Restores stack for unequal count of save/restore calls
  412. if (canvas.states.length != stack.length)
  413. {
  414. canvas.states = stack;
  415. }
  416. };
  417. /**
  418. * Function: drawChildren
  419. *
  420. * Draws this stencil inside the given bounds.
  421. */
  422. mxStencil.prototype.drawChildren = function(canvas, shape, x, y, w, h, node, aspect, disableShadow, paint)
  423. {
  424. if (node != null && w > 0 && h > 0)
  425. {
  426. var tmp = node.firstChild;
  427. while (tmp != null)
  428. {
  429. if (tmp.nodeType == mxConstants.NODETYPE_ELEMENT)
  430. {
  431. this.drawNode(canvas, shape, tmp, aspect, disableShadow, paint);
  432. }
  433. tmp = tmp.nextSibling;
  434. }
  435. }
  436. };
  437. /**
  438. * Function: computeAspect
  439. *
  440. * Returns a rectangle that contains the offset in x and y and the horizontal
  441. * and vertical scale in width and height used to draw this shape inside the
  442. * given <mxRectangle>.
  443. *
  444. * Parameters:
  445. *
  446. * shape - <mxShape> to be drawn.
  447. * bounds - <mxRectangle> that should contain the stencil.
  448. * direction - Optional direction of the shape to be darwn.
  449. */
  450. mxStencil.prototype.computeAspect = function(shape, x, y, w, h, direction)
  451. {
  452. var x0 = x;
  453. var y0 = y;
  454. var sx = w / this.w0;
  455. var sy = h / this.h0;
  456. var inverse = (direction == mxConstants.DIRECTION_NORTH || direction == mxConstants.DIRECTION_SOUTH);
  457. if (inverse)
  458. {
  459. sy = w / this.h0;
  460. sx = h / this.w0;
  461. var delta = (w - h) / 2;
  462. x0 += delta;
  463. y0 -= delta;
  464. }
  465. if (this.aspect == 'fixed')
  466. {
  467. sy = Math.min(sx, sy);
  468. sx = sy;
  469. // Centers the shape inside the available space
  470. if (inverse)
  471. {
  472. x0 += (h - this.w0 * sx) / 2;
  473. y0 += (w - this.h0 * sy) / 2;
  474. }
  475. else
  476. {
  477. x0 += (w - this.w0 * sx) / 2;
  478. y0 += (h - this.h0 * sy) / 2;
  479. }
  480. }
  481. return new mxRectangle(x0, y0, sx, sy);
  482. };
  483. /**
  484. * Function: drawNode
  485. *
  486. * Draws this stencil inside the given bounds.
  487. */
  488. mxStencil.prototype.drawNode = function(canvas, shape, node, aspect, disableShadow, paint)
  489. {
  490. var name = node.nodeName;
  491. var x0 = aspect.x;
  492. var y0 = aspect.y;
  493. var sx = aspect.width;
  494. var sy = aspect.height;
  495. var minScale = Math.min(sx, sy);
  496. if (name == 'save')
  497. {
  498. canvas.save();
  499. }
  500. else if (name == 'restore')
  501. {
  502. canvas.restore();
  503. }
  504. else if (paint)
  505. {
  506. if (name == 'path')
  507. {
  508. canvas.begin();
  509. var parseRegularly = true;
  510. if (node.getAttribute('rounded') == '1')
  511. {
  512. parseRegularly = false;
  513. var arcSize = Number(node.getAttribute('arcSize'));
  514. var pointCount = 0;
  515. var segs = [];
  516. // Renders the elements inside the given path
  517. var childNode = node.firstChild;
  518. while (childNode != null)
  519. {
  520. if (childNode.nodeType == mxConstants.NODETYPE_ELEMENT)
  521. {
  522. var childName = childNode.nodeName;
  523. if (childName == 'move' || childName == 'line')
  524. {
  525. if (childName == 'move' || segs.length == 0)
  526. {
  527. segs.push([]);
  528. }
  529. segs[segs.length - 1].push(new mxPoint(x0 + Number(childNode.getAttribute('x')) * sx,
  530. y0 + Number(childNode.getAttribute('y')) * sy));
  531. pointCount++;
  532. }
  533. else
  534. {
  535. //We only support move and line for rounded corners
  536. parseRegularly = true;
  537. break;
  538. }
  539. }
  540. childNode = childNode.nextSibling;
  541. }
  542. if (!parseRegularly && pointCount > 0)
  543. {
  544. for (var i = 0; i < segs.length; i++)
  545. {
  546. var close = false, ps = segs[i][0], pe = segs[i][segs[i].length - 1];
  547. if (ps.x == pe.x && ps.y == pe.y)
  548. {
  549. segs[i].pop();
  550. close = true;
  551. }
  552. this.addPoints(canvas, segs[i], true, arcSize, close);
  553. }
  554. }
  555. else
  556. {
  557. parseRegularly = true;
  558. }
  559. }
  560. if (parseRegularly)
  561. {
  562. // Renders the elements inside the given path
  563. var childNode = node.firstChild;
  564. while (childNode != null)
  565. {
  566. if (childNode.nodeType == mxConstants.NODETYPE_ELEMENT)
  567. {
  568. this.drawNode(canvas, shape, childNode, aspect, disableShadow, paint);
  569. }
  570. childNode = childNode.nextSibling;
  571. }
  572. }
  573. }
  574. else if (name == 'close')
  575. {
  576. canvas.close();
  577. }
  578. else if (name == 'move')
  579. {
  580. canvas.moveTo(x0 + Number(node.getAttribute('x')) * sx, y0 + Number(node.getAttribute('y')) * sy);
  581. }
  582. else if (name == 'line')
  583. {
  584. canvas.lineTo(x0 + Number(node.getAttribute('x')) * sx, y0 + Number(node.getAttribute('y')) * sy);
  585. }
  586. else if (name == 'quad')
  587. {
  588. canvas.quadTo(x0 + Number(node.getAttribute('x1')) * sx,
  589. y0 + Number(node.getAttribute('y1')) * sy,
  590. x0 + Number(node.getAttribute('x2')) * sx,
  591. y0 + Number(node.getAttribute('y2')) * sy);
  592. }
  593. else if (name == 'curve')
  594. {
  595. canvas.curveTo(x0 + Number(node.getAttribute('x1')) * sx,
  596. y0 + Number(node.getAttribute('y1')) * sy,
  597. x0 + Number(node.getAttribute('x2')) * sx,
  598. y0 + Number(node.getAttribute('y2')) * sy,
  599. x0 + Number(node.getAttribute('x3')) * sx,
  600. y0 + Number(node.getAttribute('y3')) * sy);
  601. }
  602. else if (name == 'arc')
  603. {
  604. canvas.arcTo(Number(node.getAttribute('rx')) * sx,
  605. Number(node.getAttribute('ry')) * sy,
  606. Number(node.getAttribute('x-axis-rotation')),
  607. Number(node.getAttribute('large-arc-flag')),
  608. Number(node.getAttribute('sweep-flag')),
  609. x0 + Number(node.getAttribute('x')) * sx,
  610. y0 + Number(node.getAttribute('y')) * sy);
  611. }
  612. else if (name == 'rect')
  613. {
  614. canvas.rect(x0 + Number(node.getAttribute('x')) * sx,
  615. y0 + Number(node.getAttribute('y')) * sy,
  616. Number(node.getAttribute('w')) * sx,
  617. Number(node.getAttribute('h')) * sy);
  618. }
  619. else if (name == 'roundrect')
  620. {
  621. var arcsize = Number(node.getAttribute('arcsize'));
  622. if (arcsize == 0)
  623. {
  624. arcsize = mxConstants.RECTANGLE_ROUNDING_FACTOR * 100;
  625. }
  626. var w = Number(node.getAttribute('w')) * sx;
  627. var h = Number(node.getAttribute('h')) * sy;
  628. var factor = Number(arcsize) / 100;
  629. var r = Math.min(w * factor, h * factor);
  630. canvas.roundrect(x0 + Number(node.getAttribute('x')) * sx,
  631. y0 + Number(node.getAttribute('y')) * sy,
  632. w, h, r, r);
  633. }
  634. else if (name == 'ellipse')
  635. {
  636. canvas.ellipse(x0 + Number(node.getAttribute('x')) * sx,
  637. y0 + Number(node.getAttribute('y')) * sy,
  638. Number(node.getAttribute('w')) * sx,
  639. Number(node.getAttribute('h')) * sy);
  640. }
  641. else if (name == 'image')
  642. {
  643. if (!shape.outline)
  644. {
  645. var src = this.evaluateAttribute(node, 'src', shape);
  646. canvas.image(x0 + Number(node.getAttribute('x')) * sx,
  647. y0 + Number(node.getAttribute('y')) * sy,
  648. Number(node.getAttribute('w')) * sx,
  649. Number(node.getAttribute('h')) * sy,
  650. src, false, node.getAttribute('flipH') == '1',
  651. node.getAttribute('flipV') == '1');
  652. }
  653. }
  654. else if (name == 'text')
  655. {
  656. if (!shape.outline)
  657. {
  658. var str = this.evaluateTextAttribute(node, 'str', shape);
  659. var rotation = node.getAttribute('vertical') == '1' ? -90 : 0;
  660. if (node.getAttribute('align-shape') == '0')
  661. {
  662. var dr = shape.rotation;
  663. // Depends on flipping
  664. var flipH = mxUtils.getValue(shape.style, mxConstants.STYLE_FLIPH, 0) == 1;
  665. var flipV = mxUtils.getValue(shape.style, mxConstants.STYLE_FLIPV, 0) == 1;
  666. if (flipH && flipV)
  667. {
  668. rotation -= dr;
  669. }
  670. else if (flipH || flipV)
  671. {
  672. rotation += dr;
  673. }
  674. else
  675. {
  676. rotation -= dr;
  677. }
  678. }
  679. rotation -= node.getAttribute('rotation');
  680. canvas.text(x0 + Number(node.getAttribute('x')) * sx,
  681. y0 + Number(node.getAttribute('y')) * sy,
  682. 0, 0, str, node.getAttribute('align') || 'left',
  683. node.getAttribute('valign') || 'top', false, '',
  684. null, false, rotation);
  685. }
  686. }
  687. else if (name == 'include-shape')
  688. {
  689. var stencil = mxStencilRegistry.getStencil(node.getAttribute('name'));
  690. if (stencil != null)
  691. {
  692. var x = x0 + Number(node.getAttribute('x')) * sx;
  693. var y = y0 + Number(node.getAttribute('y')) * sy;
  694. var w = Number(node.getAttribute('w')) * sx;
  695. var h = Number(node.getAttribute('h')) * sy;
  696. stencil.drawShape(canvas, shape, x, y, w, h);
  697. }
  698. }
  699. else if (name == 'fillstroke')
  700. {
  701. canvas.fillAndStroke();
  702. }
  703. else if (name == 'fill')
  704. {
  705. canvas.fill();
  706. }
  707. else if (name == 'stroke')
  708. {
  709. canvas.stroke();
  710. }
  711. else if (name == 'strokewidth')
  712. {
  713. var s = (node.getAttribute('fixed') == '1') ? 1 : minScale;
  714. canvas.setStrokeWidth(Number(node.getAttribute('width')) * s);
  715. }
  716. else if (name == 'dashed')
  717. {
  718. canvas.setDashed(node.getAttribute('dashed') == '1');
  719. }
  720. else if (name == 'dashpattern')
  721. {
  722. var value = node.getAttribute('pattern');
  723. if (value != null)
  724. {
  725. var tmp = value.split(' ');
  726. var pat = [];
  727. for (var i = 0; i < tmp.length; i++)
  728. {
  729. if (tmp[i].length > 0)
  730. {
  731. pat.push(Number(tmp[i]) * minScale);
  732. }
  733. }
  734. value = pat.join(' ');
  735. canvas.setDashPattern(value);
  736. }
  737. }
  738. else if (name == 'strokecolor')
  739. {
  740. canvas.setStrokeColor(node.getAttribute('color'));
  741. }
  742. else if (name == 'linecap')
  743. {
  744. canvas.setLineCap(node.getAttribute('cap'));
  745. }
  746. else if (name == 'linejoin')
  747. {
  748. canvas.setLineJoin(node.getAttribute('join'));
  749. }
  750. else if (name == 'miterlimit')
  751. {
  752. canvas.setMiterLimit(Number(node.getAttribute('limit')));
  753. }
  754. else if (name == 'fillcolor')
  755. {
  756. canvas.setFillColor(node.getAttribute('color'));
  757. }
  758. else if (name == 'alpha')
  759. {
  760. canvas.setAlpha(node.getAttribute('alpha'));
  761. }
  762. else if (name == 'fillalpha')
  763. {
  764. canvas.setAlpha(node.getAttribute('alpha'));
  765. }
  766. else if (name == 'strokealpha')
  767. {
  768. canvas.setAlpha(node.getAttribute('alpha'));
  769. }
  770. else if (name == 'fontcolor')
  771. {
  772. canvas.setFontColor(node.getAttribute('color'));
  773. }
  774. else if (name == 'fontstyle')
  775. {
  776. canvas.setFontStyle(node.getAttribute('style'));
  777. }
  778. else if (name == 'fontfamily')
  779. {
  780. canvas.setFontFamily(node.getAttribute('family'));
  781. }
  782. else if (name == 'fontsize')
  783. {
  784. canvas.setFontSize(Number(node.getAttribute('size')) * minScale);
  785. }
  786. if (disableShadow && (name == 'fillstroke' || name == 'fill' || name == 'stroke'))
  787. {
  788. disableShadow = false;
  789. canvas.setShadow(false);
  790. }
  791. }
  792. };
  793. __mxOutput.mxStencil = typeof mxStencil !== 'undefined' ? mxStencil : undefined;