KickJava   Java API By Example, From Geeks To Geeks.

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


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.AffineTransform JavaDoc;
21 import java.awt.geom.Rectangle2D JavaDoc;
22
23 import org.apache.batik.ext.awt.image.renderable.Filter;
24 import org.apache.batik.gvt.CompositeGraphicsNode;
25 import org.apache.batik.gvt.GraphicsNode;
26 import org.apache.batik.gvt.filter.Mask;
27 import org.apache.batik.gvt.filter.MaskRable8Bit;
28 import org.w3c.dom.Element JavaDoc;
29 import org.w3c.dom.Node JavaDoc;
30
31 /**
32  * Bridge class for the <mask> element.
33  *
34  * @author <a HREF="mailto:tkormann@apache.org">Thierry Kormann</a>
35  * @version $Id: SVGMaskElementBridge.java,v 1.17 2004/08/18 07:12:35 vhardy Exp $
36  */

37 public class SVGMaskElementBridge extends AbstractSVGBridge
38     implements MaskBridge {
39
40     /**
41      * Constructs a new bridge for the &lt;mask> element.
42      */

43     public SVGMaskElementBridge() {}
44
45     /**
46      * Returns 'mask'.
47      */

48     public String JavaDoc getLocalName() {
49         return SVG_MASK_TAG;
50     }
51
52     /**
53      * Creates a <tt>Mask</tt> according to the specified parameters.
54      *
55      * @param ctx the bridge context to use
56      * @param maskElement the element that defines the mask
57      * @param maskedElement the element that references the mask element
58      * @param maskedNode the graphics node to mask
59      */

60     public Mask createMask(BridgeContext ctx,
61                            Element JavaDoc maskElement,
62                            Element JavaDoc maskedElement,
63                            GraphicsNode maskedNode) {
64
65         String JavaDoc s;
66
67         // get mask region using 'maskUnits'
68
Rectangle2D JavaDoc maskRegion = SVGUtilities.convertMaskRegion
69             (maskElement, maskedElement, maskedNode, ctx);
70
71         //
72
// Build the GVT tree that represents the mask
73
//
74
GVTBuilder builder = ctx.getGVTBuilder();
75         CompositeGraphicsNode maskNode = new CompositeGraphicsNode();
76         CompositeGraphicsNode maskNodeContent = new CompositeGraphicsNode();
77         maskNode.getChildren().add(maskNodeContent);
78         boolean hasChildren = false;
79         for(Node node = maskElement.getFirstChild();
80             node != null;
81             node = node.getNextSibling()){
82
83             // check if the node is a valid Element
84
if(node.getNodeType() != Node.ELEMENT_NODE) {
85                 continue;
86             }
87
88             Element JavaDoc child = (Element JavaDoc)node;
89             GraphicsNode gn = builder.build(ctx, child) ;
90             if(gn == null) {
91                 continue;
92             }
93             hasChildren = true;
94             maskNodeContent.getChildren().add(gn);
95         }
96         if (!hasChildren) {
97             return null; // empty mask
98
}
99
100         // 'transform' attribute
101
AffineTransform JavaDoc Tx;
102         s = maskElement.getAttributeNS(null, SVG_TRANSFORM_ATTRIBUTE);
103         if (s.length() != 0) {
104             Tx = SVGUtilities.convertTransform
105                 (maskElement, SVG_TRANSFORM_ATTRIBUTE, s);
106         } else {
107             Tx = new AffineTransform JavaDoc();
108         }
109
110         // 'maskContentUnits' attribute - default is userSpaceOnUse
111
short coordSystemType;
112         s = maskElement.getAttributeNS(null, SVG_MASK_CONTENT_UNITS_ATTRIBUTE);
113         if (s.length() == 0) {
114             coordSystemType = SVGUtilities.USER_SPACE_ON_USE;
115         } else {
116             coordSystemType = SVGUtilities.parseCoordinateSystem
117                 (maskElement, SVG_MASK_CONTENT_UNITS_ATTRIBUTE, s);
118         }
119
120         // additional transform to move to objectBoundingBox coordinate system
121
if (coordSystemType == SVGUtilities.OBJECT_BOUNDING_BOX) {
122             Tx = SVGUtilities.toObjectBBox(Tx, maskedNode);
123         }
124
125         maskNodeContent.setTransform(Tx);
126
127         Filter filter = maskedNode.getFilter();
128         if (filter == null) {
129             // Make the initial source as a RenderableImage
130
filter = maskedNode.getGraphicsNodeRable(true);
131         }
132
133         return new MaskRable8Bit(filter, maskNode, maskRegion);
134     }
135 }
136
Popular Tags