Exemplo n.º 1
0
  private void paint(Shuttle shuttle) throws InvalidXMLException {
    Element e = shuttle.node;
    String shapeName = e.getLocalName();

    if (!SVG.NS.equals(e.getNamespaceURI())) {

      for (Node c = e.getFirstChild(); c != null; c = c.getNextSibling()) {
        if (c.getNodeType() != Node.ELEMENT_NODE) continue;
        Shuttle cp = new Shuttle(shuttle, Element.class.cast(c));
        paint(cp);
      }
    } else if (shapeName == null) {
      LOG.warning("shapeName is null");
    } else if (shapeName.equals("g")) {

      for (Node c = e.getFirstChild(); c != null; c = c.getNextSibling()) {
        if (c.getNodeType() != Node.ELEMENT_NODE) continue;
        Shuttle cp = new Shuttle(shuttle, Element.class.cast(c));
        paint(cp);
      }
    } else if (shapeName.equals("path")) {
      Attr d = e.getAttributeNode("d");
      if (d != null) {
        shuttle.shape = SVGUtils.pathToShape(d.getValue());
        drawShape(shuttle);
      }
    } else if (shapeName.equals("polyline")) {
      Attr points = e.getAttributeNode("points");
      if (points != null) {
        shuttle.shape = SVGUtils.polylineToShape(points.getValue());
        drawShape(shuttle);
      }
    } else if (shapeName.equals("polygon")) {
      Attr points = e.getAttributeNode("points");
      if (points != null) {
        shuttle.shape = SVGUtils.polygonToShape(points.getValue());
        drawShape(shuttle);
      }
    } else if (shapeName.equals("rect")) {

      Attr x = e.getAttributeNode("x");
      Attr y = e.getAttributeNode("y");
      Attr w = e.getAttributeNode("width");
      Attr h = e.getAttributeNode("height");
      if (x != null && y != null && w != null && h != null) {
        shuttle.shape =
            new Rectangle2D.Double(
                Double.parseDouble(x.getValue()),
                Double.parseDouble(y.getValue()),
                Double.parseDouble(w.getValue()),
                Double.parseDouble(h.getValue()));
        drawShape(shuttle);
      }
    } else if (shapeName.equals("line")) {
      Attr x1 = e.getAttributeNode("x1");
      Attr y1 = e.getAttributeNode("y1");
      Attr x2 = e.getAttributeNode("x2");
      Attr y2 = e.getAttributeNode("y2");
      if (x1 != null && y1 != null && x2 != null && y2 != null) {
        shuttle.shape =
            new Line2D.Double(
                Double.parseDouble(x1.getValue()),
                Double.parseDouble(y1.getValue()),
                Double.parseDouble(x2.getValue()),
                Double.parseDouble(y2.getValue()));
        drawShape(shuttle);
      }
    } else if (shapeName.equals("circle")) {
      Attr cx = e.getAttributeNode("cx");
      Attr cy = e.getAttributeNode("cy");
      Attr r = e.getAttributeNode("r");
      if (cx != null && cy != null && r != null) {
        double radius = Double.parseDouble(r.getValue());
        shuttle.shape =
            new Ellipse2D.Double(
                Double.parseDouble(cx.getValue()) - radius,
                Double.parseDouble(cy.getValue()) - radius,
                radius * 2,
                radius * 2);
        drawShape(shuttle);
      }
    } else if (shapeName.equals("ellipse")) {
      Attr cx = e.getAttributeNode("cx");
      Attr cy = e.getAttributeNode("cy");
      Attr rx = e.getAttributeNode("rx");
      Attr ry = e.getAttributeNode("ry");
      if (cx != null && cy != null && rx != null && ry != null) {
        double radiusx = Double.parseDouble(rx.getValue());
        double radiusy = Double.parseDouble(ry.getValue());
        shuttle.shape =
            new Ellipse2D.Double(
                Double.parseDouble(cx.getValue()) - radiusx,
                Double.parseDouble(cy.getValue()) - radiusy,
                radiusx * 2,
                radiusy * 2);
        drawShape(shuttle);
      }
    } else if (StringUtils.isIn(shapeName, "title", "defs", "desc", "metadata")) {
      // ignore
    } else if (shapeName.equals("text")) {
      Attr x = e.getAttributeNode("x");
      Attr y = e.getAttributeNode("y");
      if (x != null && y != null) {

        Font f = new Font(shuttle.fontFamily, Font.PLAIN, (int) shuttle.fontSize);

        FontRenderContext frc = shuttle.g.getFontRenderContext();
        TextLayout tl = new TextLayout(e.getTextContent(), f, frc);
        shuttle.shape = tl.getOutline(null);
        shuttle.shape =
            AffineTransform.getTranslateInstance(
                    Double.parseDouble(x.getValue()), Double.parseDouble(y.getValue()))
                .createTransformedShape(shuttle.shape);

        drawShape(shuttle);
      }
    } else if (shapeName.equals("svg")) {

      for (Node c = e.getFirstChild(); c != null; c = c.getNextSibling()) {
        if (c.getNodeType() != Node.ELEMENT_NODE) continue;
        Shuttle cp = new Shuttle(shuttle, Element.class.cast(c));
        paint(cp);
      }
    } else {
      LOG.warning("cannot display <" + e.getLocalName() + ">");
    }
  }
Exemplo n.º 2
0
  public void paint(Graphics2D g, Node dom, Rectangle2D viewRect) throws InvalidXMLException {

    if (g == null) throw new NullPointerException("g is null");
    if (dom == null) throw new NullPointerException("dom is null");
    Element root = null;
    if (dom.getNodeType() == Node.DOCUMENT_NODE) {
      root = Document.class.cast(dom).getDocumentElement();
    } else if (dom.getNodeType() == Node.ELEMENT_NODE) {
      root = Element.class.cast(dom);
    }

    if (root == null) throw new InvalidXMLException(dom, "no root");
    if (!XMLUtilities.isA(root, SVG.NS, "svg"))
      throw new InvalidXMLException(root, "not a SVG root");

    Dimension2D srcSize = getSize(root);
    Rectangle2D viewBox = null;
    Attr viewBoxAttr = root.getAttributeNode("viewBox");
    if (viewBoxAttr != null) {
      String tokens[] = viewBoxAttr.getValue().trim().split("[ \t\n]+");
      if (tokens.length != 4) throw new InvalidXMLException(viewBoxAttr, "invalid ");
      viewBox =
          new Rectangle2D.Double(
              Double.parseDouble(tokens[0]),
              Double.parseDouble(tokens[1]),
              Double.parseDouble(tokens[2]),
              Double.parseDouble(tokens[3]));
      srcSize = new Dimension2D.Double(viewBox.getWidth(), viewBox.getHeight());
    }

    AffineTransform originalTr = null;
    if (viewRect == null) {
      viewRect = new Rectangle2D.Double(0, 0, srcSize.getWidth(), srcSize.getHeight());
    } else {
      if (srcSize.getWidth() > 0 && srcSize.getHeight() > 0) {
        originalTr = g.getTransform();

        double ratio =
            Math.min(
                viewRect.getWidth() / srcSize.getWidth(),
                viewRect.getHeight() / srcSize.getHeight());

        g.translate(
            (viewRect.getWidth() - srcSize.getWidth() * ratio) / 2.0,
            (viewRect.getHeight() - srcSize.getHeight() * ratio) / 2.0);
        g.scale(ratio, ratio);
      }
    }
    Shape oldclip = g.getClip();
    Shuttle shuttle = new Shuttle(g, root);
    if (viewBox != null) {
      AffineTransform tr = AffineTransform.getTranslateInstance(-viewBox.getX(), -viewBox.getY());
      shuttle.transform.concatenate(tr);
      Area area = new Area(shuttle.clip);
      area.intersect(
          new Area(new Rectangle2D.Double(0, 0, viewBox.getWidth(), viewBox.getHeight())));
      shuttle.clip = area;
    }

    paint(shuttle);

    if (originalTr != null) {
      g.setTransform(originalTr);
    }
    g.setClip(oldclip);
  }