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
10 * VTT Technical Research Centre of Finland - initial API and implementation
\r
11 *******************************************************************************/
\r
12 package org.simantics.g2d.svg;
\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
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
32 * This is a SVG implementation to the PaintableSymbol interface.
\r
34 public class SVGImage implements Image {
\r
36 static EnumSet<Feature> caps = EnumSet.of(Feature.Vector);
\r
38 private Rectangle2D bounds;
\r
39 private final String nodeIdentifier;
\r
40 private final String svgDocument;
\r
41 private Point targetSize;
\r
43 public static SVGImage loadFromURL(String nodeIdentifier, URL url) throws IOException {
\r
44 InputStream in = url.openStream();
\r
46 return new SVGImage(nodeIdentifier, in);
\r
52 public static IFactory<Image> createFactoryFromString(String nodeIdentifier, String svgDocument) {
\r
53 return createFactoryFromString(svgDocument, null);
\r
56 public static IFactory<Image> createFactoryFromString(String nodeIdentifier, String svgDocument, Point targetSize) {
\r
57 return new SVGFactory(nodeIdentifier, svgDocument, targetSize);
\r
60 static class SVGFactory implements IFactory<Image> {
\r
61 String nodeIdentifier;
\r
64 public SVGFactory(String nodeIdentifier, String document) {
\r
65 this(nodeIdentifier, document, null);
\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
73 this.nodeIdentifier = nodeIdentifier;
\r
74 this.document = document;
\r
75 this.targetSize = referenceSize;
\r
78 public Image get() throws ProvisionException {
\r
79 return new SVGImage(nodeIdentifier, document, targetSize);
\r
82 public boolean equals(Object obj) {
\r
87 if (!obj.getClass().equals(getClass()))
\r
90 SVGFactory other = (SVGFactory)obj;
\r
92 if (!nodeIdentifier.equals(other.nodeIdentifier))
\r
94 if (targetSize != null) {
\r
95 if (!targetSize.equals(other.targetSize))
\r
98 if (other.targetSize != null)
\r
102 return document.equals(other.document);
\r
105 public int hashCode() {
\r
106 return nodeIdentifier.hashCode() * 31 + document.hashCode() + 123;
\r
110 public SVGImage(String nodeIdentifier, String svgDocument)
\r
112 this(nodeIdentifier, svgDocument, null);
\r
115 public SVGImage(String nodeIdentifier, String svgDocument, Point targetSize)
\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
122 this.nodeIdentifier = nodeIdentifier;
\r
123 this.svgDocument = svgDocument;
\r
124 this.targetSize = targetSize;
\r
125 //this.bounds = SVGNode.getBounds(svgDocument);
\r
128 public SVGImage(String nodeIdentifier, InputStream svgInput)
\r
130 if (nodeIdentifier == null)
\r
131 throw new NullPointerException("nodeIdentifier is null");
\r
135 BufferedReader reader = new BufferedReader(new InputStreamReader(svgInput, "UTF-8"));
\r
137 while((line = reader.readLine()) != null) {
\r
140 } catch(IOException e) {
\r
143 this.nodeIdentifier = nodeIdentifier;
\r
144 this.bounds = SVGNode.getBounds(data);
\r
145 this.svgDocument = data;
\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
153 SVGNode node = parent.getOrCreateNode(nodeIdentifier, SVGNode.class);
\r
154 node.setData(svgDocument);
\r
155 node.setTargetSize(targetSize);
\r
156 node.useMipMap(true);
\r
162 public Rectangle2D getBounds() {
\r
163 if(bounds == null) bounds = SVGNode.getBounds(svgDocument);
\r
168 public Shape getOutline() {
\r
169 return getBounds();
\r
173 public void addImageListener(ImageListener listener) {
\r
177 public EnumSet<Feature> getFeatures() {
\r
182 public void removeImageListener(ImageListener listener) {
\r