Ejemplo n.º 1
0
  /** 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");
  }
Ejemplo n.º 2
0
  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();
  }