示例#1
0
 /*
  * (non-Javadoc)
  *
  * @see
  * org.eclipse.birt.chart.device.util.ChartTextRenderer#renderOutline(org
  * .eclipse.birt.chart.device.IPrimitiveRenderer,
  * org.eclipse.birt.chart.model.attribute.LineAttributes,
  * java.awt.geom.Rectangle2D.Double)
  */
 @Override
 protected void renderOutline(
     IPrimitiveRenderer renderer, LineAttributes lineAttribs, Double rect) {
   if (lineAttribs != null && lineAttribs.isVisible() && lineAttribs.getColor() != null) {
     SVGGraphics2D g2d = (SVGGraphics2D) ((IDeviceRenderer) renderer).getGraphicsContext();
     Stroke sPrevious = null;
     final ColorDefinition cd = lineAttribs.getColor();
     final Stroke sCurrent = ((SVGRendererImpl) renderer).getCachedStroke(lineAttribs);
     if (sCurrent != null) // SOME STROKE DEFINED?
     {
       sPrevious = g2d.getStroke();
       g2d.setStroke(sCurrent);
     }
     g2d.setColor((Color) _sxs.getColor(cd));
     g2d.draw(rect);
     g2d.setNoFillColor(g2d.getCurrentElement());
     if (sPrevious != null) // RESTORE PREVIOUS STROKE
     {
       g2d.setStroke(sPrevious);
     }
   }
 }
示例#2
0
 /*
  * (non-Javadoc)
  *
  * @see
  * org.eclipse.birt.chart.device.util.ChartTextRenderer#fillWithoutDefer
  * (java.awt.Graphics2D, java.awt.Shape)
  */
 @Override
 protected void fillShadow(Graphics2D g2d, Shape shape) {
   ((SVGGraphics2D) g2d).fill(shape, false);
 }
示例#3
0
  /**
   * This method will only throw exceptions if some aspect of the test's internal operation fails.
   */
  public TestReport runImpl() throws Exception {
    DefaultTestReport report = new DefaultTestReport(this);

    SVGGraphics2D g2d = buildSVGGraphics2D();
    g2d.setSVGCanvasSize(CANVAS_SIZE);

    //
    // Generate SVG content
    //
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    OutputStreamWriter osw = new OutputStreamWriter(bos, "UTF-8");
    try {
      painter.paint(g2d);
      configureSVGGraphics2D(g2d);
      g2d.stream(osw);
      osw.flush();
      bos.flush();
      bos.close();
    } catch (Exception e) {
      StringWriter trace = new StringWriter();
      e.printStackTrace(new PrintWriter(trace));
      report.setErrorCode(ERROR_CANNOT_GENERATE_SVG);
      report.setDescription(
          new TestReport.Entry[] {
            new TestReport.Entry(
                Messages.formatMessage(ENTRY_KEY_ERROR_DESCRIPTION, null),
                Messages.formatMessage(
                    ERROR_CANNOT_GENERATE_SVG,
                    new String[] {
                      painter == null ? "null" : painter.getClass().getName(),
                      e.getClass().getName(),
                      e.getMessage(),
                      trace.toString()
                    }))
          });
      report.setPassed(false);
      return report;
    }

    //
    // Compare with reference SVG
    //
    InputStream refStream = null;
    try {
      refStream = new BufferedInputStream(refURL.openStream());
    } catch (Exception e) {
      report.setErrorCode(ERROR_CANNOT_OPEN_REFERENCE_SVG_FILE);
      report.setDescription(
          new TestReport.Entry[] {
            new TestReport.Entry(
                Messages.formatMessage(ENTRY_KEY_ERROR_DESCRIPTION, null),
                Messages.formatMessage(
                    ERROR_CANNOT_OPEN_REFERENCE_SVG_FILE,
                    new Object[] {
                      refURL != null ? refURL.toExternalForm() : "null", e.getMessage()
                    }))
          });
      report.setPassed(false);
      save(bos.toByteArray());
      return report;
    }

    InputStream newStream = new ByteArrayInputStream(bos.toByteArray());

    boolean accurate = true;
    String refLine = null;
    String newLine = null;
    int ln = 1;

    try {
      // accurate = compare(refStream, newStream);
      BufferedReader refReader = new BufferedReader(new InputStreamReader(refStream));
      BufferedReader newReader = new BufferedReader(new InputStreamReader(newStream));
      while ((refLine = refReader.readLine()) != null) {
        newLine = newReader.readLine();
        if (newLine == null || !refLine.equals(newLine)) {
          accurate = false;
          break;
        }
        ln++;
      }

      if (accurate) {
        // need to make sure newLine is null as well
        newLine = newReader.readLine();
        if (newLine != null) {
          accurate = false;
        }
      }

    } catch (IOException e) {
      report.setErrorCode(ERROR_ERROR_WHILE_COMPARING_FILES);
      report.setDescription(
          new TestReport.Entry[] {
            new TestReport.Entry(
                Messages.formatMessage(ENTRY_KEY_ERROR_DESCRIPTION, null),
                Messages.formatMessage(
                    ERROR_ERROR_WHILE_COMPARING_FILES,
                    new Object[] {refURL.toExternalForm(), e.getMessage()}))
          });
      report.setPassed(false);
      save(bos.toByteArray());
      return report;
    }

    if (!accurate) {
      save(bos.toByteArray());
      int cn = computeColumnNumber(refLine, newLine);
      String expectedChar = "eol";
      if (cn >= 0 && refLine != null && refLine.length() > cn) {
        expectedChar = (new Character(refLine.charAt(cn))).toString();
      }
      String foundChar = "null";
      if (cn >= 0 && newLine != null && newLine.length() > cn) {
        foundChar = (new Character(newLine.charAt(cn))).toString();
      }

      if (expectedChar.equals(" ")) {
        expectedChar = "' '";
      }
      if (foundChar.equals(" ")) {
        foundChar = "' '";
      }

      report.setErrorCode(ERROR_GENERATED_SVG_INACCURATE);
      report.addDescriptionEntry(
          Messages.formatMessage(ENTRY_KEY_LINE_NUMBER, null), new Integer(ln));
      report.addDescriptionEntry(
          Messages.formatMessage(ENTRY_KEY_COLUMN_NUMBER, null), new Integer(cn));
      report.addDescriptionEntry(
          Messages.formatMessage(ENTRY_KEY_COLUMN_EXPECTED_VALUE, null), expectedChar);
      report.addDescriptionEntry(
          Messages.formatMessage(ENTRY_KEY_COLUMN_FOUND_VALUE, null), foundChar);
      report.addDescriptionEntry(Messages.formatMessage(ENTRY_KEY_REFERENCE_LINE, null), refLine);
      report.addDescriptionEntry(Messages.formatMessage(ENTRY_KEY_NEW_LINE, null), newLine);
      report.setPassed(false);
    } else {
      report.setPassed(true);
    }

    return report;
  }
  /**
   * creates the font entry:
   *
   * <PRE>
   * <font>
   * <glyph ... />
   * ...
   * </font>
   * </PRE>
   *
   * @return string representing the entry
   */
  @Override
  public String toString() {
    StringBuffer result = new StringBuffer();

    Enumeration<Font> fonts = this.glyphs.keys();
    while (fonts.hasMoreElements()) {
      Font font = fonts.nextElement();

      // replace font family for svg
      Map<Attribute, Object> attributes = FontUtilities.getAttributes(font);

      // familiy
      result.append("<font id=\"");
      result.append(FontIncluder.getFontPSName(font));
      result.append("\">\n");

      // font-face
      result.append("<font-face font-family=\"");
      result.append(
          FontUtilities.getWindowsFontFamily((String) attributes.get(TextAttribute.FAMILY)));
      result.append("\" ");

      // bold
      if (TextAttribute.WEIGHT_BOLD.equals(attributes.get(TextAttribute.WEIGHT))) {
        result.append("font-weight=\"bold\" ");
      } else {
        result.append("font-weight=\"normal\" ");
      }

      // italic
      if (TextAttribute.POSTURE_OBLIQUE.equals(attributes.get(TextAttribute.POSTURE))) {
        result.append("font-style=\"italic\" ");
      } else {
        result.append("font-style=\"normal\" ");
      }

      // size
      Float size = (Float) attributes.get(TextAttribute.SIZE);
      result.append("font-size=\"");
      result.append(SVGGraphics2D.fixedPrecision(size.floatValue()));
      result.append("\" ");

      // number of coordinate units on the em square,
      // the size of the design grid on which glyphs are laid out
      result.append("units-per-em=\"");
      result.append(SVGGraphics2D.fixedPrecision(SVGGlyph.FONT_SIZE));
      result.append("\" ");

      TextLayout tl =
          new TextLayout("By", font, new FontRenderContext(new AffineTransform(), true, true));

      // The maximum unaccented height of the font within the font coordinate system.
      // If the attribute is not specified, the effect is as if the attribute were set
      // to the difference between the units-per-em value and the vert-origin-y value
      // for the corresponding font.
      result.append("ascent=\"");
      result.append(tl.getAscent());
      result.append("\" ");

      // The maximum unaccented depth of the font within the font coordinate system.
      // If the attribute is not specified, the effect is as if the attribute were set
      // to the vert-origin-y value for the corresponding font.
      result.append("desscent=\"");
      result.append(tl.getDescent());
      result.append("\" ");

      // For horizontally oriented glyph layouts, indicates the alignment
      // coordinate for glyphs to achieve alphabetic baseline alignment.
      // result.append("alphabetic=\"0\"

      // close "<font-face"
      result.append("/>\n");

      // missing glyph
      SVGGlyph glyph = createGlyph(font.getMissingGlyphCode(), font);
      result.append("<missing-glyph ");
      result.append(glyph.getHorizontalAdvanceXString());
      result.append(" ");
      result.append(glyph.getPathString());
      result.append("/>\n");

      // regular glyphs
      Iterator<SVGGlyph> glyphs = getGlyphs(font).values().iterator();
      while (glyphs.hasNext()) {
        result.append(glyphs.next().toString());
        result.append("\n");
      }

      // close "<font>"
      result.append("</font>\n");
    }

    return result.toString();
  }
示例#5
0
  public static void main(String[] args) throws Exception {
    // Create an SVG document.
    DOMImplementation impl = SVGDOMImplementation.getDOMImplementation();
    String svgNS = SVGDOMImplementation.SVG_NAMESPACE_URI;
    SVGDocument doc = (SVGDocument) impl.createDocument(svgNS, "svg", null);

    // Create a converter for this document.
    SVGGraphics2D g = new SVGGraphics2D(doc);

    // Do some drawing.
    Shape circle = new Ellipse2D.Double(0, 0, 50, 50);
    g.setPaint(Color.red);
    g.fill(circle);
    g.translate(60, 0);
    g.setPaint(Color.green);
    g.fill(circle);
    g.translate(60, 0);
    g.setPaint(Color.blue);
    g.fill(circle);
    g.setSVGCanvasSize(new Dimension(180, 50));

    // Populate the document root with the generated SVG content.
    Element root = doc.getDocumentElement();
    g.getRoot(root);

    Writer out = new OutputStreamWriter(System.out, "UTF-8");
    g.stream(out, true);

    // Display the document.
    JSVGCanvas canvas = new JSVGCanvas();
    JFrame f = new JFrame();
    f.getContentPane().add(canvas);
    canvas.setSVGDocument(doc);
    f.pack();
    f.setVisible(true);
  }