Statistics
| Revision:

root / trunk / libraries / libFMap / src / com / iver / cit / gvsig / fmap / core / styles / SVGStyle.java @ 13881

History | View | Annotate | Download (4.44 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 com.iver.cit.gvsig.fmap.core.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

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

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

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

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

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

    
102
        public void drawInsideRectangle(Graphics2D g, Rectangle rect) {
103
                AffineTransform ataux = new AffineTransform();
104

    
105
        ataux.translate(0,0);//rect.getX(), rect.getY());
106

    
107
        ataux.concatenate(ViewBox.getViewTransform(null, elt,
108
                        (float) rect.getWidth(), (float) rect.getHeight()));
109

    
110
        RenderingHints renderingHints = new RenderingHints(null);
111
        renderingHints.putAll(defaultRenderingHints);
112
                g.setRenderingHints(renderingHints);
113
                gvtRoot.setTransform(ataux);
114
                gvtRoot.paint(g);
115
        }
116

    
117
        public boolean isSuitableFor(ISymbol symbol) {
118
                return true;
119
        }
120

    
121
        public String getClassName() {
122
                return getClass().getName();
123
        }
124

    
125

    
126

    
127
        public void setSource(File f) throws IOException {
128
                sourceFile = f.getAbsolutePath();
129
                Document svgDoc = loader.loadDocument(f.toURI().toString());
130
        gvtRoot = gvtBuilder.build(ctx, svgDoc);
131
        renderer.setTree(gvtRoot);
132
        elt = ((SVGDocument)svgDoc).getRootElement();
133
        }
134

    
135
        public Rectangle getBounds() {
136
                Rectangle2D r = gvtRoot.getBounds();
137
                return new Rectangle((int) r.getX(),
138
                                 (int) r.getY(),
139
                                 (int) r.getWidth(),
140
                                 (int) r.getHeight());
141
        }
142

    
143
        public void drawOutline(Graphics2D g, Rectangle r) {
144
                drawInsideRectangle(g, r);
145
        }
146
}