forked from jgraph/mxgraph
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmxStencil.java
More file actions
718 lines (621 loc) · 17.6 KB
/
mxStencil.java
File metadata and controls
718 lines (621 loc) · 17.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
/**
* Copyright (c) 2010-2012, JGraph Ltd
*/
package com.mxgraph.shape;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import com.mxgraph.canvas.mxGraphics2DCanvas;
import com.mxgraph.canvas.mxGraphicsCanvas2D;
import com.mxgraph.util.mxConstants;
import com.mxgraph.util.mxPoint;
import com.mxgraph.util.mxRectangle;
import com.mxgraph.util.mxUtils;
import com.mxgraph.view.mxCellState;
/**
* Implements a stencil for the given XML definition. This class implements the mxGraph
* stencil schema.
*/
public class mxStencil implements mxIShape
{
private static final Logger log = Logger.getLogger(mxStencil.class.getName());
/**
* Holds the top-level node of the stencil definition.
*/
protected Element desc;
/**
* Holds the aspect of the shape. Default is "auto".
*/
protected String aspect = null;
/**
* Holds the width of the shape. Default is 100.
*/
protected double w0 = 100;
/**
* Holds the height of the shape. Default is 100.
*/
protected double h0 = 100;
/**
* Holds the XML node with the stencil description.
*/
protected Element bgNode = null;
/**
* Holds the XML node with the stencil description.
*/
protected Element fgNode = null;
/**
* Holds the strokewidth direction from the description.
*/
protected String strokewidth = null;
/**
* Holds the last x-position of the cursor.
*/
protected double lastMoveX = 0;
/**
* Holds the last y-position of the cursor.
*/
protected double lastMoveY = 0;
/**
* Constructs a new stencil for the given mxGraph shape description.
*/
public mxStencil(Element description)
{
setDescription(description);
}
/**
* Returns the description.
*/
public Element getDescription()
{
return desc;
}
/**
* Sets the description.
*/
public void setDescription(Element value)
{
desc = value;
parseDescription();
}
/**
* Creates the canvas for rendering the stencil.
*/
protected mxGraphicsCanvas2D createCanvas(mxGraphics2DCanvas gc)
{
return new mxGraphicsCanvas2D(gc.getGraphics());
}
/**
* Paints the stencil for the given state.
*/
public void paintShape(mxGraphics2DCanvas gc, mxCellState state)
{
Map<String, Object> style = state.getStyle();
mxGraphicsCanvas2D canvas = createCanvas(gc);
double rotation = mxUtils.getDouble(style, mxConstants.STYLE_ROTATION,
0);
String direction = mxUtils.getString(style,
mxConstants.STYLE_DIRECTION, null);
// Default direction is east (ignored if rotation exists)
if (direction != null)
{
if (direction.equals("north"))
{
rotation += 270;
}
else if (direction.equals("west"))
{
rotation += 180;
}
else if (direction.equals("south"))
{
rotation += 90;
}
}
// New styles for shape flipping the stencil
boolean flipH = mxUtils.isTrue(style, mxConstants.STYLE_STENCIL_FLIPH,
false);
boolean flipV = mxUtils.isTrue(style, mxConstants.STYLE_STENCIL_FLIPV,
false);
if (flipH && flipV)
{
rotation += 180;
flipH = false;
flipV = false;
}
// Saves the global state for each cell
canvas.save();
// Adds rotation and horizontal/vertical flipping
rotation = rotation % 360;
if (rotation != 0 || flipH || flipV)
{
canvas.rotate(rotation, flipH, flipV, state.getCenterX(),
state.getCenterY());
}
// Note: Overwritten in mxStencil.paintShape (can depend on aspect)
mxRectangle aspect = computeAspect(state, state, direction);
double minScale = Math.min(aspect.getWidth(), aspect.getHeight());
double sw = strokewidth.equals("inherit") ? mxUtils.getDouble(
state.getStyle(), mxConstants.STYLE_STROKEWIDTH, 1)
* state.getView().getScale() : Double
.parseDouble(strokewidth) * minScale;
canvas.setStrokeWidth(sw);
double alpha = mxUtils.getDouble(style, mxConstants.STYLE_OPACITY, 100) / 100;
String gradientColor = mxUtils.getString(style,
mxConstants.STYLE_GRADIENTCOLOR, null);
// Converts colors with special keyword none to null
if (gradientColor != null && gradientColor.equals(mxConstants.NONE))
{
gradientColor = null;
}
String fillColor = mxUtils.getString(style,
mxConstants.STYLE_FILLCOLOR, null);
if (fillColor != null && fillColor.equals(mxConstants.NONE))
{
fillColor = null;
}
String strokeColor = mxUtils.getString(style,
mxConstants.STYLE_STROKECOLOR, null);
if (strokeColor != null && strokeColor.equals(mxConstants.NONE))
{
strokeColor = null;
}
// Draws the shadow if the fillColor is not transparent
if (mxUtils.isTrue(style, mxConstants.STYLE_SHADOW, false))
{
drawShadow(canvas, state, rotation, flipH, flipV, state, alpha, fillColor != null, aspect);
}
canvas.setAlpha(alpha);
// Sets the dashed state
if (mxUtils.isTrue(style, mxConstants.STYLE_DASHED, false))
{
canvas.setDashed(true);
}
// Draws background and foreground
if (strokeColor != null || fillColor != null)
{
if (strokeColor != null)
{
canvas.setStrokeColor(strokeColor);
}
if (fillColor != null)
{
if (gradientColor != null
&& !gradientColor.equals("transparent"))
{
canvas.setGradient(fillColor, gradientColor, state.getX(),
state.getY(), state.getWidth(), state.getHeight(),
direction, 1, 1);
}
else
{
canvas.setFillColor(fillColor);
}
}
// Draws background and foreground of shape
drawShape(canvas, state, state, aspect, true);
drawShape(canvas, state, state, aspect, false);
}
}
/**
* Draws the shadow.
*/
protected void drawShadow(mxGraphicsCanvas2D canvas, mxCellState state, double rotation, boolean flipH,
boolean flipV, mxRectangle bounds, double alpha, boolean filled, mxRectangle aspect)
{
// Requires background in generic shape for shadow, looks like only one
// fillAndStroke is allowed per current path, try working around that
// Computes rotated shadow offset
double rad = rotation * Math.PI / 180;
double cos = Math.cos(-rad);
double sin = Math.sin(-rad);
mxPoint offset = mxUtils.getRotatedPoint(new mxPoint(mxConstants.SHADOW_OFFSETX, mxConstants.SHADOW_OFFSETY), cos, sin);
if (flipH)
{
offset.setX(offset.getX() * -1);
}
if (flipV)
{
offset.setY(offset.getY() * -1);
}
// TODO: Use save/restore instead of negative offset to restore (requires fix for HTML canvas)
canvas.translate(offset.getX(), offset.getY());
// Returns true if a shadow has been painted (path has been created)
if (drawShape(canvas, state, bounds, aspect, true))
{
canvas.setAlpha(mxConstants.STENCIL_SHADOW_OPACITY * alpha);
// TODO: Implement new shadow
//canvas.shadow(mxConstants.STENCIL_SHADOWCOLOR, filled);
}
canvas.translate(-offset.getX(), -offset.getY());
}
/**
* Draws this stencil inside the given bounds.
*/
public boolean drawShape(mxGraphicsCanvas2D canvas, mxCellState state,
mxRectangle bounds, mxRectangle aspect, boolean background)
{
Element elt = (background) ? bgNode : fgNode;
if (elt != null)
{
lastMoveX = 0;
lastMoveY = 0;
Node tmp = elt.getFirstChild();
while (tmp != null)
{
if (tmp.getNodeType() == Node.ELEMENT_NODE)
{
drawElement(canvas, state, (Element) tmp, aspect);
}
tmp = tmp.getNextSibling();
}
return true;
}
return false;
}
/**
* Returns a rectangle that contains the offset in x and y and the horizontal
* and vertical scale in width and height used to draw this shape inside the
* given rectangle.
*/
protected mxRectangle computeAspect(mxCellState state, mxRectangle bounds,
String direction)
{
double x0 = bounds.getX();
double y0 = bounds.getY();
double sx = bounds.getWidth() / w0;
double sy = bounds.getHeight() / h0;
boolean inverse = (direction != null && (direction.equals("north") || direction
.equals("south")));
if (inverse)
{
sy = bounds.getWidth() / h0;
sx = bounds.getHeight() / w0;
double delta = (bounds.getWidth() - bounds.getHeight()) / 2;
x0 += delta;
y0 -= delta;
}
if (aspect.equals("fixed"))
{
sy = Math.min(sx, sy);
sx = sy;
// Centers the shape inside the available space
if (inverse)
{
x0 += (bounds.getHeight() - this.w0 * sx) / 2;
y0 += (bounds.getWidth() - this.h0 * sy) / 2;
}
else
{
x0 += (bounds.getWidth() - this.w0 * sx) / 2;
y0 += (bounds.getHeight() - this.h0 * sy) / 2;
}
}
return new mxRectangle(x0, y0, sx, sy);
}
/**
* Drawsthe given element.
*/
protected void drawElement(mxGraphicsCanvas2D canvas, mxCellState state,
Element node, mxRectangle aspect)
{
String name = node.getNodeName();
double x0 = aspect.getX();
double y0 = aspect.getY();
double sx = aspect.getWidth();
double sy = aspect.getHeight();
double minScale = Math.min(sx, sy);
// LATER: Move to lookup table
if (name.equals("save"))
{
canvas.save();
}
else if (name.equals("restore"))
{
canvas.restore();
}
else if (name.equals("path"))
{
canvas.begin();
// Renders the elements inside the given path
Node childNode = node.getFirstChild();
while (childNode != null)
{
if (childNode.getNodeType() == Node.ELEMENT_NODE)
{
drawElement(canvas, state, (Element) childNode, aspect);
}
childNode = childNode.getNextSibling();
}
}
else if (name.equals("close"))
{
canvas.close();
}
else if (name.equals("move"))
{
lastMoveX = x0 + getDouble(node, "x") * sx;
lastMoveY = y0 + getDouble(node, "y") * sy;
canvas.moveTo(lastMoveX, lastMoveY);
}
else if (name.equals("line"))
{
lastMoveX = x0 + getDouble(node, "x") * sx;
lastMoveY = y0 + getDouble(node, "y") * sy;
canvas.lineTo(lastMoveX, lastMoveY);
}
else if (name.equals("quad"))
{
lastMoveX = x0 + getDouble(node, "x2") * sx;
lastMoveY = y0 + getDouble(node, "y2") * sy;
canvas.quadTo(x0 + getDouble(node, "x1") * sx,
y0 + getDouble(node, "y1") * sy, lastMoveX, lastMoveY);
}
else if (name.equals("curve"))
{
lastMoveX = x0 + getDouble(node, "x3") * sx;
lastMoveY = y0 + getDouble(node, "y3") * sy;
canvas.curveTo(x0 + getDouble(node, "x1") * sx,
y0 + getDouble(node, "y1") * sy, x0 + getDouble(node, "x2")
* sx, y0 + getDouble(node, "y2") * sy, lastMoveX,
lastMoveY);
}
else if (name.equals("arc"))
{
// Arc from stencil is turned into curves in image output
double r1 = getDouble(node, "rx") * sx;
double r2 = getDouble(node, "ry") * sy;
double angle = getDouble(node, "x-axis-rotation");
double largeArcFlag = getDouble(node, "large-arc-flag");
double sweepFlag = getDouble(node, "sweep-flag");
double x = x0 + getDouble(node, "x") * sx;
double y = y0 + getDouble(node, "y") * sy;
double[] curves = mxUtils.arcToCurves(this.lastMoveX,
this.lastMoveY, r1, r2, angle, largeArcFlag, sweepFlag, x,
y);
for (int i = 0; i < curves.length; i += 6)
{
canvas.curveTo(curves[i], curves[i + 1], curves[i + 2],
curves[i + 3], curves[i + 4], curves[i + 5]);
lastMoveX = curves[i + 4];
lastMoveY = curves[i + 5];
}
}
else if (name.equals("rect"))
{
canvas.rect(x0 + getDouble(node, "x") * sx,
y0 + getDouble(node, "y") * sy, getDouble(node, "w") * sx,
getDouble(node, "h") * sy);
}
else if (name.equals("roundrect"))
{
double arcsize = getDouble(node, "arcsize");
if (arcsize == 0)
{
arcsize = mxConstants.RECTANGLE_ROUNDING_FACTOR * 100;
}
double w = getDouble(node, "w") * sx;
double h = getDouble(node, "h") * sy;
double factor = arcsize / 100;
double r = Math.min(w * factor, h * factor);
canvas.roundrect(x0 + getDouble(node, "x") * sx,
y0 + getDouble(node, "y") * sy, getDouble(node, "w") * sx,
getDouble(node, "h") * sy, r, r);
}
else if (name.equals("ellipse"))
{
canvas.ellipse(x0 + getDouble(node, "x") * sx,
y0 + getDouble(node, "y") * sy, getDouble(node, "w") * sx,
getDouble(node, "h") * sy);
}
else if (name.equals("image"))
{
String src = evaluateAttribute(node, "src", state);
canvas.image(x0 + getDouble(node, "x") * sx,
y0 + getDouble(node, "y") * sy, getDouble(node, "w") * sx,
getDouble(node, "h") * sy, src, false,
getString(node, "flipH", "0").equals("1"),
getString(node, "flipV", "0").equals("1"));
}
else if (name.equals("text"))
{
String str = evaluateAttribute(node, "str", state);
double rotation = getString(node, "vertical", "0").equals("1") ? -90 : 0;
canvas.text(x0 + getDouble(node, "x") * sx,
y0 + getDouble(node, "y") * sy, 0, 0, str,
node.getAttribute("align"), node.getAttribute("valign"),
false, "", null, false, rotation, null);
}
else if (name.equals("include-shape"))
{
mxStencil stencil = mxStencilRegistry.getStencil(node
.getAttribute("name"));
if (stencil != null)
{
double x = x0 + getDouble(node, "x") * sx;
double y = y0 + getDouble(node, "y") * sy;
double w = getDouble(node, "w") * sx;
double h = getDouble(node, "h") * sy;
mxRectangle tmp = new mxRectangle(x, y, w, h);
stencil.drawShape(canvas, state, tmp, aspect, true);
stencil.drawShape(canvas, state, tmp, aspect, false);
}
}
else if (name.equals("fillstroke"))
{
canvas.fillAndStroke();
}
else if (name.equals("fill"))
{
canvas.fill();
}
else if (name.equals("stroke"))
{
canvas.stroke();
}
else if (name.equals("strokewidth"))
{
double s = (getInt(node, "fixed", 0) == 1) ? 1 : minScale;
canvas.setStrokeWidth(getDouble(node, "width") * s);
}
else if (name.equals("dashed"))
{
String dashed = node.getAttribute("dashed");
if (dashed != null)
{
canvas.setDashed(dashed.equals("1"));
}
}
else if (name.equals("dashpattern"))
{
String value = node.getAttribute("pattern");
if (value != null)
{
String[] tmp = value.split(" ");
StringBuffer pat = new StringBuffer();
for (int i = 0; i < tmp.length; i++)
{
if (tmp[i].length() > 0)
{
pat.append(Double.parseDouble(tmp[i]) * minScale);
pat.append(" ");
}
}
value = pat.toString();
}
canvas.setDashPattern(value);
}
else if (name.equals("strokecolor"))
{
canvas.setStrokeColor(node.getAttribute("color"));
}
else if (name.equals("linecap"))
{
canvas.setLineCap(node.getAttribute("cap"));
}
else if (name.equals("linejoin"))
{
canvas.setLineJoin(node.getAttribute("join"));
}
else if (name.equals("miterlimit"))
{
canvas.setMiterLimit(getDouble(node, "limit"));
}
else if (name.equals("fillcolor"))
{
canvas.setFillColor(node.getAttribute("color"));
}
else if (name.equals("fontcolor"))
{
canvas.setFontColor(node.getAttribute("color"));
}
else if (name.equals("fontstyle"))
{
canvas.setFontStyle(getInt(node, "style", 0));
}
else if (name.equals("fontfamily"))
{
canvas.setFontFamily(node.getAttribute("family"));
}
else if (name.equals("fontsize"))
{
canvas.setFontSize(getDouble(node, "size") * minScale);
}
}
/**
* Returns the given attribute or the default value.
*/
protected int getInt(Element elt, String attribute, int defaultValue)
{
String value = elt.getAttribute(attribute);
if (value != null && value.length() > 0)
{
try
{
defaultValue = (int) Math.floor(Float.parseFloat(value));
}
catch (NumberFormatException e)
{
log.log(Level.SEVERE, "Invalid value for attribute " + attribute + " in " + elt.getTagName(), e);
}
}
return defaultValue;
}
/**
* Returns the given attribute or 0.
*/
protected double getDouble(Element elt, String attribute)
{
return getDouble(elt, attribute, 0);
}
/**
* Returns the given attribute or the default value.
*/
protected double getDouble(Element elt, String attribute,
double defaultValue)
{
String value = elt.getAttribute(attribute);
if (value != null && value.length() > 0)
{
try
{
defaultValue = Double.parseDouble(value);
}
catch (NumberFormatException e)
{
log.log(Level.SEVERE, "Invalid value for attribute " + attribute + " in " + elt.getTagName(), e);
}
}
return defaultValue;
}
/**
* Returns the given attribute or the default value.
*/
protected String getString(Element elt, String attribute,
String defaultValue)
{
String value = elt.getAttribute(attribute);
if (value != null && value.length() > 0)
{
defaultValue = value;
}
return defaultValue;
}
/**
* Parses the description of this shape.
*/
protected void parseDescription()
{
// LATER: Preprocess nodes for faster painting
fgNode = (Element) desc.getElementsByTagName("foreground").item(0);
bgNode = (Element) desc.getElementsByTagName("background").item(0);
w0 = getDouble(desc, "w", w0);
h0 = getDouble(desc, "h", h0);
// Possible values for aspect are: variable and fixed where
// variable means fill the available space and fixed means
// use w0 and h0 to compute the aspect.
aspect = getString(desc, "aspect", "variable");
// Possible values for strokewidth are all numbers and "inherit"
// where the inherit means take the value from the style (ie. the
// user-defined stroke-width). Note that the strokewidth is scaled
// by the minimum scaling that is used to draw the shape (sx, sy).
strokewidth = getString(desc, "strokewidth", "1");
}
/**
* Gets the attribute for the given name from the given node. If the attribute
* does not exist then the text content of the node is evaluated and if it is
* a function it is invoked with <state> as the only argument and the return
* value is used as the attribute value to be returned.
*/
public String evaluateAttribute(Element elt, String attribute,
mxCellState state)
{
String result = elt.getAttribute(attribute);
if (result == null)
{
// JS functions as text content are currently not supported in Java
}
return result;
}
}