001: /*
002: *
003: *
004: * Copyright 1990-2007 Sun Microsystems, Inc. All Rights Reserved.
005: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER
006: *
007: * This program is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU General Public License version
009: * 2 only, as published by the Free Software Foundation.
010: *
011: * This program is distributed in the hope that it will be useful, but
012: * WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * General Public License version 2 for more details (a copy is
015: * included at /legal/license.txt).
016: *
017: * You should have received a copy of the GNU General Public License
018: * version 2 along with this work; if not, write to the Free Software
019: * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA
021: *
022: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
023: * Clara, CA 95054 or visit www.sun.com if you need additional
024: * information or have any questions.
025: */
026: package com.sun.perseus.j2d;
027:
028: import com.sun.pisces.PiscesRenderer;
029: import com.sun.pisces.Transform6;
030:
031: import org.w3c.dom.svg.SVGRect;
032:
033: /**
034: * RadialGradientPaint support.
035: *
036: * @version $Id: RadialGradientPaintDef.java,v 1.4 2006/04/21 06:35:33 st125089 Exp $
037: */
038: public class RadialGradientPaintDef implements PaintDef {
039: public static final int CYCLE_NONE = 0;
040: public static final int CYCLE_REPEAT = 1;
041: public static final int CYCLE_REFLECT = 2;
042:
043: private Transform6 IDENTITY = new Transform6();
044:
045: /**
046: * The gradient x-axis center origin
047: */
048: float cx;
049:
050: /**
051: * The gradient y-axis center origin
052: */
053: float cy;
054:
055: /**
056: * The gradient x-axis focal
057: */
058: float fx;
059:
060: /**
061: * The gradient y-axis focal
062: */
063: float fy;
064:
065: /**
066: * The gradient radius
067: */
068: float r;
069:
070: /**
071: * The gradient stops.
072: */
073: float[] fractions;
074:
075: /**
076: * The gradient stops, as fixed point values.
077: */
078: int[] frac;
079:
080: /**
081: * The array of ARGB color values.
082: */
083: int[] rgba;
084:
085: /**
086: * The last used rgba array, accounting for operation opacity.
087: */
088: int[] lrgba;
089:
090: /**
091: * The last paintOpacity.
092: */
093: int lastPaintOpacity = 255;
094:
095: /**
096: * One of the cycle methods (CYCLE_NONE,
097: * CYCLE_REPEAT, CYCLE_REFLECT
098: */
099: int cycleMethod;
100:
101: /**
102: * Set to true if this gradient is in objectBoundingBox space.
103: */
104: protected boolean isObjectBBox = false;
105:
106: /**
107: * An additional transform from the gradient space. This corresponds to a
108: * SVG gradientTransform attribute.
109: */
110: protected Transform gradientTransform;
111:
112: /**
113: * Constructs an <code>RadialGradientPaint</code>.
114: *
115: * @param cx the gradient x-axis origin
116: * @param cy the gradient y-axis orign
117: * @param fx the gradient x-axis focal point
118: * @param fy the gradient y-axis focal point
119: * @param r the gradient radius
120: * @param fractions the array of stop values
121: * @param rgba the array of ARGB color values
122: * @param cycleMethod one of the cycle methods (CYCLE_NONE,
123: * CYCLE_REPEAT, CYCLE_REFLECT
124: * @param isObjectBBox if set to true, the RenderGraphic's current
125: * paintTarget object bounding box should be used to append
126: * and additional transform to the gradientTransform. The objectBoundingBox
127: * transform is appended to the right of the gradientTransform.
128: * @param gradientTransform an additional transform to add between the
129: * device coordinate space and the gradient's coordinate space.
130: */
131: public RadialGradientPaintDef(final float cx, final float cy,
132: final float fx, final float fy, final float r,
133: final float[] fractions, final int[] rgba,
134: final int cycleMethod, final boolean isObjectBBox,
135: final Transform gradientTransform) {
136: this .cx = cx;
137: this .cy = cy;
138: this .fx = fx;
139: this .fy = fy;
140: this .r = r;
141: this .fractions = fractions;
142: this .rgba = rgba;
143: this .cycleMethod = cycleMethod;
144: this .isObjectBBox = isObjectBBox;
145: this .gradientTransform = gradientTransform;
146: }
147:
148: /**
149: * Sets the paint on a PiscesRender.
150: *
151: * @param rg the RenderGraphics on requesting the paint to be set.
152: * @param renderer the PiscesRender on which to set the paint.
153: * @param paintOpacity additional paint opacity.
154: */
155: public void setPaint(final PiscesRenderGraphics rg,
156: final PiscesRenderer pr, final int paintOpacity) {
157: // First, lazilly compute the fractions in fixed point.
158: if (frac == null) {
159: frac = new int[fractions.length];
160: for (int i = 0; i < fractions.length; i++) {
161: frac[i] = (int) (fractions[i] * 65536);
162: }
163: }
164:
165: // Now, lazilly compute the offset colors accounting for the current
166: // paint opacity.
167: int[] c = rgba;
168: if (paintOpacity != 255) {
169: c = lrgba;
170: if (paintOpacity != lastPaintOpacity) {
171: if (lrgba == null) {
172: lrgba = new int[rgba.length];
173: }
174: int a = 0;
175: for (int i = 0; i < rgba.length; i++) {
176: lrgba[i] = (rgba[i] & 0x00ffffff);
177: a = (paintOpacity * (0xff & (rgba[i] >> 24)) / 255);
178: lrgba[i] |= (a << 24);
179: }
180: lastPaintOpacity = paintOpacity;
181: }
182: }
183:
184: // Finally, compute the paint transform.
185:
186: // Start with the paintTarget's user space coordinate system.
187: Transform txf = null;
188: if (rg.paintTransform != null) {
189: txf = new Transform(rg.paintTransform);
190: } else {
191: txf = new Transform(rg.transform.m00 / 65536f,
192: rg.transform.m10 / 65536f,
193: rg.transform.m01 / 65536f,
194: rg.transform.m11 / 65536f,
195: rg.transform.m02 / 65536f,
196: rg.transform.m12 / 65536f);
197: }
198:
199: // Append the objectBoundingBox space to user space transform.
200: if (isObjectBBox) {
201: SVGRect bbox = rg.paintTarget.getBBox();
202: txf.mTranslate(bbox.getX(), bbox.getY());
203: txf.mScale(bbox.getWidth(), bbox.getHeight());
204: }
205:
206: // Now, append the gradient transform.
207: if (gradientTransform != null) {
208: txf.mMultiply(gradientTransform);
209: }
210:
211: Transform6 t = new Transform6();
212: t.m00 = (int) (txf.m0 * 65536);
213: t.m10 = (int) (txf.m1 * 65536);
214: t.m01 = (int) (txf.m2 * 65536);
215: t.m11 = (int) (txf.m3 * 65536);
216: t.m02 = (int) (txf.m4 * 65536);
217: t.m12 = (int) (txf.m5 * 65536);
218:
219: pr.setRadialGradient((int) (cx * 65536), (int) (cy * 65536),
220: (int) (fx * 65536), (int) (fy * 65536),
221: (int) (r * 65536), frac, rgba, cycleMethod, t);
222: }
223: }
|