001: /*
002:
003: Licensed to the Apache Software Foundation (ASF) under one or more
004: contributor license agreements. See the NOTICE file distributed with
005: this work for additional information regarding copyright ownership.
006: The ASF licenses this file to You under the Apache License, Version 2.0
007: (the "License"); you may not use this file except in compliance with
008: the License. You may obtain a copy of the License at
009:
010: http://www.apache.org/licenses/LICENSE-2.0
011:
012: Unless required by applicable law or agreed to in writing, software
013: distributed under the License is distributed on an "AS IS" BASIS,
014: WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015: See the License for the specific language governing permissions and
016: limitations under the License.
017:
018: */
019: package org.apache.batik.bridge;
020:
021: import java.awt.Color;
022: import java.awt.Paint;
023: import java.awt.geom.AffineTransform;
024: import java.awt.geom.Point2D;
025:
026: import org.apache.batik.ext.awt.MultipleGradientPaint;
027: import org.apache.batik.ext.awt.RadialGradientPaint;
028: import org.apache.batik.gvt.GraphicsNode;
029: import org.w3c.dom.Element;
030:
031: /**
032: * Bridge class for the <radialGradient> element.
033: *
034: * @author <a href="mailto:tkormann@apache.org">Thierry Kormann</a>
035: * @version $Id: SVGRadialGradientElementBridge.java 475477 2006-11-15 22:44:28Z cam $
036: */
037: public class SVGRadialGradientElementBridge extends
038: AbstractSVGGradientElementBridge {
039:
040: /**
041: * Constructs a new SVGRadialGradientElementBridge.
042: */
043: public SVGRadialGradientElementBridge() {
044: }
045:
046: /**
047: * Returns 'radialGradient'.
048: */
049: public String getLocalName() {
050: return SVG_RADIAL_GRADIENT_TAG;
051: }
052:
053: /**
054: * Builds a radial gradient according to the specified parameters.
055: *
056: * @param paintElement the element that defines a Paint
057: * @param paintedElement the element referencing the paint
058: * @param paintedNode the graphics node on which the Paint will be applied
059: * @param spreadMethod the spread method
060: * @param colorSpace the color space (sRGB | LinearRGB)
061: * @param transform the gradient transform
062: * @param colors the colors of the gradient
063: * @param offsets the offsets
064: * @param ctx the bridge context to use
065: */
066: protected Paint buildGradient(Element paintElement,
067: Element paintedElement, GraphicsNode paintedNode,
068: MultipleGradientPaint.CycleMethodEnum spreadMethod,
069: MultipleGradientPaint.ColorSpaceEnum colorSpace,
070: AffineTransform transform, Color[] colors, float[] offsets,
071: BridgeContext ctx) {
072:
073: // 'cx' attribute - default is 50%
074: String cxStr = SVGUtilities.getChainableAttributeNS(
075: paintElement, null, SVG_CX_ATTRIBUTE, ctx);
076: if (cxStr.length() == 0) {
077: cxStr = SVG_RADIAL_GRADIENT_CX_DEFAULT_VALUE;
078: }
079:
080: // 'cy' attribute - default is 50%
081: String cyStr = SVGUtilities.getChainableAttributeNS(
082: paintElement, null, SVG_CY_ATTRIBUTE, ctx);
083: if (cyStr.length() == 0) {
084: cyStr = SVG_RADIAL_GRADIENT_CY_DEFAULT_VALUE;
085: }
086:
087: // 'r' attribute - default is 50%
088: String rStr = SVGUtilities.getChainableAttributeNS(
089: paintElement, null, SVG_R_ATTRIBUTE, ctx);
090: if (rStr.length() == 0) {
091: rStr = SVG_RADIAL_GRADIENT_R_DEFAULT_VALUE;
092: }
093:
094: // 'fx' attribute - default is same as cx
095: String fxStr = SVGUtilities.getChainableAttributeNS(
096: paintElement, null, SVG_FX_ATTRIBUTE, ctx);
097: if (fxStr.length() == 0) {
098: fxStr = cxStr;
099: }
100:
101: // 'fy' attribute - default is same as cy
102: String fyStr = SVGUtilities.getChainableAttributeNS(
103: paintElement, null, SVG_FY_ATTRIBUTE, ctx);
104: if (fyStr.length() == 0) {
105: fyStr = cyStr;
106: }
107:
108: // 'gradientUnits' attribute - default is objectBoundingBox
109: short coordSystemType;
110: String s = SVGUtilities.getChainableAttributeNS(paintElement,
111: null, SVG_GRADIENT_UNITS_ATTRIBUTE, ctx);
112: if (s.length() == 0) {
113: coordSystemType = SVGUtilities.OBJECT_BOUNDING_BOX;
114: } else {
115: coordSystemType = SVGUtilities.parseCoordinateSystem(
116: paintElement, SVG_GRADIENT_UNITS_ATTRIBUTE, s, ctx);
117: }
118:
119: // additional transform to move to objectBoundingBox coordinate system
120: if (coordSystemType == SVGUtilities.OBJECT_BOUNDING_BOX) {
121: transform = SVGUtilities.toObjectBBox(transform,
122: paintedNode);
123: }
124: UnitProcessor.Context uctx = UnitProcessor.createContext(ctx,
125: paintElement);
126:
127: float r = SVGUtilities.convertLength(rStr, SVG_R_ATTRIBUTE,
128: coordSystemType, uctx);
129: // A value of zero will cause the area to be painted as a single color
130: // using the color and opacity of the last gradient stop.
131: if (r == 0) {
132: return colors[colors.length - 1];
133: } else {
134: Point2D c = SVGUtilities.convertPoint(cxStr,
135: SVG_CX_ATTRIBUTE, cyStr, SVG_CY_ATTRIBUTE,
136: coordSystemType, uctx);
137:
138: Point2D f = SVGUtilities.convertPoint(fxStr,
139: SVG_FX_ATTRIBUTE, fyStr, SVG_FY_ATTRIBUTE,
140: coordSystemType, uctx);
141:
142: // <!> FIXME: colorSpace ignored for radial gradient at this time
143: return new RadialGradientPaint(c, r, f, offsets, colors,
144: spreadMethod, RadialGradientPaint.SRGB, transform);
145: }
146: }
147: }
|