Statistics
| Revision:

root / trunk / extensions / extSymbology / src / org / gvsig / symbology / fmap / styles / SVGStyle.java @ 20768

History | View | Annotate | Download (5.25 KB)

1
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
2
 *
3
 * Copyright (C) 2005 IVER T.I. and Generalitat Valenciana.
4
 *
5
 * This program is free software; you can redistribute it and/or
6
 * modify it under the terms of the GNU General Public License
7
 * as published by the Free Software Foundation; either version 2
8
 * of the License, or (at your option) any later version.
9
 *
10
 * This program is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 * GNU General Public License for more details.
14
 *
15
 * You should have received a copy of the GNU General Public License
16
 * along with this program; if not, write to the Free Software
17
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
18
 *
19
 * For more information, contact:
20
 *
21
 *  Generalitat Valenciana
22
 *   Conselleria d'Infraestructures i Transport
23
 *   Av. Blasco Ib??ez, 50
24
 *   46010 VALENCIA
25
 *   SPAIN
26
 *
27
 *      +34 963862235
28
 *   gvsig@gva.es
29
 *      www.gvsig.gva.es
30
 *
31
 *    or
32
 *
33
 *   IVER T.I. S.A
34
 *   Salamanca 50
35
 *   46005 Valencia
36
 *   Spain
37
 *
38
 *   +34 963163400
39
 *   dac@iver.es
40
 */
41
package org.gvsig.symbology.fmap.styles;
42
import java.awt.Graphics2D;
43
import java.awt.Rectangle;
44
import java.awt.RenderingHints;
45
import java.awt.geom.AffineTransform;
46
import java.awt.geom.Rectangle2D;
47
import java.io.File;
48
import java.io.IOException;
49
import java.net.URL;
50

    
51
import org.apache.batik.bridge.BridgeContext;
52
import org.apache.batik.bridge.DocumentLoader;
53
import org.apache.batik.bridge.GVTBuilder;
54
import org.apache.batik.bridge.UserAgentAdapter;
55
import org.apache.batik.bridge.ViewBox;
56
import org.apache.batik.dom.svg.SVGOMDocument;
57
import org.apache.batik.gvt.GraphicsNode;
58
import org.apache.batik.gvt.renderer.StaticRenderer;
59
import org.w3c.dom.Document;
60
import org.w3c.dom.Element;
61

    
62
import com.iver.cit.gvsig.fmap.core.SymbologyFactory;
63
import com.iver.cit.gvsig.fmap.core.symbols.ISymbol;
64
/**
65
 * Style for a SVG files.This is a XML specification and file format for
66
 * describing two-dimensional vector graphics, both static and animated.
67
 * SVG can be purely declarative or may include scripting. Images can contain
68
 * hyperlinks using outbound simple XLinks.It is an open standard created
69
 * by the World Wide Web Consortium..
70
 *
71
 * @author jaume dominguez faus - jaume.dominguez@iver.es
72
 *
73
 */
74
public class SVGStyle extends BackgroundFileStyle {
75

    
76
        private GVTBuilder gvtBuilder = new GVTBuilder();
77
    private UserAgentAdapter userAgent;
78
        private DocumentLoader loader;
79
        private StaticRenderer renderer = new StaticRenderer();
80
        private GraphicsNode gvtRoot;
81
        private BridgeContext ctx;
82
        private Element elt;
83

    
84
        protected static RenderingHints defaultRenderingHints;
85
    static {
86
        defaultRenderingHints = new RenderingHints(null);
87
        defaultRenderingHints.put(RenderingHints.KEY_ANTIALIASING,
88
                                  RenderingHints.VALUE_ANTIALIAS_ON);
89

    
90
        defaultRenderingHints.put(RenderingHints.KEY_INTERPOLATION,
91
                                  RenderingHints.VALUE_INTERPOLATION_BILINEAR);
92
    }
93
    
94
    /**
95
     * Constructor method
96
     *
97
     */
98
        public SVGStyle(){
99
                userAgent = new UserAgentAdapter();
100
                loader    = new DocumentLoader(userAgent);
101
                ctx       = new BridgeContext(userAgent, loader);
102
                renderer.setDoubleBuffered(true);
103
        }
104

    
105
        public void drawInsideRectangle(Graphics2D g, Rectangle rect, boolean keepAspectRatio) {
106
                
107
                if (keepAspectRatio) {
108
                        
109
                        AffineTransform ataux = ViewBox.getViewTransform(null, elt,
110
                                        (float) rect.getWidth(), (float) rect.getHeight(), ctx);
111
                        RenderingHints renderingHints = new RenderingHints(null);
112
                        renderingHints.putAll(defaultRenderingHints);
113
                        g.setRenderingHints(renderingHints);
114
                        gvtRoot.setTransform(ataux);
115
                        gvtRoot.paint(g);
116
                
117
                } else {
118
                        
119
                        Rectangle2D bounds = gvtRoot.getBounds();
120
                        AffineTransform ataux = AffineTransform.getTranslateInstance(
121
                                        rect.getX(),- rect.getY()-rect.getHeight());
122
                        ataux.concatenate(AffineTransform.getScaleInstance(
123
                                        rect.getWidth()/bounds.getWidth(),
124
                                        rect.getHeight()/bounds.getHeight()));
125
                        RenderingHints renderingHints = new RenderingHints(null);
126
                        renderingHints.putAll(defaultRenderingHints);
127
                        g.setRenderingHints(renderingHints);
128
                        gvtRoot.setTransform(ataux);
129
                        gvtRoot.paint(g);
130
                }
131
        }
132

    
133
        public boolean isSuitableFor(ISymbol symbol) {
134
                return true;
135
        }
136

    
137
        public String getClassName() {
138
                return getClass().getName();
139
        }
140

    
141
        public void  setSource(URL url) throws IOException {
142
                
143
                File f = new File(url.getFile());
144
                File fAux = null;
145
                if(f.isAbsolute()){
146
                        sourceFile = url;
147
                        fAux=f;
148
                        isRelativePath=false;
149
                }
150
                else{
151
                        sourceFile = new URL(SymbologyFactory.SymbolLibraryPath + File.separator + f.getPath());
152
                        fAux= new File(sourceFile.getFile());
153
                        isRelativePath=true;
154
                }
155
                                 
156
                Document svgDoc = loader.loadDocument(fAux.toURI().toString());
157
                gvtRoot = gvtBuilder.build(ctx, svgDoc);
158
        renderer.setTree(gvtRoot);
159
        elt = ((SVGOMDocument)svgDoc).getRootElement();
160
        }
161

    
162
        public Rectangle getBounds() {
163
                Rectangle2D r = gvtRoot.getBounds();
164
                return new Rectangle((int) r.getX(),
165
                                 (int) r.getY(),
166
                                 (int) r.getWidth(),
167
                                 (int) r.getHeight());
168
        }
169

    
170
        public void drawOutline(Graphics2D g, Rectangle r) {
171
                drawInsideRectangle(g, r);
172
        }
173
}