Statistics
| Revision:

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

History | View | Annotate | Download (4.32 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.Rectangle2D;
46
import java.io.File;
47
import java.io.IOException;
48

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

    
60
import com.iver.cit.gvsig.fmap.core.symbols.ISymbol;
61
import com.iver.utiles.XMLEntity;
62
/**
63
 *
64
 * @author jaume dominguez faus - jaume.dominguez@iver.es
65
 *
66
 */
67
public class SVGStyle extends AbstractStyle {
68

    
69
        private GVTBuilder gvtBuilder = new GVTBuilder();
70
    private UserAgentAdapter userAgent;
71
        private DocumentLoader loader;
72
        private StaticRenderer renderer = new StaticRenderer();
73
        private GraphicsNode gvtRoot;
74
        private BridgeContext ctx;
75
        private Element elt;
76
        private String sourceFile;
77
        protected static RenderingHints defaultRenderingHints;
78
    static {
79
        defaultRenderingHints = new RenderingHints(null);
80
        defaultRenderingHints.put(RenderingHints.KEY_ANTIALIASING,
81
                                  RenderingHints.VALUE_ANTIALIAS_ON);
82

    
83
        defaultRenderingHints.put(RenderingHints.KEY_INTERPOLATION,
84
                                  RenderingHints.VALUE_INTERPOLATION_BILINEAR);
85
    }
86

    
87
        public SVGStyle(){
88
                userAgent = new UserAgentAdapter();
89
                loader    = new DocumentLoader(userAgent);
90
                ctx       = new BridgeContext(userAgent, loader);
91
                renderer.setDoubleBuffered(true);
92
        }
93

    
94
        public void drawInsideRectangle(Graphics2D g, Rectangle r) {
95
                long t1, t2;
96
                Graphics2D g2 = (Graphics2D) g;
97
                RenderingHints renderingHints = new RenderingHints(defaultRenderingHints);
98
                g2.setRenderingHints(renderingHints);
99
                gvtRoot.setTransform((ViewBox.getViewTransform(null, elt, (float) r.getWidth()-1, (float) r.getHeight()-1)));
100
                gvtRoot.paint(g2);
101
        }
102

    
103
        public boolean isSuitableFor(ISymbol symbol) {
104
                return true;
105
        }
106

    
107
        public String getClassName() {
108
                return getClass().getName();
109
        }
110

    
111
        public XMLEntity getXMLEntity() {
112
                XMLEntity xml = new XMLEntity();
113
                xml.putProperty("className", getClassName());
114
                xml.putProperty("source", sourceFile);
115
                xml.putProperty("desc", getDescription());
116
                return xml;
117
        }
118

    
119
        public void setXMLEntity(XMLEntity xml) {
120
                try {
121
                        setSource(new File(xml.getStringProperty("source")));
122
                        setDescription(xml.getStringProperty("desc"));
123
                } catch (IOException e) {
124
                        // TODO Auto-generated catch block
125
                        e.printStackTrace();
126
                }
127
        }
128

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

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

    
145
}