📄 mxsvgcanvas.java
字号:
/** * $Id: mxSvgCanvas.java,v 1.41 2009/02/15 10:01:06 gaudenz Exp $ * Copyright (c) 2007, Gaudenz Alder */package com.mxgraph.canvas;import java.util.Hashtable;import java.util.List;import org.w3c.dom.Document;import org.w3c.dom.Element;import com.mxgraph.util.mxConstants;import com.mxgraph.util.mxPoint;import com.mxgraph.util.mxUtils;/** * An implementation of a canvas that uses SVG for painting. This canvas * ignores the STYLE_LABEL_BACKGROUNDCOLOR and * STYLE_LABEL_BORDERCOLOR styles due to limitations of SVG. */public class mxSvgCanvas extends mxBasicCanvas{ /** * Holds the HTML document that represents the canvas. */ protected Document document; /** * Constructs a new SVG canvas for the specified dimension and scale. * * @param width Width of the canvas to be constructed. * @param height Height of the canvas to be constructed. * @param scale Scale of the canvas to be constructed. */ public mxSvgCanvas() { this(null); } /** * Constructs a new SVG canvas for the specified bounds, scale and * background color. * * @param x X-coordinate of the origin of the canvas. * @param y Y-coordinate of the origin of the canvas. * @param width Width of the canvas to be constructed. * @param height Height of the canvas to be constructed. * @param scale Scale of the canvas to be constructed. * @param backgroundColor Color of the background. Default is null * (transparent). */ public mxSvgCanvas(Document document) { setDocument(document); } /** * */ public void appendSvgElement(Element node) { if (document != null) { document.getDocumentElement().appendChild(node); } } /** * */ public void setDocument(Document document) { this.document = document; } /** * Returns a reference to the document that represents the canvas. * * @return Returns the document. */ public Document getDocument() { return document; } /* * (non-Javadoc) * @see com.mxgraph.canvas.mxICanvas#drawVertex(int, int, int, int, java.util.Hashtable) */ public Object drawVertex(int x, int y, int w, int h, Hashtable style) { Element elem = null; int start = mxUtils.getInt(style, mxConstants.STYLE_STARTSIZE); x += translate.x; y += translate.y; if (start == 0) { elem = drawShape(x, y, w, h, style); } else { start = (int) Math.round(start * scale); // Removes some styles to draw the content area Hashtable cloned = new Hashtable(style); cloned.remove(mxConstants.STYLE_FILLCOLOR); cloned.remove(mxConstants.STYLE_ROUNDED); if (mxUtils.isTrue(style, mxConstants.STYLE_HORIZONTAL, true)) { elem = drawShape(x, y, w, start, style); drawShape(x, y + start, w, h - start, cloned); } else { elem = drawShape(x, y, start, h, style); drawShape(x + start, y, w - start, h, cloned); } } return elem; } /* * (non-Javadoc) * @see com.mxgraph.canvas.mxICanvas#drawEdge(java.util.List, java.util.Hashtable) */ public Object drawEdge(List pts, Hashtable style) { // Transpose all points by cloning into a new array pts = mxUtils.translatePoints(pts, translate.x, translate.y); // Draws the line Element elem = drawLine(pts, style); // Applies opacity float opacity = mxUtils.getFloat(style, mxConstants.STYLE_OPACITY, 100); if (opacity != 100) { String value = String.valueOf(opacity / 100); elem.setAttribute("fill-opacity", value); elem.setAttribute("stroke-opacity", value); } return elem; } /* * (non-Javadoc) * @see com.mxgraph.canvas.mxICanvas#drawLabel(java.lang.String, int, int, int, int, java.util.Hashtable, boolean) */ public Object drawLabel(String label, int x, int y, int w, int h, Hashtable style, boolean isHtml) { if (drawLabels) { x += translate.x; y -= translate.y; return drawText(label, x, y, w, h, style); } return null; } /** * Draws the shape specified with the STYLE_SHAPE key in the given style. * * @param x X-coordinate of the shape. * @param y Y-coordinate of the shape. * @param w Width of the shape. * @param h Height of the shape. * @param style Style of the the shape. */ public Element drawShape(int x, int y, int w, int h, Hashtable style) { String fillColor = mxUtils.getString(style, mxConstants.STYLE_FILLCOLOR, "none"); String gradientColor = mxUtils.getString(style, mxConstants.STYLE_GRADIENTCOLOR); String strokeColor = mxUtils.getString(style, mxConstants.STYLE_STROKECOLOR); float strokeWidth = (float) (mxUtils.getFloat(style, mxConstants.STYLE_STROKEWIDTH, 1) * scale); // Draws the shape String shape = mxUtils.getString(style, mxConstants.STYLE_SHAPE); Element elem = null; Element background = null; if (shape.equals(mxConstants.SHAPE_IMAGE)) { String img = getImageForStyle(style); if (img != null) { elem = document.createElement("image"); elem.setAttribute("x", String.valueOf(x)); elem.setAttribute("y", String.valueOf(y)); elem.setAttribute("width", String.valueOf(w)); elem.setAttribute("height", String.valueOf(h)); // TODO: Add global namespace shortcut in document elem.setAttributeNS(mxConstants.NS_XLINK, "href", img); } } else if (shape.equals(mxConstants.SHAPE_LINE)) { String direction = mxUtils.getString(style, mxConstants.STYLE_DIRECTION, mxConstants.DIRECTION_EAST); String d = null; if (direction.equals(mxConstants.DIRECTION_EAST) || direction.equals(mxConstants.DIRECTION_WEST)) { int mid = (int) (y + h / 2); d = "M " + x + " " + mid + " L " + (x + w) + " " + mid; } else { int mid = (int) (x + w / 2); d = "M " + mid + " " + y + " L " + mid + " " + (y + h); } elem = document.createElement("path"); elem.setAttribute("d", d + " Z"); } else if (shape.equals(mxConstants.SHAPE_ELLIPSE)) { elem = document.createElement("ellipse"); elem.setAttribute("cx", String.valueOf(x + w / 2)); elem.setAttribute("cy", String.valueOf(y + h / 2)); elem.setAttribute("rx", String.valueOf(w / 2)); elem.setAttribute("ry", String.valueOf(h / 2)); } else if (shape.equals(mxConstants.SHAPE_DOUBLE_ELLIPSE)) { elem = document.createElement("g"); background = document.createElement("ellipse"); background.setAttribute("cx", String.valueOf(x + w / 2)); background.setAttribute("cy", String.valueOf(y + h / 2)); background.setAttribute("rx", String.valueOf(w / 2)); background.setAttribute("ry", String.valueOf(h / 2)); elem.appendChild(background); int inset = (int) ((3 + strokeWidth) * scale); Element foreground = document.createElement("ellipse"); foreground.setAttribute("fill", "none"); foreground.setAttribute("stroke", strokeColor); foreground .setAttribute("stroke-width", String.valueOf(strokeWidth)); foreground.setAttribute("cx", String.valueOf(x + w / 2)); foreground.setAttribute("cy", String.valueOf(y + h / 2)); foreground.setAttribute("rx", String.valueOf(w / 2 - inset)); foreground.setAttribute("ry", String.valueOf(h / 2 - inset)); elem.appendChild(foreground); } else if (shape.equals(mxConstants.SHAPE_RHOMBUS)) { elem = document.createElement("path"); String d = "M " + (x + w / 2) + " " + y + " L " + (x + w) + " " + (y + h / 2) + " L " + (x + w / 2) + " " + (y + h) + " L " + x + " " + (y + h / 2); elem.setAttribute("d", d + " Z"); } else if (shape.equals(mxConstants.SHAPE_TRIANGLE)) { elem = document.createElement("path"); String direction = mxUtils.getString(style, mxConstants.STYLE_DIRECTION, ""); String d = null; if (direction.equals(mxConstants.DIRECTION_NORTH)) { d = "M " + x + " " + (y + h) + " L " + (x + w / 2) + " " + y + " L " + (x + w) + " " + (y + h); } else if (direction.equals(mxConstants.DIRECTION_SOUTH)) { d = "M " + x + " " + y + " L " + (x + w / 2) + " " + (y + h) + " L " + (x + w) + " " + y; } else if (direction.equals(mxConstants.DIRECTION_WEST)) { d = "M " + (x + w) + " " + y + " L " + x + " " + (y + h / 2) + " L " + (x + w) + " " + (y + h); } else // east { d = "M " + x + " " + y + " L " + (x + w) + " " + (y + h / 2) + " L " + x + " " + (y + h); } elem.setAttribute("d", d + " Z"); } else if (shape.equals(mxConstants.SHAPE_HEXAGON)) { elem = document.createElement("path"); String direction = mxUtils.getString(style, mxConstants.STYLE_DIRECTION, ""); String d = null; if (direction.equals(mxConstants.DIRECTION_NORTH) || direction.equals(mxConstants.DIRECTION_SOUTH)) { d = "M " + (x + 0.5 * w) + " " + y + " L " + (x + w) + " " + (y + 0.25 * h) + " L " + (x + w) + " " + (y + 0.75 * h) + " L " + (x + 0.5 * w) + " " + (y + h) + " L " + x + " " + (y + 0.75 * h) + " L " + x + " " + (y + 0.25 * h); } else { d = "M " + (x + 0.25 * w) + " " + y + " L " + (x + 0.75 * w) + " " + y + " L " + (x + w) + " " + (y + 0.5 * h) + " L " + (x + 0.75 * w) + " " + (y + h) + " L " + (x + 0.25 * w) + " " + (y + h) + " L " + x + " " + (y + 0.5 * h); } elem.setAttribute("d", d + " Z"); } else if (shape.equals(mxConstants.SHAPE_CLOUD)) { elem = document.createElement("path"); String d = "M " + (x + 0.25 * w) + " " + (y + 0.25 * h) + " C " + (x + 0.05 * w) + " " + (y + 0.25 * h) + " " + x + " " + (y + 0.5 * h) + " " + (x + 0.16 * w) + " " + (y + 0.55 * h) + " C " + x + " " + (y + 0.66 * h) + " " + (x + 0.18 * w) + " " + (y + 0.9 * h) + " " + (x + 0.31 * w) + " " + (y + 0.8 * h) + " C " + (x + 0.4 * w) + " " + (y + h) + " " + (x + 0.7 * w) + " " + (y + h) + " " + (x + 0.8 * w) + " " + (y + 0.8 * h) + " C " + (x + w) + " " + (y + 0.8 * h) + " " + (x + w) + " " + (y + 0.6 * h) + " " + (x + 0.875 * w) + " " + (y + 0.5 * h) + " C " + (x + w) + " " + (y + 0.3 * h) + " " + (x + 0.8 * w) + " " + (y + 0.1 * h) + " " + (x + 0.625 * w) + " " + (y + 0.2 * h) + " C " + (x + 0.5 * w) + " " + (y + 0.05 * h) + " " + (x + 0.3 * w) + " " + (y + 0.05 * h) + " " + (x + 0.25 * w) + " " + (y + 0.25 * h); elem.setAttribute("d", d + " Z"); } else if (shape.equals(mxConstants.SHAPE_ACTOR)) { elem = document.createElement("path"); double width3 = w / 3; String d = " M " + x + " " + (y + h) + " C " + x + " " + (y + 3 * h / 5) + " " + x + " " + (y + 2 * h / 5) + " " + (x + w / 2) + " " + (y + 2 * h / 5) + " C " + (x + w / 2 - width3) + " " + (y + 2 * h / 5) + " " + (x + w / 2 - width3) + " " + y + " " + (x + w / 2) + " " + y + " C " + (x + w / 2 + width3) + " " + y + " " + (x + w / 2 + width3) + " " + (y + 2 * h / 5) + " " + (x + w / 2) + " " + (y + 2 * h / 5) + " C " + (x + w) + " " + (y + 2 * h / 5) + " " + (x + w) + " " + (y + 3 * h / 5) + " " + (x + w) + " " + (y + h); elem.setAttribute("d", d + " Z"); } else if (shape.equals(mxConstants.SHAPE_CYLINDER)) { elem = document.createElement("g"); background = document.createElement("path");
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -