1 18 package org.apache.batik.svggen; 19 20 import java.awt.Image ; 21 import java.awt.geom.AffineTransform ; 22 import java.awt.image.RenderedImage ; 23 import java.awt.image.renderable.RenderableImage ; 24 25 import org.w3c.dom.Element ; 26 27 36 public class SimpleImageHandler implements GenericImageHandler, SVGSyntax, ErrorConstants { 37 static final String XLINK_NAMESPACE_URI = 40 "http://www.w3.org/1999/xlink"; 41 42 45 protected ImageHandler imageHandler; 46 47 52 public SimpleImageHandler(ImageHandler imageHandler){ 53 if (imageHandler == null){ 54 throw new IllegalArgumentException (); 55 } 56 57 this.imageHandler = imageHandler; 58 } 59 60 64 public void setDOMTreeManager(DOMTreeManager domTreeManager){ 65 } 66 67 72 public Element createElement(SVGGeneratorContext generatorContext) { 73 Element imageElement = 75 generatorContext.getDOMFactory().createElementNS 76 (SVG_NAMESPACE_URI, SVG_IMAGE_TAG); 77 78 return imageElement; 79 } 80 81 84 public AffineTransform handleImage(Image image, 85 Element imageElement, 86 int x, int y, 87 int width, int height, 88 SVGGeneratorContext generatorContext) { 89 90 int imageWidth = image.getWidth(null); 91 int imageHeight = image.getHeight(null); 92 if(imageWidth == 0 || imageHeight == 0 || 93 width == 0 || height == 0) { 94 95 handleEmptyImage(imageElement); 97 98 } else { 99 imageHandler.handleImage(image, imageElement, generatorContext); 100 setImageAttributes(imageElement, x, y, width, height, 101 generatorContext); 102 } 103 return null; 104 } 105 106 109 public AffineTransform handleImage(RenderedImage image, 110 Element imageElement, 111 int x, int y, 112 int width, int height, 113 SVGGeneratorContext generatorContext) { 114 115 int imageWidth = image.getWidth(); 116 int imageHeight = image.getHeight(); 117 118 if(imageWidth == 0 || imageHeight == 0 || 119 width == 0 || height == 0) { 120 121 handleEmptyImage(imageElement); 123 124 } else { 125 imageHandler.handleImage(image, imageElement, generatorContext); 126 setImageAttributes(imageElement, x, y, width, height, 127 generatorContext); 128 } 129 return null; 130 } 131 132 135 public AffineTransform handleImage(RenderableImage image, 136 Element imageElement, 137 double x, double y, 138 double width, double height, 139 SVGGeneratorContext generatorContext) { 140 141 double imageWidth = image.getWidth(); 142 double imageHeight = image.getHeight(); 143 144 if(imageWidth == 0 || imageHeight == 0 || 145 width == 0 || height == 0) { 146 147 handleEmptyImage(imageElement); 149 150 } else { 151 imageHandler.handleImage(image, imageElement, generatorContext); 152 setImageAttributes(imageElement, x, y, width, height, generatorContext); 153 } 154 return null; 155 } 156 157 161 protected void setImageAttributes(Element imageElement, 162 double x, 163 double y, 164 double width, 165 double height, 166 SVGGeneratorContext generatorContext) { 167 imageElement.setAttributeNS(null, 168 SVG_X_ATTRIBUTE, 169 generatorContext.doubleString(x)); 170 imageElement.setAttributeNS(null, 171 SVG_Y_ATTRIBUTE, 172 generatorContext.doubleString(y)); 173 imageElement.setAttributeNS(null, 174 SVG_WIDTH_ATTRIBUTE, 175 generatorContext.doubleString(width)); 176 imageElement.setAttributeNS(null, 177 SVG_HEIGHT_ATTRIBUTE, 178 generatorContext.doubleString(height)); 179 imageElement.setAttributeNS(null, 180 SVG_PRESERVE_ASPECT_RATIO_ATTRIBUTE, 181 SVG_NONE_VALUE); 182 } 183 184 protected void handleEmptyImage(Element imageElement) { 185 imageElement.setAttributeNS(XLINK_NAMESPACE_URI, 186 ATTR_XLINK_HREF, ""); 187 imageElement.setAttributeNS(null, SVG_WIDTH_ATTRIBUTE, "0"); 188 imageElement.setAttributeNS(null, SVG_HEIGHT_ATTRIBUTE, "0"); 189 } 190 191 } 192 | Popular Tags |