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