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.scenegraph.g2d.nodes;
14 import java.awt.Graphics2D;
15 import java.awt.Point;
16 import java.awt.geom.AffineTransform;
17 import java.awt.geom.Rectangle2D;
18 import java.io.ByteArrayInputStream;
19 import java.io.IOException;
20 import java.lang.ref.WeakReference;
21 import java.math.BigInteger;
24 import java.security.MessageDigest;
25 import java.security.NoSuchAlgorithmException;
26 import java.util.ArrayList;
27 import java.util.HashMap;
28 import java.util.List;
30 import java.util.WeakHashMap;
32 import org.simantics.scenegraph.ExportableWidget.RasterOutputWidget;
33 import org.simantics.scenegraph.LoaderNode;
34 import org.simantics.scenegraph.ScenegraphUtils;
35 import org.simantics.scenegraph.g2d.G2DNode;
36 import org.simantics.scenegraph.utils.BufferedImage;
37 import org.simantics.scenegraph.utils.G2DUtils;
38 import org.simantics.scenegraph.utils.InitValueSupport;
39 import org.simantics.scenegraph.utils.MipMapBufferedImage;
40 import org.simantics.scenegraph.utils.MipMapVRamBufferedImage;
41 import org.simantics.scenegraph.utils.VRamBufferedImage;
42 import org.simantics.scl.runtime.function.Function1;
43 import org.simantics.scl.runtime.function.Function2;
44 import org.simantics.utils.threads.AWTThread;
46 import com.kitfox.svg.SVGCache;
47 import com.kitfox.svg.SVGDiagram;
48 import com.kitfox.svg.SVGElement;
49 import com.kitfox.svg.SVGException;
50 import com.kitfox.svg.SVGRoot;
51 import com.kitfox.svg.SVGUniverse;
52 import com.kitfox.svg.Text;
53 import com.kitfox.svg.Tspan;
54 import com.kitfox.svg.animation.AnimationElement;
57 public class SVGNode extends G2DNode implements InitValueSupport, LoaderNode {
59 public static class SVGNodeAssignment {
60 public String elementId;
61 public String attributeNameOrId;
63 public SVGNodeAssignment(String elementId, String attributeNameOrId, String value) {
64 this.elementId = elementId;
65 this.attributeNameOrId = attributeNameOrId;
69 public int hashCode() {
72 result = prime * result + ((attributeNameOrId == null) ? 0 : attributeNameOrId.hashCode());
73 result = prime * result + ((elementId == null) ? 0 : elementId.hashCode());
74 result = prime * result + ((value == null) ? 0 : value.hashCode());
78 public boolean equals(Object obj) {
83 if (getClass() != obj.getClass())
85 SVGNodeAssignment other = (SVGNodeAssignment) obj;
86 if (attributeNameOrId == null) {
87 if (other.attributeNameOrId != null)
89 } else if (!attributeNameOrId.equals(other.attributeNameOrId))
91 if (elementId == null) {
92 if (other.elementId != null)
94 } else if (!elementId.equals(other.elementId))
97 if (other.value != null)
99 } else if (!value.equals(other.value))
105 private static final long serialVersionUID = 8508750881358776559L;
107 protected String data = null;
108 protected String defaultData = null;
109 protected Point targetSize = null;
110 protected Boolean useMipMap = true;
111 protected Rectangle2D bounds = null;
113 protected List<SVGNodeAssignment> assignments = new ArrayList<SVGNodeAssignment>();
115 transient BufferedImage buffer = null;
116 transient String documentCache = null;
117 transient SVGDiagram diagramCache = null;
118 transient String dataHash = null;
120 static transient Map<String, WeakReference<BufferedImage>> bufferCache = new HashMap<String, WeakReference<BufferedImage>>();
123 public void cleanup() {
127 public void setAssignments(List<SVGNodeAssignment> ass) {
129 assignments.addAll(ass);
132 public void cleanDiagramCache() {
133 SVGDiagram d = diagramCache;
136 SVGUniverse univ = SVGCache.getSVGUniverse();
137 if (univ.decRefCountAndClear(d.getXMLBase()) == 0) {
139 //System.out.println("cleared: " + d.getXMLBase());
144 static WeakHashMap<String, String> dataCache = new WeakHashMap<String, String>();
146 @PropertySetter("SVG")
148 public void setData(String data) {
149 String cached = dataCache.get(data);
150 if (cached == null) {
152 dataCache.put(data, data);
155 this.defaultData = cached;
158 @SyncField("targetSize")
159 public void setTargetSize(Point p) {
160 this.targetSize = p; // FIXME: Point doesn't serialize correctly for some reason
163 @SyncField("targetSize")
164 public void setTargetSize(int x, int y) {
165 this.targetSize = new Point(x, y); // FIXME: Point doesn't serialize correctly for some reason
168 @SyncField("useMipMap")
169 public void useMipMap(Boolean use) {
170 this.useMipMap = use;
173 @PropertySetter("Bounds")
175 public void setBounds(Rectangle2D bounds) {
176 this.bounds = bounds;
180 public Rectangle2D getBoundsInLocal() {
187 public void render(Graphics2D g2d) {
189 return; // Not initialized
191 if (!data.equals(documentCache) || diagramCache == null || buffer == null)
194 AffineTransform ot = null;
195 if (!transform.isIdentity()) {
196 ot = g2d.getTransform();
197 g2d.transform(transform);
204 g2d.setTransform(ot);
207 protected String parseSVG() {
212 SVGUniverse univ = SVGCache.getSVGUniverse();
214 // NOTE: hard-coded to assume all SVG data is encoded in UTF-8
215 byte[] dataBytes = data.getBytes("UTF-8");
216 dataHash = digest(dataBytes, assignments);
217 if (diagramCache != null)
218 univ.decRefCount(diagramCache.getXMLBase());
219 URI uri = univ.loadSVG(new ByteArrayInputStream(dataBytes), dataHash);
220 diagramCache = univ.getDiagram(uri, false);
221 if (diagramCache != null) {
222 if (diagramCache.getRoot() == null) {
223 diagramCache = univ.getDiagram(univ.loadSVG(BROKEN_SVG_DATA), false);
225 } else if (diagramCache.getRoot().getBoundingBox().isEmpty()) {
226 diagramCache = univ.getDiagram(univ.loadSVG(EMPTY_SVG_DATA), false);
229 for(SVGNodeAssignment ass : assignments) {
230 SVGElement e = diagramCache.getElement(ass.elementId);
232 if("$text".equals(ass.attributeNameOrId)) {
234 t.setText(ass.value);
235 Text text = (Text)t.getParent();
238 e.setAttribute(ass.attributeNameOrId, AnimationElement.AT_AUTO, ass.value);
242 if(!assignments.isEmpty())
243 diagramCache.updateTime(0);
246 documentCache = data;
247 if (diagramCache != null) {
248 setBounds((Rectangle2D) diagramCache.getRoot().getBoundingBox().clone());
249 univ.incRefCount(diagramCache.getXMLBase());
251 setBounds(new Rectangle2D.Double());
254 } catch (SVGException e) {
255 setBounds((Rectangle2D) diagramCache.getViewRect().clone());
256 } catch (IOException e) {
263 public static Rectangle2D getBounds(String data) {
264 return getBounds(data, null);
267 public static Rectangle2D getBounds(String data, List<SVGNodeAssignment> assignments) {
269 new Exception("null SVG data").printStackTrace();
273 SVGDiagram diagramCache = null;
275 // NOTE: hard-coded to assume all SVG data is encoded in UTF-8
276 byte[] dataBytes = data.getBytes("UTF-8");
277 String digest = digest(dataBytes, assignments);
279 SVGUniverse univ = SVGCache.getSVGUniverse();
280 // TODO: this completely removes any parallel processing from the SVG loading which would be nice to have.
281 synchronized (univ) {
282 //System.out.println(Thread.currentThread() + ": LOADING SVG: " + digest);
283 URI uri = univ.loadSVG(new ByteArrayInputStream(dataBytes), digest);
284 diagramCache = univ.getDiagram(uri, false);
285 if (diagramCache != null) {
286 if (diagramCache.getRoot() == null) {
287 diagramCache = univ.getDiagram(univ.loadSVG(BROKEN_SVG_DATA));
288 } else if (diagramCache.getRoot().getBoundingBox().isEmpty()) {
289 diagramCache = univ.getDiagram(univ.loadSVG(EMPTY_SVG_DATA));
294 Rectangle2D rect = null;
295 if (diagramCache != null) {
296 SVGRoot root = diagramCache.getRoot();
297 Rectangle2D bbox = root.getBoundingBox();
298 rect = (Rectangle2D) bbox.clone();
300 rect = new Rectangle2D.Double();
303 } catch (SVGException e) {
304 return ((Rectangle2D) diagramCache.getViewRect().clone());
305 } catch(IOException e) {
310 public static Rectangle2D getRealBounds(String data) {
311 return getRealBounds(data, null);
314 public static Rectangle2D getRealBounds(String data, List<SVGNodeAssignment> assignments) {
316 new Exception("null SVG data").printStackTrace();
320 SVGDiagram diagramCache = null;
322 // NOTE: hard-coded to assume all SVG data is encoded in UTF-8
323 byte[] dataBytes = data.getBytes("UTF-8");
324 String digest = digest(dataBytes, assignments);
326 SVGUniverse univ = SVGCache.getSVGUniverse();
327 // TODO: this completely removes any parallel processing from the SVG loading which would be nice to have.
328 synchronized (univ) {
329 //System.out.println(Thread.currentThread() + ": LOADING SVG: " + digest);
330 URI uri = univ.loadSVG(new ByteArrayInputStream(dataBytes), digest);
331 diagramCache = univ.getDiagram(uri, false);
332 if (diagramCache != null) {
333 SVGRoot root = diagramCache.getRoot();
334 if (root == null) return new Rectangle2D.Double();
335 return (Rectangle2D)root.getBoundingBox().clone();
338 } catch (SVGException e) {
339 return ((Rectangle2D) diagramCache.getViewRect().clone());
340 } catch(IOException e) {
345 protected void initBuffer(Graphics2D g2d) {
347 if (!data.equals(documentCache) || diagramCache == null) {
348 dataHash = parseSVG();
349 if (diagramCache == null) {
350 System.err.println("UNABLE TO PARSE SVG:\n" + data);
355 if (buffer != null) {
358 diagramCache.setIgnoringClipHeuristic(true); // FIXME
359 if(bufferCache.containsKey(dataHash) && bufferCache.get(dataHash).get() != null) {
360 buffer = bufferCache.get(dataHash).get();
361 } else if(diagramCache.getViewRect().getWidth()==0 || diagramCache.getViewRect().getHeight()==0) {
363 } else if(useMipMap) {
364 if(G2DUtils.isAccelerated(g2d)) {
365 buffer = new MipMapVRamBufferedImage(diagramCache, bounds, targetSize);
367 buffer = new MipMapBufferedImage(diagramCache, bounds, targetSize);
369 bufferCache.put(dataHash, new WeakReference<BufferedImage>(buffer));
371 if(G2DUtils.isAccelerated(g2d)) {
372 buffer = new VRamBufferedImage(diagramCache, bounds, targetSize);
374 buffer = new BufferedImage(diagramCache, bounds, targetSize);
376 bufferCache.put(dataHash, new WeakReference<BufferedImage>(buffer));
380 public void setProperty(String field, Object value) {
381 if("data".equals(field)) {
382 // System.out.println("SVGNode data -> " + value);
383 this.data = (String)value;
384 } else if ("z".equals(field)) {
385 // System.out.println("SVGNode z -> " + value);
386 setZIndex((Integer)value);
387 } else if ("position".equals(field)) {
388 // System.out.println("SVGNode position -> " + value);
389 Point point = (Point)value;
390 setTransform(AffineTransform.getTranslateInstance(point.x, point.y));
391 // setPosition(point.x, point.y);
396 public void initValues() {
402 static WeakHashMap<String, String> digestCache = new WeakHashMap<String, String>();
404 static String digest(byte[] dataBytes, List<SVGNodeAssignment> assignments) {
406 MessageDigest md = MessageDigest.getInstance("MD5");
407 byte[] messageDigest = md.digest(dataBytes);
408 BigInteger number = new BigInteger(1, messageDigest);
409 String dataHash = number.toString(16) + (assignments != null ? assignments.hashCode() : 0);
410 String result = digestCache.get(dataHash);
413 digestCache.put(dataHash,dataHash);
416 } catch (NoSuchAlgorithmException e) {
418 throw new Error("MD5 digest must exist.");
422 static URL BROKEN_SVG_DATA = SVGNode.class.getResource("broken.svg");
423 static URL EMPTY_SVG_DATA = SVGNode.class.getResource("empty.svg");
426 public Function1<Object, Boolean> getPropertyFunction(String propertyName) {
427 return ScenegraphUtils.getMethodPropertyFunction(AWTThread.getThreadAccess(), this, propertyName);
431 public <T> T getProperty(String propertyName) {
436 public void setPropertyCallback(Function2<String, Object, Boolean> callback) {
439 public void synchronizeDocument(String document) {
443 public void synchronizeTransform(double[] data) {
444 this.setTransform(new AffineTransform(data));