protected static void transferChildren(SVGElement target, SVGElement source) { ModelNode child = ((ModelNode) source).getFirstChildNode(); while(child != null) { if (child instanceof PatchedElement) { PatchedElement pe = (PatchedElement) child; child = child.getNextSiblingNode(); setNullIds( (SVGElement) pe, true); source.removeChild((Node)pe); target.appendChild((Node)pe); setNullIds( (SVGElement) pe, false); } else { SceneManager.log(Level.SEVERE, "PatchedElement must be used instead of " + child.getClass().getName()); //NOI18N } } }
/** * Returns the content of the 'desc' child of the given element. */ public static String getDescription(SVGElement elt) { String result = ""; boolean preserve = false; Node n = elt.getFirstChild(); if (n != null && n.getNodeType() == Node.ELEMENT_NODE) { String name = (n.getPrefix() == null) ? n.getNodeName() : n.getLocalName(); if (name.equals(SVG_DESC_TAG)) { preserve = ((SVGLangSpace)n).getXMLspace().equals (SVG_PRESERVE_VALUE); for (n = n.getFirstChild(); n != null; n = n.getNextSibling()) { if (n.getNodeType() == Node.TEXT_NODE) { result += n.getNodeValue(); } } } } return (preserve) ? XMLSupport.preserveXMLSpace(result) : XMLSupport.defaultXMLSpace(result); }
private boolean orderFocusableElements(SVGElement elem, Set<String>ids, List<String>orderedIds) { String id = elem.getId(); if (id != null) { if ( ids.remove(id)) { orderedIds.add(id); if (ids.isEmpty()) { return true; } } } SVGElement child = (SVGElement) elem.getFirstElementChild(); while(child != null) { if (orderFocusableElements( child, ids, orderedIds)) { return true; } child = (SVGElement)child.getNextElementSibling(); } return false; }
public void saveSelection() { if (m_selectedId == null) { SVGObject [] selected = getSelected(); if ( selected != null && selected.length > 0 && selected[0] != null) { SVGElement elem = selected[0].getSVGElement(); m_selectedId = elem.getId(); m_selectedTag = elem.getLocalName(); } else { m_selectedId = null; m_selectedTag = null; } } }
private void collectFocusableElements(SVGElement elem, Set<String> elemIds) { if (elem instanceof PatchedAnimationElement ) { String beginTrait = elem.getTrait(SVGConstants.SVG_BEGIN_ATTRIBUTE); for (String pattern : ANIM_PATTERNS) { int p = 0; while ( (p=beginTrait.indexOf(pattern, p)) != -1) { int i = p - 1; while( i >= 0 && isElementIdChar(beginTrait.charAt(i))) { i--; } String id = beginTrait.substring(i+1, p); if ( getElementById(id) != null) { elemIds.add(id); } p += pattern.length(); } } } SVGElement child = (SVGElement) elem.getFirstElementChild(); while(child != null) { collectFocusableElements( child, elemIds); child = (SVGElement)child.getNextElementSibling(); } }
public static void setTransform(SVGElement e, float x, float y, float rotation) { SVGTransformList list = ((SVGTransformable) e).getTransform().getBaseVal(); list.clear(); if (isSet(rotation)) { SVGTransform tx = e.getOwnerSVGElement().createSVGTransform(); tx.setRotate(rotation, 0, 0); list.appendItem(tx); } if (isSet(x) || isSet(y)) { SVGTransform tx = e.getOwnerSVGElement().createSVGTransform(); tx.setTranslate(x, y); list.appendItem(tx); } }
/** * Adds an animation to the SVG element and starts the animation. * * @param animate * that animation element * @param element * the element to animate * @param duration * the duration of the animation in milliseconds * @param from * the starting point for the animation, can be null * @param to * the end point for the animation, cannot be null */ public static void animate(SVGOMAnimationElement animate, SVGElement element, int duration, String from, String to) { animate.setAttribute(SMILConstants.SMIL_DUR_ATTRIBUTE, duration + "ms"); if (from != null) { animate.setAttribute(SMILConstants.SMIL_FROM_ATTRIBUTE, from); } animate.setAttribute(SMILConstants.SMIL_TO_ATTRIBUTE, to); element.appendChild(animate); try { animate.beginElement(); } catch (NullPointerException e) { } }
public static boolean isViewBoxMarker( ModelNode node) { return node instanceof SVGElement && ID_VIEWBOX_MARKER.equals(((SVGElement) node).getId()); }
public void run() { Node parent = elem.getParentNode(); // HACK - clear all elements' ids so that the element removal is possible setNullIds(elem, true); try { parent.removeChild(elem); } finally { setNullIds(elem, false); } } });
private Map<String, String[]> rotateElements() { SceneManager m_sceneMgr = getSceneManager(); int angle = rotateRight ? 90 : -90; SVGSVGElement svgRoot = m_sceneMgr.getPerseusController().getSVGRootElement(); SVGRect viewBoxRect = svgRoot.getRectTrait(SVGConstants.SVG_VIEW_BOX_ATTRIBUTE); float translateH = viewBoxRect != null ? viewBoxRect.getHeight() : 0f; float translateW = viewBoxRect != null ? viewBoxRect.getWidth() : 0f; float[] translate = rotateRight ? new float[]{ translateH, 0f} : new float[]{ 0f, translateW}; SVGElement elem = (SVGElement) svgRoot.getFirstElementChild(); Rectangle bBox = new Rectangle(); Map<String, String[]> textChanges = new HashMap<String, String[]>(); while (elem != null) { // process rotateElement(elem, angle, translate, textChanges, bBox); // get next elem = (SVGElement) elem.getNextElementSibling(); } //getModel().setAttributes(textChanges); m_sceneMgr.getScreenManager().repaint(bBox, SVGObjectOutline.SELECTOR_OVERLAP); m_sceneMgr.getScreenManager().refresh(); return textChanges; }
/** * Get the relative coordinates of a point within the coordinate system of a * particular SVG Element. * * @param evt Event, needs to be a DOMMouseEvent * @param reference SVG Element the coordinate system is used of * @return Array containing the X and Y values */ public static double[] getRelativeCoordinates(Event evt, Element reference) { if(evt instanceof DOMMouseEvent && reference instanceof SVGLocatable && reference instanceof SVGElement) { // Get the screen (pixel!) coordinates DOMMouseEvent gnme = (DOMMouseEvent) evt; SVGMatrix mat = ((SVGLocatable) reference).getScreenCTM(); SVGMatrix imat = mat.inverse(); SVGPoint cPt = ((SVGElement) reference).getOwnerSVGElement().createSVGPoint(); cPt.setX(gnme.getClientX()); cPt.setY(gnme.getClientY()); // Have Batik transform the screen (pixel!) coordinates into SVG element // coordinates cPt = cPt.matrixTransform(imat); return new double[] { cPt.getX(), cPt.getY() }; } return null; }
/** {@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); } }
protected static SVGElement getElementById( ModelNode node, String id) { if (node instanceof SVGElement) { SVGElement svgElem = (SVGElement) node; if ( id.equals(svgElem.getId())) { return svgElem; } } ModelNode child = node.getFirstChildNode(); while( child != null) { SVGElement res = getElementById(child, id); if (res != null) { return res; } child = child.getNextSiblingNode(); } return null; }
/** * Get the relative coordinates of a point within the coordinate system of a * particular SVG Element. * * @param evt Event, needs to be a DOMMouseEvent * @param reference SVG Element the coordinate system is used of * @return Array containing the X and Y values */ public static double[] getRelativeCoordinates(Event evt, Element reference) { if(evt instanceof DOMMouseEvent && reference instanceof SVGLocatable && reference instanceof SVGElement) { // Get the screen (pixel!) coordinates DOMMouseEvent gnme = (DOMMouseEvent) evt; SVGMatrix mat = ((SVGLocatable) reference).getScreenCTM(); SVGMatrix imat = mat.inverse(); SVGPoint cPt = ((SVGElement) reference).getOwnerSVGElement().createSVGPoint(); cPt.setX(gnme.getClientX()); cPt.setY(gnme.getClientY()); // Have Batik transform the screen (pixel!) coordinates into SVG element // coordinates cPt = cPt.matrixTransform(imat); return new double[] { cPt.getX(), cPt.getY() }; } return null; }
public static void setNullIds(SVGElement elem, boolean isNull) { if (elem instanceof PatchedElement) { ((PatchedElement) elem).setNullId(isNull); } else if ( elem.getId() != null) { SceneManager.log(Level.SEVERE, "PatchedElement must be used instead of " + elem.getClass().getName()); //NOI18N } SVGElement child = (SVGElement) elem.getFirstElementChild(); while(child != null) { setNullIds( child, isNull); child = (SVGElement)child.getNextElementSibling(); } }
/** * Returns the content of the 'desc' child of the given element. */ public static String getDescription(SVGElement elt) { String result = ""; boolean preserve = false; Node n = elt.getFirstChild(); if (n != null && n.getNodeType() == Node.ELEMENT_NODE) { String name = (n.getPrefix() == null) ? n.getNodeName() : n.getLocalName(); if (name.equals(SVG_DESC_TAG)) { preserve = ((SVGLangSpace)n).getXMLspace().equals (SVG_PRESERVE_VALUE); for (n = n.getFirstChild(); n != null; n = n.getNextSibling()) { if (n.getNodeType() == Node.TEXT_NODE) { result += n.getNodeValue(); } } } } return (preserve) ? XMLSupport.preserveXMLSpace(result) : XMLSupport.defaultXMLSpace(result); }
public static SVGElement hideAllButSubtree( ModelNode node, String id) { if ( node instanceof SVGElement) { SVGElement elem = (SVGElement) node; if ( id.equals(elem.getId())) { return elem; } } ModelNode child = node.getFirstChildNode(); SVGElement visibleChild = null; while( child != null) { SVGElement e; if ( (e=hideAllButSubtree(child, id)) != null) { visibleChild = e; } child = child.getNextSiblingNode(); } if ( node instanceof CompositeGraphicsNode) { CompositeGraphicsNode elem = (CompositeGraphicsNode) node; if (visibleChild == null) { elem.setVisibility(false); elem.setTrait(SVGConstants.SVG_VISIBILITY_ATTRIBUTE, "hidden"); //NOI18N } } return visibleChild; }
/** * Returns the content of the 'desc' child of the given element. */ public static String getDescription(SVGElement elt) { String result = ""; boolean preserve = false; Node n = elt.getFirstChild(); if (n != null && n.getNodeType() == Node.ELEMENT_NODE) { String name = (n.getPrefix() == null) ? n.getNodeName() : n.getLocalName(); if (name.equals(SVG_DESC_TAG)) { preserve = ((SVGLangSpace)n).getXMLspace().equals (SVG_PRESERVE_VALUE); for (n = n.getFirstChild(); n != null; n = n.getNextSibling()) { if (n.getNodeType() == Node.TEXT_NODE) { result += n.getNodeValue(); } } } } return (preserve) ? XMLSupport.preserveXMLSpace(result) : XMLSupport.defaultXMLSpace(result); }