├── .gitignore
├── README.md
├── pom.xml
├── skia-macosx-x86_64.patch
└── src
├── main
└── java
│ └── skia
│ └── javacpp
│ ├── AwtHelper.java
│ ├── Skia.java
│ ├── core.java
│ ├── effects.java
│ ├── images.java
│ └── utils.java
└── test
└── java
└── skia
└── javacpp
├── AwtHelperTest.java
├── SkPaintTest.java
├── SkiaTest.java
└── gm
├── GM.java
├── GMRegistry.java
├── Glitch.java
├── aaclip.java
├── aarectmodes.java
├── arithmode.java
├── bigmatrix.java
├── bitmapcopy.java
├── bitmapfilters.java
├── bitmapscroll.java
├── blurs.java
├── cmykjpeg.java
├── colormatrix.java
├── complexclip.java
├── complexclip2.java
├── convexpaths.java
├── cubicpaths.java
├── degeneratesegments.java
├── drawbitmaprect.java
├── emptypath.java
├── filltypes.java
├── filltypespersp.java
├── fontscaler.java
├── giantbitmap.java
├── gmmain.java
├── gradients.java
├── gradtext.java
├── hairmodes.java
├── imageblur.java
├── lcdtext.java
├── linepaths.java
├── morphology.java
├── nocolorbleed.java
├── patheffects.java
├── pathfill.java
├── pathreverse.java
├── points.java
├── poly2poly.java
├── quadpaths.java
├── shaderbounds.java
├── shadertext.java
├── shadows.java
├── strokefill.java
├── strokerects.java
├── strokes.java
├── tablecolorfilter.java
├── testimagefilters.java
├── tilemodes.java
├── tinybitmap.java
├── verttext.java
├── verttext2.java
└── xfermodes.java
/.gitignore:
--------------------------------------------------------------------------------
1 | # Maven
2 | target
3 |
4 | # Eclipse
5 | .classpath
6 | .project
7 | .settings
8 |
9 | #IntelliJ IDEA
10 | .idea
11 | *.iml
12 |
13 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | Skia Java Binding.
2 |
3 | When Java2D anti-aliasing slows down your application, try Skia.
4 |
5 | Known issues:
6 |
7 | * BigMatrixGM doesn't work
8 | * DrawBitmapRectGM doesn't work
--------------------------------------------------------------------------------
/pom.xml:
--------------------------------------------------------------------------------
1 |
3 | 4.0.0
4 |
5 | skia
6 | skia-javacpp
7 | jar
8 | 0.1-SNAPSHOT
9 |
10 | Skia Java Binding
11 |
12 |
13 | scm:git:git://github.com/eungju/skia-javacpp.git
14 | scm:git:git@github.com:eungju/skia-javacpp.git
15 | https://eungju@github.com/eungju/skia-javacpp.git
16 |
17 |
18 |
19 |
20 | eungju
21 | Eungju PARK
22 |
23 |
24 |
25 |
26 |
27 | com.googlecode.javacpp
28 | javacpp
29 | 0.2
30 |
31 |
32 | com.google.guava
33 | guava
34 | 11.0.2
35 | test
36 |
37 |
38 | junit
39 | junit-dep
40 | 4.10
41 |
42 |
43 | org.jmock
44 | jmock
45 | ${jmock.version}
46 | test
47 |
48 |
49 | org.jmock
50 | jmock-legacy
51 | ${jmock.version}
52 | test
53 |
54 |
55 | org.jmock
56 | jmock-junit4
57 | ${jmock.version}
58 | test
59 |
60 |
61 |
62 |
63 |
64 |
65 | maven-compiler-plugin
66 |
67 | true
68 | true
69 | true
70 | ${maven.compiler.source}
71 | ${maven.compiler.target}
72 | ${maven.compiler.encoding}
73 |
74 |
75 |
76 | org.apache.maven.plugins
77 | maven-surefire-plugin
78 |
79 |
80 | **/*Test.java
81 |
82 |
83 |
84 |
85 | org.apache.maven.plugins
86 | maven-source-plugin
87 |
88 |
89 | attach-sources
90 | verify
91 |
92 | jar
93 |
94 |
95 |
96 |
97 |
98 | org.apache.maven.plugins
99 | maven-javadoc-plugin
100 |
101 |
102 | attach-javadoc
103 | verify
104 |
105 | jar
106 |
107 |
108 |
109 |
110 |
111 | org.apache.maven.plugins
112 | maven-dependency-plugin
113 | 2.3
114 |
115 |
116 |
117 | properties
118 |
119 |
120 |
121 |
122 |
123 | org.codehaus.mojo
124 | exec-maven-plugin
125 | 1.2.1
126 |
127 |
128 | build-jnilib
129 | process-classes
130 |
131 | exec
132 |
133 |
134 | java
135 |
136 | -jar ${com.googlecode.javacpp:javacpp:jar}
137 | -classpath ${project.build.outputDirectory}
138 | -d ${project.build.outputDirectory}/skia/javacpp/macosx-x86_64
139 | -o jniSkia
140 | skia/javacpp/*
141 |
142 |
143 |
144 |
145 | build-test-jnilib
146 | process-test-classes
147 |
148 | exec
149 |
150 |
151 | java
152 |
153 | -jar ${com.googlecode.javacpp:javacpp:jar}
154 | -classpath ${project.build.testOutputDirectory}
155 | -d ${project.build.testOutputDirectory}/skia/javacpp/gm/macosx-x86_64
156 | -o jniGlitch
157 | skia/javacpp/gm/Glitch
158 |
159 |
160 |
161 |
162 |
163 |
164 |
165 |
166 |
167 |
168 |
169 | org.apache.maven.plugins
170 | maven-surefire-report-plugin
171 |
172 |
173 | undercover
174 | undercover-maven-plugin
175 |
176 |
177 | html
178 | coberturaxml
179 | emmaxml
180 |
181 |
182 |
183 |
184 | org.apache.maven.plugins
185 | maven-javadoc-plugin
186 |
187 | ${maven.compiler.encoding}
188 | ${maven.compiler.encoding}
189 | ${maven.compiler.encoding}
190 |
191 |
192 |
193 | org.codehaus.mojo
194 | jdepend-maven-plugin
195 |
196 |
197 | maven-pmd-plugin
198 |
199 | ${maven.compiler.encoding}
200 | 50
201 | true
202 | ${maven.compiler.target}
203 |
204 |
205 |
206 | org.codehaus.mojo
207 | findbugs-maven-plugin
208 |
209 |
210 |
211 |
212 |
213 |
214 | javacpp
215 | http://maven2.javacpp.googlecode.com/git
216 |
217 |
218 | zact
219 | http://zact.googlecode.com/svn/maven/repository
220 |
221 |
222 |
223 |
224 |
225 | zact-snaphost
226 | http://zact.googlecode.com/svn/maven/snapshot-repository
227 |
228 | true
229 |
230 |
231 |
232 |
233 |
234 |
235 | zact
236 | dav:https://zact.googlecode.com/svn/maven/repository
237 |
238 |
239 | zact-snapshot
240 | dav:https://zact.googlecode.com/svn/maven/snapshot-repository
241 |
242 |
243 |
244 |
245 | utf-8
246 | 1.5
247 | 1.5
248 | 2.6.0-RC2
249 |
250 |
251 |
252 |
--------------------------------------------------------------------------------
/skia-macosx-x86_64.patch:
--------------------------------------------------------------------------------
1 | Index: src/ports/SkFontHost_mac_coretext.cpp
2 | ===================================================================
3 | --- src/ports/SkFontHost_mac_coretext.cpp (revision 3485)
4 | +++ src/ports/SkFontHost_mac_coretext.cpp (working copy)
5 | @@ -1948,7 +1948,7 @@
6 | int count = CFArrayGetCount(cfArray);
7 | if (tags) {
8 | for (int i = 0; i < count; ++i) {
9 | - tags[i] = (SkFontTableTag)CFArrayGetValueAtIndex(cfArray, i);
10 | + tags[i] = (SkFontTableTag)(uintptr_t)CFArrayGetValueAtIndex(cfArray, i);
11 | }
12 | }
13 | return count;
14 | Index: gyp/common_conditions.gypi
15 | ===================================================================
16 | --- gyp/common_conditions.gypi (revision 3485)
17 | +++ gyp/common_conditions.gypi (working copy)
18 | @@ -126,6 +126,7 @@
19 | },
20 | },
21 | 'xcode_settings': {
22 | + 'ARCHS': ['x86', 'x86_64'],
23 | 'SYMROOT': '<(DEPTH)/xcodebuild',
24 | 'SDKROOT': 'macosx10.6',
25 | # trying to get this to work, but it needs clang I think...
26 |
--------------------------------------------------------------------------------
/src/main/java/skia/javacpp/AwtHelper.java:
--------------------------------------------------------------------------------
1 | package skia.javacpp;
2 |
3 | import com.googlecode.javacpp.IntPointer;
4 |
5 | import java.awt.*;
6 | import java.awt.geom.AffineTransform;
7 | import java.awt.geom.PathIterator;
8 | import java.awt.geom.Point2D;
9 | import java.awt.image.BufferedImage;
10 | import java.awt.image.DataBufferInt;
11 |
12 | import static skia.javacpp.core.*;
13 |
14 | public class AwtHelper {
15 | public static BufferedImage toBufferedImage(SkBitmap bitmap) {
16 | int w = bitmap.width();
17 | int h = bitmap.height();
18 | bitmap.lockPixels();
19 | try {
20 | IntPointer pixelPtr = new IntPointer(bitmap.getPixels());
21 | BufferedImage bi = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB_PRE);
22 | DataBufferInt dataBuffer = (DataBufferInt) bi.getRaster().getDataBuffer();
23 | int[] pixels = dataBuffer.getData();
24 | pixelPtr.get(pixels);
25 | return bi;
26 | } finally {
27 | bitmap.unlockPixels();
28 | }
29 | }
30 |
31 | public static SkBitmap toSkBitmap(BufferedImage image) {
32 | int w = image.getWidth();
33 | int h = image.getHeight();
34 | SkBitmap bitmap = new SkBitmap();
35 | bitmap.setConfig(SkBitmap.kARGB_8888_Config, w, h);
36 | bitmap.allocPixels();
37 | bitmap.lockPixels();
38 | try {
39 | IntPointer pixelPtr = new IntPointer(bitmap.getPixels());
40 | int[] samples = image.getRGB(0, 0, w, h, null, 0, w);
41 | for (int i = 0; i < samples.length; i++) {
42 | samples[i] = SkPreMultiplyColor(samples[i]);
43 | }
44 | pixelPtr.put(samples);
45 | } finally {
46 | bitmap.unlockPixels();
47 | }
48 | return bitmap;
49 | }
50 |
51 | public static SkPath toSkPath(Shape shape, AffineTransform transform) {
52 | SkPath path = new SkPath();
53 | PathIterator pi = shape.getPathIterator(transform);
54 | float[] f = new float[6];
55 | while (!pi.isDone()) {
56 | int i = pi.currentSegment(f);
57 | pi.next();
58 | switch (i) {
59 | case PathIterator.SEG_MOVETO:
60 | path.moveTo(f[0], f[1]);
61 | break;
62 | case PathIterator.SEG_LINETO:
63 | path.lineTo(f[0], f[1]);
64 | break;
65 | case PathIterator.SEG_QUADTO:
66 | path.quadTo(f[0], f[1], f[2], f[3]);
67 | break;
68 | case PathIterator.SEG_CUBICTO:
69 | path.cubicTo(f[0], f[1], f[2], f[3], f[4], f[5]);
70 | break;
71 | case PathIterator.SEG_CLOSE:
72 | path.close();
73 | break;
74 | }
75 | }
76 | return path;
77 | }
78 |
79 | public static int toSkColor(Color color) {
80 | return SkColorSetARGB(color.getAlpha(), color.getRed(), color.getGreen(), color.getBlue());
81 | }
82 |
83 | public static int[] toSkColorArray(Color... colors) {
84 | int[] skColors = new int[colors.length];
85 | for (int i = 0; i < colors.length; i++) {
86 | skColors[i] = toSkColor(colors[i]);
87 | }
88 | return skColors;
89 | }
90 |
91 | public static SkPoint toSkPoint(Point2D point) {
92 | return SkPoint.Make((float) point.getX(), (float) point.getY());
93 | }
94 |
95 | public static int toSkShaderTileMode(MultipleGradientPaint.CycleMethod method) {
96 | switch (method) {
97 | case NO_CYCLE:
98 | return SkShader.kClamp_TileMode;
99 | case REFLECT:
100 | return SkShader.kMirror_TileMode;
101 | case REPEAT:
102 | return SkShader.kRepeat_TileMode;
103 | }
104 | throw new IllegalArgumentException("Unsupported cycle method " + method);
105 | }
106 |
107 | }
108 |
--------------------------------------------------------------------------------
/src/main/java/skia/javacpp/Skia.java:
--------------------------------------------------------------------------------
1 | package skia.javacpp;
2 |
3 | import com.googlecode.javacpp.BytePointer;
4 | import com.googlecode.javacpp.annotation.Platform;
5 | import com.googlecode.javacpp.annotation.Properties;
6 |
7 | import java.nio.charset.Charset;
8 |
9 | import static skia.javacpp.Skia.*;
10 |
11 | @Properties({
12 | @Platform(define={"SK_SCALAR_IS_FLOAT", "SK_CAN_USE_FLOAT", "SK_RELEASE", "GR_RELEASE 1", "NDEBUG"}),
13 | @Platform(value="windows", includepath=windowsIncludepath, linkpath=windowsLinkpath, link=windowsLink),
14 | @Platform(value="linux", includepath=linuxIncludepath, linkpath=linuxLinkpath, link=linuxLink),
15 | @Platform(value="macosx", includepath=macosxIncludepath, linkpath=macosxLinkpath, link=macosxLink, framework="Foundation:ApplicationServices:OpenGL")
16 | })
17 | public class Skia {
18 | public static final String windowsIncludepath = "../skia/include/core;../skia/include/config;../skia/include/effects;../skia/include/images;../skia/include/pipe;../skia/include/utils";
19 | public static final String windowsLinkpath = "../skia/out/gyp/Release/lib;C:/Program Files/Microsoft SDKs/Windows/v7.0A/Lib";
20 | public static final String windowsLink = "core;effects;gr:images;opts;opts_ssse3;ports;skgr:utils;gdi32;Usp10;User32;ole32;oleaut32";
21 | public static final String linuxIncludepath = "../skia/include/core:../skia/include/config:../skia/include/effects:../skia/include/images:../skia/include/pipe:../skia/include/utils";
22 | public static final String linuxLinkpath = "../skia/out/Release/obj.target/gyp";
23 | public static final String linuxLink = "core:effects:gr:images:opts:opts_ssse3:ports:skgr:utils:zlib:freetype:png";
24 | public static final String macosxIncludepath = "../skia/include/core:../skia/include/config:../skia/include/effects:../skia/include/images:../skia/include/pipe:../skia/include/utils";
25 | public static final String macosxLinkpath = "../skia/out/Release";
26 | public static final String macosxLink = "core:effects:gr:images:jpeg:opts:opts_ssse3:ports:skgr:utils:zlib";
27 |
28 | public static final Charset UTF_8 = Charset.forName("UTF-8");
29 | public static final Charset UTF_16 = Charset.forName("UTF-16");
30 | public static final Charset UTF_32 = Charset.forName("UTF-32");
31 |
32 | public static BytePointer toPointer(String text, Charset charset) {
33 | byte[] raw = text.getBytes(charset);
34 | return new BytePointer(raw);
35 | }
36 | }
37 |
--------------------------------------------------------------------------------
/src/main/java/skia/javacpp/images.java:
--------------------------------------------------------------------------------
1 | package skia.javacpp;
2 |
3 | import com.googlecode.javacpp.IntPointer;
4 | import com.googlecode.javacpp.Loader;
5 | import com.googlecode.javacpp.Pointer;
6 | import com.googlecode.javacpp.annotation.ByRef;
7 | import com.googlecode.javacpp.annotation.Cast;
8 | import com.googlecode.javacpp.annotation.Const;
9 | import com.googlecode.javacpp.annotation.Platform;
10 | import com.googlecode.javacpp.annotation.Properties;
11 |
12 | import static skia.javacpp.core.*;
13 |
14 | @Properties({
15 | @Platform(include={"SkImageDecoder.h", "SkImageEncoder.h"})
16 | })
17 | public class images {
18 | static { Loader.load(Skia.class); }
19 |
20 | public static class SkImageDecoder extends Pointer {
21 | static { Loader.load(Skia.class); }
22 |
23 | //enum Format
24 | public static final int kUnknown_Format = 0,
25 | kBMP_Format = 1,
26 | kGIF_Format = 2,
27 | kICO_Format = 3,
28 | kJPEG_Format = 4,
29 | kPNG_Format = 5,
30 | kWBMP_Format = 6;
31 |
32 | public native @Cast("SkImageDecoder::Format") int getFormat();
33 |
34 | public native boolean getDitherImage();
35 |
36 | public native void setDitherImage(boolean dither);
37 |
38 | public static class Peeker extends SkRefCnt {
39 | static { Loader.load(Skia.class); }
40 |
41 | protected Peeker() {}
42 |
43 | public native boolean peek(String tag, @Const Pointer data, @Cast("size_t") int length);
44 | };
45 |
46 | public native Peeker getPeeker();
47 | public native Peeker setPeeker(Peeker peeker);
48 |
49 | public static class Chooser extends SkRefCnt {
50 | static { Loader.load(Skia.class); }
51 |
52 | protected Chooser() {}
53 |
54 | public native void begin(int count);
55 | public native void inspect(int index, @Cast("SkBitmap::Config") int config, int width, int height);
56 | public native int choose();
57 | };
58 |
59 | public native Chooser getChooser();
60 | public native Chooser setChooser(Chooser chooser);
61 |
62 | public native void setPrefConfigTable(@Cast("const SkBitmap::Config*") int[] pref);
63 |
64 | public native SkBitmap.Allocator getAllocator();
65 | public native SkBitmap.Allocator setAllocator(SkBitmap.Allocator allocator);
66 |
67 | public native int getSampleSize();
68 | public native void setSampleSize(int size);
69 |
70 | public native void resetSampleSize();
71 |
72 | public native void cancelDecode();
73 |
74 | //enum Mode
75 | public static final int kDecodeBounds_Mode = 0,
76 | kDecodePixels_Mode = 1;
77 |
78 | public native boolean decode(SkStream stream, SkBitmap bitmap, @Cast("SkBitmap::Config") int pref, @Cast("SkImageDecoder::Mode") int mode);
79 | public native boolean decode(SkStream stream, SkBitmap bitmap, @Cast("SkImageDecoder::Mode") int mode);
80 |
81 | public native static SkImageDecoder Factory(SkStream stream);
82 |
83 | public native static boolean DecodeFile(String file, SkBitmap bitmap, @Cast("SkBitmap::Config") int prefConfig, @Cast("SkImageDecoder::Mode") int mode, @Cast("SkImageDecoder::Format*") IntPointer format/* = NULL*/);
84 | public native static boolean DecodeFile(String file, SkBitmap bitmap);
85 | public native static boolean DecodeMemory(@Const Pointer buffer, @Cast("size_t") int size, SkBitmap bitmap,
86 | @Cast("SkBitmap::Config") int prefConfig, @Cast("SkImageDecoder::Mode") int mode,
87 | @Cast("SkImageDecoder::Format*") IntPointer format/* = NULL*/);
88 | public native static boolean DecodeMemory(@Const Pointer buffer, @Cast("size_t") int size, SkBitmap bitmap);
89 | public native static boolean DecodeStream(SkStream stream, SkBitmap bitmap,
90 | @Cast("SkBitmap::Config") int prefConfig, @Cast("SkImageDecoder::Mode") int mode,
91 | @Cast("SkImageDecoder::Format*") IntPointer format/* = NULL*/);
92 | public native static boolean DecodeStream(SkStream stream, SkBitmap bitmap);
93 |
94 | public native static @Cast("SkBitmap::Config") int GetDeviceConfig();
95 | public native static void SetDeviceConfig(@Cast("SkBitmap::Config") int config);
96 | }
97 |
98 | public static class SkImageEncoder extends Pointer {
99 | static { Loader.load(Skia.class); }
100 |
101 | //enum Type
102 | public static final int kJPEG_Type = 0,
103 | kPNG_Type = 1;
104 |
105 | public native static SkImageEncoder Create(@Cast("SkImageEncoder::Type") int type);
106 |
107 | public static final int kDefaultQuality = 80;
108 |
109 | public native boolean encodeFile(String file, @Const @ByRef SkBitmap bitmap, int quality);
110 | public native boolean encodeStream(SkWStream stream, @Const @ByRef SkBitmap bitmap, int quality);
111 |
112 | public native static boolean EncodeFile(String file, @Const @ByRef SkBitmap bitmap, @Cast("SkImageEncoder::Type") int type, int quality);
113 | public native static boolean EncodeStream(SkWStream stream, @Const @ByRef SkBitmap bitmap, @Cast("SkImageEncoder::Type") int type, int quality);
114 | }
115 | }
116 |
--------------------------------------------------------------------------------
/src/main/java/skia/javacpp/utils.java:
--------------------------------------------------------------------------------
1 | package skia.javacpp;
2 |
3 | import com.googlecode.javacpp.Loader;
4 | import com.googlecode.javacpp.annotation.NoDeallocator;
5 | import com.googlecode.javacpp.annotation.Platform;
6 | import com.googlecode.javacpp.annotation.Properties;
7 |
8 | import static skia.javacpp.core.*;
9 |
10 | @Properties({
11 | @Platform(include={"SkDeferredCanvas.h", "SkUnitMappers.h"})
12 | })
13 | public class utils {
14 | static { Loader.load(Skia.class); }
15 |
16 | /*
17 | * SkDeferredCanvas.h
18 | */
19 |
20 | public static class SkDeferredCanvas extends SkCanvas {
21 | static { Loader.load(Skia.class); }
22 |
23 | public SkDeferredCanvas() {
24 | allocate();
25 | deallocator(new UnrefDeallocator(this));
26 | }
27 | @NoDeallocator
28 | private native void allocate();
29 |
30 | public SkDeferredCanvas(SkDevice device) {
31 | allocate(device);
32 | deallocator(new UnrefDeallocator(this));
33 | };
34 | @NoDeallocator
35 | private native void allocate(SkDevice device);
36 |
37 | //TODO: public SkDeferredCanvas(SkDevice device, DeviceContext deviceContext) { allocate(device, deviceContext); };
38 | }
39 |
40 | /*
41 | * SkUnitMappers.h
42 | */
43 |
44 | public static class SkDiscreteMapper extends SkUnitMapper {
45 | static { Loader.load(Skia.class); }
46 |
47 | public SkDiscreteMapper(int segments) {
48 | allocate(segments);
49 | deallocator(new UnrefDeallocator(this));
50 | }
51 | @NoDeallocator
52 | private native void allocate(int segments);
53 | };
54 |
55 | public static class SkCosineMapper extends SkUnitMapper {
56 | static { Loader.load(Skia.class); }
57 |
58 | public SkCosineMapper() {
59 | allocate();
60 | deallocator(new UnrefDeallocator(this));
61 | }
62 | @NoDeallocator
63 | private native void allocate();
64 | };
65 | }
66 |
--------------------------------------------------------------------------------
/src/test/java/skia/javacpp/AwtHelperTest.java:
--------------------------------------------------------------------------------
1 | package skia.javacpp;
2 |
3 | import com.google.common.base.Stopwatch;
4 | import org.junit.Test;
5 |
6 | import java.awt.*;
7 | import java.awt.image.BufferedImage;
8 |
9 | import static skia.javacpp.core.*;
10 |
11 | public class AwtHelperTest {
12 | @Test public void toBufferedImage() {
13 | SkBitmap bitmap = new SkBitmap();
14 | bitmap.setConfig(SkBitmap.kARGB_8888_Config, 256, 256);
15 | bitmap.allocPixels();
16 | bitmap.eraseColor(SK_ColorBLUE);
17 | Stopwatch s = new Stopwatch().start();
18 | for (int i = 0; i < 1000; i++) {
19 | AwtHelper.toBufferedImage(bitmap);
20 | }
21 | s.stop();
22 | System.out.println(s.elapsedMillis());
23 | }
24 |
25 | @Test public void toSkBitmap() {
26 | BufferedImage image = new BufferedImage(100, 100, BufferedImage.TYPE_4BYTE_ABGR);
27 | Graphics2D g2d = image.createGraphics();
28 | g2d.setBackground(Color.RED);
29 | g2d.clearRect(0, 0, image.getWidth() / 2, image.getHeight() / 2);
30 | SkBitmap bitmap = AwtHelper.toSkBitmap(image);
31 | SkiaTest.saveToFile("to_SkBitmap", bitmap);
32 | }
33 | }
34 |
--------------------------------------------------------------------------------
/src/test/java/skia/javacpp/SkPaintTest.java:
--------------------------------------------------------------------------------
1 | package skia.javacpp;
2 |
3 | import com.googlecode.javacpp.Pointer;
4 | import org.junit.Test;
5 |
6 | import static org.junit.Assert.*;
7 | import static skia.javacpp.core.*;
8 |
9 | public class SkPaintTest {
10 | @Test public void getFontMetrics() {
11 | SkPaint dut = new SkPaint();
12 | dut.setTextSize(12);
13 | dut.setTypeface(SkTypeface.CreateFromName("Arial", SkTypeface.kNormal));
14 | SkPaint.FontMetrics metrics = new SkPaint.FontMetrics();
15 | assertEquals(dut.getFontSpacing(), dut.getFontMetrics(metrics, 1), 0);
16 | }
17 |
18 | @Test public void textToGlyphs() {
19 | SkPaint dut = new SkPaint();
20 | dut.setTextEncoding(SkPaint.kUTF8_TextEncoding);
21 | dut.setTypeface(SkTypeface.CreateFromName("Arial", SkTypeface.kNormal));
22 | short[] glyphs = new short[4];
23 | dut.textToGlyphs("ABCD", glyphs);
24 | assertArrayEquals(new short[] {36, 37, 38, 39}, glyphs);
25 | }
26 |
27 | @Test public void getTextWidths() {
28 | SkPaint dut = new SkPaint();
29 | dut.setTypeface(SkTypeface.CreateFromName("Arial", SkTypeface.kNormal));
30 | dut.setTextSize(12);
31 | dut.setTextEncoding(SkPaint.kUTF8_TextEncoding);
32 | Pointer text = SkPaint.encodeText("ABCD", dut.getTextEncoding());
33 | float[] widths = new float[4];
34 | dut.getTextWidths(text, text.capacity(), widths, null);
35 | assertArrayEquals(new float[] {8, 8, 9, 9}, widths, 1F);
36 | }
37 | }
38 |
--------------------------------------------------------------------------------
/src/test/java/skia/javacpp/gm/GM.java:
--------------------------------------------------------------------------------
1 | package skia.javacpp.gm;
2 |
3 | import java.util.ArrayList;
4 | import java.util.List;
5 |
6 | import static skia.javacpp.core.*;
7 |
8 | public abstract class GM {
9 | public static SkISize make_isize(int w, int h) {
10 | SkISize sz = new SkISize();
11 | sz.set(w, h);
12 | return sz;
13 | }
14 |
15 | public GM() {
16 | fBGColor = SK_ColorWHITE;
17 | }
18 |
19 | //enum Flags
20 | public static final int kSkipPDF_Flag = 1 << 0,
21 | kSkipPicture_Flag = 1 << 1;
22 |
23 | public void draw(SkCanvas canvas) {
24 | drawBackground(canvas);
25 | drawContent(canvas);
26 | }
27 |
28 | public void drawBackground(SkCanvas canvas) {
29 | onDrawBackground(canvas);
30 | }
31 |
32 | public void drawContent(SkCanvas canvas) {
33 | onDraw(canvas);
34 | }
35 |
36 | public SkISize getISize() {
37 | return onISize();
38 | }
39 |
40 | public String shortName() {
41 | if (fShortName == null) {
42 | fShortName = onShortName();
43 | }
44 | return fShortName;
45 | }
46 |
47 | public int getFlags() {
48 | return onGetFlags();
49 | }
50 |
51 | public SkMatrix getInitialTransform() {
52 | return onGetInitialTransform();
53 | }
54 |
55 | public int getBGColor() {
56 | return fBGColor;
57 | }
58 |
59 | public void setBGColor(int color) {
60 | fBGColor = color;
61 | }
62 |
63 | public void drawSizeBounds(SkCanvas canvas, int color) {
64 | SkISize size = getISize();
65 | SkRect r = SkRect.MakeWH(size.width(), size.height());
66 | SkPaint paint = new SkPaint();
67 | paint.setColor(color);
68 | canvas.drawRect(r, paint);
69 | }
70 |
71 | public static void SetResourcePath(String resourcePath) {
72 | gResourcePath = resourcePath == null ? "" : resourcePath;
73 | }
74 |
75 | protected static String gResourcePath;
76 |
77 | protected abstract void onDraw(SkCanvas canvas);
78 |
79 | protected void onDrawBackground(SkCanvas canvas) {
80 | canvas.drawColor(fBGColor);
81 | }
82 |
83 | protected abstract SkISize onISize();
84 | protected abstract String onShortName();
85 | protected int onGetFlags() { return 0; }
86 | protected SkMatrix onGetInitialTransform() { return SkMatrix.I(); }
87 |
88 | private String fShortName;
89 | private int fBGColor;
90 | }
91 |
--------------------------------------------------------------------------------
/src/test/java/skia/javacpp/gm/GMRegistry.java:
--------------------------------------------------------------------------------
1 | package skia.javacpp.gm;
2 |
3 | public class GMRegistry {
4 | public static interface Factory {
5 | GM apply();
6 | }
7 | }
8 |
--------------------------------------------------------------------------------
/src/test/java/skia/javacpp/gm/Glitch.java:
--------------------------------------------------------------------------------
1 | package skia.javacpp.gm;
2 |
3 | import com.googlecode.javacpp.Loader;
4 | import com.googlecode.javacpp.annotation.Cast;
5 | import com.googlecode.javacpp.annotation.Platform;
6 | import com.googlecode.javacpp.annotation.Properties;
7 |
8 | @Properties({
9 | @Platform(include={"stdlib.h"})
10 | })
11 | public class Glitch {
12 | static { Loader.load(); }
13 |
14 | public native static int rand();
15 |
16 | public native static void srand(@Cast("unsigned") int seed);
17 | }
18 |
--------------------------------------------------------------------------------
/src/test/java/skia/javacpp/gm/aaclip.java:
--------------------------------------------------------------------------------
1 | package skia.javacpp.gm;
2 |
3 | import static skia.javacpp.core.*;
4 |
5 | public class aaclip {
6 | /** Draw a 2px border around the target, then red behind the target;
7 | set the clip to match the target, then draw >> the target in blue.
8 | */
9 |
10 | static void draw (SkCanvas canvas, SkRect target, int x, int y) {
11 | SkPaint borderPaint = new SkPaint();
12 | borderPaint.setColor(SkColorSetRGB(0x0, 0xDD, 0x0));
13 | borderPaint.setAntiAlias(true);
14 | SkPaint backgroundPaint = new SkPaint();
15 | backgroundPaint.setColor(SkColorSetRGB(0xDD, 0x0, 0x0));
16 | backgroundPaint.setAntiAlias(true);
17 | SkPaint foregroundPaint = new SkPaint();
18 | foregroundPaint.setColor(SkColorSetRGB(0x0, 0x0, 0xDD));
19 | foregroundPaint.setAntiAlias(true);
20 |
21 | canvas.save();
22 | canvas.translate(SkIntToScalar(x), SkIntToScalar(y));
23 | target.inset(SkIntToScalar(-2), SkIntToScalar(-2));
24 | canvas.drawRect(target, borderPaint);
25 | target.inset(SkIntToScalar(2), SkIntToScalar(2));
26 | canvas.drawRect(target, backgroundPaint);
27 | canvas.clipRect(target, SkRegion.kIntersect_Op, true);
28 | target.inset(SkIntToScalar(-4), SkIntToScalar(-4));
29 | canvas.drawRect(target, foregroundPaint);
30 | canvas.restore();
31 | }
32 |
33 | static void draw_square (SkCanvas canvas, int x, int y) {
34 | SkRect target = SkRect.MakeWH(10 * SK_Scalar1, 10 * SK_Scalar1);
35 | draw(canvas, target, x, y);
36 | }
37 |
38 | static void draw_column (SkCanvas canvas, int x, int y) {
39 | SkRect target = SkRect.MakeWH(1 * SK_Scalar1, 10 * SK_Scalar1);
40 | draw(canvas, target, x, y);
41 | }
42 |
43 | static void draw_bar (SkCanvas canvas, int x, int y) {
44 | SkRect target = SkRect.MakeWH(10 * SK_Scalar1, 1 * SK_Scalar1);
45 | draw(canvas, target, x, y);
46 | }
47 |
48 | static void draw_rect_tests (SkCanvas canvas) {
49 | draw_square(canvas, 10, 10);
50 | draw_column(canvas, 30, 10);
51 | draw_bar(canvas, 10, 30);
52 | }
53 |
54 | /**
55 | Test a set of clipping problems discovered while writing blitAntiRect,
56 | and test all the code paths through the clipping blitters.
57 | Each region should show as a blue center surrounded by a 2px green
58 | border, with no red.
59 | */
60 | public static class AAClipGM extends GM {
61 | @Override
62 | protected String onShortName() {
63 | return "aaclip";
64 | }
65 |
66 | @Override
67 | protected SkISize onISize() {
68 | return make_isize(640, 480);
69 | }
70 |
71 | @Override
72 | protected void onDraw(SkCanvas canvas) {
73 | // Initial pixel-boundary-aligned draw
74 | draw_rect_tests(canvas);
75 |
76 | // Repeat 4x with .2, .4, .6, .8 px offsets
77 | canvas.translate(SK_Scalar1 / 5, SK_Scalar1 / 5);
78 | canvas.translate(SkIntToScalar(50), 0);
79 | draw_rect_tests(canvas);
80 |
81 | canvas.translate(SK_Scalar1 / 5, SK_Scalar1 / 5);
82 | canvas.translate(SkIntToScalar(50), 0);
83 | draw_rect_tests(canvas);
84 |
85 | canvas.translate(SK_Scalar1 / 5, SK_Scalar1 / 5);
86 | canvas.translate(SkIntToScalar(50), 0);
87 | draw_rect_tests(canvas);
88 |
89 | canvas.translate(SK_Scalar1 / 5, SK_Scalar1 / 5);
90 | canvas.translate(SkIntToScalar(50), 0);
91 | draw_rect_tests(canvas);
92 | }
93 | }
94 |
95 | public static GMRegistry.Factory MyFactory = new GMRegistry.Factory() {
96 | public GM apply() {
97 | return new AAClipGM();
98 | }
99 | };
100 | }
101 |
--------------------------------------------------------------------------------
/src/test/java/skia/javacpp/gm/aarectmodes.java:
--------------------------------------------------------------------------------
1 | package skia.javacpp.gm;
2 |
3 | import static skia.javacpp.core.*;
4 |
5 | public class aarectmodes {
6 | static void test4(SkCanvas canvas) {
7 | SkPaint paint = new SkPaint();
8 | paint.setAntiAlias(true);
9 | SkPoint[] pts = {
10 | SkPoint.Make(10, 160), SkPoint.Make(610, 160),
11 | SkPoint.Make(610, 160), SkPoint.Make(10, 160),
12 |
13 | SkPoint.Make(610, 160), SkPoint.Make(610, 160),
14 | SkPoint.Make(610, 199), SkPoint.Make(610, 199),
15 |
16 | SkPoint.Make(10, 198), SkPoint.Make(610, 198),
17 | SkPoint.Make(610, 199), SkPoint.Make(10, 199),
18 |
19 | SkPoint.Make(10, 160), SkPoint.Make(10, 160),
20 | SkPoint.Make(10, 199), SkPoint.Make(10, 199)
21 | };
22 | byte[] verbs = {
23 | 0, 1, 1, 1, 4,
24 | 0, 1, 1, 1, 4,
25 | 0, 1, 1, 1, 4,
26 | 0, 1, 1, 1, 4
27 | };
28 | SkPath path = new SkPath();
29 | for (int i = 0; i < verbs.length; ++i) {
30 | SkPoint ptPtr = pts[i];
31 | switch ((int) verbs[i]) {
32 | case SkPath.kMove_Verb:
33 | path.moveTo(ptPtr.fX(), ptPtr.fY());
34 | break;
35 | case SkPath.kLine_Verb:
36 | path.lineTo(ptPtr.fX(), ptPtr.fY());
37 | break;
38 | case SkPath.kClose_Verb:
39 | path.close();
40 | break;
41 | default:
42 | SkASSERT(false);
43 | break;
44 | }
45 | }
46 | SkRect clip = SkRect.MakeLTRB(0, 130, 772, 531);
47 | canvas.clipRect(clip);
48 | canvas.drawPath(path, paint);
49 | }
50 |
51 | static SkCanvas create_canvas(int w, int h) {
52 | SkBitmap bm = new SkBitmap();
53 | bm.setConfig(SkBitmap.kARGB_8888_Config, w, h);
54 | bm.allocPixels();
55 | bm.eraseColor(0);
56 | return new SkCanvas(bm);
57 | }
58 |
59 | static SkBitmap extract_bitmap(SkCanvas canvas) {
60 | return canvas.getDevice().accessBitmap(false);
61 | }
62 |
63 | static class Mode {
64 | int fMode;
65 | String fLabel;
66 | public Mode(int mode, String label) {
67 | fMode = mode;
68 | fLabel = label;
69 | }
70 | }
71 | static final Mode[] gModes = {
72 | new Mode(SkXfermode.kClear_Mode, "Clear"),
73 | new Mode(SkXfermode.kSrc_Mode, "Src"),
74 | new Mode(SkXfermode.kDst_Mode, "Dst"),
75 | new Mode(SkXfermode.kSrcOver_Mode, "SrcOver"),
76 | new Mode(SkXfermode.kDstOver_Mode, "DstOver"),
77 | new Mode(SkXfermode.kSrcIn_Mode, "SrcIn"),
78 | new Mode(SkXfermode.kDstIn_Mode, "DstIn"),
79 | new Mode(SkXfermode.kSrcOut_Mode, "SrcOut"),
80 | new Mode(SkXfermode.kDstOut_Mode, "DstOut"),
81 | new Mode(SkXfermode.kSrcATop_Mode, "SrcATop"),
82 | new Mode(SkXfermode.kDstATop_Mode, "DstATop"),
83 | new Mode(SkXfermode.kXor_Mode, "Xor" ),
84 | };
85 |
86 | static final int gWidth = 64;
87 | static final int gHeight = 64;
88 | static final float W = SkIntToScalar(gWidth);
89 | static final float H = SkIntToScalar(gHeight);
90 |
91 | static float drawCell(SkCanvas canvas, SkXfermode mode,
92 | int a0, int a1) {
93 |
94 | SkPaint paint = new SkPaint();
95 | paint.setAntiAlias(true);
96 |
97 | SkRect r = SkRect.MakeWH(W, H);
98 | r.inset(W/10, H/10);
99 |
100 | paint.setColor(SK_ColorBLUE);
101 | paint.setAlpha(a0);
102 | canvas.drawOval(r, paint);
103 |
104 | paint.setColor(SK_ColorRED);
105 | paint.setAlpha(a1);
106 | paint.setXfermode(mode);
107 |
108 | float offset = SK_Scalar1 / 3;
109 | SkRect rect = SkRect.MakeXYWH(W / 4 + offset,
110 | H / 4 + offset,
111 | W / 2, H / 2);
112 | canvas.drawRect(rect, paint);
113 |
114 | return H;
115 | }
116 |
117 | static SkShader make_bg_shader() {
118 | SkBitmap bm = new SkBitmap();
119 | bm.setConfig(SkBitmap.kARGB_8888_Config, 2, 2);
120 | bm.allocPixels();
121 | bm.getAddr32(0, 0).put(bm.getAddr32(1, 1).put(0xFFFFFFFF).get());
122 | bm.getAddr32(1, 0).put(bm.getAddr32(0, 1).put(SkPackARGB32(0xFF, 0xCC, 0xCC, 0xCC)).get());
123 |
124 | SkShader s = SkShader.CreateBitmapShader(bm,
125 | SkShader.kRepeat_TileMode,
126 | SkShader.kRepeat_TileMode);
127 |
128 | SkMatrix m = new SkMatrix();
129 | m.setScale(SkIntToScalar(6), SkIntToScalar(6));
130 | s.setLocalMatrix(m);
131 | return s;
132 | }
133 |
134 | public static class AARectModesGM extends GM {
135 | private SkPaint fBGPaint = new SkPaint();
136 | public AARectModesGM () {
137 | fBGPaint.setShader(make_bg_shader());
138 | }
139 |
140 | @Override
141 | protected String onShortName() {
142 | return "aarectmodes";
143 | }
144 |
145 | @Override
146 | protected SkISize onISize() { return make_isize(640, 480); }
147 |
148 | @Override
149 | protected void onDraw(SkCanvas canvas) {
150 | // test4(canvas);
151 | final SkRect bounds = SkRect.MakeWH(W, H);
152 | final int[] gAlphaValue = { 0xFF, 0x88, 0x88 };
153 |
154 | canvas.translate(SkIntToScalar(4), SkIntToScalar(4));
155 |
156 | for (int alpha = 0; alpha < 4; ++alpha) {
157 | canvas.save();
158 | canvas.save();
159 | for (int i = 0; i < gModes.length; ++i) {
160 | if (6 == i) {
161 | canvas.restore();
162 | canvas.translate(W * 5, 0);
163 | canvas.save();
164 | }
165 | SkXfermode mode = SkXfermode.Create(gModes[i].fMode);
166 |
167 | canvas.drawRect(bounds, fBGPaint);
168 | canvas.saveLayer(bounds, null);
169 | float dy = drawCell(canvas, mode,
170 | gAlphaValue[alpha & 1],
171 | gAlphaValue[alpha & 2]);
172 | canvas.restore();
173 |
174 | canvas.translate(0, dy * 5 / 4);
175 | }
176 | canvas.restore();
177 | canvas.restore();
178 | canvas.translate(W * 5 / 4, 0);
179 | }
180 | }
181 |
182 | // disable pdf for now, since it crashes on mac
183 | @Override
184 | protected int onGetFlags() { return kSkipPDF_Flag; }
185 | }
186 |
187 | public static GMRegistry.Factory MyFactory = new GMRegistry.Factory() {
188 | public GM apply() {
189 | return new AARectModesGM();
190 | }
191 | };
192 | }
193 |
--------------------------------------------------------------------------------
/src/test/java/skia/javacpp/gm/arithmode.java:
--------------------------------------------------------------------------------
1 | package skia.javacpp.gm;
2 |
3 | import static skia.javacpp.core.*;
4 | import static skia.javacpp.effects.*;
5 |
6 | public class arithmode {
7 | static final int WW = 100;
8 | static final int HH = 32;
9 |
10 | static SkBitmap make_bm() {
11 | SkBitmap bm = new SkBitmap();
12 | bm.setConfig(SkBitmap.kARGB_8888_Config, WW, HH);
13 | bm.allocPixels();
14 | bm.eraseColor(0);
15 | return bm;
16 | }
17 |
18 | static SkBitmap make_src() {
19 | SkBitmap bm = make_bm();
20 | SkCanvas canvas = new SkCanvas(bm);
21 | SkPaint paint = new SkPaint();
22 | SkPoint pts[] = { SkPoint.Make(0, 0), SkPoint.Make(SkIntToScalar(WW), SkIntToScalar(HH)) };
23 | int[] colors = {
24 | SK_ColorBLACK, SK_ColorGREEN, SK_ColorCYAN,
25 | SK_ColorRED, SK_ColorMAGENTA, SK_ColorWHITE
26 | };
27 | SkShader s = SkGradientShader.CreateLinear(pts, colors, null, SkShader.kClamp_TileMode);
28 | paint.setShader(s);
29 | canvas.drawPaint(paint);
30 | return bm;
31 | }
32 |
33 | static SkBitmap make_dst() {
34 | SkBitmap bm = make_bm();
35 | SkCanvas canvas = new SkCanvas(bm);
36 | SkPaint paint = new SkPaint();
37 | SkPoint pts[] = { SkPoint.Make(0, SkIntToScalar(HH)), SkPoint.Make(SkIntToScalar(WW), 0) };
38 | int[] colors = {
39 | SK_ColorBLUE, SK_ColorYELLOW, SK_ColorBLACK, SK_ColorGREEN, SK_ColorGRAY
40 | };
41 | SkShader s = SkGradientShader.CreateLinear(pts, colors, null, SkShader.kClamp_TileMode);
42 | paint.setShader(s);
43 | canvas.drawPaint(paint);
44 | return bm;
45 | }
46 |
47 | static SkBitmap make_arith(SkBitmap src, SkBitmap dst,
48 | float[] k) {
49 | SkBitmap bm = make_bm();
50 | SkCanvas canvas = new SkCanvas(bm);
51 | SkPaint paint = new SkPaint();
52 | canvas.drawBitmap(dst, 0, 0, null);
53 | SkXfermode xfer = SkArithmeticMode.Create(k[0], k[1], k[2], k[3]);
54 | paint.setXfermode(xfer);
55 | canvas.drawBitmap(src, 0, 0, paint);
56 | return bm;
57 | }
58 |
59 | static void show_k_text(SkCanvas canvas, float x, float y, float[] k) {
60 | SkPaint paint = new SkPaint();
61 | paint.setTextSize(SkIntToScalar(24));
62 | paint.setAntiAlias(true);
63 | for (int i = 0; i < 4; ++i) {
64 | String str = String.valueOf(k[i]).replaceFirst("\\.?0+$", "");
65 | float width = paint.measureText(str);
66 | canvas.drawText(str, x, y + paint.getTextSize(), paint);
67 | x += width + SkIntToScalar(10);
68 | }
69 | }
70 |
71 | public static class ArithmodeGM extends GM {
72 | @Override
73 | protected String onShortName() {
74 | return "arithmode";
75 | }
76 |
77 | @Override
78 | protected SkISize onISize() { return SkISize.Make(640, 480); }
79 |
80 | @Override
81 | protected void onDraw(SkCanvas canvas) {
82 | SkBitmap src = make_src();
83 | SkBitmap dst = make_dst();
84 |
85 | final float one = SK_Scalar1;
86 | final float[] K = {
87 | 0, 0, 0, 0,
88 | 0, 0, 0, one,
89 | 0, one, 0, 0,
90 | 0, 0, one, 0,
91 | 0, one, one, 0,
92 | 0, one, -one, 0,
93 | 0, one/2, one/2, 0,
94 | 0, one/2, one/2, one/4,
95 | 0, one/2, one/2, -one/4,
96 | one/4, one/2, one/2, 0,
97 | -one/4, one/2, one/2, 0,
98 | };
99 |
100 | int i = 0;
101 | float y = 0;
102 | float gap = SkIntToScalar(src.width() + 20);
103 | while (i < K.length) {
104 | float[] k = new float[4];
105 | System.arraycopy(K, i, k, 0, 4);
106 | float x = 0;
107 | SkBitmap res = make_arith(src, dst, k);
108 | canvas.drawBitmap(src, x, y, null);
109 | x += gap;
110 | canvas.drawBitmap(dst, x, y, null);
111 | x += gap;
112 | canvas.drawBitmap(res, x, y, null);
113 | x += gap;
114 | show_k_text(canvas, x, y, k);
115 | i += 4;
116 | y += SkIntToScalar(src.height() + 12);
117 | }
118 | }
119 | }
120 |
121 | public static GMRegistry.Factory MyFactory = new GMRegistry.Factory() {
122 | public GM apply() {
123 | return new ArithmodeGM();
124 | }
125 | };
126 | }
127 |
--------------------------------------------------------------------------------
/src/test/java/skia/javacpp/gm/bigmatrix.java:
--------------------------------------------------------------------------------
1 | package skia.javacpp.gm;
2 |
3 | import com.googlecode.javacpp.IntPointer;
4 |
5 | import static skia.javacpp.core.*;
6 |
7 | public class bigmatrix {
8 | public static class BigMatrixGM extends GM {
9 | public BigMatrixGM() {
10 | this.setBGColor(0xFF66AA99);
11 | }
12 |
13 | @Override
14 | protected String onShortName() {
15 | return "bigmatrix";
16 | }
17 |
18 | @Override
19 | protected SkISize onISize() {
20 | return make_isize(50, 50);
21 | }
22 |
23 | @Override
24 | protected void onDraw(SkCanvas canvas) {
25 | SkMatrix m = new SkMatrix();
26 | m.reset();
27 | m.setRotate(33 * SK_Scalar1);
28 | m.postScale(3000 * SK_Scalar1, 3000 * SK_Scalar1);
29 | m.postTranslate(6000 * SK_Scalar1, -5000 * SK_Scalar1);
30 | canvas.concat(m);
31 |
32 | SkPaint paint = new SkPaint();
33 | paint.setColor(SK_ColorRED);
34 | paint.setAntiAlias(true);
35 |
36 | m.invert(m);
37 |
38 | SkPath path = new SkPath();
39 |
40 | SkPoint pt = SkPoint.Make(10 * SK_Scalar1, 10 * SK_Scalar1);
41 | float small = 1 / (500 * SK_Scalar1);
42 |
43 | m.mapPoints(pt, 1);
44 | path.addCircle(pt.fX(), pt.fY(), small);
45 | canvas.drawPath(path, paint);
46 |
47 | pt.set(30 * SK_Scalar1, 10 * SK_Scalar1);
48 | m.mapPoints(pt, 1);
49 | SkRect rect = SkRect.MakeLTRB(pt.fX() - small, pt.fY() - small,
50 | pt.fX() + small, pt.fY() + small);
51 | canvas.drawRect(rect, paint);
52 |
53 | SkBitmap bmp = new SkBitmap();
54 | bmp.setConfig(SkBitmap.kARGB_8888_Config, 2, 2);
55 | bmp.allocPixels();
56 | bmp.lockPixels();
57 | IntPointer pixels = new IntPointer(bmp.getPixels());
58 | pixels.put(0, SkPackARGB32(0xFF, 0xFF, 0x00, 0x00));
59 | pixels.put(1, SkPackARGB32(0xFF, 0x00, 0xFF, 0x00));
60 | pixels.put(2, SkPackARGB32(0x80, 0x00, 0x00, 0x00));
61 | pixels.put(3, SkPackARGB32(0xFF, 0x00, 0x00, 0xFF));
62 | bmp.unlockPixels();
63 | pt.set(30 * SK_Scalar1, 30 * SK_Scalar1);
64 | m.mapPoints(pt, 1);
65 | SkShader shader = SkShader.CreateBitmapShader(
66 | bmp,
67 | SkShader.kRepeat_TileMode,
68 | SkShader.kRepeat_TileMode);
69 | SkMatrix s = new SkMatrix();
70 | s.reset();
71 | s.setScale(SK_Scalar1 / 1000, SK_Scalar1 / 1000);
72 | shader.setLocalMatrix(s);
73 | paint.setShader(shader);
74 | paint.setAntiAlias(false);
75 | paint.setFilterBitmap(true);
76 | rect.setLTRB(pt.fX() - small, pt.fY() - small,
77 | pt.fX() + small, pt.fY() + small);
78 | canvas.drawRect(rect, paint);
79 | }
80 | }
81 |
82 | public static GMRegistry.Factory MyFactory = new GMRegistry.Factory() {
83 | public GM apply() {
84 | return new BigMatrixGM();
85 | }
86 | };
87 | }
88 |
--------------------------------------------------------------------------------
/src/test/java/skia/javacpp/gm/bitmapcopy.java:
--------------------------------------------------------------------------------
1 | package skia.javacpp.gm;
2 |
3 | import static skia.javacpp.core.*;
4 |
5 | public class bitmapcopy {
6 | static final String[] gConfigNames = {
7 | "unknown config",
8 | "A1",
9 | "A8",
10 | "Index8",
11 | "565",
12 | "4444",
13 | "8888"
14 | };
15 |
16 | static int[] gConfigs = {
17 | SkBitmap.kRGB_565_Config,
18 | SkBitmap.kARGB_4444_Config,
19 | SkBitmap.kARGB_8888_Config,
20 | };
21 |
22 | static final int NUM_CONFIGS = gConfigs.length;
23 |
24 | static void draw_checks(SkCanvas canvas, int width, int height) {
25 | SkPaint paint = new SkPaint();
26 | paint.setColor(SK_ColorRED);
27 | canvas.drawRectCoords(0, 0, width / 2, height / 2, paint);
28 | paint.setColor(SK_ColorGREEN);
29 | canvas.drawRectCoords(width / 2, 0, width, height / 2, paint);
30 | paint.setColor(SK_ColorBLUE);
31 | canvas.drawRectCoords(0, height / 2, width / 2, height, paint);
32 | paint.setColor(SK_ColorYELLOW);
33 | canvas.drawRectCoords(width / 2, height / 2, width, height, paint);
34 | }
35 |
36 | public static class BitmapCopyGM extends GM {
37 | public SkBitmap[] fDst = new SkBitmap[NUM_CONFIGS];
38 | {
39 | for (int i = 0; i < fDst.length; i++) {
40 | fDst[i] = new SkBitmap();
41 | }
42 | }
43 |
44 | public BitmapCopyGM() {
45 | this.setBGColor(0xFFDDDDDD);
46 | }
47 |
48 | @Override
49 | protected String onShortName() {
50 | return "bitmapcopy";
51 | }
52 |
53 | @Override
54 | protected SkISize onISize() {
55 | return make_isize(540, 330);
56 | }
57 |
58 | @Override
59 | protected void onDraw(SkCanvas canvas) {
60 | SkPaint paint = new SkPaint();
61 | float horizMargin = (SkIntToScalar(10));
62 | float vertMargin = (SkIntToScalar(10));
63 |
64 | draw_checks(canvas, 40, 40);
65 | SkBitmap src = canvas.getDevice().accessBitmap(false);
66 |
67 | for (int i = 0; i < NUM_CONFIGS; ++i) {
68 | if (!src.deepCopyTo(fDst[i], gConfigs[i])) {
69 | src.copyTo(fDst[i], gConfigs[i]);
70 | }
71 | }
72 |
73 | canvas.clear(0xFFDDDDDD);
74 | paint.setAntiAlias(true);
75 | float width = SkIntToScalar(40);
76 | float height = SkIntToScalar(40);
77 | if (paint.getFontSpacing() > height) {
78 | height = paint.getFontSpacing();
79 | }
80 | for (int i = 0; i < NUM_CONFIGS; i++) {
81 | String name = gConfigNames[src.config()];
82 | float textWidth = paint.measureText(name);
83 | if (textWidth > width) {
84 | width = textWidth;
85 | }
86 | }
87 | float horizOffset = width + horizMargin;
88 | float vertOffset = height + vertMargin;
89 | canvas.translate(SkIntToScalar(20), SkIntToScalar(20));
90 |
91 | for (int i = 0; i < NUM_CONFIGS; i++) {
92 | canvas.save();
93 | // Draw destination config name
94 | String name = gConfigNames[fDst[i].config()];
95 | float textWidth = paint.measureText(name);
96 | float x = (width - textWidth) / 2f;
97 | float y = paint.getFontSpacing() / 2f;
98 | canvas.drawText(name, x, y, paint);
99 |
100 | // Draw destination bitmap
101 | canvas.translate(0, vertOffset);
102 | x = (width - 40) / 2f;
103 | canvas.drawBitmap(fDst[i], x, 0, paint);
104 | canvas.restore();
105 |
106 | canvas.translate(horizOffset, 0);
107 | }
108 | }
109 |
110 | @Override
111 | protected int onGetFlags() { return kSkipPicture_Flag; }
112 | }
113 |
114 | public static GMRegistry.Factory MyFactory = new GMRegistry.Factory() {
115 | public GM apply() {
116 | return new BitmapCopyGM();
117 | }
118 | };
119 | }
120 |
--------------------------------------------------------------------------------
/src/test/java/skia/javacpp/gm/bitmapfilters.java:
--------------------------------------------------------------------------------
1 | package skia.javacpp.gm;
2 |
3 | import static skia.javacpp.core.*;
4 |
5 | public class bitmapfilters {
6 | static void make_bm(SkBitmap bm) {
7 | final int[] colors = {
8 | SK_ColorRED, SK_ColorGREEN,
9 | SK_ColorBLUE, SK_ColorWHITE
10 | };
11 | int[] colorsPM = new int[4];
12 | for (int i = 0; i < colors.length; ++i) {
13 | colorsPM[i] = SkPreMultiplyColor(colors[i]);
14 | }
15 | SkColorTable ctable = new SkColorTable(colorsPM);
16 |
17 | bm.setConfig(SkBitmap.kIndex8_Config, 2, 2);
18 | bm.allocPixels(ctable);
19 |
20 | bm.getAddr8(0, 0).put((byte) 0);
21 | bm.getAddr8(1, 0).put((byte) 1);
22 | bm.getAddr8(0, 1).put((byte) 2);
23 | bm.getAddr8(1, 1).put((byte) 3);
24 | }
25 |
26 | static float draw_bm(SkCanvas canvas, SkBitmap bm,
27 | float x, float y, SkPaint paint) {
28 | canvas.drawBitmap(bm, x, y, paint);
29 | return SkIntToScalar(bm.width()) * 5/4;
30 | }
31 |
32 | static float draw_set(SkCanvas c, SkBitmap bm, float x,
33 | SkPaint p) {
34 | x += draw_bm(c, bm, x, 0, p);
35 | p.setFilterBitmap(true);
36 | x += draw_bm(c, bm, x, 0, p);
37 | p.setDither(true);
38 | return x + draw_bm(c, bm, x, 0, p);
39 | }
40 |
41 | static final String[] gConfigNames = {
42 | "unknown config",
43 | "A1",
44 | "A8",
45 | "Index8",
46 | "565",
47 | "4444",
48 | "8888"
49 | };
50 |
51 | static float draw_row(SkCanvas canvas, SkBitmap bm) {
52 | int saveCount = canvas.getSaveCount();
53 | canvas.save();
54 | try {
55 | SkPaint paint = new SkPaint();
56 | float x = 0;
57 | final int scale = 32;
58 |
59 | paint.setAntiAlias(true);
60 | final String name = gConfigNames[bm.config()];
61 | canvas.drawText(name, x, SkIntToScalar(bm.height())*scale*5/8,
62 | paint);
63 | canvas.translate(SkIntToScalar(48), 0);
64 |
65 | canvas.scale(SkIntToScalar(scale), SkIntToScalar(scale));
66 |
67 | x += draw_set(canvas, bm, 0, paint);
68 | paint.reset();
69 | paint.setAlpha(0x80);
70 | draw_set(canvas, bm, x, paint);
71 | return x * scale / 3;
72 | } finally {
73 | canvas.restoreToCount(saveCount);
74 | }
75 | }
76 |
77 | public static class FilterGM extends GM {
78 | private boolean fOnce;
79 | void init() {
80 | if (fOnce) {
81 | return;
82 | }
83 | fOnce = true;
84 | make_bm(fBM8);
85 | fBM8.copyTo(fBM4444, SkBitmap.kARGB_4444_Config);
86 | fBM8.copyTo(fBM16, SkBitmap.kRGB_565_Config);
87 | fBM8.copyTo(fBM32, SkBitmap.kARGB_8888_Config);
88 | }
89 | public SkBitmap fBM8 = new SkBitmap(), fBM4444 = new SkBitmap(), fBM16 = new SkBitmap(), fBM32 = new SkBitmap();
90 |
91 | FilterGM() {
92 | fOnce = false;
93 | this.setBGColor(0xFFDDDDDD);
94 | }
95 |
96 | @Override
97 | protected String onShortName() {
98 | return "bitmapfilters";
99 | }
100 |
101 | @Override
102 | protected SkISize onISize() {
103 | return make_isize(540, 330);
104 | }
105 |
106 | @Override
107 | protected void onDraw(SkCanvas canvas) {
108 | this.init();
109 |
110 | float x = SkIntToScalar(10);
111 | float y = SkIntToScalar(10);
112 |
113 | canvas.translate(x, y);
114 | y = draw_row(canvas, fBM8);
115 | canvas.translate(0, y);
116 | y = draw_row(canvas, fBM4444);
117 | canvas.translate(0, y);
118 | y = draw_row(canvas, fBM16);
119 | canvas.translate(0, y);
120 | draw_row(canvas, fBM32);
121 | }
122 | }
123 |
124 | public static GMRegistry.Factory MyFactory = new GMRegistry.Factory() {
125 | public GM apply() {
126 | return new FilterGM();
127 | }
128 | };
129 | }
130 |
--------------------------------------------------------------------------------
/src/test/java/skia/javacpp/gm/bitmapscroll.java:
--------------------------------------------------------------------------------
1 | package skia.javacpp.gm;
2 |
3 | import static skia.javacpp.core.*;
4 |
5 | public class bitmapscroll {
6 | /** Create a bitmap image suitable for testing SkBitmap::scrollRect().
7 | *
8 | * @param quarterWidth bitmap will be 4x this many pixels wide
9 | * @param quarterHeight bitmap will be 4x this many pixels tall
10 | * @param bitmap the bitmap data is written into this object
11 | */
12 | static void make_bitmap(int quarterWidth, int quarterHeight, SkBitmap bitmap) {
13 | SkPaint pRed = new SkPaint(), pWhite = new SkPaint(), pGreen = new SkPaint(), pBlue = new SkPaint(), pLine = new SkPaint(), pAlphaGray = new SkPaint();
14 | pRed.setColor(0xFFFF9999);
15 | pWhite.setColor(0xFFFFFFFF);
16 | pGreen.setColor(0xFF99FF99);
17 | pBlue.setColor(0xFF9999FF);
18 | pLine.setColor(0xFF000000);
19 | pLine.setStyle(SkPaint.kStroke_Style);
20 | pAlphaGray.setColor(0x66888888);
21 |
22 | // Prepare bitmap, and a canvas that draws into it.
23 | bitmap.reset();
24 | bitmap.setConfig(SkBitmap.kARGB_8888_Config,
25 | quarterWidth*4, quarterHeight*4);
26 | bitmap.allocPixels();
27 | SkCanvas canvas = new SkCanvas(bitmap);
28 |
29 | float w = SkIntToScalar(quarterWidth);
30 | float h = SkIntToScalar(quarterHeight);
31 | canvas.drawRectCoords( 0, 0, w*2, h*2, pRed);
32 | canvas.drawRectCoords(w*2, 0, w*4, h*2, pGreen);
33 | canvas.drawRectCoords( 0, h*2, w*2, h*4, pBlue);
34 | canvas.drawRectCoords(w*2, h*2, w*4, h*4, pWhite);
35 | canvas.drawRectCoords(w, h, w*3, h*3, pAlphaGray);
36 | canvas.drawLine(w*2, 0, w*2, h*4, pLine);
37 | canvas.drawLine( 0, h*2, w*4, h*2, pLine);
38 | canvas.drawRectCoords(w, h, w*3, h*3, pLine);
39 | }
40 |
41 | public static class BitmapScrollGM extends GM {
42 | private boolean fInited;
43 | private void init() {
44 | if (fInited) {
45 | return;
46 | }
47 | fInited = true;
48 | // Create the original bitmap.
49 | make_bitmap(quarterWidth, quarterHeight, origBitmap);
50 | }
51 |
52 | public BitmapScrollGM() {
53 | fInited = false;
54 | this.setBGColor(0xFFDDDDDD);
55 | }
56 |
57 | @Override
58 | protected String onShortName() {
59 | return "bitmapscroll";
60 | }
61 |
62 | @Override
63 | protected SkISize onISize() {
64 | return make_isize(800, 600);
65 | }
66 |
67 | @Override
68 | protected void onDraw(SkCanvas canvas) {
69 | this.init();
70 | SkIRect scrollCenterRegion = SkIRect.MakeXYWH(
71 | quarterWidth, quarterHeight, quarterWidth * 2 + 1, quarterHeight * 2 + 1);
72 | int x = quarterWidth;
73 | int y = quarterHeight;
74 | int xSpacing = quarterWidth * 20;
75 | int ySpacing = quarterHeight * 16;
76 |
77 | // Draw left-hand text labels.
78 | drawLabel(canvas, "scroll entire bitmap",
79 | x, y, x, y + ySpacing);
80 | drawLabel(canvas, "scroll part of bitmap",
81 | x, y + ySpacing, x, y + ySpacing*2);
82 | x += 30;
83 |
84 | // Draw various permutations of scrolled bitmaps, scrolling a bit
85 | // further each time.
86 | draw9(canvas, x, y, null, quarterWidth*1/2, quarterHeight*1/2);
87 | draw9(canvas, x, y+ySpacing, scrollCenterRegion,
88 | quarterWidth*1/2, quarterHeight*1/2);
89 | x += xSpacing;
90 | draw9(canvas, x, y, null, quarterWidth*3/2, quarterHeight*3/2);
91 | draw9(canvas, x, y+ySpacing, scrollCenterRegion,
92 | quarterWidth*3/2, quarterHeight*3/2);
93 | x += xSpacing;
94 | draw9(canvas, x, y, null, quarterWidth*5/2, quarterHeight*5/2);
95 | draw9(canvas, x, y+ySpacing, scrollCenterRegion,
96 | quarterWidth*5/2, quarterHeight*5/2);
97 | x += xSpacing;
98 | draw9(canvas, x, y, null, quarterWidth*9/2, quarterHeight*9/2);
99 | draw9(canvas, x, y+ySpacing, scrollCenterRegion,
100 | quarterWidth*9/2, quarterHeight*9/2);
101 | }
102 |
103 | void drawLabel(SkCanvas canvas, String text, int startX, int startY,
104 | int endX, int endY) {
105 | SkPaint paint = new SkPaint();
106 | paint.setColor(0xFF000000);
107 | SkPath path = new SkPath();
108 | path.moveTo(SkIntToScalar(startX), SkIntToScalar(startY));
109 | path.lineTo(SkIntToScalar(endX), SkIntToScalar(endY));
110 | canvas.drawTextOnPath(text, path, null, paint);
111 | }
112 |
113 | /** Stamp out 9 copies of origBitmap, scrolled in each direction (and
114 | * not scrolled at all).
115 | */
116 | void draw9(SkCanvas canvas, int x, int y, SkIRect subset,
117 | int scrollX, int scrollY) {
118 | for (int yMult=-1; yMult<=1; yMult++) {
119 | for (int xMult=-1; xMult<=1; xMult++) {
120 | // Figure out the (x,y) to draw this copy at
121 | float bitmapX = SkIntToScalar(
122 | x + quarterWidth * 5 * (xMult+1));
123 | float bitmapY = SkIntToScalar(
124 | y + quarterHeight * 5 * (yMult+1));
125 |
126 | // Scroll a new copy of the bitmap, and then draw it.
127 | // scrollRect() should always return true, even if it's a no-op
128 | SkBitmap scrolledBitmap = new SkBitmap();
129 | boolean copyToReturnValue = origBitmap.copyTo(
130 | scrolledBitmap, origBitmap.config());
131 | SkASSERT(copyToReturnValue);
132 | boolean scrollRectReturnValue = scrolledBitmap.scrollRect(
133 | subset, scrollX * xMult, scrollY * yMult);
134 | SkASSERT(scrollRectReturnValue);
135 | canvas.drawBitmap(scrolledBitmap, bitmapX, bitmapY);
136 | }
137 | }
138 | }
139 |
140 | private static final int quarterWidth = 10;
141 | private static final int quarterHeight = 14;
142 | private SkBitmap origBitmap = new SkBitmap();
143 | }
144 |
145 | public static GMRegistry.Factory MyFactory = new GMRegistry.Factory() {
146 | public GM apply() {
147 | return new BitmapScrollGM();
148 | }
149 | };
150 | }
151 |
--------------------------------------------------------------------------------
/src/test/java/skia/javacpp/gm/blurs.java:
--------------------------------------------------------------------------------
1 | package skia.javacpp.gm;
2 |
3 | import static skia.javacpp.core.*;
4 | import static skia.javacpp.effects.*;
5 |
6 | public class blurs {
7 | public static class BlursGM extends GM {
8 | public BlursGM() {
9 | this.setBGColor(0xFFDDDDDD);
10 | }
11 |
12 | @Override
13 | protected String onShortName() {
14 | return "blurs";
15 | }
16 |
17 | @Override
18 | protected SkISize onISize() {
19 | return make_isize(700, 500);
20 | }
21 |
22 | @Override
23 | protected void onDraw(SkCanvas canvas) {
24 | int NONE = -999;
25 | class Rec {
26 | int fStyle;
27 | int fCx, fCy;
28 | public Rec(int style, int cx, int cy) {
29 | fStyle = style;
30 | fCx = cx;
31 | fCy = cy;
32 | }
33 | }
34 | final Rec[] gRecs = {
35 | new Rec(NONE, 0, 0),
36 | new Rec(SkBlurMaskFilter.kInner_BlurStyle, -1, 0),
37 | new Rec(SkBlurMaskFilter.kNormal_BlurStyle, 0, 1),
38 | new Rec(SkBlurMaskFilter.kSolid_BlurStyle, 0, -1),
39 | new Rec(SkBlurMaskFilter.kOuter_BlurStyle, 1, 0),
40 | };
41 |
42 | SkPaint paint = new SkPaint();
43 | paint.setAntiAlias(true);
44 | paint.setTextSize(SkIntToScalar(25));
45 | canvas.translate(SkIntToScalar(-40), SkIntToScalar(0));
46 |
47 | int flags = SkBlurMaskFilter.kNone_BlurFlag;
48 | for (int j = 0; j < 2; j++) {
49 | canvas.save();
50 | paint.setColor(SK_ColorBLUE);
51 | for (int i = 0; i < gRecs.length; i++) {
52 | if (gRecs[i].fStyle != NONE) {
53 | SkMaskFilter mf = SkBlurMaskFilter.Create(
54 | SkIntToScalar(20), gRecs[i].fStyle, flags
55 | );
56 | paint.setMaskFilter(mf);
57 | } else {
58 | paint.setMaskFilter(null);
59 | }
60 | canvas.drawCircle(SkIntToScalar(200 + gRecs[i].fCx*100)
61 | , SkIntToScalar(200 + gRecs[i].fCy*100)
62 | , SkIntToScalar(50)
63 | , paint);
64 | }
65 | // draw text
66 | {
67 | SkMaskFilter mf = SkBlurMaskFilter.Create(
68 | SkIntToScalar(4)
69 | , SkBlurMaskFilter.kNormal_BlurStyle
70 | , flags
71 | );
72 | paint.setMaskFilter(mf);
73 | float x = SkIntToScalar(70);
74 | float y = SkIntToScalar(400);
75 | paint.setColor(SK_ColorBLACK);
76 | canvas.drawText("Hamburgefons Style", x, y, paint);
77 | canvas.drawText("Hamburgefons Style", x, y + SkIntToScalar(50), paint);
78 | paint.setMaskFilter(null);
79 | paint.setColor(SK_ColorWHITE);
80 | x -= SkIntToScalar(2);
81 | y -= SkIntToScalar(2);
82 | canvas.drawText("Hamburgefons Style", x, y, paint);
83 | }
84 | canvas.restore();
85 | flags = SkBlurMaskFilter.kHighQuality_BlurFlag;
86 | canvas.translate(SkIntToScalar(350), SkIntToScalar(0));
87 | }
88 | }
89 | }
90 |
91 | public static GMRegistry.Factory MyFactory = new GMRegistry.Factory() {
92 | public GM apply() {
93 | return new BlursGM();
94 | }
95 | };
96 | }
97 |
--------------------------------------------------------------------------------
/src/test/java/skia/javacpp/gm/cmykjpeg.java:
--------------------------------------------------------------------------------
1 | package skia.javacpp.gm;
2 |
3 | import static skia.javacpp.core.*;
4 | import static skia.javacpp.images.*;
5 |
6 | public class cmykjpeg {
7 | public static class CMYKJpegGM extends GM {
8 | public CMYKJpegGM() {
9 |
10 | // parameters to the "decode" call
11 | boolean dither = false;
12 | int prefConfig = SkBitmap.kARGB_8888_Config;
13 |
14 | String filename = gResourcePath;
15 | if (!filename.endsWith("/") && !filename.endsWith("\\")) {
16 | filename += "/";
17 | }
18 |
19 | filename += "CMYK.jpg";
20 |
21 | SkFILEStream stream = new SkFILEStream(filename);
22 | SkImageDecoder codec = SkImageDecoder.Factory(stream);
23 | if (codec != null) {
24 | stream.rewind();
25 | codec.setDitherImage(dither);
26 | codec.decode(stream, fBitmap, prefConfig,
27 | SkImageDecoder.kDecodePixels_Mode);
28 | }
29 | }
30 |
31 | @Override
32 | protected String onShortName() {
33 | return "cmykjpeg";
34 | }
35 |
36 | @Override
37 | protected SkISize onISize() {
38 | return make_isize(640, 480);
39 | }
40 |
41 | @Override
42 | protected void onDraw(SkCanvas canvas) {
43 | canvas.translate(20*SK_Scalar1, 20*SK_Scalar1);
44 | canvas.drawBitmap(fBitmap, 0, 0);
45 | }
46 |
47 | private SkBitmap fBitmap = new SkBitmap();
48 | }
49 |
50 | public static GMRegistry.Factory MyFactory = new GMRegistry.Factory() {
51 | public GM apply() {
52 | return new CMYKJpegGM();
53 | }
54 | };
55 | }
56 |
--------------------------------------------------------------------------------
/src/test/java/skia/javacpp/gm/colormatrix.java:
--------------------------------------------------------------------------------
1 | package skia.javacpp.gm;
2 |
3 | import static skia.javacpp.core.*;
4 | import static skia.javacpp.effects.*;
5 |
6 | public class colormatrix {
7 | static final int WIDTH = 500;
8 | static final int HEIGHT = 500;
9 |
10 | static class SkOnce {
11 | public SkOnce() { fOnce = false; };
12 |
13 | public boolean once() {
14 | if (fOnce) {
15 | return false;
16 | }
17 | fOnce = true;
18 | return true;
19 | }
20 |
21 | private boolean fOnce;
22 | };
23 |
24 | static void setColorMatrix(SkPaint paint, SkColorMatrix matrix) {
25 | paint.setColorFilter(new SkColorMatrixFilter(matrix));
26 | }
27 |
28 | static void setArray(SkPaint paint, float[] array) {
29 | paint.setColorFilter(new SkColorMatrixFilter(array));
30 | }
31 |
32 | public static class ColorMatrixGM extends GM {
33 | private SkOnce fOnce = new SkOnce();
34 | private void init() {
35 | if (fOnce.once()) {
36 | fBitmap = createBitmap(64, 64);
37 | }
38 | }
39 |
40 | public ColorMatrixGM() {
41 | this.setBGColor(0xFF808080);
42 | }
43 |
44 | @Override
45 | protected String onShortName() {
46 | return "colormatrix";
47 | }
48 |
49 | @Override
50 | protected SkISize onISize() {
51 | return make_isize(WIDTH, HEIGHT);
52 | }
53 |
54 | SkBitmap createBitmap(int width, int height) {
55 | SkBitmap bm = new SkBitmap();
56 | bm.setConfig(SkBitmap.kARGB_8888_Config, width, height);
57 | bm.allocPixels();
58 | SkCanvas canvas = new SkCanvas(bm);
59 | canvas.clear(0x0);
60 | for (int y = 0; y < height; ++y) {
61 | for (int x = 0; x < width; ++x) {
62 | SkPaint paint = new SkPaint();
63 | paint.setColor(SkColorSetARGB(255, x * 255 / width, y * 255 / height, 0));
64 | canvas.drawRect(SkRect.MakeXYWH(x, y, 1, 1), paint);
65 | }
66 | }
67 | return bm;
68 | }
69 |
70 | @Override
71 | protected void onDraw(SkCanvas canvas) {
72 | this.init();
73 |
74 | SkPaint paint = new SkPaint();
75 | SkColorMatrix matrix = new SkColorMatrix();
76 |
77 | matrix.setIdentity();
78 | setColorMatrix(paint, matrix);
79 | canvas.drawBitmap(fBitmap, 0, 0, paint);
80 |
81 | matrix.setRotate(SkColorMatrix.kR_Axis, 90);
82 | setColorMatrix(paint, matrix);
83 | canvas.drawBitmap(fBitmap, 80, 0, paint);
84 |
85 | matrix.setRotate(SkColorMatrix.kG_Axis, 90);
86 | setColorMatrix(paint, matrix);
87 | canvas.drawBitmap(fBitmap, 160, 0, paint);
88 |
89 | matrix.setRotate(SkColorMatrix.kB_Axis, 90);
90 | setColorMatrix(paint, matrix);
91 | canvas.drawBitmap(fBitmap, 240, 0, paint);
92 |
93 | matrix.setSaturation(SkFloatToScalar(0.0f));
94 | setColorMatrix(paint, matrix);
95 | canvas.drawBitmap(fBitmap, 0, 80, paint);
96 |
97 | matrix.setSaturation(SkFloatToScalar(0.5f));
98 | setColorMatrix(paint, matrix);
99 | canvas.drawBitmap(fBitmap, 80, 80, paint);
100 |
101 | matrix.setSaturation(SkFloatToScalar(1.0f));
102 | setColorMatrix(paint, matrix);
103 | canvas.drawBitmap(fBitmap, 160, 80, paint);
104 |
105 | matrix.setSaturation(SkFloatToScalar(2.0f));
106 | setColorMatrix(paint, matrix);
107 | canvas.drawBitmap(fBitmap, 240, 80, paint);
108 |
109 | matrix.setRGB2YUV();
110 | setColorMatrix(paint, matrix);
111 | canvas.drawBitmap(fBitmap, 0, 160, paint);
112 |
113 | matrix.setYUV2RGB();
114 | setColorMatrix(paint, matrix);
115 | canvas.drawBitmap(fBitmap, 80, 160, paint);
116 |
117 | float s1 = SK_Scalar1;
118 | float s255 = SkIntToScalar(255);
119 | // Move red into alpha, set color to white
120 | float[] data = {
121 | 0, 0, 0, 0, s255,
122 | 0, 0, 0, 0, s255,
123 | 0, 0, 0, 0, s255,
124 | s1, 0, 0, 0, 0,
125 | };
126 |
127 | setArray(paint, data);
128 | canvas.drawBitmap(fBitmap, 160, 160, paint);
129 | }
130 |
131 | private SkBitmap fBitmap;
132 | }
133 |
134 | public static GMRegistry.Factory MyFactory = new GMRegistry.Factory() {
135 | public GM apply() {
136 | return new ColorMatrixGM();
137 | }
138 | };
139 | }
140 |
--------------------------------------------------------------------------------
/src/test/java/skia/javacpp/gm/complexclip.java:
--------------------------------------------------------------------------------
1 | package skia.javacpp.gm;
2 |
3 | import static skia.javacpp.core.*;
4 |
5 | public class complexclip {
6 | static final int gPathColor = SK_ColorBLACK;
7 | static final int gClipAColor = SK_ColorBLUE;
8 | static final int gClipBColor = SK_ColorRED;
9 |
10 | public static class ComplexClipGM extends GM {
11 | private boolean fDoAAClip;
12 | public ComplexClipGM(boolean aaclip) {
13 | fDoAAClip = aaclip;
14 | this.setBGColor(0xFFDDDDDD);
15 | // this->setBGColor(SkColorSetRGB(0xB0,0xDD,0xB0));
16 | }
17 |
18 | @Override
19 | protected String onShortName() {
20 | return String.format("complexclip_%s", fDoAAClip ? "aa" : "bw");
21 | }
22 |
23 | @Override
24 | protected SkISize onISize() { return make_isize(970, 780); }
25 |
26 | @Override
27 | protected void onDraw(SkCanvas canvas) {
28 | SkPath path = new SkPath();
29 | path.moveTo(SkIntToScalar(0), SkIntToScalar(50));
30 | path.quadTo(SkIntToScalar(0), SkIntToScalar(0), SkIntToScalar(50), SkIntToScalar(0));
31 | path.lineTo(SkIntToScalar(175), SkIntToScalar(0));
32 | path.quadTo(SkIntToScalar(200), SkIntToScalar(0), SkIntToScalar(200), SkIntToScalar(25));
33 | path.lineTo(SkIntToScalar(200), SkIntToScalar(150));
34 | path.quadTo(SkIntToScalar(200), SkIntToScalar(200), SkIntToScalar(150), SkIntToScalar(200));
35 | path.lineTo(SkIntToScalar(0), SkIntToScalar(200));
36 | path.close();
37 | path.moveTo(SkIntToScalar(50), SkIntToScalar(50));
38 | path.lineTo(SkIntToScalar(150), SkIntToScalar(50));
39 | path.lineTo(SkIntToScalar(150), SkIntToScalar(125));
40 | path.quadTo(SkIntToScalar(150), SkIntToScalar(150), SkIntToScalar(125), SkIntToScalar(150));
41 | path.lineTo(SkIntToScalar(50), SkIntToScalar(150));
42 | path.close();
43 | path.setFillType(SkPath.kEvenOdd_FillType);
44 | SkPaint pathPaint = new SkPaint();
45 | pathPaint.setAntiAlias(true);
46 | pathPaint.setColor(gPathColor);
47 |
48 | SkPath clipA = new SkPath();
49 | clipA.moveTo(SkIntToScalar(10), SkIntToScalar(20));
50 | clipA.lineTo(SkIntToScalar(165), SkIntToScalar(22));
51 | clipA.lineTo(SkIntToScalar(70), SkIntToScalar(105));
52 | clipA.lineTo(SkIntToScalar(165), SkIntToScalar(177));
53 | clipA.lineTo(SkIntToScalar(-5), SkIntToScalar(180));
54 | clipA.close();
55 |
56 | SkPath clipB = new SkPath();
57 | clipB.moveTo(SkIntToScalar(40), SkIntToScalar(10));
58 | clipB.lineTo(SkIntToScalar(190), SkIntToScalar(15));
59 | clipB.lineTo(SkIntToScalar(195), SkIntToScalar(190));
60 | clipB.lineTo(SkIntToScalar(40), SkIntToScalar(185));
61 | clipB.lineTo(SkIntToScalar(155), SkIntToScalar(100));
62 | clipB.close();
63 |
64 | SkPaint paint = new SkPaint();
65 | paint.setAntiAlias(true);
66 | paint.setTextSize(SkIntToScalar(20));
67 |
68 | class Op {
69 | int fOp;
70 | String fName;
71 | public Op(int op, String name) {
72 | fOp = op;
73 | fName = name;
74 | }
75 | };
76 | final Op[] gOps = { //extra spaces in names for measureText
77 | new Op(SkRegion.kIntersect_Op, "Isect "),
78 | new Op(SkRegion.kDifference_Op, "Diff "),
79 | new Op(SkRegion.kUnion_Op, "Union "),
80 | new Op(SkRegion.kXOR_Op, "Xor "),
81 | new Op(SkRegion.kReverseDifference_Op, "RDiff ")
82 | };
83 |
84 | canvas.translate(SkIntToScalar(20), SkIntToScalar(20));
85 | canvas.scale(3 * SK_Scalar1 / 4, 3 * SK_Scalar1 / 4);
86 |
87 | for (int invBits = 0; invBits < 4; ++invBits) {
88 | canvas.save();
89 | for (int op = 0; op < gOps.length; ++op) {
90 | this.drawHairlines(canvas, path, clipA, clipB);
91 |
92 | boolean doInvA = SkToBool(invBits & 1);
93 | boolean doInvB = SkToBool(invBits & 2);
94 | canvas.save();
95 | // set clip
96 | clipA.setFillType(doInvA ? SkPath.kInverseEvenOdd_FillType :
97 | SkPath.kEvenOdd_FillType);
98 | clipB.setFillType(doInvB ? SkPath.kInverseEvenOdd_FillType :
99 | SkPath.kEvenOdd_FillType);
100 | canvas.clipPath(clipA, SkRegion.kIntersect_Op, fDoAAClip);
101 | canvas.clipPath(clipB, gOps[op].fOp, fDoAAClip);
102 |
103 | // draw path clipped
104 | canvas.drawPath(path, pathPaint);
105 | canvas.restore();
106 |
107 |
108 | float txtX = SkIntToScalar(45);
109 | paint.setColor(gClipAColor);
110 | final String aTxt = doInvA ? "InvA " : "A ";
111 | canvas.drawText(aTxt, txtX, SkIntToScalar(220), paint);
112 | txtX += paint.measureText(aTxt);
113 | paint.setColor(SK_ColorBLACK);
114 | canvas.drawText(gOps[op].fName,
115 | txtX, SkIntToScalar(220), paint);
116 | txtX += paint.measureText(gOps[op].fName);
117 | paint.setColor(gClipBColor);
118 | final String bTxt = doInvB ? "InvB " : "B ";
119 | canvas.drawText(bTxt, txtX, SkIntToScalar(220), paint);
120 |
121 | canvas.translate(SkIntToScalar(250),0);
122 | }
123 | canvas.restore();
124 | canvas.translate(0, SkIntToScalar(250));
125 | }
126 | }
127 | private void drawHairlines(SkCanvas canvas, SkPath path,
128 | SkPath clipA, SkPath clipB) {
129 | SkPaint paint = new SkPaint();
130 | paint.setAntiAlias(true);
131 | paint.setStyle(SkPaint.kStroke_Style);
132 | final int fade = 0x33;
133 |
134 | // draw path in hairline
135 | paint.setColor(gPathColor); paint.setAlpha(fade);
136 | canvas.drawPath(path, paint);
137 |
138 | // draw clips in hair line
139 | paint.setColor(gClipAColor); paint.setAlpha(fade);
140 | canvas.drawPath(clipA, paint);
141 | paint.setColor(gClipBColor); paint.setAlpha(fade);
142 | canvas.drawPath(clipB, paint);
143 | }
144 | }
145 |
146 | public static GMRegistry.Factory gFact0 = new GMRegistry.Factory() {
147 | public GM apply() {
148 | return new ComplexClipGM(false);
149 | }
150 | };
151 |
152 | public static GMRegistry.Factory gFact1 = new GMRegistry.Factory() {
153 | public GM apply() {
154 | return new ComplexClipGM(true);
155 | }
156 | };
157 | }
158 |
--------------------------------------------------------------------------------
/src/test/java/skia/javacpp/gm/complexclip2.java:
--------------------------------------------------------------------------------
1 | package skia.javacpp.gm;
2 |
3 | import static skia.javacpp.core.*;
4 |
5 | public class complexclip2 {
6 | public static class ComplexClip2GM extends GM {
7 | public ComplexClip2GM() {
8 | this.setBGColor(SkColorSetRGB(0xDD,0xA0,0xDD));
9 |
10 | float xA = 0 * SK_Scalar1;
11 | float xB = 10 * SK_Scalar1;
12 | float xC = 20 * SK_Scalar1;
13 | float xD = 30 * SK_Scalar1;
14 | float xE = 40 * SK_Scalar1;
15 | float xF = 50 * SK_Scalar1;
16 |
17 | float yA = 0 * SK_Scalar1;
18 | float yB = 10 * SK_Scalar1;
19 | float yC = 20 * SK_Scalar1;
20 | float yD = 30 * SK_Scalar1;
21 | float yE = 40 * SK_Scalar1;
22 | float yF = 50 * SK_Scalar1;
23 |
24 | fWidth = xF - xA;
25 | fHeight = yF - yA;
26 |
27 | fRects[0].set(xB, yB, xE, yE);
28 | fRectColors[0] = SK_ColorRED;
29 |
30 | fRects[1].set(xA, yA, xD, yD);
31 | fRectColors[1] = SK_ColorGREEN;
32 |
33 | fRects[2].set(xC, yA, xF, yD);
34 | fRectColors[2] = SK_ColorBLUE;
35 |
36 | fRects[3].set(xA, yC, xD, yF);
37 | fRectColors[3] = SK_ColorYELLOW;
38 |
39 | fRects[4].set(xC, yC, xF, yF);
40 | fRectColors[4] = SK_ColorCYAN;
41 |
42 | fTotalWidth = kCols * fWidth + SK_Scalar1 * (kCols + 1) * kPadX;
43 | fTotalHeight = kRows * fHeight + SK_Scalar1 * (kRows + 1) * kPadY;
44 |
45 | int[] ops = {
46 | SkRegion.kDifference_Op,
47 | SkRegion.kIntersect_Op,
48 | SkRegion.kUnion_Op,
49 | SkRegion.kXOR_Op,
50 | SkRegion.kReverseDifference_Op,
51 | SkRegion.kReplace_Op,
52 | };
53 |
54 | SkRandom r = new SkRandom();
55 | for (int i = 0; i < kRows; ++i) {
56 | for (int j = 0; j < kCols; ++j) {
57 | for (int k = 0; k < 5; ++k) {
58 | int x = r.nextU();
59 | if (x < 0) {
60 | x = (Integer.MAX_VALUE % ops.length) + Math.abs(x);
61 | x = ops.length - (x % ops.length) - 1;
62 | } else {
63 | x = x % ops.length;
64 | }
65 | fOps[j*kRows+i][k] = ops[x];
66 | }
67 | }
68 | }
69 | }
70 |
71 | protected static final int kRows = 5;
72 | protected static final int kCols = 5;
73 | protected static final int kPadX = 20;
74 | protected static final int kPadY = 20;
75 |
76 | @Override
77 | protected String onShortName() {
78 | return "complexclip2";
79 | }
80 |
81 | @Override
82 | protected SkISize onISize() {
83 | return make_isize(SkScalarRoundToInt(fTotalWidth),
84 | SkScalarRoundToInt(fTotalHeight));
85 | }
86 |
87 | @Override
88 | protected void onDraw(SkCanvas canvas) {
89 | SkPaint rectPaint = new SkPaint();
90 | rectPaint.setStyle(SkPaint.kStroke_Style);
91 | rectPaint.setStrokeWidth(-1);
92 |
93 | SkPaint fillPaint = new SkPaint();
94 | fillPaint.setColor(SkColorSetRGB(0xA0,0xDD,0xA0));
95 |
96 | for (int i = 0; i < kRows; ++i) {
97 | for (int j = 0; j < kCols; ++j) {
98 | canvas.save();
99 | canvas.translate(kPadX * SK_Scalar1 + (fWidth + kPadX * SK_Scalar1)*j,
100 | kPadY * SK_Scalar1 + (fHeight + kPadY * SK_Scalar1)*i);
101 | canvas.save();
102 | for (int k = 0; k < 5; ++k) {
103 | canvas.clipRect(fRects[k], fOps[j*kRows+i][k]);
104 | }
105 | canvas.drawRect(SkRect.MakeWH(fWidth, fHeight), fillPaint);
106 | canvas.restore();
107 | for (int k = 0; k < 5; ++k) {
108 | rectPaint.setColor(fRectColors[k]);
109 | canvas.drawRect(fRects[k], rectPaint);
110 | }
111 | canvas.restore();
112 | }
113 | }
114 | }
115 | private SkRect[] fRects = new SkRect[5];
116 | {
117 | for (int i = 0; i < fRects.length; i++) {
118 | fRects[i] = new SkRect();
119 | }
120 | }
121 | private int[] fRectColors = new int[5];
122 | private int[][] fOps = new int[kRows * kCols][5];
123 | private float fWidth;
124 | private float fHeight;
125 | private float fTotalWidth;
126 | private float fTotalHeight;
127 | }
128 |
129 | public static GMRegistry.Factory MyFactory = new GMRegistry.Factory() {
130 | public GM apply() {
131 | return new ComplexClip2GM();
132 | }
133 | };
134 | }
135 |
--------------------------------------------------------------------------------
/src/test/java/skia/javacpp/gm/convexpaths.java:
--------------------------------------------------------------------------------
1 | package skia.javacpp.gm;
2 |
3 | import java.util.LinkedList;
4 |
5 | import static skia.javacpp.core.*;
6 |
7 | public class convexpaths {
8 | static class SkOnce {
9 | public SkOnce() { fDidOnce = false; }
10 |
11 | public boolean needToDo() { return !fDidOnce; }
12 | public boolean alreadyDone() { return fDidOnce; }
13 | public void accomplished() {
14 | SkASSERT(!fDidOnce);
15 | fDidOnce = true;
16 | }
17 |
18 | private boolean fDidOnce;
19 | };
20 |
21 | public static class ConvexPathsGM extends GM {
22 | SkOnce fOnce = new SkOnce();
23 | public ConvexPathsGM() {
24 | this.setBGColor(0xFF000000);
25 | }
26 |
27 | @Override
28 | protected String onShortName() {
29 | return "convexpaths";
30 | }
31 |
32 | @Override
33 | protected SkISize onISize() {
34 | return make_isize(1200, 900);
35 | }
36 |
37 | void makePaths() {
38 | if (fOnce.alreadyDone()) {
39 | return;
40 | }
41 | fOnce.accomplished();
42 |
43 | // CW
44 | fPaths.add(new SkPath());fPaths.getLast().moveTo(0, 0);
45 | fPaths.getLast().quadTo(50 * SK_Scalar1, 100 * SK_Scalar1,
46 | 0, 100 * SK_Scalar1);
47 | fPaths.getLast().lineTo(0, 0);
48 |
49 | // CCW
50 | fPaths.add(new SkPath());fPaths.getLast().moveTo(0, 0);
51 | fPaths.getLast().lineTo(0, 100 * SK_Scalar1);
52 | fPaths.getLast().quadTo(50 * SK_Scalar1, 100 * SK_Scalar1,
53 | 0, 0);
54 |
55 | // CW
56 | fPaths.add(new SkPath());fPaths.getLast().moveTo(0, 50 * SK_Scalar1);
57 | fPaths.getLast().quadTo(50 * SK_Scalar1, 0,
58 | 100 * SK_Scalar1, 50 * SK_Scalar1);
59 | fPaths.getLast().quadTo(50 * SK_Scalar1, 100 * SK_Scalar1,
60 | 0, 50 * SK_Scalar1);
61 |
62 | // CCW
63 | fPaths.add(new SkPath());fPaths.getLast().moveTo(0, 50 * SK_Scalar1);
64 | fPaths.getLast().quadTo(50 * SK_Scalar1, 100 * SK_Scalar1,
65 | 100 * SK_Scalar1, 50 * SK_Scalar1);
66 | fPaths.getLast().quadTo(50 * SK_Scalar1, 0,
67 | 0, 50 * SK_Scalar1);
68 |
69 | fPaths.add(new SkPath());fPaths.getLast().addRect(0, 0,
70 | 100 * SK_Scalar1, 100 * SK_Scalar1,
71 | SkPath.kCW_Direction);
72 |
73 | fPaths.add(new SkPath());fPaths.getLast().addRect(0, 0,
74 | 100 * SK_Scalar1, 100 * SK_Scalar1,
75 | SkPath.kCCW_Direction);
76 |
77 | fPaths.add(new SkPath());fPaths.getLast().addCircle(50 * SK_Scalar1, 50 * SK_Scalar1,
78 | 50 * SK_Scalar1, SkPath.kCW_Direction);
79 |
80 | fPaths.add(new SkPath());fPaths.getLast().addCircle(50 * SK_Scalar1, 50 * SK_Scalar1,
81 | 40 * SK_Scalar1, SkPath.kCCW_Direction);
82 |
83 | fPaths.add(new SkPath());fPaths.getLast().addOval(SkRect.MakeXYWH(0, 0,
84 | 50 * SK_Scalar1,
85 | 100 * SK_Scalar1),
86 | SkPath.kCW_Direction);
87 |
88 | fPaths.add(new SkPath());fPaths.getLast().addOval(SkRect.MakeXYWH(0, 0,
89 | 100 * SK_Scalar1,
90 | 50 * SK_Scalar1),
91 | SkPath.kCCW_Direction);
92 |
93 | fPaths.add(new SkPath());fPaths.getLast().addOval(SkRect.MakeXYWH(0, 0,
94 | 100 * SK_Scalar1,
95 | 5 * SK_Scalar1),
96 | SkPath.kCCW_Direction);
97 |
98 | fPaths.add(new SkPath());fPaths.getLast().addOval(SkRect.MakeXYWH(0, 0,
99 | SK_Scalar1,
100 | 100 * SK_Scalar1),
101 | SkPath.kCCW_Direction);
102 |
103 | fPaths.add(new SkPath());fPaths.getLast().addRoundRect(SkRect.MakeXYWH(0, 0,
104 | SK_Scalar1 * 100,
105 | SK_Scalar1 * 100),
106 | 40 * SK_Scalar1, 20 * SK_Scalar1,
107 | SkPath.kCW_Direction);
108 |
109 | fPaths.add(new SkPath());fPaths.getLast().addRoundRect(SkRect.MakeXYWH(0, 0,
110 | SK_Scalar1 * 100,
111 | SK_Scalar1 * 100),
112 | 20 * SK_Scalar1, 40 * SK_Scalar1,
113 | SkPath.kCCW_Direction);
114 |
115 | // shallow diagonals
116 | fPaths.add(new SkPath());fPaths.getLast().lineTo(100 * SK_Scalar1, SK_Scalar1);
117 | fPaths.getLast().lineTo(98 * SK_Scalar1, 100 * SK_Scalar1);
118 | fPaths.getLast().lineTo(3 * SK_Scalar1, 96 * SK_Scalar1);
119 |
120 | /*
121 | It turns out arcTos are not automatically marked as convex and they
122 | may in fact be ever so slightly concave.
123 | fPaths.push_back().arcTo(SkRect::MakeXYWH(0, 0,
124 | 50 * SK_Scalar1,
125 | 100 * SK_Scalar1),
126 | 25 * SK_Scalar1, 130 * SK_Scalar1, false);
127 | */
128 |
129 | // cubics
130 | fPaths.add(new SkPath());fPaths.getLast().cubicTo( 1 * SK_Scalar1, 1 * SK_Scalar1,
131 | 10 * SK_Scalar1, 90 * SK_Scalar1,
132 | 0 * SK_Scalar1, 100 * SK_Scalar1);
133 | fPaths.add(new SkPath());fPaths.getLast().cubicTo(100 * SK_Scalar1, 50 * SK_Scalar1,
134 | 20 * SK_Scalar1, 100 * SK_Scalar1,
135 | 0 * SK_Scalar1, 0 * SK_Scalar1);
136 |
137 | // triangle where one edge is a degenerate quad
138 | fPaths.add(new SkPath());fPaths.getLast().moveTo(SkFloatToScalar(8.59375f), 45 * SK_Scalar1);
139 | fPaths.getLast().quadTo(SkFloatToScalar(16.9921875f), 45 * SK_Scalar1,
140 | SkFloatToScalar(31.25f), 45 * SK_Scalar1);
141 | fPaths.getLast().lineTo(100 * SK_Scalar1, 100 * SK_Scalar1);
142 | fPaths.getLast().lineTo(SkFloatToScalar(8.59375f), 45 * SK_Scalar1);
143 |
144 | // point degenerate
145 | fPaths.add(new SkPath());fPaths.getLast().moveTo(50 * SK_Scalar1, 50 * SK_Scalar1);
146 | fPaths.getLast().lineTo(50 * SK_Scalar1, 50 * SK_Scalar1);
147 |
148 | fPaths.add(new SkPath());fPaths.getLast().moveTo(50 * SK_Scalar1, 50 * SK_Scalar1);
149 | fPaths.getLast().quadTo(50 * SK_Scalar1, 50 * SK_Scalar1,
150 | 50 * SK_Scalar1, 50 * SK_Scalar1);
151 | fPaths.add(new SkPath());fPaths.getLast().moveTo(50 * SK_Scalar1, 50 * SK_Scalar1);
152 | fPaths.getLast().cubicTo(50 * SK_Scalar1, 50 * SK_Scalar1,
153 | 50 * SK_Scalar1, 50 * SK_Scalar1,
154 | 50 * SK_Scalar1, 50 * SK_Scalar1);
155 |
156 | // moveTo only paths
157 | fPaths.add(new SkPath());fPaths.getLast().moveTo(0, 0);
158 | fPaths.getLast().moveTo(0, 0);
159 | fPaths.getLast().moveTo(SK_Scalar1, SK_Scalar1);
160 | fPaths.getLast().moveTo(SK_Scalar1, SK_Scalar1);
161 | fPaths.getLast().moveTo(10 * SK_Scalar1, 10 * SK_Scalar1);
162 |
163 | fPaths.add(new SkPath());fPaths.getLast().moveTo(0, 0);
164 | fPaths.getLast().moveTo(0, 0);
165 |
166 | // line degenerate
167 | fPaths.add(new SkPath());fPaths.getLast().lineTo(100 * SK_Scalar1, 100 * SK_Scalar1);
168 | fPaths.add(new SkPath());fPaths.getLast().quadTo(100 * SK_Scalar1, 100 * SK_Scalar1, 0, 0);
169 | fPaths.add(new SkPath());fPaths.getLast().quadTo(100 * SK_Scalar1, 100 * SK_Scalar1,
170 | 50 * SK_Scalar1, 50 * SK_Scalar1);
171 | fPaths.add(new SkPath());fPaths.getLast().quadTo(50 * SK_Scalar1, 50 * SK_Scalar1,
172 | 100 * SK_Scalar1, 100 * SK_Scalar1);
173 | fPaths.add(new SkPath());fPaths.getLast().cubicTo(0, 0,
174 | 0, 0,
175 | 100 * SK_Scalar1, 100 * SK_Scalar1);
176 |
177 | // small circle. This is listed last so that it has device coords far
178 | // from the origin (small area relative to x,y values).
179 | fPaths.add(new SkPath());fPaths.getLast().addCircle(0, 0, SkFloatToScalar(0.8f));
180 | }
181 |
182 | @Override
183 | protected void onDraw(SkCanvas canvas) {
184 | this.makePaths();
185 |
186 | SkPaint paint = new SkPaint();
187 | paint.setAntiAlias(true);
188 | SkRandom rand = new SkRandom();
189 | canvas.translate(20 * SK_Scalar1, 20 * SK_Scalar1);
190 | for (int i = 0; i < fPaths.size(); ++i) {
191 | canvas.save();
192 | // position the path, and make it at off-integer coords.
193 | canvas.translate(SK_Scalar1 * 200 * (i % 5) + SK_Scalar1 / 4,
194 | SK_Scalar1 * 200 * (i / 5) + 3 * SK_Scalar1 / 4);
195 | int color = rand.nextU();
196 | color |= 0xff000000;
197 | paint.setColor(color);
198 | SkASSERT(fPaths.get(i).isConvex());
199 | canvas.drawPath(fPaths.get(i), paint);
200 | canvas.restore();
201 | }
202 | }
203 |
204 | private LinkedList fPaths = new LinkedList();
205 | }
206 |
207 | public static GMRegistry.Factory MyFactory = new GMRegistry.Factory() {
208 | public GM apply() {
209 | return new ConvexPathsGM();
210 | }
211 | };
212 | }
213 |
--------------------------------------------------------------------------------
/src/test/java/skia/javacpp/gm/drawbitmaprect.java:
--------------------------------------------------------------------------------
1 | package skia.javacpp.gm;
2 |
3 | import static skia.javacpp.core.*;
4 | import static skia.javacpp.effects.*;
5 |
6 | public class drawbitmaprect {
7 | static void makebm(SkBitmap bm, int config, int w, int h) {
8 | bm.setConfig(config, w, h);
9 | bm.allocPixels();
10 | bm.eraseColor(0);
11 |
12 | SkCanvas canvas = new SkCanvas(bm);
13 |
14 | float wScalar = SkIntToScalar(w);
15 | float hScalar = SkIntToScalar(h);
16 |
17 | SkPoint pt = SkPoint.Make(wScalar / 2, hScalar / 2);
18 |
19 | float radius = 4 * SkMaxScalar(wScalar, hScalar);
20 |
21 | int[] colors = { SK_ColorRED, SK_ColorYELLOW,
22 | SK_ColorGREEN, SK_ColorMAGENTA,
23 | SK_ColorBLUE, SK_ColorCYAN,
24 | SK_ColorRED};
25 |
26 | float[] pos = {0,
27 | SK_Scalar1 / 6,
28 | 2 * SK_Scalar1 / 6,
29 | 3 * SK_Scalar1 / 6,
30 | 4 * SK_Scalar1 / 6,
31 | 5 * SK_Scalar1 / 6,
32 | SK_Scalar1};
33 |
34 | SkPaint paint = new SkPaint();
35 | paint.setShader(SkGradientShader.CreateRadial(
36 | pt, radius,
37 | colors, pos,
38 | SkShader.kRepeat_TileMode));
39 | SkRect rect = SkRect.MakeWH(wScalar, hScalar);
40 | SkMatrix mat = SkMatrix.I();
41 | for (int i = 0; i < 4; ++i) {
42 | paint.getShader().setLocalMatrix(mat);
43 | canvas.drawRect(rect, paint);
44 | rect.inset(wScalar / 8, hScalar / 8);
45 | mat.postScale(SK_Scalar1 / 4, SK_Scalar1 / 4);
46 | }
47 | }
48 |
49 | static final int gSize = 1024;
50 |
51 | public static class DrawBitmapRectGM extends GM {
52 | public SkBitmap fLargeBitmap = new SkBitmap();
53 |
54 | @Override
55 | protected String onShortName() {
56 | return "drawbitmaprect";
57 | }
58 |
59 | @Override
60 | protected SkISize onISize() { return make_isize(gSize, gSize); }
61 |
62 | @Override
63 | protected void onDraw(SkCanvas canvas) {
64 | final int kBmpSize = 2048;
65 | if (fLargeBitmap.isNullPixels() ) {
66 | makebm(fLargeBitmap,
67 | SkBitmap.kARGB_8888_Config,
68 | kBmpSize, kBmpSize);
69 | }
70 | SkRect dstRect = SkRect.MakeLTRB(0, 0, SkIntToScalar(64), SkIntToScalar(64));
71 | final int kMaxSrcRectSize = 1 << (SkNextLog2(kBmpSize) + 2);
72 |
73 | final int kPadX = 30;
74 | final int kPadY = 40;
75 | SkPaint paint = new SkPaint();
76 | paint.setAlpha(0x20);
77 | canvas.drawBitmapRect(fLargeBitmap, null,
78 | SkRect.MakeWH(gSize * SK_Scalar1,
79 | gSize * SK_Scalar1),
80 | paint);
81 | canvas.translate(SK_Scalar1 * kPadX / 2,
82 | SK_Scalar1 * kPadY / 2);
83 | SkPaint blackPaint = new SkPaint();
84 | float titleHeight = SK_Scalar1 * 24;
85 | blackPaint.setColor(SK_ColorBLACK);
86 | blackPaint.setTextSize(titleHeight);
87 | blackPaint.setAntiAlias(true);
88 | String title;
89 | title = String.format("Bitmap size: %d x %d", kBmpSize, kBmpSize);
90 | canvas.drawText(title, 0,
91 | titleHeight, blackPaint);
92 |
93 | canvas.translate(0, SK_Scalar1 * kPadY / 2 + titleHeight);
94 | int rowCount = 0;
95 | canvas.save();
96 | for (int w = 1; w <= kMaxSrcRectSize; w *= 4) {
97 | for (int h = 1; h <= kMaxSrcRectSize; h *= 4) {
98 |
99 | SkIRect srcRect = SkIRect.MakeXYWH((kBmpSize - w) / 2,
100 | (kBmpSize - h) / 2,
101 | w, h);
102 | canvas.drawBitmapRect(fLargeBitmap, srcRect, dstRect);
103 |
104 | String label;
105 | label = String.format("%d x %d", w, h);
106 | blackPaint.setAntiAlias(true);
107 | blackPaint.setStyle(SkPaint.kFill_Style);
108 | blackPaint.setTextSize(SK_Scalar1 * 10);
109 | float baseline = dstRect.height() +
110 | blackPaint.getTextSize() + SK_Scalar1 * 3;
111 | canvas.drawText(label,
112 | 0, baseline,
113 | blackPaint);
114 | blackPaint.setStyle(SkPaint.kStroke_Style);
115 | blackPaint.setStrokeWidth(SK_Scalar1);
116 | blackPaint.setAntiAlias(false);
117 | canvas.drawRect(dstRect, blackPaint);
118 |
119 | canvas.translate(dstRect.width() + SK_Scalar1 * kPadX, 0);
120 | ++rowCount;
121 | if ((dstRect.width() + kPadX) * rowCount > gSize) {
122 | canvas.restore();
123 | canvas.translate(0, dstRect.height() + SK_Scalar1 * kPadY);
124 | canvas.save();
125 | rowCount = 0;
126 | }
127 | }
128 | }
129 | }
130 | }
131 |
132 | public static GMRegistry.Factory factory = new GMRegistry.Factory() {
133 | public GM apply() {
134 | return new DrawBitmapRectGM();
135 | }
136 | };
137 | }
138 |
--------------------------------------------------------------------------------
/src/test/java/skia/javacpp/gm/emptypath.java:
--------------------------------------------------------------------------------
1 | package skia.javacpp.gm;
2 |
3 | import static skia.javacpp.core.*;
4 |
5 | public class emptypath {
6 | public static class EmptyPathGM extends GM {
7 |
8 | @Override
9 | protected String onShortName() {
10 | return "emptypath";
11 | }
12 |
13 | @Override
14 | protected SkISize onISize() { return make_isize(600, 280); }
15 |
16 | void drawEmpty(SkCanvas canvas,
17 | int color,
18 | SkRect clip,
19 | int style,
20 | int fill) {
21 | SkPath path = new SkPath();
22 | path.setFillType(fill);
23 | SkPaint paint = new SkPaint();
24 | paint.setColor(color);
25 | paint.setStyle(style);
26 | canvas.save();
27 | canvas.clipRect(clip);
28 | canvas.drawPath(path, paint);
29 | canvas.restore();
30 | }
31 |
32 | @Override
33 | protected void onDraw(SkCanvas canvas) {
34 | class FillAndName {
35 | int fFill;
36 | String fName;
37 |
38 | public FillAndName(int fill, String name) {
39 | fFill = fill;
40 | fName = name;
41 | }
42 | };
43 | final FillAndName[] gFills = {
44 | new FillAndName(SkPath.kWinding_FillType, "Winding"),
45 | new FillAndName(SkPath.kEvenOdd_FillType, "Even / Odd"),
46 | new FillAndName(SkPath.kInverseWinding_FillType, "Inverse Winding"),
47 | new FillAndName(SkPath.kInverseEvenOdd_FillType, "Inverse Even / Odd"),
48 | };
49 | class StyleAndName {
50 | int fStyle;
51 | String fName;
52 | public StyleAndName(int style, String name) {
53 | fStyle = style;
54 | fName = name;
55 | }
56 | };
57 | final StyleAndName[] gStyles = {
58 | new StyleAndName(SkPaint.kFill_Style, "Fill"),
59 | new StyleAndName(SkPaint.kStroke_Style, "Stroke"),
60 | new StyleAndName(SkPaint.kStrokeAndFill_Style, "Stroke And Fill"),
61 | };
62 |
63 | SkPaint titlePaint = new SkPaint();
64 | titlePaint.setColor(SK_ColorBLACK);
65 | titlePaint.setAntiAlias(true);
66 | titlePaint.setLCDRenderText(true);
67 | titlePaint.setTextSize(15 * SK_Scalar1);
68 | String title = "Empty Paths Drawn Into Rectangle Clips With " +
69 | "Indicated Style and Fill";
70 | canvas.drawText(title,
71 | 20 * SK_Scalar1,
72 | 20 * SK_Scalar1,
73 | titlePaint);
74 |
75 | SkRandom rand = new SkRandom();
76 | SkRect rect = SkRect.MakeWH(100*SK_Scalar1, 30*SK_Scalar1);
77 | int i = 0;
78 | canvas.save();
79 | canvas.translate(10 * SK_Scalar1, 0);
80 | canvas.save();
81 | for (int style = 0; style < gStyles.length; ++style) {
82 | for (int fill = 0; fill < gFills.length; ++fill) {
83 | if (0 == i % 4) {
84 | canvas.restore();
85 | canvas.translate(0, rect.height() + 40 * SK_Scalar1);
86 | canvas.save();
87 | } else {
88 | canvas.translate(rect.width() + 40 * SK_Scalar1, 0);
89 | }
90 | ++i;
91 |
92 |
93 | int color = rand.nextU();
94 | color = 0xff000000| color; // force solid
95 | this.drawEmpty(canvas, color, rect,
96 | gStyles[style].fStyle, gFills[fill].fFill);
97 |
98 | SkPaint rectPaint = new SkPaint();
99 | rectPaint.setColor(SK_ColorBLACK);
100 | rectPaint.setStyle(SkPaint.kStroke_Style);
101 | rectPaint.setStrokeWidth(-1);
102 | rectPaint.setAntiAlias(true);
103 | canvas.drawRect(rect, rectPaint);
104 |
105 | SkPaint labelPaint = new SkPaint();
106 | labelPaint.setColor(color);
107 | labelPaint.setAntiAlias(true);
108 | labelPaint.setLCDRenderText(true);
109 | labelPaint.setTextSize(12 * SK_Scalar1);
110 | canvas.drawText(gStyles[style].fName,
111 | 0, rect.height() + 15 * SK_Scalar1,
112 | labelPaint);
113 | canvas.drawText(gFills[fill].fName,
114 | 0, rect.height() + 28 * SK_Scalar1,
115 | labelPaint);
116 | }
117 | }
118 | canvas.restore();
119 | canvas.restore();
120 | }
121 | }
122 |
123 | public static GMRegistry.Factory MyFactory = new GMRegistry.Factory() {
124 | public GM apply() {
125 | return new EmptyPathGM();
126 | }
127 | };
128 | }
129 |
--------------------------------------------------------------------------------
/src/test/java/skia/javacpp/gm/filltypes.java:
--------------------------------------------------------------------------------
1 | package skia.javacpp.gm;
2 |
3 | import static skia.javacpp.core.*;
4 |
5 | public class filltypes {
6 | public static class FillTypeGM extends GM {
7 | private SkPath fPath = new SkPath();
8 | public FillTypeGM() {
9 | this.setBGColor(0xFFDDDDDD);
10 | final float radius = SkIntToScalar(45);
11 | fPath.addCircle(SkIntToScalar(50), SkIntToScalar(50), radius);
12 | fPath.addCircle(SkIntToScalar(100), SkIntToScalar(100), radius);
13 | }
14 |
15 | @Override
16 | protected String onShortName() {
17 | return "filltypes";
18 | }
19 |
20 | @Override
21 | protected SkISize onISize() {
22 | return make_isize(835, 840);
23 | }
24 |
25 | void showPath(SkCanvas canvas, int x, int y, int ft,
26 | float scale, SkPaint paint) {
27 |
28 | final SkRect r = SkRect.MakeLTRB(0, 0, SkIntToScalar(150), SkIntToScalar(150));
29 |
30 | canvas.save();
31 | canvas.translate(SkIntToScalar(x), SkIntToScalar(y));
32 | canvas.clipRect(r);
33 | canvas.drawColor(SK_ColorWHITE);
34 | fPath.setFillType(ft);
35 | canvas.translate(r.centerX(), r.centerY());
36 | canvas.scale(scale, scale);
37 | canvas.translate(-r.centerX(), -r.centerY());
38 | canvas.drawPath(fPath, paint);
39 | canvas.restore();
40 | }
41 |
42 | void showFour(SkCanvas canvas, float scale, SkPaint paint) {
43 | showPath(canvas, 0, 0, SkPath.kWinding_FillType,
44 | scale, paint);
45 | showPath(canvas, 200, 0, SkPath.kEvenOdd_FillType,
46 | scale, paint);
47 | showPath(canvas, 00, 200, SkPath.kInverseWinding_FillType,
48 | scale, paint);
49 | showPath(canvas, 200, 200, SkPath.kInverseEvenOdd_FillType,
50 | scale, paint);
51 | }
52 |
53 | @Override
54 | protected void onDraw(SkCanvas canvas) {
55 | canvas.translate(SkIntToScalar(20), SkIntToScalar(20));
56 |
57 | SkPaint paint = new SkPaint();
58 | final float scale = SkIntToScalar(5)/4;
59 |
60 | paint.setAntiAlias(false);
61 |
62 | showFour(canvas, SK_Scalar1, paint);
63 | canvas.translate(SkIntToScalar(450), 0);
64 | showFour(canvas, scale, paint);
65 |
66 | paint.setAntiAlias(true);
67 |
68 | canvas.translate(SkIntToScalar(-450), SkIntToScalar(450));
69 | showFour(canvas, SK_Scalar1, paint);
70 | canvas.translate(SkIntToScalar(450), 0);
71 | showFour(canvas, scale, paint);
72 | }
73 | }
74 |
75 | public static GMRegistry.Factory MyFactory = new GMRegistry.Factory() {
76 | public GM apply() {
77 | return new FillTypeGM();
78 | }
79 | };
80 | }
81 |
--------------------------------------------------------------------------------
/src/test/java/skia/javacpp/gm/filltypespersp.java:
--------------------------------------------------------------------------------
1 | package skia.javacpp.gm;
2 |
3 | import static skia.javacpp.core.*;
4 | import static skia.javacpp.effects.*;
5 |
6 | public class filltypespersp {
7 | public static class FillTypePerspGM extends GM {
8 | private SkPath fPath = new SkPath();
9 |
10 | public FillTypePerspGM() {
11 | final float radius = SkIntToScalar(45);
12 | fPath.addCircle(SkIntToScalar(50), SkIntToScalar(50), radius);
13 | fPath.addCircle(SkIntToScalar(100), SkIntToScalar(100), radius);
14 | }
15 |
16 | @Override
17 | protected String onShortName() {
18 | return "filltypespersp";
19 | }
20 |
21 | @Override
22 | protected SkISize onISize() {
23 | return make_isize(835, 840);
24 | }
25 |
26 | void showPath(SkCanvas canvas, int x, int y, int ft,
27 | float scale, SkPaint paint) {
28 |
29 | final SkRect r = SkRect.MakeLTRB(0, 0, SkIntToScalar(150), SkIntToScalar(150));
30 |
31 | canvas.save();
32 | canvas.translate(SkIntToScalar(x), SkIntToScalar(y));
33 | canvas.clipRect(r);
34 | canvas.drawColor(SK_ColorWHITE);
35 | fPath.setFillType(ft);
36 | canvas.translate(r.centerX(), r.centerY());
37 | canvas.scale(scale, scale);
38 | canvas.translate(-r.centerX(), -r.centerY());
39 | canvas.drawPath(fPath, paint);
40 | canvas.restore();
41 | }
42 |
43 | void showFour(SkCanvas canvas, float scale, boolean aa) {
44 |
45 | SkPaint paint = new SkPaint();
46 | SkPoint center = SkPoint.Make(SkIntToScalar(100), SkIntToScalar(100));
47 | int[] colors = {SK_ColorBLUE, SK_ColorRED, SK_ColorGREEN};
48 | float[] pos = {0, SK_ScalarHalf, SK_Scalar1};
49 | SkShader s = SkGradientShader.CreateRadial(center,
50 | SkIntToScalar(100),
51 | colors,
52 | pos,
53 | SkShader.kClamp_TileMode);
54 | paint.setShader(s);
55 | paint.setAntiAlias(aa);
56 |
57 | showPath(canvas, 0, 0, SkPath.kWinding_FillType,
58 | scale, paint);
59 | showPath(canvas, 200, 0, SkPath.kEvenOdd_FillType,
60 | scale, paint);
61 | showPath(canvas, 00, 200, SkPath.kInverseWinding_FillType,
62 | scale, paint);
63 | showPath(canvas, 200, 200, SkPath.kInverseEvenOdd_FillType,
64 | scale, paint);
65 | }
66 |
67 | @Override
68 | protected void onDraw(SkCanvas canvas) {
69 | // do perspective drawPaint as the background;
70 | SkPaint bkgnrd = new SkPaint();
71 | SkPoint center = SkPoint.Make(SkIntToScalar(100),
72 | SkIntToScalar(100));
73 | int[] colors = {SK_ColorBLACK, SK_ColorCYAN,
74 | SK_ColorYELLOW, SK_ColorWHITE};
75 | float[] pos = {0, SK_ScalarHalf / 2,
76 | 3 * SK_ScalarHalf / 2, SK_Scalar1};
77 | SkShader s = SkGradientShader.CreateRadial(center,
78 | SkIntToScalar(1000),
79 | colors,
80 | pos,
81 | SkShader.kClamp_TileMode);
82 | bkgnrd.setShader(s);
83 | canvas.save();
84 | canvas.translate(SkIntToScalar(100), SkIntToScalar(100));
85 | SkMatrix mat = new SkMatrix();
86 | mat.reset();
87 | mat.setPerspY(SkScalarToPersp(SK_Scalar1 / 1000));
88 | canvas.concat(mat);
89 | canvas.drawPaint(bkgnrd);
90 | canvas.restore();
91 |
92 | // draw the paths in perspective
93 | SkMatrix persp = new SkMatrix();
94 | persp.reset();
95 | persp.setPerspX(SkScalarToPersp(-SK_Scalar1 / 1800));
96 | persp.setPerspY(SkScalarToPersp(SK_Scalar1 / 500));
97 | canvas.concat(persp);
98 |
99 | canvas.translate(SkIntToScalar(20), SkIntToScalar(20));
100 | final float scale = SkIntToScalar(5)/4;
101 |
102 | showFour(canvas, SK_Scalar1, false);
103 | canvas.translate(SkIntToScalar(450), 0);
104 | showFour(canvas, scale, false);
105 |
106 | canvas.translate(SkIntToScalar(-450), SkIntToScalar(450));
107 | showFour(canvas, SK_Scalar1, true);
108 | canvas.translate(SkIntToScalar(450), 0);
109 | showFour(canvas, scale, true);
110 | }
111 | }
112 |
113 | public static GMRegistry.Factory MyFactory = new GMRegistry.Factory() {
114 | public GM apply() {
115 | return new FillTypePerspGM();
116 | }
117 | };
118 | }
119 |
--------------------------------------------------------------------------------
/src/test/java/skia/javacpp/gm/fontscaler.java:
--------------------------------------------------------------------------------
1 | package skia.javacpp.gm;
2 |
3 | import static skia.javacpp.core.*;
4 |
5 | public class fontscaler {
6 | public static class FontScalerGM extends GM {
7 | public FontScalerGM() {
8 | this.setBGColor(0xFFFFFFFF);
9 | }
10 |
11 | @Override
12 | protected String onShortName() {
13 | return "fontscaler";
14 | }
15 |
16 | @Override
17 | protected SkISize onISize() {
18 | return make_isize(1450, 750);
19 | }
20 |
21 | static void rotate_about(SkCanvas canvas,
22 | float degrees,
23 | float px, float py) {
24 | canvas.translate(px, py);
25 | canvas.rotate(degrees);
26 | canvas.translate(-px, -py);
27 | }
28 |
29 | @Override
30 | protected void onDraw(SkCanvas canvas) {
31 | SkPaint paint = new SkPaint();
32 |
33 | paint.setAntiAlias(true);
34 | paint.setLCDRenderText(true);
35 | //With freetype the default (normal hinting) can be really ugly.
36 | //Most distros now set slight (vertical hinting only) in any event.
37 | paint.setHinting(SkPaint.kSlight_Hinting);
38 | paint.setTypeface(SkTypeface.CreateFromName("Times Roman", SkTypeface.kNormal));
39 |
40 | String text = "Hamburgefons ooo mmm";
41 | final int textLen = text.length();
42 |
43 | for (int j = 0; j < 2; ++j) {
44 | for (int i = 0; i < 6; ++i) {
45 | float x = SkIntToScalar(10);
46 | float y = SkIntToScalar(20);
47 |
48 | int saveCount = canvas.getSaveCount();
49 | canvas.save();
50 | try {
51 | canvas.translate(SkIntToScalar(50 + i * 230),
52 | SkIntToScalar(20));
53 | rotate_about(canvas, SkIntToScalar(i * 5), x, y * 10);
54 |
55 | {
56 | SkPaint p = new SkPaint();
57 | p.setAntiAlias(true);
58 | SkRect r = new SkRect();
59 | r.set(x - SkIntToScalar(3), SkIntToScalar(15),
60 | x - SkIntToScalar(1), SkIntToScalar(280));
61 | canvas.drawRect(r, p);
62 | }
63 |
64 | int index = 0;
65 | for (int ps = 6; ps <= 22; ps++) {
66 | paint.setTextSize(SkIntToScalar(ps));
67 | canvas.drawText(text, x, y, paint);
68 | y += paint.getFontMetrics(null);
69 | index += 1;
70 | }
71 | } finally {
72 | canvas.restoreToCount(saveCount);
73 | }
74 | }
75 | canvas.translate(0, SkIntToScalar(360));
76 | paint.setSubpixelText(true);
77 | }
78 | }
79 | }
80 |
81 | public static GMRegistry.Factory MyFactory = new GMRegistry.Factory() {
82 | public GM apply() {
83 | return new FontScalerGM();
84 | }
85 | };
86 | }
87 |
--------------------------------------------------------------------------------
/src/test/java/skia/javacpp/gm/giantbitmap.java:
--------------------------------------------------------------------------------
1 | package skia.javacpp.gm;
2 |
3 | import static skia.javacpp.core.*;
4 |
5 | public class giantbitmap {
6 | static final int W = 257;
7 | static final int H = 161;
8 |
9 | public static class GiantBitmapGM extends GM {
10 | private SkBitmap fBM;
11 | private int fMode;
12 | private boolean fDoFilter;
13 | private boolean fDoRotate;
14 |
15 | SkBitmap getBitmap() {
16 | if (null == fBM) {
17 | fBM = new SkBitmap();
18 | fBM.setConfig(SkBitmap.kARGB_8888_Config, W, H);
19 | fBM.allocPixels();
20 | fBM.eraseColor(SK_ColorWHITE);
21 |
22 | final int[] colors = {
23 | SK_ColorBLUE, SK_ColorRED, SK_ColorBLACK, SK_ColorGREEN
24 | };
25 |
26 | SkCanvas canvas = new SkCanvas(fBM);
27 | SkPaint paint = new SkPaint();
28 | paint.setAntiAlias(true);
29 | paint.setStrokeWidth(SkIntToScalar(20));
30 |
31 | if (false) {
32 | for (int y = -H*2; y < H; y += 50) {
33 | float yy = SkIntToScalar(y);
34 | paint.setColor(colors[y/50 & 0x3]);
35 | canvas.drawLine(0, yy, SkIntToScalar(W), yy + SkIntToScalar(W),
36 | paint);
37 | }
38 | } else {
39 | for (int x = -W; x < W; x += 60) {
40 | paint.setColor(colors[x/60 & 0x3]);
41 |
42 | float xx = SkIntToScalar(x);
43 | canvas.drawLine(xx, 0, xx, SkIntToScalar(H),
44 | paint);
45 | }
46 | }
47 | }
48 | return fBM;
49 | }
50 |
51 | public GiantBitmapGM(int mode, boolean doFilter, boolean doRotate) {
52 | fBM = null;
53 | fMode = mode;
54 | fDoFilter = doFilter;
55 | fDoRotate = doRotate;
56 | }
57 |
58 | @Override
59 | protected String onShortName() {
60 | String str = "giantbitmap_";
61 | switch (fMode) {
62 | case SkShader.kClamp_TileMode:
63 | str += "clamp";
64 | break;
65 | case SkShader.kRepeat_TileMode:
66 | str += "repeat";
67 | break;
68 | case SkShader.kMirror_TileMode:
69 | str += "mirror";
70 | break;
71 | default:
72 | break;
73 | }
74 | str += fDoFilter ? "_bilerp" : "_point";
75 | str += fDoRotate ? "_rotate" : "_scale";
76 | return str;
77 | }
78 |
79 | @Override
80 | protected SkISize onISize() { return SkISize.Make(640, 480); }
81 |
82 | @Override
83 | protected void onDraw(SkCanvas canvas) {
84 | SkPaint paint = new SkPaint();
85 | SkShader s = SkShader.CreateBitmapShader(getBitmap(), fMode, fMode);
86 |
87 | SkMatrix m = new SkMatrix();
88 | if (fDoRotate) {
89 | // m.setRotate(SkIntToScalar(30), 0, 0);
90 | m.setSkew(SK_Scalar1, 0, 0, 0);
91 | // m.postScale(2*SK_Scalar1/3, 2*SK_Scalar1/3);
92 | } else {
93 | float scale = 11*SK_Scalar1/12;
94 | m.setScale(scale, scale);
95 | }
96 | s.setLocalMatrix(m);
97 |
98 | paint.setShader(s);
99 | paint.setFilterBitmap(fDoFilter);
100 |
101 | canvas.translate(SkIntToScalar(50), SkIntToScalar(50));
102 |
103 | SkRect r = SkRect.MakeXYWH(-50, -50, 32, 16);
104 | // canvas->drawRect(r, paint); return;
105 | canvas.drawPaint(paint);
106 | }
107 | }
108 |
109 | public static GMRegistry.Factory G000 = new GMRegistry.Factory() {
110 | public GM apply() {
111 | return new GiantBitmapGM(SkShader.kClamp_TileMode, false, false);
112 | }
113 | };
114 | public static GMRegistry.Factory G100 = new GMRegistry.Factory() {
115 | public GM apply() {
116 | return new GiantBitmapGM(SkShader.kRepeat_TileMode, false, false);
117 | }
118 | };
119 | public static GMRegistry.Factory G200 = new GMRegistry.Factory() {
120 | public GM apply() {
121 | return new GiantBitmapGM(SkShader.kMirror_TileMode, false, false);
122 | }
123 | };
124 | public static GMRegistry.Factory G010 = new GMRegistry.Factory() {
125 | public GM apply() {
126 | return new GiantBitmapGM(SkShader.kClamp_TileMode, true, false);
127 | }
128 | };
129 | public static GMRegistry.Factory G110 = new GMRegistry.Factory() {
130 | public GM apply() {
131 | return new GiantBitmapGM(SkShader.kRepeat_TileMode, true, false);
132 | }
133 | };
134 | public static GMRegistry.Factory G210 = new GMRegistry.Factory() {
135 | public GM apply() {
136 | return new GiantBitmapGM(SkShader.kMirror_TileMode, true, false);
137 | }
138 | };
139 |
140 | public static GMRegistry.Factory G001 = new GMRegistry.Factory() {
141 | public GM apply() {
142 | return new GiantBitmapGM(SkShader.kClamp_TileMode, false, true);
143 | }
144 | };
145 | public static GMRegistry.Factory G101 = new GMRegistry.Factory() {
146 | public GM apply() {
147 | return new GiantBitmapGM(SkShader.kRepeat_TileMode, false, true);
148 | }
149 | };
150 | public static GMRegistry.Factory G201 = new GMRegistry.Factory() {
151 | public GM apply() {
152 | return new GiantBitmapGM(SkShader.kMirror_TileMode, false, true);
153 | }
154 | };
155 | public static GMRegistry.Factory G011 = new GMRegistry.Factory() {
156 | public GM apply() {
157 | return new GiantBitmapGM(SkShader.kClamp_TileMode, true, true);
158 | }
159 | };
160 | public static GMRegistry.Factory G111 = new GMRegistry.Factory() {
161 | public GM apply() {
162 | return new GiantBitmapGM(SkShader.kRepeat_TileMode, true, true);
163 | }
164 | };
165 | public static GMRegistry.Factory G211 = new GMRegistry.Factory() {
166 | public GM apply() {
167 | return new GiantBitmapGM(SkShader.kMirror_TileMode, true, true);
168 | }
169 | };
170 | }
171 |
--------------------------------------------------------------------------------
/src/test/java/skia/javacpp/gm/gradtext.java:
--------------------------------------------------------------------------------
1 | package skia.javacpp.gm;
2 |
3 | import static skia.javacpp.core.*;
4 | import static skia.javacpp.effects.*;
5 |
6 | public class gradtext {
7 | static SkShader make_grad(float width) {
8 | int colors[] = { SK_ColorRED, 0x0000FF00, SK_ColorBLUE };
9 | SkPoint pts[] = { SkPoint.Make(0, 0), SkPoint.Make(width, 0) };
10 | return SkGradientShader.CreateLinear(pts, colors, null, SkShader.kMirror_TileMode);
11 | }
12 |
13 | static SkShader make_grad2(float width) {
14 | int colors[] = { SK_ColorRED, SK_ColorGREEN, SK_ColorBLUE };
15 | SkPoint pts[] = { SkPoint.Make(0, 0), SkPoint.Make(width, 0) };
16 | return SkGradientShader.CreateLinear(pts, colors, null, SkShader.kMirror_TileMode);
17 | }
18 |
19 | public static class GradTextGM extends GM {
20 | @Override
21 | protected String onShortName() {
22 | return "gradtext";
23 | }
24 |
25 | @Override
26 | protected SkISize onISize() { return make_isize(500, 480); }
27 |
28 | static void draw_text(SkCanvas canvas, SkPaint paint) {
29 | String text = "When in the course of human events";
30 | canvas.drawText(text, 0, 0, paint);
31 | }
32 |
33 | static void draw_text3(SkCanvas canvas, SkPaint paint) {
34 | SkPaint p = new SkPaint(paint);
35 |
36 | p.setAntiAlias(false);
37 | draw_text(canvas, p);
38 | p.setAntiAlias(true);
39 | canvas.translate(0, paint.getTextSize() * 4/3);
40 | draw_text(canvas, p);
41 | p.setLCDRenderText(true);
42 | canvas.translate(0, paint.getTextSize() * 4/3);
43 | draw_text(canvas, p);
44 | }
45 |
46 | @Override
47 | protected void onDraw(SkCanvas canvas) {
48 | SkPaint paint = new SkPaint();
49 | paint.setTextSize(SkIntToScalar(26));
50 |
51 | SkISize size = this.getISize();
52 | SkRect r = SkRect.MakeWH(SkIntToScalar(size.width()),
53 | SkIntToScalar(size.height()) / 2);
54 | canvas.drawRect(r, paint);
55 |
56 | canvas.translate(SkIntToScalar(20), paint.getTextSize());
57 |
58 | for (int i = 0; i < 2; ++i) {
59 | paint.setShader(make_grad(SkIntToScalar(80)));
60 | draw_text3(canvas, paint);
61 |
62 | canvas.translate(0, paint.getTextSize() * 2);
63 |
64 | paint.setShader(make_grad2(SkIntToScalar(80)));
65 | draw_text3(canvas, paint);
66 |
67 | canvas.translate(0, paint.getTextSize() * 2);
68 | }
69 | }
70 | }
71 |
72 | public static GMRegistry.Factory MyFactory = new GMRegistry.Factory() {
73 | public GM apply() {
74 | return new GradTextGM();
75 | }
76 | };
77 | }
78 |
--------------------------------------------------------------------------------
/src/test/java/skia/javacpp/gm/hairmodes.java:
--------------------------------------------------------------------------------
1 | package skia.javacpp.gm;
2 |
3 | import static skia.javacpp.core.*;
4 |
5 | public class hairmodes {
6 | static SkCanvas create_canvas(int w, int h) {
7 | SkBitmap bm = new SkBitmap();
8 | bm.setConfig(SkBitmap.kARGB_8888_Config, w, h);
9 | bm.allocPixels();
10 | bm.eraseColor(0);
11 | return new SkCanvas(bm);
12 | }
13 |
14 | static SkBitmap extract_bitmap(SkCanvas canvas) {
15 | return canvas.getDevice().accessBitmap(false);
16 | }
17 |
18 | static class Mode {
19 | int fMode;
20 | String fLabel;
21 | public Mode(int mode, String label) {
22 | fMode = mode;
23 | fLabel = label;
24 | }
25 | }
26 | static final Mode[] gModes = {
27 | new Mode(SkXfermode.kClear_Mode, "Clear" ),
28 | new Mode(SkXfermode.kSrc_Mode, "Src" ),
29 | new Mode(SkXfermode.kDst_Mode, "Dst" ),
30 | new Mode(SkXfermode.kSrcOver_Mode, "SrcOver" ),
31 | new Mode(SkXfermode.kDstOver_Mode, "DstOver" ),
32 | new Mode(SkXfermode.kSrcIn_Mode, "SrcIn" ),
33 | new Mode(SkXfermode.kDstIn_Mode, "DstIn" ),
34 | new Mode(SkXfermode.kSrcOut_Mode, "SrcOut" ),
35 | new Mode(SkXfermode.kDstOut_Mode, "DstOut" ),
36 | new Mode(SkXfermode.kSrcATop_Mode, "SrcATop" ),
37 | new Mode(SkXfermode.kDstATop_Mode, "DstATop" ),
38 | new Mode(SkXfermode.kXor_Mode, "Xor" ),
39 | };
40 |
41 | static final int gWidth = 64;
42 | static final int gHeight = 64;
43 | static final float W = SkIntToScalar(gWidth);
44 | static final float H = SkIntToScalar(gHeight);
45 |
46 | static float drawCell(SkCanvas canvas, SkXfermode mode, int a0, int a1) {
47 |
48 | SkPaint paint = new SkPaint();
49 | paint.setAntiAlias(true);
50 |
51 | SkRect r = SkRect.MakeWH(W, H);
52 | r.inset(W/10, H/10);
53 |
54 | paint.setColor(SK_ColorBLUE);
55 | paint.setAlpha(a0);
56 | canvas.drawOval(r, paint);
57 |
58 | paint.setColor(SK_ColorRED);
59 | paint.setAlpha(a1);
60 | paint.setXfermode(mode);
61 | for (int angle = 0; angle < 24; ++angle) {
62 | float x = SkScalarCos(SkIntToScalar(angle) * (SK_ScalarPI * 2) / 24) * gWidth;
63 | float y = SkScalarSin(SkIntToScalar(angle) * (SK_ScalarPI * 2) / 24) * gHeight;
64 | paint.setStrokeWidth(SK_Scalar1 * angle * 2 / 24);
65 | canvas.drawLine(W/2, H/2, W/2 + x, H/2 + y, paint);
66 | }
67 |
68 | return H;
69 | }
70 |
71 | static SkShader make_bg_shader() {
72 | SkBitmap bm = new SkBitmap();
73 | bm.setConfig(SkBitmap.kARGB_8888_Config, 2, 2);
74 | bm.allocPixels();
75 | bm.getAddr32(0, 0).put(0xFFFFFFFF);
76 | bm.getAddr32(1, 1).put(0xFFFFFFFF);
77 | bm.getAddr32(1, 0).put(SkPackARGB32(0xFF, 0xCC, 0xCC, 0xCC));
78 | bm.getAddr32(0, 1).put(SkPackARGB32(0xFF, 0xCC, 0xCC, 0xCC));
79 |
80 | SkShader s = SkShader.CreateBitmapShader(bm,
81 | SkShader.kRepeat_TileMode,
82 | SkShader.kRepeat_TileMode);
83 |
84 | SkMatrix m = new SkMatrix();
85 | m.setScale(SkIntToScalar(6), SkIntToScalar(6));
86 | s.setLocalMatrix(m);
87 | return s;
88 | }
89 |
90 | public static class HairModesGM extends GM {
91 | private SkPaint fBGPaint = new SkPaint();
92 | public HairModesGM() {
93 | fBGPaint.setShader(make_bg_shader());
94 | }
95 |
96 | @Override
97 | protected String onShortName() {
98 | return "hairmodes";
99 | }
100 |
101 | @Override
102 | protected SkISize onISize() { return make_isize(640, 480); }
103 |
104 | @Override
105 | protected void onDraw(SkCanvas canvas) {
106 | final SkRect bounds = SkRect.MakeWH(W, H);
107 | final int gAlphaValue[] = { 0xFF, 0x88, 0x88 };
108 |
109 | canvas.translate(SkIntToScalar(4), SkIntToScalar(4));
110 |
111 | for (int alpha = 0; alpha < 4; ++alpha) {
112 | canvas.save();
113 | canvas.save();
114 | for (int i = 0; i < gModes.length; ++i) {
115 | if (6 == i) {
116 | canvas.restore();
117 | canvas.translate(W * 5, 0);
118 | canvas.save();
119 | }
120 | SkXfermode mode = SkXfermode.Create(gModes[i].fMode);
121 |
122 | canvas.drawRect(bounds, fBGPaint);
123 | canvas.saveLayer(bounds, null);
124 | float dy = drawCell(canvas, mode,
125 | gAlphaValue[alpha & 1],
126 | gAlphaValue[alpha & 2]);
127 | canvas.restore();
128 |
129 | canvas.translate(0, dy * 5 / 4);
130 | }
131 | canvas.restore();
132 | canvas.restore();
133 | canvas.translate(W * 5 / 4, 0);
134 | }
135 | }
136 |
137 | // disable pdf for now, since it crashes on mac
138 | @Override
139 | protected int onGetFlags() { return kSkipPDF_Flag; }
140 | }
141 |
142 | public static GMRegistry.Factory MyFactory = new GMRegistry.Factory() {
143 | public GM apply() {
144 | return new HairModesGM();
145 | }
146 | };
147 | }
148 |
--------------------------------------------------------------------------------
/src/test/java/skia/javacpp/gm/imageblur.java:
--------------------------------------------------------------------------------
1 | package skia.javacpp.gm;
2 |
3 | import static skia.javacpp.core.*;
4 | import static skia.javacpp.effects.*;
5 |
6 | public class imageblur {
7 | static final int WIDTH = 500;
8 | static final int HEIGHT = 500;
9 |
10 | public static class ImageBlurGM extends GM {
11 | public ImageBlurGM() {
12 | this.setBGColor(0xFF000000);
13 | }
14 |
15 | @Override
16 | protected String onShortName() {
17 | return "imageblur";
18 | }
19 |
20 | @Override
21 | protected SkISize onISize() {
22 | return make_isize(WIDTH, HEIGHT);
23 | }
24 |
25 | @Override
26 | protected void onDraw(SkCanvas canvas) {
27 | SkPaint paint = new SkPaint();
28 | paint.setImageFilter(new SkBlurImageFilter(24.0f, 0.0f));
29 | canvas.saveLayer(null, paint);
30 | paint.setAntiAlias(true);
31 | String str = "The quick brown fox jumped over the lazy dog.";
32 | Glitch.srand(1234);
33 | for (int i = 0; i < 25; ++i) {
34 | int x = Glitch.rand() % WIDTH;
35 | int y = Glitch.rand() % HEIGHT;
36 | paint.setColor(Glitch.rand() % 0x1000000 | 0xFF000000);
37 | paint.setTextSize(Glitch.rand() % 300);
38 | canvas.drawText(str, x, y, paint);
39 | }
40 | canvas.restore();
41 | }
42 | }
43 |
44 | public static GMRegistry.Factory MyFactory = new GMRegistry.Factory() {
45 | public GM apply() {
46 | return new ImageBlurGM();
47 | }
48 | };
49 | }
50 |
--------------------------------------------------------------------------------
/src/test/java/skia/javacpp/gm/lcdtext.java:
--------------------------------------------------------------------------------
1 | package skia.javacpp.gm;
2 |
3 | import static skia.javacpp.core.*;
4 |
5 | public class lcdtext {
6 | public static class LcdTextGM extends GM {
7 | public LcdTextGM() {
8 | final int pointSize = 36;
9 | textHeight = SkIntToScalar(pointSize);
10 | }
11 |
12 | @Override
13 | protected String onShortName() {
14 | return "lcdtext";
15 | }
16 |
17 | @Override
18 | protected SkISize onISize() { return make_isize(640, 480); }
19 |
20 | @Override
21 | protected void onDraw(SkCanvas canvas) {
22 |
23 | y = textHeight;
24 | drawText(canvas, "TEXT: SubpixelTrue LCDRenderTrue",
25 | true, true);
26 | drawText(canvas, "TEXT: SubpixelTrue LCDRenderFalse",
27 | true, false);
28 | drawText(canvas, "TEXT: SubpixelFalse LCDRenderTrue",
29 | false, true);
30 | drawText(canvas, "TEXT: SubpixelFalse LCDRenderFalse",
31 | false, false);
32 | }
33 |
34 | void drawText(SkCanvas canvas, String string,
35 | boolean subpixelTextEnabled, boolean lcdRenderTextEnabled) {
36 | SkPaint paint = new SkPaint();
37 | paint.setColor(SK_ColorBLACK);
38 | paint.setDither(true);
39 | paint.setAntiAlias(true);
40 | paint.setSubpixelText(subpixelTextEnabled);
41 | paint.setLCDRenderText(lcdRenderTextEnabled);
42 | paint.setTextSize(textHeight);
43 |
44 | canvas.drawText(string, 0, y, paint);
45 | y += textHeight;
46 | }
47 |
48 | private float y, textHeight;
49 | }
50 |
51 | public static GMRegistry.Factory MyFactory = new GMRegistry.Factory() {
52 | public GM apply() {
53 | return new LcdTextGM();
54 | }
55 | };
56 | }
57 |
--------------------------------------------------------------------------------
/src/test/java/skia/javacpp/gm/morphology.java:
--------------------------------------------------------------------------------
1 | package skia.javacpp.gm;
2 |
3 | import static skia.javacpp.core.*;
4 | import static skia.javacpp.effects.*;
5 |
6 | public class morphology {
7 | static final int WIDTH = 640;
8 | static final int HEIGHT = 480;
9 |
10 | public static class MorphologyGM extends GM {
11 | public MorphologyGM() {
12 | this.setBGColor(0xFF000000);
13 | fOnce = false;
14 | }
15 |
16 | @Override
17 | protected String onShortName() {
18 | return "morphology";
19 | }
20 |
21 | void make_bitmap() {
22 | fBitmap.setConfig(SkBitmap.kARGB_8888_Config, 135, 135);
23 | fBitmap.allocPixels();
24 | SkDevice device = new SkDevice(fBitmap);
25 | SkCanvas canvas = new SkCanvas(device);
26 | canvas.clear(0x0);
27 | SkPaint paint = new SkPaint();
28 | paint.setAntiAlias(true);
29 | String str1 = "ABC";
30 | String str2 = "XYZ";
31 | paint.setColor(0xFFFFFFFF);
32 | paint.setTextSize(64);
33 | canvas.drawText(str1, 10, 55, paint);
34 | canvas.drawText(str2, 10, 110, paint);
35 | }
36 |
37 | @Override
38 | protected SkISize onISize() {
39 | return make_isize(WIDTH, HEIGHT);
40 | }
41 |
42 | @Override
43 | protected void onDraw(SkCanvas canvas) {
44 | if (!fOnce) {
45 | make_bitmap();
46 | fOnce = true;
47 | }
48 | class Sample {
49 | int fRadiusX, fRadiusY;
50 | boolean fErode;
51 | float fX, fY;
52 | public Sample(int radiusX, int radiusY, boolean erode, float x, float y) {
53 | fRadiusX = radiusX;
54 | fRadiusY = radiusY;
55 | fErode = erode;
56 | fX = x;
57 | fY = y;
58 | }
59 | }
60 |
61 | Sample[] samples = {
62 | new Sample(0, 0, false, 0, 0),
63 | new Sample(0, 2, false, 140, 0),
64 | new Sample(2, 0, false, 280, 0),
65 | new Sample(2, 2, false, 420, 0),
66 | new Sample(0, 0, true, 0, 140),
67 | new Sample(0, 2, true, 140, 140),
68 | new Sample(2, 0, true, 280, 140),
69 | new Sample(2, 2, true, 420, 140),
70 | };
71 | String str = "The quick brown fox jumped over the lazy dog.";
72 | SkPaint paint = new SkPaint();
73 | for (int i = 0; i < samples.length; ++i) {
74 | if (samples[i].fErode) {
75 | paint.setImageFilter(new SkErodeImageFilter(
76 | samples[i].fRadiusX,
77 | samples[i].fRadiusY));
78 | } else {
79 | paint.setImageFilter(new SkDilateImageFilter(
80 | samples[i].fRadiusX,
81 | samples[i].fRadiusY));
82 | }
83 | SkRect bounds = SkRect.MakeXYWH(samples[i].fX,
84 | samples[i].fY,
85 | 140, 140);
86 | canvas.saveLayer(bounds, paint);
87 | canvas.drawBitmap(fBitmap, samples[i].fX, samples[i].fY);
88 | canvas.restore();
89 | }
90 | }
91 |
92 | private SkBitmap fBitmap = new SkBitmap();
93 | private boolean fOnce;
94 | }
95 |
96 | public static GMRegistry.Factory MyFactory = new GMRegistry.Factory() {
97 | public GM apply() {
98 | return new MorphologyGM();
99 | }
100 | };
101 | }
102 |
--------------------------------------------------------------------------------
/src/test/java/skia/javacpp/gm/nocolorbleed.java:
--------------------------------------------------------------------------------
1 | package skia.javacpp.gm;
2 |
3 | import com.googlecode.javacpp.IntPointer;
4 |
5 | import static skia.javacpp.core.*;
6 |
7 | public class nocolorbleed {
8 | public static class NoColorBleedGM extends GM {
9 | public NoColorBleedGM() {
10 | this.setBGColor(0xFFDDDDDD);
11 | }
12 |
13 | @Override
14 | protected String onShortName() {
15 | return "nocolorbleed";
16 | }
17 |
18 | @Override
19 | protected SkISize onISize() {
20 | return make_isize(200, 200);
21 | }
22 |
23 | @Override
24 | protected void onDraw(SkCanvas canvas) {
25 | SkBitmap sprite = new SkBitmap();
26 | sprite.setConfig(SkBitmap.kARGB_8888_Config, 4, 4, 4 * (Integer.SIZE / 8));
27 | int[] spriteData = {
28 | SK_ColorBLACK, SK_ColorCYAN, SK_ColorMAGENTA, SK_ColorYELLOW,
29 | SK_ColorBLACK, SK_ColorWHITE, SK_ColorBLACK, SK_ColorRED,
30 | SK_ColorGREEN, SK_ColorBLACK, SK_ColorWHITE, SK_ColorBLUE,
31 | SK_ColorYELLOW, SK_ColorMAGENTA, SK_ColorCYAN, SK_ColorBLACK
32 | };
33 | sprite.allocPixels();
34 | sprite.lockPixels();
35 | IntPointer addr = sprite.getAddr32(0, 0);
36 | for (int i = 0; i < spriteData.length; ++i) {
37 | addr.put(i, SkPreMultiplyColor(spriteData[i]));
38 | }
39 | sprite.unlockPixels();
40 |
41 | // We draw a magnified subrect of the sprite
42 | // sample interpolation may cause color bleeding around edges
43 | // the subrect is a pure white area
44 | SkIRect srcRect = new SkIRect();
45 | SkRect dstRect = new SkRect();
46 | SkPaint paint = new SkPaint();
47 | paint.setFilterBitmap(true);
48 | //First row : full texture with and without filtering
49 | srcRect.setXYWH(0, 0, 4, 4);
50 | dstRect.setXYWH(SkIntToScalar(0), SkIntToScalar(0)
51 | , SkIntToScalar(100), SkIntToScalar(100));
52 | canvas.drawBitmapRect(sprite, srcRect, dstRect, paint);
53 | dstRect.setXYWH(SkIntToScalar(100), SkIntToScalar(0)
54 | , SkIntToScalar(100), SkIntToScalar(100));
55 | canvas.drawBitmapRect(sprite, srcRect, dstRect);
56 | //Second row : sub rect of texture with and without filtering
57 | srcRect.setXYWH(1, 1, 2, 2);
58 | dstRect.setXYWH(SkIntToScalar(25), SkIntToScalar(125)
59 | , SkIntToScalar(50), SkIntToScalar(50));
60 | canvas.drawBitmapRect(sprite, srcRect, dstRect, paint);
61 | dstRect.setXYWH(SkIntToScalar(125), SkIntToScalar(125)
62 | , SkIntToScalar(50), SkIntToScalar(50));
63 | canvas.drawBitmapRect(sprite, srcRect, dstRect);
64 | }
65 | }
66 |
67 | public static GMRegistry.Factory MyFactory = new GMRegistry.Factory() {
68 | public GM apply() {
69 | return new NoColorBleedGM();
70 | }
71 | };
72 | }
73 |
--------------------------------------------------------------------------------
/src/test/java/skia/javacpp/gm/patheffects.java:
--------------------------------------------------------------------------------
1 | package skia.javacpp.gm;
2 |
3 | import static skia.javacpp.core.*;
4 | import static skia.javacpp.effects.*;
5 |
6 | public class patheffects {
7 | static void compose_pe(SkPaint paint) {
8 | SkPathEffect pe = paint.getPathEffect();
9 | SkPathEffect corner = new SkCornerPathEffect(25);
10 | SkPathEffect compose;
11 | if (pe != null) {
12 | compose = new SkComposePathEffect(pe, corner);
13 | } else {
14 | compose = corner;
15 | }
16 | paint.setPathEffect(compose);
17 | }
18 |
19 | static PE_Proc hair_pe = new PE_Proc() {
20 | public void apply(SkPaint paint) {
21 | paint.setStrokeWidth(0);
22 | }
23 | };
24 |
25 | static PE_Proc hair2_pe = new PE_Proc() {
26 | public void apply(SkPaint paint) {
27 | paint.setStrokeWidth(0);
28 | compose_pe(paint);
29 | }
30 | };
31 |
32 | static PE_Proc stroke_pe = new PE_Proc() {
33 | public void apply(SkPaint paint) {
34 | paint.setStrokeWidth(12);
35 | compose_pe(paint);
36 | }
37 | };
38 |
39 | static PE_Proc dash_pe = new PE_Proc() {
40 | public void apply(SkPaint paint) {
41 | float[] inter = { 20, 10, 10, 10 };
42 | paint.setStrokeWidth(12);
43 | paint.setPathEffect(new SkDashPathEffect(inter, 0));
44 | compose_pe(paint);
45 | }
46 | };
47 |
48 | static final int[] gXY = {
49 | 4, 0, 0, -4, 8, -4, 12, 0, 8, 4, 0, 4
50 | };
51 |
52 | static void scale(SkPath path, float scale) {
53 | SkMatrix m = new SkMatrix();
54 | m.setScale(scale, scale);
55 | path.transform(m);
56 | }
57 |
58 | static PE_Proc one_d_pe = new PE_Proc() {
59 | public void apply(SkPaint paint) {
60 | SkPath path = new SkPath();
61 | path.moveTo(SkIntToScalar(gXY[0]), SkIntToScalar(gXY[1]));
62 | for (int i = 2; i < gXY.length; i += 2)
63 | path.lineTo(SkIntToScalar(gXY[i]), SkIntToScalar(gXY[i+1]));
64 | path.close();
65 | path.offset(SkIntToScalar(-6), 0);
66 | scale(path, 1.5f);
67 |
68 | paint.setPathEffect(new SkPath1DPathEffect(path, SkIntToScalar(21), 0,
69 | SkPath1DPathEffect.kRotate_Style));
70 | compose_pe(paint);
71 | }
72 | };
73 |
74 | interface PE_Proc {
75 | public void apply(SkPaint paint);
76 | }
77 | static final PE_Proc[] gPE = { hair_pe, hair2_pe, stroke_pe, dash_pe, one_d_pe };
78 |
79 | static PE_Proc fill_pe = new PE_Proc() {
80 | public void apply(SkPaint paint) {
81 | paint.setStyle(SkPaint.kFill_Style);
82 | paint.setPathEffect(null);
83 | }
84 | };
85 |
86 | static PE_Proc discrete_pe = new PE_Proc() {
87 | public void apply(SkPaint paint) {
88 | paint.setPathEffect(new SkDiscretePathEffect(10, 4));
89 | }
90 | };
91 |
92 | static SkPathEffect MakeTileEffect() {
93 | SkMatrix m = new SkMatrix();
94 | m.setScale(SkIntToScalar(12), SkIntToScalar(12));
95 |
96 | SkPath path = new SkPath();
97 | path.addCircle(0, 0, SkIntToScalar(5));
98 |
99 | return new SkPath2DPathEffect(m, path);
100 | }
101 |
102 | static PE_Proc tile_pe = new PE_Proc() {
103 | public void apply(SkPaint paint) {
104 | paint.setPathEffect(MakeTileEffect());
105 | }
106 | };
107 |
108 | static final PE_Proc[] gPE2 = { fill_pe, discrete_pe, tile_pe };
109 |
110 | public static class PathEffectGM extends GM {
111 | @Override
112 | protected String onShortName() {
113 | return "patheffect";
114 | }
115 |
116 | @Override
117 | protected SkISize onISize() { return make_isize(800, 600); }
118 |
119 | @Override
120 | protected void onDraw(SkCanvas canvas) {
121 | SkPaint paint = new SkPaint();
122 | paint.setAntiAlias(true);
123 | paint.setStyle(SkPaint.kStroke_Style);
124 |
125 | SkPath path = new SkPath();
126 | path.moveTo(20, 20);
127 | path.lineTo(70, 120);
128 | path.lineTo(120, 30);
129 | path.lineTo(170, 80);
130 | path.lineTo(240, 50);
131 |
132 | int i;
133 | canvas.save();
134 | for (i = 0; i < gPE.length; i++) {
135 | gPE[i].apply(paint);
136 | canvas.drawPath(path, paint);
137 | canvas.translate(0, 75);
138 | }
139 | canvas.restore();
140 |
141 | path.reset();
142 | SkRect r = SkRect.MakeLTRB(0, 0, 250, 120);
143 | path.addOval(r, SkPath.kCW_Direction);
144 | r.inset(50, 50);
145 | path.addRect(r, SkPath.kCCW_Direction);
146 |
147 | canvas.translate(320, 20);
148 | for (i = 0; i < gPE2.length; i++) {
149 | gPE2[i].apply(paint);
150 | canvas.drawPath(path, paint);
151 | canvas.translate(0, 160);
152 | }
153 |
154 | SkIRect rect = SkIRect.MakeXYWH(20, 20, 60, 60);
155 | for (i = 0; i < gPE.length; i++) {
156 | SkPaint p = new SkPaint();
157 | p.setAntiAlias(true);
158 | p.setStyle(SkPaint.kFill_Style);
159 | gPE[i].apply(p);
160 | canvas.drawIRect(rect, p);
161 | canvas.translate(75, 0);
162 | }
163 | }
164 | }
165 |
166 | public static GMRegistry.Factory PathEffectFactory = new GMRegistry.Factory() {
167 | public GM apply() {
168 | return new PathEffectGM();
169 | }
170 | };
171 | }
172 |
--------------------------------------------------------------------------------
/src/test/java/skia/javacpp/gm/pathfill.java:
--------------------------------------------------------------------------------
1 | package skia.javacpp.gm;
2 |
3 | import static skia.javacpp.core.*;
4 |
5 | public class pathfill {
6 | static interface MakePathProc {
7 | public float apply(SkPath path);
8 | }
9 |
10 | static MakePathProc make_frame = new MakePathProc() {
11 | public float apply(SkPath path) {
12 | SkRect r = SkRect.MakeLTRB(SkIntToScalar(10), SkIntToScalar(10),
13 | SkIntToScalar(630), SkIntToScalar(470));
14 | path.addRoundRect(r, SkIntToScalar(15), SkIntToScalar(15));
15 |
16 | SkPaint paint = new SkPaint();
17 | paint.setStyle(SkPaint.kStroke_Style);
18 | paint.setStrokeWidth(SkIntToScalar(5));
19 | paint.getFillPath(path, path);
20 | return SkIntToScalar(15);
21 | }
22 | };
23 |
24 | static MakePathProc make_triangle = new MakePathProc() {
25 | public float apply(SkPath path) {
26 | final int[] gCoord = {
27 | 10, 20, 15, 5, 30, 30
28 | };
29 | path.moveTo(SkIntToScalar(gCoord[0]), SkIntToScalar(gCoord[1]));
30 | path.lineTo(SkIntToScalar(gCoord[2]), SkIntToScalar(gCoord[3]));
31 | path.lineTo(SkIntToScalar(gCoord[4]), SkIntToScalar(gCoord[5]));
32 | path.close();
33 | path.offset(SkIntToScalar(10), SkIntToScalar(0));
34 | return SkIntToScalar(30);
35 | }
36 | };
37 |
38 | static MakePathProc make_rect = new MakePathProc() {
39 | public float apply(SkPath path) {
40 | SkRect r = SkRect.MakeLTRB(SkIntToScalar(10), SkIntToScalar(10),
41 | SkIntToScalar(30), SkIntToScalar(30));
42 | path.addRect(r);
43 | path.offset(SkIntToScalar(10), SkIntToScalar(0));
44 | return SkIntToScalar(30);
45 | }
46 | };
47 |
48 | static MakePathProc make_oval = new MakePathProc() {
49 | public float apply(SkPath path) {
50 | SkRect r = SkRect.MakeLTRB(SkIntToScalar(10), SkIntToScalar(10),
51 | SkIntToScalar(30), SkIntToScalar(30));
52 | path.addOval(r);
53 | path.offset(SkIntToScalar(10), SkIntToScalar(0));
54 | return SkIntToScalar(30);
55 | }
56 | };
57 |
58 | static MakePathProc make_sawtooth = new MakePathProc() {
59 | public float apply(SkPath path) {
60 | float x = SkIntToScalar(20);
61 | float y = SkIntToScalar(20);
62 | final float x0 = x;
63 | final float dx = SK_Scalar1 * 5;
64 | final float dy = SK_Scalar1 * 10;
65 |
66 | path.moveTo(x, y);
67 | for (int i = 0; i < 32; i++) {
68 | x += dx;
69 | path.lineTo(x, y - dy);
70 | x += dx;
71 | path.lineTo(x, y + dy);
72 | }
73 | path.lineTo(x, y + (2 * dy));
74 | path.lineTo(x0, y + (2 * dy));
75 | path.close();
76 | return SkIntToScalar(30);
77 | }
78 | };
79 |
80 | static float make_star(SkPath path, int n) {
81 | final float c = SkIntToScalar(45);
82 | final float r = SkIntToScalar(20);
83 |
84 | float rad = -SK_ScalarPI / 2;
85 | final float drad = (n >>> 1) * SK_ScalarPI * 2 / n;
86 |
87 | path.moveTo(c, c - r);
88 | for (int i = 1; i < n; i++) {
89 | rad += drad;
90 | float cosV = SkScalarCos(rad), sinV = SkScalarSin(rad);
91 | path.lineTo(c + SkScalarMul(cosV, r), c + SkScalarMul(sinV, r));
92 | }
93 | path.close();
94 | return r * 2 * 6 / 5;
95 | }
96 |
97 | static MakePathProc make_star_5 = new MakePathProc() {
98 | public float apply(SkPath path) { return make_star(path, 5); }
99 | };
100 |
101 | static MakePathProc make_star_13 = new MakePathProc() {
102 | public float apply(SkPath path) { return make_star(path, 13); }
103 | };
104 |
105 | static final MakePathProc[] gProcs = {
106 | make_frame,
107 | make_triangle,
108 | make_rect,
109 | make_oval,
110 | make_sawtooth,
111 | make_star_5,
112 | make_star_13
113 | };
114 |
115 | static final int N = gProcs.length;
116 |
117 | public static class PathFillGM extends GM {
118 | private SkPath[] fPath = new SkPath[N];
119 | private float[] fDY = new float[N];
120 | public PathFillGM() {
121 | for (int i = 0; i < N; i++) {
122 | fPath[i] = new SkPath();
123 | fDY[i] = gProcs[i].apply(fPath[i]);
124 | }
125 | }
126 |
127 | @Override
128 | protected String onShortName() {
129 | return "pathfill";
130 | }
131 |
132 | @Override
133 | protected SkISize onISize() {
134 | return make_isize(640, 480);
135 | }
136 |
137 | @Override
138 | protected void onDraw(SkCanvas canvas) {
139 | SkPaint paint = new SkPaint();
140 | paint.setAntiAlias(true);
141 |
142 | for (int i = 0; i < N; i++) {
143 | canvas.drawPath(fPath[i], paint);
144 | canvas.translate(SkIntToScalar(0), fDY[i]);
145 | }
146 | }
147 | }
148 |
149 | public static GMRegistry.Factory MyFactory = new GMRegistry.Factory() {
150 | public GM apply() {
151 | return new PathFillGM();
152 | }
153 | };
154 | }
155 |
--------------------------------------------------------------------------------
/src/test/java/skia/javacpp/gm/pathreverse.java:
--------------------------------------------------------------------------------
1 | package skia.javacpp.gm;
2 |
3 | import static skia.javacpp.core.*;
4 |
5 | public class pathreverse {
6 | static void test_path(SkCanvas canvas, SkPath path) {
7 | SkPaint paint = new SkPaint();
8 | paint.setAntiAlias(true);
9 | canvas.drawPath(path, paint);
10 |
11 | paint.setStyle(SkPaint.kStroke_Style);
12 | paint.setColor(SK_ColorRED);
13 | canvas.drawPath(path, paint);
14 | }
15 |
16 | static void test_rev(SkCanvas canvas, SkPath path) {
17 | test_path(canvas, path);
18 |
19 | SkPath rev = new SkPath();
20 | rev.reverseAddPath(path);
21 | canvas.save();
22 | canvas.translate(150, 0);
23 | test_path(canvas, rev);
24 | canvas.restore();
25 | }
26 |
27 | static void test_rev(SkCanvas canvas) {
28 | SkRect r = SkRect.MakeLTRB(10, 10, 100, 60);
29 |
30 | SkPath path = new SkPath();
31 |
32 | path.addRect(r); test_rev(canvas, path);
33 |
34 | canvas.translate(0, 100);
35 | path.offset(20, 20);
36 | path.addRect(r); test_rev(canvas, path);
37 |
38 | canvas.translate(0, 100);
39 | path.reset();
40 | path.moveTo(10, 10); path.lineTo(30, 30);
41 | path.addOval(r);
42 | r.offset(50, 20);
43 | path.addOval(r);
44 | test_rev(canvas, path);
45 |
46 | SkPaint paint = new SkPaint();
47 | paint.setTextSize(SkIntToScalar(100));
48 | SkTypeface hira = SkTypeface.CreateFromName("Hiragino Maru Gothic Pro", SkTypeface.kNormal);
49 | paint.setTypeface(hira);
50 | path.reset();
51 | paint.getTextPath("e", 50, 50, path);
52 | canvas.translate(0, 100);
53 | test_rev(canvas, path);
54 | }
55 |
56 | public static class PathReverseGM extends GM {
57 | @Override
58 | protected String onShortName() {
59 | return "path-reverse";
60 | }
61 |
62 | @Override
63 | protected SkISize onISize() {
64 | return make_isize(640, 480);
65 | }
66 |
67 | @Override
68 | protected void onDraw(SkCanvas canvas) {
69 | SkRect r = SkRect.MakeLTRB(10, 10, 100, 60);
70 |
71 | SkPath path = new SkPath();
72 |
73 | path.addRect(r); test_rev(canvas, path);
74 |
75 | canvas.translate(0, 100);
76 | path.offset(20, 20);
77 | path.addRect(r); test_rev(canvas, path);
78 |
79 | canvas.translate(0, 100);
80 | path.reset();
81 | path.moveTo(10, 10); path.lineTo(30, 30);
82 | path.addOval(r);
83 | r.offset(50, 20);
84 | path.addOval(r);
85 | test_rev(canvas, path);
86 |
87 | SkPaint paint = new SkPaint();
88 | paint.setTextSize(SkIntToScalar(100));
89 | SkTypeface hira = SkTypeface.CreateFromName("Hiragino Maru Gothic Pro", SkTypeface.kNormal);
90 | paint.setTypeface(hira);
91 | path.reset();
92 | paint.getTextPath("e", 50, 50, path);
93 | canvas.translate(0, 100);
94 | test_rev(canvas, path);
95 | }
96 | }
97 |
98 | public static GMRegistry.Factory MyFactory = new GMRegistry.Factory() {
99 | public GM apply() {
100 | return new PathReverseGM();
101 | }
102 | };
103 | }
104 |
--------------------------------------------------------------------------------
/src/test/java/skia/javacpp/gm/points.java:
--------------------------------------------------------------------------------
1 | package skia.javacpp.gm;
2 |
3 | import static skia.javacpp.core.*;
4 |
5 | public class points {
6 | public static class PointsGM extends GM {
7 | @Override
8 | protected String onShortName() {
9 | return "points";
10 | }
11 |
12 | @Override
13 | protected SkISize onISize() {
14 | return make_isize(640, 490);
15 | }
16 |
17 | static void fill_pts(SkPoint pts[], SkRandom rand) {
18 | for (int i = 0; i < pts.length; i++) {
19 | float y = rand.nextUScalar1() * 480;
20 | float x = rand.nextUScalar1() * 640;
21 | pts[i] = new SkPoint();
22 | pts[i].set(x, y);
23 | }
24 | }
25 |
26 | @Override
27 | protected void onDraw(SkCanvas canvas) {
28 | canvas.translate(SK_Scalar1, SK_Scalar1);
29 |
30 | SkRandom rand = new SkRandom();
31 | SkPaint p0 = new SkPaint(), p1 = new SkPaint(), p2 = new SkPaint(), p3 = new SkPaint();
32 | final int n = 99;
33 |
34 | p0.setColor(SK_ColorRED);
35 | p1.setColor(SK_ColorGREEN);
36 | p2.setColor(SK_ColorBLUE);
37 | p3.setColor(SK_ColorWHITE);
38 |
39 | p0.setStrokeWidth(SkIntToScalar(4));
40 | p2.setStrokeCap(SkPaint.kRound_Cap);
41 | p2.setStrokeWidth(SkIntToScalar(6));
42 |
43 | SkPoint[] pts = new SkPoint[n];
44 | fill_pts(pts, rand);
45 |
46 | canvas.drawPoints(SkCanvas.kPolygon_PointMode, pts, p0);
47 | canvas.drawPoints(SkCanvas.kLines_PointMode, pts, p1);
48 | canvas.drawPoints(SkCanvas.kPoints_PointMode, pts, p2);
49 | canvas.drawPoints(SkCanvas.kPoints_PointMode, pts, p3);
50 | }
51 | }
52 |
53 | public static GMRegistry.Factory MyFactory = new GMRegistry.Factory() {
54 | public GM apply() {
55 | return new PointsGM();
56 | }
57 | };
58 | }
59 |
--------------------------------------------------------------------------------
/src/test/java/skia/javacpp/gm/poly2poly.java:
--------------------------------------------------------------------------------
1 | package skia.javacpp.gm;
2 |
3 | import static skia.javacpp.core.*;
4 |
5 | public class poly2poly {
6 | public static class Poly2PolyGM extends GM {
7 | @Override
8 | public String onShortName() {
9 | return "poly2poly";
10 | }
11 |
12 | @Override
13 | public SkISize onISize() {
14 | return make_isize(835, 840);
15 | }
16 |
17 | static void doDraw(SkCanvas canvas, SkPaint paint, int isrc[], int idst[], int count) {
18 | SkMatrix matrix = new SkMatrix();
19 | SkPoint[] src = new SkPoint[count], dst = new SkPoint[count];
20 |
21 | for (int i = 0; i < count; i++) {
22 | src[i] = new SkPoint();
23 | src[i].set(SkIntToScalar(isrc[2*i+0]), SkIntToScalar(isrc[2*i+1]));
24 | dst[i] = new SkPoint();
25 | dst[i].set(SkIntToScalar(idst[2*i+0]), SkIntToScalar(idst[2*i+1]));
26 | }
27 |
28 | canvas.save();
29 | matrix.setPolyToPoly(src, dst);
30 | canvas.concat(matrix);
31 |
32 | paint.setColor(SK_ColorGRAY);
33 | paint.setStyle(SkPaint.kStroke_Style);
34 | final float D = SkIntToScalar(64);
35 | canvas.drawRectCoords(0, 0, D, D, paint);
36 | canvas.drawLine(0, 0, D, D, paint);
37 | canvas.drawLine(0, D, D, 0, paint);
38 |
39 | SkPaint.FontMetrics fm = new SkPaint.FontMetrics();
40 | paint.getFontMetrics(fm);
41 | paint.setColor(SK_ColorRED);
42 | paint.setStyle(SkPaint.kFill_Style);
43 | float x = D/2;
44 | float y = D/2 - (fm.fAscent() + fm.fDescent())/2;
45 | String str = String.valueOf(count);
46 | canvas.drawText(str, x, y, paint);
47 |
48 | canvas.restore();
49 | }
50 |
51 | @Override
52 | protected void onDraw(SkCanvas canvas) {
53 | SkPaint paint = new SkPaint();
54 | paint.setAntiAlias(true);
55 | paint.setStrokeWidth(SkIntToScalar(4));
56 | paint.setTextSize(SkIntToScalar(40));
57 | paint.setTextAlign(SkPaint.kCenter_Align);
58 |
59 | canvas.save();
60 | canvas.translate(SkIntToScalar(10), SkIntToScalar(10));
61 | // translate (1 point)
62 | final int src1[] = { 0, 0 };
63 | final int dst1[] = { 5, 5 };
64 | doDraw(canvas, paint, src1, dst1, 1);
65 | canvas.restore();
66 |
67 | canvas.save();
68 | canvas.translate(SkIntToScalar(160), SkIntToScalar(10));
69 | // rotate/uniform-scale (2 points)
70 | final int src2[] = { 32, 32, 64, 32 };
71 | final int dst2[] = { 32, 32, 64, 48 };
72 | doDraw(canvas, paint, src2, dst2, 2);
73 | canvas.restore();
74 |
75 | canvas.save();
76 | canvas.translate(SkIntToScalar(10), SkIntToScalar(110));
77 | // rotate/skew (3 points)
78 | final int src3[] = { 0, 0, 64, 0, 0, 64 };
79 | final int dst3[] = { 0, 0, 96, 0, 24, 64 };
80 | doDraw(canvas, paint, src3, dst3, 3);
81 | canvas.restore();
82 |
83 | canvas.save();
84 | canvas.translate(SkIntToScalar(160), SkIntToScalar(110));
85 | // perspective (4 points)
86 | final int src4[] = { 0, 0, 64, 0, 64, 64, 0, 64 };
87 | final int dst4[] = { 0, 0, 96, 0, 64, 96, 0, 64 };
88 | doDraw(canvas, paint, src4, dst4, 4);
89 | canvas.restore();
90 | }
91 | }
92 |
93 | public static GMRegistry.Factory MyFactory = new GMRegistry.Factory() {
94 | public GM apply() {
95 | return new Poly2PolyGM();
96 | }
97 | };
98 | }
99 |
--------------------------------------------------------------------------------
/src/test/java/skia/javacpp/gm/shaderbounds.java:
--------------------------------------------------------------------------------
1 | package skia.javacpp.gm;
2 |
3 | import static skia.javacpp.core.*;
4 | import static skia.javacpp.effects.*;
5 |
6 | public class shaderbounds {
7 | static ShaderGenFunc MakeLinear = new ShaderGenFunc() {
8 | public SkShader apply(float width, float height, boolean alternate) {
9 | SkPoint[] pts = { SkPoint.Make(0, 0), SkPoint.Make(width, height)};
10 | int[] colors = {SK_ColorRED, SK_ColorGREEN};
11 | if (alternate) {
12 | pts[1].fY(0);
13 | colors[0] = SK_ColorBLUE;
14 | colors[1] = SK_ColorYELLOW;
15 | }
16 | return SkGradientShader.CreateLinear(pts, colors, null, SkShader.kClamp_TileMode, null);
17 | }
18 | };
19 |
20 | static interface ShaderGenFunc {
21 | public SkShader apply(float width, float height, boolean alternate);
22 | }
23 |
24 | public static class ShaderBoundsGM extends GM {
25 | public ShaderBoundsGM(ShaderGenFunc maker, String name) {
26 | fShaderMaker = maker;
27 | fName = name;
28 | }
29 |
30 | @Override
31 | protected String onShortName() {
32 | return fName;
33 | }
34 |
35 | @Override
36 | protected SkISize onISize() { return make_isize(320, 240); }
37 |
38 | @Override
39 | protected SkMatrix onGetInitialTransform() {
40 | SkMatrix result = new SkMatrix();
41 | float scale = SkFloatToScalar(0.8f);
42 | result.setScale(scale, scale);
43 | result.postTranslate(SkIntToScalar(7), SkIntToScalar(23));
44 | return result;
45 | }
46 |
47 | @Override
48 | protected void onDraw(SkCanvas canvas) {
49 | // The PDF device has already clipped to the content area, but we
50 | // do it again here so that the raster and pdf results are consistent.
51 | canvas.clipRect(SkRect.MakeWH(SkIntToScalar(320),
52 | SkIntToScalar(240)));
53 |
54 | SkMatrix canvasScale = new SkMatrix();
55 | float scale = SkFloatToScalar(0.7f);
56 | canvasScale.setScale(scale, scale);
57 | canvas.concat(canvasScale);
58 |
59 | // Background shader.
60 | SkPaint paint = new SkPaint();
61 | paint.setShader(MakeShader(559, 387, false));
62 | SkRect r = SkRect.MakeXYWH(SkIntToScalar(-12), SkIntToScalar(-41),
63 | SkIntToScalar(571), SkIntToScalar(428));
64 | canvas.drawRect(r, paint);
65 |
66 | // Constrained shader.
67 | paint.setShader(MakeShader(101, 151, true));
68 | r = SkRect.MakeXYWH(SkIntToScalar(43), SkIntToScalar(71),
69 | SkIntToScalar(101), SkIntToScalar(151));
70 | canvas.clipRect(r);
71 | canvas.drawRect(r, paint);
72 | }
73 |
74 | SkShader MakeShader(int width, int height, boolean background) {
75 | float scale = SkFloatToScalar(0.5f);
76 | if (background) {
77 | scale = SkFloatToScalar(0.6f);
78 | }
79 | float shaderWidth = SkIntToScalar(width)/scale;
80 | float shaderHeight = SkIntToScalar(height)/scale;
81 | SkShader shader = fShaderMaker.apply(shaderWidth, shaderHeight, background);
82 | SkMatrix shaderScale = new SkMatrix();
83 | shaderScale.setScale(scale, scale);
84 | shader.setLocalMatrix(shaderScale);
85 | return shader;
86 | }
87 |
88 | private ShaderGenFunc fShaderMaker;
89 | private String fName;
90 | //private SkShader MakeShader(boolean background);
91 | }
92 |
93 | public static GMRegistry.Factory MyFactory = new GMRegistry.Factory() {
94 | public GM apply() {
95 | return new ShaderBoundsGM(MakeLinear, "shaderbounds_linear");
96 | }
97 | };
98 | }
99 |
--------------------------------------------------------------------------------
/src/test/java/skia/javacpp/gm/shadertext.java:
--------------------------------------------------------------------------------
1 | package skia.javacpp.gm;
2 |
3 | import static skia.javacpp.core.*;
4 | import static skia.javacpp.effects.*;
5 | import static skia.javacpp.utils.*;
6 |
7 | public class shadertext {
8 | static void makebm(SkBitmap bm, int config, int w, int h) {
9 | bm.setConfig(config, w, h);
10 | bm.allocPixels();
11 | bm.eraseColor(0);
12 |
13 | SkCanvas canvas = new SkCanvas(bm);
14 | float s = SkIntToScalar(SkMin32(w, h));
15 | SkPoint[] pts = { SkPoint.Make(0, 0), SkPoint.Make(s, s) };
16 | int[] colors = { SK_ColorRED, SK_ColorGREEN, SK_ColorBLUE };
17 | float[] pos = { 0, SK_Scalar1/2, SK_Scalar1 };
18 | SkPaint paint = new SkPaint();
19 |
20 | SkUnitMapper um = null;
21 |
22 | um = new SkCosineMapper();
23 |
24 | paint.setDither(true);
25 | paint.setShader(SkGradientShader.CreateLinear(pts, colors, pos, SkShader.kClamp_TileMode, um));
26 | canvas.drawPaint(paint);
27 | }
28 |
29 | static SkShader MakeBitmapShader(int tx, int ty, int w, int h) {
30 | SkBitmap bmp = new SkBitmap();
31 | if (bmp.isNull()) {
32 | makebm(bmp, SkBitmap.kARGB_8888_Config, w/2, h/4);
33 | }
34 | return SkShader.CreateBitmapShader(bmp, tx, ty);
35 | }
36 |
37 | static class GradData {
38 | int fCount;
39 | final int[] fColors;
40 | final float[] fPos;
41 | public GradData(int count, int[] colors, float[] pos) {
42 | fCount = count;
43 | fColors = new int[count];
44 | System.arraycopy(colors, 0, fColors, 0, count);
45 | fPos = pos;
46 | }
47 | };
48 |
49 | static final int[] gColors = {
50 | SK_ColorRED, SK_ColorGREEN, SK_ColorBLUE, SK_ColorWHITE, SK_ColorBLACK
51 | };
52 |
53 | static final GradData[] gGradData = {
54 | new GradData(2, gColors, null),
55 | new GradData(5, gColors, null),
56 | };
57 |
58 | static final GradMaker MakeLinear = new GradMaker() {
59 | public SkShader apply(SkPoint[] pts, GradData data, int tm, SkUnitMapper mapper) {
60 | return SkGradientShader.CreateLinear(pts, data.fColors, data.fPos, tm, mapper);
61 | }
62 | };
63 |
64 | static final GradMaker MakeRadial = new GradMaker() {
65 | public SkShader apply(SkPoint[] pts, GradData data, int tm, SkUnitMapper mapper) {
66 | SkPoint center = new SkPoint();
67 | center.set(SkScalarAve(pts[0].fX(), pts[1].fX()),
68 | SkScalarAve(pts[0].fY(), pts[1].fY()));
69 | return SkGradientShader.CreateRadial(center, center.fX(), data.fColors,
70 | data.fPos, tm, mapper);
71 | }
72 | };
73 |
74 | static final GradMaker MakeSweep = new GradMaker() {
75 | public SkShader apply(SkPoint[] pts, GradData data, int tm, SkUnitMapper mapper) {
76 | SkPoint center = new SkPoint();
77 | center.set(SkScalarAve(pts[0].fX(), pts[1].fX()),
78 | SkScalarAve(pts[0].fY(), pts[1].fY()));
79 | return SkGradientShader.CreateSweep(center.fX(), center.fY(), data.fColors,
80 | data.fPos, mapper);
81 | }
82 | };
83 |
84 | static final GradMaker Make2Radial = new GradMaker() {
85 | public SkShader apply(SkPoint[] pts, GradData data, int tm, SkUnitMapper mapper) {
86 | SkPoint center0 = new SkPoint(), center1 = new SkPoint();
87 | center0.set(SkScalarAve(pts[0].fX(), pts[1].fX()),
88 | SkScalarAve(pts[0].fY(), pts[1].fY()));
89 | center1.set(SkScalarInterp(pts[0].fX(), pts[1].fX(), SkIntToScalar(3)/5),
90 | SkScalarInterp(pts[0].fY(), pts[1].fY(), SkIntToScalar(1)/4));
91 | return SkGradientShader.CreateTwoPointRadial(
92 | center1, (pts[1].fX() - pts[0].fX()) / 7,
93 | center0, (pts[1].fX() - pts[0].fX()) / 2,
94 | data.fColors, data.fPos, tm, mapper);
95 | }
96 | };
97 |
98 | static interface GradMaker {
99 | public SkShader apply(SkPoint[] pts, GradData data, int tm, SkUnitMapper mapper);
100 | }
101 | static final GradMaker[] gGradMakers = {
102 | MakeLinear, MakeRadial, MakeSweep, Make2Radial
103 | };
104 |
105 | public static class ShaderTextGM extends GM {
106 | public ShaderTextGM() {
107 | this.setBGColor(0xFFDDDDDD);
108 | }
109 |
110 | @Override
111 | protected String onShortName() {
112 | return "shadertext";
113 | }
114 |
115 | @Override
116 | protected SkISize onISize() { return make_isize(1450, 500); }
117 |
118 | @Override
119 | protected void onDraw(SkCanvas canvas) {
120 | String text = "Shaded Text";
121 | final int textLen = text.length();
122 | final int pointSize = 36;
123 |
124 | int w = pointSize * textLen;
125 | int h = pointSize;
126 |
127 | SkPoint[] pts = {
128 | SkPoint.Make(0, 0),
129 | SkPoint.Make(SkIntToScalar(w), SkIntToScalar(h))
130 | };
131 | float textBase = SkIntToScalar(h/2);
132 |
133 | int tileModes[] = {
134 | SkShader.kClamp_TileMode,
135 | SkShader.kRepeat_TileMode,
136 | SkShader.kMirror_TileMode
137 | };
138 |
139 | final int gradCount = gGradData.length * gGradMakers.length;
140 | final int bmpCount = tileModes.length * tileModes.length;
141 | SkShader[] shaders = new SkShader[gradCount + bmpCount];
142 |
143 | int shdIdx = 0;
144 | for (int d = 0; d < gGradData.length; ++d) {
145 | for (int m = 0; m < gGradMakers.length; ++m) {
146 | shaders[shdIdx++] = gGradMakers[m].apply(pts,
147 | gGradData[d],
148 | SkShader.kClamp_TileMode,
149 | null);
150 | }
151 | }
152 | for (int tx = 0; tx < tileModes.length; ++tx) {
153 | for (int ty = 0; ty < tileModes.length; ++ty) {
154 | shaders[shdIdx++] = MakeBitmapShader(tileModes[tx],
155 | tileModes[ty],
156 | w/8, h);
157 | }
158 | }
159 |
160 | SkPaint paint = new SkPaint();
161 | paint.setDither(true);
162 | paint.setAntiAlias(true);
163 | paint.setTextSize(SkIntToScalar(pointSize));
164 |
165 | canvas.save();
166 | canvas.translate(SkIntToScalar(20), SkIntToScalar(10));
167 |
168 | SkPath path = new SkPath();
169 | path.arcTo(SkRect.MakeXYWH(SkIntToScalar(-40), SkIntToScalar(15),
170 | SkIntToScalar(300), SkIntToScalar(90)),
171 | SkIntToScalar(225), SkIntToScalar(90),
172 | false);
173 | path.close();
174 |
175 | final int testsPerCol = 8;
176 | final int rowHeight = 60;
177 | final int colWidth = 300;
178 | canvas.save();
179 | for (int s = 0; s < shaders.length; s++) {
180 | canvas.save();
181 | int i = 2*s;
182 | canvas.translate(SkIntToScalar((i / testsPerCol) * colWidth),
183 | SkIntToScalar((i % testsPerCol) * rowHeight));
184 | paint.setShader(shaders[s]);
185 | canvas.drawText(text, 0, textBase, paint);
186 | canvas.restore();
187 | canvas.save();
188 | ++i;
189 | canvas.translate(SkIntToScalar((i / testsPerCol) * colWidth),
190 | SkIntToScalar((i % testsPerCol) * rowHeight));
191 | canvas.drawTextOnPath(text, path, null, paint);
192 | canvas.restore();
193 | }
194 | canvas.restore();
195 | }
196 | }
197 |
198 | public static GMRegistry.Factory MyFactory = new GMRegistry.Factory() {
199 | public GM apply() {
200 | return new ShaderTextGM();
201 | }
202 | };
203 | }
204 |
--------------------------------------------------------------------------------
/src/test/java/skia/javacpp/gm/shadows.java:
--------------------------------------------------------------------------------
1 | package skia.javacpp.gm;
2 |
3 | import static skia.javacpp.core.*;
4 | import static skia.javacpp.effects.*;
5 |
6 | public class shadows {
7 | static void setup(SkPaint paint, int c, float strokeWidth) {
8 | paint.setColor(c);
9 | if (strokeWidth < 0) {
10 | paint.setStyle(SkPaint.kFill_Style);
11 | } else {
12 | paint.setStyle(SkPaint.kStroke_Style);
13 | paint.setStrokeWidth(strokeWidth);
14 | }
15 | }
16 |
17 | public static class ShadowsGM extends GM {
18 | public SkPath fCirclePath = new SkPath();
19 | public SkRect fRect = new SkRect();
20 |
21 | public ShadowsGM() {
22 | this.setBGColor(0xFFDDDDDD);
23 | fCirclePath.addCircle(SkIntToScalar(20), SkIntToScalar(20), SkIntToScalar(10));
24 | fRect.set(SkIntToScalar(10), SkIntToScalar(10), SkIntToScalar(30), SkIntToScalar(30));
25 | }
26 |
27 | @Override
28 | protected String onShortName() {
29 | return "shadows";
30 | }
31 |
32 | @Override
33 | protected SkISize onISize() {
34 | return make_isize(200, 80);
35 | }
36 |
37 | @Override
38 | protected void onDraw(SkCanvas canvas) {
39 | SkBlurDrawLooper[] shadowLoopers = new SkBlurDrawLooper[5];
40 | shadowLoopers[0] = new SkBlurDrawLooper(SkIntToScalar(10), SkIntToScalar(5),
41 | SkIntToScalar(10), 0xFF0000FF,
42 | SkBlurDrawLooper.kIgnoreTransform_BlurFlag |
43 | SkBlurDrawLooper.kOverrideColor_BlurFlag |
44 | SkBlurDrawLooper.kHighQuality_BlurFlag);
45 | shadowLoopers[1] = new SkBlurDrawLooper(SkIntToScalar(10), SkIntToScalar(5),
46 | SkIntToScalar(10), 0xFF0000FF,
47 | SkBlurDrawLooper.kIgnoreTransform_BlurFlag |
48 | SkBlurDrawLooper.kOverrideColor_BlurFlag );
49 | shadowLoopers[2] = new SkBlurDrawLooper(SkIntToScalar(5), SkIntToScalar(5),
50 | SkIntToScalar(10), 0xFF000000,
51 | SkBlurDrawLooper.kIgnoreTransform_BlurFlag |
52 | SkBlurDrawLooper.kHighQuality_BlurFlag );
53 | shadowLoopers[3] = new SkBlurDrawLooper(SkIntToScalar(5), SkIntToScalar(-5),
54 | SkIntToScalar(-10), 0x7FFF0000,
55 | SkBlurDrawLooper.kIgnoreTransform_BlurFlag |
56 | SkBlurDrawLooper.kOverrideColor_BlurFlag |
57 | SkBlurDrawLooper.kHighQuality_BlurFlag );
58 | shadowLoopers[4] = new SkBlurDrawLooper(SkIntToScalar(0), SkIntToScalar(5),
59 | SkIntToScalar(5), 0xFF000000,
60 | SkBlurDrawLooper.kIgnoreTransform_BlurFlag |
61 | SkBlurDrawLooper.kOverrideColor_BlurFlag |
62 | SkBlurDrawLooper.kHighQuality_BlurFlag );
63 |
64 | class Rec {
65 | int fColor;
66 | float fStrokeWidth;
67 | public Rec(int color, float strokeWidth) {
68 | fColor = color;
69 | fStrokeWidth = strokeWidth;
70 | }
71 | }
72 | Rec[] gRec = {new Rec(SK_ColorRED, -SK_Scalar1), new Rec(SK_ColorGREEN, SkIntToScalar(4))};
73 |
74 | SkPaint paint = new SkPaint();
75 | paint.setAntiAlias(true);
76 | for (int i = 0; i < shadowLoopers.length; ++i) {
77 | int saveCount = canvas.getSaveCount();
78 | canvas.save();
79 | try {
80 | paint.setLooper(shadowLoopers[i]);
81 |
82 | canvas.translate(SkIntToScalar(i*40), SkIntToScalar(0));
83 | setup(paint, gRec[0].fColor, gRec[0].fStrokeWidth);
84 | canvas.drawRect(fRect, paint);
85 |
86 | canvas.translate(SkIntToScalar(0), SkIntToScalar(40));
87 | setup(paint, gRec[1].fColor, gRec[1].fStrokeWidth);
88 | canvas.drawPath(fCirclePath, paint);
89 | } finally {
90 | canvas.restoreToCount(saveCount);
91 | }
92 | }
93 | }
94 | }
95 |
96 | public static GMRegistry.Factory MyFactory = new GMRegistry.Factory() {
97 | public GM apply() {
98 | return new ShadowsGM();
99 | }
100 | };
101 | }
102 |
--------------------------------------------------------------------------------
/src/test/java/skia/javacpp/gm/strokefill.java:
--------------------------------------------------------------------------------
1 | package skia.javacpp.gm;
2 |
3 | import skia.javacpp.Skia;
4 |
5 | import static skia.javacpp.core.*;
6 |
7 | public class strokefill {
8 | public static class StrokeFillGM extends GM {
9 | @Override
10 | protected String onShortName() {
11 | return "stroke-fill";
12 | }
13 |
14 | @Override
15 | protected SkISize onISize() {
16 | return make_isize(640, 480);
17 | }
18 |
19 | static void show_bold(SkCanvas canvas, String text, float x, float y, SkPaint paint) {
20 | SkPaint p = new SkPaint(paint);
21 | canvas.drawText(text, x, y, p);
22 | p.setFakeBoldText(true);
23 | canvas.drawText(text, x, y + SkIntToScalar(120), p);
24 | }
25 |
26 | @Override
27 | protected void onDraw(SkCanvas canvas) {
28 | float x = SkIntToScalar(100);
29 | float y = SkIntToScalar(88);
30 |
31 | SkPaint paint = new SkPaint();
32 | paint.setAntiAlias(true);
33 | paint.setTextSize(SkIntToScalar(100));
34 | paint.setStrokeWidth(SkIntToScalar(5));
35 |
36 | SkTypeface face = SkTypeface.CreateFromName("Papyrus", SkTypeface.kNormal);
37 | paint.setTypeface(face);
38 | show_bold(canvas, "Hello", x, y, paint);
39 |
40 | face = SkTypeface.CreateFromName("Hiragino Maru Gothic Pro", SkTypeface.kNormal);
41 | paint.setTypeface(face);
42 | String hyphen = new String(new byte[] { (byte) 0xE3, (byte) 0x83, (byte) 0xBC }, Skia.UTF_8);
43 | show_bold(canvas, hyphen, x + SkIntToScalar(300), y, paint);
44 |
45 | paint.setStyle(SkPaint.kStrokeAndFill_Style);
46 |
47 | SkPath path = new SkPath();
48 | path.setFillType(SkPath.kWinding_FillType);
49 | path.addCircle(x, y + SkIntToScalar(200), SkIntToScalar(50), SkPath.kCW_Direction);
50 | path.addCircle(x, y + SkIntToScalar(200), SkIntToScalar(40), SkPath.kCCW_Direction);
51 | canvas.drawPath(path, paint);
52 |
53 | SkPath path2 = new SkPath();
54 | path2.setFillType(SkPath.kWinding_FillType);
55 | path2.addCircle(x + SkIntToScalar(120), y + SkIntToScalar(200), SkIntToScalar(50), SkPath.kCCW_Direction);
56 | path2.addCircle(x + SkIntToScalar(120), y + SkIntToScalar(200), SkIntToScalar(40), SkPath.kCW_Direction);
57 | canvas.drawPath(path2, paint);
58 |
59 | path2.reset();
60 | path2.addCircle(x + SkIntToScalar(240), y + SkIntToScalar(200), SkIntToScalar(50), SkPath.kCCW_Direction);
61 | canvas.drawPath(path2, paint);
62 | SkASSERT(path2.cheapIsDirection(SkPath.kCCW_Direction));
63 |
64 | path2.reset();
65 | SkASSERT(!path2.cheapComputeDirection(null));
66 | path2.addCircle(x + SkIntToScalar(360), y + SkIntToScalar(200), SkIntToScalar(50), SkPath.kCW_Direction);
67 | SkASSERT(path2.cheapIsDirection(SkPath.kCW_Direction));
68 | canvas.drawPath(path2, paint);
69 | }
70 | }
71 |
72 | public static GMRegistry.Factory MyFactory = new GMRegistry.Factory() {
73 | public GM apply() {
74 | return new StrokeFillGM();
75 | }
76 | };
77 | }
78 |
--------------------------------------------------------------------------------
/src/test/java/skia/javacpp/gm/strokerects.java:
--------------------------------------------------------------------------------
1 | package skia.javacpp.gm;
2 |
3 | import static skia.javacpp.core.*;
4 |
5 | public class strokerects {
6 | static final int W = 400;
7 | static final int H = 400;
8 | static final int N = 100;
9 |
10 | static final float SW = SkIntToScalar(W);
11 | static final float SH = SkIntToScalar(H);
12 |
13 | public static class StrokeRectGM extends GM {
14 | @Override
15 | protected String onShortName() {
16 | return "strokerects";
17 | }
18 |
19 | @Override
20 | protected SkISize onISize() {
21 | return make_isize(W*2, H*2);
22 | }
23 |
24 | static void rnd_rect(SkRect r, SkRandom rand) {
25 | float x = rand.nextUScalar1() * W;
26 | float y = rand.nextUScalar1() * H;
27 | float w = rand.nextUScalar1() * (W >>> 2);
28 | float h = rand.nextUScalar1() * (H >>> 2);
29 | float hoffset = rand.nextSScalar1();
30 | float woffset = rand.nextSScalar1();
31 |
32 | r.set(x, y, x + w, y + h);
33 | r.offset(-w/2 + woffset, -h/2 + hoffset);
34 | }
35 |
36 | @Override
37 | protected void onDraw(SkCanvas canvas) {
38 | SkPaint paint = new SkPaint();
39 | paint.setStyle(SkPaint.kStroke_Style);
40 |
41 | for (int y = 0; y < 2; y++) {
42 | paint.setAntiAlias(y != 0);
43 | for (int x = 0; x < 2; x++) {
44 | paint.setStrokeWidth(x * SkIntToScalar(3));
45 |
46 | int saveCount = canvas.getSaveCount();
47 | canvas.save();
48 | try {
49 | canvas.translate(SW * x, SH * y);
50 | canvas.clipRect(SkRect.MakeLTRB(SkIntToScalar(2), SkIntToScalar(2), SW - SkIntToScalar(2), SH - SkIntToScalar(2)));
51 |
52 | SkRandom rand = new SkRandom();
53 | for (int i = 0; i < N; i++) {
54 | SkRect r = new SkRect();
55 | rnd_rect(r, rand);
56 | canvas.drawRect(r, paint);
57 | }
58 | } finally {
59 | canvas.restoreToCount(saveCount);
60 | }
61 | }
62 | }
63 | }
64 | }
65 |
66 | public static GMRegistry.Factory MyFactory = new GMRegistry.Factory() {
67 | public GM apply() {
68 | return new StrokeRectGM();
69 | }
70 | };
71 | }
72 |
--------------------------------------------------------------------------------
/src/test/java/skia/javacpp/gm/strokes.java:
--------------------------------------------------------------------------------
1 | package skia.javacpp.gm;
2 |
3 | import static skia.javacpp.core.*;
4 |
5 | public class strokes {
6 | static final int W = 400;
7 | static final int H = 400;
8 | static final int N = 50;
9 |
10 | static final float SW = SkIntToScalar(W);
11 | static final float SH = SkIntToScalar(H);
12 |
13 | static void rnd_rect(SkRect r, SkPaint paint, SkRandom rand) {
14 | float x = rand.nextUScalar1() * W;
15 | float y = rand.nextUScalar1() * H;
16 | float w = rand.nextUScalar1() * (W >>> 2);
17 | float h = rand.nextUScalar1() * (H >>> 2);
18 | float hoffset = rand.nextSScalar1();
19 | float woffset = rand.nextSScalar1();
20 |
21 | r.set(x, y, x + w, y + h);
22 | r.offset(-w/2 + woffset, -h/2 + hoffset);
23 |
24 | paint.setColor(rand.nextU());
25 | paint.setAlpha(0xFF);
26 | }
27 |
28 | public static class StrokesGM extends GM {
29 | @Override
30 | protected String onShortName() {
31 | return "strokes_round";
32 | }
33 |
34 | @Override
35 | protected SkISize onISize() {
36 | return make_isize(W, H*2);
37 | }
38 |
39 | @Override
40 | protected void onDraw(SkCanvas canvas) {
41 | SkPaint paint = new SkPaint();
42 | paint.setStyle(SkPaint.kStroke_Style);
43 | paint.setStrokeWidth(SkIntToScalar(9)/2);
44 |
45 | for (int y = 0; y < 2; y++) {
46 | paint.setAntiAlias(y != 0);
47 | int saveCount = canvas.getSaveCount();
48 | canvas.save();
49 | try {
50 | canvas.translate(0, SH * y);
51 | canvas.clipRect(SkRect.MakeLTRB(
52 | SkIntToScalar(2), SkIntToScalar(2)
53 | , SW - SkIntToScalar(2), SH - SkIntToScalar(2)
54 | ));
55 |
56 | SkRandom rand = new SkRandom();
57 | for (int i = 0; i < N; i++) {
58 | SkRect r = new SkRect();
59 | rnd_rect(r, paint, rand);
60 | canvas.drawOval(r, paint);
61 | rnd_rect(r, paint, rand);
62 | canvas.drawRoundRect(r, r.width()/4, r.height()/4, paint);
63 | rnd_rect(r, paint, rand);
64 | }
65 | } finally {
66 | canvas.restoreToCount(saveCount);
67 | }
68 | }
69 | }
70 | }
71 |
72 | public static class Strokes2GM extends GM {
73 | private SkPath fPath = new SkPath();
74 |
75 | public Strokes2GM() {
76 | SkRandom rand = new SkRandom();
77 | fPath.moveTo(0, 0);
78 | for (int i = 0; i < 13; i++) {
79 | float x = rand.nextUScalar1() * (W >>> 1);
80 | float y = rand.nextUScalar1() * (H >>> 1);
81 | fPath.lineTo(x, y);
82 | }
83 | }
84 |
85 | @Override
86 | protected String onShortName() {
87 | return "strokes_poly";
88 | }
89 |
90 | @Override
91 | protected SkISize onISize() {
92 | return make_isize(W, H*2);
93 | }
94 |
95 | static void rotate(float angle, float px, float py, SkCanvas canvas) {
96 | SkMatrix matrix = new SkMatrix();
97 | matrix.setRotate(angle, px, py);
98 | canvas.concat(matrix);
99 | }
100 |
101 | @Override
102 | protected void onDraw(SkCanvas canvas) {
103 | canvas.drawColor(SK_ColorWHITE);
104 |
105 | SkPaint paint = new SkPaint();
106 | paint.setStyle(SkPaint.kStroke_Style);
107 | paint.setStrokeWidth(SkIntToScalar(9)/2);
108 |
109 | for (int y = 0; y < 2; y++) {
110 | paint.setAntiAlias(y != 0);
111 | int saveCount = canvas.getSaveCount();
112 | canvas.save();
113 | try {
114 | canvas.translate(0, SH * y);
115 | canvas.clipRect(SkRect.MakeLTRB(SkIntToScalar(2),
116 | SkIntToScalar(2),
117 | SW - SkIntToScalar(2),
118 | SH - SkIntToScalar(2)));
119 |
120 | SkRandom rand = new SkRandom();
121 | for (int i = 0; i < N/2; i++) {
122 | SkRect r = new SkRect();
123 | rnd_rect(r, paint, rand);
124 | rotate(SkIntToScalar(15), SW/2, SH/2, canvas);
125 | canvas.drawPath(fPath, paint);
126 | }
127 | } finally {
128 | canvas.restoreToCount(saveCount);
129 | }
130 | }
131 | }
132 | }
133 |
134 | public static GMRegistry.Factory MyFactory = new GMRegistry.Factory() {
135 | public GM apply() {
136 | return new StrokesGM();
137 | }
138 | };
139 |
140 | public static GMRegistry.Factory MyFactory2 = new GMRegistry.Factory() {
141 | public GM apply() {
142 | return new Strokes2GM();
143 | }
144 | };
145 | }
146 |
--------------------------------------------------------------------------------
/src/test/java/skia/javacpp/gm/tablecolorfilter.java:
--------------------------------------------------------------------------------
1 | package skia.javacpp.gm;
2 |
3 | import static skia.javacpp.core.*;
4 | import static skia.javacpp.effects.*;
5 |
6 | public class tablecolorfilter {
7 | static void make_bm0(SkBitmap bm) {
8 | int W = 120;
9 | int H = 120;
10 | bm.setConfig(SkBitmap.kARGB_8888_Config, W, H);
11 | bm.allocPixels();
12 | bm.eraseColor(0);
13 |
14 | SkCanvas canvas = new SkCanvas(bm);
15 | SkPaint paint = new SkPaint();
16 | SkPoint pts[] = { SkPoint.Make(0, 0), SkPoint.Make(SkIntToScalar(W), SkIntToScalar(H)) };
17 | int[] colors = {
18 | SK_ColorBLACK, SK_ColorGREEN, SK_ColorCYAN,
19 | SK_ColorRED, 0, SK_ColorBLUE, SK_ColorWHITE
20 | };
21 | SkShader s = SkGradientShader.CreateLinear(pts, colors, null, SkShader.kClamp_TileMode);
22 | paint.setShader(s);
23 | canvas.drawPaint(paint);
24 | }
25 |
26 | static void make_bm1(SkBitmap bm) {
27 | int W = 120;
28 | int H = 120;
29 | bm.setConfig(SkBitmap.kARGB_8888_Config, W, H);
30 | bm.allocPixels();
31 | bm.eraseColor(0);
32 |
33 | SkCanvas canvas = new SkCanvas(bm);
34 | SkPaint paint = new SkPaint();
35 | float cx = SkIntToScalar(W)/2;
36 | float cy = SkIntToScalar(H)/2;
37 | int[] colors = {
38 | SK_ColorRED, SK_ColorGREEN, SK_ColorBLUE,
39 | };
40 | SkShader s = SkGradientShader.CreateRadial(SkPoint.Make(SkIntToScalar(W) / 2,
41 | SkIntToScalar(H) / 2),
42 | SkIntToScalar(W) / 2, colors, null,
43 | SkShader.kClamp_TileMode);
44 | paint.setShader(s);
45 | paint.setAntiAlias(true);
46 | canvas.drawCircle(cx, cy, cx, paint);
47 | }
48 |
49 | static void make_table0(byte[] table) {
50 | for (int i = 0; i < 256; ++i) {
51 | int n = i >>> 5;
52 | table[i] = (byte)((n << 5) | (n << 2) | (n >>> 1));
53 | }
54 | }
55 | static void make_table1(byte[] table) {
56 | for (int i = 0; i < 256; ++i) {
57 | table[i] = (byte)(i * i / 255);
58 | }
59 | }
60 | static void make_table2(byte[] table) {
61 | for (int i = 0; i < 256; ++i) {
62 | float fi = i / 255.0f;
63 | table[i] = (byte)(Math.sqrt(fi) * 255);
64 | }
65 | }
66 |
67 | static SkColorFilter make_cf0() {
68 | byte[] table = new byte[256]; make_table0(table);
69 | return SkTableColorFilter.Create(table);
70 | }
71 | static SkColorFilter make_cf1() {
72 | byte[] table = new byte[256]; make_table1(table);
73 | return SkTableColorFilter.Create(table);
74 | }
75 | static SkColorFilter make_cf2() {
76 | byte[] table = new byte[256]; make_table2(table);
77 | return SkTableColorFilter.Create(table);
78 | }
79 | static SkColorFilter make_cf3() {
80 | byte[] table0 = new byte[256]; make_table0(table0);
81 | byte[] table1 = new byte[256]; make_table1(table1);
82 | byte[] table2 = new byte[256]; make_table2(table2);
83 | return SkTableColorFilter.CreateARGB(null, table0, table1, table2);
84 | }
85 |
86 | public static class TableColorFilterGM extends GM {
87 | @Override
88 | protected String onShortName() {
89 | return "tablecolorfilter";
90 | }
91 |
92 | @Override
93 | protected SkISize onISize() {
94 | return SkISize.Make(700, 300);
95 | }
96 |
97 | @Override
98 | protected void onDraw(SkCanvas canvas) {
99 | canvas.drawColor(0xFFDDDDDD);
100 | canvas.translate(20, 20);
101 |
102 | float x = 0, y = 0;
103 |
104 | SkBitmap[] gMakers = new SkBitmap[] { new SkBitmap(), new SkBitmap() };
105 | make_bm0(gMakers[0]);
106 | make_bm1(gMakers[1]);
107 | for (int maker = 0; maker < gMakers.length; ++maker) {
108 | SkBitmap bm = gMakers[maker];
109 | SkPaint paint = new SkPaint();
110 | x = 0;
111 | canvas.drawBitmap(bm, x, y, paint);
112 | paint.setColorFilter(make_cf0()); x += bm.width() * 9 / 8;
113 | canvas.drawBitmap(bm, x, y, paint);
114 | paint.setColorFilter(make_cf1()); x += bm.width() * 9 / 8;
115 | canvas.drawBitmap(bm, x, y, paint);
116 | paint.setColorFilter(make_cf2()); x += bm.width() * 9 / 8;
117 | canvas.drawBitmap(bm, x, y, paint);
118 | paint.setColorFilter(make_cf3()); x += bm.width() * 9 / 8;
119 | canvas.drawBitmap(bm, x, y, paint);
120 |
121 | y += bm.height() * 9 / 8;
122 | }
123 | }
124 | }
125 |
126 | public static GMRegistry.Factory MyFactory = new GMRegistry.Factory() {
127 | public GM apply() {
128 | return new TableColorFilterGM();
129 | }
130 | };
131 | }
132 |
--------------------------------------------------------------------------------
/src/test/java/skia/javacpp/gm/testimagefilters.java:
--------------------------------------------------------------------------------
1 | package skia.javacpp.gm;
2 |
3 | import static skia.javacpp.core.*;
4 | import static skia.javacpp.effects.*;
5 |
6 | public class testimagefilters {
7 | static final float FILTER_WIDTH = SkIntToScalar(150);
8 | static final float FILTER_HEIGHT = SkIntToScalar(200);
9 |
10 | static SkImageFilter make0() { return new SkDownSampleImageFilter(SK_Scalar1 / 5); }
11 | static SkImageFilter make1() { return new SkOffsetImageFilter(SkIntToScalar(16), SkIntToScalar(16)); }
12 | static SkImageFilter make2() {
13 | SkColorFilter cf = SkColorFilter.CreateModeFilter(SK_ColorBLUE, SkXfermode.kSrcIn_Mode);
14 | return new SkColorFilterImageFilter(cf);
15 | }
16 | static SkImageFilter make3() {
17 | return new SkBlurImageFilter(8, 0);
18 | }
19 |
20 | static SkImageFilter make4() {
21 | SkImageFilter outer = new SkOffsetImageFilter(SkIntToScalar(16), SkIntToScalar(16));
22 | SkImageFilter inner = new SkDownSampleImageFilter(SK_Scalar1 / 5);
23 | return new SkComposeImageFilter(outer, inner);
24 | }
25 | static SkImageFilter make5() {
26 | SkImageFilter first = new SkOffsetImageFilter(SkIntToScalar(16), SkIntToScalar(16));
27 | SkImageFilter second = new SkDownSampleImageFilter(SK_Scalar1 / 5);
28 | return new SkMergeImageFilter(first, second);
29 | }
30 |
31 | static SkImageFilter make6() {
32 | SkImageFilter outer = new SkOffsetImageFilter(SkIntToScalar(16), SkIntToScalar(16));
33 | SkImageFilter inner = new SkDownSampleImageFilter(SK_Scalar1 / 5);
34 | SkImageFilter compose = new SkComposeImageFilter(outer, inner);
35 |
36 | SkColorFilter cf = SkColorFilter.CreateModeFilter(0x880000FF, SkXfermode.kSrcIn_Mode);
37 |
38 | SkImageFilter blue = new SkColorFilterImageFilter(cf);
39 |
40 | return new SkMergeImageFilter(compose, blue);
41 | }
42 |
43 | static SkImageFilter make7() {
44 | SkImageFilter outer = new SkOffsetImageFilter(SkIntToScalar(16), SkIntToScalar(16));
45 | SkImageFilter inner = make3();
46 | SkImageFilter compose = new SkComposeImageFilter(outer, inner);
47 |
48 | SkColorFilter cf = SkColorFilter.CreateModeFilter(0x880000FF, SkXfermode.kSrcIn_Mode);
49 |
50 | SkImageFilter blue = new SkColorFilterImageFilter(cf);
51 |
52 | return new SkMergeImageFilter(compose, blue);
53 | }
54 |
55 | static void draw0(SkCanvas canvas) {
56 | SkPaint p = new SkPaint();
57 | p.setAntiAlias(true);
58 | SkRect r = SkRect.MakeWH(FILTER_WIDTH, FILTER_HEIGHT);
59 | r.inset(SK_Scalar1 * 12, SK_Scalar1 * 12);
60 | p.setColor(SK_ColorRED);
61 | canvas.drawOval(r, p);
62 | }
63 |
64 | public static class TestImageFiltersGM extends GM {
65 | @Override
66 | protected String onShortName() {
67 | return "testimagefilters";
68 | }
69 |
70 | @Override
71 | protected SkISize onISize() { return SkISize.Make(700, 460); }
72 |
73 | @Override
74 | protected void onDraw(SkCanvas canvas) {
75 | // this->drawSizeBounds(canvas, 0xFFCCCCCC);
76 |
77 | SkImageFilter[] gFilterProc = {
78 | make0(), make1(), make2(), make3(), make4(), make5(), make6(), make7()
79 | };
80 |
81 | final SkRect bounds = SkRect.MakeWH(FILTER_WIDTH, FILTER_HEIGHT);
82 |
83 | final float dx = bounds.width() * 8 / 7;
84 | final float dy = bounds.height() * 8 / 7;
85 |
86 | canvas.translate(SkIntToScalar(8), SkIntToScalar(8));
87 |
88 | for (int i = 0; i < gFilterProc.length; ++i) {
89 | int ix = i % 4;
90 | int iy = i / 4;
91 |
92 | int saveCount = canvas.getSaveCount();
93 | canvas.save();
94 | try {
95 | canvas.translate(ix * dx, iy * dy);
96 |
97 | SkPaint p = new SkPaint();
98 | p.setStyle(SkPaint.kStroke_Style);
99 | canvas.drawRect(bounds, p);
100 |
101 | SkPaint paint = new SkPaint();
102 | paint.setImageFilter(gFilterProc[i]);
103 | canvas.saveLayer(bounds, paint);
104 | draw0(canvas);
105 | } finally {
106 | canvas.restoreToCount(saveCount);
107 | }
108 | }
109 | }
110 | }
111 |
112 | public static GMRegistry.Factory MyFactory = new GMRegistry.Factory() {
113 | public GM apply() {
114 | return new TestImageFiltersGM();
115 | }
116 | };
117 | }
118 |
--------------------------------------------------------------------------------
/src/test/java/skia/javacpp/gm/tilemodes.java:
--------------------------------------------------------------------------------
1 | package skia.javacpp.gm;
2 |
3 | import static skia.javacpp.core.*;
4 | import static skia.javacpp.effects.*;
5 | import static skia.javacpp.utils.*;
6 |
7 | public class tilemodes {
8 | static void makebm(SkBitmap bm, int config, int w, int h) {
9 | bm.setConfig(config, w, h);
10 | bm.allocPixels();
11 | bm.eraseColor(0);
12 |
13 | SkCanvas canvas = new SkCanvas(bm);
14 | SkPoint[] pts = { SkPoint.Make(0, 0), SkPoint.Make(SkIntToScalar(w), SkIntToScalar(h)) };
15 | int[] colors = { SK_ColorRED, SK_ColorGREEN, SK_ColorBLUE };
16 | float[] pos = { 0, SK_Scalar1/2, SK_Scalar1 };
17 | SkPaint paint = new SkPaint();
18 |
19 | SkUnitMapper um = null;
20 |
21 | um = new SkCosineMapper();
22 | // um = new SkDiscreteMapper(12);
23 |
24 | paint.setDither(true);
25 | paint.setShader(SkGradientShader.CreateLinear(pts, colors, pos, SkShader.kClamp_TileMode, um));
26 | canvas.drawPaint(paint);
27 | }
28 |
29 | static void setup(SkPaint paint, SkBitmap bm, boolean filter,
30 | int tmx, int tmy) {
31 | SkShader shader = SkShader.CreateBitmapShader(bm, tmx, tmy);
32 | paint.setShader(shader);
33 | paint.setFilterBitmap(filter);
34 | }
35 |
36 | static final int[] gConfigs = {
37 | SkBitmap.kARGB_8888_Config,
38 | SkBitmap.kRGB_565_Config,
39 | SkBitmap.kARGB_4444_Config
40 | };
41 | static final int gWidth = 32;
42 | static final int gHeight = 32;
43 |
44 | public static class TilingGM extends GM {
45 | private SkBlurDrawLooper fLooper;
46 |
47 | public TilingGM() {
48 | fLooper = new SkBlurDrawLooper(SkIntToScalar(1), SkIntToScalar(2), SkIntToScalar(2),
49 | 0x88000000);
50 | for (int i = 0; i < gConfigs.length; i++) {
51 | fTexture[i] = new SkBitmap();
52 | makebm(fTexture[i], gConfigs[i], gWidth, gHeight);
53 | }
54 | }
55 |
56 | public SkBitmap[] fTexture = new SkBitmap[gConfigs.length];
57 |
58 | @Override
59 | protected String onShortName() {
60 | return "tilemodes";
61 | }
62 |
63 | @Override
64 | protected SkISize onISize() { return make_isize(880, 560); }
65 |
66 | @Override
67 | protected void onDraw(SkCanvas canvas) {
68 |
69 | SkRect r = SkRect.MakeLTRB(0, 0, SkIntToScalar(gWidth*2), SkIntToScalar(gHeight*2));
70 |
71 | final String[] gConfigNames = { "8888", "565", "4444" };
72 |
73 | final boolean[] gFilters = { false, true };
74 | final String[] gFilterNames = { "point", "bilinear" };
75 |
76 | final int[] gModes = { SkShader.kClamp_TileMode, SkShader.kRepeat_TileMode, SkShader.kMirror_TileMode };
77 | final String[] gModeNames = { "C", "R", "M" };
78 |
79 | float y = SkIntToScalar(24);
80 | float x = SkIntToScalar(10);
81 |
82 | for (int kx = 0; kx < gModes.length; kx++) {
83 | for (int ky = 0; ky < gModes.length; ky++) {
84 | SkPaint p = new SkPaint();
85 | String str;
86 | p.setAntiAlias(true);
87 | p.setDither(true);
88 | p.setLooper(fLooper);
89 | str = String.format("[%s,%s]", gModeNames[kx], gModeNames[ky]);
90 |
91 | p.setTextAlign(SkPaint.kCenter_Align);
92 | canvas.drawText(str, x + r.width()/2, y, p);
93 |
94 | x += r.width() * 4 / 3;
95 | }
96 | }
97 |
98 | y += SkIntToScalar(16);
99 |
100 | for (int i = 0; i < gConfigs.length; i++) {
101 | for (int j = 0; j < gFilters.length; j++) {
102 | x = SkIntToScalar(10);
103 | for (int kx = 0; kx < gModes.length; kx++) {
104 | for (int ky = 0; ky < gModes.length; ky++) {
105 | SkPaint paint = new SkPaint();
106 | setup(paint, fTexture[i], gFilters[j], gModes[kx], gModes[ky]);
107 | paint.setDither(true);
108 |
109 | canvas.save();
110 | canvas.translate(x, y);
111 | canvas.drawRect(r, paint);
112 | canvas.restore();
113 |
114 | x += r.width() * 4 / 3;
115 | }
116 | }
117 | {
118 | SkPaint p = new SkPaint();
119 | String str;
120 | p.setAntiAlias(true);
121 | p.setLooper(fLooper);
122 | str = String.format("%s, %s", gConfigNames[i], gFilterNames[j]);
123 | canvas.drawText(str, x, y + r.height() * 2 / 3, p);
124 | }
125 |
126 | y += r.height() * 4 / 3;
127 | }
128 | }
129 | }
130 | }
131 |
132 | public static GMRegistry.Factory MyFactory = new GMRegistry.Factory() {
133 | public GM apply() {
134 | return new TilingGM();
135 | }
136 | };
137 | }
138 |
--------------------------------------------------------------------------------
/src/test/java/skia/javacpp/gm/tinybitmap.java:
--------------------------------------------------------------------------------
1 | package skia.javacpp.gm;
2 |
3 | import com.googlecode.javacpp.IntPointer;
4 |
5 | import static skia.javacpp.core.*;
6 |
7 | public class tinybitmap {
8 | static SkBitmap make_bitmap() {
9 | SkBitmap bm = new SkBitmap();
10 |
11 | SkColorTable ctable = new SkColorTable(1);
12 | IntPointer c = ctable.lockColors();
13 | c.put(0, SkPackARGB32(0x80, 0x80, 0, 0));
14 | ctable.unlockColors(true);
15 |
16 | bm.setConfig(SkBitmap.kIndex8_Config, 1, 1);
17 | bm.allocPixels(ctable);
18 |
19 | bm.lockPixels();
20 | bm.getAddr8(0, 0).put((byte) 0);
21 | bm.unlockPixels();
22 | return bm;
23 | }
24 |
25 | public static class TinyBitmapGM extends GM {
26 | private SkBitmap fBM;
27 | public TinyBitmapGM() {
28 | this.setBGColor(0xFFDDDDDD);
29 | fBM = make_bitmap();
30 | }
31 |
32 | @Override
33 | protected String onShortName() {
34 | return "tinybitmap";
35 | }
36 |
37 | @Override
38 | protected SkISize onISize() { return make_isize(100, 100); }
39 |
40 | @Override
41 | protected void onDraw(SkCanvas canvas) {
42 | SkShader s =
43 | SkShader.CreateBitmapShader(fBM, SkShader.kRepeat_TileMode,
44 | SkShader.kMirror_TileMode);
45 | SkPaint paint = new SkPaint();
46 | paint.setAlpha(0x80);
47 | paint.setShader(s);
48 | canvas.drawPaint(paint);
49 | }
50 | }
51 |
52 | public static GMRegistry.Factory MyFactory = new GMRegistry.Factory() {
53 | public GM apply() {
54 | return new TinyBitmapGM();
55 | }
56 | };
57 | }
58 |
--------------------------------------------------------------------------------
/src/test/java/skia/javacpp/gm/verttext.java:
--------------------------------------------------------------------------------
1 | package skia.javacpp.gm;
2 |
3 | import static skia.javacpp.core.*;
4 |
5 | public class verttext {
6 | static final int TEXT_SIZE = 48;
7 | static final String gText = "Hello";
8 |
9 | public static class VertTextGM extends GM {
10 | @Override
11 | protected String onShortName() {
12 | return "verttext";
13 | }
14 |
15 | @Override
16 | protected SkISize onISize() { return make_isize(640, 480); }
17 |
18 | static void drawBaseline(SkCanvas canvas, SkPaint paint,
19 | float x, float y) {
20 | float total = paint.measureText(gText);
21 |
22 | SkPaint p = new SkPaint();
23 | p.setAntiAlias(true);
24 | p.setColor(0x80FF0000);
25 | canvas.drawLine(x, y,
26 | paint.isVerticalText() ? x : x + total,
27 | paint.isVerticalText() ? y + total : y,
28 | p);
29 |
30 | p.setColor(0xFF0000FF);
31 | float[] adv = new float[gText.length()];
32 | paint.getTextWidths(gText, adv, null);
33 | for (int i = 0; i < gText.length(); ++i) {
34 | canvas.drawCircle(x, y, SK_Scalar1 * 3 / 2, p);
35 | if (paint.isVerticalText()) {
36 | y += adv[i];
37 | } else {
38 | x += adv[i];
39 | }
40 | }
41 | canvas.drawCircle(x, y, SK_Scalar1 * 3 / 2, p);
42 | }
43 |
44 | @Override
45 | protected void onDraw(SkCanvas canvas) {
46 | float x = SkIntToScalar(100);
47 | float y = SkIntToScalar(50);
48 |
49 | for (int i = 0; i < 4; ++i) {
50 | SkPaint paint = new SkPaint();
51 | paint.setAntiAlias(true);
52 | paint.setTextSize(SkIntToScalar(TEXT_SIZE));
53 |
54 |
55 | paint.setVerticalText(false);
56 | drawBaseline(canvas, paint, x, y);
57 | canvas.drawText(gText, x, y, paint);
58 |
59 | paint.setVerticalText(true);
60 | drawBaseline(canvas, paint, x, y);
61 | canvas.drawText(gText, x, y, paint);
62 |
63 | x += SkIntToScalar(40);
64 | y += SkIntToScalar(120);
65 |
66 | canvas.rotate(SkIntToScalar(-15));
67 | }
68 | }
69 | }
70 |
71 | public static GMRegistry.Factory MyFactory = new GMRegistry.Factory() {
72 | public GM apply() {
73 | return new VertTextGM();
74 | }
75 | };
76 | }
77 |
--------------------------------------------------------------------------------
/src/test/java/skia/javacpp/gm/verttext2.java:
--------------------------------------------------------------------------------
1 | package skia.javacpp.gm;
2 |
3 | import static skia.javacpp.core.*;
4 |
5 | public class verttext2 {
6 | public static class VertText2GM extends GM {
7 | public VertText2GM() {
8 | final int pointSize = 24;
9 | textHeight = SkIntToScalar(pointSize);
10 | prop = SkTypeface.CreateFromName("Helvetica", SkTypeface.kNormal);
11 | mono = SkTypeface.CreateFromName("Courier New", SkTypeface.kNormal);
12 | }
13 |
14 | @Override
15 | protected String onShortName() {
16 | return "verttext2";
17 | }
18 |
19 | @Override
20 | protected SkISize onISize() { return make_isize(640, 480); }
21 |
22 | @Override
23 | protected void onDraw(SkCanvas canvas) {
24 |
25 | for (int i = 0; i < 3; ++i) {
26 | SkPaint paint = new SkPaint();
27 | paint.setColor(SK_ColorRED);
28 | paint.setAntiAlias(true);
29 | y = textHeight;
30 | canvas.drawLine(0, SkIntToScalar(10),
31 | SkIntToScalar(110), SkIntToScalar(10), paint);
32 | canvas.drawLine(0, SkIntToScalar(240),
33 | SkIntToScalar(110), SkIntToScalar(240), paint);
34 | canvas.drawLine(0, SkIntToScalar(470),
35 | SkIntToScalar(110), SkIntToScalar(470), paint);
36 | drawText(canvas, "Proportional / Top Aligned",
37 | prop, SkPaint.kLeft_Align);
38 | drawText(canvas, "< Proportional / Centered >",
39 | prop, SkPaint.kCenter_Align);
40 | drawText(canvas, "Monospaced / Top Aligned",
41 | mono, SkPaint.kLeft_Align);
42 | drawText(canvas, "< Monospaced / Centered >",
43 | mono, SkPaint.kCenter_Align);
44 | canvas.rotate(SkIntToScalar(-15));
45 | canvas.translate(textHeight * 4, SkIntToScalar(50));
46 | if (i > 0) {
47 | canvas.translate(0, SkIntToScalar(50));
48 | }
49 | }
50 | }
51 |
52 | void drawText(SkCanvas canvas, String string,
53 | SkTypeface family, int alignment) {
54 | SkPaint paint = new SkPaint();
55 | paint.setColor(SK_ColorBLACK);
56 | paint.setAntiAlias(true);
57 | paint.setVerticalText(true);
58 | paint.setTextAlign(alignment);
59 | paint.setTypeface(family);
60 | paint.setTextSize(textHeight);
61 |
62 | canvas.drawText(string, y,
63 | alignment == SkPaint.kLeft_Align ? 10 : 240, paint);
64 | y += textHeight;
65 | }
66 |
67 | private float y, textHeight;
68 | private SkTypeface prop;
69 | private SkTypeface mono;
70 | }
71 |
72 | public static GMRegistry.Factory MyFactory = new GMRegistry.Factory() {
73 | public GM apply() {
74 | return new VertText2GM();
75 | }
76 | };
77 | }
78 |
--------------------------------------------------------------------------------
/src/test/java/skia/javacpp/gm/xfermodes.java:
--------------------------------------------------------------------------------
1 | package skia.javacpp.gm;
2 |
3 | import com.googlecode.javacpp.ShortPointer;
4 |
5 | import static skia.javacpp.core.*;
6 |
7 | public class xfermodes {
8 | static void make_bitmaps(int w, int h, SkBitmap src, SkBitmap dst) {
9 | src.setConfig(SkBitmap.kARGB_8888_Config, w, h);
10 | src.allocPixels();
11 | src.eraseColor(0);
12 |
13 | SkCanvas c = new SkCanvas(src);
14 | SkPaint p = new SkPaint();
15 | SkRect r = new SkRect();
16 | float ww = SkIntToScalar(w);
17 | float hh = SkIntToScalar(h);
18 |
19 | p.setAntiAlias(true);
20 | p.setColor(0xFFFFCC44);
21 | r.set(0, 0, ww*3/4, hh*3/4);
22 | c.drawOval(r, p);
23 |
24 | dst.setConfig(SkBitmap.kARGB_8888_Config, w, h);
25 | dst.allocPixels();
26 | dst.eraseColor(0);
27 | c.setBitmapDevice(dst);
28 |
29 | p.setColor(0xFF66AAFF);
30 | r.set(ww/3, hh/3, ww*19/20, hh*19/20);
31 | c.drawRect(r, p);
32 | }
33 |
34 | public static class XfermodesGM extends GM {
35 | SkBitmap fBG = new SkBitmap();
36 | SkBitmap fSrcB = new SkBitmap(), fDstB = new SkBitmap();
37 | boolean fOnce;
38 |
39 | void draw_mode(SkCanvas canvas, SkXfermode mode, int alpha,
40 | float x, float y) {
41 | SkPaint p = new SkPaint();
42 |
43 | canvas.drawBitmap(fSrcB, x, y, p);
44 | p.setAlpha(alpha);
45 | p.setXfermode(mode);
46 | canvas.drawBitmap(fDstB, x, y, p);
47 | }
48 |
49 | void init() {
50 | if (!fOnce) {
51 | // Do all this work in a temporary so we get a deep copy
52 | short[] localData = { (short) 0xFFFF, (short) 0xCCCF, (short) 0xCCCF, (short) 0xFFFF };
53 | SkBitmap scratchBitmap = new SkBitmap();
54 | scratchBitmap.setConfig(SkBitmap.kARGB_4444_Config, 2, 2, 4);
55 | scratchBitmap.setPixels(new ShortPointer(localData));
56 | scratchBitmap.setIsOpaque(true);
57 | scratchBitmap.copyTo(fBG, SkBitmap.kARGB_4444_Config);
58 |
59 | make_bitmaps(W, H, fSrcB, fDstB);
60 | fOnce = true;
61 | }
62 | }
63 |
64 | public static final int W = 64;
65 | public static final int H = 64;
66 | public XfermodesGM() { fOnce = false; }
67 |
68 | @Override
69 | protected String onShortName() {
70 | return "xfermodes";
71 | }
72 |
73 | @Override
74 | protected SkISize onISize() {
75 | return make_isize(790, 640);
76 | }
77 |
78 | @Override
79 | protected void onDraw(SkCanvas canvas) {
80 | this.init();
81 |
82 | canvas.translate(SkIntToScalar(10), SkIntToScalar(20));
83 |
84 | class Mode {
85 | int fMode;
86 | String fLabel;
87 | public Mode(int mode, String label) {
88 | fMode = mode;
89 | fLabel = label;
90 | }
91 | }
92 | Mode[] gModes = {
93 | new Mode(SkXfermode.kClear_Mode, "Clear" ),
94 | new Mode(SkXfermode.kSrc_Mode, "Src" ),
95 | new Mode(SkXfermode.kDst_Mode, "Dst" ),
96 | new Mode(SkXfermode.kSrcOver_Mode, "SrcOver" ),
97 | new Mode(SkXfermode.kDstOver_Mode, "DstOver" ),
98 | new Mode(SkXfermode.kSrcIn_Mode, "SrcIn" ),
99 | new Mode(SkXfermode.kDstIn_Mode, "DstIn" ),
100 | new Mode(SkXfermode.kSrcOut_Mode, "SrcOut" ),
101 | new Mode(SkXfermode.kDstOut_Mode, "DstOut" ),
102 | new Mode(SkXfermode.kSrcATop_Mode, "SrcATop" ),
103 | new Mode(SkXfermode.kDstATop_Mode, "DstATop" ),
104 | new Mode(SkXfermode.kXor_Mode, "Xor" ),
105 |
106 | new Mode(SkXfermode.kPlus_Mode, "Plus" ),
107 | new Mode(SkXfermode.kMultiply_Mode, "Multiply" ),
108 | new Mode(SkXfermode.kScreen_Mode, "Screen" ),
109 | new Mode(SkXfermode.kOverlay_Mode, "Overlay" ),
110 | new Mode(SkXfermode.kDarken_Mode, "Darken" ),
111 | new Mode(SkXfermode.kLighten_Mode, "Lighten" ),
112 | new Mode(SkXfermode.kColorDodge_Mode, "ColorDodge" ),
113 | new Mode(SkXfermode.kColorBurn_Mode, "ColorBurn" ),
114 | new Mode(SkXfermode.kHardLight_Mode, "HardLight" ),
115 | new Mode(SkXfermode.kSoftLight_Mode, "SoftLight" ),
116 | new Mode(SkXfermode.kDifference_Mode, "Difference" ),
117 | new Mode(SkXfermode.kExclusion_Mode, "Exclusion" ),
118 | };
119 |
120 | final float w = SkIntToScalar(W);
121 | final float h = SkIntToScalar(H);
122 | SkShader s = SkShader.CreateBitmapShader(fBG,
123 | SkShader.kRepeat_TileMode,
124 | SkShader.kRepeat_TileMode);
125 | SkMatrix m = new SkMatrix();
126 | m.setScale(SkIntToScalar(6), SkIntToScalar(6));
127 | s.setLocalMatrix(m);
128 |
129 | SkPaint labelP = new SkPaint();
130 | labelP.setAntiAlias(true);
131 | labelP.setTextAlign(SkPaint.kCenter_Align);
132 |
133 | final int W = 5;
134 |
135 | float x0 = 0;
136 | for (int twice = 0; twice < 2; twice++) {
137 | float x = x0, y = 0;
138 | for (int i = 0; i < gModes.length; i++) {
139 | SkXfermode mode = SkXfermode.Create(gModes[i].fMode);
140 | SkRect r = new SkRect();
141 | r.set(x, y, x+w, y+h);
142 |
143 | SkPaint p = new SkPaint();
144 | p.setStyle(SkPaint.kFill_Style);
145 | p.setShader(s);
146 | canvas.drawRect(r, p);
147 |
148 | canvas.saveLayer(r, null, SkCanvas.kARGB_ClipLayer_SaveFlag);
149 | draw_mode(canvas, mode, twice != 0 ? 0x88 : 0xFF, r.fLeft(), r.fTop());
150 | canvas.restore();
151 |
152 | r.inset(-SK_ScalarHalf, -SK_ScalarHalf);
153 | p.setStyle(SkPaint.kStroke_Style);
154 | p.setShader(null);
155 | canvas.drawRect(r, p);
156 |
157 | // #if 1
158 | canvas.drawText(gModes[i].fLabel,
159 | x + w/2, y - labelP.getTextSize()/2, labelP);
160 | // #endif
161 | x += w + SkIntToScalar(10);
162 | if ((i % W) == W - 1) {
163 | x = x0;
164 | y += h + SkIntToScalar(30);
165 | }
166 | }
167 | x0 += SkIntToScalar(400);
168 | }
169 | }
170 | }
171 |
172 | public static GMRegistry.Factory MyFactory = new GMRegistry.Factory() {
173 | public GM apply() {
174 | return new XfermodesGM();
175 | }
176 | };
177 | }
178 |
--------------------------------------------------------------------------------