1 18 package org.apache.batik.svggen; 19 20 import java.awt.Graphics2D ; 21 import java.awt.TexturePaint ; 22 import java.awt.geom.Rectangle2D ; 23 import java.awt.image.BufferedImage ; 24 import java.awt.image.RenderedImage ; 25 26 import org.apache.batik.ext.awt.g2d.GraphicContext; 27 import org.w3c.dom.Document ; 28 import org.w3c.dom.Element ; 29 30 37 public class SVGTexturePaint extends AbstractSVGConverter { 38 41 public SVGTexturePaint(SVGGeneratorContext generatorContext) { 42 super(generatorContext); 43 } 44 45 55 public SVGDescriptor toSVG(GraphicContext gc) { 56 return toSVG((TexturePaint )gc.getPaint()); 57 } 58 59 65 public SVGPaintDescriptor toSVG(TexturePaint texture) { 66 SVGPaintDescriptor patternDesc = (SVGPaintDescriptor)descMap.get(texture); 68 Document domFactory = generatorContext.domFactory; 69 70 if (patternDesc == null) { 71 Rectangle2D anchorRect = texture.getAnchorRect(); 72 Element patternDef = domFactory.createElementNS(SVG_NAMESPACE_URI, 73 SVG_PATTERN_TAG); 74 patternDef.setAttributeNS(null, SVG_PATTERN_UNITS_ATTRIBUTE, 75 SVG_USER_SPACE_ON_USE_VALUE); 76 77 patternDef.setAttributeNS(null, SVG_X_ATTRIBUTE, 81 doubleString(anchorRect.getX())); 82 patternDef.setAttributeNS(null, SVG_Y_ATTRIBUTE, 83 doubleString(anchorRect.getY())); 84 patternDef.setAttributeNS(null, SVG_WIDTH_ATTRIBUTE, 85 doubleString(anchorRect.getWidth())); 86 patternDef.setAttributeNS(null, SVG_HEIGHT_ATTRIBUTE, 87 doubleString(anchorRect.getHeight())); 88 89 BufferedImage textureImage = texture.getImage(); 93 if (textureImage.getWidth() > 0 && 97 textureImage.getHeight() > 0){ 98 99 if(textureImage.getWidth() != anchorRect.getWidth() || 101 textureImage.getHeight() != anchorRect.getHeight()){ 102 103 if(anchorRect.getWidth() > 0 && 105 anchorRect.getHeight() > 0){ 106 double scaleX = 107 anchorRect.getWidth()/textureImage.getWidth(); 108 double scaleY = 109 anchorRect.getHeight()/textureImage.getHeight(); 110 BufferedImage newImage 111 = new BufferedImage ((int)(scaleX* 112 textureImage.getWidth()), 113 (int)(scaleY* 114 textureImage.getHeight()), 115 BufferedImage.TYPE_INT_ARGB); 116 117 Graphics2D g = newImage.createGraphics(); 118 g.scale(scaleX, scaleY); 119 g.drawImage(textureImage, 0, 0, null); 120 g.dispose(); 121 122 textureImage = newImage; 123 } 124 } 125 } 126 127 131 Element patternContent 132 = generatorContext.genericImageHandler.createElement 133 (generatorContext); 134 135 generatorContext.genericImageHandler.handleImage 136 ((RenderedImage )textureImage, 137 patternContent, 138 0, 139 0, 140 textureImage.getWidth(), 141 textureImage.getHeight(), 142 generatorContext); 143 144 patternDef.appendChild(patternContent); 145 146 patternDef.setAttributeNS(null, ATTR_ID, 147 generatorContext.idGenerator. 148 generateID(ID_PREFIX_PATTERN)); 149 150 StringBuffer patternAttrBuf = new StringBuffer (URL_PREFIX); 151 patternAttrBuf.append(SIGN_POUND); 152 patternAttrBuf.append(patternDef.getAttributeNS(null, ATTR_ID)); 153 patternAttrBuf.append(URL_SUFFIX); 154 155 patternDesc = new SVGPaintDescriptor(patternAttrBuf.toString(), 156 SVG_OPAQUE_VALUE, 157 patternDef); 158 159 descMap.put(texture, patternDesc); 160 defSet.add(patternDef); 161 } 162 163 return patternDesc; 164 } 165 } 166 | Popular Tags |