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 protected final String nodeIdentifier;
40 protected final String svgDocument;
41 protected Point targetSize;
43 public static SVGImage loadFromURL(String nodeIdentifier, URL url) throws IOException {
44 try (InputStream in = url.openStream()) {
45 return new SVGImage(nodeIdentifier, in);
49 public static IFactory<Image> createFactoryFromString(String nodeIdentifier, String svgDocument) {
50 return createFactoryFromString(svgDocument, null);
53 public static IFactory<Image> createFactoryFromString(String nodeIdentifier, String svgDocument, Point targetSize) {
54 return new SVGFactory(nodeIdentifier, svgDocument, targetSize);
57 static class SVGFactory implements IFactory<Image> {
58 String nodeIdentifier;
61 public SVGFactory(String nodeIdentifier, String document) {
62 this(nodeIdentifier, document, null);
64 public SVGFactory(String nodeIdentifier, String document, Point referenceSize) {
65 if (nodeIdentifier == null)
66 throw new NullPointerException("nodeIdentifier is null");
68 throw new NullPointerException("document is null");
70 this.nodeIdentifier = nodeIdentifier;
71 this.document = document;
72 this.targetSize = referenceSize;
75 public Image get() throws ProvisionException {
76 return new SVGImage(nodeIdentifier, document, targetSize);
79 public boolean equals(Object obj) {
84 if (!obj.getClass().equals(getClass()))
87 SVGFactory other = (SVGFactory)obj;
89 if (!nodeIdentifier.equals(other.nodeIdentifier))
91 if (targetSize != null) {
92 if (!targetSize.equals(other.targetSize))
95 if (other.targetSize != null)
99 return document.equals(other.document);
102 public int hashCode() {
103 return nodeIdentifier.hashCode() * 31 + document.hashCode() + 123;
107 public SVGImage(String nodeIdentifier, String svgDocument)
109 this(nodeIdentifier, svgDocument, null);
112 public SVGImage(String nodeIdentifier, String svgDocument, Point targetSize)
114 if (nodeIdentifier == null)
115 throw new NullPointerException("nodeIdentifier is null");
116 if (svgDocument == null)
117 throw new NullPointerException("svgDocument is null");
119 this.nodeIdentifier = nodeIdentifier;
120 this.svgDocument = svgDocument;
121 this.targetSize = targetSize;
122 //this.bounds = SVGNode.getBounds(svgDocument);
125 public SVGImage(String nodeIdentifier, InputStream svgInput)
127 if (nodeIdentifier == null)
128 throw new NullPointerException("nodeIdentifier is null");
132 BufferedReader reader = new BufferedReader(new InputStreamReader(svgInput, "UTF-8"));
134 while((line = reader.readLine()) != null) {
137 } catch(IOException e) {
140 this.nodeIdentifier = nodeIdentifier;
141 this.bounds = SVGNode.getBounds(data);
142 this.svgDocument = data;
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
150 SVGNode node = parent.getOrCreateNode(nodeIdentifier, SVGNode.class);
151 node.setData(svgDocument);
152 node.setTargetSize(targetSize);
153 node.useMipMap(true);
159 public Rectangle2D getBounds() {
160 if(bounds == null) bounds = SVGNode.getBounds(svgDocument);
165 public Shape getOutline() {
170 public void addImageListener(ImageListener listener) {
174 public EnumSet<Feature> getFeatures() {
179 public void removeImageListener(ImageListener listener) {