-
Notifications
You must be signed in to change notification settings - Fork 48
/
SWTGraphics.java
1556 lines (1389 loc) · 39.5 KB
/
SWTGraphics.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
/*******************************************************************************
* Copyright (c) 2000, 2024 IBM Corporation and others.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* IBM Corporation - initial API and implementation
*******************************************************************************/
package org.eclipse.draw2d;
import java.util.ArrayList;
import java.util.List;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.graphics.FontMetrics;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.LineAttributes;
import org.eclipse.swt.graphics.Path;
import org.eclipse.swt.graphics.PathData;
import org.eclipse.swt.graphics.Pattern;
import org.eclipse.swt.graphics.Region;
import org.eclipse.swt.graphics.TextLayout;
import org.eclipse.swt.graphics.Transform;
import org.eclipse.swt.widgets.Display;
import org.eclipse.draw2d.geometry.PointList;
import org.eclipse.draw2d.geometry.Rectangle;
/**
* A concrete implementation of <code>Graphics</code> using an SWT
* <code>GC</code>. There are 2 states contained in this graphics class -- the
* applied state which is the actual state of the GC and the current state which
* is the current state of this graphics object. Certain properties can be
* changed multiple times and the GC won't be updated until it's actually used.
* <P>
* WARNING: This class is not intended to be subclassed.
*/
public class SWTGraphics extends Graphics {
/**
* An internal type used to represent and update the GC's clipping.
*
* @since 3.1
*/
interface Clipping {
/**
* Sets the clip's bounding rectangle into the provided argument and returns it
* for convenince.
*
* @param rect the rect
* @return the given rect
* @since 3.1
*/
Rectangle getBoundingBox(Rectangle rect);
Clipping getCopy();
void intersect(int left, int top, int right, int bottom);
void scale(float horizontal, float vertical);
void setOn(GC gc, int translateX, int translateY);
void translate(float dx, float dy);
}
/**
* Any state stored in this class is only applied when it is needed by a
* specific graphics call.
*
* @since 3.1
*/
static class LazyState {
Color bgColor;
Color fgColor;
Font font;
int graphicHints;
LineAttributes lineAttributes;
Clipping relativeClip;
}
static class RectangleClipping implements Clipping {
private float top;
private float left;
private float bottom;
private float right;
RectangleClipping(float left, float top, float right, float bottom) {
this.left = left;
this.right = right;
this.bottom = bottom;
this.top = top;
}
RectangleClipping(org.eclipse.swt.graphics.Rectangle rect) {
left = rect.x;
top = rect.y;
right = rect.x + rect.width;
bottom = rect.y + rect.height;
}
RectangleClipping(Rectangle rect) {
left = rect.x;
top = rect.y;
right = rect.right();
bottom = rect.bottom();
}
@Override
public Rectangle getBoundingBox(Rectangle rect) {
rect.x = (int) left;
rect.y = (int) top;
rect.width = (int) Math.ceil(right) - rect.x;
rect.height = (int) Math.ceil(bottom) - rect.y;
return rect;
}
@Override
public Clipping getCopy() {
return new RectangleClipping(left, top, right, bottom);
}
@Override
public void intersect(int left, int top, final int right, final int bottom) {
this.left = Math.max(this.left, left);
this.right = Math.min(this.right, right);
this.top = Math.max(this.top, top);
this.bottom = Math.min(this.bottom, bottom);
// use left/top -1 to ensure ceiling function doesn't add a pixel
if (this.right < this.left || this.bottom < this.top) {
this.right = this.left - 1;
this.bottom = this.top - 1;
}
}
@Override
public void scale(float horz, float vert) {
left /= horz;
right /= horz;
top /= vert;
bottom /= vert;
}
@Override
public void setOn(GC gc, int translateX, int translateY) {
int xInt = (int) Math.floor(left);
int yInt = (int) Math.floor(top);
gc.setClipping(xInt + translateX, yInt + translateY, (int) Math.ceil(right) - xInt,
(int) Math.ceil(bottom) - yInt);
}
@Override
public void translate(float dx, float dy) {
left += dx;
right += dx;
top += dy;
bottom += dy;
}
}
/**
* Contains the entire state of the Graphics.
*/
static class State extends LazyState implements Cloneable {
float[] affineMatrix;
int alpha;
Pattern bgPattern;
int dx;
int dy;
Pattern fgPattern;
@Override
public State clone() throws CloneNotSupportedException {
State clone = (State) super.clone();
clone.lineAttributes = SWTGraphics.clone(clone.lineAttributes);
return clone;
}
/**
* Copies all state information from the given State to this State
*
* @param state The State to copy from
*/
public void copyFrom(State state) {
bgColor = state.bgColor;
fgColor = state.fgColor;
lineAttributes = SWTGraphics.clone(state.lineAttributes);
dx = state.dx;
dy = state.dy;
bgPattern = state.bgPattern;
fgPattern = state.fgPattern;
font = state.font;
graphicHints = state.graphicHints;
affineMatrix = state.affineMatrix;
relativeClip = state.relativeClip;
alpha = state.alpha;
}
}
static final int AA_MASK;
static final int AA_SHIFT;
static final int AA_WHOLE_NUMBER = 1;
static final int ADVANCED_GRAPHICS_MASK;
static final int ADVANCED_HINTS_DEFAULTS;
static final int ADVANCED_HINTS_MASK;
static final int ADVANCED_SHIFT;
static final int FILL_RULE_MASK;
static final int FILL_RULE_SHIFT;
static final int FILL_RULE_WHOLE_NUMBER = -1;
static final int INTERPOLATION_MASK;
static final int INTERPOLATION_SHIFT;
static final int INTERPOLATION_WHOLE_NUMBER = 1;
static final int TEXT_AA_MASK;
static final int TEXT_AA_SHIFT;
static final int XOR_MASK;
static final int XOR_SHIFT;
static {
XOR_SHIFT = 3;
AA_SHIFT = 8;
TEXT_AA_SHIFT = 10;
INTERPOLATION_SHIFT = 12;
FILL_RULE_SHIFT = 14;
ADVANCED_SHIFT = 15;
AA_MASK = 3 << AA_SHIFT;
FILL_RULE_MASK = 1 << FILL_RULE_SHIFT; // If changed to more than 1-bit,
// check references!
INTERPOLATION_MASK = 3 << INTERPOLATION_SHIFT;
TEXT_AA_MASK = 3 << TEXT_AA_SHIFT;
XOR_MASK = 1 << XOR_SHIFT;
ADVANCED_GRAPHICS_MASK = 1 << ADVANCED_SHIFT;
ADVANCED_HINTS_MASK = TEXT_AA_MASK | AA_MASK | INTERPOLATION_MASK;
ADVANCED_HINTS_DEFAULTS = ((SWT.DEFAULT + AA_WHOLE_NUMBER) << TEXT_AA_SHIFT)
| ((SWT.DEFAULT + AA_WHOLE_NUMBER) << AA_SHIFT)
| ((SWT.DEFAULT + INTERPOLATION_WHOLE_NUMBER) << INTERPOLATION_SHIFT);
}
private final LazyState appliedState = new LazyState();
private final State currentState = new State();
private boolean elementsNeedUpdate;
private final GC gc;
private boolean sharedClipping;
private final List<State> stack = new ArrayList<>();
private int stackPointer = 0;
Transform transform;
private int translateX = 0;
private int translateY = 0;
/**
* Constructs a new SWTGraphics that draws to the Canvas using the given GC.
*
* @param gc the GC
*/
public SWTGraphics(GC gc) {
this.gc = gc;
init();
}
/**
* If the background color has changed, this change will be pushed to the GC.
* Also calls {@link #checkGC()}.
*/
protected final void checkFill() {
if (!currentState.bgColor.equals(appliedState.bgColor) && currentState.bgPattern == null) {
appliedState.bgColor = currentState.bgColor;
gc.setBackground(appliedState.bgColor);
}
checkGC();
}
/**
* If the rendering hints or the clip region has changed, these changes will be
* pushed to the GC. Rendering hints include anti-alias, xor, join, cap, line
* style, fill rule, interpolation, and other settings.
*/
protected final void checkGC() {
if (appliedState.relativeClip != currentState.relativeClip) {
appliedState.relativeClip = currentState.relativeClip;
currentState.relativeClip.setOn(gc, translateX, translateY);
}
if (appliedState.graphicHints != currentState.graphicHints) {
reconcileHints(gc, appliedState.graphicHints, currentState.graphicHints);
appliedState.graphicHints = currentState.graphicHints;
}
}
/**
* If the line width, line style, foreground or background colors have changed,
* these changes will be pushed to the GC. Also calls {@link #checkGC()}.
*/
protected final void checkPaint() {
checkGC();
if (!currentState.fgColor.equals(appliedState.fgColor) && currentState.fgPattern == null) {
gc.setForeground(appliedState.fgColor = currentState.fgColor);
}
LineAttributes lineAttributes = currentState.lineAttributes;
if (!appliedState.lineAttributes.equals(lineAttributes)) {
if (getAdvanced()) {
gc.setLineAttributes(clone(lineAttributes)); // Clone lineAttributes because on Windows hi-dpi the line
// width may be increased
} else {
gc.setLineWidth((int) lineAttributes.width);
gc.setLineCap(lineAttributes.cap);
gc.setLineJoin(lineAttributes.join);
gc.setLineStyle(lineAttributes.style);
if (lineAttributes.dash != null) {
gc.setLineDash(convertFloatArrayToInt(lineAttributes.dash));
}
}
appliedState.lineAttributes = clone(lineAttributes);
}
if (!currentState.bgColor.equals(appliedState.bgColor) && currentState.bgPattern == null) {
appliedState.bgColor = currentState.bgColor;
gc.setBackground(appliedState.bgColor);
}
}
/**
* @since 3.1
*/
private void checkSharedClipping() {
if (sharedClipping) {
sharedClipping = false;
boolean previouslyApplied = (appliedState == currentState.relativeClip);
// Fix: currentState.relativeClip can be null and lead to NPE
if (currentState.relativeClip != null) {
currentState.relativeClip = currentState.relativeClip.getCopy();
}
if (previouslyApplied) {
appliedState.relativeClip = currentState.relativeClip;
}
}
}
/**
* If the font has changed, this change will be pushed to the GC. Also calls
* {@link #checkPaint()} and {@link #checkFill()}.
*/
protected final void checkText() {
checkPaint();
if (!appliedState.font.equals(currentState.font)) {
appliedState.font = currentState.font;
gc.setFont(appliedState.font);
}
}
/**
* @see Graphics#clipRect(Rectangle)
*/
@Override
public void clipRect(Rectangle rect) {
if (currentState.relativeClip == null) {
throw new IllegalStateException("The current clipping area does not " + //$NON-NLS-1$
"support intersection."); //$NON-NLS-1$
}
checkSharedClipping();
currentState.relativeClip.intersect(rect.x, rect.y, rect.right(), rect.bottom());
appliedState.relativeClip = null;
}
/**
* @see Graphics#dispose()
*/
@Override
public void dispose() {
while (stackPointer > 0) {
popState();
}
if (transform != null) {
transform.dispose();
}
}
/**
* @see Graphics#drawArc(int, int, int, int, int, int)
*/
@Override
public void drawArc(int x, int y, int width, int height, int offset, int length) {
checkPaint();
gc.drawArc(x + translateX, y + translateY, width, height, offset, length);
}
/**
* @see Graphics#drawFocus(int, int, int, int)
*/
@Override
public void drawFocus(int x, int y, int w, int h) {
checkPaint();
gc.drawFocus(x + translateX, y + translateY, w + 1, h + 1);
}
/**
* @see Graphics#drawImage(Image, int, int)
*/
@Override
public void drawImage(Image srcImage, int x, int y) {
checkGC();
gc.drawImage(srcImage, x + translateX, y + translateY);
}
/**
* @see Graphics#drawImage(Image, int, int, int, int, int, int, int, int)
*/
@Override
public void drawImage(Image srcImage, int x1, int y1, int w1, int h1, int x2, int y2, int w2, int h2) {
checkGC();
gc.drawImage(srcImage, x1, y1, w1, h1, x2 + translateX, y2 + translateY, w2, h2);
}
/**
* @see Graphics#drawLine(int, int, int, int)
*/
@Override
public void drawLine(int x1, int y1, int x2, int y2) {
checkPaint();
gc.drawLine(x1 + translateX, y1 + translateY, x2 + translateX, y2 + translateY);
}
/**
* @see Graphics#drawOval(int, int, int, int)
*/
@Override
public void drawOval(int x, int y, int width, int height) {
checkPaint();
gc.drawOval(x + translateX, y + translateY, width, height);
}
/**
* This method requires advanced graphics support. A check should be made to
* ensure advanced graphics is supported in the user's environment before
* calling this method. See {@link GC#getAdvanced()}.
*
* @see Graphics#drawPath(Path)
*/
@Override
public void drawPath(Path path) {
checkPaint();
initTransform(false);
gc.drawPath(path);
}
/**
* @see Graphics#drawPoint(int, int)
*/
@Override
public void drawPoint(int x, int y) {
checkPaint();
gc.drawPoint(x + translateX, y + translateY);
}
/**
* @see Graphics#drawPolygon(int[])
*/
@Override
public void drawPolygon(int[] points) {
checkPaint();
try {
translatePointArray(points, translateX, translateY);
gc.drawPolygon(points);
} finally {
translatePointArray(points, -translateX, -translateY);
}
}
/**
* @see Graphics#drawPolygon(PointList)
*/
@Override
public void drawPolygon(PointList points) {
drawPolygon(points.toIntArray());
}
/**
* @see Graphics#drawPolyline(int[])
*/
@Override
public void drawPolyline(int[] points) {
checkPaint();
try {
translatePointArray(points, translateX, translateY);
gc.drawPolyline(points);
} finally {
translatePointArray(points, -translateX, -translateY);
}
}
/**
* @see Graphics#drawPolyline(PointList)
*/
@Override
public void drawPolyline(PointList points) {
drawPolyline(points.toIntArray());
}
/**
* @see Graphics#drawRectangle(int, int, int, int)
*/
@Override
public void drawRectangle(int x, int y, int width, int height) {
checkPaint();
gc.drawRectangle(x + translateX, y + translateY, width, height);
}
/**
* @see Graphics#drawRoundRectangle(Rectangle, int, int)
*/
@Override
public void drawRoundRectangle(Rectangle r, int arcWidth, int arcHeight) {
checkPaint();
gc.drawRoundRectangle(r.x + translateX, r.y + translateY, r.width, r.height, arcWidth, arcHeight);
}
/**
* @see Graphics#drawString(String, int, int)
*/
@Override
public void drawString(String s, int x, int y) {
checkText();
gc.drawString(s, x + translateX, y + translateY, true);
}
/**
* @see Graphics#drawText(String, int, int)
*/
@Override
public void drawText(String s, int x, int y) {
checkText();
gc.drawText(s, x + translateX, y + translateY, true);
}
/**
* @see Graphics#drawTextLayout(TextLayout, int, int, int, int, Color, Color)
*/
@Override
public void drawTextLayout(TextLayout layout, int x, int y, int selectionStart, int selectionEnd,
Color selectionForeground, Color selectionBackground) {
// $TODO probably just call checkPaint since Font and BG color don't
// apply
checkText();
layout.draw(gc, x + translateX, y + translateY, selectionStart, selectionEnd, selectionForeground,
selectionBackground);
}
/**
* @see Graphics#fillArc(int, int, int, int, int, int)
*/
@Override
public void fillArc(int x, int y, int width, int height, int offset, int length) {
checkFill();
gc.fillArc(x + translateX, y + translateY, width, height, offset, length);
}
/**
* @see Graphics#fillGradient(int, int, int, int, boolean)
*/
@Override
public void fillGradient(int x, int y, int w, int h, boolean vertical) {
checkPaint();
gc.fillGradientRectangle(x + translateX, y + translateY, w, h, vertical);
}
/**
* @see Graphics#fillOval(int, int, int, int)
*/
@Override
public void fillOval(int x, int y, int width, int height) {
checkFill();
gc.fillOval(x + translateX, y + translateY, width, height);
}
/**
* This method requires advanced graphics support. A check should be made to
* ensure advanced graphics is supported in the user's environment before
* calling this method. See {@link GC#getAdvanced()}.
*
* @see Graphics#fillPath(Path)
*/
@Override
public void fillPath(Path path) {
checkFill();
initTransform(false);
gc.fillPath(path);
}
/**
* @see Graphics#fillPolygon(int[])
*/
@Override
public void fillPolygon(int[] points) {
checkFill();
try {
translatePointArray(points, translateX, translateY);
gc.fillPolygon(points);
} finally {
translatePointArray(points, -translateX, -translateY);
}
}
/**
* @see Graphics#fillPolygon(PointList)
*/
@Override
public void fillPolygon(PointList points) {
fillPolygon(points.toIntArray());
}
/**
* @see Graphics#fillRectangle(int, int, int, int)
*/
@Override
public void fillRectangle(int x, int y, int width, int height) {
checkFill();
gc.fillRectangle(x + translateX, y + translateY, width, height);
}
/**
* @see Graphics#fillRoundRectangle(Rectangle, int, int)
*/
@Override
public void fillRoundRectangle(Rectangle r, int arcWidth, int arcHeight) {
checkFill();
gc.fillRoundRectangle(r.x + translateX, r.y + translateY, r.width, r.height, arcWidth, arcHeight);
}
/**
* @see Graphics#fillString(String, int, int)
*/
@Override
public void fillString(String s, int x, int y) {
checkText();
gc.drawString(s, x + translateX, y + translateY, false);
}
/**
* @see Graphics#fillText(String, int, int)
*/
@Override
public void fillText(String s, int x, int y) {
checkText();
gc.drawText(s, x + translateX, y + translateY, false);
}
/**
* @see Graphics#getAlpha()
*/
@Override
public int getAlpha() {
return currentState.alpha;
}
/**
* @see Graphics#getAntialias()
*/
@Override
public int getAntialias() {
return ((currentState.graphicHints & AA_MASK) >> AA_SHIFT) - AA_WHOLE_NUMBER;
}
@Override
public boolean getAdvanced() {
return (currentState.graphicHints & ADVANCED_GRAPHICS_MASK) != 0;
}
/**
* @see Graphics#getBackgroundColor()
*/
@Override
public Color getBackgroundColor() {
return currentState.bgColor;
}
/**
* @see Graphics#getClip(Rectangle)
*/
@Override
public Rectangle getClip(Rectangle rect) {
if (currentState.relativeClip != null) {
currentState.relativeClip.getBoundingBox(rect);
return rect;
}
throw new IllegalStateException("Clipping can no longer be queried due to transformations"); //$NON-NLS-1$
}
/**
* @see Graphics#getFillRule()
*/
@Override
public int getFillRule() {
return ((currentState.graphicHints & FILL_RULE_MASK) >> FILL_RULE_SHIFT) - FILL_RULE_WHOLE_NUMBER;
}
/**
* @see Graphics#getFont()
*/
@Override
public Font getFont() {
return currentState.font;
}
/**
* @see Graphics#getFontMetrics()
*/
@Override
public FontMetrics getFontMetrics() {
checkText();
return gc.getFontMetrics();
}
/**
* @see Graphics#getForegroundColor()
*/
@Override
public Color getForegroundColor() {
return currentState.fgColor;
}
/**
* @see Graphics#getInterpolation()
*/
@Override
public int getInterpolation() {
return ((currentState.graphicHints & INTERPOLATION_MASK) >> INTERPOLATION_SHIFT) - INTERPOLATION_WHOLE_NUMBER;
}
/**
* @since 3.5
*/
public void getLineAttributes(LineAttributes lineAttributes) {
copyLineAttributes(lineAttributes, currentState.lineAttributes);
}
/**
* @see Graphics#getLineCap()
*/
@Override
public int getLineCap() {
return currentState.lineAttributes.cap;
}
/**
* @see Graphics#getLineJoin()
*/
@Override
public int getLineJoin() {
return currentState.lineAttributes.join;
}
/**
* @see Graphics#getLineStyle()
*/
@Override
public int getLineStyle() {
return currentState.lineAttributes.style;
}
/**
* @see Graphics#getLineWidth()
*/
@Override
public int getLineWidth() {
return (int) currentState.lineAttributes.width;
}
@Override
public float getLineWidthFloat() {
return currentState.lineAttributes.width;
}
@Override
public float getLineMiterLimit() {
return currentState.lineAttributes.miterLimit;
}
/**
* @since 3.5
*/
public float[] getLineDash() {
return currentState.lineAttributes.dash.clone();
}
/**
* @since 3.5
*/
public float getLineDashOffset() {
return currentState.lineAttributes.dashOffset;
}
/**
* @see Graphics#getTextAntialias()
*/
@Override
public int getTextAntialias() {
return ((currentState.graphicHints & TEXT_AA_MASK) >> TEXT_AA_SHIFT) - AA_WHOLE_NUMBER;
}
/**
* @see Graphics#getXORMode()
*/
@Override
public boolean getXORMode() {
return (currentState.graphicHints & XOR_MASK) != 0;
}
/**
* Called by constructor, initializes all State information for currentState
*/
protected void init() {
currentState.bgColor = appliedState.bgColor = gc.getBackground();
currentState.fgColor = appliedState.fgColor = gc.getForeground();
currentState.font = appliedState.font = gc.getFont();
currentState.lineAttributes = gc.getLineAttributes();
appliedState.lineAttributes = clone(currentState.lineAttributes);
currentState.graphicHints |= gc.getLineStyle();
currentState.graphicHints |= gc.getAdvanced() ? ADVANCED_GRAPHICS_MASK : 0;
currentState.graphicHints |= gc.getXORMode() ? XOR_MASK : 0;
appliedState.graphicHints = currentState.graphicHints;
currentState.relativeClip = new RectangleClipping(gc.getClipping());
currentState.alpha = gc.getAlpha();
}
private void initTransform(boolean force) {
if (!force && translateX == 0 && translateY == 0) {
return;
}
if (transform == null) {
transform = new Transform(Display.getCurrent());
elementsNeedUpdate = true;
transform.translate(translateX, translateY);
translateX = 0;
translateY = 0;
gc.setTransform(transform);
currentState.graphicHints |= ADVANCED_GRAPHICS_MASK;
}
}
/**
* @see Graphics#popState()
*/
@Override
public void popState() {
stackPointer--;
restoreState(stack.get(stackPointer));
}
/**
* @see Graphics#pushState()
*/
@Override
public void pushState() {
if (currentState.relativeClip == null) {
throw new IllegalStateException("The clipping has been modified in" + //$NON-NLS-1$
"a way that cannot be saved and restored."); //$NON-NLS-1$
}
try {
State s;
currentState.dx = translateX;
currentState.dy = translateY;
if (elementsNeedUpdate) {
elementsNeedUpdate = false;
currentState.affineMatrix = new float[6];
transform.getElements(currentState.affineMatrix);
}
if (stack.size() > stackPointer) {
s = stack.get(stackPointer);
s.copyFrom(currentState);
} else {
stack.add(currentState.clone());
}
sharedClipping = true;
stackPointer++;
} catch (CloneNotSupportedException e) {
throw new RuntimeException(e);
}
}
private static void reconcileHints(GC gc, int applied, int hints) {
int changes = hints ^ applied;
if ((changes & XOR_MASK) != 0) {
gc.setXORMode((hints & XOR_MASK) != 0);
}
// Check to see if there is anything remaining
changes &= ~XOR_MASK;
if (changes != 0) {
if ((changes & INTERPOLATION_MASK) != 0) {
gc.setInterpolation(((hints & INTERPOLATION_MASK) >> INTERPOLATION_SHIFT) - INTERPOLATION_WHOLE_NUMBER);
}
if ((changes & FILL_RULE_MASK) != 0) {
gc.setFillRule(((hints & FILL_RULE_MASK) >> FILL_RULE_SHIFT) - FILL_RULE_WHOLE_NUMBER);
}
if ((changes & AA_MASK) != 0) {
gc.setAntialias(((hints & AA_MASK) >> AA_SHIFT) - AA_WHOLE_NUMBER);
}
if ((changes & TEXT_AA_MASK) != 0) {
gc.setTextAntialias(((hints & TEXT_AA_MASK) >> TEXT_AA_SHIFT) - AA_WHOLE_NUMBER);
}
// If advanced was flagged, but none of the conditions which trigger
// advanced
// actually got applied, force advanced graphics on.
if ((changes & ADVANCED_GRAPHICS_MASK) != 0) {
if ((hints & ADVANCED_GRAPHICS_MASK) != 0 && !gc.getAdvanced()) {
gc.setAdvanced(true);
}
}
}
}
/**
* @see Graphics#restoreState()
*/
@Override
public void restoreState() {
restoreState(stack.get(stackPointer - 1));
}
/**
* Sets all State information to that of the given State, called by
* restoreState()
*
* @param s the State
*/
protected void restoreState(State s) {
/*
* We must set the transformation matrix first since it affects things like
* clipping regions and patterns.
*/
setAffineMatrix(s.affineMatrix);
currentState.relativeClip = s.relativeClip;
sharedClipping = true;
// If the GC is currently advanced, but it was not when pushed, revert
if (gc.getAdvanced() && (s.graphicHints & ADVANCED_GRAPHICS_MASK) == 0) {
// Set applied clip to null to force a re-setting of the clipping.
appliedState.relativeClip = null;
gc.setAdvanced(false);
appliedState.graphicHints &= ~ADVANCED_HINTS_MASK;
appliedState.graphicHints |= ADVANCED_HINTS_DEFAULTS;
}
setBackgroundColor(s.bgColor);
setBackgroundPattern(s.bgPattern);
setForegroundColor(s.fgColor);
setForegroundPattern(s.fgPattern);
setAlpha(s.alpha);
setLineAttributes(s.lineAttributes);
setFont(s.font);
// This method must come last because above methods will incorrectly set
// advanced state
setGraphicHints(s.graphicHints);
translateX = currentState.dx = s.dx;
translateY = currentState.dy = s.dy;
}
/**
* This method requires advanced graphics support. A check should be made to
* ensure advanced graphics is supported in the user's environment before
* calling this method. See {@link GC#getAdvanced()}.
*
* @see Graphics#rotate(float)
*/
@Override
public void rotate(float degrees) {
// Flush clipping, patter, etc., before applying transform
checkGC();
initTransform(true);