KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > batik > bridge > SVGFeCompositeElementBridge


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;
19
20 import java.awt.geom.Rectangle2D JavaDoc;
21 import java.util.ArrayList JavaDoc;
22 import java.util.List JavaDoc;
23 import java.util.Map JavaDoc;
24
25 import org.apache.batik.ext.awt.image.CompositeRule;
26 import org.apache.batik.ext.awt.image.PadMode;
27 import org.apache.batik.ext.awt.image.renderable.CompositeRable8Bit;
28 import org.apache.batik.ext.awt.image.renderable.Filter;
29 import org.apache.batik.ext.awt.image.renderable.PadRable8Bit;
30 import org.apache.batik.gvt.GraphicsNode;
31 import org.w3c.dom.Element JavaDoc;
32
33 /**
34  * Bridge class for the <feComposite> element.
35  *
36  * @author <a HREF="mailto:tkormann@apache.org">Thierry Kormann</a>
37  * @version $Id: SVGFeCompositeElementBridge.java,v 1.15 2004/08/18 07:12:33 vhardy Exp $
38  */

39 public class SVGFeCompositeElementBridge
40     extends AbstractSVGFilterPrimitiveElementBridge {
41
42
43     /**
44      * Constructs a new bridge for the &lt;feComposite> element.
45      */

46     public SVGFeCompositeElementBridge() {}
47
48     /**
49      * Returns 'feComposite'.
50      */

51     public String JavaDoc getLocalName() {
52         return SVG_FE_COMPOSITE_TAG;
53     }
54
55     /**
56      * Creates a <tt>Filter</tt> primitive according to the specified
57      * parameters.
58      *
59      * @param ctx the bridge context to use
60      * @param filterElement the element that defines a filter
61      * @param filteredElement the element that references the filter
62      * @param filteredNode the graphics node to filter
63      *
64      * @param inputFilter the <tt>Filter</tt> that represents the current
65      * filter input if the filter chain.
66      * @param filterRegion the filter area defined for the filter chain
67      * the new node will be part of.
68      * @param filterMap a map where the mediator can map a name to the
69      * <tt>Filter</tt> it creates. Other <tt>FilterBridge</tt>s
70      * can then access a filter node from the filterMap if they
71      * know its name.
72      */

73     public Filter createFilter(BridgeContext ctx,
74                                Element JavaDoc filterElement,
75                                Element JavaDoc filteredElement,
76                                GraphicsNode filteredNode,
77                                Filter inputFilter,
78                                Rectangle2D JavaDoc filterRegion,
79                                Map JavaDoc filterMap) {
80
81         // 'operator' attribute - default is 'over'
82
CompositeRule rule = convertOperator(filterElement);
83
84         // 'in' attribute
85
Filter in = getIn(filterElement,
86                           filteredElement,
87                           filteredNode,
88                           inputFilter,
89                           filterMap,
90                           ctx);
91         if (in == null) {
92             return null; // disable the filter
93
}
94
95         // 'in2' attribute - required
96
Filter in2 = getIn2(filterElement,
97                             filteredElement,
98                             filteredNode,
99                             inputFilter,
100                             filterMap,
101                             ctx);
102         if (in2 == null) {
103             return null; // disable the filter
104
}
105
106         Rectangle2D JavaDoc defaultRegion;
107         defaultRegion = (Rectangle2D JavaDoc)in.getBounds2D().clone();
108         defaultRegion.add(in2.getBounds2D());
109
110         // get filter primitive chain region
111
Rectangle2D JavaDoc primitiveRegion
112             = SVGUtilities.convertFilterPrimitiveRegion(filterElement,
113                                                         filteredElement,
114                                                         filteredNode,
115                                                         defaultRegion,
116                                                         filterRegion,
117                                                         ctx);
118
119         List JavaDoc srcs = new ArrayList JavaDoc(2);
120         srcs.add(in2);
121         srcs.add(in);
122         Filter filter = new CompositeRable8Bit(srcs, rule, true);
123
124         // handle the 'color-interpolation-filters' property
125
handleColorInterpolationFilters(filter, filterElement);
126
127         filter = new PadRable8Bit(filter, primitiveRegion, PadMode.ZERO_PAD);
128
129         // update the filter Map
130
updateFilterMap(filterElement, filter, filterMap);
131
132         return filter;
133     }
134
135     /**
136      * Converts the 'operator' attribute of the specified feComposite
137      * filter primitive element.
138      *
139      * @param filterElement the feComposite filter element
140      */

141     protected static CompositeRule convertOperator(Element JavaDoc filterElement) {
142         String JavaDoc s = filterElement.getAttributeNS(null, SVG_OPERATOR_ATTRIBUTE);
143         if (s.length() == 0) {
144             return CompositeRule.OVER; // default is over
145
}
146         if (SVG_ATOP_VALUE.equals(s)) {
147             return CompositeRule.ATOP;
148         }
149         if (SVG_IN_VALUE.equals(s)) {
150             return CompositeRule.IN;
151         }
152         if (SVG_OVER_VALUE.equals(s)) {
153             return CompositeRule.OVER;
154         }
155         if (SVG_OUT_VALUE.equals(s)) {
156             return CompositeRule.OUT;
157         }
158         if (SVG_XOR_VALUE.equals(s)) {
159             return CompositeRule.XOR;
160         }
161         if (SVG_ARITHMETIC_VALUE.equals(s)) {
162             float k1 = convertNumber(filterElement, SVG_K1_ATTRIBUTE, 0);
163             float k2 = convertNumber(filterElement, SVG_K2_ATTRIBUTE, 0);
164             float k3 = convertNumber(filterElement, SVG_K3_ATTRIBUTE, 0);
165             float k4 = convertNumber(filterElement, SVG_K4_ATTRIBUTE, 0);
166             return CompositeRule.ARITHMETIC(k1, k2, k3, k4);
167         }
168         throw new BridgeException(filterElement, ERR_ATTRIBUTE_VALUE_MALFORMED,
169                                   new Object JavaDoc[] {SVG_OPERATOR_ATTRIBUTE, s});
170     }
171 }
172
Popular Tags