-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathJSGraphics2D.java
More file actions
1023 lines (900 loc) · 24.7 KB
/
Copy pathJSGraphics2D.java
File metadata and controls
1023 lines (900 loc) · 24.7 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
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 swingjs;
import java.util.Hashtable;
import java.util.Map;
import java.util.Map.Entry;
import javajs.J2SIgnoreImport;
import jsjava.awt.AlphaComposite;
import jsjava.awt.BasicStroke;
import jsjava.awt.Color;
import jsjava.awt.Composite;
import jsjava.awt.Font;
import jsjava.awt.FontMetrics;
import jsjava.awt.Graphics;
import jsjava.awt.GraphicsConfiguration;
import jsjava.awt.Image;
import jsjava.awt.Paint;
import jsjava.awt.Rectangle;
import jsjava.awt.RenderingHints;
import jsjava.awt.RenderingHints.Key;
import jsjava.awt.Shape;
import jsjava.awt.Stroke;
import jsjava.awt.font.FontRenderContext;
import jsjava.awt.geom.AffineTransform;
import jsjava.awt.geom.Path2D;
import jsjava.awt.geom.PathIterator;
import jsjava.awt.image.BufferedImage;
import jsjava.awt.image.BufferedImageOp;
import jsjava.awt.image.ImageObserver;
import jsjava.awt.image.RenderedImage;
import jsjava.awt.image.renderable.RenderableImage;
import jsjava.text.AttributedCharacterIterator;
import jssun.java2d.SunGraphics2D;
import swingjs.api.DOMNode;
import swingjs.api.HTML5Canvas;
import swingjs.api.HTML5CanvasContext2D;
/**
* generic 2D drawing methods -- JavaScript version
*
* guessing a lot here -- just getting something out; converted from JSpecView
*
* but see sun.java2d.SunGraphics2D.java for insight into the issues of full
* implementation
*
* @author Bob Hanson hansonr@stolaf.edu
*/
@J2SIgnoreImport(jsjava.awt.AlphaComposite.class)
public class JSGraphics2D extends SunGraphics2D implements Cloneable {
private boolean backgroundPainted;
public int constrainX;
public int constrainY;
private int width;
private int height;
private HTML5Canvas canvas;
private HTML5CanvasContext2D ctx;
private GraphicsConfiguration gc;
private BasicStroke currentStroke;
private Shape currentClip;
private AlphaComposite currentComposite;
private int initialState;
//public int strokeState;
//public int transformState;
//public int clipState;
//public Color foregroundColor;
boolean isShifted;// private, but only JavaScript
private Font font;
//private Color currentColor;
public JSGraphics2D(Object canvas) { // this must be Object, because we are passing an actual HTML5 canvas
hints = new RenderingHints(new Hashtable());
this.canvas = (HTML5Canvas) canvas;
ctx = this.canvas.getContext("2d");
transform = new AffineTransform();
setStroke(new BasicStroke());
/**
* @j2sNative
*
* this.gc = SwingJS;
* this.width = canvas.width;
* this.height = canvas.height;
*
*/
{
}
}
/**
* the SwingJS object
*/
@Override
public GraphicsConfiguration getDeviceConfiguration() {
return gc;
}
@Override
public void drawLine(int x0, int y0, int x1, int y1) {
boolean inPath = this.inPath;
if (!inPath)
ctx.beginPath();
ctx.moveTo(x0, y0);
ctx.lineTo(x1, y1);
if (!inPath)
ctx.stroke();
}
@Override
public void drawOval(int left, int top, int width, int height) {
if (width == height)
doCirc(left, top, width, false);
else
doArc(left, top, width, height, 0, 360, false);
}
@Override
public void fillOval(int left, int top, int width, int height) {
if (width == height)
doCirc(left, top, width, true);
else
doArc(left, top, width, height, 0, 360, true);
}
@Override
public void drawArc(int x, int y, int width, int height, int startAngle,
int arcAngle) {
doArc(x, y, width, height, startAngle, arcAngle, false);
}
@Override
public void fillArc(int centerX, int centerY, int width, int height, int startAngle,
int arcAngle) {
doArc(centerX, centerY, width, height, startAngle, arcAngle, true);
}
private void doCirc(int left, int top, int diameter, boolean fill) {
double r = diameter / 2f;
ctx.beginPath();
ctx.arc(left + r, top + r, r, 0, 2 * Math.PI, false);
ctx.closePath();
if (fill)
ctx.fill();
else
ctx.stroke();
}
private void doArc(double x, double y, double width, double height, double startAngle,
double arcAngle, boolean fill) {
// boolean doClose = (arcAngle - startAngle == 360);
ctx.save();
{
if (arcAngle < 0) {
startAngle += arcAngle;
arcAngle = -arcAngle;
}
ctx.translate(x, y);
ctx.scale(width / 2, height / 2);
ctx.beginPath();
ctx.arc(1, 1, 1, toRad (360 - startAngle), toRad (360 - arcAngle - startAngle), true);
if (fill)
ctx.lineTo(1, 1);
}
ctx.restore();
if (fill)
ctx.fill();
else
ctx.stroke();
}
private double toRad(double a) {
return a * (Math.PI / 180);
}
@Override
public void drawPolygon(int[] ayPoints, int[] axPoints, int nPoints) {
doPoly(ayPoints, axPoints, nPoints, false);
}
/**
* @param axPoints
* @param ayPoints
* @param nPoints
* @param doFill
*/
private void doPoly(int[] axPoints, int[] ayPoints, int nPoints,
boolean doFill) {
ctx.beginPath();
ctx.moveTo(axPoints[0], ayPoints[0]);
for (int i = 1; i < nPoints; i++)
ctx.lineTo(axPoints[i], ayPoints[i]);
if (doFill) {
ctx.fill();
} else {
ctx.lineTo(axPoints[0], ayPoints[0]);
ctx.stroke();
}
}
@Override
public void drawRect(int x, int y, int width, int height) {
ctx.beginPath();
ctx.rect(x, y, width, height);
ctx.stroke();
}
public void background(Color bgcolor) {
backgroundColor = bgcolor;
if (bgcolor == null) {
/*
*
* reduce antialiasing, thank you,
* http://www.rgraph.net/docs/howto-get-crisp-lines-with-no-antialias.html
*/
if (!isShifted)
ctx.translate(-0.5, -0.5);
isShifted = true;
return;
}
clearRect(0, 0, width, height);
}
@Override
public void fillPolygon(int[] ayPoints, int[] axPoints, int nPoints) {
doPoly(ayPoints, axPoints, nPoints, true);
}
@Override
public void fillRect(int x, int y, int width, int height) {
backgroundPainted = true;
ctx.fillRect(x, y, width, height);
}
@Override
public void setFont(Font font) {
// this equality check speeds mark/reset significantly
if (font == this.font)
return;
this.font = font;
if (font != null)
HTML5CanvasContext2D.setFont(ctx, JSToolkit.getCanvasFont(font));
}
public void setStrokeBold(boolean tf) {
setLineWidth(tf ? 2. : 1.);
}
private void setLineWidth(double d) {
HTML5CanvasContext2D.setLineWidth(ctx, d);
}
public boolean canDoLineTo() {
return true;
}
boolean inPath;
public void doStroke(boolean isBegin) {
inPath = isBegin;
if (isBegin) {
ctx.beginPath();
} else {
ctx.stroke();
}
}
public void lineTo(int x2, int y2) {
ctx.lineTo(x2, y2);
}
@Override
public void clip(Shape s) {
doShape(s);
currentClip = s;
ctx.clip();
}
@Override
public void draw(Shape s) {
doShape(s);
ctx.stroke();
}
private int doShape(Shape s) {
ctx.beginPath();
double[] pts = new double[6];
PathIterator pi = s.getPathIterator(null);
while (!pi.isDone()) {
switch (pi.currentSegment(pts)) {
case PathIterator.SEG_MOVETO:
ctx.moveTo(pts[0], pts[1]);
break;
case PathIterator.SEG_LINETO:
ctx.lineTo(pts[0], pts[1]);
break;
case PathIterator.SEG_QUADTO:
ctx.quadraticCurveTo(pts[0], pts[1], pts[2], pts[3]);
break;
case PathIterator.SEG_CUBICTO:
ctx.bezierCurveTo(pts[0], pts[1], pts[2], pts[3], pts[4], pts[5]);
break;
case PathIterator.SEG_CLOSE:
ctx.closePath();
break;
}
pi.next();
}
return pi.getWindingRule();
// then fill or stroke or clip
}
@Override
public void fill(Shape s) {
if (doShape(s) == Path2D.WIND_EVEN_ODD)
/**
* @j2sNative
*
* this.ctx.fill("evenodd");
*/
{
} else
ctx.fill();
}
@Override
public boolean drawImage(Image img, int x, int y, ImageObserver observer) {
return drawImagePriv(img, x, y, observer);
}
@Override
public boolean drawImage(Image img, int x, int y, int width, int height,
ImageObserver observer) {
backgroundPainted = true;
if (img != null) {
DOMNode imgNode = getImageNode(img);
if (imgNode != null)
ctx.drawImage(imgNode, x, y, width, height);
if (observer != null)
observe(img, observer, imgNode != null);
}
return true;
}
private DOMNode getImageNode(Image img) {
backgroundPainted = true;
DOMNode imgNode = DOMNode.getImageNode(img);
return (imgNode == null ? JSToolkit.getCompositor().createImageNode(img) : imgNode);
}
private void observe(Image img, ImageObserver observer, boolean isOK) {
observer.imageUpdate(img, (isOK ? 0 : ImageObserver.ABORT | ImageObserver.ERROR), -1, -1, -1, -1);
}
@Override
public boolean drawImage(Image img, int x, int y, Color bgcolor,
ImageObserver observer) {
backgroundPainted = true;
JSToolkit.notImplemented(null);
return drawImage(img, x, y, observer);
}
@Override
public boolean drawImage(Image img, int x, int y, int width, int height,
Color bgcolor, ImageObserver observer) {
backgroundPainted = true;
JSToolkit.notImplemented(null);
return drawImage(img, x, y, width, height, observer);
}
@SuppressWarnings("unused")
@Override
public boolean drawImage(Image img, int dx1, int dy1, int dx2, int dy2,
int sx1, int sy1, int sx2, int sy2, ImageObserver observer) {
backgroundPainted = true;
if (img != null) {
byte[] bytes = null;
DOMNode imgNode = getImageNode(img);
if (imgNode != null)
HTML5CanvasContext2D.stretchImage(ctx, imgNode, sx1, sy1, sx2 - sx1, sy2
- sy1, dx1, dy1, dx2 - dx1, dy2 - dy1);
if (observer != null)
observe(img, observer, imgNode != null);
}
return true;
}
@Override
public boolean drawImage(Image img, int dx1, int dy1, int dx2, int dy2,
int sx1, int sy1, int sx2, int sy2, Color bgcolor, ImageObserver observer) {
JSToolkit.notImplemented(null);
return drawImage(img, dx1, dy1, dx2, dy2, sx1, sy1, sx2, sy2, observer);
}
@Override
public boolean drawImage(Image img, AffineTransform xform, ImageObserver obs) {
return drawImageXT(img, xform, obs);
}
@Override
public void drawRenderedImage(RenderedImage img, AffineTransform xform) {
drawImageXT((Image) img, xform, null);
}
@Override
public void drawRenderableImage(RenderableImage img, AffineTransform xform) {
drawImageXT((Image) img, xform, null);
}
private boolean drawImageXT(Image img, AffineTransform xform,
ImageObserver obs) {
ctx.save();
transformCTX(xform);
boolean ret = drawImagePriv(img, 0, 0, obs);
ctx.restore();
return ret;
}
@SuppressWarnings("unused")
public boolean drawImagePriv(Image img, int x, int y, ImageObserver observer) {
backgroundPainted = true;
if (img != null) {
int[] pixels = null;
boolean isRGB = false; // usually RGBA
/**
* @j2sNative
*
* pixels = img._pix;
* isRGB = (img.imageType == 1);
*
*/
{
}
DOMNode imgNode = null;
int width = ((BufferedImage)img).getRaster().getWidth();
int height = ((BufferedImage)img).getRaster().getHeight();
if (pixels == null) {
if ((imgNode = getImageNode(img)) != null)
ctx.drawImage(imgNode, x, y, width, height);
} else {
Object imageData = HTML5CanvasContext2D.getImageData(ctx, x, y, width, height);
int[] buf8 = HTML5CanvasContext2D.getBuf8(imageData);
for (int pt = 0, i = 0, n = Math.min(buf8.length/4, pixels.length); i < n; i++) {
int argb = pixels[i];
buf8[pt++] = (argb >> 16) & 0xFF;
buf8[pt++] = (argb >> 8) & 0xFF;
buf8[pt++] = argb & 0xFF;
buf8[pt++] = (isRGB ? 0xFF : (argb >> 24) & 0xFF);
}
HTML5CanvasContext2D.putImageData(ctx, imageData, x, y);
}
if (observer != null)
observe(img, observer, imgNode != null);
}
return true;
}
@Override
public boolean hit(Rectangle rect, Shape s, boolean onStroke) {
JSToolkit.notImplemented(null);
return false;
}
@Override
public Stroke getStroke() {
return currentStroke;
}
@SuppressWarnings("unused")
@Override
public void setStroke(Stroke s) {
if (!(s instanceof BasicStroke))
return;
BasicStroke b = currentStroke = (BasicStroke) s;
float[] dash = b.getDashArray();
int[] idash = new int[dash == null ? 0 : dash.length];
for (int i = idash.length; --i >= 0;)
idash[i] = (int) dash[i];
ctx.setLineDash(idash);
setLineWidth(b.getLineWidth());
String lineCap, lineJoin;
float miterLimit = -1;
switch (b.getEndCap()) {
case BasicStroke.CAP_BUTT:
lineCap = "butt";
break;
case BasicStroke.CAP_SQUARE:
lineCap = "square";
break;
case BasicStroke.CAP_ROUND:
default:
lineCap = "round";
}
switch (b.getLineJoin()) {
case BasicStroke.JOIN_BEVEL:
lineJoin = "bevel";
break;
case BasicStroke.JOIN_MITER:
lineJoin = "miter";
miterLimit = b.getMiterLimit();
break;
case BasicStroke.JOIN_ROUND:
lineJoin = "round";
}
/**
* @j2sNative
*
* this.ctx.lineCap = lineCap; this.ctx.lineJoin = lineJoin; if
* (miterLimit >= 0) this.ctx.miterLimit = miterLimit;
*/
{
}
// SwingJS TODO more here
}
@Override
public void setRenderingHint(Key hintKey, Object hintValue) {
hints.put(hintKey, hintValue);
}
@Override
public Object getRenderingHint(Key hintKey) {
return hints.get(hintKey);
}
@Override
public void setRenderingHints(Map<?, ?> hints) {
this.hints = new RenderingHints((Map<Key, ?>) hints);
}
@Override
public void addRenderingHints(Map<?, ?> hints) {
for (Entry<?, ?> e : hints.entrySet())
this.hints.put(e.getKey(), e.getValue());
}
@Override
public RenderingHints getRenderingHints() {
return hints;
}
@Override
public void setBackground(Color color) {
background(color);
}
@Override
public Color getBackground() {
return backgroundColor;
}
@Override
public Color getColor() {
return foregroundColor;
}
@Override
public void setColor(Color c) {
foregroundColor = c;
setGraphicsColor(c);
}
@Override
public void setPaint(Paint paint) {
setColor((Color) paint);
}
@Override
public Font getFont() {
if (font == null)
font = new Font("Arial", Font.PLAIN, 12);
return font;
}
@Override
public FontMetrics getFontMetrics() {
return getFont().getFontMetrics();
}
@Override
public FontMetrics getFontMetrics(Font f) {
return f.getFontMetrics();
}
@Override
public void clipRect(int x, int y, int width, int height) {
// SwingJS -- this is not quite right. Should ADD this to the clipping
// region
ctx.beginPath();
ctx.rect(x, y, width, height);
setCurrentClip(x, y, width, height);
ctx.clip();
}
@Override
public void setClip(int x, int y, int width, int height) {
// clipping is disabled because for general component painting
// because it consumes so much processing.
// it is presumed that the user will clip when desired
/**
* @j2sNative
*
* if (arguments.length == 1) { this.setClip1(x); return; }
*/
{}
setCurrentClip(x, y, width, height);
ctx.beginPath();
ctx.rect(x, y, width, height);
ctx.clip();
}
private void setCurrentClip(int x, int y, int width, int height) {
Rectangle r = (currentClip instanceof Rectangle ? (Rectangle) currentClip : null);
if (r == null || r.x != x || r.y != y || r.width != width || r.height != height)
currentClip = new Rectangle(x, y, width, height);
}
@Override
public void setClip(Shape clip) {
setClip1(clip);
}
public void setClip1(Shape clip) {
currentClip = clip;
ctx.beginPath();
doShape(clip);
ctx.clip();
}
@Override
public void clearRect(int x, int y, int width, int height) {
ctx.clearRect(x, y, width, height);
setGraphicsColor(backgroundColor == null ? Color.WHITE : backgroundColor);
fillRect(x, y, width, height);
setGraphicsColor(foregroundColor);
}
private void setGraphicsColor(Color c) {
if (c == null)
return; // this was the case with a JRootPanel graphic call
HTML5CanvasContext2D.setColor(ctx, JSToolkit.getCSSColor(c));
}
@Override
public void drawPolyline(int[] xPoints, int[] yPoints, int nPoints) {
if (nPoints < 2)
return;
ctx.moveTo(xPoints[0], yPoints[0]);
for (int i = 1; i < nPoints; i++) {
ctx.lineTo(xPoints[i], yPoints[i]);
}
ctx.stroke();
}
@Override
public void copyArea(int x, int y, int width, int height, int dx, int dy) {
JSToolkit.notImplemented(null);
}
@Override
public void drawRoundRect(int x, int y, int width, int height, int arcWidth,
int arcHeight) {
JSToolkit.notImplemented(null);
drawRect(x, y, width, height);
}
@Override
public void fillRoundRect(int x, int y, int width, int height, int arcWidth,
int arcHeight) {
JSToolkit.notImplemented(null);
fillRect(x, y, width, height);
}
@Override
public Shape getClip() {
return currentClip == null ? getClipBoundsImpl() : currentClip;
}
@Override
public void drawString(String s, int x, int y) {
ctx.fillText(s, x, y);
}
public void drawStringUnique(String s, int x, int y) {
ctx.fillText(s, x, y);
}
@Override
public void drawStringTrans(String str, float x, float y) {
// apply affine transformation first
JSToolkit.notImplemented(null);
}
@Override
public void drawString(AttributedCharacterIterator iterator, int x, int y) {
JSToolkit.notImplemented(null);
}
@Override
public void drawStringAttrTrans(AttributedCharacterIterator iterator,
float x, float y) {
JSToolkit.notImplemented(null);
}
@Override
public void translateTrans(double tx, double ty) {
JSToolkit.notImplemented(null);
}
@Override
public void shear(double shx, double shy) {
JSToolkit.notImplemented(null);
}
@Override
public void translate(int x, int y) {
ctx.translate(x, y);
transform.translate(x, y);
}
@Override
public void rotate(double radians) {
ctx.rotate(radians);
transform.rotate(radians);
}
@Override
public void rotate(double theta, double x, double y) {
ctx.translate(x, y);
ctx.rotate(theta);
ctx.translate(-x, -y);
transform.rotate(theta, x, y);
}
@Override
public void scale(double sx, double sy) {
ctx.scale(sx, sy);
transform.scale(sx, sy);
}
/**
* concatenates the given transform matrix to the current transform
*
*/
@Override
public void transform(AffineTransform t) {
transformCTX(t);
transform.concatenate(t);
}
private void transformCTX(AffineTransform t) {
/**
* @j2sNative
*
* this.ctx.transform (t.m00, t.m10, t.m01, t.m11, t.m02, t.m12);
*/
{}
}
/**
* sets the transform matrix to the given one
*/
@Override
public void setTransform(AffineTransform t) {
/**
* @j2sNative
*
* this.ctx.setTransform (t.m00, t.m10, t.m01, t.m11, t.m02,
* t.m12);
*
* this.$transform.setTransformImpl(t);
*
*/
{
transform.setTransform(t);
}
}
/**
* Returns a copy of the current transform
*/
@Override
public AffineTransform getTransform() {
return (AffineTransform) transform.clone();
}
// /**
// * Returns the current Transform ignoring the "constrain"
// * rectangle.
// */
// public AffineTransform cloneTransform() {
// return (AffineTransform) transform.clone();
// }
//
@Override
public Paint getPaint() {
return getColor();
}
@Override
public FontRenderContext getFontRenderContext() {
return font.getFontMetrics().getFontRenderContext();
}
@Override
public void setPaintMode() {
JSToolkit.notImplemented(null);
}
@Override
public void setXORMode(Color c1) {
JSToolkit.notImplemented(null);
}
@SuppressWarnings("unused")
@Override
public Rectangle getClipBounds() {
Rectangle r = null;
/**
* @j2sNative
*
* if (arguments.length == 1) r = arguments[0];
*/
{
}
Rectangle clipRect = getClipBoundsImpl();
if (r == null) {
r = clipRect;
} else {
r.x = clipRect.x;
r.y = clipRect.y;
r.width = clipRect.width;
r.height = clipRect.height;
}
return r;
}
private Rectangle getClipBoundsImpl() {
if (currentClip == null) {
currentClip = new Rectangle(0, 0, width, height);
}
return currentClip.getBounds();
}
@Override
public void setComposite(Composite comp) {
// this equality check speeds mark/reset significantly
if (comp == this.currentComposite)
return;
// alpha composite only here
int newRule = 0;
boolean isValid = (comp == null || currentComposite == null ||
(comp instanceof AlphaComposite)
&& (newRule = ((AlphaComposite) comp).getRule()) != currentComposite.getRule());
if (isValid && JSToolkit.setGraphicsCompositeAlpha(this, newRule)) {
currentComposite = (AlphaComposite) comp;
}
setAlpha(comp == null ? 1 : ((AlphaComposite) comp).getAlpha());
}
public Composite getComposite() {
return currentComposite;
}
@Override
public void drawImage(BufferedImage img, BufferedImageOp op, int x, int y) {
JSToolkit.drawImageOp(this, img, op, x, y);
}
public void setAlpha(float f) {
/**
* @j2sNative
*
* this.ctx.globalAlpha = f;
*/
{}
}
public HTML5Canvas getCanvas() {
return canvas;
}
/**
* used to determine if it is likely that the background was painted
*
* @return background taint count
*/
public boolean isBackgroundPainted() {
return backgroundPainted;
}
/////////////// saving of the state ////////////////
private final static int SAVE_ALPHA = 0;
private final static int SAVE_COMPOSITE = 1;
private final static int SAVE_STROKE = 2;
private final static int SAVE_TRANSFORM = 3;
private final static int SAVE_FONT = 4;
private final static int SAVE_CLIP = 5;
private final static int SAVE_BACKGROUND_PAINTED = 6;
private final static int SAVE_MAX = 7;
/**
* creates a save object to extend the capabilities of context2d.save()
* that brings that into line with Java's graphics2d .create()
*
* in development -- we need to identify all differences
*
*
* @return the length of the ctx._aSaved array after the push
*/
public int mark() {
ctx.save();
Object[] map = new Object[SAVE_MAX];
float alpha = 0;
/**
* @j2sNative
*
* alpha = this.ctx.globalAlpha;
*
*/
{}
map[SAVE_ALPHA] = Float.valueOf(alpha);
map[SAVE_COMPOSITE] = currentComposite;
map[SAVE_STROKE] = currentStroke;
map[SAVE_TRANSFORM] = transform;
map[SAVE_FONT] = font;
map[SAVE_CLIP] = currentClip;
map[SAVE_BACKGROUND_PAINTED] = (backgroundPainted ? Boolean.TRUE : Boolean.FALSE);
backgroundPainted = false;
return HTML5CanvasContext2D.push(ctx, map);
}
/**
* try to equate g.dispose() with ctx.restore().
* @param n0
*/
public void reset(int n0) {
if (n0 < 1)
n0 = 1;
while (HTML5CanvasContext2D.getSavedLevel(ctx) >= n0) {
Object[] map = HTML5CanvasContext2D.pop(ctx);
setComposite((Composite) map[SAVE_COMPOSITE]);
Float alpha = (Float) map[SAVE_ALPHA];
if (alpha != null)
setAlpha(alpha.floatValue());
setStroke((Stroke) map[SAVE_STROKE]);
setTransform((AffineTransform) map[SAVE_TRANSFORM]);
setFont((Font) map[SAVE_FONT]);
currentClip = (Shape) map[SAVE_CLIP];
backgroundPainted = ((Boolean) map[SAVE_BACKGROUND_PAINTED]).booleanValue();
ctx.restore();
}
}
@Override
public Graphics create() {
return (Graphics) clone();
}
@Override
public Object clone() {
int n = mark();
JSGraphics2D g = null;
/**
* avoid super call to Object.clone();
*
* @j2sNative
*
* g = Clazz.clone(this);
*
*/
{
g = this; // not passed on to JavaScript
}
g.transform = new AffineTransform(transform);
if (hints != null) {
g.hints = (RenderingHints) hints.clone();
}
/*
* FontInfos are re-used, so must be cloned too, if they are valid, and be
* nulled out if invalid. The implied trade-off is that there is more to be
* gained from re-using these objects than is lost by having to clone them
* when the SG2D is cloned.
*/
// if (this.fontInfo != null) {
// if (this.validFontInfo) {