1 18 package org.apache.batik.svggen; 19 20 import java.util.HashMap ; 21 import java.util.Iterator ; 22 import java.util.Map ; 23 24 import org.apache.batik.ext.awt.g2d.GraphicContext; 25 import org.apache.batik.ext.awt.g2d.TransformStackElement; 26 import org.w3c.dom.Element ; 27 28 60 public class DOMGroupManager implements SVGSyntax, ErrorConstants { 61 public final static short DRAW = 0x01; 62 public final static short FILL = 0x10; 63 64 68 protected GraphicContext gc; 69 70 73 protected DOMTreeManager domTreeManager; 74 75 78 protected SVGGraphicContext groupGC; 79 80 83 protected Element currentGroup; 84 85 92 public DOMGroupManager(GraphicContext gc, DOMTreeManager domTreeManager) { 93 if (gc == null) 94 throw new SVGGraphics2DRuntimeException(ERR_GC_NULL); 95 96 if (domTreeManager == null) 97 throw new SVGGraphics2DRuntimeException(ERR_DOMTREEMANAGER_NULL); 98 99 this.gc = gc; 100 this.domTreeManager = domTreeManager; 101 102 recycleCurrentGroup(); 104 105 groupGC = domTreeManager.gcConverter.toSVG(gc); 107 } 108 109 112 void recycleCurrentGroup() { 113 currentGroup = domTreeManager.getDOMFactory(). 115 createElementNS(SVG_NAMESPACE_URI, SVG_G_TAG); 116 } 117 118 122 public void addElement(Element element) { 123 addElement(element, (short)(DRAW|FILL)); 124 } 125 126 130 public void addElement(Element element, short method) { 131 if (!currentGroup.hasChildNodes()) { 136 currentGroup.appendChild(element); 137 138 groupGC = domTreeManager.gcConverter.toSVG(gc); 139 SVGGraphicContext deltaGC; 140 deltaGC = processDeltaGC(groupGC, 141 domTreeManager.defaultGC); 142 domTreeManager.getStyleHandler(). 143 setStyle(currentGroup, deltaGC.getGroupContext(), 144 domTreeManager.getGeneratorContext()); 145 if ((method & DRAW) == 0) { 146 deltaGC.getGraphicElementContext().put(SVG_STROKE_ATTRIBUTE, 148 SVG_NONE_VALUE); 149 } 150 if ((method & FILL) == 0) { 151 deltaGC.getGraphicElementContext().put(SVG_FILL_ATTRIBUTE, 153 SVG_NONE_VALUE); 154 } 155 domTreeManager.getStyleHandler(). 156 setStyle(element, deltaGC.getGraphicElementContext(), 157 domTreeManager.getGeneratorContext()); 158 setTransform(currentGroup, deltaGC.getTransformStack()); 159 domTreeManager.appendGroup(currentGroup, this); 160 } else { 161 if(gc.isTransformStackValid()) { 162 SVGGraphicContext elementGC = 168 domTreeManager.gcConverter.toSVG(gc); 169 SVGGraphicContext deltaGC = processDeltaGC(elementGC, groupGC); 170 171 trimContextForElement(deltaGC, element); 175 if (countOverrides(deltaGC) <= domTreeManager.maxGCOverrides) { 176 currentGroup.appendChild(element); 177 if ((method & DRAW) == 0) { 180 deltaGC.getContext(). 182 put(SVG_STROKE_ATTRIBUTE, SVG_NONE_VALUE); 183 } 184 if ((method & FILL) == 0) { 185 deltaGC.getContext(). 187 put(SVG_FILL_ATTRIBUTE, SVG_NONE_VALUE); 188 } 189 domTreeManager.getStyleHandler(). 190 setStyle(element, deltaGC.getContext(), 191 domTreeManager.getGeneratorContext()); 192 setTransform(element, deltaGC.getTransformStack()); 193 } else { 194 currentGroup = 198 domTreeManager.getDOMFactory(). 199 createElementNS(SVG_NAMESPACE_URI, SVG_G_TAG); 200 addElement(element, method); 201 } 202 } else { 203 currentGroup = 208 domTreeManager.getDOMFactory(). 209 createElementNS(SVG_NAMESPACE_URI, SVG_G_TAG); 210 gc.validateTransformStack(); 211 addElement(element, method); 212 } 213 } 214 } 215 216 221 protected int countOverrides(SVGGraphicContext deltaGC) { 222 return deltaGC.getGroupContext().size(); 223 } 224 225 228 protected void trimContextForElement(SVGGraphicContext svgGC, Element element) { 229 String tag = element.getTagName(); 230 Map groupAttrMap = svgGC.getGroupContext(); 231 if (tag != null) { 232 Iterator iter = groupAttrMap.keySet().iterator(); 237 while(iter.hasNext()){ 238 String attrName = (String )iter.next(); 239 SVGAttribute attr = SVGAttributeMap.get(attrName); 240 if(attr != null && !attr.appliesTo(tag)) 241 groupAttrMap.remove(attrName); 242 } 243 } 244 } 245 246 250 protected void setTransform(Element element, 251 TransformStackElement transformStack[]) { 252 String transform = domTreeManager.gcConverter. 253 toSVG(transformStack).trim(); 254 if (transform.length() > 0) 255 element.setAttributeNS(null, SVG_TRANSFORM_ATTRIBUTE, transform); 256 } 257 258 263 protected SVGGraphicContext processDeltaGC(SVGGraphicContext gc, 264 SVGGraphicContext referenceGc) { 265 Map groupDelta = processDeltaMap(gc.getGroupContext(), 266 referenceGc.getGroupContext()); 267 Map graphicElementDelta = gc.getGraphicElementContext(); 268 269 TransformStackElement gcTransformStack[] = gc.getTransformStack(); 270 TransformStackElement referenceStack[] = referenceGc.getTransformStack(); 271 int deltaStackLength = gcTransformStack.length - referenceStack.length; 272 TransformStackElement deltaTransformStack[] = 273 new TransformStackElement[deltaStackLength]; 274 275 System.arraycopy(gcTransformStack, referenceStack.length, 276 deltaTransformStack, 0, deltaStackLength); 277 278 286 287 300 301 SVGGraphicContext deltaGC = new SVGGraphicContext(groupDelta, 302 graphicElementDelta, 303 deltaTransformStack); 304 305 return deltaGC; 306 } 307 308 314 protected Map processDeltaMap(Map map, Map referenceMap) { 315 Map mapDelta = new HashMap (); 317 Iterator iter = map.keySet().iterator(); 318 while (iter.hasNext()){ 319 String key = (String )iter.next(); 320 String value = (String )map.get(key); 321 String refValue = (String )referenceMap.get(key); 322 if (!value.equals(refValue)) { 323 330 mapDelta.put(key, value); 331 } 332 } 333 return mapDelta; 334 } 335 } 336 | Popular Tags |