]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.g2d/src/org/simantics/g2d/svg/SVGImage.java
Two rendering glitch fixes for time series charts
[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     protected final String nodeIdentifier;
40     protected final String svgDocument;
41     protected Point targetSize;
42
43     public static SVGImage loadFromURL(String nodeIdentifier, URL url) throws IOException {
44         try (InputStream in = url.openStream()) {
45             return new SVGImage(nodeIdentifier, in);
46         }
47     }
48
49     public static IFactory<Image> createFactoryFromString(String nodeIdentifier, String svgDocument) {
50         return createFactoryFromString(svgDocument, null);
51     }
52
53     public static IFactory<Image> createFactoryFromString(String nodeIdentifier, String svgDocument, Point targetSize) {
54         return new SVGFactory(nodeIdentifier, svgDocument, targetSize);
55     }
56
57     static class SVGFactory implements IFactory<Image> {
58         String nodeIdentifier;
59         String document;
60         Point targetSize;
61         public SVGFactory(String nodeIdentifier, String document) {
62             this(nodeIdentifier, document, null);
63         }
64         public SVGFactory(String nodeIdentifier, String document, Point referenceSize) {
65             if (nodeIdentifier == null)
66                 throw new NullPointerException("nodeIdentifier is null");
67             if (document == null)
68                 throw new NullPointerException("document is null");
69
70             this.nodeIdentifier = nodeIdentifier;
71             this.document = document;
72             this.targetSize = referenceSize;
73         }
74         @Override
75         public Image get() throws ProvisionException {
76             return new SVGImage(nodeIdentifier, document, targetSize);
77         }
78         @Override
79         public boolean equals(Object obj) {
80             if (this == obj)
81                 return true;
82             if (obj == null)
83                 return false;
84             if (!obj.getClass().equals(getClass()))
85                 return false;
86
87             SVGFactory other = (SVGFactory)obj;
88
89             if (!nodeIdentifier.equals(other.nodeIdentifier))
90                 return false;
91             if (targetSize != null) {
92                 if (!targetSize.equals(other.targetSize))
93                     return false;
94             } else {
95                 if (other.targetSize != null)
96                     return false;
97             }
98
99             return document.equals(other.document);
100         }
101         @Override
102         public int hashCode() {
103             return nodeIdentifier.hashCode() * 31 + document.hashCode() + 123;
104         }
105     }
106
107     public SVGImage(String nodeIdentifier, String svgDocument)
108     {
109         this(nodeIdentifier, svgDocument, null);
110     }
111
112     public SVGImage(String nodeIdentifier, String svgDocument, Point targetSize)
113     {
114         if (nodeIdentifier == null)
115             throw new NullPointerException("nodeIdentifier is null");
116         if (svgDocument == null)
117             throw new NullPointerException("svgDocument is null");
118
119         this.nodeIdentifier = nodeIdentifier;
120         this.svgDocument = svgDocument;
121         this.targetSize = targetSize;
122         //this.bounds = SVGNode.getBounds(svgDocument);
123     }
124
125     public SVGImage(String nodeIdentifier, InputStream svgInput)
126     {
127         if (nodeIdentifier == null)
128             throw new NullPointerException("nodeIdentifier is null");
129
130         String data = "";
131         try {
132             BufferedReader reader = new BufferedReader(new InputStreamReader(svgInput, "UTF-8"));
133             String line = "";
134             while((line = reader.readLine()) != null) {
135                 data += line+"\n";
136             }
137         } catch(IOException e) {
138         }
139
140         this.nodeIdentifier = nodeIdentifier;
141         this.bounds = SVGNode.getBounds(data);
142         this.svgDocument = data;
143     }
144
145     @Override
146     public Node init(G2DParentNode parent) {
147         // FIXME: mipmaps enabled here by default, since some apps just don't work without them.
148         // Figure out a way to pass the mipmap argument from above
149
150         SVGNode node = parent.getOrCreateNode(nodeIdentifier, SVGNode.class);
151         node.setData(svgDocument);
152         node.setTargetSize(targetSize);
153         node.useMipMap(true);
154
155         return node;
156     }
157
158     @Override
159     public Rectangle2D getBounds() {
160         if(bounds == null) bounds = SVGNode.getBounds(svgDocument);
161         return bounds;
162     }
163
164     @Override
165     public Shape getOutline() {
166         return getBounds();
167     }
168
169     @Override
170     public void addImageListener(ImageListener listener) {
171     }
172
173     @Override
174     public EnumSet<Feature> getFeatures() {
175         return caps;
176     }
177
178     @Override
179     public void removeImageListener(ImageListener listener) {
180     }
181
182 }