1 18 package org.apache.batik.bridge.svg12; 19 20 import java.awt.Color ; 21 import java.awt.Paint ; 22 import java.util.HashMap ; 23 import java.util.Map ; 24 25 import org.apache.batik.bridge.AbstractSVGBridge; 26 import org.apache.batik.bridge.BridgeContext; 27 import org.apache.batik.bridge.BridgeException; 28 import org.apache.batik.bridge.CSSUtilities; 29 import org.apache.batik.bridge.ErrorConstants; 30 import org.apache.batik.bridge.PaintBridge; 31 import org.apache.batik.bridge.PaintServer; 32 import org.apache.batik.css.engine.CSSEngine; 33 import org.apache.batik.css.engine.CSSStylableElement; 34 import org.apache.batik.css.engine.StyleMap; 35 import org.apache.batik.css.engine.value.Value; 36 import org.apache.batik.css.engine.value.svg.ICCColor; 37 import org.apache.batik.dom.svg.SVGOMDocument; 38 import org.apache.batik.dom.util.XLinkSupport; 39 import org.apache.batik.gvt.GraphicsNode; 40 import org.apache.batik.util.SVG12Constants; 41 import org.apache.batik.util.SVG12CSSConstants; 42 import org.apache.batik.util.ParsedURL; 43 import org.w3c.dom.Element ; 44 import org.w3c.dom.css.CSSValue; 45 46 51 public class SVGSolidColorElementBridge extends AbstractSVGBridge 52 implements PaintBridge { 53 54 57 public SVGSolidColorElementBridge() { } 58 59 62 public String getNamespaceURI() { 63 return SVG12Constants.SVG_NAMESPACE_URI; 64 } 65 66 69 public String getLocalName() { 70 return SVG12Constants.SVG_SOLID_COLOR_TAG; 71 } 72 73 82 public Paint createPaint(BridgeContext ctx, 83 Element paintElement, 84 Element paintedElement, 85 GraphicsNode paintedNode, 86 float opacity) { 87 88 opacity = extractOpacity(paintElement, opacity, ctx); 89 90 return extractColor(paintElement, opacity, ctx); 91 } 92 93 protected static float extractOpacity(Element paintElement, 94 float opacity, 95 BridgeContext ctx) { 96 Map refs = new HashMap (); 97 CSSEngine eng = CSSUtilities.getCSSEngine(paintElement); 98 int pidx = eng.getPropertyIndex 99 (SVG12CSSConstants.CSS_SOLID_OPACITY_PROPERTY); 100 101 for (;;) { 102 Value opacityVal = 103 CSSUtilities.getComputedStyle(paintElement, pidx); 104 105 StyleMap sm = 107 ((CSSStylableElement)paintElement).getComputedStyleMap(null); 108 if (!sm.isNullCascaded(pidx)) { 109 float attr = PaintServer.convertOpacity(opacityVal); 111 return (opacity * attr); 112 } 113 114 String uri = XLinkSupport.getXLinkHref(paintElement); 115 if (uri.length() == 0) { 116 return opacity; } 118 119 SVGOMDocument doc = (SVGOMDocument)paintElement.getOwnerDocument(); 120 ParsedURL purl = new ParsedURL(doc.getURL(), uri); 121 122 if (refs.containsKey(purl)) { 124 throw new BridgeException 125 (paintElement, 126 ErrorConstants.ERR_XLINK_HREF_CIRCULAR_DEPENDENCIES, 127 new Object [] {uri}); 128 } 129 refs.put(purl, purl); 130 paintElement = ctx.getReferencedElement(paintElement, uri); 131 } 132 } 133 134 protected static Color extractColor(Element paintElement, 135 float opacity, 136 BridgeContext ctx) { 137 Map refs = new HashMap (); 138 CSSEngine eng = CSSUtilities.getCSSEngine(paintElement); 139 int pidx = eng.getPropertyIndex 140 (SVG12CSSConstants.CSS_SOLID_COLOR_PROPERTY); 141 142 for (;;) { 143 Value colorDef = 144 CSSUtilities.getComputedStyle(paintElement, pidx); 145 146 StyleMap sm = 148 ((CSSStylableElement)paintElement).getComputedStyleMap(null); 149 if (!sm.isNullCascaded(pidx)) { 150 if (colorDef.getCssValueType() == 152 CSSValue.CSS_PRIMITIVE_VALUE) { 153 return PaintServer.convertColor(colorDef, opacity); 154 } else { 155 return PaintServer.convertRGBICCColor 156 (paintElement, colorDef.item(0), 157 (ICCColor)colorDef.item(1), 158 opacity, ctx); 159 } 160 } 161 162 163 String uri = XLinkSupport.getXLinkHref(paintElement); 164 if (uri.length() == 0) { 165 return new Color (0, 0, 0, opacity); 167 } 168 169 SVGOMDocument doc = (SVGOMDocument)paintElement.getOwnerDocument(); 170 ParsedURL purl = new ParsedURL(doc.getURL(), uri); 171 172 if (refs.containsKey(purl)) { 174 throw new BridgeException 175 (paintElement, 176 ErrorConstants.ERR_XLINK_HREF_CIRCULAR_DEPENDENCIES, 177 new Object [] {uri}); 178 } 179 refs.put(purl, purl); 180 paintElement = ctx.getReferencedElement(paintElement, uri); 181 } 182 } 183 } 184 | Popular Tags |