KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > batik > bridge > svg12 > SVGSolidColorElementBridge


1 /*
2
3    Copyright 2001-2003 The Apache Software Foundation
4
5    Licensed under the Apache License, Version 2.0 (the "License");
6    you may not use this file except in compliance with the License.
7    You may obtain a copy of the License at
8
9        http://www.apache.org/licenses/LICENSE-2.0
10
11    Unless required by applicable law or agreed to in writing, software
12    distributed under the License is distributed on an "AS IS" BASIS,
13    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14    See the License for the specific language governing permissions and
15    limitations under the License.
16
17  */

18 package org.apache.batik.bridge.svg12;
19
20 import java.awt.Color JavaDoc;
21 import java.awt.Paint JavaDoc;
22 import java.util.HashMap JavaDoc;
23 import java.util.Map JavaDoc;
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 JavaDoc;
44 import org.w3c.dom.css.CSSValue;
45
46 /**
47  * Bridge class for a regular polygon element.
48  *
49  * @author <a HREF="mailto:thomas.deweese@kodak.com">Thomas Deweese</a>
50  */

51 public class SVGSolidColorElementBridge extends AbstractSVGBridge
52     implements PaintBridge {
53
54     /**
55      * Constructs a new bridge for the &lt;rect> element.
56      */

57     public SVGSolidColorElementBridge() { /* nothing */ }
58
59     /**
60      * Returns the SVG namespace URI.
61      */

62     public String JavaDoc getNamespaceURI() {
63         return SVG12Constants.SVG_NAMESPACE_URI;
64     }
65
66     /**
67      * Returns 'rect'.
68      */

69     public String JavaDoc getLocalName() {
70         return SVG12Constants.SVG_SOLID_COLOR_TAG;
71     }
72
73     /**
74      * Creates a <tt>Paint</tt> according to the specified parameters.
75      *
76      * @param ctx the bridge context to use
77      * @param paintElement the element that defines a Paint
78      * @param paintedElement the element referencing the paint
79      * @param paintedNode the graphics node on which the Paint will be applied
80      * @param opacity the opacity of the Paint to create
81      */

82     public Paint JavaDoc 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 JavaDoc refs = new HashMap JavaDoc();
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             // Was solid-opacity explicity set on this element?
106
StyleMap sm =
107                 ((CSSStylableElement)paintElement).getComputedStyleMap(null);
108             if (!sm.isNullCascaded(pidx)) {
109                 // It was explicit...
110
float attr = PaintServer.convertOpacity(opacityVal);
111                 return (opacity * attr);
112             }
113
114             String JavaDoc uri = XLinkSupport.getXLinkHref(paintElement);
115             if (uri.length() == 0) {
116                 return opacity; // no xlink:href found, exit
117
}
118
119             SVGOMDocument doc = (SVGOMDocument)paintElement.getOwnerDocument();
120             ParsedURL purl = new ParsedURL(doc.getURL(), uri);
121
122             // check if there is circular dependencies
123
if (refs.containsKey(purl)) {
124                 throw new BridgeException
125                     (paintElement,
126                      ErrorConstants.ERR_XLINK_HREF_CIRCULAR_DEPENDENCIES,
127                      new Object JavaDoc[] {uri});
128             }
129             refs.put(purl, purl);
130             paintElement = ctx.getReferencedElement(paintElement, uri);
131         }
132     }
133
134     protected static Color JavaDoc extractColor(Element paintElement,
135                                         float opacity,
136                                         BridgeContext ctx) {
137         Map JavaDoc refs = new HashMap JavaDoc();
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             // Was solid-color explicity set on this element?
147
StyleMap sm =
148                 ((CSSStylableElement)paintElement).getComputedStyleMap(null);
149             if (!sm.isNullCascaded(pidx)) {
150                 // It was explicit...
151
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 JavaDoc uri = XLinkSupport.getXLinkHref(paintElement);
164             if (uri.length() == 0) {
165                 // no xlink:href found, exit
166
return new Color JavaDoc(0, 0, 0, opacity);
167             }
168
169             SVGOMDocument doc = (SVGOMDocument)paintElement.getOwnerDocument();
170             ParsedURL purl = new ParsedURL(doc.getURL(), uri);
171
172             // check if there is circular dependencies
173
if (refs.containsKey(purl)) {
174                 throw new BridgeException
175                     (paintElement,
176                      ErrorConstants.ERR_XLINK_HREF_CIRCULAR_DEPENDENCIES,
177                      new Object JavaDoc[] {uri});
178             }
179             refs.put(purl, purl);
180             paintElement = ctx.getReferencedElement(paintElement, uri);
181         }
182     }
183 }
184
Popular Tags