1 18 package org.apache.batik.bridge; 19 20 import java.awt.color.ICC_Profile ; 21 import java.io.IOException ; 22 23 import org.apache.batik.dom.svg.SVGOMDocument; 24 import org.apache.batik.dom.util.XLinkSupport; 25 import org.apache.batik.ext.awt.color.ICCColorSpaceExt; 26 import org.apache.batik.ext.awt.color.NamedProfileCache; 27 import org.apache.batik.util.ParsedURL; 28 import org.w3c.dom.Document ; 29 import org.w3c.dom.Element ; 30 import org.w3c.dom.Node ; 31 import org.w3c.dom.NodeList ; 32 33 39 public class SVGColorProfileElementBridge extends AbstractSVGBridge 40 implements ErrorConstants { 41 42 45 public NamedProfileCache cache = new NamedProfileCache(); 46 47 50 public String getLocalName() { 51 return SVG_COLOR_PROFILE_TAG; 52 } 53 54 63 public ICCColorSpaceExt createICCColorSpaceExt(BridgeContext ctx, 64 Element paintedElement, 65 String iccProfileName) { 66 ICCColorSpaceExt cs = cache.request(iccProfileName.toLowerCase()); 68 if (cs != null){ 69 return cs; 70 } 71 72 Document doc = paintedElement.getOwnerDocument(); 75 NodeList list = doc.getElementsByTagNameNS(SVG_NAMESPACE_URI, 76 SVG_COLOR_PROFILE_TAG); 77 78 int n = list.getLength(); 79 Element profile = null; 80 for(int i=0; i<n; i++){ 81 Node node = list.item(i); 82 if(node.getNodeType() == Node.ELEMENT_NODE){ 83 Element profileNode = (Element )node; 84 String nameAttr 85 = profileNode.getAttributeNS(null, SVG_NAME_ATTRIBUTE); 86 87 if(iccProfileName.equalsIgnoreCase(nameAttr)){ 88 profile = profileNode; 89 } 90 } 91 } 92 93 if(profile == null) 94 return null; 95 96 String href = XLinkSupport.getXLinkHref(profile); 99 ICC_Profile p = null; 100 if (href != null) { 101 String baseURI= ((SVGOMDocument)doc).getURL(); 102 ParsedURL purl = new ParsedURL(baseURI, href); 103 if (!purl.complete()) 104 throw new BridgeException(paintedElement, ERR_URI_MALFORMED, 105 new Object [] {href}); 106 try{ 107 ParsedURL pDocURL = null; 108 if (baseURI != null) { 109 pDocURL = new ParsedURL(baseURI); 110 } 111 112 ctx.getUserAgent().checkLoadExternalResource(purl, 113 pDocURL); 114 115 p = ICC_Profile.getInstance(purl.openStream()); 116 } catch(IOException e) { 117 throw new BridgeException(paintedElement, ERR_URI_IO, 118 new Object [] {href}); 119 } catch(SecurityException e) { 121 throw new BridgeException(paintedElement, ERR_URI_UNSECURE, 122 new Object [] {href}); 123 } 124 } 125 if (p == null) { 126 return null; 127 } 128 129 int intent = convertIntent(profile); 131 cs = new ICCColorSpaceExt(p, intent); 132 133 cache.put(iccProfileName.toLowerCase(), cs); 135 return cs; 136 } 137 138 private static int convertIntent(Element profile) { 139 140 String intent 141 = profile.getAttributeNS(null, SVG_RENDERING_INTENT_ATTRIBUTE); 142 143 if (intent.length() == 0) { 144 return ICCColorSpaceExt.AUTO; 145 } 146 if (SVG_PERCEPTUAL_VALUE.equals(intent)) { 147 return ICCColorSpaceExt.PERCEPTUAL; 148 } 149 if (SVG_AUTO_VALUE.equals(intent)) { 150 return ICCColorSpaceExt.AUTO; 151 } 152 if (SVG_RELATIVE_COLORIMETRIC_VALUE.equals(intent)) { 153 return ICCColorSpaceExt.RELATIVE_COLORIMETRIC; 154 } 155 if (SVG_ABSOLUTE_COLORIMETRIC_VALUE.equals(intent)) { 156 return ICCColorSpaceExt.ABSOLUTE_COLORIMETRIC; 157 } 158 if (SVG_SATURATION_VALUE.equals(intent)) { 159 return ICCColorSpaceExt.SATURATION; 160 } 161 throw new BridgeException 162 (profile, ERR_ATTRIBUTE_VALUE_MALFORMED, 163 new Object [] {SVG_RENDERING_INTENT_ATTRIBUTE, intent}); 164 } 165 } 166 | Popular Tags |