1 18 package org.apache.batik.svggen; 19 20 import java.awt.Shape ; 21 import java.awt.geom.GeneralPath ; 22 import java.awt.geom.Line2D ; 23 24 import org.apache.batik.ext.awt.g2d.GraphicContext; 25 import org.w3c.dom.Element ; 26 27 33 public class SVGClip extends AbstractSVGConverter { 34 37 public static final Shape ORIGIN = new Line2D.Float (0,0,0,0); 38 39 42 public static final SVGClipDescriptor NO_CLIP = 43 new SVGClipDescriptor(SVG_NONE_VALUE, null); 44 45 48 private SVGShape shapeConverter; 49 50 53 public SVGClip(SVGGeneratorContext generatorContext) { 54 super(generatorContext); 55 this.shapeConverter = new SVGShape(generatorContext); 56 } 57 58 67 public SVGDescriptor toSVG(GraphicContext gc) { 68 Shape clip = gc.getClip(); 69 70 SVGClipDescriptor clipDesc = null; 71 72 if (clip != null) { 73 StringBuffer clipPathAttrBuf = new StringBuffer (URL_PREFIX); 74 75 GeneralPath clipPath = new GeneralPath (clip); 77 78 ClipKey clipKey = new ClipKey(clipPath, generatorContext); 80 clipDesc = (SVGClipDescriptor)descMap.get(clipKey); 81 82 if (clipDesc == null) { 83 Element clipDef = clipToSVG(clip); 84 if (clipDef == null) 85 clipDesc = NO_CLIP; 86 else { 87 clipPathAttrBuf.append(SIGN_POUND); 88 clipPathAttrBuf.append(clipDef.getAttributeNS(null, ATTR_ID)); 89 clipPathAttrBuf.append(URL_SUFFIX); 90 91 clipDesc = new SVGClipDescriptor(clipPathAttrBuf.toString(), 92 clipDef); 93 94 descMap.put(clipKey, clipDesc); 95 defSet.add(clipDef); 96 } 97 } 98 } else 99 clipDesc = NO_CLIP; 100 101 return clipDesc; 102 } 103 104 111 private Element clipToSVG(Shape clip) { 112 Element clipDef = 113 generatorContext.domFactory.createElementNS(SVG_NAMESPACE_URI, 114 SVG_CLIP_PATH_TAG); 115 clipDef.setAttributeNS(null, SVG_CLIP_PATH_UNITS_ATTRIBUTE, 116 SVG_USER_SPACE_ON_USE_VALUE); 117 118 clipDef.setAttributeNS(null, ATTR_ID, 119 generatorContext. 120 idGenerator.generateID(ID_PREFIX_CLIP_PATH)); 121 122 Element clipPath = shapeConverter.toSVG(clip); 123 if (clipPath != null) { 126 clipDef.appendChild(clipPath); 127 return clipDef; 128 } else { 129 clipDef.appendChild(shapeConverter.toSVG(ORIGIN)); 134 return clipDef; 135 } 136 } 137 } 138 139 145 class ClipKey { 146 150 int hashCodeValue = 0; 151 152 155 public ClipKey(GeneralPath proxiedPath, SVGGeneratorContext gc){ 156 String pathData = SVGPath.toSVGPathData(proxiedPath, gc); 157 hashCodeValue = pathData.hashCode(); 158 } 159 160 163 public int hashCode() { 164 return hashCodeValue; 165 } 166 167 171 public boolean equals(Object clipKey){ 172 boolean isEqual = false; 173 if((clipKey != null) &&clipKey instanceof ClipKey) 174 isEqual = (hashCodeValue == ((ClipKey)clipKey).hashCodeValue); 175 176 return isEqual; 177 } 178 } 179 | Popular Tags |