KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > batik > svggen > SVGTexturePaint


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.svggen;
19
20 import java.awt.Graphics2D JavaDoc;
21 import java.awt.TexturePaint JavaDoc;
22 import java.awt.geom.Rectangle2D JavaDoc;
23 import java.awt.image.BufferedImage JavaDoc;
24 import java.awt.image.RenderedImage JavaDoc;
25
26 import org.apache.batik.ext.awt.g2d.GraphicContext;
27 import org.w3c.dom.Document JavaDoc;
28 import org.w3c.dom.Element JavaDoc;
29
30 /**
31  * Utility class that converts a TexturePaint object into an
32  * SVG pattern element
33  *
34  * @author <a HREF="mailto:vincent.hardy@eng.sun.com">Vincent Hardy</a>
35  * @version $Id: SVGTexturePaint.java,v 1.18 2004/08/18 07:15:09 vhardy Exp $
36  */

37 public class SVGTexturePaint extends AbstractSVGConverter {
38     /**
39      * @param generatorContext used to build Elements
40      */

41     public SVGTexturePaint(SVGGeneratorContext generatorContext) {
42         super(generatorContext);
43     }
44
45     /**
46      * Converts part or all of the input GraphicContext into
47      * a set of attribute/value pairs and related definitions
48      *
49      * @param gc GraphicContext to be converted
50      * @return descriptor of the attributes required to represent
51      * some or all of the GraphicContext state, along
52      * with the related definitions
53      * @see org.apache.batik.svggen.SVGDescriptor
54      */

55     public SVGDescriptor toSVG(GraphicContext gc) {
56         return toSVG((TexturePaint JavaDoc)gc.getPaint());
57     }
58
59     /**
60      * @param texture the TexturePaint to be converted
61      * @return a descriptor whose paint value references
62      * a pattern. The definition of the
63      * pattern in put in the patternDefsMap
64      */

65     public SVGPaintDescriptor toSVG(TexturePaint JavaDoc texture) {
66         // Reuse definition if pattern has already been converted
67
SVGPaintDescriptor patternDesc = (SVGPaintDescriptor)descMap.get(texture);
68         Document JavaDoc domFactory = generatorContext.domFactory;
69
70         if (patternDesc == null) {
71             Rectangle2D JavaDoc anchorRect = texture.getAnchorRect();
72             Element JavaDoc patternDef = domFactory.createElementNS(SVG_NAMESPACE_URI,
73                                                             SVG_PATTERN_TAG);
74             patternDef.setAttributeNS(null, SVG_PATTERN_UNITS_ATTRIBUTE,
75                                       SVG_USER_SPACE_ON_USE_VALUE);
76
77             //
78
// First, set the pattern anchor
79
//
80
patternDef.setAttributeNS(null, SVG_X_ATTRIBUTE,
81                                     doubleString(anchorRect.getX()));
82             patternDef.setAttributeNS(null, SVG_Y_ATTRIBUTE,
83                                     doubleString(anchorRect.getY()));
84             patternDef.setAttributeNS(null, SVG_WIDTH_ATTRIBUTE,
85                                     doubleString(anchorRect.getWidth()));
86             patternDef.setAttributeNS(null, SVG_HEIGHT_ATTRIBUTE,
87                                     doubleString(anchorRect.getHeight()));
88
89             //
90
// Now, add an image element for the image.
91
//
92
BufferedImage JavaDoc textureImage = texture.getImage();
93             //
94
// Rescale the image to fit the anchor rectangle
95
//
96
if (textureImage.getWidth() > 0 &&
97                 textureImage.getHeight() > 0){
98
99                 // Rescale only if necessary
100
if(textureImage.getWidth() != anchorRect.getWidth() ||
101                    textureImage.getHeight() != anchorRect.getHeight()){
102
103                     // Rescale only if anchor area is not a point or a line
104
if(anchorRect.getWidth() > 0 &&
105                        anchorRect.getHeight() > 0){
106                         double scaleX =
107                             anchorRect.getWidth()/textureImage.getWidth();
108                         double scaleY =
109                             anchorRect.getHeight()/textureImage.getHeight();
110                         BufferedImage JavaDoc newImage
111                             = new BufferedImage JavaDoc((int)(scaleX*
112                                                       textureImage.getWidth()),
113                                                 (int)(scaleY*
114                                                       textureImage.getHeight()),
115                                                 BufferedImage.TYPE_INT_ARGB);
116
117                         Graphics2D JavaDoc g = newImage.createGraphics();
118                         g.scale(scaleX, scaleY);
119                         g.drawImage(textureImage, 0, 0, null);
120                         g.dispose();
121
122                         textureImage = newImage;
123                     }
124                 }
125             }
126
127             // generatorContext.imageHandler.
128
// handleImage((RenderedImage)textureImage, imageElement,
129
// generatorContext);
130

131             Element JavaDoc patternContent
132                 = generatorContext.genericImageHandler.createElement
133                 (generatorContext);
134
135             generatorContext.genericImageHandler.handleImage
136                 ((RenderedImage JavaDoc)textureImage,
137                  patternContent,
138                  0,
139                  0,
140                  textureImage.getWidth(),
141                  textureImage.getHeight(),
142                  generatorContext);
143
144             patternDef.appendChild(patternContent);
145
146             patternDef.setAttributeNS(null, ATTR_ID,
147                                       generatorContext.idGenerator.
148                                       generateID(ID_PREFIX_PATTERN));
149
150             StringBuffer JavaDoc patternAttrBuf = new StringBuffer JavaDoc(URL_PREFIX);
151             patternAttrBuf.append(SIGN_POUND);
152             patternAttrBuf.append(patternDef.getAttributeNS(null, ATTR_ID));
153             patternAttrBuf.append(URL_SUFFIX);
154
155             patternDesc = new SVGPaintDescriptor(patternAttrBuf.toString(),
156                                                  SVG_OPAQUE_VALUE,
157                                                  patternDef);
158
159             descMap.put(texture, patternDesc);
160             defSet.add(patternDef);
161         }
162
163         return patternDesc;
164     }
165 }
166
Popular Tags