Esempio n. 1
0
  private BufferedImage getImageIcon(String fileName) throws KettleException {
    InputStream inputStream = null;
    try {
      BufferedImage image = ImageIO.read(new File(fileName));
      if (image == null) {
        image = ImageIO.read(new File("/" + fileName));
      }
      if (image == null) {
        inputStream = getClass().getResourceAsStream(fileName);
        if (inputStream == null) {
          inputStream = getClass().getResourceAsStream("/" + fileName);
        }
        if (inputStream == null) {
          throw new KettleException("Unable to load image from file : '" + fileName + "'");
        }
        image = ImageIO.read(inputStream);
      }

      WaitingImageObserver observer = new WaitingImageObserver(image);
      observer.waitImageLoaded();

      return image;
    } catch (Throwable e) {
      throw new KettleException("Unable to load image from file : '" + fileName + "'", e);
    } finally {
      if (inputStream != null) {
        try {
          inputStream.close();
        } catch (IOException e) {
          throw new KettleException("Unable to close image reading stream", e);
        }
      }
    }
  }
  protected boolean drawImage(
      final RenderableReplacedContentBox content,
      final Image image,
      final com.lowagie.text.Image itextImage) {
    final StyleSheet layoutContext = content.getStyleSheet();
    final boolean shouldScale = layoutContext.getBooleanStyleProperty(ElementStyleKeys.SCALE);

    final int x = (int) StrictGeomUtility.toExternalValue(content.getX());
    final int y = (int) StrictGeomUtility.toExternalValue(content.getY());
    final int width = (int) StrictGeomUtility.toExternalValue(content.getWidth());
    final int height = (int) StrictGeomUtility.toExternalValue(content.getHeight());

    if (width == 0 || height == 0) {
      PdfLogicalPageDrawable.logger.debug("Error: Image area is empty: " + content);
      return false;
    }

    final WaitingImageObserver obs = new WaitingImageObserver(image);
    obs.waitImageLoaded();
    final int imageWidth = image.getWidth(obs);
    final int imageHeight = image.getHeight(obs);
    if (imageWidth < 1 || imageHeight < 1) {
      return false;
    }

    final Rectangle2D.Double drawAreaBounds = new Rectangle2D.Double(x, y, width, height);
    final AffineTransform scaleTransform;

    final Graphics2D g2;
    if (shouldScale == false) {
      double deviceScaleFactor = 1;
      final double devResolution =
          getMetaData().getNumericFeatureValue(OutputProcessorFeature.DEVICE_RESOLUTION);
      if (getMetaData().isFeatureSupported(OutputProcessorFeature.IMAGE_RESOLUTION_MAPPING)) {
        if (devResolution != 72.0 && devResolution > 0) {
          // Need to scale the device to its native resolution before attempting to draw the image..
          deviceScaleFactor = (72.0 / devResolution);
        }
      }

      final int clipWidth = Math.min(width, (int) Math.ceil(deviceScaleFactor * imageWidth));
      final int clipHeight = Math.min(height, (int) Math.ceil(deviceScaleFactor * imageHeight));
      final ElementAlignment horizontalAlignment =
          (ElementAlignment) layoutContext.getStyleProperty(ElementStyleKeys.ALIGNMENT);
      final ElementAlignment verticalAlignment =
          (ElementAlignment) layoutContext.getStyleProperty(ElementStyleKeys.VALIGNMENT);
      final int alignmentX =
          (int) RenderUtility.computeHorizontalAlignment(horizontalAlignment, width, clipWidth);
      final int alignmentY =
          (int) RenderUtility.computeVerticalAlignment(verticalAlignment, height, clipHeight);

      g2 = (Graphics2D) getGraphics().create();
      g2.clip(drawAreaBounds);
      g2.translate(x, y);
      g2.translate(alignmentX, alignmentY);
      g2.clip(new Rectangle2D.Float(0, 0, clipWidth, clipHeight));
      g2.scale(deviceScaleFactor, deviceScaleFactor);

      scaleTransform = null;
    } else {
      g2 = (Graphics2D) getGraphics().create();
      g2.clip(drawAreaBounds);
      g2.translate(x, y);
      g2.clip(new Rectangle2D.Float(0, 0, width, height));

      final double scaleX;
      final double scaleY;

      final boolean keepAspectRatio =
          layoutContext.getBooleanStyleProperty(ElementStyleKeys.KEEP_ASPECT_RATIO);
      if (keepAspectRatio) {
        final double scaleFactor =
            Math.min(width / (double) imageWidth, height / (double) imageHeight);
        scaleX = scaleFactor;
        scaleY = scaleFactor;
      } else {
        scaleX = width / (double) imageWidth;
        scaleY = height / (double) imageHeight;
      }

      final int clipWidth = (int) (scaleX * imageWidth);
      final int clipHeight = (int) (scaleY * imageHeight);

      final ElementAlignment horizontalAlignment =
          (ElementAlignment) layoutContext.getStyleProperty(ElementStyleKeys.ALIGNMENT);
      final ElementAlignment verticalAlignment =
          (ElementAlignment) layoutContext.getStyleProperty(ElementStyleKeys.VALIGNMENT);
      final int alignmentX =
          (int) RenderUtility.computeHorizontalAlignment(horizontalAlignment, width, clipWidth);
      final int alignmentY =
          (int) RenderUtility.computeVerticalAlignment(verticalAlignment, height, clipHeight);

      g2.translate(alignmentX, alignmentY);
      scaleTransform = AffineTransform.getScaleInstance(scaleX, scaleY);
    }

    final PdfGraphics2D pdfGraphics2D = (PdfGraphics2D) g2;
    pdfGraphics2D.drawPdfImage(itextImage, image, scaleTransform, null);
    g2.dispose();
    return true;
  }
  /**
   * Helperfunction to extract an image from an imagereference. If the image is contained as
   * java.awt.Image object only, the image is recoded into an PNG-Image.
   *
   * @param reference the image reference.
   * @return an image.
   * @throws com.lowagie.text.DocumentException if no PDFImageElement could be created using the
   *     given ImageReference.
   * @throws java.io.IOException if the image could not be read.
   */
  public Image getImage(final ImageContainer reference) throws DocumentException, IOException {
    if (reference == null) {
      throw new NullPointerException();
    }

    Object identity = null;
    java.awt.Image image = null;
    if (reference instanceof URLImageContainer) {
      final URLImageContainer urlImageContainer = (URLImageContainer) reference;
      final ResourceKey url = urlImageContainer.getResourceKey();
      if (url != null && urlImageContainer.isLoadable()) {
        identity = url;
        final Image cached = (Image) cachedImages.get(identity);
        if (cached != null) {
          return cached;
        }

        try {
          final ResourceData resourceData = resourceManager.load(url);
          final byte[] data = resourceData.getResource(resourceManager);
          final Image itextimage = Image.getInstance(data);
          cachedImages.put(identity, itextimage);
          return itextimage;
        } catch (ResourceException re) {
          RTFImageCache.logger.info("Caught illegal Image, will recode to PNG instead", re);
        } catch (BadElementException be) {
          RTFImageCache.logger.info("Caught illegal Image, will recode to PNG instead", be);
        } catch (IOException ioe) {
          RTFImageCache.logger.info(
              "Unable to read the raw-data, will try to recode image-data.", ioe);
        }

        try {
          final Resource resource = resourceManager.create(url, null, Image.class);
          image = (java.awt.Image) resource.getResource();
        } catch (ResourceException re) {
          RTFImageCache.logger.info("Caught illegal Image, will try to find local instance", re);
        }
      }
    }

    if (reference instanceof LocalImageContainer && image == null) {
      final LocalImageContainer localImageContainer = (LocalImageContainer) reference;
      image = localImageContainer.getImage();
      if (image != null) {
        // check, if the content was cached ...
        identity = localImageContainer.getIdentity();
        if (identity != null) {
          final Image cachedImage = (Image) cachedImages.get(identity);
          if (cachedImage != null) {
            return cachedImage;
          }
        }
      }
    }
    if (image == null) {
      return null;
    }

    final WaitingImageObserver obs = new WaitingImageObserver(image);
    obs.waitImageLoaded();

    try {
      final byte[] data = RenderUtility.encodeImage(image);
      final Image itextimage = Image.getInstance(data);
      if (identity != null) {
        cachedImages.put(identity, itextimage);
      }
      return itextimage;
    } catch (UnsupportedEncoderException uee) {
      logger.warn("Assertation-Failure: PNG encoding failed.", uee);
      return null;
    }
  }
  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();
      }
    }
  }