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.model;
027:
028: import com.sun.perseus.util.SVGConstants;
029:
030: import org.w3c.dom.DOMException;
031:
032: import org.w3c.dom.svg.SVGRect;
033:
034: import com.sun.perseus.j2d.LinearGradientPaintDef;
035: import com.sun.perseus.j2d.PaintDef;
036: import com.sun.perseus.j2d.PaintTarget;
037: import com.sun.perseus.j2d.Transform;
038:
039: /**
040: * <code>LienarGraident</code> represents an SVG Tiny 1.2
041: * <code><linearGradient></code> element.
042: * <br />
043: *
044: * @version $Id: LinearGradient.java,v 1.6 2006/06/29 10:47:32 ln156897 Exp $
045: */
046: public class LinearGradient extends GradientElement {
047: /**
048: * Gradient start on the x-axis
049: */
050: float x1;
051:
052: /**
053: * Gradient start on the y-axis
054: */
055: float y1;
056:
057: /**
058: * Gradient end on the x-axis
059: */
060: float x2 = 1;
061:
062: /**
063: * Gradient end on the y-axis
064: */
065: float y2 = 0;
066:
067: /**
068: * Constructor.
069: *
070: * @param ownerDocument this element's owner <code>DocumentNode</code>
071: */
072: public LinearGradient(final DocumentNode ownerDocument) {
073: super (ownerDocument);
074:
075: isObjectBBox = true;
076: }
077:
078: /**
079: * @return the SVGConstants.SVG_LINEAR_GRADIENT_TAG value
080: */
081: public String getLocalName() {
082: return SVGConstants.SVG_LINEAR_GRADIENT_TAG;
083: }
084:
085: /**
086: * Used by <code>DocumentNode</code> to create a new instance from
087: * a prototype <code>Rect</code>.
088: *
089: * @param doc the <code>DocumentNode</code> for which a new node is
090: * should be created.
091: * @return a new <code>LinearGradient</code> for the requested document.
092: */
093: public ElementNode newInstance(final DocumentNode doc) {
094: return new LinearGradient(doc);
095: }
096:
097: /**
098: * Sets the x1 property.
099: *
100: * @param newX1 the new origin along the x-axis
101: */
102: public void setX1(final float newX1) {
103: if (newX1 == x1) {
104: return;
105: }
106:
107: this .x1 = newX1;
108: onPaintChange();
109: }
110:
111: /**
112: * @return the origin of the gradient starting point on the x-axis.
113: */
114: public float getX1() {
115: return x1;
116: }
117:
118: /**
119: * Sets the y1 property.
120: *
121: * @param newY1 the new origin along the y-axis
122: */
123: public void setY1(final float newY1) {
124: if (newY1 == y1) {
125: return;
126: }
127:
128: this .y1 = newY1;
129: onPaintChange();
130: }
131:
132: /**
133: * @return the origin of the gradient starting point on the y-axis.
134: */
135: public float getY1() {
136: return y1;
137: }
138:
139: /**
140: * Sets the x2 property.
141: *
142: * @param newX2 the new end along the x-axis
143: */
144: public void setX2(final float newX2) {
145: if (newX2 == x2) {
146: return;
147: }
148:
149: this .x2 = newX2;
150: onPaintChange();
151: }
152:
153: /**
154: * @return the end of the gradient starting point on the x-axis.
155: */
156: public float getX2() {
157: return x2;
158: }
159:
160: /**
161: * Sets the y2 property.
162: *
163: * @param newY2 the new end along the y-axis
164: */
165: public void setY2(final float newY2) {
166: if (newY2 == y2) {
167: return;
168: }
169:
170: this .y2 = newY2;
171: onPaintChange();
172: }
173:
174: /**
175: * @return the end of the gradient starting point on the y-axis.
176: */
177: public float getY2() {
178: return y2;
179: }
180:
181: /**
182: * LinearGradient handles x1, x2, y1, y2.
183: *
184: * @param traitName the name of the trait which the element may support.
185: * @return true if this element supports the given trait in one of the
186: * trait accessor methods.
187: */
188: boolean supportsTrait(final String traitName) {
189: if (SVGConstants.SVG_X1_ATTRIBUTE == traitName
190: || SVGConstants.SVG_X2_ATTRIBUTE == traitName
191: || SVGConstants.SVG_Y1_ATTRIBUTE == traitName
192: || SVGConstants.SVG_Y2_ATTRIBUTE == traitName) {
193: return true;
194: } else {
195: return super .supportsTrait(traitName);
196: }
197: }
198:
199: /**
200: * LinearGradient handles x1, y1, x2, y2 traits as
201: * FloatTraitAnims.
202: *
203: * @param traitName the trait name.
204: */
205: TraitAnim createTraitAnimImpl(final String traitName) {
206: if (SVGConstants.SVG_X1_ATTRIBUTE == traitName
207: || SVGConstants.SVG_X2_ATTRIBUTE == traitName
208: || SVGConstants.SVG_Y1_ATTRIBUTE == traitName
209: || SVGConstants.SVG_Y2_ATTRIBUTE == traitName) {
210: return new FloatTraitAnim(this , traitName, TRAIT_TYPE_FLOAT);
211: } else {
212: return super .createTraitAnimImpl(traitName);
213: }
214: }
215:
216: /**
217: * @param name the requested trait name (e.g., "ry")
218: * @return the trait's value, as a string.
219: *
220: * @throws DOMException with error code NOT_SUPPORTED_ERROR if the requested
221: * trait is not supported on this element or null.
222: * @throws DOMException with error code TYPE_MISMATCH_ERR if requested
223: * trait's computed value cannot be converted to a String (SVG Tiny only).
224: */
225: public String getTraitImpl(final String name) throws DOMException {
226: if (SVGConstants.SVG_X1_ATTRIBUTE == name) {
227: return Float.toString(x1);
228: } else if (SVGConstants.SVG_Y1_ATTRIBUTE == name) {
229: return Float.toString(y1);
230: } else if (SVGConstants.SVG_X2_ATTRIBUTE == name) {
231: return Float.toString(x2);
232: } else if (SVGConstants.SVG_Y2_ATTRIBUTE == name) {
233: return Float.toString(y2);
234: } else {
235: return super .getTraitImpl(name);
236: }
237: }
238:
239: /**
240: * @param name the requested trait name (e.g., "y")
241: * @return the requested trait value
242: *
243: * @throws DOMException with error code NOT_SUPPORTED_ERROR if the requested
244: * trait is not supported on this element or null.
245: * @throws DOMException with error code TYPE_MISMATCH_ERR if requested
246: * trait's computed value cannot be converted to a float
247: * @throws SecurityException if the application does not have the necessary
248: * privilege rights to access this (SVG) content.
249: */
250: float getFloatTraitImpl(final String name) throws DOMException {
251: if (SVGConstants.SVG_X1_ATTRIBUTE == name) {
252: return x1;
253: } else if (SVGConstants.SVG_Y1_ATTRIBUTE == name) {
254: return y1;
255: } else if (SVGConstants.SVG_X2_ATTRIBUTE == name) {
256: return x2;
257: } else if (SVGConstants.SVG_Y2_ATTRIBUTE == name) {
258: return y2;
259: } else {
260: return super .getFloatTraitImpl(name);
261: }
262: }
263:
264: /**
265: * Set the trait value as float array.
266: *
267: * @param name the trait's name.
268: * @param value the trait's value.
269: *
270: * @throws DOMException with error code NOT_SUPPORTED_ERROR if the requested
271: * trait is not supported on this element.
272: * @throws DOMException with error code TYPE_MISMATCH_ERR if the requested
273: * trait's value cannot be specified as a float
274: * @throws DOMException with error code INVALID_ACCESS_ERR if the input
275: * value is an invalid value for the given trait.
276: */
277: void setFloatArrayTrait(final String name, final float[][] value)
278: throws DOMException {
279: if (SVGConstants.SVG_X1_ATTRIBUTE == name) {
280: setX1(value[0][0]);
281: } else if (SVGConstants.SVG_Y1_ATTRIBUTE == name) {
282: setY1(value[0][0]);
283: } else if (SVGConstants.SVG_X2_ATTRIBUTE == name) {
284: setX2(value[0][0]);
285: } else if (SVGConstants.SVG_Y2_ATTRIBUTE == name) {
286: setY2(value[0][0]);
287: } else {
288: super .setFloatArrayTrait(name, value);
289: }
290: }
291:
292: /**
293: * Validates the input trait value.
294: *
295: * @param traitName the name of the trait to be validated.
296: * @param value the value to be validated
297: * @param reqNamespaceURI the namespace of the element requesting
298: * validation.
299: * @param reqLocalName the local name of the element requesting validation.
300: * @param reqTraitNamespace the namespace of the trait which has the values
301: * value on the requesting element.
302: * @param reqTraitName the name of the trait which has the values value on
303: * the requesting element.
304: * @throws DOMException with error code INVALID_ACCESS_ERR if the input
305: * value is incompatible with the given trait.
306: */
307: public float[][] validateFloatArrayTrait(final String traitName,
308: final String value, final String reqNamespaceURI,
309: final String reqLocalName, final String reqTraitNamespace,
310: final String reqTraitName) throws DOMException {
311: if (SVGConstants.SVG_X1_ATTRIBUTE == traitName
312: || SVGConstants.SVG_X2_ATTRIBUTE == traitName
313: || SVGConstants.SVG_Y1_ATTRIBUTE == traitName
314: || SVGConstants.SVG_Y2_ATTRIBUTE == traitName) {
315: return new float[][] { { parseFloatTrait(traitName, value) } };
316: } else {
317: return super .validateFloatArrayTrait(traitName, value,
318: reqNamespaceURI, reqLocalName, reqTraitNamespace,
319: reqTraitName);
320: }
321:
322: }
323:
324: /**
325: * Supported traits: stroke-width, stroke-miterlimit, stroke-dashoffset,
326: * fill-rule, stroke-linejoin, stroke-linecap, display, visibility,
327: * color, fill, stroke, fill-opacity, stroke-opacity, stroke-dasharray
328: *
329: * @param name the name of the trait to set.
330: * @param value the value of the trait to set.
331: *
332: * @throws DOMException with error code NOT_SUPPORTED_ERROR if the requested
333: * trait is not supported on this element or null.
334: * @throws DOMException with error code TYPE_MISMATCH_ERR if the requested
335: * trait's value cannot be specified as a String
336: * @throws DOMException with error code INVALID_ACCESS_ERR if the input
337: * value is an invalid value for the given trait or null.
338: * @throws DOMException with error code NO_MODIFICATION_ALLOWED_ERR: if
339: * attempt is made to change readonly trait.
340: */
341: public void setTraitImpl(final String name, final String value)
342: throws DOMException {
343: if (SVGConstants.SVG_X1_ATTRIBUTE == name) {
344: setX1(parseFloatTrait(name, value));
345: } else if (SVGConstants.SVG_Y1_ATTRIBUTE == name) {
346: setY1(parseFloatTrait(name, value));
347: } else if (SVGConstants.SVG_X2_ATTRIBUTE == name) {
348: setX2(parseFloatTrait(name, value));
349: } else if (SVGConstants.SVG_Y2_ATTRIBUTE == name) {
350: setY2(parseFloatTrait(name, value));
351: } else {
352: super .setTraitImpl(name, value);
353: }
354: }
355:
356: /**
357: * Set the trait value as float.
358: *
359: * Supported float traits: stroke-width, stroke-miterlimit,
360: * stroke-dashoffset, fill-opacity, stroke-opacity.
361: *
362: * @param name the name of the trait to set.
363: * @param value the value of the trait to set.
364: *
365: * @throws DOMException with error code NOT_SUPPORTED_ERROR if the requested
366: * trait is not supported on this element.
367: * @throws DOMException with error code TYPE_MISMATCH_ERR if the requested
368: * trait's value cannot be specified as a float
369: * @throws DOMException with error code INVALID_ACCESS_ERR if the input
370: * value is an invalid value for the given trait.
371: * @throws SecurityException if the application does not have the necessary
372: * privilege rights to access this (SVG) content.
373: */
374: public void setFloatTraitImpl(final String name, final float value)
375: throws DOMException {
376: if (SVGConstants.SVG_X1_ATTRIBUTE == name) {
377: setX1(value);
378: } else if (SVGConstants.SVG_Y1_ATTRIBUTE == name) {
379: setY1(value);
380: } else if (SVGConstants.SVG_X2_ATTRIBUTE == name) {
381: setX2(value);
382: } else if (SVGConstants.SVG_Y2_ATTRIBUTE == name) {
383: setY2(value);
384: } else {
385: super .setFloatTraitImpl(name, value);
386: }
387: }
388:
389: /**
390: * @param name the name of the trait to convert.
391: * @param value the float trait value to convert.
392: */
393: String toStringTrait(final String name, final float[][] value) {
394: if (SVGConstants.SVG_X1_ATTRIBUTE == name
395: || SVGConstants.SVG_Y1_ATTRIBUTE == name
396: || SVGConstants.SVG_X2_ATTRIBUTE == name
397: || SVGConstants.SVG_Y2_ATTRIBUTE == name) {
398: return Float.toString(value[0][0]);
399: } else {
400: return super .toStringTrait(name, value);
401: }
402: }
403:
404: /**
405: * Computes the paint in user space on use.
406: *
407: * @return the computed PaintDef.
408: */
409: protected PaintDef computePaint() {
410: if (computedPaint == null) {
411: buildGradientColorMap();
412: computedPaint = new LinearGradientPaintDef(x1, y1, x2, y2,
413: lastColorMapFractions, lastColorMapRGBA,
414: LinearGradientPaintDef.CYCLE_NONE, isObjectBBox,
415: transform);
416: }
417: return computedPaint;
418: }
419: }
|