1 18 package org.apache.batik.svggen; 19 20 import java.awt.AlphaComposite ; 21 import java.util.HashMap ; 22 import java.util.LinkedList ; 23 import java.util.List ; 24 import java.util.Map ; 25 26 import org.apache.batik.ext.awt.g2d.GraphicContext; 27 import org.w3c.dom.Element ; 28 29 47 public class SVGAlphaComposite extends AbstractSVGConverter { 48 51 private Map compositeDefsMap = new HashMap (); 52 53 56 private boolean backgroundAccessRequired = false; 57 58 61 public SVGAlphaComposite(SVGGeneratorContext generatorContext) { 62 super(generatorContext); 63 64 compositeDefsMap.put(AlphaComposite.Src, 68 compositeToSVG(AlphaComposite.Src)); 69 compositeDefsMap.put(AlphaComposite.SrcIn, 70 compositeToSVG(AlphaComposite.SrcIn)); 71 compositeDefsMap.put(AlphaComposite.SrcOut, 72 compositeToSVG(AlphaComposite.SrcOut)); 73 compositeDefsMap.put(AlphaComposite.DstIn, 74 compositeToSVG(AlphaComposite.DstIn)); 75 compositeDefsMap.put(AlphaComposite.DstOut, 76 compositeToSVG(AlphaComposite.DstOut)); 77 compositeDefsMap.put(AlphaComposite.DstOver, 78 compositeToSVG(AlphaComposite.DstOver)); 79 compositeDefsMap.put(AlphaComposite.Clear, 80 compositeToSVG(AlphaComposite.Clear)); 81 } 82 83 86 public List getAlphaCompositeFilterSet() { 87 return new LinkedList (compositeDefsMap.values()); 88 } 89 90 94 public boolean requiresBackgroundAccess() { 95 return backgroundAccessRequired; 96 } 97 98 108 public SVGDescriptor toSVG(GraphicContext gc) { 109 return toSVG((AlphaComposite )gc.getComposite()); 110 } 111 112 117 public SVGCompositeDescriptor toSVG(AlphaComposite composite) { 118 SVGCompositeDescriptor compositeDesc = 119 (SVGCompositeDescriptor)descMap.get(composite); 120 121 if(compositeDesc == null){ 122 String opacityValue = doubleString(composite.getAlpha()); 124 125 String filterValue = null; 128 Element filterDef = null; 129 if(composite.getRule() != AlphaComposite.SRC_OVER){ 130 AlphaComposite majorComposite = 134 AlphaComposite.getInstance(composite.getRule()); 135 filterDef = (Element )compositeDefsMap.get(majorComposite); 136 defSet.add(filterDef); 137 138 StringBuffer filterAttrBuf = new StringBuffer (URL_PREFIX); 140 filterAttrBuf.append(SIGN_POUND); 141 filterAttrBuf.append(filterDef.getAttributeNS(null, ATTR_ID)); 142 filterAttrBuf.append(URL_SUFFIX); 143 144 filterValue = filterAttrBuf.toString(); 145 } else 146 filterValue = SVG_NONE_VALUE; 147 148 compositeDesc = new SVGCompositeDescriptor(opacityValue, filterValue, 149 filterDef); 150 151 descMap.put(composite, compositeDesc); 152 } 153 154 if (composite.getRule() != AlphaComposite.SRC_OVER) 155 backgroundAccessRequired = true; 156 157 return compositeDesc; 158 } 159 160 165 private Element compositeToSVG(AlphaComposite composite) { 166 String operator = null; 168 169 String input1 = null; 171 172 String input2 = null; 174 175 String k2 = "0"; 178 179 String id = null; 181 182 switch(composite.getRule()){ 183 case AlphaComposite.CLEAR: 184 operator = SVG_ARITHMETIC_VALUE; 185 input1 = SVG_SOURCE_GRAPHIC_VALUE; 186 input2 = SVG_BACKGROUND_IMAGE_VALUE; 187 id = ID_PREFIX_ALPHA_COMPOSITE_CLEAR; 188 break; 189 case AlphaComposite.SRC: 190 operator = SVG_ARITHMETIC_VALUE; 191 input1 = SVG_SOURCE_GRAPHIC_VALUE; 192 input2 = SVG_BACKGROUND_IMAGE_VALUE; 193 id = ID_PREFIX_ALPHA_COMPOSITE_SRC; 194 k2 = SVG_DIGIT_ONE_VALUE; 195 break; 196 case AlphaComposite.SRC_IN: 197 operator = SVG_IN_VALUE; 198 input1 = SVG_SOURCE_GRAPHIC_VALUE; 199 input2 = SVG_BACKGROUND_IMAGE_VALUE; 200 id = ID_PREFIX_ALPHA_COMPOSITE_SRC_IN; 201 break; 202 case AlphaComposite.SRC_OUT: 203 operator = SVG_OUT_VALUE; 204 input1 = SVG_SOURCE_GRAPHIC_VALUE; 205 input2 = SVG_BACKGROUND_IMAGE_VALUE; 206 id = ID_PREFIX_ALPHA_COMPOSITE_SRC_OUT; 207 break; 208 case AlphaComposite.DST_IN: 209 operator = SVG_IN_VALUE; 210 input2 = SVG_SOURCE_GRAPHIC_VALUE; 211 input1 = SVG_BACKGROUND_IMAGE_VALUE; 212 id = ID_PREFIX_ALPHA_COMPOSITE_DST_IN; 213 break; 214 case AlphaComposite.DST_OUT: 215 operator = SVG_OUT_VALUE; 216 input2 = SVG_SOURCE_GRAPHIC_VALUE; 217 input1 = SVG_BACKGROUND_IMAGE_VALUE; 218 id = ID_PREFIX_ALPHA_COMPOSITE_DST_OUT; 219 break; 220 case AlphaComposite.DST_OVER: 221 operator = SVG_OVER_VALUE; 222 input2 = SVG_SOURCE_GRAPHIC_VALUE; 223 input1 = SVG_BACKGROUND_IMAGE_VALUE; 224 id = ID_PREFIX_ALPHA_COMPOSITE_DST_OVER; 225 break; 226 default: 227 throw new Error (); 228 } 229 230 Element compositeFilter = 231 generatorContext.domFactory.createElementNS(SVG_NAMESPACE_URI, 232 SVG_FILTER_TAG); 233 compositeFilter.setAttributeNS(null, ATTR_ID, id); 234 compositeFilter.setAttributeNS(null, SVG_FILTER_UNITS_ATTRIBUTE, 235 SVG_OBJECT_BOUNDING_BOX_VALUE); 236 compositeFilter.setAttributeNS(null, SVG_X_ATTRIBUTE, SVG_ZERO_PERCENT_VALUE); 237 compositeFilter.setAttributeNS(null, SVG_Y_ATTRIBUTE, SVG_ZERO_PERCENT_VALUE); 238 compositeFilter.setAttributeNS(null, SVG_WIDTH_ATTRIBUTE, 239 SVG_HUNDRED_PERCENT_VALUE); 240 compositeFilter.setAttributeNS(null, SVG_HEIGHT_ATTRIBUTE, 241 SVG_HUNDRED_PERCENT_VALUE); 242 243 Element feComposite = 244 generatorContext.domFactory.createElementNS(SVG_NAMESPACE_URI, 245 SVG_FE_COMPOSITE_TAG); 246 feComposite.setAttributeNS(null, SVG_OPERATOR_ATTRIBUTE, operator); 247 feComposite.setAttributeNS(null, SVG_IN_ATTRIBUTE, input1); 248 feComposite.setAttributeNS(null, SVG_IN2_ATTRIBUTE, input2); 249 feComposite.setAttributeNS(null, SVG_K2_ATTRIBUTE, k2); 250 feComposite.setAttributeNS(null, SVG_RESULT_ATTRIBUTE, SVG_COMPOSITE_VALUE); 251 252 Element feFlood = 253 generatorContext.domFactory.createElementNS(SVG_NAMESPACE_URI, 254 SVG_FE_FLOOD_TAG); 255 feFlood.setAttributeNS(null, SVG_FLOOD_COLOR_ATTRIBUTE, "white"); 256 feFlood.setAttributeNS(null, SVG_FLOOD_OPACITY_ATTRIBUTE, "1"); 257 feFlood.setAttributeNS(null, SVG_RESULT_ATTRIBUTE, SVG_FLOOD_VALUE); 258 259 260 Element feMerge = 261 generatorContext.domFactory.createElementNS(SVG_NAMESPACE_URI, 262 SVG_FE_MERGE_TAG); 263 Element feMergeNodeFlood = 264 generatorContext.domFactory.createElementNS(SVG_NAMESPACE_URI, 265 SVG_FE_MERGE_NODE_TAG); 266 feMergeNodeFlood.setAttributeNS(null, SVG_IN_ATTRIBUTE, SVG_FLOOD_VALUE); 267 Element feMergeNodeComposite = 268 generatorContext.domFactory.createElementNS(SVG_NAMESPACE_URI, 269 SVG_FE_MERGE_NODE_TAG); 270 feMergeNodeComposite.setAttributeNS(null, SVG_IN_ATTRIBUTE, 271 SVG_COMPOSITE_VALUE); 272 273 feMerge.appendChild(feMergeNodeFlood); 274 feMerge.appendChild(feMergeNodeComposite); 275 276 compositeFilter.appendChild(feFlood); 277 compositeFilter.appendChild(feComposite); 278 compositeFilter.appendChild(feMerge); 279 280 return compositeFilter; 281 } 282 } 283 | Popular Tags |