forked from genail/pulpcore
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCoreGraphics.java
More file actions
2061 lines (1793 loc) · 67.5 KB
/
CoreGraphics.java
File metadata and controls
2061 lines (1793 loc) · 67.5 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
/*
Copyright (c) 2008, Interactive Pulp, LLC
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
* Neither the name of Interactive Pulp, LLC nor the names of its
contributors may be used to endorse or promote products derived from
this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.
*/
package pulpcore.image;
import java.util.EmptyStackException;
import pulpcore.Build;
import pulpcore.math.CoreMath;
import pulpcore.math.Rect;
import pulpcore.math.Transform;
/**
Graphics rendering routines onto a CoreImage surface.
<p>
The default blend mode is {@link BlendMode#SrcOver()}.
<p>
The clip is in view-space - not affected by the Transform.
*/
public class CoreGraphics {
// Line drawing options
private static final boolean CENTER_PIXEL = true;
private static final boolean SWAP_POINTS = true;
/**
Nearest neighbor interpolation method (used for image scaling).
Also known as "pixel scaling".
*/
public static final int INTERPOLATION_NEAREST_NEIGHBOR = 0;
/**
Bilinear interpolation method (used for image scaling).
*/
public static final int INTERPOLATION_BILINEAR = 1;
/**
Specifies that the left edge of the image should be clamped (not antialiased).
@see #setEdgeClamp(int)
*/
public static final int EDGE_CLAMP_LEFT = 1;
/**
Specifies that the right edge of the image should be clamped (not antialiased).
@see #setEdgeClamp(int)
*/
public static final int EDGE_CLAMP_RIGHT = 2;
/**
Specifies that the bottom edge of the image should be clamped (not antialiased).
@see #setEdgeClamp(int)
*/
public static final int EDGE_CLAMP_BOTTOM = 4;
/**
Specifies that the top edge of the image should be clamped (not antialiased).
@see #setEdgeClamp(int)
*/
public static final int EDGE_CLAMP_TOP = 8;
/**
Specifies that all edges of the image should be clamped (not antialiased).
@see #setEdgeClamp(int)
*/
public static final int EDGE_CLAMP_ALL = EDGE_CLAMP_LEFT | EDGE_CLAMP_RIGHT |
EDGE_CLAMP_BOTTOM | EDGE_CLAMP_TOP;
/**
Specifies that all edges of the image should be antialiased (not clamped).
@see #setEdgeClamp(int)
*/
public static final int EDGE_CLAMP_NONE = 0;
// Clipping for drawLine
private static final int CLIP_CODE_LEFT = 8;
private static final int CLIP_CODE_RIGHT = 4;
private static final int CLIP_CODE_ABOVE = 2;
private static final int CLIP_CODE_BELOW = 1;
// Surface data
private final int surfaceWidth;
private final int surfaceHeight;
private final int[] surfaceData;
private final boolean surfaceHasAlpha;
// The clip rectangle
private int clipX;
private int clipY;
private int clipWidth;
private int clipHeight;
// A clipped object
private int objectX;
private int objectY;
private int objectWidth;
private int objectHeight;
// Scan line
private int scanY;
private int scanStartX;
private int scanEndX;
private Composite composite;
private BlendMode blendMode;
/** If true, bilinear filtering is used when scaling images. */
private boolean bilinear;
/** Edge clamp mask */
private int edgeClamp;
/** The current alpha value. 0 is fully tranaparent, 255 is fully opaque. */
private int alpha;
/** The current color (ARGB). */
private int srcColor;
/** The current color blended with the current alpha. */
private int srcColorBlended;
private boolean isSrcColorTransparent;
private CoreFont font;
private final Transform transform = new Transform();
private Transform[] transformStack = new Transform[16];
private int transformStackSize = 0;
/* package-private */ CoreGraphics(CoreImage surface) {
surfaceWidth = surface.getWidth();
surfaceHeight = surface.getHeight();
surfaceData = surface.getData();
surfaceHasAlpha = !surface.isOpaque();
for (int i = 0; i < transformStack.length; i++) {
transformStack[i] = new Transform();
}
reset();
}
/**
Gets the underlying data array of the surface image.
@deprecated This will be moved in 0.12.
*/
public int[] getSurfaceData() {
return surfaceData;
}
public int getSurfaceWidth() {
return surfaceWidth;
}
public int getSurfaceHeight() {
return surfaceHeight;
}
//
// Rendering options
//
/**
Resets the rendering attributes for this CoreGraphics object to the
default values:
<ul>
<li>No clip</li>
<li>Identity transform (and the transform stack is cleared)</li>
<li>color = BLACK</li>
<li>alpha = 255</li>
<li>blendMode = BlendMode.SrcOver()</li>
<li>interpolation = INTERPOLATION_BILINEAR</li>
<li>edgeClamp = EDGE_CLAMP_NONE</li>
<li>font = null</li>
</ul>
*/
public void reset() {
removeClip();
setAlpha(0xff);
setColor(Colors.BLACK);
setBlendMode(BlendMode.SrcOver());
bilinear = true;
edgeClamp = EDGE_CLAMP_NONE;
font = null;
transformStackSize = 0;
transform.clear();
}
public void setBlendMode(BlendMode blendMode) {
if (this.blendMode != blendMode) {
this.blendMode = blendMode;
if (surfaceHasAlpha) {
this.composite = this.blendMode.alpha;
}
else {
this.composite = this.blendMode.opaque;
}
}
}
public BlendMode getBlendMode() {
return blendMode;
}
public void setInterpolation(int interpolation) {
this.bilinear = (interpolation == INTERPOLATION_BILINEAR);
}
public int getInterpolation() {
return bilinear ? INTERPOLATION_BILINEAR : INTERPOLATION_NEAREST_NEIGHBOR;
}
/**
Sets the edge mode for bilinear interpolated scaled images. The default is
{@link #EDGE_CLAMP_NONE}. Valid values are a bitmask combination of
{@link #EDGE_CLAMP_LEFT}, {@link #EDGE_CLAMP_RIGHT}, {@link #EDGE_CLAMP_BOTTOM},
{@link #EDGE_CLAMP_TOP}. A clamp edge appears "hard", and an unclamped edge appears "soft".
@param edgeClamp A bitmask defining how the edges of an image are rendered.
*/
public void setEdgeClamp(int edgeClamp) {
this.edgeClamp = edgeClamp;
}
/**
Gets the edge clamp bitmask.
@see #setEdgeClamp(int)
*/
public int getEdgeClamp() {
return edgeClamp;
}
public void setFont(CoreFont font) {
this.font = font;
}
public CoreFont getFont() {
if (font == null) {
font = CoreFont.getSystemFont();
}
return font;
}
//
// Transforms
//
/**
Adds (pushes) a copy of the current transform to the top of the transform stack.
*/
public void pushTransform() {
if (transformStackSize == transformStack.length) {
// Double the size of the stack
Transform[] newTransformStack = new Transform[transformStack.length * 2];
System.arraycopy(transformStack, 0, newTransformStack, 0, transformStack.length);
for (int i = transformStack.length; i < newTransformStack.length; i++) {
newTransformStack[i] = new Transform();
}
transformStack = newTransformStack;
}
transformStack[transformStackSize].set(transform);
transformStackSize++;
}
/**
Removes (pops) the transform at the top of the transform stack and sets the
current transform to that popped transform.
@throws EmptyStackException if the stack is empty
*/
public void popTransform() throws EmptyStackException {
if (transformStackSize == 0) {
throw new EmptyStackException();
}
transformStackSize--;
transform.set(transformStack[transformStackSize]);
}
/**
Returns the current transform. The returned instance will always be
the current transform of this graphics context.
*/
public Transform getTransform() {
return transform;
}
/**
Sets the current transform to a copy of the specified transform. If
the specified transform is null, the current transform is cleared, i.e.,
set to the identity matrix.
*/
public void setTransform(Transform newTransform) {
transform.set(newTransform);
}
/**
Sets the current transform to the identiy matrix.
*/
public void clearTransform() {
transform.clear();
}
//
// Clipping
//
public void removeClip() {
clipX = 0;
clipY = 0;
clipWidth = surfaceWidth;
clipHeight = surfaceHeight;
}
/**
The clip is not affected by the transform.
*/
public void setClip(Rect r) {
setClip(r.x, r.y, r.width, r.height);
}
/**
The clip is not affected by the transform.
*/
public void setClip(int x, int y, int w, int h) {
removeClip();
clipRect(x, y, w, h);
}
/**
The clip is not affected by the transform.
*/
public void clipRect(Rect r) {
clipRect(r.x, r.y, r.width, r.height);
}
/**
The clip is not affected by the transform.
*/
public void clipRect(int x, int y, int w, int h) {
clipObject(x, y, w, h);
clipX = objectX;
clipY = objectY;
clipWidth = objectWidth;
clipHeight = objectHeight;
}
private void clipObject(int x, int y, int w, int h) {
if (x < clipX) {
w -= clipX - x;
x = clipX;
}
if (y < clipY) {
h -= clipY - y;
y = clipY;
}
if (x + w > clipX + clipWidth) {
w = clipX + clipWidth - x;
}
if (y + h > clipY + clipHeight) {
h = clipY + clipHeight - y;
}
objectX = x;
objectY = y;
objectWidth = w;
objectHeight = h;
}
public int getClipX() {
return clipX;
}
public int getClipY() {
return clipY;
}
public int getClipWidth() {
return clipWidth;
}
public int getClipHeight() {
return clipHeight;
}
/**
Gets the current clip as a newly allocated Rect.
*/
public Rect getClip() {
Rect clip = new Rect();
getClip(clip);
return clip;
}
/**
Copies the current clip into the specified Rect.
*/
public void getClip(Rect rect) {
rect.setBounds(clipX, clipY, clipWidth, clipHeight);
}
//
// ARGB color
//
public void setAlpha(int alpha) {
if (alpha <= 0) {
this.alpha = 0;
}
else if (alpha >= 0xff) {
this.alpha = 0xff;
}
else {
this.alpha = alpha;
}
// Update srcColorBlended, isSrcColorTransparent
setColor(srcColor);
}
public int getAlpha() {
return alpha;
}
/**
Sets the current color. The color is used for drawing rectangles and lines.
@see Colors
*/
public void setColor(int argbColor) {
srcColor = argbColor;
int newAlpha = ((srcColor >>> 24) * alpha + 127) / 255;
srcColorBlended = Colors.rgba(srcColor, newAlpha);
srcColorBlended = Colors.premultiply(srcColorBlended);
isSrcColorTransparent = (srcColorBlended >>> 24) == 0;
}
/**
Returns the current color in ARGB format.
@see Colors
*/
public int getColor() {
return srcColor;
}
//
// Primitive rendering
//
/**
Draws a line using the current color.
*/
public void drawLine(int x1, int y1, int x2, int y2) {
x1 = CoreMath.toFixed(x1);
y1 = CoreMath.toFixed(y1);
x2 = CoreMath.toFixed(x2);
y2 = CoreMath.toFixed(y2);
int dx = x2 - x1;
int dy = y2 - y1;
if (dx == 0 && dy == 0) {
// Do nothing
}
else if ((dx == 0 || dy == 0) && (transform.getType() == Transform.TYPE_IDENTITY ||
transform.getType() == Transform.TYPE_TRANSLATE))
{
// Horizontal and vertical lines
int nx1 = transform.transformX(x1, y1);
int ny1 = transform.transformY(x1, y1);
int nx2 = transform.transformX(x2, y2);
int ny2 = transform.transformY(x2, y2);
pushTransform();
transform.clear();
transform.translate(Math.min(nx1, nx2), Math.min(ny1, ny2));
internalFillRect(CoreMath.abs(dx) + CoreMath.ONE, CoreMath.abs(dy) + CoreMath.ONE);
popTransform();
}
else {
internalDrawLine(x1, y1, x2, y2, true);
}
}
/**
Draws a line using the current color.
*/
public void drawLine(double x1, double y1, double x2, double y2) {
drawLineFixedPoint(
CoreMath.toFixed(x1),
CoreMath.toFixed(y1),
CoreMath.toFixed(x2),
CoreMath.toFixed(y2));
}
/**
Draws a line (at fixed-point coordinates) using the current color.
*/
public void drawLineFixedPoint(int x1, int y1, int x2, int y2) {
internalDrawLine(x1, y1, x2, y2, false);
}
/**
Draws a rectangle using the current color. This method draws
rectangles at integer coordinates.
<p>
Note, this method is different from
java.awt.Graphics.drawRect() which draws a rectangle with
a width of (w+1) and a height of (h+1).
*/
public void drawRect(int x, int y, int w, int h) {
fillRect(x, y, w, 1);
fillRect(x, y + h - 1, w, 1);
fillRect(x, y, 1, h);
fillRect(x + w - 1, y, 1, h);
}
/**
Fills the entire surface with the current color.
Same as calling {@code fillRect(0, 0, surfaceWidth, surfaceHeight)} with
the identity transform.
*/
public void fill() {
int type = transform.getType();
if (type != Transform.TYPE_IDENTITY) {
pushTransform();
transform.clear();
}
internalFillRect(CoreMath.toFixed(surfaceWidth), CoreMath.toFixed(surfaceHeight));
if (type != Transform.TYPE_IDENTITY) {
popTransform();
}
}
/**
Fills the area defined by the clip with the background color of the current drawing
surface. For opaque surfaces, the background color is black. For surfaces with alpha,
the background color is transparent. The current blend mode is ignored - the area
if filled using the Porter-Duff Source rule.
*/
public void clear() {
int backgroundColor = surfaceHasAlpha ? Colors.TRANSPARENT : Colors.BLACK;
int offset = clipX + clipY * surfaceWidth;
for (int y = 0; y < clipHeight; y++) {
for (int x = 0; x < clipWidth; x++) {
surfaceData[offset++] = backgroundColor;
}
offset += surfaceWidth - clipWidth;
}
}
/**
Fills a rectangle with the current color.
*/
public void fillRect(int x, int y, int w, int h) {
fillRectFixedPoint(CoreMath.toFixed(x), CoreMath.toFixed(y),
CoreMath.toFixed(w), CoreMath.toFixed(h));
}
/**
Fills a rectangle with the current color.
*/
public void fillRect(double x, double y, double w, double h) {
fillRectFixedPoint(CoreMath.toFixed(x), CoreMath.toFixed(y),
CoreMath.toFixed(w), CoreMath.toFixed(h));
}
/**
Fills a rectangle (at fixed-point coordinates) with the current color.
*/
public void fillRectFixedPoint(int fx, int fy, int fw, int fh) {
if (isSrcColorTransparent || fw == 0 || fh == 0) {
return;
}
pushTransform();
transform.translate(fx, fy);
int type = transform.getType();
if ((type & Transform.TYPE_ROTATE) != 0) {
internalFillRotatedRect(fw, fh);
}
else {
int x = transform.getTranslateX();
int y = transform.getTranslateY();
int w = CoreMath.mul(transform.getScaleX(), fw);
int h = CoreMath.mul(transform.getScaleY(), fh);
if (CoreMath.fracPart(x) != 0 ||
CoreMath.fracPart(y) != 0 ||
CoreMath.fracPart(w) != 0 ||
CoreMath.fracPart(h) != 0)
{
internalFillRectFixedPoint(fw, fh);
}
else {
internalFillRect(fw, fh);
}
}
popTransform();
}
//
// String rendering
//
public void drawString(String str) {
if (str == null || str.length() == 0 || alpha == 0) {
return;
}
if (font == null) {
font = CoreFont.getSystemFont();
}
pushTransform();
int nextIndex = font.getCharIndex(str.charAt(0));
for (int i = 0; i < str.length(); i++) {
int index = nextIndex;
int pos = font.charPositions[index];
int charWidth = font.charPositions[index+1] - pos;
drawImage(font.getImage(), pos, 0, charWidth, font.getHeight());
if (i < str.length() - 1) {
nextIndex = font.getCharIndex(str.charAt(i + 1));
int dx = charWidth + font.getKerning(index, nextIndex);
transform.translate(CoreMath.toFixed(dx), 0);
}
}
popTransform();
}
/**
Internal method used to clip the TextField.
The problem is the clip is limited to View Space, but the
TextField needs clipping to occur in Local Space.
This method can be removed if, in future versions,
CoreGraphics objects can be clipped in Local Space.
*/
public void drawChar(char ch, int maxWidth) {
if (alpha == 0 || maxWidth <= 0) {
return;
}
if (font == null) {
font = CoreFont.getSystemFont();
}
int index = font.getCharIndex(ch);
int pos = font.charPositions[index];
int charWidth = font.charPositions[index+1] - pos;
int w = Math.min(charWidth, maxWidth);
drawImage(font.getImage(), pos, 0, w, font.getHeight());
}
public void drawString(String str, int x, int y) {
if (str == null || str.length() == 0 || alpha == 0) {
return;
}
pushTransform();
transform.translate(CoreMath.toFixed(x), CoreMath.toFixed(y));
drawString(str);
popTransform();
}
public void drawScaledString(String str, int x, int y, int w, int h) {
if (str == null || str.length() == 0 || w == 0 || h == 0 || alpha == 0) {
return;
}
if (font == null) {
font = CoreFont.getSystemFont();
}
int originalWidth = font.getStringWidth(str);
int originalHeight = font.getHeight();
if (originalWidth == 0 || originalHeight == 0) {
return;
}
int scaleX = CoreMath.toFixed(w) / originalWidth;
int scaleY = CoreMath.toFixed(h) / originalHeight;
pushTransform();
transform.translate(CoreMath.toFixed(x), CoreMath.toFixed(y));
transform.scale(scaleX, scaleY);
drawString(str);
popTransform();
}
public void drawRotatedString(String str, int x, int y, int w, int h, int angle) {
drawRotatedString(str, x, y, w, h, CoreMath.cos(angle), CoreMath.sin(angle));
}
public void drawRotatedString(String str, int x, int y, int w, int h,
int cosAngle, int sinAngle)
{
if (str == null || str.length() == 0 || alpha == 0) {
return;
}
if (font == null) {
font = CoreFont.getSystemFont();
}
int originalWidth = font.getStringWidth(str);
int originalHeight = font.getHeight();
if (originalWidth == 0 || originalHeight == 0) {
return;
}
int fw = CoreMath.toFixed(w);
int fh = CoreMath.toFixed(h);
int fSrcWidth = CoreMath.toFixed(originalWidth);
int fSrcHeight = CoreMath.toFixed(originalHeight);
int scaleX = fw / originalWidth;
int scaleY = fh / originalHeight;
pushTransform();
transform.translate(CoreMath.toFixed(x), CoreMath.toFixed(y));
transform.scale(scaleX, scaleY);
transform.translate(fSrcWidth / 2, fSrcHeight / 2);
transform.rotate(cosAngle, sinAngle);
transform.translate(-fSrcWidth / 2, -fSrcHeight / 2);
drawString(str);
popTransform();
}
//
// Image rendering
//
/**
Checks if the image arguments are valid. The image must be non-null and
the source bounds must be within the image bounds. The width and height
of the source bounds can be zero, in which case nothing is drawn.
If the arguments are not valid, this method throws an IllegalArgumentException.
*/
private void validateImage(CoreImage image, int srcX, int srcY, int srcWidth, int srcHeight) {
if (image == null) {
throw new IllegalArgumentException("CoreImage is null");
}
else if (srcX < 0 || srcY < 0 ||
srcWidth < 0 || srcHeight < 0 ||
srcX + srcWidth > image.getWidth() ||
srcY + srcHeight > image.getHeight())
{
throw new IllegalArgumentException("CoreImage source bounds outside of image bounds");
}
}
public void drawImage(CoreImage image) {
if (image != null) {
drawImage(image, 0, 0, image.getWidth(), image.getHeight());
}
}
public void drawImage(CoreImage image,
int srcX, int srcY, int srcWidth, int srcHeight)
{
if (Build.DEBUG) {
validateImage(image, srcX, srcY, srcWidth, srcHeight);
}
if (alpha == 0 || srcWidth == 0 || srcHeight == 0) {
return;
}
int type = transform.getType();
if (type == Transform.TYPE_IDENTITY || type == Transform.TYPE_TRANSLATE) {
int x = transform.getTranslateX();
int y = transform.getTranslateY();
if (CoreMath.fracPart(x) != 0 || CoreMath.fracPart(y) != 0) {
internalDrawScaledImage(image, srcX, srcY, srcWidth, srcHeight);
}
else {
internalDrawImage(image, srcX, srcY, srcWidth, srcHeight);
}
}
else if ((type & Transform.TYPE_ROTATE) != 0) {
internalDrawRotatedImage(image, srcX, srcY, srcWidth, srcHeight);
}
else {
internalDrawScaledImage(image, srcX, srcY, srcWidth, srcHeight);
}
}
/**
Draws an image at a specific location. The image is drawn using
the current clip, transform, alpha value, and blend mode.
If the image is null, no action is taken and no exception is thrown.
@param image the image to draw.
@param x the x coordinate.
@param y the y coordinate.
*/
public void drawImage(CoreImage image, int x, int y) {
if (image != null) {
drawImage(image, x, y, 0, 0, image.getWidth(), image.getHeight());
}
}
public void drawImage(CoreImage image, int x, int y,
int srcX, int srcY, int srcWidth, int srcHeight)
{
if (Build.DEBUG) {
validateImage(image, srcX, srcY, srcWidth, srcHeight);
}
if (alpha == 0 || srcWidth == 0 || srcHeight == 0) {
return;
}
pushTransform();
int fx = CoreMath.toFixed(x);
int fy = CoreMath.toFixed(y);
transform.translate(fx, fy);
drawImage(image, srcX, srcY, srcWidth, srcHeight);
popTransform();
}
public void drawScaledImage(CoreImage image, int x, int y, int w, int h) {
if (image != null) {
drawScaledImage(image, x, y, w, h, 0, 0, image.getWidth(), image.getHeight());
}
}
public void drawScaledImage(CoreImage image, int x, int y, int w, int h,
int srcX, int srcY, int srcWidth, int srcHeight)
{
if (Build.DEBUG) {
validateImage(image, srcX, srcY, srcWidth, srcHeight);
}
if (alpha == 0 || srcWidth == 0 || srcHeight == 0) {
return;
}
pushTransform();
int fx = CoreMath.toFixed(x);
int fy = CoreMath.toFixed(y);
transform.translate(fx, fy);
int type = transform.getType();
if (type == Transform.TYPE_IDENTITY || type == Transform.TYPE_TRANSLATE) {
// Scale by exact dimensions
internalDrawScaledImage(image, w, h, srcX, srcY, srcWidth, srcHeight);
}
else {
// Scale by value
int fScaleX = CoreMath.toFixed(w) / srcWidth;
int fScaleY = CoreMath.toFixed(h) / srcHeight;
transform.scale(fScaleX, fScaleY);
drawImage(image, srcX, srcY, srcWidth, srcHeight);
}
popTransform();
}
/**
Draws a rotated and scaled image. The image is rotated around it's center.
@param angle a fixed-point angle, typically in the range from 0 to
2 * CoreMath.PI.
*/
public void drawRotatedImage(CoreImage image, int x, int y, int w, int h, int angle) {
if (image != null) {
drawRotatedImage(image, x, y, w, h, CoreMath.cos(angle), CoreMath.sin(angle),
0, 0, image.getWidth(), image.getHeight());
}
}
/**
Draws a rotated and scaled image. The image is rotated around it's center.
@param angle a fixed-point angle, typically in the range from 0 to
2 * CoreMath.PI.
*/
public void drawRotatedImage(CoreImage image, int x, int y, int w, int h, int angle,
int srcX, int srcY, int srcWidth, int srcHeight)
{
drawRotatedImage(image, x, y, w, h, CoreMath.cos(angle), CoreMath.sin(angle),
srcX, srcY, srcWidth, srcHeight);
}
/**
Draws a rotated and scaled image using pre-computed cosine and sine
of the angle. The image is rotated around it's center.
@param cosAngle The fixed-point cosine of the angle.
@param sinAngle The fixed-point sine of the angle.
*/
public void drawRotatedImage(CoreImage image, int x, int y, int w, int h,
int cosAngle, int sinAngle)
{
if (image != null) {
drawRotatedImage(image, x, y, w, h, cosAngle, sinAngle,
0, 0, image.getWidth(), image.getHeight());
}
}
/**
Draws a rotated and scaled image using pre-computed cosine and sine
of the angle. The image is rotated around it's center.
@param cosAngle The fixed-point cosine of the angle.
@param sinAngle The fixed-point sine of the angle.
*/
public void drawRotatedImage(CoreImage image, int x, int y, int w, int h,
int cosAngle, int sinAngle,
int srcX, int srcY, int srcWidth, int srcHeight)
{
if (Build.DEBUG) {
validateImage(image, srcX, srcY, srcWidth, srcHeight);
}
if (alpha == 0 || srcWidth == 0 || srcHeight == 0) {
return;
}
pushTransform();
int fx = CoreMath.toFixed(x);
int fy = CoreMath.toFixed(y);
int fw = CoreMath.toFixed(w);
int fh = CoreMath.toFixed(h);
int fSrcWidth = CoreMath.toFixed(srcWidth);
int fSrcHeight = CoreMath.toFixed(srcHeight);
int fScaleX = fw / srcWidth;
int fScaleY = fh / srcHeight;
transform.translate(fx, fy);
transform.scale(fScaleX, fScaleY);
transform.translate(fSrcWidth / 2, fSrcHeight / 2);
transform.rotate(cosAngle, sinAngle);
transform.translate(-fSrcWidth / 2, -fSrcHeight / 2);
drawImage(image, srcX, srcY, srcWidth, srcHeight);
popTransform();
}
//
// Image rendering - internal. This is where actual rendering occurs.
// (normal, scaled, and rotated/sheared)
//
private void internalDrawImage(CoreImage image,
int srcX, int srcY, int srcWidth, int srcHeight)
{
if (Build.DEBUG) {
validateImage(image, srcX, srcY, srcWidth, srcHeight);
}
if (alpha == 0 || srcWidth == 0 || srcHeight == 0) {
return;
}
int x = CoreMath.toInt(transform.getTranslateX());
int y = CoreMath.toInt(transform.getTranslateY());
clipObject(x, y, srcWidth, srcHeight);
if (objectWidth <= 0 || objectHeight <= 0) {
return;
}