Java org.w3c.dom.svg.SVGDocument 代码实例

・20 分钟阅读

以下是展示如何使用org.w3c.dom.svg.SVGDocument的最佳示例。 我们使用了代码质量辨别算法从开源项目中提取出了最佳的优秀示例。

实例 1


@Override
public Element getSVGString(SVGDocument doc) {
    Element group = null;
    group = doc.createElementNS("http://www.w3.org/2000/svg", "g");
    group.setAttributeNS(null, "id", getLayerId());
    if (isAddOpacity()) {
        group.setAttributeNS(null, "style", "opacity:" + getOpacity());
    }
    group.appendChild(getBoxDefinition(doc));
    group.appendChild(getImageDefinition(doc));
    group.appendChild(getStartImageText(doc));
    // Process Sub Activities
    group.appendChild(getSubActivitiesSVGString(doc));
    group.appendChild(getEndImageDefinition(doc));
    //Add Arrow
    group.appendChild(getArrows(doc));
    return group;
}
 

实例 2


protected Element getArrows(SVGDocument doc) {
    Element subGroup = null;
    subGroup = doc.createElementNS("http://www.w3.org/2000/svg", "g");
    if (subActivities != null) {
        ActivityInterface activity = null;
        String id = null;
        SVGCoordinates myStartCoords = getStartIconExitArrowCoords();
        SVGCoordinates myExitCoords = getEndIconEntryArrowCoords();
        SVGCoordinates activityExitCoords = null;
        SVGCoordinates activityEntryCoords = null;
        Iterator<ActivityInterface> itr = subActivities.iterator();
        while (itr.hasNext()) {
            activity = itr.next();
            activityExitCoords = activity.getExitArrowCoords();
            activityEntryCoords = activity.getEntryArrowCoords();
            subGroup.appendChild(getArrowDefinition(doc, myStartCoords.getXLeft(), myStartCoords.getYTop(), activityEntryCoords.getXLeft(), activityEntryCoords.getYTop(), id));
            subGroup.appendChild(getArrowDefinition(doc, activityExitCoords.getXLeft(), activityExitCoords.getYTop(), myExitCoords.getXLeft(), myExitCoords.getYTop(), id));
        }
    }
    return subGroup;
}
 

实例 3


public Element getSVGString(SVGDocument doc) {
    Element group1 = null;
    group1 = doc.createElementNS("http://www.w3.org/2000/svg", "g");
    group1.setAttributeNS(null, "id", getLayerId());
    if (isAddOpacity()) {
        group1.setAttributeNS(null, "style", "opacity:" + getOpacity());
    }
    group1.appendChild(getBoxDefinition(doc));
    group1.appendChild(getImageDefinition(doc));
    group1.appendChild(getStartImageText(doc));
    // Get Sub Activities
    group1.appendChild(getSubActivitiesSVGString(doc));
    group1.appendChild(getEndImageDefinition(doc));
    // Add Arrow
    group1.appendChild(getArrows(doc));
    return group1;
}
 

实例 4


protected Element getArrowDefinition(SVGDocument doc, int startX, int startY, int endX, int endY, String id) {         //here we have to find whether
    Element path = doc.createElementNS("http://www.w3.org/2000/svg", "path");
    if (startX == endX || startY == endY) {
        path.setAttributeNS(null, "d", "M " + startX + "," + startY + " L " + endX + "," + endY);
    } else {
        if (layoutManager.isVerticalLayout()) {
            int middleX, middleY;
            if ((startX < endX)) {
                middleY = startY;
                middleX = endX;
            } else {
                middleY = endY;
                middleX = startX;
            }
            path.setAttributeNS(null, "d", "M " + startX + "," + startY + " L " + middleX + "," + middleY + " L " + endX +
                    "," + endY);
        } else {
            path.setAttributeNS(null, "d", "M " + startX + "," + startY + " L " + ((startX + 1 * endX) / 2) +
                    "," + startY + " L " + ((startX + 1 * endX) / 2) + "," + endY + " L " + endX + "," + endY);                              //use constants for these propotions
        }
    }
    path.setAttributeNS(null, "id", id);
    path.setAttributeNS(null, "style", getArrowStyle());
    return path;
}
 

实例 5


@Override
public Element getSVGString(SVGDocument document) {
    doc = (SVGDocument) dom.createDocument(svgNS, "svg", null);
    root = doc.getDocumentElement();
    root.setAttributeNS(null, "height", "1000");
    root.appendChild(getDefs(doc));
    root.appendChild(getBoxDefinition(doc));
    root.appendChild(getImageDefinition(doc));
    // Process Sub Activities
    root.appendChild(getSubActivitiesSVGString(doc));
    root.appendChild(getEndImageDefinition(doc));
    // Add Arrow
    root.appendChild(getArrows(doc));
    return root;
}
 

实例 6


/**
 * We add an "image" background to the SVG so that the user can edit the SVG
 * externally.
 * 
 * When importing back in we will remove the background image.
 * @param doc The DOM document to add the background image to.
 * @return The updated DOM document.
 */
protected SVGDocument addImgBackground(SVGDocument doc) {
    try {
        if (doc == null) {
            return doc;
        }
        File imgFile = imgSrc.createImageFile();
        if (imgFile == null) return doc;
        Dimension imgDim = imgSrc.getDimension();
        Element img = doc.createElement("image");
        img.setAttribute("xlink:href", imgFile.getCanonicalPath());
        img.setAttribute("width", Integer.toString(imgDim.width));
        img.setAttribute("height", Integer.toString(imgDim.height));
        img.setAttribute("x", Integer.toString(0));
        img.setAttribute("y", Integer.toString(0));
        img.setAttribute("id", MusicPageSVG.SVG_BACKGROUND_ID);
        Element root = doc.getDocumentElement(); // <svg>
        if (root != null) {
            Node firstChild = root.getFirstChild();
            if (firstChild != null) {
                root.insertBefore(img, firstChild);
            } else {
                root.appendChild(img);
            }
        } else {
            // We should never be in this situation.
            root = doc.createElement("svg");
            root.appendChild(img);
            doc.appendChild(root);
        }
        // If we created a new document, it won't have width/height
        if (!root.hasAttribute("width")) {
            root.setAttribute("width", Integer.toString(imgDim.width));
        }
        if (!root.hasAttribute("height")) {
            root.setAttribute("height", Integer.toString(imgDim.height));
        }
        return doc;
    } catch (IOException ex) {
        Exceptions.printStackTrace(ex);
    }
    return doc;
}
 

实例 7


/**
 * Creates a splash image with the message "No item selected"
 * 
 * @return
 */
protected SVGDocument createTestImage() {
        DOMImplementation impl = SVGDOMImplementation.getDOMImplementation();
        String svgNS = SVGDOMImplementation.SVG_NAMESPACE_URI;
        SVGDocument doc = (SVGDocument) impl.createDocument(svgNS, "svg", null);
        Element root = doc.getDocumentElement();
        root.setAttributeNS(null, "width", "500px");
        root.setAttributeNS(null, "height", "500px");
        Element text = doc.createElementNS(SVGDOMImplementation.SVG_NAMESPACE_URI, "text");
        text.setAttributeNS(null, "x", "10");
        text.setAttributeNS(null, "y", "20");
        text.setAttributeNS(null, "font-size", DEFAULT_FONT_SIZE);
        text.setAttributeNS(null, "font-family", DEFAULT_FONT_FAMILY);
        text.setAttributeNS(null, "font-weight", "bold");
        text.setAttributeNS(null, "fill", "red");
        text.setAttributeNS(null, "stroke", "none");
        text.setTextContent("No item selected");
        root.appendChild(text);
        return doc;
}
 

实例 8


@Override
public Element getSVGString(SVGDocument document) {
    LayoutManager layoutManager = org.wso2.carbon.bpel.ui.bpel2svg.BPEL2SVGFactory.getInstance().getLayoutManager();
    // Build Specific Code
    //attention---------    getSVGFileHeader();
    doc = (SVGDocument) dom.createDocument(svgNS, "svg", null);
    root = doc.getDocumentElement();
    root.setAttributeNS(null, "height", "1000");
    root.appendChild(getDefs(doc));
    root.appendChild(getBoxDefinition(doc));
    root.appendChild(getImageDefinition(doc));
    // Process Sub Activities
    root.appendChild(getSubActivitiesSVGString(doc));
    root.appendChild(getEndImageDefinition(doc));
    // Add Arrow
    root.appendChild(getArrows(doc));
    // Add End Tag
    // getSCGFileFooter(); //this isn't used
    return root;
}
 

实例 9


/**
 * Create a new plotting document.
 */
public SVGPlot() {
  super();
  // Get a DOMImplementation.
  DOMImplementation domImpl = getDomImpl();
  DocumentType dt = domImpl.createDocumentType(SVGConstants.SVG_SVG_TAG, SVGConstants.SVG_PUBLIC_ID, SVGConstants.SVG_SYSTEM_ID);
  // Workaround: sometimes DocumentType doesn't work right, which
  // causes problems with
  // serialization...
  if(dt.getName() == null) {
    dt = null;
  }
  document = (SVGDocument) domImpl.createDocument(SVGConstants.SVG_NAMESPACE_URI, SVGConstants.SVG_SVG_TAG, dt);
  root = document.getDocumentElement();
  // setup common SVG namespaces
  root.setAttribute(SVGConstants.XMLNS_PREFIX, SVGConstants.SVG_NAMESPACE_URI);
  root.setAttributeNS(SVGConstants.XMLNS_NAMESPACE_URI, SVGConstants.XMLNS_PREFIX + ":" + SVGConstants.XLINK_PREFIX, SVGConstants.XLINK_NAMESPACE_URI);
  // create element for SVG definitions
  defs = svgElement(SVGConstants.SVG_DEFS_TAG);
  root.appendChild(defs);
  // create element for Stylesheet information.
  style = SVGUtil.makeStyleElement(document);
  root.appendChild(style);
  // create a CSS class manager.
  cssman = new CSSClassManager();
}
 

实例 10


public void updateDocument(FractalDocument doc) {
    canvas.addGVTTreeRendererListener(new GVTTreeRendererAdapter() {
        @Override
        public void gvtRenderingCompleted(GVTTreeRendererEvent e) {
            synchronized (lock) {
                updateManagerIsReady = true;
                for (Iterator<Runnable> it = temporaryRunnableQueue.iterator(); it.hasNext();) {
                    Runnable next = it.next();
                    canvas.getUpdateManager().getUpdateRunnableQueue().invokeLater(next);
                    it.remove();
                }
            }
        }
    });
    rootBoundingBox = doc.getMetadata().getBoundingBox();
    currentBoundingBox = rootBoundingBox;
    // put any existing group elements inside a known root group with ZIndex Integer.MAX_VALUE
    SVGDocument document = doc.getSvgDoc();
    SVGElementCreator creator = new SVGElementCreator(document);
    SVGSVGElement root = document.getRootElement();
    Element group = creator.createGroup();
    NodeList children = root.getChildNodes();
    int childCount = children.getLength();
    List<Node> nodesToAppendToGroup = new ArrayList<Node>();
    for (int i=0; i< childCount; i++) {
        Node child = children.item(i);
        if (child.getNodeType() == Node.ELEMENT_NODE) {
            Element childEl = (Element) child;
            if ("g".equals(childEl.getTagName())) {
                nodesToAppendToGroup.add(childEl);
            }
        }
    }
    for (Node node:nodesToAppendToGroup) {
        group.appendChild(root.removeChild(node));
    }
    root.appendChild(group);
    zIndexMap.put(group.getAttributeNS(null, "id"), Integer.MAX_VALUE);
    canvas.setSVGDocument(document);
}
 

实例 11


/**
 * This should only ever be called in the Update Manager thread
 *
 * @param overlayId
 */
private void removeOverlayInUpdateManager(String overlayId) {
    String overlayGroupId = overlayIdMap.remove(overlayId);
    if (overlayGroupId != null) {
        zIndexMap.remove(overlayGroupId);
        SVGDocument myDoc = canvas.getSVGDocument();
        SVGSVGElement root = myDoc.getRootElement();
        Element el = root.getElementById(overlayGroupId);
        if (el != null) {
            root.removeChild(el);
        }
        overlayBoundingBoxes.remove(overlayGroupId);
        BoundingBox newBoundingBox = rootBoundingBox;
        for (BoundingBox box : overlayBoundingBoxes.values()) {
            newBoundingBox = newBoundingBox.expandToInclude(box);
        }
        currentBoundingBox = newBoundingBox;
        String viewBox = currentBoundingBox.forSvg();
        root.setAttributeNS(null, "viewBox", viewBox);
    } else {
        Log.gui.debug("Overlay hasn't been set");
    }
}
 

实例 12


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();
}
 

实例 13


protected void installSVGDocument(SVGDocument doc) {
    if (toolTipDocs != null) {
        Iterator i = toolTipDocs.keySet().iterator();
        while (i.hasNext()) {
            SVGDocument ttdoc;
            ttdoc = (SVGDocument)i.next();
            if (ttdoc == null) continue;
            NodeEventTarget root;
            root = (NodeEventTarget)ttdoc.getRootElement();
            if (root == null) continue;
            root.removeEventListenerNS
                (XMLConstants.XML_EVENTS_NAMESPACE_URI,
                 SVGConstants.SVG_EVENT_MOUSEOVER,
                 toolTipListener, false);
            root.removeEventListenerNS
                (XMLConstants.XML_EVENTS_NAMESPACE_URI,
                 SVGConstants.SVG_EVENT_MOUSEOUT,
                 toolTipListener, false);
        }
        toolTipDocs = null;
    }
    lastTarget = null;
    if (toolTipMap != null) {
        toolTipMap.clear();
    }
    super.installSVGDocument(doc);
}
 

实例 14


/**
 * Updates the existing SVG document with the new annotations (if any) from 
 * the com.ebixio.virtmus.shapes.* objects in the <b>shapes</b> array.
 */
public void transferShapes2Doc() {
    if (shapes.isEmpty()) return;
    SvgGenerator svgGenerator = new SvgGenerator();
    SVGGraphics2D svgGraphics2D = svgGenerator.getGraphics();
    // The dimensions of the SVG page (should match the size of the
    // music page image the annotations were drawn on).
    svgGraphics2D.setSVGCanvasSize(imgSrc.getDimension());
    for (VmShape s: shapes) {
        s.paint(svgGraphics2D);
    }
    shapes.clear();
    SVGDocument newSvgDoc = str2Document(svgGenerator.getSVG());
    if (svgDocument == null) {
        svgDocument = newSvgDoc;
    } else {
        // The root <svg> of the existing document.
        // We will append new shapes to this document.
        Element svgRoot = svgDocument.getRootElement();
        Element element = newSvgDoc.getRootElement();  // The <svg> element
        NodeList children = element.getChildNodes();
        for (int i = 0; i < children.getLength(); i++) {
            Node child = children.item(i);
            if (child.getNodeType() != Node.COMMENT_NODE) {
                if ( !child.getNodeName().equals("defs") ) {
                    Node dup = cloneNodeRec(child, svgDocument);
                    svgRoot.appendChild(dup);
                }
            }
        }
    }        
}
 

实例 15


private BufferedImage transCodeSVG(SVGDocument svg, float size){
        if (size < 0) {
            size = DEFAULT_SIZE;
        }
        SVGTranscoder transcoder = new SVGTranscoder();
        SVGSVGElement svgRootElement = svg.getRootElement();
        float svgWidth = svgRootElement.getWidth().getBaseVal().getValue();
        float svgHeight = svgRootElement.getHeight().getBaseVal().getValue();
//        float aspectRatio = svgWidth/svgHeight;
//        int height = Math.round(size);
//        int width = (int)(aspectRatio * height);
        Dimension dim = getWidthAndHeight(svgWidth, svgHeight, size);
        return transcoder.transcode(svg, dim.width, dim.height);
    }
 
讨论
淘淘あ西西 profile image