📄 mxsvgcanvas.java
字号:
double dy = Math.min(40, Math.floor(h / 5)); String d = " M " + x + " " + (y + dy) + " C " + x + " " + (y - dy / 3) + " " + (x + w) + " " + (y - dy / 3) + " " + (x + w) + " " + (y + dy) + " L " + (x + w) + " " + (y + h - dy) + " C " + (x + w) + " " + (y + h + dy / 3) + " " + x + " " + (y + h + dy / 3) + " " + x + " " + (y + h - dy); background.setAttribute("d", d + " Z"); elem.appendChild(background); Element foreground = document.createElement("path"); d = "M " + x + " " + (y + dy) + " C " + x + " " + (y + 2 * dy) + " " + (x + w) + " " + (y + 2 * dy) + " " + (x + w) + " " + (y + dy); foreground.setAttribute("d", d); foreground.setAttribute("fill", "none"); foreground.setAttribute("stroke", strokeColor); foreground .setAttribute("stroke-width", String.valueOf(strokeWidth)); elem.appendChild(foreground); } else { elem = document.createElement("rect"); elem.setAttribute("x", String.valueOf(x)); elem.setAttribute("y", String.valueOf(y)); elem.setAttribute("width", String.valueOf(w)); elem.setAttribute("height", String.valueOf(h)); } Element bg = background; if (bg == null) { bg = elem; } bg.setAttribute("fill", fillColor); bg.setAttribute("stroke", strokeColor); bg.setAttribute("stroke-width", String.valueOf(strokeWidth)); // Adds the shadow element Element shadowElement = null; if (mxUtils.isTrue(style, mxConstants.STYLE_SHADOW, false) && !fillColor.equals("none")) { shadowElement = (Element) bg.cloneNode(true); shadowElement.setAttribute("transform", mxConstants.SVG_SHADOWTRANSFORM); shadowElement.setAttribute("fill", mxConstants.W3C_SHADOWCOLOR); shadowElement.setAttribute("stroke", mxConstants.W3C_SHADOWCOLOR); shadowElement.setAttribute("stroke-width", String .valueOf(strokeWidth)); appendSvgElement(shadowElement); } // Applies rotation double rotation = mxUtils.getDouble(style, mxConstants.STYLE_ROTATION); if (rotation != 0) { int cx = x + w / 2; int cy = y + h / 2; elem.setAttribute("transform", "rotate(" + rotation + "," + cx + "," + cy + ")"); if (shadowElement != null) { shadowElement.setAttribute("transform", "rotate(" + rotation + "," + cx + "," + cy + ") " + mxConstants.SVG_SHADOWTRANSFORM); } } // 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); if (shadowElement != null) { shadowElement.setAttribute("fill-opacity", value); shadowElement.setAttribute("stroke-opacity", value); } } appendSvgElement(elem); return elem; } /** * Draws the given lines as segments between all points of the given list * of mxPoints. * * @param pts List of points that define the line. * @param style Style to be used for painting the line. */ public Element drawLine(List pts, Hashtable style) { Element group = document.createElement("g"); Element path = document.createElement("path"); String strokeColor = mxUtils.getString(style, mxConstants.STYLE_STROKECOLOR); float tmpStroke = (float) (mxUtils.getFloat(style, mxConstants.STYLE_STROKEWIDTH, 1)); float strokeWidth = (float) (tmpStroke * scale); if (strokeColor != null && strokeWidth > 0) { // Draws the start marker Object marker = style.get(mxConstants.STYLE_STARTARROW); mxPoint pt = (mxPoint) pts.get(1); mxPoint p0 = (mxPoint) pts.get(0); mxPoint offset = null; if (marker != null) { float size = (float) (mxUtils.getFloat(style, mxConstants.STYLE_STARTSIZE, mxConstants.DEFAULT_MARKERSIZE)); offset = drawMarker(group, marker, pt, p0, size, tmpStroke, strokeColor); } else { double dx = pt.getX() - p0.getX(); double dy = pt.getY() - p0.getY(); double dist = Math.max(1, Math.sqrt(dx * dx + dy * dy)); double nx = dx * strokeWidth / dist; double ny = dy * strokeWidth / dist; offset = new mxPoint(nx / 2, ny / 2); } // Applies offset to the point if (offset != null) { p0 = (mxPoint) p0.clone(); p0.setX(p0.getX() + offset.getX()); p0.setY(p0.getY() + offset.getY()); offset = null; } // Draws the end marker marker = style.get(mxConstants.STYLE_ENDARROW); pt = (mxPoint) pts.get(pts.size() - 2); mxPoint pe = (mxPoint) pts.get(pts.size() - 1); if (marker != null) { float size = (float) (mxUtils.getFloat(style, mxConstants.STYLE_ENDSIZE, mxConstants.DEFAULT_MARKERSIZE)); offset = drawMarker(group, marker, pt, pe, size, tmpStroke, strokeColor); } else { double dx = pt.getX() - p0.getX(); double dy = pt.getY() - p0.getY(); double dist = Math.max(1, Math.sqrt(dx * dx + dy * dy)); double nx = dx * strokeWidth / dist; double ny = dy * strokeWidth / dist; offset = new mxPoint(nx / 2, ny / 2); } // Applies offset to the point if (offset != null) { pe = (mxPoint) pe.clone(); pe.setX(pe.getX() + offset.getX()); pe.setY(pe.getY() + offset.getY()); offset = null; } // Draws the line segments pt = p0; String d = "M " + pt.getX() + " " + pt.getY(); for (int i = 1; i < pts.size() - 1; i++) { pt = (mxPoint) pts.get(i); d += " L " + pt.getX() + " " + pt.getY(); } d += " L " + pe.getX() + " " + pe.getY(); path.setAttribute("d", d); path.setAttribute("stroke", strokeColor); path.setAttribute("fill", "none"); path.setAttribute("stroke-width", String.valueOf(strokeWidth)); group.appendChild(path); appendSvgElement(group); } return group; } /** * Draws the specified marker as a child path in the given parent. */ public mxPoint drawMarker(Element parent, Object type, mxPoint p0, mxPoint pe, float size, float strokeWidth, String color) { mxPoint offset = null; // Computes the norm and the inverse norm double dx = pe.getX() - p0.getX(); double dy = pe.getY() - p0.getY(); double dist = Math.max(1, Math.sqrt(dx * dx + dy * dy)); double absSize = size * scale; double nx = dx * absSize / dist; double ny = dy * absSize / dist; pe = (mxPoint) pe.clone(); pe.setX(pe.getX() - nx * strokeWidth / (2 * size)); pe.setY(pe.getY() - ny * strokeWidth / (2 * size)); nx *= 0.5 + strokeWidth / 2; ny *= 0.5 + strokeWidth / 2; Element path = document.createElement("path"); path.setAttribute("stroke-width", String.valueOf(strokeWidth * scale)); path.setAttribute("stroke", color); path.setAttribute("fill", color); String d = null; if (type.equals(mxConstants.ARROW_CLASSIC) || type.equals(mxConstants.ARROW_BLOCK)) { d = "M " + pe.getX() + " " + pe.getY() + " L " + (pe.getX() - nx - ny / 2) + " " + (pe.getY() - ny + nx / 2) + ((!type.equals(mxConstants.ARROW_CLASSIC)) ? "" : " L " + (pe.getX() - nx * 3 / 4) + " " + (pe.getY() - ny * 3 / 4)) + " L " + (pe.getX() + ny / 2 - nx) + " " + (pe.getY() - ny - nx / 2) + " z"; } else if (type.equals(mxConstants.ARROW_OPEN)) { nx *= 1.2; ny *= 1.2; d = "M " + (pe.getX() - nx - ny / 2) + " " + (pe.getY() - ny + nx / 2) + " L " + (pe.getX() - nx / 6) + " " + (pe.getY() - ny / 6) + " L " + (pe.getX() + ny / 2 - nx) + " " + (pe.getY() - ny - nx / 2) + " M " + pe.getX() + " " + pe.getY(); path.setAttribute("fill", "none"); } else if (type.equals(mxConstants.ARROW_OVAL)) { nx *= 1.2; ny *= 1.2; absSize *= 1.2; d = "M " + (pe.getX() - ny / 2) + " " + (pe.getY() + nx / 2) + " a " + (absSize / 2) + " " + (absSize / 2) + " 0 1,1 " + (nx / 8) + " " + (ny / 8) + " z"; } else if (type.equals(mxConstants.ARROW_DIAMOND)) { d = "M " + (pe.getX() + nx / 2) + " " + (pe.getY() + ny / 2) + " L " + (pe.getX() - ny / 2) + " " + (pe.getY() + nx / 2) + " L " + (pe.getX() - nx / 2) + " " + (pe.getY() - ny / 2) + " L " + (pe.getX() + ny / 2) + " " + (pe.getY() - nx / 2) + " z"; } if (d != null) { path.setAttribute("d", d); parent.appendChild(path); } return offset; } /** * Draws the specified text either using drawHtmlString or using drawString. * * @param text Text to be painted. * @param x X-coordinate of the text. * @param y Y-coordinate of the text. * @param w Width of the text. * @param h Height of the text. * @param style Style to be used for painting the text. */ public Object drawText(String text, int x, int y, int w, int h, Hashtable style) { Element elem = null; String fontColor = mxUtils.getString(style, mxConstants.STYLE_FONTCOLOR, "black"); String fontFamily = mxUtils.getString(style, mxConstants.STYLE_FONTFAMILY, mxConstants.DEFAULT_FONTFAMILIES); int fontSize = (int) (mxUtils.getInt(style, mxConstants.STYLE_FONTSIZE, mxConstants.DEFAULT_FONTSIZE) * scale); if (text != null && text.length() > 0) { elem = document.createElement("text"); // Applies the opacity float opacity = mxUtils.getFloat(style, mxConstants.STYLE_TEXT_OPACITY, 100); if (opacity != 100) { String value = String.valueOf(opacity / 100); elem.setAttribute("fill-opacity", value); elem.setAttribute("stroke-opacity", value); } elem.setAttribute("text-anchor", "middle"); elem.setAttribute("font-weight", "normal"); elem.setAttribute("font-decoration", "none"); elem.setAttribute("font-size", String.valueOf(fontSize)); elem.setAttribute("font-family", fontFamily); elem.setAttribute("fill", fontColor); String[] lines = text.split("\n"); y += fontSize + (h - lines.length * (fontSize + mxConstants.LINESPACING)) / 2 - 2; for (int i = 0; i < lines.length; i++) { Element tspan = document.createElement("tspan"); tspan.setAttribute("x", String.valueOf(x + w / 2)); tspan.setAttribute("y", String.valueOf(y)); tspan.appendChild(document.createTextNode(lines[i])); elem.appendChild(tspan); y += fontSize + mxConstants.LINESPACING; } appendSvgElement(elem); } return elem; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -