Statistics
| Revision:

svn-gvsig-desktop / trunk / libraries / libFMap / src / com / iver / cit / gvsig / fmap / core / styles / SVGStyle.java @ 11051

History | View | Annotate | Download (4.55 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
import com.iver.utiles.XMLEntity;
63
/**
64
 *
65
 * @author jaume dominguez faus - jaume.dominguez@iver.es
66
 *
67
 */
68
public class SVGStyle extends AbstractStyle {
69

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

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

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

    
95
        public void drawInsideRectangle(Graphics2D g, Rectangle rect) {
96
                AffineTransform ataux = new AffineTransform();
97

    
98
        ataux.translate(0,0);//rect.getX(), rect.getY());
99

    
100
        ataux.concatenate(ViewBox.getViewTransform(null, elt,
101
                        (float) rect.getWidth(), (float) rect.getHeight()));
102

    
103
        RenderingHints renderingHints = new RenderingHints(defaultRenderingHints);
104
                g.setRenderingHints(renderingHints);
105
                gvtRoot.setTransform(ataux);
106
                gvtRoot.paint(g);
107
        }
108

    
109
        public boolean isSuitableFor(ISymbol symbol) {
110
                return true;
111
        }
112

    
113
        public String getClassName() {
114
                return getClass().getName();
115
        }
116

    
117
        public XMLEntity getXMLEntity() {
118
                XMLEntity xml = new XMLEntity();
119
                xml.putProperty("className", getClassName());
120
                xml.putProperty("source", sourceFile);
121
                xml.putProperty("desc", getDescription());
122
                return xml;
123
        }
124

    
125
        public void setXMLEntity(XMLEntity xml) {
126
                try {
127
                        setSource(new File(xml.getStringProperty("source")));
128
                        setDescription(xml.getStringProperty("desc"));
129
                } catch (IOException e) {
130
                        // TODO Auto-generated catch block
131
                        e.printStackTrace();
132
                }
133
        }
134

    
135
        public void setSource(File f) throws IOException {
136
                sourceFile = f.getAbsolutePath();
137
                Document svgDoc = loader.loadDocument(f.toURI().toString());
138
        gvtRoot = gvtBuilder.build(ctx, svgDoc);
139
        renderer.setTree(gvtRoot);
140
        elt = ((SVGDocument)svgDoc).getRootElement();
141
        }
142

    
143
        protected Rectangle getBounds() {
144
                Rectangle2D r = gvtRoot.getBounds();
145
                return new Rectangle((int) r.getX(),
146
                                 (int) r.getY(),
147
                                 (int) r.getWidth(),
148
                                 (int) r.getHeight());
149
        }
150

    
151
        public void drawOutline(Graphics2D g, Rectangle r) {
152
                drawInsideRectangle(g, r);
153
        }
154
}