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