forked from jgraph/jgraphx
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mxGraphicsCanvas2D.java
1689 lines (1449 loc) · 33 KB
/
mxGraphicsCanvas2D.java
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
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package com.mxgraph.canvas;
import java.awt.AlphaComposite;
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Composite;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.GradientPaint;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Paint;
import java.awt.Rectangle;
import java.awt.Stroke;
import java.awt.font.TextAttribute;
import java.awt.geom.AffineTransform;
import java.awt.geom.Ellipse2D;
import java.awt.geom.GeneralPath;
import java.awt.geom.Point2D;
import java.awt.geom.Rectangle2D;
import java.text.AttributedString;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Stack;
import javax.swing.CellRendererPane;
import javax.swing.JLabel;
import com.mxgraph.util.mxConstants;
import com.mxgraph.util.mxLightweightLabel;
import com.mxgraph.util.mxUtils;
/**
* Used for exporting images. To render to an image from a given XML string,
* graph size and background color, the following code is used:
*
* <code>
* BufferedImage image = mxUtils.createBufferedImage(width, height, background);
* Graphics2D g2 = image.createGraphics();
* mxUtils.setAntiAlias(g2, true, true);
* XMLReader reader = SAXParserFactory.newInstance().newSAXParser().getXMLReader();
* reader.setContentHandler(new mxSaxOutputHandler(new mxGraphicsCanvas2D(g2)));
* reader.parse(new InputSource(new StringReader(xml)));
* </code>
*
* Text rendering is available for plain text and HTML markup, the latter with optional
* word wrapping. CSS support is limited to the following:
* http://docs.oracle.com/javase/6/docs/api/index.html?javax/swing/text/html/CSS.html
*/
public class mxGraphicsCanvas2D implements mxICanvas2D
{
/**
* Specifies the image scaling quality. Default is Image.SCALE_SMOOTH.
* See {@link #scaleImage(Image, int, int)}
*/
public static int IMAGE_SCALING = Image.SCALE_SMOOTH;
/**
* Specifies the additional pixels when computing the text width for HTML labels.
* Default is 2.
*/
public static int JAVA_TEXT_WIDTH_DELTA = 2;
/**
* Specifies the size of the cache used to store parsed colors
*/
public static int COLOR_CACHE_SIZE = 100;
/**
* Reference to the graphics instance for painting.
*/
protected Graphics2D graphics;
/**
* Specifies if text output should be rendered. Default is true.
*/
protected boolean textEnabled = true;
/**
* Represents the current state of the canvas.
*/
protected transient CanvasState state = new CanvasState();
/**
* Stack of states for save/restore.
*/
protected transient Stack<CanvasState> stack = new Stack<CanvasState>();
/**
* Holds the current path.
*/
protected transient GeneralPath currentPath;
/**
* Optional renderer pane to be used for HTML label rendering.
*/
protected CellRendererPane rendererPane;
/**
* Font caching.
*/
protected transient Font lastFont = null;
/**
* Font caching.
*/
protected transient int lastFontStyle = 0;
/**
* Font caching.
*/
protected transient int lastFontSize = 0;
/**
* Font caching.
*/
protected transient String lastFontFamily = "";
/**
* Stroke caching.
*/
protected transient Stroke lastStroke = null;
/**
* Stroke caching.
*/
protected transient float lastStrokeWidth = 0;
/**
* Stroke caching.
*/
protected transient int lastCap = 0;
/**
* Stroke caching.
*/
protected transient int lastJoin = 0;
/**
* Stroke caching.
*/
protected transient float lastMiterLimit = 0;
/**
* Stroke caching.
*/
protected transient boolean lastDashed = false;
/**
* Stroke caching.
*/
protected transient Object lastDashPattern = "";
/**
* Caches parsed colors.
*/
@SuppressWarnings("serial")
protected transient LinkedHashMap<String, Color> colorCache = new LinkedHashMap<String, Color>()
{
@Override
protected boolean removeEldestEntry(Map.Entry<String, Color> eldest)
{
return size() > COLOR_CACHE_SIZE;
}
};
/**
* Constructs a new graphics export canvas.
*/
public mxGraphicsCanvas2D(Graphics2D g)
{
setGraphics(g);
state.g = g;
// Initializes the cell renderer pane for drawing HTML markup
try
{
rendererPane = new CellRendererPane();
}
catch (Exception e)
{
// ignore
}
}
/**
* Sets the graphics instance.
*/
public void setGraphics(Graphics2D value)
{
graphics = value;
}
/**
* Returns the graphics instance.
*/
public Graphics2D getGraphics()
{
return graphics;
}
/**
* Returns true if text should be rendered.
*/
public boolean isTextEnabled()
{
return textEnabled;
}
/**
* Disables or enables text rendering.
*/
public void setTextEnabled(boolean value)
{
textEnabled = value;
}
/**
* Saves the current canvas state.
*/
public void save()
{
stack.push(state);
state = cloneState(state);
state.g = (Graphics2D) state.g.create();
}
/**
* Restores the last canvas state.
*/
public void restore()
{
state = stack.pop();
}
/**
* Returns a clone of thec given state.
*/
protected CanvasState cloneState(CanvasState state)
{
try
{
return (CanvasState) state.clone();
}
catch (CloneNotSupportedException e)
{
e.printStackTrace();
}
return null;
}
/**
*
*/
public void scale(double value)
{
// This implementation uses custom scale/translate and built-in rotation
state.scale = state.scale * value;
}
/**
*
*/
public void translate(double dx, double dy)
{
// This implementation uses custom scale/translate and built-in rotation
state.dx += dx;
state.dy += dy;
}
/**
*
*/
public void rotate(double theta, boolean flipH, boolean flipV, double cx, double cy)
{
cx += state.dx;
cy += state.dy;
cx *= state.scale;
cy *= state.scale;
state.g.rotate(Math.toRadians(theta), cx, cy);
// This implementation uses custom scale/translate and built-in rotation
// Rotation state is part of the AffineTransform in state.transform
if (flipH && flipV)
{
theta += 180;
}
else if (flipH ^ flipV)
{
double tx = (flipH) ? cx : 0;
int sx = (flipH) ? -1 : 1;
double ty = (flipV) ? cy : 0;
int sy = (flipV) ? -1 : 1;
state.g.translate(tx, ty);
state.g.scale(sx, sy);
state.g.translate(-tx, -ty);
}
state.theta = theta;
state.rotationCx = cx;
state.rotationCy = cy;
state.flipH = flipH;
state.flipV = flipV;
}
/**
*
*/
public void setStrokeWidth(double value)
{
// Lazy and cached instantiation strategy for all stroke properties
if (value != state.strokeWidth)
{
state.strokeWidth = value;
}
}
/**
* Caches color conversion as it is expensive.
*/
public void setStrokeColor(String value)
{
// Lazy and cached instantiation strategy for all stroke properties
if (state.strokeColorValue == null || !state.strokeColorValue.equals(value))
{
state.strokeColorValue = value;
state.strokeColor = null;
}
}
/**
*
*/
public void setDashed(boolean value)
{
// Lazy and cached instantiation strategy for all stroke properties
if (value != state.dashed)
{
state.dashed = value;
}
}
/**
*
*/
public void setDashPattern(String value)
{
if (value != null && value.length() > 0)
{
state.dashPattern = mxUtils.parseDashPattern(value);
}
}
/**
*
*/
public void setLineCap(String value)
{
if (!state.lineCap.equals(value))
{
state.lineCap = value;
}
}
/**
*
*/
public void setLineJoin(String value)
{
if (!state.lineJoin.equals(value))
{
state.lineJoin = value;
}
}
/**
*
*/
public void setMiterLimit(double value)
{
if (value != state.miterLimit)
{
state.miterLimit = value;
}
}
/**
*
*/
public void setFontSize(double value)
{
if (value != state.fontSize)
{
state.fontSize = value;
}
}
/**
*
*/
public void setFontColor(String value)
{
if (state.fontColorValue == null || !state.fontColorValue.equals(value))
{
state.fontColorValue = value;
state.fontColor = null;
}
}
/**
*
*/
public void setFontBackgroundColor(String value)
{
if (state.fontBackgroundColorValue == null || !state.fontBackgroundColorValue.equals(value))
{
state.fontBackgroundColorValue = value;
state.fontBackgroundColor = null;
}
}
/**
*
*/
public void setFontBorderColor(String value)
{
if (state.fontBorderColorValue == null || !state.fontBorderColorValue.equals(value))
{
state.fontBorderColorValue = value;
state.fontBorderColor = null;
}
}
/**
*
*/
public void setFontFamily(String value)
{
if (!state.fontFamily.equals(value))
{
state.fontFamily = value;
}
}
/**
*
*/
public void setFontStyle(int value)
{
if (value != state.fontStyle)
{
state.fontStyle = value;
}
}
/**
*
*/
public void setAlpha(double value)
{
if (state.alpha != value)
{
state.g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, (float) (value)));
state.alpha = value;
}
}
/**
*
*/
public void setFillColor(String value)
{
if (state.fillColorValue == null || !state.fillColorValue.equals(value))
{
state.fillColorValue = value;
state.fillColor = null;
// Setting fill color resets gradient paint
state.gradientPaint = null;
}
}
/**
*
*/
public void setGradient(String color1, String color2, double x, double y, double w, double h, String direction, double alpha1,
double alpha2)
{
// LATER: Add lazy instantiation and check if paint already created
float x1 = (float) ((state.dx + x) * state.scale);
float y1 = (float) ((state.dy + y) * state.scale);
float x2 = (float) x1;
float y2 = (float) y1;
h *= state.scale;
w *= state.scale;
if (direction == null || direction.length() == 0 || direction.equals(mxConstants.DIRECTION_SOUTH))
{
y2 = (float) (y1 + h);
}
else if (direction.equals(mxConstants.DIRECTION_EAST))
{
x2 = (float) (x1 + w);
}
else if (direction.equals(mxConstants.DIRECTION_NORTH))
{
y1 = (float) (y1 + h);
}
else if (direction.equals(mxConstants.DIRECTION_WEST))
{
x1 = (float) (x1 + w);
}
Color c1 = parseColor(color1);
if (alpha1 != 1)
{
c1 = new Color(c1.getRed(), c1.getGreen(), c1.getBlue(), (int) (alpha1 * 255));
}
Color c2 = parseColor(color2);
if (alpha2 != 1)
{
c2 = new Color(c2.getRed(), c2.getGreen(), c2.getBlue(), (int) (alpha2 * 255));
}
state.gradientPaint = new GradientPaint(x1, y1, c1, x2, y2, c2, true);
// Resets fill color
state.fillColorValue = null;
}
/**
* Helper method that uses {@link mxUtils#parseColor(String)}.
*/
protected Color parseColor(String hex)
{
Color result = colorCache.get(hex);
if (result == null)
{
result = mxUtils.parseColor(hex);
colorCache.put(hex, result);
}
return result;
}
/**
*
*/
public void rect(double x, double y, double w, double h)
{
currentPath = new GeneralPath();
currentPath.append(new Rectangle2D.Double((state.dx + x) * state.scale, (state.dy + y) * state.scale, w * state.scale, h
* state.scale), false);
}
/**
* Implements a rounded rectangle using a path.
*/
public void roundrect(double x, double y, double w, double h, double dx, double dy)
{
// LATER: Use arc here or quad in VML/SVG for exact match
begin();
moveTo(x + dx, y);
lineTo(x + w - dx, y);
quadTo(x + w, y, x + w, y + dy);
lineTo(x + w, y + h - dy);
quadTo(x + w, y + h, x + w - dx, y + h);
lineTo(x + dx, y + h);
quadTo(x, y + h, x, y + h - dy);
lineTo(x, y + dy);
quadTo(x, y, x + dx, y);
}
/**
*
*/
public void ellipse(double x, double y, double w, double h)
{
currentPath = new GeneralPath();
currentPath.append(new Ellipse2D.Double((state.dx + x) * state.scale, (state.dy + y) * state.scale, w * state.scale, h
* state.scale), false);
}
/**
*
*/
public void image(double x, double y, double w, double h, String src, boolean aspect, boolean flipH, boolean flipV)
{
if (src != null && w > 0 && h > 0)
{
Image img = loadImage(src);
if (img != null)
{
Rectangle bounds = getImageBounds(img, x, y, w, h, aspect);
img = scaleImage(img, bounds.width, bounds.height);
if (img != null)
{
drawImage(createImageGraphics(bounds.x, bounds.y, bounds.width, bounds.height, flipH, flipV), img, bounds.x, bounds.y);
}
}
}
}
/**
*
*/
protected void drawImage(Graphics2D graphics, Image image, int x, int y)
{
graphics.drawImage(image, x, y, null);
}
/**
* Hook for image caching.
*/
protected Image loadImage(String src)
{
return mxUtils.loadImage(src);
}
/**
*
*/
protected final Rectangle getImageBounds(Image img, double x, double y, double w, double h, boolean aspect)
{
x = (state.dx + x) * state.scale;
y = (state.dy + y) * state.scale;
w *= state.scale;
h *= state.scale;
if (aspect)
{
Dimension size = getImageSize(img);
double s = Math.min(w / size.width, h / size.height);
int sw = (int) Math.round(size.width * s);
int sh = (int) Math.round(size.height * s);
x += (w - sw) / 2;
y += (h - sh) / 2;
w = sw;
h = sh;
}
else
{
w = Math.round(w);
h = Math.round(h);
}
return new Rectangle((int) x, (int) y, (int) w, (int) h);
}
/**
* Returns the size for the given image.
*/
protected Dimension getImageSize(Image image)
{
return new Dimension(image.getWidth(null), image.getHeight(null));
}
/**
* Uses {@link #IMAGE_SCALING} to scale the given image.
*/
protected Image scaleImage(Image img, int w, int h)
{
Dimension size = getImageSize(img);
if (w == size.width && h == size.height)
{
return img;
}
else
{
return img.getScaledInstance(w, h, IMAGE_SCALING);
}
}
/**
* Creates a graphic instance for rendering an image.
*/
protected final Graphics2D createImageGraphics(double x, double y, double w, double h, boolean flipH, boolean flipV)
{
Graphics2D g2 = state.g;
if (flipH || flipV)
{
g2 = (Graphics2D) g2.create();
if (flipV && flipH)
{
g2.rotate(Math.toRadians(180), x + w / 2, y + h / 2);
}
else
{
int sx = 1;
int sy = 1;
int dx = 0;
int dy = 0;
if (flipH)
{
sx = -1;
dx = (int) (-w - 2 * x);
}
if (flipV)
{
sy = -1;
dy = (int) (-h - 2 * y);
}
g2.scale(sx, sy);
g2.translate(dx, dy);
}
}
return g2;
}
/**
* Creates a HTML document around the given markup.
*/
protected String createHtmlDocument(String text, String align, String valign, int w, int h, boolean wrap, String overflow, boolean clip)
{
StringBuffer css = new StringBuffer();
css.append("display:inline;");
css.append("font-family:" + state.fontFamily + ";");
css.append("font-size:" + Math.round(state.fontSize) + " pt;");
css.append("color:" + state.fontColorValue + ";");
// KNOWN: Line-height ignored in JLabel
css.append("line-height:" + ((mxConstants.ABSOLUTE_LINE_HEIGHT) ? Math.round(state.fontSize * mxConstants.LINE_HEIGHT) + " pt" : mxConstants.LINE_HEIGHT) + ";");
boolean setWidth = false;
if ((state.fontStyle & mxConstants.FONT_BOLD) == mxConstants.FONT_BOLD)
{
css.append("font-weight:bold;");
}
if ((state.fontStyle & mxConstants.FONT_ITALIC) == mxConstants.FONT_ITALIC)
{
css.append("font-style:italic;");
}
if ((state.fontStyle & mxConstants.FONT_UNDERLINE) == mxConstants.FONT_UNDERLINE)
{
css.append("text-decoration:underline;");
}
if (align != null)
{
if (align.equals(mxConstants.ALIGN_CENTER))
{
css.append("text-align:center;");
}
else if (align.equals(mxConstants.ALIGN_RIGHT))
{
css.append("text-align:right;");
}
}
if (state.fontBackgroundColorValue != null)
{
css.append("background-color:" + state.fontBackgroundColorValue + ";");
}
// KNOWN: Border ignored in JLabel
if (state.fontBorderColorValue != null)
{
css.append("border:1pt solid " + state.fontBorderColorValue + ";");
}
// KNOWN: max-width/-height ignored in JLabel
if (clip)
{
css.append("overflow:hidden;");
setWidth = true;
}
else if (overflow != null)
{
if (overflow.equals("fill"))
{
css.append("height:" + Math.round(h) + "pt;");
setWidth = true;
}
else if (overflow.equals("width"))
{
setWidth = true;
if (h > 0)
{
css.append("height:" + Math.round(h) + "pt;");
}
}
}
if (wrap)
{
if (!clip)
{
// NOTE: Max-width not available in Java
setWidth = true;
}
css.append("white-space:normal;");
}
else
{
css.append("white-space:nowrap;");
}
if (setWidth && w > 0)
{
css.append("width:" + Math.round(w) + "pt;");
}
return createHtmlDocument(text, css.toString());
}
/**
* Creates a HTML document for the given text and CSS style.
*/
protected String createHtmlDocument(String text, String style)
{
return "<html><div style=\"" + style + "\">" + text + "</div></html>";
}
/**
* Hook to return the renderer for HTML formatted text. This implementation returns
* the shared instance of mxLighweightLabel.
*/
protected JLabel getTextRenderer()
{
return mxLightweightLabel.getSharedInstance();
}
/**
*
*/
protected Point2D getMargin(String align, String valign)
{
double dx = 0;
double dy = 0;
if (align != null)
{
if (align.equals(mxConstants.ALIGN_CENTER))
{
dx = -0.5;
}
else if (align.equals(mxConstants.ALIGN_RIGHT))
{
dx = -1;
}
}
if (valign != null)
{
if (valign.equals(mxConstants.ALIGN_MIDDLE))
{
dy = -0.5;
}
else if (valign.equals(mxConstants.ALIGN_BOTTOM))
{
dy = -1;
}
}
return new Point2D.Double(dx, dy);
}
/**
* Draws the given HTML text.
*/
protected void htmlText(double x, double y, double w, double h, String str, String align, String valign, boolean wrap, String format,
String overflow, boolean clip, double rotation)
{
x += state.dx;
y += state.dy;
JLabel textRenderer = getTextRenderer();
if (textRenderer != null && rendererPane != null)
{
// Use native scaling for HTML
AffineTransform previous = state.g.getTransform();
state.g.scale(state.scale, state.scale);
double rad = rotation * (Math.PI / 180);
state.g.rotate(rad, x, y);
// Renders the scaled text with a correction factor of
// PX_PER_PIXEL for px in HTML vs pixels in the bitmap
boolean widthFill = false;
boolean fill = false;
String original = str;
if (overflow != null)
{
widthFill = overflow.equals("width");
fill = overflow.equals("fill");
}
str = createHtmlDocument(str, align, valign, (widthFill || fill) ?
(int) Math.round(w) : 0, (fill) ?
(int) Math.round(h) : 0, wrap, overflow, clip);
textRenderer.setText(str);
Dimension pref = textRenderer.getPreferredSize();
int prefWidth = pref.width;
int prefHeight = pref.height;
// Poor man's max-width
// TODO: Is this still needed?
if (((clip || wrap) && prefWidth > w && w > 0) || (clip && prefHeight > h && h > 0))
{
// TextWidthDelta is workaround for inconsistent word wrapping in Java
int cw = (int) Math.round(w + ((wrap) ? JAVA_TEXT_WIDTH_DELTA : 0));
int ch = (int) Math.round(h);
str = createHtmlDocument(original, align, valign, cw, ch, wrap, overflow, clip);
textRenderer.setText(str);
pref = textRenderer.getPreferredSize();
prefWidth = pref.width;
prefHeight = pref.height + 2;
}
// Matches HTML output
if (clip && w > 0 && h > 0)
{
prefWidth = Math.min(pref.width, (int) w);
prefHeight = Math.min(prefHeight, (int) h);
h = prefHeight;
}
else if (!clip && wrap && w > 0 && h > 0)
{
prefWidth = pref.width;
w = Math.max(pref.width, (int) w);
h = prefHeight;
prefHeight = Math.max(prefHeight, (int) h);
}
else if (!clip && !wrap)
{
if (w > 0 && w < prefWidth)
{
w = prefWidth;
}
if (h > 0 && h < prefHeight)
{
h = prefHeight;
}
}
Point2D margin = getMargin(align, valign);
x += margin.getX() * prefWidth;
y += margin.getY() * prefHeight;
if (w == 0)
{
w = prefWidth;
}
if (h == 0)
{
h = prefHeight;
}
rendererPane.paintComponent(state.g, textRenderer, rendererPane, (int) Math.round(x), (int) Math.round(y), (int) Math.round(w),
(int) Math.round(h), true);
state.g.setTransform(previous);
}
}
/**
* Draws the given text.
*/
public void text(double x, double y, double w, double h, String str, String align, String valign, boolean wrap, String format,
String overflow, boolean clip, double rotation, String textDirection)
{
// TODO: Add support for text direction
if (format != null && format.equals("html"))
{
htmlText(x, y, w, h, str, align, valign, wrap, format, overflow, clip, rotation);
}
else
{
plainText(x, y, w, h, str, align, valign, wrap, format, overflow, clip, rotation);
}
}
/**