Package net.sf.jasperreports.engine

Examples of net.sf.jasperreports.engine.JRRenderable


        JRPrintElement element = (JRPrintElement)it.next();

        JRImageMapRenderer imageMap = null;
        if (element instanceof JRPrintImage)
        {
          JRRenderable renderer = ((JRPrintImage) element).getRenderer();
          if (renderer instanceof JRImageMapRenderer)
          {
            imageMap = (JRImageMapRenderer) renderer;
            if (!imageMap.hasImageAreaHyperlinks())
            {
View Full Code Here


   *
   * @param image the image whose renderer should be cached
   */
  public void cacheRenderer(JRPrintImage image)
  {
    JRRenderable renderer = image.getRenderer();
    if (renderer != null)
    {
      cachedRenderers.put(renderer.getId(), renderer);
    }
  }
View Full Code Here

    availableImageWidth = (availableImageWidth < 0)?0:availableImageWidth;

    int availableImageHeight = printImage.getHeight() - topPadding - bottomPadding;
    availableImageHeight = (availableImageHeight < 0)?0:availableImageHeight;

    JRRenderable renderer = printImage.getRenderer();

    if (
      renderer != null &&
      availableImageWidth > 0 &&
      availableImageHeight > 0
      )
    {
      if (renderer.getType() == JRRenderable.TYPE_IMAGE)
      {
        // Image renderers are all asked for their image data at some point.
        // Better to test and replace the renderer now, in case of lazy load error.
        renderer = JRImageRenderer.getOnErrorRendererForImageData(renderer, printImage.getOnErrorTypeValue());
      }
    }
    else
    {
      renderer = null;
    }

    if (renderer != null)
    {
      int xoffset = 0;
      int yoffset = 0;

      Chunk chunk = null;

      float scaledWidth = availableImageWidth;
      float scaledHeight = availableImageHeight;

      if (renderer.getType() == JRRenderable.TYPE_IMAGE)
      {
        com.lowagie.text.Image image = null;

        float xalignFactor = getXAlignFactor(printImage);
        float yalignFactor = getYAlignFactor(printImage);

        switch(printImage.getScaleImageValue())
        {
          case CLIP :
          {
            // Image load might fail, from given image data.
            // Better to test and replace the renderer now, in case of lazy load error.
            renderer =
              JRImageRenderer.getOnErrorRendererForDimension(
                renderer,
                printImage.getOnErrorTypeValue()
                );
            if (renderer == null)
            {
              break;
            }
           
            int normalWidth = availableImageWidth;
            int normalHeight = availableImageHeight;

            Dimension2D dimension = renderer.getDimension();
            if (dimension != null)
            {
              normalWidth = (int)dimension.getWidth();
              normalHeight = (int)dimension.getHeight();
            }

            xoffset = (int)(xalignFactor * (availableImageWidth - normalWidth));
            yoffset = (int)(yalignFactor * (availableImageHeight - normalHeight));

            int minWidth = Math.min(normalWidth, availableImageWidth);
            int minHeight = Math.min(normalHeight, availableImageHeight);

            BufferedImage bi =
              new BufferedImage(minWidth, minHeight, BufferedImage.TYPE_INT_ARGB);

            Graphics2D g = bi.createGraphics();
            if (printImage.getModeValue() == ModeEnum.OPAQUE)
            {
              g.setColor(printImage.getBackcolor());
              g.fillRect(0, 0, minWidth, minHeight);
            }
            renderer.render(
              g,
              new java.awt.Rectangle(
                (xoffset > 0 ? 0 : xoffset),
                (yoffset > 0 ? 0 : yoffset),
                normalWidth,
                normalHeight
                )
              );
            g.dispose();

            xoffset = (xoffset < 0 ? 0 : xoffset);
            yoffset = (yoffset < 0 ? 0 : yoffset);

            //awtImage = bi.getSubimage(0, 0, minWidth, minHeight);

            //image = com.lowagie.text.Image.getInstance(awtImage, printImage.getBackcolor());
            image = com.lowagie.text.Image.getInstance(bi, null);

            break;
          }
          case FILL_FRAME :
          {
            if (printImage.isUsingCache() && loadedImagesMap.containsKey(renderer))
            {
              image = (com.lowagie.text.Image)loadedImagesMap.get(renderer);
            }
            else
            {
              try
              {
                image = com.lowagie.text.Image.getInstance(renderer.getImageData());
                imageTesterPdfContentByte.addImage(image, 10, 0, 0, 10, 0, 0);
              }
              catch(Exception e)
              {
                JRImageRenderer tmpRenderer =
                  JRImageRenderer.getOnErrorRendererForImage(
                    JRImageRenderer.getInstance(renderer.getImageData()),
                    printImage.getOnErrorTypeValue()
                    );
                if (tmpRenderer == null)
                {
                  break;
                }
                java.awt.Image awtImage = tmpRenderer.getImage();
                image = com.lowagie.text.Image.getInstance(awtImage, null);
              }

              if (printImage.isUsingCache())
              {
                loadedImagesMap.put(renderer, image);
              }
            }

            image.scaleAbsolute(availableImageWidth, availableImageHeight);
            break;
          }
          case RETAIN_SHAPE :
          default :
          {
            if (printImage.isUsingCache() && loadedImagesMap.containsKey(renderer))
            {
              image = (com.lowagie.text.Image)loadedImagesMap.get(renderer);
            }
            else
            {
              try
              {
                image = com.lowagie.text.Image.getInstance(renderer.getImageData());
                imageTesterPdfContentByte.addImage(image, 10, 0, 0, 10, 0, 0);
              }
              catch(Exception e)
              {
                JRImageRenderer tmpRenderer =
                  JRImageRenderer.getOnErrorRendererForImage(
                    JRImageRenderer.getInstance(renderer.getImageData()),
                    printImage.getOnErrorTypeValue()
                    );
                if (tmpRenderer == null)
                {
                  break;
                }
                java.awt.Image awtImage = tmpRenderer.getImage();
                image = com.lowagie.text.Image.getInstance(awtImage, null);
              }

              if (printImage.isUsingCache())
              {
                loadedImagesMap.put(renderer, image);
              }
            }

            image.scaleToFit(availableImageWidth, availableImageHeight);

            xoffset = (int)(xalignFactor * (availableImageWidth - image.getPlainWidth()));
            yoffset = (int)(yalignFactor * (availableImageHeight - image.getPlainHeight()));

            xoffset = (xoffset < 0 ? 0 : xoffset);
            yoffset = (yoffset < 0 ? 0 : yoffset);

            break;
          }
        }

        if (image != null)
        {
          chunk = new Chunk(image, 0, 0);

          scaledWidth = image.getScaledWidth();
          scaledHeight = image.getScaledHeight();
        }
      }
      else
      {
        double normalWidth = availableImageWidth;
        double normalHeight = availableImageHeight;

        double displayWidth = availableImageWidth;
        double displayHeight = availableImageHeight;

        double ratioX = 1f;
        double ratioY = 1f;
       
        Rectangle2D clip = null;

        Dimension2D dimension = renderer.getDimension();
        if (dimension != null)
        {
          normalWidth = dimension.getWidth();
          normalHeight = dimension.getHeight();
          displayWidth = normalWidth;
          displayHeight = normalHeight;
         
          float xalignFactor = getXAlignFactor(printImage);
          float yalignFactor = getYAlignFactor(printImage);

          switch (printImage.getScaleImageValue())
          {
            case CLIP:
            {
              xoffset = (int) (xalignFactor * (availableImageWidth - normalWidth));
              yoffset = (int) (yalignFactor * (availableImageHeight - normalHeight));
              clip =
                new Rectangle2D.Double(
                  - xoffset,
                  - yoffset,
                  availableImageWidth,
                  availableImageHeight
                  );
              break;
            }
            case FILL_FRAME:
            {
              ratioX = availableImageWidth / normalWidth;
              ratioY = availableImageHeight / normalHeight;
              normalWidth *= ratioX;
              normalHeight *= ratioY;
              xoffset = 0;
              yoffset = 0;
              break;
            }
            case RETAIN_SHAPE:
            default:
            {
              ratioX = availableImageWidth / normalWidth;
              ratioY = availableImageHeight / normalHeight;
              ratioX = ratioX < ratioY ? ratioX : ratioY;
              ratioY = ratioX;
              normalWidth *= ratioX;
              normalHeight *= ratioY;
              xoffset = (int) (xalignFactor * (availableImageWidth - normalWidth));
              yoffset = (int) (yalignFactor * (availableImageHeight - normalHeight));
              break;
            }
          }
        }

        PdfTemplate template = pdfContentByte.createTemplate((float)displayWidth, (float)displayHeight);

        Graphics2D g = forceSvgShapes
          ? template.createGraphicsShapes((float)displayWidth, (float)displayHeight)
          : template.createGraphics(availableImageWidth, availableImageHeight, new LocalFontMapper());

        if (clip != null)
        {
          g.setClip(clip);
        }
       
        if (printImage.getModeValue() == ModeEnum.OPAQUE)
        {
          g.setColor(printImage.getBackcolor());
          g.fillRect(0, 0, (int)displayWidth, (int)displayHeight);
        }

        Rectangle2D rectangle = new Rectangle2D.Double(0, 0, displayWidth, displayHeight);

        renderer.render(g, rectangle);
        g.dispose();

        pdfContentByte.saveState();
        pdfContentByte.addTemplate(
          template,
View Full Code Here

//    int xoffset = 0;
//    int yoffset = 0;

    tableBuilder.buildCellHeader(styleCache.getCellStyle(gridCell), gridCell.getColSpan(), gridCell.getRowSpan());

    JRRenderable renderer = image.getRenderer();

    if (
      renderer != null &&
      availableImageWidth > 0 &&
      availableImageHeight > 0
      )
    {
      if (renderer.getType() == JRRenderable.TYPE_IMAGE && !image.isLazy())
      {
        // Non-lazy image renderers are all asked for their image data at some point.
        // Better to test and replace the renderer now, in case of lazy load error.
        renderer = JRImageRenderer.getOnErrorRendererForImageData(renderer, image.getOnErrorTypeValue());
      }
    }
    else
    {
      renderer = null;
    }

    if (renderer != null)
    {
//      float xalignFactor = getXAlignFactor(image);
//      float yalignFactor = getYAlignFactor(image);

      switch (image.getScaleImageValue())
      {
        case FILL_FRAME :
        {
          width = availableImageWidth;
          height = availableImageHeight;
//          xoffset = 0;
//          yoffset = 0;
          break;
        }
        case CLIP :
        case RETAIN_SHAPE :
        default :
        {
          double normalWidth = availableImageWidth;
          double normalHeight = availableImageHeight;

          if (!image.isLazy())
          {
            // Image load might fail.
            JRRenderable tmpRenderer =
              JRImageRenderer.getOnErrorRendererForDimension(renderer, image.getOnErrorTypeValue());
            Dimension2D dimension = tmpRenderer == null ? null : tmpRenderer.getDimension();
            // If renderer was replaced, ignore image dimension.
            if (tmpRenderer == renderer && dimension != null)
            {
              normalWidth = dimension.getWidth();
              normalHeight = dimension.getHeight();
View Full Code Here

    exportReportElement(image);
    exportBox(image.getLineBox());
    exportGraphicElement(image);
   

    JRRenderable renderer = image.getRenderer();
    if (renderer != null)
    {
      xmlWriter.startElement(JRXmlConstants.ELEMENT_imageSource);
      xmlWriter.addAttribute(JRXmlConstants.ATTRIBUTE_isEmbedded, isEmbeddingImages && !image.isLazy(), false);
 
      String imageSource = "";
     
      if (renderer.getType() == JRRenderable.TYPE_SVG)
      {
        renderer =
          new JRWrappingSvgRenderer(
            renderer,
            new Dimension(image.getWidth(), image.getHeight()),
            ModeEnum.OPAQUE == image.getModeValue() ? image.getBackcolor() : null
            );
      }
       
      if (image.isLazy())
      {
        imageSource = ((JRImageRenderer)renderer).getImageLocation();
      }
      else if (isEmbeddingImages)
      {
        try
        {
          ByteArrayInputStream bais = new ByteArrayInputStream(renderer.getImageData());
          ByteArrayOutputStream baos = new ByteArrayOutputStream();
         
          Base64Encoder encoder = new Base64Encoder(bais, baos);
          encoder.process();
         
          imageSource = new String(baos.toByteArray(), DEFAULT_XML_ENCODING);
        }
        catch (IOException e)
        {
          throw new JRException("Error embedding image into XML.", e);
        }
      }
      else
      {
        if (renderer.getType() == JRRenderable.TYPE_IMAGE && rendererToImagePathMap.containsKey(renderer))
        {
          imageSource = (String)rendererToImagePathMap.get(renderer);
        }
        else
        {
          imageSource = IMAGE_PREFIX + getNextImageId();
          imageNameToImageDataMap.put(imageSource, renderer.getImageData());
         
          imageSource = new File(imagesDir, imageSource).getPath();
          rendererToImagePathMap.put(renderer, imageSource);
        }
      }
View Full Code Here

          for(Iterator it = imagesToProcess.iterator(); it.hasNext();)
          {
            JRPrintElementIndex imageIndex = (JRPrintElementIndex)it.next();
 
            JRPrintImage image = getImage(jasperPrintList, imageIndex);
            JRRenderable renderer = image.getRenderer();
            if (renderer.getType() == JRRenderable.TYPE_SVG)
            {
              renderer =
                new JRWrappingSvgRenderer(
                  renderer,
                  new Dimension(image.getWidth(), image.getHeight()),
                  ModeEnum.OPAQUE == image.getModeValue() ? image.getBackcolor() : null
                  );
            }
 
            byte[] imageData = renderer.getImageData();
 
            File imageFile = new File(imagesDir, getImageName(imageIndex));
            FileOutputStream fos = null;
 
            try
View Full Code Here

      writer.write("<a name=\"");
      writer.write(image.getAnchorName());
      writer.write("\"></a>");
    }
   
    JRRenderable renderer = image.getRenderer();
    JRRenderable originalRenderer = renderer;
    boolean imageMapRenderer = renderer != null
        && renderer instanceof JRImageMapRenderer
        && ((JRImageMapRenderer) renderer).hasImageAreaHyperlinks();

    boolean hasHyperlinks = false;

    if(renderer != null)
    {
      if (imageMapRenderer)
      {
        hasHyperlinks = true;
        hyperlinkStarted = false;
      }
      else
      {
        hasHyperlinks = startHyperlink(image);
      }
     
      writer.write("<img");
      String imagePath = null;
      String imageMapName = null;
      List imageMapAreas = null;
 
      ScaleImageEnum scaleImage = image.getScaleImageValue();
     
      if (renderer != null)
      {
        if (renderer.getType() == JRRenderable.TYPE_IMAGE && rendererToImagePathMap.containsKey(renderer.getId()))
        {
          imagePath = (String)rendererToImagePathMap.get(renderer.getId());
        }
        else
        {
          if (image.isLazy())
          {
            imagePath = ((JRImageRenderer)renderer).getImageLocation();
          }
          else
          {
            JRPrintElementIndex imageIndex = getElementIndex();
            imagesToProcess.add(imageIndex);
 
            String imageName = getImageName(imageIndex);
            imagePath = imagesURI + imageName;
          }
 
          rendererToImagePathMap.put(renderer.getId(), imagePath);
        }
       
        if (imageMapRenderer)
        {
          Rectangle renderingArea = new Rectangle(image.getWidth(), image.getHeight());
         
          if (renderer.getType() == JRRenderable.TYPE_IMAGE)
          {
            imageMapName = (String) imageMaps.get(new Pair(renderer.getId(), renderingArea));
          }
 
          if (imageMapName == null)
          {
            imageMapName = "map_" + getElementIndex().toString();
            imageMapAreas = ((JRImageMapRenderer) originalRenderer).getImageAreaHyperlinks(renderingArea);//FIXMECHART
           
            if (renderer.getType() == JRRenderable.TYPE_IMAGE)
            {
              imageMaps.put(new Pair(renderer.getId(), renderingArea), imageMapName);
            }
          }
        }
      }
 
      writer.write(" src=\"");
      if (imagePath != null)
      {
        writer.write(imagePath);
      }
      writer.write("\"");
   
      int availableImageWidth = image.getWidth() - image.getLineBox().getLeftPadding().intValue() - image.getLineBox().getRightPadding().intValue();
      if (availableImageWidth < 0)
      {
        availableImageWidth = 0;
      }
   
      int availableImageHeight = image.getHeight() - image.getLineBox().getTopPadding().intValue() - image.getLineBox().getBottomPadding().intValue();
      if (availableImageHeight < 0)
      {
        availableImageHeight = 0;
      }
   
      switch (scaleImage)
      {
        case FILL_FRAME :
        {
          int leftDiff = 0;
          int topDiff = 0;
          int widthDiff = 0;
          int heightDiff = 0;

          JRLineBox box = image.getLineBox();
          if (box != null)
          {
            leftDiff = box.getLeftPadding().intValue();
            topDiff = box.getTopPadding().intValue();
            widthDiff =
              getInsideBorderOffset(box.getLeftPen().getLineWidth().floatValue())
              + getInsideBorderOffset(box.getRightPen().getLineWidth().floatValue());
            heightDiff =
              getInsideBorderOffset(box.getTopPen().getLineWidth().floatValue())
              + getInsideBorderOffset(box.getBottomPen().getLineWidth().floatValue());
          }
         
          writer.write(" style=\"position:absolute;left:");
          writer.write(toSizeUnit(leftDiff));
          writer.write(";top:");
          writer.write(toSizeUnit(topDiff));
          writer.write(";width:");
          writer.write(toSizeUnit(availableImageWidth - widthDiff));
          writer.write(";height:");
          writer.write(toSizeUnit(availableImageHeight - heightDiff));
          writer.write("\"");
   
          break;
        }
        case CLIP :
        {
          double normalWidth = availableImageWidth;
          double normalHeight = availableImageHeight;
   
          if (!image.isLazy())
          {
            // Image load might fail.
            JRRenderable tmpRenderer =
              JRImageRenderer.getOnErrorRendererForDimension(renderer, image.getOnErrorTypeValue());
            Dimension2D dimension = tmpRenderer == null ? null : tmpRenderer.getDimension();
            // If renderer was replaced, ignore image dimension.
            if (tmpRenderer == renderer && dimension != null)
            {
              normalWidth = dimension.getWidth();
              normalHeight = dimension.getHeight();
            }
          }

          int leftDiff = 0;
          int topDiff = 0;
          int widthDiff = 0;
          int heightDiff = 0;

          JRLineBox box = image.getLineBox();
          if (box != null)
          {
            leftDiff = box.getLeftPadding().intValue();
            topDiff = box.getTopPadding().intValue();
            widthDiff =
              getInsideBorderOffset(box.getLeftPen().getLineWidth().floatValue())
              + getInsideBorderOffset(box.getRightPen().getLineWidth().floatValue());
            heightDiff =
              getInsideBorderOffset(box.getTopPen().getLineWidth().floatValue())
              + getInsideBorderOffset(box.getBottomPen().getLineWidth().floatValue());
          }
         
          writer.write(" style=\"position:absolute;left:");
          writer.write(toSizeUnit((int)(leftDiff + xAlignFactor * (availableImageWidth - widthDiff - normalWidth))));
          writer.write(";top:");
          writer.write(toSizeUnit((int)(topDiff + yAlignFactor * (availableImageHeight - heightDiff - normalHeight))));
          writer.write(";width:");
          writer.write(toSizeUnit((int)normalWidth));
          writer.write(";height:");
          writer.write(toSizeUnit((int)normalHeight));
          writer.write(";clip:rect(");
          writer.write(toSizeUnit((int)(yAlignFactor * (normalHeight - availableImageHeight + heightDiff))));
          writer.write(",");
          writer.write(toSizeUnit((int)(xAlignFactor * normalWidth + (1 - xAlignFactor) * (availableImageWidth - widthDiff))));
          writer.write(",");
          writer.write(toSizeUnit((int)(yAlignFactor * normalHeight + (1 - yAlignFactor) * (availableImageHeight - heightDiff))));
          writer.write(",");
          writer.write(toSizeUnit((int)(xAlignFactor * (normalWidth - availableImageWidth + widthDiff))));
          writer.write(")\"");

          break;
        }
        case RETAIN_SHAPE :
        default :
        {
          double normalWidth = availableImageWidth;
          double normalHeight = availableImageHeight;
   
          if (!image.isLazy())
          {
            // Image load might fail.
            JRRenderable tmpRenderer =
              JRImageRenderer.getOnErrorRendererForDimension(renderer, image.getOnErrorTypeValue());
            Dimension2D dimension = tmpRenderer == null ? null : tmpRenderer.getDimension();
            // If renderer was replaced, ignore image dimension.
            if (tmpRenderer == renderer && dimension != null)
            {
              normalWidth = dimension.getWidth();
              normalHeight = dimension.getHeight();
View Full Code Here

    availableImageWidth = (availableImageWidth < 0)?0:availableImageWidth;

    int availableImageHeight = printImage.getHeight() - topPadding - bottomPadding;
    availableImageHeight = (availableImageHeight < 0)?0:availableImageHeight;
   
    JRRenderable renderer = printImage.getRenderer();
   
    if (
      renderer != null &&
      availableImageWidth > 0 &&
      availableImageHeight > 0
      )
    {
      if (renderer.getType() == JRRenderable.TYPE_IMAGE)
      {
        // Image renderers are all asked for their image data and dimension at some point.
        // Better to test and replace the renderer now, in case of lazy load error.
        renderer = JRImageRenderer.getOnErrorRendererForImageData(renderer, printImage.getOnErrorTypeValue());
        if (renderer != null)
        {
          renderer = JRImageRenderer.getOnErrorRendererForDimension(renderer, printImage.getOnErrorTypeValue());
        }
      }
    }
    else
    {
      renderer = null;
    }

    if (renderer != null)
    {
      int normalWidth = availableImageWidth;
      int normalHeight = availableImageHeight;

      Dimension2D dimension = renderer.getDimension();
      if (dimension != null)
      {
        normalWidth = (int)dimension.getWidth();
        normalHeight = (int)dimension.getHeight();
      }
 
      float xalignFactor = 0f;
      switch (printImage.getHorizontalAlignmentValue())
      {
        case RIGHT :
        {
          xalignFactor = 1f;
          break;
        }
        case CENTER :
        {
          xalignFactor = 0.5f;
          break;
        }
        case LEFT :
        default :
        {
          xalignFactor = 0f;
          break;
        }
      }

      float yalignFactor = 0f;
      switch (printImage.getVerticalAlignmentValue())
      {
        case BOTTOM :
        {
          yalignFactor = 1f;
          break;
        }
        case MIDDLE :
        {
          yalignFactor = 0.5f;
          break;
        }
        case TOP :
        default :
        {
          yalignFactor = 0f;
          break;
        }
      }

      switch (printImage.getScaleImageValue())// FIXME maybe put this in JRFiller
      {
        case CLIP :
        {
          int xoffset = (int)(xalignFactor * (availableImageWidth - normalWidth));
          int yoffset = (int)(yalignFactor * (availableImageHeight - normalHeight));

          Shape oldClipShape = grx.getClip();

          grx.clip(
            new Rectangle(
              printImage.getX() + leftPadding + offsetX,
              printImage.getY() + topPadding + offsetY,
              availableImageWidth,
              availableImageHeight
              )
            );
         
          try
          {
            renderer.render(
              grx,
              new Rectangle(
                printImage.getX() + leftPadding + offsetX + xoffset,
                printImage.getY() + topPadding + offsetY + yoffset,
                normalWidth,
                normalHeight
                )
              );
          }
          finally
          {
            grx.setClip(oldClipShape);
          }
 
          break;
        }
        case FILL_FRAME :
        {
          renderer.render(
            grx,
            new Rectangle(
              printImage.getX() + leftPadding + offsetX,
              printImage.getY() + topPadding + offsetY,
              availableImageWidth,
              availableImageHeight
              )
            );
 
          break;
        }
        case RETAIN_SHAPE :
        default :
        {
          if (printImage.getHeight() > 0)
          {
            double ratio = (double)normalWidth / (double)normalHeight;
           
            if( ratio > (double)availableImageWidth / (double)availableImageHeight )
            {
              normalWidth = availableImageWidth;
              normalHeight = (int)(availableImageWidth / ratio);
            }
            else
            {
              normalWidth = (int)(availableImageHeight * ratio);
              normalHeight = availableImageHeight;
            }

            int xoffset = (int)(xalignFactor * (availableImageWidth - normalWidth));
            int yoffset = (int)(yalignFactor * (availableImageHeight - normalHeight));

            renderer.render(
              grx,
              new Rectangle(
                printImage.getX() + leftPadding + offsetX + xoffset,
                printImage.getY() + topPadding + offsetY + yoffset,
                normalWidth,
View Full Code Here

    availableImageWidth = availableImageWidth < 0 ? 0 : availableImageWidth;

    int availableImageHeight = element.getHeight() - topPadding - bottomPadding;
    availableImageHeight = availableImageHeight < 0 ? 0 : availableImageHeight;

    JRRenderable renderer = element.getRenderer();

    if (
      renderer != null &&
      availableImageWidth > 0 &&
      availableImageHeight > 0
      )
    {
      if (renderer.getType() == JRRenderable.TYPE_IMAGE)
      {
        // Image renderers are all asked for their image data and dimension at some point.
        // Better to test and replace the renderer now, in case of lazy load error.
        renderer = JRImageRenderer.getOnErrorRendererForImageData(renderer, element.getOnErrorTypeValue());
        if (renderer != null)
        {
          renderer = JRImageRenderer.getOnErrorRendererForDimension(renderer, element.getOnErrorTypeValue());
        }
      }
      else
      {
        renderer =
          new JRWrappingSvgRenderer(
            renderer,
            new Dimension(element.getWidth(), element.getHeight()),
            ModeEnum.OPAQUE == element.getModeValue() ? element.getBackcolor() : null
            );
      }
    }
    else
    {
      renderer = null;
    }

    if (renderer != null)
    {
      int normalWidth = availableImageWidth;
      int normalHeight = availableImageHeight;

      Dimension2D dimension = renderer.getDimension();
      if (dimension != null)
      {
        normalWidth = (int) dimension.getWidth();
        normalHeight = (int) dimension.getHeight();
      }

      float xalignFactor = 0f;
      switch (element.getHorizontalAlignmentValue())
      {
        case RIGHT:
        {
          xalignFactor = 1f;
          break;
        }
        case CENTER:
        {
          xalignFactor = 0.5f;
          break;
        }
        case LEFT:
        default:
        {
          xalignFactor = 0f;
          break;
        }
      }

      float yalignFactor = 0f;
      switch (element.getVerticalAlignmentValue())
      {
        case BOTTOM:
        {
          yalignFactor = 1f;
          break;
        }
        case MIDDLE:
        {
          yalignFactor = 0.5f;
          break;
        }
        case TOP:
        default:
        {
          yalignFactor = 0f;
          break;
        }
      }
     
      byte[] imageData = null;
      int topOffset = 0;
      int leftOffset = 0;
      int bottomOffset = 0;
      int rightOffset = 0;
     
      switch (element.getScaleImageValue())
      {
        case CLIP:
        {
          BufferedImage bi = new BufferedImage(availableImageWidth, availableImageHeight, BufferedImage.TYPE_INT_ARGB);
          Graphics2D grx = bi.createGraphics();
         
          int xoffset = (int) (xalignFactor * (availableImageWidth - normalWidth));
          int yoffset = (int) (yalignFactor * (availableImageHeight - normalHeight));

          Shape oldClipShape = grx.getClip();

          grx.clip(
            new Rectangle(
              0,
              0,
              availableImageWidth,
              availableImageHeight
              )
            );
         
          try
          {
            renderer.render(
              grx,
              new Rectangle(
                xoffset,
                yoffset,
                normalWidth,
                normalHeight
                )
              );
          }
          finally
          {
            grx.setClip(oldClipShape);
          }

          topOffset = topPadding;
          leftOffset = leftPadding;
          bottomOffset = bottomPadding;
          rightOffset = rightPadding;

          imageData = JRImageLoader.loadImageDataFromAWTImage(bi, JRRenderable.IMAGE_TYPE_PNG);

          break;
        }
        case FILL_FRAME:
        {
          topOffset = topPadding;
          leftOffset = leftPadding;
          bottomOffset = bottomPadding;
          rightOffset = rightPadding;

          imageData = renderer.getImageData();

          break;
        }
        case RETAIN_SHAPE:
        default:
        {
          if (element.getHeight() > 0)
          {
            double ratio = (double) normalWidth / (double) normalHeight;

            if (ratio > (double) availableImageWidth / (double) availableImageHeight)
            {
              normalWidth = availableImageWidth;
              normalHeight = (int) (availableImageWidth / ratio);
            }
            else
            {
              normalWidth = (int) (availableImageHeight * ratio);
              normalHeight = availableImageHeight;
            }

            topOffset = topPadding + (int) (yalignFactor * (availableImageHeight - normalHeight));
            leftOffset = leftPadding + (int) (xalignFactor * (availableImageWidth - normalWidth));
            bottomOffset = bottomPadding + (int) ((1f - yalignFactor) * (availableImageHeight - normalHeight));
            rightOffset = rightPadding + (int) ((1f - xalignFactor) * (availableImageWidth - normalWidth));

            imageData = renderer.getImageData();
          }

          break;
        }
      }
View Full Code Here

    JRBasePrintImage printImage = new JRBasePrintImage(
        reportConverter.getDefaultStyleProvider());
    reportConverter.copyBaseAttributes(element, printImage);
    printImage.setScaleImage(ScaleImageEnum.RETAIN_SHAPE);
   
    JRRenderable barcodeImage = evaluateBarcode(reportConverter, element);
    printImage.setRenderer(barcodeImage);
   
    return printImage;
  }
View Full Code Here

TOP

Related Classes of net.sf.jasperreports.engine.JRRenderable

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.