]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.g2d/src/org/simantics/g2d/svg/SVGImage.java
Fixed all line endings of the repository
[simantics/platform.git] / bundles / org.simantics.g2d / src / org / simantics / g2d / svg / SVGImage.java
1 /*******************************************************************************
2  * Copyright (c) 2007, 2010 Association for Decentralized Information Management
3  * in Industry THTH ry.
4  * All rights reserved. This program and the accompanying materials
5  * are made available under the terms of the Eclipse Public License v1.0
6  * which accompanies this distribution, and is available at
7  * http://www.eclipse.org/legal/epl-v10.html
8  *
9  * Contributors:
10  *     VTT Technical Research Centre of Finland - initial API and implementation
11  *******************************************************************************/
12 package org.simantics.g2d.svg;
13
14 import java.awt.Point;
15 import java.awt.Shape;
16 import java.awt.geom.Rectangle2D;
17 import java.io.BufferedReader;
18 import java.io.IOException;
19 import java.io.InputStream;
20 import java.io.InputStreamReader;
21 import java.net.URL;
22 import java.util.EnumSet;
23
24 import org.simantics.g2d.image.Image;
25 import org.simantics.scenegraph.Node;
26 import org.simantics.scenegraph.g2d.G2DParentNode;
27 import org.simantics.scenegraph.g2d.nodes.SVGNode;
28 import org.simantics.utils.datastructures.cache.IFactory;
29 import org.simantics.utils.datastructures.cache.ProvisionException;
30
31 /**
32  * This is a SVG implementation to the PaintableSymbol interface.
33  */
34 public class SVGImage implements Image {
35
36     static EnumSet<Feature> caps = EnumSet.of(Feature.Vector);
37
38     private Rectangle2D bounds;
39     private final String nodeIdentifier;
40     private final String svgDocument;
41     private Point targetSize;
42
43     public static SVGImage loadFromURL(String nodeIdentifier, URL url) throws IOException {
44         InputStream in = url.openStream();
45         try {
46             return new SVGImage(nodeIdentifier, in);
47         } finally {
48             in.close();
49         }
50     }
51
52     public static IFactory<Image> createFactoryFromString(String nodeIdentifier, String svgDocument) {
53         return createFactoryFromString(svgDocument, null);
54     }
55
56     public static IFactory<Image> createFactoryFromString(String nodeIdentifier, String svgDocument, Point targetSize) {
57         return new SVGFactory(nodeIdentifier, svgDocument, targetSize);
58     }
59
60     static class SVGFactory implements IFactory<Image> {
61         String nodeIdentifier;
62         String document;
63         Point targetSize;
64         public SVGFactory(String nodeIdentifier, String document) {
65             this(nodeIdentifier, document, null);
66         }
67         public SVGFactory(String nodeIdentifier, String document, Point referenceSize) {
68             if (nodeIdentifier == null)
69                 throw new NullPointerException("nodeIdentifier is null");
70             if (document == null)
71                 throw new NullPointerException("document is null");
72
73             this.nodeIdentifier = nodeIdentifier;
74             this.document = document;
75             this.targetSize = referenceSize;
76         }
77         @Override
78         public Image get() throws ProvisionException {
79             return new SVGImage(nodeIdentifier, document, targetSize);
80         }
81         @Override
82         public boolean equals(Object obj) {
83             if (this == obj)
84                 return true;
85             if (obj == null)
86                 return false;
87             if (!obj.getClass().equals(getClass()))
88                 return false;
89
90             SVGFactory other = (SVGFactory)obj;
91
92             if (!nodeIdentifier.equals(other.nodeIdentifier))
93                 return false;
94             if (targetSize != null) {
95                 if (!targetSize.equals(other.targetSize))
96                     return false;
97             } else {
98                 if (other.targetSize != null)
99                     return false;
100             }
101
102             return document.equals(other.document);
103         }
104         @Override
105         public int hashCode() {
106             return nodeIdentifier.hashCode() * 31 + document.hashCode() + 123;
107         }
108     }
109
110     public SVGImage(String nodeIdentifier, String svgDocument)
111     {
112         this(nodeIdentifier, svgDocument, null);
113     }
114
115     public SVGImage(String nodeIdentifier, String svgDocument, Point targetSize)
116     {
117         if (nodeIdentifier == null)
118             throw new NullPointerException("nodeIdentifier is null");
119         if (svgDocument == null)
120             throw new NullPointerException("svgDocument is null");
121
122         this.nodeIdentifier = nodeIdentifier;
123         this.svgDocument = svgDocument;
124         this.targetSize = targetSize;
125         //this.bounds = SVGNode.getBounds(svgDocument);
126     }
127
128     public SVGImage(String nodeIdentifier, InputStream svgInput)
129     {
130         if (nodeIdentifier == null)
131             throw new NullPointerException("nodeIdentifier is null");
132
133         String data = "";
134         try {
135             BufferedReader reader = new BufferedReader(new InputStreamReader(svgInput, "UTF-8"));
136             String line = "";
137             while((line = reader.readLine()) != null) {
138                 data += line+"\n";
139             }
140         } catch(IOException e) {
141         }
142
143         this.nodeIdentifier = nodeIdentifier;
144         this.bounds = SVGNode.getBounds(data);
145         this.svgDocument = data;
146     }
147
148     @Override
149     public Node init(G2DParentNode parent) {
150         // FIXME: mipmaps enabled here by default, since some apps just don't work without them.
151         // Figure out a way to pass the mipmap argument from above
152
153         SVGNode node = parent.getOrCreateNode(nodeIdentifier, SVGNode.class);
154         node.setData(svgDocument);
155         node.setTargetSize(targetSize);
156         node.useMipMap(true);
157
158         return node;
159     }
160
161     @Override
162     public Rectangle2D getBounds() {
163         if(bounds == null) bounds = SVGNode.getBounds(svgDocument);
164         return bounds;
165     }
166
167     @Override
168     public Shape getOutline() {
169         return getBounds();
170     }
171
172     @Override
173     public void addImageListener(ImageListener listener) {
174     }
175
176     @Override
177     public EnumSet<Feature> getFeatures() {
178         return caps;
179     }
180
181     @Override
182     public void removeImageListener(ImageListener listener) {
183     }
184
185 }