1 18 package org.apache.batik.svggen; 19 20 import java.awt.GradientPaint ; 21 import java.awt.Paint ; 22 import java.awt.geom.Point2D ; 23 24 import org.apache.batik.ext.awt.g2d.GraphicContext; 25 import org.w3c.dom.Document ; 26 import org.w3c.dom.Element ; 27 28 35 public class SVGLinearGradient extends AbstractSVGConverter { 36 39 public SVGLinearGradient(SVGGeneratorContext generatorContext) { 40 super(generatorContext); 41 } 42 43 53 public SVGDescriptor toSVG(GraphicContext gc) { 54 Paint paint = gc.getPaint(); 55 return toSVG((GradientPaint )paint); 56 } 57 58 64 public SVGPaintDescriptor toSVG(GradientPaint gradient) { 65 SVGPaintDescriptor gradientDesc = 67 (SVGPaintDescriptor)descMap.get(gradient); 68 69 Document domFactory = generatorContext.domFactory; 70 71 if (gradientDesc == null) { 72 Element gradientDef = 73 domFactory.createElementNS(SVG_NAMESPACE_URI, 74 SVG_LINEAR_GRADIENT_TAG); 75 gradientDef.setAttributeNS(null, SVG_GRADIENT_UNITS_ATTRIBUTE, 76 SVG_USER_SPACE_ON_USE_VALUE); 77 78 Point2D p1 = gradient.getPoint1(); 82 Point2D p2 = gradient.getPoint2(); 83 gradientDef.setAttributeNS(null, SVG_X1_ATTRIBUTE, 84 "" + doubleString(p1.getX())); 85 gradientDef.setAttributeNS(null, SVG_Y1_ATTRIBUTE, 86 "" + doubleString(p1.getY())); 87 gradientDef.setAttributeNS(null, SVG_X2_ATTRIBUTE, 88 "" + doubleString(p2.getX())); 89 gradientDef.setAttributeNS(null, SVG_Y2_ATTRIBUTE, 90 "" + doubleString(p2.getY())); 91 92 String spreadMethod = SVG_PAD_VALUE; 96 if(gradient.isCyclic()) 97 spreadMethod = SVG_REFLECT_VALUE; 98 gradientDef.setAttributeNS 99 (null, SVG_SPREAD_METHOD_ATTRIBUTE, spreadMethod); 100 101 Element gradientStop = 105 domFactory.createElementNS(SVG_NAMESPACE_URI, SVG_STOP_TAG); 106 gradientStop.setAttributeNS(null, SVG_OFFSET_ATTRIBUTE, 107 SVG_ZERO_PERCENT_VALUE); 108 109 SVGPaintDescriptor colorDesc = SVGColor.toSVG(gradient.getColor1(), generatorContext); 110 gradientStop.setAttributeNS(null, SVG_STOP_COLOR_ATTRIBUTE, 111 colorDesc.getPaintValue()); 112 gradientStop.setAttributeNS(null, SVG_STOP_OPACITY_ATTRIBUTE, 113 colorDesc.getOpacityValue()); 114 115 gradientDef.appendChild(gradientStop); 116 117 gradientStop = 121 domFactory.createElementNS(SVG_NAMESPACE_URI, SVG_STOP_TAG); 122 gradientStop.setAttributeNS(null, SVG_OFFSET_ATTRIBUTE, 123 SVG_HUNDRED_PERCENT_VALUE); 124 125 colorDesc = SVGColor.toSVG(gradient.getColor2(), generatorContext); 126 gradientStop.setAttributeNS(null, SVG_STOP_COLOR_ATTRIBUTE, 127 colorDesc.getPaintValue()); 128 gradientStop.setAttributeNS(null, SVG_STOP_OPACITY_ATTRIBUTE, 129 colorDesc.getOpacityValue()); 130 131 gradientDef.appendChild(gradientStop); 132 133 gradientDef. 137 setAttributeNS(null, ATTR_ID, 138 generatorContext.idGenerator. 139 generateID(ID_PREFIX_LINEAR_GRADIENT)); 140 141 StringBuffer paintAttrBuf = new StringBuffer (URL_PREFIX); 145 paintAttrBuf.append(SIGN_POUND); 146 paintAttrBuf.append(gradientDef.getAttributeNS(null, ATTR_ID)); 147 paintAttrBuf.append(URL_SUFFIX); 148 149 gradientDesc = new SVGPaintDescriptor(paintAttrBuf.toString(), 150 SVG_OPAQUE_VALUE, 151 gradientDef); 152 153 descMap.put(gradient, gradientDesc); 157 defSet.add(gradientDef); 158 } 159 160 return gradientDesc; 161 } 162 } 163 | Popular Tags |