X-Git-Url: https://gerrit.simantics.org/r/gitweb?p=simantics%2Fplatform.git;a=blobdiff_plain;f=bundles%2Forg.simantics.modeling%2Fsrc%2Forg%2Fsimantics%2Fmodeling%2FSCLScenegraph.java;h=82115ef0e0825e2c9b8b2c69793f6bffd015344b;hp=1e80b832d70aa15fbc2752772792e8202175a08d;hb=f48fa9bd04b1802047c1eba99ad73eb4234a46c2;hpb=181b3b454b2b6f02491ab082e9f3b0cea1587857 diff --git a/bundles/org.simantics.modeling/src/org/simantics/modeling/SCLScenegraph.java b/bundles/org.simantics.modeling/src/org/simantics/modeling/SCLScenegraph.java index 1e80b832d..82115ef0e 100644 --- a/bundles/org.simantics.modeling/src/org/simantics/modeling/SCLScenegraph.java +++ b/bundles/org.simantics.modeling/src/org/simantics/modeling/SCLScenegraph.java @@ -1,6 +1,7 @@ package org.simantics.modeling; import java.awt.BasicStroke; +import java.awt.Color; import java.awt.Dimension; import java.awt.RenderingHints; import java.awt.RenderingHints.Key; @@ -45,10 +46,12 @@ import org.simantics.diagram.elements.DiagramNodeUtil; import org.simantics.diagram.elements.TextGridNode; import org.simantics.diagram.elements.TextNode; import org.simantics.diagram.stubs.DiagramResource; +import org.simantics.g2d.canvas.Hints; import org.simantics.g2d.canvas.ICanvasContext; import org.simantics.g2d.diagram.DiagramHints; import org.simantics.g2d.diagram.IDiagram; import org.simantics.g2d.diagram.handler.DataElementMap; +import org.simantics.g2d.diagram.participant.ElementPainter.SelectionShapeNode; import org.simantics.g2d.diagram.participant.Selection; import org.simantics.g2d.element.IElement; import org.simantics.g2d.scenegraph.ICanvasSceneGraphProvider; @@ -66,6 +69,7 @@ import org.simantics.scenegraph.g2d.nodes.BackgroundNode; import org.simantics.scenegraph.g2d.nodes.BoundsNode; import org.simantics.scenegraph.g2d.nodes.ConnectionNode; import org.simantics.scenegraph.g2d.nodes.DataNode; +import org.simantics.scenegraph.g2d.nodes.LinkNode; import org.simantics.scenegraph.g2d.nodes.NavigationNode; import org.simantics.scenegraph.g2d.nodes.SVGNode; import org.simantics.scenegraph.g2d.nodes.SelectionNode; @@ -82,6 +86,7 @@ import org.slf4j.LoggerFactory; import org.w3c.dom.DOMImplementation; import org.w3c.dom.Document; import org.w3c.dom.Element; +import org.w3c.dom.Node; import org.w3c.dom.NodeList; public class SCLScenegraph { @@ -91,9 +96,16 @@ public class SCLScenegraph { private static final String MAIN_SECTION = "main"; private static final String SELECTION_SECTION = "selection"; private static final String SELECTION_MASK_SECTION = "selectionMask"; - + private static final String[] ALL_SECTIONS = { MAIN_SECTION, SELECTION_SECTION, SELECTION_MASK_SECTION }; + // Changed from 0.001 to 0.0001 to prevent creation of huge BufferedImage's when + // generating PDF from SVG. If SVG contains any transparency then Batik uses + // bitmap-rendering which remarkably slows things down + // See org.apache.batik.gvt.AbstractGraphicsNode.paint(Graphics2D) where decisions are made + // if AlphaComposite should be painted + private static final String OPACITY = "0.0001"; + @Deprecated public static ICanvasSceneGraphProvider getICanvasSceneGraphProvider(Resource model, Resource diagram, String diagramRVI) throws DatabaseException, InterruptedException { ICanvasSceneGraphProvider provider = DiagramNodeUtil.loadSceneGraphProvider(model, diagram, diagramRVI); @@ -276,6 +288,23 @@ public class SCLScenegraph { } return "No nodes in scenegraph!"; } + + /** + * Returns background color of a canvasContext or null. + * @param ctx + * @return color in RGBA List format, or null. + */ + public static List getBackgroundColor(ICanvasContext ctx) { + Color color = ctx.getDefaultHintContext().getHint(Hints.KEY_BACKGROUND_COLOR); + if (color == null) + return null; + ArrayList rgba = new ArrayList<>(4); + rgba.add(color.getRed()); + rgba.add(color.getGreen()); + rgba.add(color.getBlue()); + rgba.add(color.getAlpha()); + return rgba; + } public static String sceneGraphTest (ICanvasContext ctx, String module, String value) { @@ -378,12 +407,14 @@ public class SCLScenegraph { public static final String svgNS = "http://www.w3.org/2000/svg"; - public Generator(SVGGeneratorContext ctx, boolean joku) { - super(ctx, joku); + public Generator(SVGGeneratorContext ctx, boolean textAsShapes) { + super(ctx, textAsShapes); } public Generator(Document document) { super(document); + // prevent batik comment in each g-element + getGeneratorContext().setComment(null); } @Override @@ -623,6 +654,7 @@ public class SCLScenegraph { RenderSVGContext result = new RenderSVGContext(); + double[] matrix = new double[6]; try { Selection selection = ctx.getAtMostOneItemOfClass(Selection.class); @@ -644,17 +676,28 @@ public class SCLScenegraph { // get the bounds of the content Rectangle2D content = rtreeBounds; - // To account for dynamic padding of selection rectangles (5 units + stroke width) - int offset = 6; - double scale = width < 0 || height < 0 ? 1.0 : Math.min((width - 2*offset) / content.getWidth(), (height - 2*offset) / content.getHeight()); - - svgGenerator.translate(offset, offset); - svgGenerator.scale(scale, scale); + if (content != null) { + // To account for dynamic padding of selection rectangles (5 units + stroke width) + int offset = 6; + + double scale = width < 0 || height < 0 ? 1.0 : Math.min((width - 2*offset) / content.getWidth(), (height - 2*offset) / content.getHeight()); + + AffineTransform tr = new AffineTransform(); + tr.translate(offset, offset); + tr.scale(scale, scale); + tr.translate(-content.getX(), -content.getY()); + tr.getMatrix(matrix); + svgGenerator.setSVGCanvasSize(new Dimension((int)Math.ceil(scale * content.getWidth()) + 2*offset, (int)Math.ceil(scale * content.getHeight()) + 2*offset)); + } else { + svgGenerator.setSVGCanvasSize(new Dimension(100, 100)); + } + //svgGenerator.translate(offset, offset); + //svgGenerator.scale(scale, scale); // translate svgGenerator to the x and y coordinates of current content - svgGenerator.translate(-content.getX(), -content.getY()); + //svgGenerator.translate(-content.getX(), -content.getY()); - svgGenerator.setSVGCanvasSize(new Dimension((int)Math.ceil(scale * content.getWidth()) + 2*offset, (int)Math.ceil(scale * content.getHeight()) + 2*offset)); + //svgGenerator.setClip(content); result.append(MAIN_SECTION, ""); @@ -681,9 +724,11 @@ public class SCLScenegraph { StringBuilder res = new StringBuilder(); res.append(""); + res.append(""); res.append(result.get(MAIN_SECTION)); res.append(result.get(SELECTION_SECTION)); res.append(result.get(SELECTION_MASK_SECTION)); + res.append(""); res.append(""); // System.err.println(" == FINAL RESULT == "); @@ -764,7 +809,7 @@ public class SCLScenegraph { String key = getKey((ConnectionNode) node); parentBuilder.append(MAIN_SECTION, "\n"); parentBuilder.append(SELECTION_SECTION, "\n"); - parentBuilder.append(SELECTION_MASK_SECTION, "\n"); + parentBuilder.append(SELECTION_MASK_SECTION, "\n"); Element doc = renderSVGNode(svgGenerator, (IG2DNode)node); String svg = printSVGDocument(doc); @@ -792,7 +837,7 @@ public class SCLScenegraph { parentBuilder.append(SELECTION_SECTION, "\n"); parentBuilder.append(MAIN_SECTION, "\n"); - } else if (node instanceof SelectionNode) { + } else if (isSelection0(node)) { SelectionNode n = (SelectionNode)node; SingleElementNode parentSEN = (SingleElementNode)NodeUtil.getNearestParentOfType(node, SingleElementNode.class); @@ -808,15 +853,11 @@ public class SCLScenegraph { parentBuilder2.append(SELECTION_SECTION, "\n"); parentBuilder2.append(SELECTION_SECTION, svg); parentBuilder2.append(SELECTION_SECTION, "\n"); - AffineTransform transform = svgGenerator.getTransform(); - double[] matrix = new double[6]; - transform.getMatrix(matrix); - String matrixString = String.format("matrix(%f,%f,%f,%f,%f,%f)", matrix[0], matrix[1], matrix[2], matrix[3], matrix[4], matrix[5]); - parentBuilder2.append(SELECTION_MASK_SECTION, "\n"); + parentBuilder2.append(SELECTION_MASK_SECTION, "\n"); Rectangle2D rect = n.getRect(); // NaN if(rect.getHeight() == rect.getHeight() && rect.getWidth() == rect.getWidth()) { - parentBuilder2.append(SELECTION_MASK_SECTION,""); @@ -824,9 +865,32 @@ public class SCLScenegraph { parentBuilder2.append(SELECTION_MASK_SECTION,"\n"); } + } else if (node instanceof SelectionNode) { + + Element doc = renderSVGNode(svgGenerator, (IG2DNode)node); + String svg = printSVGDocument(doc); + parentBuilder.append(MAIN_SECTION, ""); + parentBuilder.append(MAIN_SECTION, svg); + parentBuilder.append(MAIN_SECTION, "\n"); + } else if (node instanceof SVGNode) { SVGNode svg = (SVGNode)node; + AffineTransform at = svg.getTransform(); + if (!at.isIdentity()) { + if(at.getScaleX() == 1.0 && at.getScaleY() == 1.0 && at.getShearX() == 0.0 && at.getShearY() == 0.0) { + String m = "translate(" + at.getTranslateX() + " " + at.getTranslateY() + ")"; + parentBuilder.append(MAIN_SECTION, "\n"); + } else { + double[] ds = new double[6]; + at.getMatrix(ds); + String m = "matrix(" + ds[0] + " " + ds[1] + " " + ds[2] + " " + ds[3] + " " + ds[4] + " " + ds[5] + ")"; + parentBuilder.append(MAIN_SECTION, "\n"); + } + } parentBuilder.append(MAIN_SECTION, svg.getSVGText()); + if (!at.isIdentity()) { + parentBuilder.append(MAIN_SECTION, "\n"); + } } else if (node instanceof G2DParentNode) { AffineTransform at = node.getTransform(); if(node instanceof SingleElementNode) { @@ -851,12 +915,101 @@ public class SCLScenegraph { parentBuilder.append(ALL_SECTIONS, "\n"); } } + } else if (node instanceof TextNode) { + TextNode text = (TextNode)node; + + SingleElementNode parentSEN = (SingleElementNode)NodeUtil.getNearestParentOfType(node, SingleElementNode.class); + if(parentSEN != null) { + + text.setShowSelection(false); + Element doc = renderSVGNode(svgGenerator, (IG2DNode)node); + String svg = printSVGDocument(doc); + parentBuilder.append(MAIN_SECTION, svg); + + RenderSVGContext parentBuilder2 = getParentBuilder(parentSEN); + + String key = getKey(parentSEN); + text.setShowSelection(true); + doc = renderSVGNode(svgGenerator, (IG2DNode)node); + svg = printSVGDocument(doc); + + parentBuilder2.append(SELECTION_SECTION, "\n"); + parentBuilder2.append(SELECTION_SECTION, svg); + parentBuilder2.append(SELECTION_SECTION, "\n"); + parentBuilder2.append(SELECTION_MASK_SECTION, "\n"); + Rectangle2D rect = text.getBounds(); + // NaN + if(rect.getHeight() == rect.getHeight() && rect.getWidth() == rect.getWidth()) { + parentBuilder2.append(SELECTION_MASK_SECTION,""); + } + parentBuilder2.append(SELECTION_MASK_SECTION,"\n"); + + } + } else if (!(node instanceof RouteGraphNode) && !(node instanceof LinkNode)){ + try { + Element doc = renderSVGNode(svgGenerator, (IG2DNode)node); + NodeList gList = doc.getElementsByTagName("g"); + if (gList.getLength() == 0) + return; + boolean hasContent = false; + for (int i = 0; i < gList.getLength(); i++) { + Node gNode = gList.item(i); + if (gNode.hasChildNodes()) { + hasContent = true; + break; + } + } + if (!hasContent) + return; + String svg = printSVGDocument(doc); + if (node instanceof SelectionShapeNode) { + SingleElementNode parentSEN = (SingleElementNode)NodeUtil.getNearestParentOfType(node, SingleElementNode.class); + if(parentSEN != null) { + String key = getKey(parentSEN); + RenderSVGContext parentBuilder2 = getParentBuilder(parentSEN); + parentBuilder2.append(SELECTION_SECTION, "\n"); + parentBuilder2.append(SELECTION_SECTION, svg); + parentBuilder2.append(SELECTION_SECTION, "\n"); + + parentBuilder2.append(SELECTION_MASK_SECTION, "\n"); + Rectangle2D rect = node.getBounds(); + // NaN + if(rect.getHeight() == rect.getHeight() && rect.getWidth() == rect.getWidth()) { + parentBuilder2.append(SELECTION_MASK_SECTION,""); + } + parentBuilder2.append(SELECTION_MASK_SECTION,"\n"); + } + } else { + parentBuilder.append(MAIN_SECTION, ""); + parentBuilder.append(MAIN_SECTION, svg); + parentBuilder.append(MAIN_SECTION, "\n"); + } + } catch (Exception e) { + // TODO: There are nodes that do not behave well when rendered to SVG. For backwards compatibility, we don't handle the exceptions. + } } //enters.put(node, b.length()); } + private boolean isSelection0(IG2DNode node) { + + if(node instanceof SelectionNode) { + SelectionNode sn = (SelectionNode)node; + return sn.getSelectionId() == 0; + } else { + return false; + } + + } + private RenderSVGContext getParentBuilder(IG2DNode node) { INode parentSEN = NodeUtil.getNearestParentOfType(node, SingleElementNode.class);