コード例 #1
0
  public void processContent(
      final ReportElement element, final Object computedValue, final Object rawValue) {
    if (computedValue == null) {
      final StyleSheet resolvedStyle = element.getComputedStyle();
      final RenderBox parentRenderBox = this.context.getRenderBox();
      if (parentRenderBox.isEmptyNodesHaveSignificance()
          || metaData.isExtraContentElement(resolvedStyle, element.getAttributes())) {
        ensureEmptyChildIsAdded(parentRenderBox, element);
        this.context.setEmpty(false);
      }
      return;
    }

    if (String.class.equals(computedValue.getClass())) {
      processText(element, (String) computedValue, rawValue);
    } else if (computedValue instanceof Shape) {
      final StyleSheet resolvedStyle = element.getComputedStyle();
      final Shape shape = (Shape) computedValue;
      final ReportDrawable reportDrawable =
          new ShapeDrawable(
              shape, resolvedStyle.getBooleanStyleProperty(ElementStyleKeys.KEEP_ASPECT_RATIO));
      processReportDrawable(element, reportDrawable, rawValue);
    } else if (computedValue instanceof ReportDrawable) {
      processReportDrawable(element, (ReportDrawable) computedValue, rawValue);
    } else if (computedValue instanceof ImageContainer
        || computedValue instanceof DrawableWrapper) {
      processReplacedContent(element, computedValue, rawValue);
    } else if (DrawableWrapper.isDrawable(computedValue)) {
      processReplacedContent(element, new DrawableWrapper(computedValue), rawValue);
    } else {
      processText(element, String.valueOf(computedValue), rawValue);
    }
  }
コード例 #2
0
  public void draw(final Graphics2D graphics2D, final Rectangle2D bounds) {
    Graphics2D g = (Graphics2D) graphics2D.create();
    g.setColor((Color) styleSheet.getStyleProperty(ElementStyleKeys.PAINT, Color.BLACK));
    if (styleSheet.getBooleanStyleProperty(ElementStyleKeys.DRAW_SHAPE)) {
      g.draw(bounds);
    }

    if (styleSheet.getBooleanStyleProperty(ElementStyleKeys.FILL_SHAPE)) {
      Graphics2D g2 = (Graphics2D) g.create();
      Color fillColor =
          (Color) styleSheet.getStyleProperty(ElementStyleKeys.FILL_COLOR, Color.WHITE);
      g2.setColor(fillColor);
      g2.fill(bounds);
      g2.dispose();
    }

    if (vectorImageBackground != null) {
      Graphics2D g2 = (Graphics2D) g.create();
      vectorImageBackground.draw(g2, bounds);
      g2.dispose();
    }
    if (rasterImageBackground != null) {
      Graphics2D g2 = (Graphics2D) g.create();

      Image image = rasterImageBackground.getImage();
      WaitingImageObserver obs = new WaitingImageObserver(image);
      obs.waitImageLoaded();

      g.setColor(Color.WHITE);
      g.setBackground(Color.WHITE);

      while (g2.drawImage(
              image,
              (int) bounds.getX(),
              (int) bounds.getY(),
              (int) bounds.getWidth(),
              (int) bounds.getHeight(),
              null)
          == false) {
        obs.waitImageLoaded();
        if (obs.isError()) {
          logger.warn("Error while loading the image during the rendering.");
          break;
        }
      }
      g2.dispose();
    }

    if (StringUtils.isEmpty(textToPrint) == false) {
      AttributedCharacterIterator paragraph = new AttributedString(textToPrint).getIterator();
      int paragraphStart = paragraph.getBeginIndex();
      int paragraphEnd = paragraph.getEndIndex();
      FontRenderContext frc = g.getFontRenderContext();
      LineBreakMeasurer lineMeasurer = new LineBreakMeasurer(paragraph, frc);

      float breakWidth = (float) bounds.getWidth();
      float drawPosY = 0;
      // Set position to the index of the first character in the paragraph.
      lineMeasurer.setPosition(paragraphStart);

      while (lineMeasurer.getPosition() < paragraphEnd) {
        TextLayout layout = lineMeasurer.nextLayout(breakWidth).getJustifiedLayout(breakWidth);
        float drawPosX = layout.isLeftToRight() ? 0 : breakWidth - layout.getAdvance();

        drawPosY += layout.getAscent();

        layout.draw(g, drawPosX, drawPosY);

        drawPosY += layout.getDescent() + layout.getLeading();
      }
    }
  }