KickJava   Java API By Example, From Geeks To Geeks.

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


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

41 public class SVGFeMergeElementBridge
42     extends AbstractSVGFilterPrimitiveElementBridge {
43
44     /**
45      * Constructs a new bridge for the &lt;feMerge> element.
46      */

47     public SVGFeMergeElementBridge() {}
48
49     /**
50      * Returns 'feMerge'.
51      */

52     public String JavaDoc getLocalName() {
53         return SVG_FE_MERGE_TAG;
54     }
55
56     /**
57      * Creates a <tt>Filter</tt> primitive according to the specified
58      * parameters.
59      *
60      * @param ctx the bridge context to use
61      * @param filterElement the element that defines a filter
62      * @param filteredElement the element that references the filter
63      * @param filteredNode the graphics node to filter
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         List JavaDoc srcs = extractFeMergeNode(filterElement,
82                                        filteredElement,
83                                        filteredNode,
84                                        inputFilter,
85                                        filterMap,
86                                        ctx);
87
88         if (srcs == null) {
89             return null; // <!> FIXME: no subelement found, result unspecified
90
}
91
92         if (srcs.size() == 0) {
93             return null; // <!> FIXME: no subelement found, result unspecified
94
}
95
96         // the default region is the input sources regions union
97
Iterator JavaDoc iter = srcs.iterator();
98         Rectangle2D JavaDoc defaultRegion =
99             (Rectangle2D JavaDoc)((Filter)iter.next()).getBounds2D().clone();
100
101         while (iter.hasNext()) {
102             defaultRegion.add(((Filter)iter.next()).getBounds2D());
103         }
104
105         // get filter primitive chain region
106
Rectangle2D JavaDoc primitiveRegion
107             = SVGUtilities.convertFilterPrimitiveRegion(filterElement,
108                                                         filteredElement,
109                                                         filteredNode,
110                                                         defaultRegion,
111                                                         filterRegion,
112                                                         ctx);
113
114         Filter filter = new CompositeRable8Bit(srcs, CompositeRule.OVER, true);
115
116         // handle the 'color-interpolation-filters' property
117
handleColorInterpolationFilters(filter, filterElement);
118
119         filter = new PadRable8Bit(filter, primitiveRegion, PadMode.ZERO_PAD);
120
121         // update the filter Map
122
updateFilterMap(filterElement, filter, filterMap);
123
124         return filter;
125     }
126
127
128     /**
129      * Returns a list of Filter objects that represents the feMergeNode of
130      * the specified feMerge filter element.
131      *
132      * @param filterElement the feMerge filter element
133      * @param filteredElement the filtered element
134      * @param filteredNode the filtered graphics node
135      * @param inputFilter the <tt>Filter</tt> that represents the current
136      * filter input if the filter chain.
137      * @param filterMap the filter map that contains named filter primitives
138      * @param ctx the bridge context
139      */

140     protected static List JavaDoc extractFeMergeNode(Element JavaDoc filterElement,
141                                              Element JavaDoc filteredElement,
142                                              GraphicsNode filteredNode,
143                                              Filter inputFilter,
144                                              Map JavaDoc filterMap,
145                                              BridgeContext ctx) {
146
147         List JavaDoc srcs = null;
148         for (Node n = filterElement.getFirstChild();
149              n != null;
150              n = n.getNextSibling()) {
151
152             if (n.getNodeType() != Node.ELEMENT_NODE) {
153                 continue;
154             }
155
156             Element JavaDoc e = (Element JavaDoc)n;
157             Bridge bridge = ctx.getBridge(e);
158             if (bridge == null ||
159                 !(bridge instanceof SVGFeMergeNodeElementBridge)) {
160                 continue;
161             }
162             Filter filter = ((SVGFeMergeNodeElementBridge)bridge).createFilter
163                 (ctx,
164                  e,
165                  filteredElement,
166                  filteredNode,
167                  inputFilter,
168                  filterMap);
169             if (filter != null) {
170                 if (srcs == null) {
171                     srcs = new LinkedList JavaDoc();
172                 }
173                 srcs.add(filter);
174             }
175         }
176         return srcs;
177     }
178
179     /**
180      * Bridge class for the &lt;feMergeNode> element.
181      */

182     public static class SVGFeMergeNodeElementBridge extends AbstractSVGBridge {
183
184         /**
185          * Constructs a new bridge for the &lt;feMergeNode> element.
186          */

187         public SVGFeMergeNodeElementBridge() {}
188
189         /**
190          * Returns 'feMergeNode'.
191          */

192         public String JavaDoc getLocalName() {
193             return SVG_FE_MERGE_NODE_TAG;
194         }
195
196         /**
197          * Creates a <tt>Filter</tt> according to the specified parameters.
198          *
199          * @param ctx the bridge context to use
200          * @param filterElement the element that defines a filter
201          * @param filteredElement the element that references the filter
202          * @param filteredNode the graphics node to filter
203          * @param inputFilter the <tt>Filter</tt> that represents the current
204          * filter input if the filter chain.
205          * @param filterMap a map where the mediator can map a name to the
206          * <tt>Filter</tt> it creates. Other <tt>FilterBridge</tt>s
207          * can then access a filter node from the filterMap if they
208          * know its name.
209          */

210         public Filter createFilter(BridgeContext ctx,
211                                    Element JavaDoc filterElement,
212                                    Element JavaDoc filteredElement,
213                                    GraphicsNode filteredNode,
214                                    Filter inputFilter,
215                                    Map JavaDoc filterMap) {
216             return getIn(filterElement,
217                          filteredElement,
218                          filteredNode,
219                          inputFilter,
220                          filterMap,
221                          ctx);
222         }
223     }
224 }
225
Popular Tags