/** * Escapes all '<', '>' and '&' characters in a string. * * @param str A String. * @return HTMlEncoded String. */ private static String htmlencode(String str) { if (str == null) { return ""; } else { StringBuilder buf = new StringBuilder(); for (char ch : str.toCharArray()) { switch (ch) { case '<': buf.append("<"); break; case '>': buf.append(">"); break; case '&': buf.append("&"); break; default: buf.append(ch); break; } } return buf.toString(); } }
/** * Writes the <code>shape</code>, <code>coords</code>, <code>href</code>, * <code>nohref</code> Attribute for the specified figure and shape. * * @return Returns true, if the polygon is inside of the image bounds. */ private boolean writePolyAttributes(IXMLElement elem, SVGFigure f, Shape shape) { AffineTransform t = TRANSFORM.getClone(f); if (t == null) { t = drawingTransform; } else { t.preConcatenate(drawingTransform); } StringBuilder buf = new StringBuilder(); float[] coords = new float[6]; GeneralPath path = new GeneralPath(); for (PathIterator i = shape.getPathIterator(t, 1.5f); ! i.isDone(); i.next()) { switch (i.currentSegment(coords)) { case PathIterator.SEG_MOVETO : if (buf.length() != 0) { throw new IllegalArgumentException("Illegal shape "+shape); } if (buf.length() != 0) { buf.append(','); } buf.append((int) coords[0]); buf.append(','); buf.append((int) coords[1]); path.moveTo(coords[0], coords[1]); break; case PathIterator.SEG_LINETO : if (buf.length() != 0) { buf.append(','); } buf.append((int) coords[0]); buf.append(','); buf.append((int) coords[1]); path.lineTo(coords[0], coords[1]); break; case PathIterator.SEG_CLOSE : path.closePath(); break; default : throw new InternalError("Illegal segment type "+i.currentSegment(coords)); } } elem.setAttribute("shape", "poly"); elem.setAttribute("coords", buf.toString()); writeHrefAttribute(elem, f); return path.intersects(new Rectangle2D.Float(bounds.x, bounds.y, bounds.width, bounds.height)); }
/** Writes the attributes of the figure into the specified DOMOutput. */ public static void writeAttributes(Figure f, DOMOutput out) throws IOException { Color color; Double dbl; String value; // Fill attributes color = FILL_COLOR.get(f); if (color == null) { value = "none"; } else { value = "000000" + Integer.toHexString(color.getRGB()); value = "#" + value.substring(value.length() - 6); } out.addAttribute("fill", value); if (WINDING_RULE.get(f) != WindingRule.NON_ZERO) { out.addAttribute("fill-rule", "evenodd"); } // Stroke attributes color = STROKE_COLOR.get(f); if (color == null) { value = "none"; } else { value = "000000" + Integer.toHexString(color.getRGB()); value = "#" + value.substring(value.length() - 6); } out.addAttribute("stroke", value); out.addAttribute("stroke-width", STROKE_WIDTH.get(f), 1d); out.addAttribute( "stroke-miterlimit", STROKE_MITER_LIMIT_FACTOR.get(f) / STROKE_WIDTH.get(f), 4d); double[] dashes = STROKE_DASHES.get(f); dbl = (STROKE_DASH_FACTOR.get(f) == null) ? STROKE_WIDTH.get(f) : STROKE_DASH_FACTOR.get(f); if (dashes != null) { StringBuilder buf = new StringBuilder(); for (int i = 0; i < dashes.length; i++) { if (i != 0) { buf.append(','); buf.append(dashes[i] * dbl); } out.addAttribute("stroke-dasharray", buf.toString()); } } out.addAttribute("stroke-dashoffset", STROKE_DASH_PHASE.get(f), 0d); // Text attributes out.addAttribute("font-size", FONT_SIZE.get(f)); // out.addAttribute("text-anchor", "start"); }
public static String toPathData(BezierPath path) { StringBuilder buf = new StringBuilder(); if (path.size() == 0) { // nothing to do } else if (path.size() == 1) { BezierPath.Node current = path.get(0); buf.append("M "); buf.append(current.x[0]); buf.append(' '); buf.append(current.y[0]); buf.append(" L "); buf.append(current.x[0]); buf.append(' '); buf.append(current.y[0] + 1); } else { BezierPath.Node previous; BezierPath.Node current; previous = current = path.get(0); buf.append("M "); buf.append(current.x[0]); buf.append(' '); buf.append(current.y[0]); for (int i = 1, n = path.size(); i < n; i++) { previous = current; current = path.get(i); if ((previous.mask & BezierPath.C2_MASK) == 0) { if ((current.mask & BezierPath.C1_MASK) == 0) { buf.append(" L "); buf.append(current.x[0]); buf.append(' '); buf.append(current.y[0]); } else { buf.append(" Q "); buf.append(current.x[1]); buf.append(' '); buf.append(current.y[1]); buf.append(' '); buf.append(current.x[0]); buf.append(' '); buf.append(current.y[0]); } } else { if ((current.mask & BezierPath.C1_MASK) == 0) { buf.append(" Q "); buf.append(current.x[2]); buf.append(' '); buf.append(current.y[2]); buf.append(' '); buf.append(current.x[0]); buf.append(' '); buf.append(current.y[0]); } else { buf.append(" C "); buf.append(previous.x[2]); buf.append(' '); buf.append(previous.y[2]); buf.append(' '); buf.append(current.x[1]); buf.append(' '); buf.append(current.y[1]); buf.append(' '); buf.append(current.x[0]); buf.append(' '); buf.append(current.y[0]); } } } if (path.isClosed()) { if (path.size() > 1) { previous = path.get(path.size() - 1); current = path.get(0); if ((previous.mask & BezierPath.C2_MASK) == 0) { if ((current.mask & BezierPath.C1_MASK) == 0) { buf.append(" L "); buf.append(current.x[0]); buf.append(' '); buf.append(current.y[0]); } else { buf.append(" Q "); buf.append(current.x[1]); buf.append(' '); buf.append(current.y[1]); buf.append(' '); buf.append(current.x[0]); buf.append(' '); buf.append(current.y[0]); } } else { if ((current.mask & BezierPath.C1_MASK) == 0) { buf.append(" Q "); buf.append(previous.x[2]); buf.append(' '); buf.append(previous.y[2]); buf.append(' '); buf.append(current.x[0]); buf.append(' '); buf.append(current.y[0]); } else { buf.append(" C "); buf.append(previous.x[2]); buf.append(' '); buf.append(previous.y[2]); buf.append(' '); buf.append(current.x[1]); buf.append(' '); buf.append(current.y[1]); buf.append(' '); buf.append(current.x[0]); buf.append(' '); buf.append(current.y[0]); } } } buf.append(" Z"); } } return buf.toString(); }