X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=blobdiff_plain;f=bundles%2Forg.simantics.scenegraph%2Fsrc%2Forg%2Fsimantics%2Fscenegraph%2Fg2d%2Fnodes%2FSVGNode.java;h=ec85fd75c86e7893a8b7341bcfde6641e380d244;hb=04f200d2010339b05ba016b6f0c247653f5bdc97;hp=97de387f8b0fce3bfcf2728a19b11cb9327b7894;hpb=0ae2b770234dfc3cbb18bd38f324125cf0faca07;p=simantics%2Fplatform.git diff --git a/bundles/org.simantics.scenegraph/src/org/simantics/scenegraph/g2d/nodes/SVGNode.java b/bundles/org.simantics.scenegraph/src/org/simantics/scenegraph/g2d/nodes/SVGNode.java index 97de387f8..ec85fd75c 100644 --- a/bundles/org.simantics.scenegraph/src/org/simantics/scenegraph/g2d/nodes/SVGNode.java +++ b/bundles/org.simantics.scenegraph/src/org/simantics/scenegraph/g2d/nodes/SVGNode.java @@ -27,6 +27,7 @@ import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; +import java.util.Set; import java.util.WeakHashMap; import org.simantics.scenegraph.ExportableWidget.RasterOutputWidget; @@ -56,53 +57,7 @@ import com.kitfox.svg.animation.AnimationElement; @RasterOutputWidget public class SVGNode extends G2DNode implements InitValueSupport, LoaderNode { - public static class SVGNodeAssignment { - public String elementId; - public String attributeNameOrId; - public String value; - public SVGNodeAssignment(String elementId, String attributeNameOrId, String value) { - this.elementId = elementId; - this.attributeNameOrId = attributeNameOrId; - this.value = value; - } - @Override - public int hashCode() { - final int prime = 31; - int result = 1; - result = prime * result + ((attributeNameOrId == null) ? 0 : attributeNameOrId.hashCode()); - result = prime * result + ((elementId == null) ? 0 : elementId.hashCode()); - result = prime * result + ((value == null) ? 0 : value.hashCode()); - return result; - } - @Override - public boolean equals(Object obj) { - if (this == obj) - return true; - if (obj == null) - return false; - if (getClass() != obj.getClass()) - return false; - SVGNodeAssignment other = (SVGNodeAssignment) obj; - if (attributeNameOrId == null) { - if (other.attributeNameOrId != null) - return false; - } else if (!attributeNameOrId.equals(other.attributeNameOrId)) - return false; - if (elementId == null) { - if (other.elementId != null) - return false; - } else if (!elementId.equals(other.elementId)) - return false; - if (value == null) { - if (other.value != null) - return false; - } else if (!value.equals(other.value)) - return false; - return true; - } - } - - private static final long serialVersionUID = 8508750881358776559L; + private static final long serialVersionUID = 8508750881358776559L; protected String data = null; protected String defaultData = null; @@ -208,51 +163,82 @@ public class SVGNode extends G2DNode implements InitValueSupport, LoaderNode { if (data == null) return null; + SVGUniverse univ = SVGCache.getSVGUniverse(); try { - SVGUniverse univ = SVGCache.getSVGUniverse(); - synchronized(univ) { - // NOTE: hard-coded to assume all SVG data is encoded in UTF-8 - byte[] dataBytes = data.getBytes("UTF-8"); - dataHash = digest(dataBytes, assignments); - if (diagramCache != null) - univ.decRefCount(diagramCache.getXMLBase()); - URI uri = univ.loadSVG(new ByteArrayInputStream(dataBytes), dataHash); - diagramCache = univ.getDiagram(uri, false); - if (diagramCache != null) { - if (diagramCache.getRoot() == null) { - diagramCache = univ.getDiagram(univ.loadSVG(BROKEN_SVG_DATA), false); - dataHash = "broken"; - } else if (diagramCache.getRoot().getBoundingBox().isEmpty()) { - diagramCache = univ.getDiagram(univ.loadSVG(EMPTY_SVG_DATA), false); - dataHash = "empty"; - } else { - for(SVGNodeAssignment ass : assignments) { - SVGElement e = diagramCache.getElement(ass.elementId); - if(e != null) { - if("$text".equals(ass.attributeNameOrId)) { - Tspan t = (Tspan)e; - t.setText(ass.value); - Text text = (Text)t.getParent(); - text.rebuild(); - } else { - e.setAttribute(ass.attributeNameOrId, AnimationElement.AT_AUTO, ass.value); - } - } - } - if(!assignments.isEmpty()) - diagramCache.updateTime(0); - } - } - documentCache = data; - if (diagramCache != null) { - setBounds((Rectangle2D) diagramCache.getRoot().getBoundingBox().clone()); - univ.incRefCount(diagramCache.getXMLBase()); - } else { - setBounds(new Rectangle2D.Double()); - } - } + Rectangle2D bbox = null; + synchronized (univ) { + // Relinquish reference to current element + if (diagramCache != null) { + univ.decRefCount(diagramCache.getXMLBase()); + diagramCache = null; + } + + + // Lets check for rootAssignment that contributes the whole SVG + SVGNodeAssignment rootAssignment = null; + if (!assignments.isEmpty()) { + for (SVGNodeAssignment ass : assignments) { + if (ass.attributeNameOrId.equals("$root")) { + rootAssignment = ass; + break; + } + } + } + byte[] dataBytes; + if (rootAssignment != null) { + dataBytes = rootAssignment.value.getBytes("UTF-8"); + } else { + // NOTE: hard-coded to assume all SVG data is encoded in UTF-8 + dataBytes = data.getBytes("UTF-8"); + } + dataHash = digest(dataBytes, assignments); + URI uri = univ.loadSVG(new ByteArrayInputStream(dataBytes), dataHash); + diagramCache = univ.getDiagram(uri, false); + + if (diagramCache != null) { + univ.incRefCount(diagramCache.getXMLBase()); + SVGRoot root = diagramCache.getRoot(); + if (root == null) { + univ.decRefCount(diagramCache.getXMLBase()); + diagramCache = univ.getDiagram(univ.loadSVG(BROKEN_SVG_DATA), false); + dataHash = "broken"; + univ.incRefCount(diagramCache.getXMLBase()); + bbox = (Rectangle2D) diagramCache.getRoot().getBoundingBox().clone(); + } else { + bbox = root.getBoundingBox(); + if (bbox.isEmpty()) { + // Lets check if this should be visible or not + Set presentationAttributes = root.getPresentationAttributes(); + if (!presentationAttributes.contains("display")) { + // TODO: fix this - How can one read values of attributes in SVG salamander??? + univ.decRefCount(diagramCache.getXMLBase()); + diagramCache = univ.getDiagram(univ.loadSVG(EMPTY_SVG_DATA), false); + dataHash = "empty"; + univ.incRefCount(diagramCache.getXMLBase()); + bbox = (Rectangle2D) root.getBoundingBox().clone(); + } else { + bbox = new Rectangle2D.Double(0, 0, 0, 0); + } + } else { + if (applyAssignments(diagramCache, assignments)) { + bbox = (Rectangle2D) root.getBoundingBox().clone(); + } else { + bbox = (Rectangle2D) bbox.clone(); + } + } + } + } else { + bbox = new Rectangle2D.Double(); + } + } + + documentCache = data; + setBounds(bbox); } catch (SVGException e) { - setBounds((Rectangle2D) diagramCache.getViewRect().clone()); + // This can only occur if diagramCache != null. + setBounds(diagramCache.getViewRect(new Rectangle2D.Double())); + univ.decRefCount(diagramCache.getXMLBase()); + diagramCache = null; } catch (IOException e) { diagramCache = null; } @@ -260,8 +246,41 @@ public class SVGNode extends G2DNode implements InitValueSupport, LoaderNode { return dataHash; } + private static boolean applyAssignments(SVGDiagram diagram, List assignments) throws SVGException { + if (assignments.isEmpty()) + return false; + boolean changed = false; + for (SVGNodeAssignment ass : assignments) { +// System.err.println("assign: " + ass.elementId + " " + ass.attributeNameOrId + " " + ass.value); +// if("opacity".equals(ass.attributeNameOrId)) +// System.err.println("faaf"); + SVGElement e = diagram.getElement(ass.elementId); + if (e != null) { + if ("$text".equals(ass.attributeNameOrId)) { + if (e instanceof Tspan) { + Tspan t = (Tspan) e; + if (ass.value.trim().isEmpty()) { + t.setText("-"); + } else { + t.setText(ass.value); + } + SVGElement parent = t.getParent(); + if (parent instanceof Text) + ((Text) parent).rebuild(); + changed = true; + } + } else { + e.setAttribute(ass.attributeNameOrId, AnimationElement.AT_AUTO, ass.value); + changed = true; + } + } + } + diagram.updateTime(0); + return changed; + } + public static Rectangle2D getBounds(String data) { - return getBounds(data, null); + return getBounds(data, null); } public static Rectangle2D getBounds(String data, List assignments) { @@ -301,7 +320,7 @@ public class SVGNode extends G2DNode implements InitValueSupport, LoaderNode { } return rect; } catch (SVGException e) { - return ((Rectangle2D) diagramCache.getViewRect().clone()); + return diagramCache.getViewRect(new Rectangle2D.Double()); } catch(IOException e) { } return null; @@ -330,13 +349,13 @@ public class SVGNode extends G2DNode implements InitValueSupport, LoaderNode { URI uri = univ.loadSVG(new ByteArrayInputStream(dataBytes), digest); diagramCache = univ.getDiagram(uri, false); if (diagramCache != null) { - SVGRoot root = diagramCache.getRoot(); + SVGRoot root = diagramCache.getRoot(); if (root == null) return new Rectangle2D.Double(); return (Rectangle2D)root.getBoundingBox().clone(); } } } catch (SVGException e) { - return ((Rectangle2D) diagramCache.getViewRect().clone()); + return diagramCache.getViewRect(new Rectangle2D.Double()); } catch(IOException e) { } return null; @@ -444,4 +463,11 @@ public class SVGNode extends G2DNode implements InitValueSupport, LoaderNode { this.setTransform(new AffineTransform(data)); } + public String getSVGText() { + String ret = data.replace("", "g>"); + //return diagramCache.toString(); + //return data.replace("", "/g>"); + return ret; + } + }