001: /*
002: *
003: * Copyright 1990-2007 Sun Microsystems, Inc. All Rights Reserved.
004: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER
005: *
006: * This program is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU General Public License version
008: * 2 only, as published by the Free Software Foundation.
009: *
010: * This program is distributed in the hope that it will be useful, but
011: * WITHOUT ANY WARRANTY; without even the implied warranty of
012: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
013: * General Public License version 2 for more details (a copy is
014: * included at /legal/license.txt).
015: *
016: * You should have received a copy of the GNU General Public License
017: * version 2 along with this work; if not, write to the Free Software
018: * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
019: * 02110-1301 USA
020: *
021: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
022: * Clara, CA 95054 or visit www.sun.com if you need additional
023: * information or have any questions.
024: */
025:
026: package com.sun.pisces;
027:
028: public final class PiscesRenderer extends PathSink implements
029: NativeFinalization {
030: static {
031: PiscesLibrary.load();
032: }
033:
034: private static boolean messageShown = false;
035:
036: public static final int ARC_OPEN = 0;
037: public static final int ARC_CHORD = 1;
038: public static final int ARC_PIE = 2;
039:
040: long nativePtr = 0L;
041: final AbstractSurface surface;
042:
043: private final NativeFinalizer finalizer;
044:
045: static {
046: String strValue;
047: int strokeXBias = 0; // default x bias
048: int strokeYBias = 0; // default y bias
049:
050: strValue = Configuration.getProperty("pisces.stroke.xbias");
051: if (strValue != null) {
052: try {
053: strokeXBias = Integer.parseInt(strValue);
054: } catch (NumberFormatException e) {
055: }
056: }
057:
058: strValue = Configuration.getProperty("pisces.stroke.ybias");
059: if (strValue != null) {
060: try {
061: strokeYBias = Integer.parseInt(strValue);
062: } catch (NumberFormatException e) {
063: }
064: }
065:
066: staticInitialize(strokeXBias, strokeYBias);
067: }
068:
069: private void notImplemented() {
070: new RuntimeException().printStackTrace();
071: System.out.println("not implemented");
072:
073: // throw new RuntimeException("PiscesRendererNativeImpl: " +
074: // "Not implemented!");
075: }
076:
077: /**
078: * Creates a renderer that will write into a given pixel array.
079: *
080: * @param data the destination surface
081: * where pixel data should be written.
082: * @param width the width of the pixel array.
083: * @param height the height of the pixel array.
084: * @param offset the starting offset of the pixel array.
085: * @param scanlineStride the scanline stride of the pixel array, in array
086: * entries.
087: * @param pixelStride the pixel stride of the pixel array, in array
088: * entries.
089: * @param type the pixel format, one of the
090: * <code>RendererBase.TYPE_*</code> constants.
091: */
092: public PiscesRenderer(Object data, int width, int height,
093: int offset, int scanlineStride, int pixelStride, int type) {
094: this ((AbstractSurface) data);
095: }
096:
097: public PiscesRenderer(AbstractSurface surface) {
098: if (!messageShown) {
099: System.out
100: .println("Using Pisces Renderer (native version)");
101: }
102:
103: this .finalizer = NativeFinalizer.createInstance(this );
104: this .surface = surface;
105: initialize();
106: messageShown = true;
107: }
108:
109: private static native void staticInitialize(int strokeXBias,
110: int strokeYBias);
111:
112: private native void initialize();
113:
114: public native void setAntialiasing(boolean antialiasingOn);
115:
116: public native boolean getAntialiasing();
117:
118: /**
119: * Sets the current paint color.
120: *
121: * @param red a value between 0 and 255.
122: * @param green a value between 0 and 255.
123: * @param blue a value between 0 and 255.
124: * @param alpha a value between 0 and 255.
125: */
126: public native void setColor(int red, int green, int blue, int alpha);
127:
128: /**
129: * Sets the current paint color. An alpha value of 255 is used.
130: *
131: * @param red a value between 0 and 255.
132: * @param green a value between 0 and 255.
133: * @param blue a value between 0 and 255.
134: */
135: public void setColor(int red, int green, int blue) {
136: setColor(red, green, blue, 255);
137: }
138:
139: public native void setCompositeRule(int compositeRule);
140:
141: public native void setComposite(int compositeRule, float alpha);
142:
143: int[] gcm_fractions = null;
144: int[] gcm_rgba = null;
145: int gcm_cycleMethod = -1;
146: GradientColorMap gradientColorMap = null;
147:
148: private boolean arraysDiffer(int[] a, int[] b) {
149: if (a == null) {
150: return true;
151: }
152: int len = b.length;
153: if (a.length != len) {
154: return true;
155: }
156: for (int i = 0; i < len; i++) {
157: if (a[i] != b[i]) {
158: return true;
159: }
160: }
161:
162: return false;
163: }
164:
165: private int[] cloneArray(int[] src) {
166: int len = src.length;
167: int[] dst = new int[len];
168: System.arraycopy(src, 0, dst, 0, len);
169: return dst;
170: }
171:
172: private void setGradientColorMap(int[] fractions, int[] rgba,
173: int cycleMethod) {
174: if (fractions.length != rgba.length) {
175: throw new IllegalArgumentException(
176: "fractions.length != rgba.length!");
177: }
178:
179: if (gradientColorMap == null || gcm_cycleMethod != cycleMethod
180: || arraysDiffer(gcm_fractions, fractions)
181: || arraysDiffer(gcm_rgba, rgba)) {
182: this .gradientColorMap = new GradientColorMap(fractions,
183: rgba, cycleMethod);
184: this .gcm_cycleMethod = cycleMethod;
185: this .gcm_fractions = cloneArray(fractions);
186: this .gcm_rgba = cloneArray(rgba);
187: }
188: }
189:
190: private native void setLinearGradientImpl(int x0, int y0, int x1,
191: int y1, int[] colors, int cycleMethod,
192: Transform6 gradientTransform);
193:
194: public void setLinearGradient(int x0, int y0, int x1, int y1,
195: int[] fractions, int[] rgba, int cycleMethod,
196: Transform6 gradientTransform) {
197: setGradientColorMap(fractions, rgba, cycleMethod);
198: setLinearGradientImpl(x0, y0, x1, y1, gradientColorMap.colors,
199: cycleMethod, gradientTransform);
200: }
201:
202: /**
203: * Java2D-style linear gradient creation. The color changes proportionally
204: * between point P0 (color0) nad P1 (color1). Cycle method constants are
205: * defined in GradientColorMap (CYCLE_*).
206: *
207: * @param x0 x coordinate of point P0
208: * @param y0 y coordinate of point P0
209: * @param color0 color of P0
210: * @param x1 x coordinate of point P1
211: * @param y1 y coordinate of point P1
212: * @param color1 color of P1
213: * @param cycleMethod type of cycling of the gradient (NONE, REFLECT, REPEAT)
214: *
215: */
216: public void setLinearGradient(int x0, int y0, int color0, int x1,
217: int y1, int color1, int cycleMethod) {
218: int[] fractions = { 0x0000, 0x10000 };
219: int[] rgba = { color0, color1 };
220: Transform6 ident = new Transform6(1 << 16, 0, 0, 1 << 16, 0, 0);
221: setLinearGradient(x0, y0, x1, y1, fractions, rgba, cycleMethod,
222: ident);
223: }
224:
225: private native void setRadialGradientImpl(int cx, int cy, int fx,
226: int fy, int radius, int[] colors, int cycleMethod,
227: Transform6 gradientTransform);
228:
229: public void setRadialGradient(int cx, int cy, int fx, int fy,
230: int radius, int[] fractions, int[] rgba, int cycleMethod,
231: Transform6 gradientTransform) {
232: setGradientColorMap(fractions, rgba, cycleMethod);
233: setRadialGradientImpl(cx, cy, fx, fy, radius,
234: gradientColorMap.colors, cycleMethod, gradientTransform);
235: }
236:
237: public void setTextureOpacity(float opacity) {
238: notImplemented();
239: }
240:
241: public void setTexture(int imageType, Object imageData, int width,
242: int height, int offset, int stride,
243: Transform6 textureTransform, boolean repeat) {
244: if (imageData instanceof int[]) {
245: setTextureImpl(imageType, (int[]) imageData, width, height,
246: offset, stride, textureTransform, repeat);
247: }
248: }
249:
250: private native void setTextureImpl(int imageType, int[] imageData,
251: int width, int height, int offset, int stride,
252: Transform6 textureTransform, boolean repeat);
253:
254: public PathSink getStroker() {
255: notImplemented();
256: return null;
257: }
258:
259: public PathSink getFiller() {
260: notImplemented();
261: return null;
262: }
263:
264: public PathSink getTextFiller() {
265: notImplemented();
266: return null;
267: }
268:
269: /**
270: * Sets the current stroke parameters.
271: *
272: * @param lineWidth the sroke width, in S15.16 format.
273: * @param capStyle the line cap style, one of
274: * <code>Stroker.CAP_*</code>.
275: * @param joinStyle the line cap style, one of
276: * <code>Stroker.JOIN_*</code>.
277: * @param miterLimit the stroke miter limit, in S15.16 format.
278: * @param dashArray an <code>int</code> array containing the dash
279: * segment lengths in S15.16 format, or <code>null</code>.
280: * @param dashPhase the starting dash offset, in S15.16 format.
281: */
282: public native void setStroke(int lineWidth, int capStyle,
283: int joinStyle, int miterLimit, int[] dashArray,
284: int dashPhase);
285:
286: /**
287: * Sets the current transform from user to window coordinates.
288: *
289: * @param transform an <code>Transform6</code> object.
290: */
291: public native void setTransform(Transform6 transform);
292:
293: public Transform6 getTransform() {
294: Transform6 transform = new Transform6();
295: getTransformImpl(transform);
296: return transform;
297: }
298:
299: private native void getTransformImpl(Transform6 transform);
300:
301: /**
302: * Sets a clip rectangle for all primitives. Each primitive will be
303: * clipped to the intersection of this rectangle and the destination
304: * image bounds.
305: */
306: public native void setClip(int minX, int minY, int width, int height);
307:
308: /**
309: * Resets the clip rectangle. Each primitive will be clipped only
310: * to the destination image bounds.
311: */
312: public native void resetClip();
313:
314: public native void beginRendering(int windingRule);
315:
316: /**
317: * Begins the rendering of path data. The supplied clipping
318: * bounds are intersected against the current clip rectangle and
319: * the destination image bounds; only pixels within the resulting
320: * rectangle may be written to.
321: */
322: public native void beginRendering(int minX, int minY, int width,
323: int height, int windingRule);
324:
325: /**
326: * Completes the rendering of path data. Destination pixels will
327: * be written at this time.
328: */
329: public native void endRendering();
330:
331: /**
332: * Returns a bounding box containing all pixels drawn during the
333: * rendering of the most recent primitive
334: * (beginRendering/endRendering pair). The bounding box is
335: * returned in the form (x, y, width, height).
336: */
337: public native void getBoundingBox(int[] bbox);
338:
339: public native void setStroke();
340:
341: public native void setFill();
342:
343: public void setTextFill() {
344: notImplemented();
345: }
346:
347: public native void moveTo(int x0, int y0);
348:
349: public native void lineTo(int x1, int y1);
350:
351: public native void lineJoin();
352:
353: public native void quadTo(int x1, int y1, int x2, int y2);
354:
355: public native void cubicTo(int x1, int y1, int x2, int y2, int x3,
356: int y3);
357:
358: public native void close();
359:
360: public native void end();
361:
362: public native void drawLine(int x0, int y0, int x1, int y1);
363:
364: /**
365: *
366: * @param x the X coordinate in S15.16 format.
367: * @param y the Y coordinate in S15.16 format.
368: * @param w the width in S15.16 format.
369: * @param h the height in S15.16 format.
370: */
371: public native void fillRect(int x, int y, int w, int h);
372:
373: public native void drawRect(int x, int y, int w, int h);
374:
375: public native void drawOval(int x, int y, int w, int h);
376:
377: public native void fillOval(int x, int y, int w, int h);
378:
379: public native void fillRoundRect(int x, int y, int w, int h,
380: int aw, int ah);
381:
382: public native void drawRoundRect(int x, int y, int w, int h,
383: int aw, int ah);
384:
385: public native void drawArc(int x, int y, int width, int height,
386: int startAngle, int arcAngle, int arcType);
387:
388: public native void fillArc(int x, int y, int width, int height,
389: int startAngle, int arcAngle, int arcType);
390:
391: public void getImageData() {
392: notImplemented();
393: }
394:
395: public native void clearRect(int x, int y, int w, int h);
396:
397: public native void setPathData(float[] data, byte[] commands,
398: int nCommands);
399:
400: public native void nativeFinalize();
401: }
|