public void documentLoadingCompleted(SVGDocumentLoaderEvent e) { NodeEventTarget root = (NodeEventTarget) e.getSVGDocument().getRootElement(); root.addEventListenerNS (XMLConstants.XML_EVENTS_NAMESPACE_URI, SVGConstants.SVG_SVGZOOM_EVENT_TYPE, new EventListener() { public void handleEvent(Event evt) { if (!(evt.getTarget() instanceof SVGSVGElement)) return; // assert(evt.getType() == // SVGConstants.SVG_SVGZOOM_EVENT_TYPE); SVGSVGElement svg = (SVGSVGElement) evt.getTarget(); scaleChange(svg.getCurrentScale()); } // handleEvent() }, false, null); }
protected Rectangle2D getViewBoxRect() { SVGDocument doc = canvas.getSVGDocument(); if (doc == null) return null; SVGSVGElement el = doc.getRootElement(); if (el == null) return null; String viewBoxStr = el.getAttributeNS (null, SVGConstants.SVG_VIEW_BOX_ATTRIBUTE); if (viewBoxStr.length() != 0) { float[] rect = ViewBox.parseViewBoxAttribute(el, viewBoxStr, null); return new Rectangle2D.Float(rect[0], rect[1], rect[2], rect[3]); } GraphicsNode gn = canvas.getGraphicsNode(); if (gn == null) return null; Rectangle2D bounds = gn.getBounds(); if (bounds == null) return null; return (Rectangle2D) bounds.clone(); }
public void setGraphicsNode(GraphicsNode gn, boolean createDispatcher) { Dimension2D dim = bridgeContext.getDocumentSize(); Dimension mySz = new Dimension((int)dim.getWidth(), (int)dim.getHeight()); JSVGComponent.this.setMySize(mySz); SVGSVGElement elt = svgDocument.getRootElement(); prevComponentSize = getSize(); AffineTransform at = calculateViewingTransform (fragmentIdentifier, elt); CanvasGraphicsNode cgn = getCanvasGraphicsNode(gn); if (cgn != null) { cgn.setViewingTransform(at); } viewingTransform = null; initialTransform = new AffineTransform(); setRenderingTransform(initialTransform, false); jsvgComponentListener.updateMatrix(initialTransform); addJGVTComponentListener(jsvgComponentListener); addComponentListener(jsvgComponentListener); super.setGraphicsNode(gn, createDispatcher); }
private void layoutSVGDocument(Rectangle bounds) { animateBounds = createAnimationElement(this, SVG_ANIMATE_TAG, SVG_VIEW_BOX_ATTRIBUTE, null); updateManager = null; datalinkMap.clear(); Graph graph = getGraph(); if (graph instanceof SVGGraph) { SVGGraph svgGraph = (SVGGraph) graph; SVGSVGElement svgElement = getSVGDocument().getRootElement(); SVGElement graphElement = svgGraph.getSVGElement(); svgElement.appendChild(graphElement); setBounds(layoutGraph(graph, bounds)); edgeLine = EdgeLine.createAndAdd(getSVGDocument(), this); } drawingDiagram = true; }
public void setBounds(final Rectangle bounds) { oldBounds = this.bounds; this.bounds = bounds; updateSVGDocument(new Runnable() { @Override public void run() { SVGSVGElement svgElement = getSVGDocument().getRootElement(); if (isAnimatable() && oldBounds != null) { String from = "0 0 " + oldBounds.width + " " + oldBounds.height; String to = "0 0 " + bounds.width + " " + bounds.height; animate(animateBounds, svgElement, getAnimationSpeed(), from, to); } else if ((svgElement != null) && (bounds != null)) svgElement.setAttribute(SVG_VIEW_BOX_ATTRIBUTE, "0 0 " + String.valueOf(bounds.width) + " " + String.valueOf(bounds.height)); } }); }
private void outputMessage(final String message) { SVGSVGElement svgElement = getSVGDocument().getRootElement(); String[] parts = message.split("\n"); int initialPosition = 200; for (int i = 0; i < parts.length; i++) { Text errorsText = createText(parts[i]); SVGOMTextElement error = (SVGOMTextElement) createElement(SVG_TEXT_TAG); error.setAttribute(SVG_Y_ATTRIBUTE, Integer.toString(initialPosition + i * 60)); error.setAttribute(SVG_FONT_SIZE_ATTRIBUTE, "20"); error.setAttribute(SVG_FONT_FAMILY_ATTRIBUTE, "sans-serif"); error.setAttribute(SVG_FILL_ATTRIBUTE, "red"); error.appendChild(errorsText); svgElement.appendChild(error); } bounds = new Rectangle(300, parts.length * 60 + 200); svgCanvas.setDocument(getSVGDocument()); }
public void postRenderAction(SVGSVGElement root) { if (wcFont.isWebSafe()) return; Document document = root.getOwnerDocument(); Element defs = root.getElementById(SVGSyntax.ID_PREFIX_GENERIC_DEFS); Element style = document.createElementNS(SVGSyntax.SVG_NAMESPACE_URI, SVGSyntax.SVG_STYLE_TAG); style.setAttributeNS(null, SVGSyntax.SVG_TYPE_ATTRIBUTE, "text/css"); CDATASection styleSheet = document.createCDATASection(""); Font font = FontUtils.getFont(); styleSheet.setData("@font-face { font-family: '" + font.getFamily() + "'; src: url('static/fonts/" + wcFont.getName() + ".ttf');}"); style.appendChild(styleSheet); defs.appendChild(style); }
/** * Returns the default Interpreter for this document. */ public Interpreter getInterpreter() { if (interpreter != null) return interpreter; SVGSVGElement root = (SVGSVGElement)document.getDocumentElement(); String lang = root.getContentScriptType(); return getInterpreter(lang); }
/** * Recursively dispatch the SVG 'onload' event. */ public void dispatchSVGLoadEvent() { SVGSVGElement root = (SVGSVGElement)document.getDocumentElement(); String lang = root.getContentScriptType(); long documentStartTime = System.currentTimeMillis(); bridgeContext.getAnimationEngine().start(documentStartTime); dispatchSVGLoad(root, true, lang); }
protected void dispatchSVGDocEvent(String eventType) { SVGSVGElement root = (SVGSVGElement)document.getDocumentElement(); // Event is dispatched on outermost SVG element. EventTarget t = root; DocumentEvent de = (DocumentEvent)document; AbstractEvent ev = (AbstractEvent) de.createEvent("SVGEvents"); ev.initEventNS(XMLConstants.XML_EVENTS_NAMESPACE_URI, eventType, false, false); t.dispatchEvent(ev); }
protected AffineTransform calculateViewingTransform (String fragIdent, SVGSVGElement svgElt) { Dimension d = getSize(); if (d.width < 1) d.width = 1; if (d.height < 1) d.height = 1; return ViewBox.getViewTransform (fragIdent, svgElt, d.width, d.height, bridgeContext); }
/** * <b>DOM</b>: Implements {@link SVGElement#getOwnerSVGElement()}. */ public SVGSVGElement getOwnerSVGElement() { for (Element e = CSSEngine.getParentCSSStylableElement(this); e != null; e = CSSEngine.getParentCSSStylableElement(e)) { if (e instanceof SVGSVGElement) { return (SVGSVGElement)e; } } return null; }
public boolean runImplBasic() throws Exception { Document doc = newSVGDoc(); Element e2 = doc.createElementNS(SVG_NAMESPACE_URI, "g"); boolean pass = e2 instanceof SVGGElement; e2 = (Element) ((AbstractDocument) doc).renameNode(e2, SVG_NAMESPACE_URI, "svg"); pass = pass && e2 instanceof SVGSVGElement; Attr a = doc.createAttributeNS(null, "test"); a = (Attr) ((AbstractDocument) doc).renameNode(a, EX_NAMESPACE_URI, "test2"); pass = pass && a.getNamespaceURI().equals(EX_NAMESPACE_URI) && a.getLocalName().equals("test2"); return pass; }
protected float[] calculateDefaultViewbox(String fragIdent, SVGSVGElement svgElt) { float[] vb = new float[4]; SVGRect rc = svgElt.getBBox(); vb[0] = rc.getX(); vb[1] = rc.getY(); vb[2] = rc.getWidth(); vb[3] = rc.getHeight(); return vb; }
public static byte[] createSVG(WordCloudRenderer renderer, SVGTextStyleHandler styleHandler) { // Create an instance of org.w3c.dom.Document. DOMImplementation domImpl = SVGDOMImplementation.getDOMImplementation(); Document document = domImpl.createDocument(SVGDOMImplementation.SVG_NAMESPACE_URI, "svg", null); // Configure the SVGGraphics2D SVGGeneratorContext ctx = SVGGeneratorContext.createDefault(document); ctx.setStyleHandler(styleHandler); SVGGraphics2D svgGenerator = new SVGGraphics2D(ctx, false); // rendering the cloud renderer.render(svgGenerator); SVGSVGElement root = (SVGSVGElement)svgGenerator.getRoot(); styleHandler.postRenderAction(root); try { Writer writer = new StringWriter(); svgGenerator.stream(root, writer); writer.close(); return writer.toString().getBytes("UTF-8"); } catch (Exception e) { throw new RuntimeException(e); } }
/** {@inheritDoc} */ public void handleXML(RendererContext context, org.w3c.dom.Document doc, String ns) throws Exception { if (getNamespace().equals(ns)) { if (!(doc instanceof SVGDocument)) { DOMImplementation impl = SVGDOMImplementation.getDOMImplementation(); doc = DOMUtilities.deepCloneDocument(doc, impl); } SVGSVGElement svg = ((SVGDocument) doc).getRootElement(); SVGDocument targetDoc = (SVGDocument)context.getProperty(SVG_DOCUMENT); SVGElement currentPageG = (SVGElement)context.getProperty(SVG_PAGE_G); Element view = targetDoc.createElementNS(getNamespace(), "svg"); Node newsvg = targetDoc.importNode(svg, true); //view.setAttributeNS(null, "viewBox", "0 0 "); int xpos = ((Integer)context.getProperty(XPOS)).intValue(); int ypos = ((Integer)context.getProperty(YPOS)).intValue(); view.setAttributeNS(null, "x", "" + xpos / 1000f); view.setAttributeNS(null, "y", "" + ypos / 1000f); // this fixes a problem where the xmlns is repeated sometimes Element ele = (Element) newsvg; ele.setAttributeNS(XMLSupport.XMLNS_NAMESPACE_URI, "xmlns", getNamespace()); if (ele.hasAttributeNS(null, "xmlns")) { ele.removeAttributeNS(null, "xmlns"); } view.appendChild(newsvg); currentPageG.appendChild(view); } }
/** * Extract the viewbox of the input SVG * @return * @throws MalformedURLException * @throws IOException */ private Rectangle extractSVGBounds(QualifiedSVGResource svg) throws IOException { // check <svg> attributes first : x, y, width, height SVGDocument svgDocument = getSVGDocument(svg); SVGSVGElement svgElement = svgDocument.getRootElement(); if (svgElement.getAttributeNode("width") != null && svgElement.getAttribute("height") != null) { UserAgent userAgent = new DensityAwareUserAgent(svg.getDensity().getDpi()); UnitProcessor.Context context = org.apache.batik.bridge.UnitProcessor.createContext( new BridgeContext(userAgent), svgElement); float width = svgLengthInPixels(svgElement.getWidth().getBaseVal(), context); float height = svgLengthInPixels(svgElement.getHeight().getBaseVal(), context); float x = 0; float y = 0; // check x and y attributes if (svgElement.getX() != null && svgElement.getX().getBaseVal() != null) { x = svgLengthInPixels(svgElement.getX().getBaseVal(), context); } if (svgElement.getY() != null && svgElement.getY().getBaseVal() != null) { y = svgLengthInPixels(svgElement.getY().getBaseVal(), context); } return new Rectangle((int) floor(x), (int) floor(y), (int) ceil(width), (int) ceil(height)); } // use computed bounds log.warn("Take time to fix desired width and height attributes of the root <svg> node for this file... " + "ROI will be computed by magic using Batik " + boundsType.name() + " bounds"); return boundsType.getBounds(getGraphicsNode(svgDocument, svg.getDensity().getDpi())); }
protected void rebuildImageNode() { // Reference copy of the imgDocument if ((imgDocument != null) && (listener != null)) { NodeEventTarget tgt = (NodeEventTarget)imgDocument.getRootElement(); tgt.removeEventListenerNS (XMLConstants.XML_EVENTS_NAMESPACE_URI, SVG_EVENT_CLICK, listener, false); tgt.removeEventListenerNS (XMLConstants.XML_EVENTS_NAMESPACE_URI, SVG_EVENT_KEYDOWN, listener, false); tgt.removeEventListenerNS (XMLConstants.XML_EVENTS_NAMESPACE_URI, SVG_EVENT_KEYPRESS, listener, false); tgt.removeEventListenerNS (XMLConstants.XML_EVENTS_NAMESPACE_URI, SVG_EVENT_KEYUP, listener, false); tgt.removeEventListenerNS (XMLConstants.XML_EVENTS_NAMESPACE_URI, SVG_EVENT_MOUSEDOWN, listener, false); tgt.removeEventListenerNS (XMLConstants.XML_EVENTS_NAMESPACE_URI, SVG_EVENT_MOUSEMOVE, listener, false); tgt.removeEventListenerNS (XMLConstants.XML_EVENTS_NAMESPACE_URI, SVG_EVENT_MOUSEOUT, listener, false); tgt.removeEventListenerNS (XMLConstants.XML_EVENTS_NAMESPACE_URI, SVG_EVENT_MOUSEOVER, listener, false); tgt.removeEventListenerNS (XMLConstants.XML_EVENTS_NAMESPACE_URI, SVG_EVENT_MOUSEUP, listener, false); listener = null; } if (imgDocument != null) { SVGSVGElement svgElement = imgDocument.getRootElement(); disposeTree(svgElement); } imgDocument = null; subCtx = null; //update of the reference of the image. GraphicsNode inode = buildImageGraphicsNode(ctx,e); ImageNode imgNode = (ImageNode)node; imgNode.setImage(inode); if (inode == null) { SVGImageElement ie = (SVGImageElement) e; String uriStr = ie.getHref().getAnimVal(); throw new BridgeException(ctx, e, ERR_URI_IMAGE_INVALID, new Object[] {uriStr}); } }
public void dispose() { if ((imgDocument != null) && (listener != null)) { NodeEventTarget tgt = (NodeEventTarget)imgDocument.getRootElement(); tgt.removeEventListenerNS (XMLConstants.XML_EVENTS_NAMESPACE_URI, SVG_EVENT_CLICK, listener, false); tgt.removeEventListenerNS (XMLConstants.XML_EVENTS_NAMESPACE_URI, SVG_EVENT_KEYDOWN, listener, false); tgt.removeEventListenerNS (XMLConstants.XML_EVENTS_NAMESPACE_URI, SVG_EVENT_KEYPRESS, listener, false); tgt.removeEventListenerNS (XMLConstants.XML_EVENTS_NAMESPACE_URI, SVG_EVENT_KEYUP, listener, false); tgt.removeEventListenerNS (XMLConstants.XML_EVENTS_NAMESPACE_URI, SVG_EVENT_MOUSEDOWN, listener, false); tgt.removeEventListenerNS (XMLConstants.XML_EVENTS_NAMESPACE_URI, SVG_EVENT_MOUSEMOVE, listener, false); tgt.removeEventListenerNS (XMLConstants.XML_EVENTS_NAMESPACE_URI, SVG_EVENT_MOUSEOUT, listener, false); tgt.removeEventListenerNS (XMLConstants.XML_EVENTS_NAMESPACE_URI, SVG_EVENT_MOUSEOVER, listener, false); tgt.removeEventListenerNS (XMLConstants.XML_EVENTS_NAMESPACE_URI, SVG_EVENT_MOUSEUP, listener, false); listener = null; } if (imgDocument != null) { SVGSVGElement svgElement = imgDocument.getRootElement(); disposeTree(svgElement); imgDocument = null; subCtx = null; } super.dispose(); }
/** * Updates the thumbnail component rendering transform. */ protected void updateThumbnailRenderingTransform() { SVGDocument svgDocument = svgCanvas.getSVGDocument(); if (svgDocument != null) { SVGSVGElement elt = svgDocument.getRootElement(); Dimension dim = svgThumbnailCanvas.getSize(); // XXX Update this to use the animated values of 'viewBox' // and 'preserveAspectRatio'. String viewBox = elt.getAttributeNS (null, SVGConstants.SVG_VIEW_BOX_ATTRIBUTE); AffineTransform Tx; if (viewBox.length() != 0) { String aspectRatio = elt.getAttributeNS (null, SVGConstants.SVG_PRESERVE_ASPECT_RATIO_ATTRIBUTE); Tx = ViewBox.getPreserveAspectRatioTransform (elt, viewBox, aspectRatio, dim.width, dim.height, null); } else { // no viewBox has been specified, create a scale transform Dimension2D docSize = svgCanvas.getSVGDocumentSize(); double sx = dim.width / docSize.getWidth(); double sy = dim.height / docSize.getHeight(); double s = Math.min(sx, sy); Tx = AffineTransform.getScaleInstance(s, s); } GraphicsNode gn = svgCanvas.getGraphicsNode(); CanvasGraphicsNode cgn = getCanvasGraphicsNode(gn); if (cgn != null) { AffineTransform vTx = cgn.getViewingTransform(); if ((vTx != null) && !vTx.isIdentity()) { try { AffineTransform invVTx = vTx.createInverse(); Tx.concatenate(invVTx); } catch (NoninvertibleTransformException nite) { /* nothing */ } } } svgThumbnailCanvas.setRenderingTransform(Tx); overlay.synchronizeAreaOfInterest(); } }
/** * <b>DOM</b>: Implements {@link SVGDocument#getRootElement()}. */ public SVGSVGElement getRootElement() { return (SVGSVGElement)getDocumentElement(); }
/** * overwrites calculateViewingTransform to allow autoFitToCanvas behaviour, if autoFitToCanvas is not set it will use the base's class * method. * * Right now fragIdent != null IS NOT SUPPORTED !!! */ @Override protected AffineTransform calculateViewingTransform(String fragIdent, SVGSVGElement svgElt) { assert fragIdent == null; // don't understand this parameter, have not found a simple test case yet if (!autoFitToCanvas || fragIdent != null) return super.calculateViewingTransform(fragIdent, svgElt); // canvas size / additional border Dimension d = getSize(); int xb = 0, yb = 0; if (d.width < 1) d.width = 1; if (d.height < 1) d.height = 1; if (d.width > 4 * xBorder) // if canvas is large enough add border d.width -= 2 * (xb = xBorder); if (d.height > 4 * yBorder) // if canvas is large enough add border d.height -= 2 * (yb = yBorder); // AffineTransform tf; // String viewBox = svgElt.getAttributeNS(null, ViewBox.SVG_VIEW_BOX_ATTRIBUTE); if (viewBox.length() == 0) { // no viewbox specified, make an own one float[] vb = calculateDefaultViewbox(fragIdent, svgElt); tf = ViewBox.getPreserveAspectRatioTransform(vb, SVGPreserveAspectRatio.SVG_PRESERVEASPECTRATIO_XMIDYMID, true, d.width, d.height); } else { String aspectRatio = svgElt.getAttributeNS(null, ViewBox.SVG_PRESERVE_ASPECT_RATIO_ATTRIBUTE); if (aspectRatio.length() > 0) tf = ViewBox.getPreserveAspectRatioTransform(svgElt, viewBox, aspectRatio, d.width, d.height); else { float[] vb = ViewBox.parseViewBoxAttribute(svgElt, viewBox); tf = ViewBox.getPreserveAspectRatioTransform(vb, SVGPreserveAspectRatio.SVG_PRESERVEASPECTRATIO_XMIDYMAX, true, d.width, d.height); } } if (xb > 0 || yb > 0) { // center image AffineTransform tf2 = AffineTransform.getTranslateInstance(xb, yb); tf2.concatenate(tf); tf = tf2; } return tf; }