Example usage for java.awt.geom Dimension2D getHeight

List of usage examples for java.awt.geom Dimension2D getHeight

Introduction

In this page you can find the example usage for java.awt.geom Dimension2D getHeight.

Prototype

public abstract double getHeight();

Source Link

Document

Returns the height of this Dimension in double precision.

Usage

From source file:com.siteview.ecc.report.xls.JRXlsExporter.java

protected void exportImage(JRPrintImage element, JRExporterGridCell gridCell, int colIndex, int rowIndex,
        int emptyCols) throws JRException {
    try {/*from   w  w  w.j  a va 2s . c o m*/
        int topPadding = Math.max(element.getLineBox().getTopPadding().intValue(),
                getImageBorderCorrection(element.getLineBox().getTopPen()));
        int leftPadding = Math.max(element.getLineBox().getLeftPadding().intValue(),
                getImageBorderCorrection(element.getLineBox().getLeftPen()));
        int bottomPadding = Math.max(element.getLineBox().getBottomPadding().intValue(),
                getImageBorderCorrection(element.getLineBox().getBottomPen()));
        int rightPadding = Math.max(element.getLineBox().getRightPadding().intValue(),
                getImageBorderCorrection(element.getLineBox().getRightPen()));

        //pngEncoder.setImage( null );

        int availableImageWidth = element.getWidth() - leftPadding - rightPadding;
        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.getOnErrorType());
                if (renderer != null) {
                    renderer = JRImageRenderer.getOnErrorRendererForDimension(renderer,
                            element.getOnErrorType());
                }
            }
        } 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.getHorizontalAlignment()) {
            case JRAlignment.HORIZONTAL_ALIGN_RIGHT: {
                xalignFactor = 1f;
                break;
            }
            case JRAlignment.HORIZONTAL_ALIGN_CENTER: {
                xalignFactor = 0.5f;
                break;
            }
            case JRAlignment.HORIZONTAL_ALIGN_LEFT:
            default: {
                xalignFactor = 0f;
                break;
            }
            }

            float yalignFactor = 0f;
            switch (element.getVerticalAlignment()) {
            case JRAlignment.VERTICAL_ALIGN_BOTTOM: {
                yalignFactor = 1f;
                break;
            }
            case JRAlignment.VERTICAL_ALIGN_MIDDLE: {
                yalignFactor = 0.5f;
                break;
            }
            case JRAlignment.VERTICAL_ALIGN_TOP:
            default: {
                yalignFactor = 0f;
                break;
            }
            }

            BufferedImage bi = new BufferedImage(element.getWidth(), element.getHeight(),
                    BufferedImage.TYPE_INT_ARGB);
            Graphics2D grx = bi.createGraphics();

            switch (element.getScaleImage()) {
            case JRImage.SCALE_IMAGE_CLIP: {
                int xoffset = (int) (xalignFactor * (availableImageWidth - normalWidth));
                int yoffset = (int) (yalignFactor * (availableImageHeight - normalHeight));

                Shape oldClipShape = grx.getClip();

                grx.clip(new Rectangle(leftPadding, topPadding, availableImageWidth, availableImageHeight));

                try {
                    renderer.render(grx, new Rectangle(xoffset + leftPadding, yoffset + topPadding, normalWidth,
                            normalHeight));
                } finally {
                    grx.setClip(oldClipShape);
                }

                break;
            }
            case JRImage.SCALE_IMAGE_FILL_FRAME: {
                renderer.render(grx,
                        new Rectangle(leftPadding, topPadding, availableImageWidth, availableImageHeight));

                break;
            }
            case JRImage.SCALE_IMAGE_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;
                    }

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

                    renderer.render(grx, new Rectangle(xoffset, yoffset, normalWidth, normalHeight));
                }

                break;
            }
            }

            short mode = backgroundMode;
            short backcolor = whiteIndex;
            if (!isIgnoreCellBackground && gridCell.getCellBackcolor() != null) {
                mode = HSSFCellStyle.SOLID_FOREGROUND;
                backcolor = getNearestColor(gridCell.getCellBackcolor()).getIndex();
            }

            short forecolor = getNearestColor(element.getLineBox().getPen().getLineColor()).getIndex();

            if (element.getMode() == JRElement.MODE_OPAQUE) {
                backcolor = getNearestColor(element.getBackcolor()).getIndex();
            }

            HSSFCellStyle cellStyle = getLoadedCellStyle(mode, backcolor, HSSFCellStyle.ALIGN_LEFT,
                    HSSFCellStyle.VERTICAL_TOP, (short) 0,
                    getLoadedFont(getDefaultFont(), forecolor, null, getLocale()), gridCell);

            createMergeRegion(gridCell, colIndex, rowIndex, cellStyle);

            cell = row.createCell(colIndex);
            //            cell.setCellStyle(cellStyle);

            HSSFClientAnchor anchor = new HSSFClientAnchor(0, 0, 0, 0, (short) colIndex, rowIndex,
                    (short) (colIndex + gridCell.getColSpan()),
                    rowIndex + (isCollapseRowSpan ? 1 : gridCell.getRowSpan()));
            anchor.setAnchorType(2);
            //pngEncoder.setImage(bi);
            //int imgIndex = workbook.addPicture(pngEncoder.pngEncode(), HSSFWorkbook.PICTURE_TYPE_PNG);
            int imgIndex = workbook.addPicture(
                    JRImageLoader.loadImageDataFromAWTImage(bi, JRRenderable.IMAGE_TYPE_PNG),
                    HSSFWorkbook.PICTURE_TYPE_PNG);
            patriarch.createPicture(anchor, imgIndex);
        }
    } catch (Exception ex) {
        throw new JRException("The cell cannot be added", ex);
    } catch (Error err) {
        throw new JRException("The cell cannot be added", err);
    }
}

From source file:net.sf.jasperreports.engine.export.ooxml.JRDocxExporter.java

/**
 *
 *//*from ww w  . j a v a  2  s. co  m*/
public void exportImage(DocxTableHelper tableHelper, JRPrintImage image, JRExporterGridCell gridCell)
        throws JRException {
    int leftPadding = image.getLineBox().getLeftPadding();
    int topPadding = image.getLineBox().getTopPadding();//FIXMEDOCX maybe consider border thickness
    int rightPadding = image.getLineBox().getRightPadding();
    int bottomPadding = image.getLineBox().getBottomPadding();

    int availableImageWidth = image.getWidth() - leftPadding - rightPadding;
    availableImageWidth = availableImageWidth < 0 ? 0 : availableImageWidth;

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

    tableHelper.getCellHelper().exportHeader(image, gridCell);

    docHelper.write("<w:p>\n");//FIXMEDOCX why is this here and not further down?
    tableHelper.getParagraphHelper().exportProps(image);

    Renderable renderer = image.getRenderer();

    if (renderer != null && availableImageWidth > 0 && availableImageHeight > 0) {
        InternalImageProcessor imageProcessor = new InternalImageProcessor(image,
                image.getScaleImageValue() != ScaleImageEnum.FILL_FRAME, gridCell, availableImageWidth,
                availableImageHeight);

        InternalImageProcessorResult imageProcessorResult = null;

        try {
            imageProcessorResult = imageProcessor.process(renderer);
        } catch (Exception e) {
            Renderable onErrorRenderer = getRendererUtil().handleImageError(e, image.getOnErrorTypeValue());
            if (onErrorRenderer != null) {
                imageProcessorResult = imageProcessor.process(onErrorRenderer);
            }
        }

        if (imageProcessorResult != null) {
            int width = availableImageWidth;
            int height = availableImageHeight;

            double cropTop = 0;
            double cropLeft = 0;
            double cropBottom = 0;
            double cropRight = 0;

            switch (image.getScaleImageValue()) {
            case FILL_FRAME: {
                width = availableImageWidth;
                height = availableImageHeight;
                break;
            }
            case CLIP: {
                double normalWidth = availableImageWidth;
                double normalHeight = availableImageHeight;

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

                if (normalWidth > availableImageWidth) {
                    switch (image.getHorizontalImageAlign()) {
                    case RIGHT: {
                        cropLeft = 65536 * (normalWidth - availableImageWidth) / normalWidth;
                        cropRight = 0;
                        break;
                    }
                    case CENTER: {
                        cropLeft = 65536 * (-availableImageWidth + normalWidth) / normalWidth / 2;
                        cropRight = cropLeft;
                        break;
                    }
                    case LEFT:
                    default: {
                        cropLeft = 0;
                        cropRight = 65536 * (normalWidth - availableImageWidth) / normalWidth;
                        break;
                    }
                    }
                    width = availableImageWidth;
                    cropLeft = cropLeft / 0.75d;
                    cropRight = cropRight / 0.75d;
                } else {
                    width = (int) normalWidth;
                }

                if (normalHeight > availableImageHeight) {
                    switch (image.getVerticalImageAlign()) {
                    case TOP: {
                        cropTop = 0;
                        cropBottom = 65536 * (normalHeight - availableImageHeight) / normalHeight;
                        break;
                    }
                    case MIDDLE: {
                        cropTop = 65536 * (normalHeight - availableImageHeight) / normalHeight / 2;
                        cropBottom = cropTop;
                        break;
                    }
                    case BOTTOM:
                    default: {
                        cropTop = 65536 * (normalHeight - availableImageHeight) / normalHeight;
                        cropBottom = 0;
                        break;
                    }
                    }
                    height = availableImageHeight;
                    cropTop = cropTop / 0.75d;
                    cropBottom = cropBottom / 0.75d;
                } else {
                    height = (int) normalHeight;
                }

                break;
            }
            case RETAIN_SHAPE:
            default: {
                double normalWidth = availableImageWidth;
                double normalHeight = availableImageHeight;

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

                double ratio = normalWidth / normalHeight;

                if (ratio > availableImageWidth / (double) availableImageHeight) {
                    width = availableImageWidth;
                    height = (int) (width / ratio);

                } else {
                    height = availableImageHeight;
                    width = (int) (ratio * height);
                }
            }
            }

            if (startPage) {
                insertBookmark(pageAnchor, docHelper);
            }
            if (image.getAnchorName() != null) {
                insertBookmark(image.getAnchorName(), docHelper);
            }

            //            boolean startedHyperlink = startHyperlink(image,false);

            docHelper.write("<w:r>\n");
            docHelper.write("<w:rPr/>\n");
            docHelper.write("<w:drawing>\n");
            docHelper.write("<wp:inline distT=\"0\" distB=\"0\" distL=\"0\" distR=\"0\">\n");
            docHelper.write("<wp:extent cx=\"" + LengthUtil.emu(width) + "\" cy=\"" + LengthUtil.emu(height)
                    + "\"/>\n");
            docHelper.write("<wp:effectExtent l=\"0\" t=\"0\" r=\"0\" b=\"0\"/>\n");

            int imageId = image.hashCode() > 0 ? image.hashCode() : -image.hashCode();
            String rId = IMAGE_LINK_PREFIX + getElementIndex(gridCell);
            docHelper.write("<wp:docPr id=\"" + imageId + "\" name=\"Picture\">\n");
            if (getHyperlinkURL(image) != null) {
                docHelper.write(
                        "<a:hlinkClick xmlns:a=\"http://schemas.openxmlformats.org/drawingml/2006/main\" r:id=\""
                                + rId + "\"/>\n");
            }
            docHelper.write("</wp:docPr>\n");
            docHelper.write("<a:graphic>\n");
            docHelper.write(
                    "<a:graphicData uri=\"http://schemas.openxmlformats.org/drawingml/2006/picture\">\n");
            docHelper.write("<pic:pic>\n");
            docHelper.write("<pic:nvPicPr><pic:cNvPr id=\"" + imageId
                    + "\" name=\"Picture\"/><pic:cNvPicPr/></pic:nvPicPr>\n");
            docHelper.write("<pic:blipFill>\n");

            docHelper.write("<a:blip r:embed=\"" + imageProcessorResult.imagePath + "\"/>");
            docHelper.write("<a:srcRect");
            if (cropLeft > 0) {
                docHelper.write(" l=\"" + (int) cropLeft + "\"");
            }
            if (cropTop > 0) {
                docHelper.write(" t=\"" + (int) cropTop + "\"");
            }
            if (cropRight > 0) {
                docHelper.write(" r=\"" + (int) cropRight + "\"");
            }
            if (cropBottom > 0) {
                docHelper.write(" b=\"" + (int) cropBottom + "\"");
            }
            docHelper.write("/>");
            docHelper.write("<a:stretch><a:fillRect/></a:stretch>\n");
            docHelper.write("</pic:blipFill>\n");
            docHelper.write("<pic:spPr><a:xfrm><a:off x=\"0\" y=\"0\"/><a:ext cx=\"" + LengthUtil.emu(width)
                    + "\" cy=\"" + LengthUtil.emu(height) + "\"/>");
            docHelper.write("</a:xfrm><a:prstGeom prst=\"rect\"></a:prstGeom></pic:spPr>\n");
            docHelper.write("</pic:pic>\n");
            docHelper.write("</a:graphicData>\n");
            docHelper.write("</a:graphic>\n");
            docHelper.write("</wp:inline>\n");
            docHelper.write("</w:drawing>\n");
            docHelper.write("</w:r>");

            String url = getHyperlinkURL(image);

            if (url != null) {
                String targetMode = "";
                switch (image.getHyperlinkTypeValue()) {
                case LOCAL_PAGE:
                case LOCAL_ANCHOR: {
                    relsHelper.exportImageLink(rId, "#" + url.replaceAll("\\W", ""), targetMode);
                    break;
                }

                case REMOTE_PAGE:
                case REMOTE_ANCHOR:
                case REFERENCE: {
                    targetMode = " TargetMode=\"External\"";
                    relsHelper.exportImageLink(rId, url, targetMode);
                    break;
                }
                default: {
                    break;
                }
                }
            }

            //            if (startedHyperlink)
            //            {
            //               endHyperlink(false);
            //            }
        }
    }

    docHelper.write("</w:p>");

    tableHelper.getCellHelper().exportFooter();
}

From source file:net.sf.jasperreports.engine.export.JRRtfExporter.java

/**
 * Export a image object//from w ww  .ja va2  s.  com
 * @param printImage JasperReports image object (JRPrintImage)
 * @throws JRException
 * @throws IOException
 */
public void exportImage(JRPrintImage printImage) throws JRException, IOException {
    int leftPadding = printImage.getLineBox().getLeftPadding();
    int topPadding = printImage.getLineBox().getTopPadding();
    int rightPadding = printImage.getLineBox().getRightPadding();
    int bottomPadding = printImage.getLineBox().getBottomPadding();

    int availableImageWidth = printImage.getWidth() - leftPadding - rightPadding;
    availableImageWidth = availableImageWidth < 0 ? 0 : availableImageWidth;

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

    Renderable renderer = printImage.getRenderer();

    if (renderer != null && availableImageWidth > 0 && availableImageHeight > 0) {
        InternalImageProcessor imageProcessor = new InternalImageProcessor(printImage,
                printImage.getScaleImageValue() != ScaleImageEnum.FILL_FRAME, availableImageWidth,
                availableImageHeight);

        InternalImageProcessorResult imageProcessorResult = null;

        try {
            imageProcessorResult = imageProcessor.process(renderer);
        } catch (Exception e) {
            Renderable onErrorRenderer = getRendererUtil().handleImageError(e,
                    printImage.getOnErrorTypeValue());
            if (onErrorRenderer != null) {
                imageProcessorResult = imageProcessor.process(onErrorRenderer);
            }
        }

        if (imageProcessorResult != null)//FIXMERTF draw image background for null images, like the other exporters do
        {
            int imageWidth = 0;
            int imageHeight = 0;
            int xoffset = 0;
            int yoffset = 0;
            int cropTop = 0;
            int cropLeft = 0;
            int cropBottom = 0;
            int cropRight = 0;

            switch (printImage.getScaleImageValue()) {
            case CLIP: {
                int normalWidth = availableImageWidth;
                int normalHeight = availableImageHeight;

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

                switch (printImage.getHorizontalImageAlign()) {
                case RIGHT: {
                    cropLeft = 65536 * (-availableImageWidth + normalWidth) / availableImageWidth;
                    cropRight = 0;
                    break;
                }
                case CENTER: {
                    cropLeft = 65536 * (-availableImageWidth + normalWidth) / availableImageWidth / 2;
                    cropRight = cropLeft;
                    break;
                }
                case LEFT:
                default: {
                    cropLeft = 0;
                    cropRight = 65536 * (-availableImageWidth + normalWidth) / availableImageWidth;
                    break;
                }
                }
                switch (printImage.getVerticalImageAlign()) {
                case TOP: {
                    cropTop = 0;
                    cropBottom = 65536 * (-availableImageHeight + normalHeight) / normalHeight;
                    break;
                }
                case MIDDLE: {
                    cropTop = 65536 * (-availableImageHeight + normalHeight) / normalHeight / 2;
                    cropBottom = cropTop;
                    break;
                }
                case BOTTOM:
                default: {
                    cropTop = 65536 * (-availableImageHeight + normalHeight) / normalHeight;
                    cropBottom = 0;
                    break;
                }
                }
                imageWidth = availableImageWidth;
                imageHeight = availableImageHeight;
                break;
            }
            case FILL_FRAME: {
                imageWidth = availableImageWidth;
                imageHeight = availableImageHeight;
                break;
            }
            case RETAIN_SHAPE:
            default: {
                int normalWidth = availableImageWidth;
                int normalHeight = availableImageHeight;

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

                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;
                }

                xoffset = (int) (ImageUtil.getXAlignFactor(printImage) * (availableImageWidth - normalWidth));
                yoffset = (int) (ImageUtil.getYAlignFactor(printImage) * (availableImageHeight - normalHeight));
                imageWidth = normalWidth;
                imageHeight = normalHeight;

                break;
            }
            }

            startElement(printImage);
            exportPen(printImage.getForecolor());//FIXMEBORDER should we have lineColor here, if at all needed?
            finishElement();
            boolean startedHyperlink = exportHyperlink(printImage);

            contentWriter
                    .write("{\\shp{\\*\\shpinst\\shpbxpage\\shpbypage\\shpwr5\\shpfhdr0\\shpfblwtxt0\\shpz");
            contentWriter.write(String.valueOf(zorder++));
            contentWriter.write("\\shpleft");
            contentWriter.write(
                    String.valueOf(LengthUtil.twip(printImage.getX() + leftPadding + xoffset + getOffsetX())));
            contentWriter.write("\\shpright");
            contentWriter.write(String.valueOf(
                    LengthUtil.twip(printImage.getX() + leftPadding + xoffset + getOffsetX() + imageWidth)));
            contentWriter.write("\\shptop");
            contentWriter.write(
                    String.valueOf(LengthUtil.twip(printImage.getY() + topPadding + yoffset + getOffsetY())));
            contentWriter.write("\\shpbottom");
            contentWriter.write(String.valueOf(
                    LengthUtil.twip(printImage.getY() + topPadding + yoffset + getOffsetY() + imageHeight)));
            contentWriter.write("{\\sp{\\sn shapeType}{\\sv 75}}");
            contentWriter.write("{\\sp{\\sn fFilled}{\\sv 0}}");
            contentWriter.write("{\\sp{\\sn fLockAspectRatio}{\\sv 0}}");

            contentWriter.write("{\\sp{\\sn cropFromTop}{\\sv ");
            contentWriter.write(String.valueOf(cropTop));
            contentWriter.write("}}");
            contentWriter.write("{\\sp{\\sn cropFromLeft}{\\sv ");
            contentWriter.write(String.valueOf(cropLeft));
            contentWriter.write("}}");
            contentWriter.write("{\\sp{\\sn cropFromBottom}{\\sv ");
            contentWriter.write(String.valueOf(cropBottom));
            contentWriter.write("}}");
            contentWriter.write("{\\sp{\\sn cropFromRight}{\\sv ");
            contentWriter.write(String.valueOf(cropRight));
            contentWriter.write("}}");

            writeShapeHyperlink(printImage);

            if (printImage.getAnchorName() != null) {
                writeAnchor(printImage.getAnchorName());
            }

            contentWriter.write("{\\sp{\\sn pib}{\\sv {\\pict");
            if (imageProcessorResult.imageType == ImageTypeEnum.JPEG) {
                contentWriter.write("\\jpegblip");
            } else {
                contentWriter.write("\\pngblip");
            }
            contentWriter.write("\n");

            ByteArrayInputStream bais = new ByteArrayInputStream(imageProcessorResult.imageData);

            int count = 0;
            int current = 0;
            while ((current = bais.read()) != -1) {
                String helperStr = Integer.toHexString(current);
                if (helperStr.length() < 2) {
                    helperStr = "0" + helperStr;
                }
                contentWriter.write(helperStr);
                count++;
                if (count == 64) {
                    contentWriter.write("\n");
                    count = 0;
                }
            }

            contentWriter.write("\n}}}");
            contentWriter.write("}}\n");
            endHyperlink(startedHyperlink);
        }
    }

    int x = printImage.getX() + getOffsetX();
    int y = printImage.getY() + getOffsetY();
    int width = printImage.getWidth();
    int height = printImage.getHeight();

    if (printImage.getLineBox().getTopPen().getLineWidth() <= 0f
            && printImage.getLineBox().getLeftPen().getLineWidth() <= 0f
            && printImage.getLineBox().getBottomPen().getLineWidth() <= 0f
            && printImage.getLineBox().getRightPen().getLineWidth() <= 0f) {
        if (printImage.getLinePen().getLineWidth() > 0f) {
            exportPen(printImage.getLinePen(), x, y, width, height);
        }
    } else {
        exportBox(printImage.getLineBox(), x, y, width, height);
    }
}

From source file:net.sf.jasperreports.engine.export.ooxml.JRXlsxExporter.java

@Override
public void exportImage(JRPrintImage image, JRExporterGridCell gridCell, int colIndex, int rowIndex,
        int emptyCols, int yCutsRow, JRGridLayout layout) throws JRException {
    int topPadding = Math.max(image.getLineBox().getTopPadding(),
            getImageBorderCorrection(image.getLineBox().getTopPen()));
    int leftPadding = Math.max(image.getLineBox().getLeftPadding(),
            getImageBorderCorrection(image.getLineBox().getLeftPen()));
    int bottomPadding = Math.max(image.getLineBox().getBottomPadding(),
            getImageBorderCorrection(image.getLineBox().getBottomPen()));
    int rightPadding = Math.max(image.getLineBox().getRightPadding(),
            getImageBorderCorrection(image.getLineBox().getRightPen()));

    int availableImageWidth = image.getWidth() - leftPadding - rightPadding;
    availableImageWidth = availableImageWidth < 0 ? 0 : availableImageWidth;

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

    cellHelper.exportHeader(gridCell, rowIndex, colIndex, maxColumnIndex, sheetInfo);

    Renderable renderer = image.getRenderer();

    if (renderer != null && availableImageWidth > 0 && availableImageHeight > 0) {
        InternalImageProcessor imageProcessor = new InternalImageProcessor(image,
                image.getScaleImageValue() != ScaleImageEnum.FILL_FRAME, gridCell, availableImageWidth,
                availableImageHeight);/*from  www  .jav a 2  s. c o m*/

        InternalImageProcessorResult imageProcessorResult = null;

        try {
            imageProcessorResult = imageProcessor.process(renderer);
        } catch (Exception e) {
            Renderable onErrorRenderer = getRendererUtil().handleImageError(e, image.getOnErrorTypeValue());
            if (onErrorRenderer != null) {
                imageProcessorResult = imageProcessor.process(onErrorRenderer);
            }
        }

        if (imageProcessorResult != null) {
            int width = availableImageWidth;
            int height = availableImageHeight;

            double cropTop = 0;
            double cropLeft = 0;
            double cropBottom = 0;
            double cropRight = 0;

            switch (image.getScaleImageValue()) {
            case FILL_FRAME: {
                width = availableImageWidth;
                height = availableImageHeight;
                break;
            }
            case CLIP: {
                double normalWidth = availableImageWidth;
                double normalHeight = availableImageHeight;

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

                //                  if (normalWidth > availableImageWidth)
                //                  {
                switch (image.getHorizontalImageAlign()) {
                case RIGHT: {
                    cropLeft = 100000 * (availableImageWidth - normalWidth) / availableImageWidth;
                    cropRight = 0;
                    break;
                }
                case CENTER: {
                    cropLeft = 100000 * (availableImageWidth - normalWidth) / availableImageWidth / 2;
                    cropRight = cropLeft;
                    break;
                }
                case LEFT:
                default: {
                    cropLeft = 0;
                    cropRight = 100000 * (availableImageWidth - normalWidth) / availableImageWidth;
                    break;
                }
                }
                //                  }
                //                  else
                //                  {
                //                     width = (int)normalWidth;
                //                  }

                //                  if (normalHeight > availableImageHeight)
                //                  {
                switch (image.getVerticalImageAlign()) {
                case TOP: {
                    cropTop = 0;
                    cropBottom = 100000 * (availableImageHeight - normalHeight) / availableImageHeight;
                    break;
                }
                case MIDDLE: {
                    cropTop = 100000 * (availableImageHeight - normalHeight) / availableImageHeight / 2;
                    cropBottom = cropTop;
                    break;
                }
                case BOTTOM:
                default: {
                    cropTop = 100000 * (availableImageHeight - normalHeight) / availableImageHeight;
                    cropBottom = 0;
                    break;
                }
                }
                //                  }
                //                  else
                //                  {
                //                     height = (int)normalHeight;
                //                  }

                break;
            }
            case RETAIN_SHAPE:
            default: {
                double normalWidth = availableImageWidth;
                double normalHeight = availableImageHeight;

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

                double ratio = normalWidth / normalHeight;

                if (ratio > availableImageWidth / (double) availableImageHeight) {
                    width = availableImageWidth;
                    height = (int) (width / ratio);

                    switch (image.getVerticalImageAlign()) {
                    case TOP: {
                        cropTop = 0;
                        cropBottom = 100000 * (availableImageHeight - height) / availableImageHeight;
                        break;
                    }
                    case MIDDLE: {
                        cropTop = 100000 * (availableImageHeight - height) / availableImageHeight / 2;
                        cropBottom = cropTop;
                        break;
                    }
                    case BOTTOM:
                    default: {
                        cropTop = 100000 * (availableImageHeight - height) / availableImageHeight;
                        cropBottom = 0;
                        break;
                    }
                    }
                } else {
                    height = availableImageHeight;
                    width = (int) (ratio * height);

                    switch (image.getHorizontalImageAlign()) {
                    case RIGHT: {
                        cropLeft = 100000 * (availableImageWidth - width) / availableImageWidth;
                        cropRight = 0;
                        break;
                    }
                    case CENTER: {
                        cropLeft = 100000 * (availableImageWidth - width) / availableImageWidth / 2;
                        cropRight = cropLeft;
                        break;
                    }
                    case LEFT:
                    default: {
                        cropLeft = 0;
                        cropRight = 100000 * (availableImageWidth - width) / availableImageWidth;
                        break;
                    }
                    }
                }
            }
            }

            XlsReportConfiguration configuration = getCurrentItemConfiguration();

            if (!configuration.isIgnoreAnchors()) {
                insertPageAnchor(colIndex, rowIndex);
                if (image.getAnchorName() != null) {
                    String ref = "'" + JRStringUtil.xmlEncode(currentSheetName) + "'!$"
                            + JRXlsAbstractExporter.getColumIndexName(colIndex, maxColumnIndex) + "$"
                            + (rowIndex + 1);
                    definedNames.append("<definedName name=\"" + getDefinedName(image.getAnchorName()) + "\">"
                            + ref + "</definedName>\n");
                }
            }

            //            boolean startedHyperlink = startHyperlink(image,false);

            drawingRelsHelper.exportImage(imageProcessorResult.imagePath);

            sheetHelper.exportMergedCells(rowIndex, colIndex, maxColumnIndex, gridCell.getRowSpan(),
                    gridCell.getColSpan());

            ImageAnchorTypeEnum imageAnchorType = ImageAnchorTypeEnum.getByName(
                    JRPropertiesUtil.getOwnProperty(image, XlsReportConfiguration.PROPERTY_IMAGE_ANCHOR_TYPE));
            if (imageAnchorType == null) {
                imageAnchorType = configuration.getImageAnchorType();
                if (imageAnchorType == null) {
                    imageAnchorType = ImageAnchorTypeEnum.MOVE_NO_SIZE;
                }
            }
            drawingHelper.write("<xdr:twoCellAnchor editAs=\"" + getAnchorType(imageAnchorType) + "\">\n");
            drawingHelper.write("<xdr:from><xdr:col>" + colIndex + "</xdr:col><xdr:colOff>"
                    + LengthUtil.emu(leftPadding) + "</xdr:colOff><xdr:row>" + rowIndex
                    + "</xdr:row><xdr:rowOff>" + LengthUtil.emu(topPadding) + "</xdr:rowOff></xdr:from>\n");
            drawingHelper.write("<xdr:to><xdr:col>" + (colIndex + gridCell.getColSpan())
                    + "</xdr:col><xdr:colOff>" + LengthUtil.emu(-rightPadding) + "</xdr:colOff><xdr:row>"
                    + (rowIndex + (configuration.isCollapseRowSpan() ? 1 : gridCell.getRowSpan()))
                    + "</xdr:row><xdr:rowOff>" + LengthUtil.emu(-bottomPadding) + "</xdr:rowOff></xdr:to>\n");

            drawingHelper.write("<xdr:pic>\n");
            drawingHelper.write("<xdr:nvPicPr><xdr:cNvPr id=\""
                    + (image.hashCode() > 0 ? image.hashCode() : -image.hashCode()) + "\" name=\"Picture\">\n");

            String href = HyperlinkTypeEnum.LOCAL_ANCHOR.equals(image.getHyperlinkTypeValue())
                    || HyperlinkTypeEnum.LOCAL_PAGE.equals(image.getHyperlinkTypeValue())
                            ? "#" + getHyperlinkURL(image)
                            : getHyperlinkURL(image);
            if (href != null) {
                drawingHelper.exportHyperlink(href);
            }

            drawingHelper.write("</xdr:cNvPr><xdr:cNvPicPr/></xdr:nvPicPr>\n");
            drawingHelper.write("<xdr:blipFill>\n");
            drawingHelper.write("<a:blip r:embed=\"" + imageProcessorResult.imagePath + "\"/>");
            drawingHelper.write("<a:srcRect");
            ////            if (cropLeft > 0)
            //               drawingHelper.write(" l=\"" + (int)cropLeft + "\"");
            ////            if (cropTop > 0)
            //               drawingHelper.write(" t=\"" + (int)cropTop + "\"");
            ////            if (cropRight > 0)
            //               drawingHelper.write(" r=\"" + (int)cropRight + "\"");
            ////            if (cropBottom > 0)
            //               drawingHelper.write(" b=\"" + (int)cropBottom + "\"");
            drawingHelper.write("/>");
            drawingHelper.write("<a:stretch><a:fillRect");
            //            if (cropLeft > 0)
            drawingHelper.write(" l=\"" + (int) cropLeft + "\"");
            //            if (cropTop > 0)
            drawingHelper.write(" t=\"" + (int) cropTop + "\"");
            //            if (cropRight > 0)
            drawingHelper.write(" r=\"" + (int) cropRight + "\"");
            //            if (cropBottom > 0)
            drawingHelper.write(" b=\"" + (int) cropBottom + "\"");
            drawingHelper.write("/></a:stretch>\n");
            drawingHelper.write("</xdr:blipFill>\n");
            drawingHelper.write("<xdr:spPr><a:xfrm><a:off x=\"0\" y=\"0\"/><a:ext cx=\"" + LengthUtil.emu(0)
                    + "\" cy=\"" + LengthUtil.emu(0) + "\"/>");
            drawingHelper.write("</a:xfrm><a:prstGeom prst=\"rect\"></a:prstGeom>\n");
            //            if (image.getModeValue() == ModeEnum.OPAQUE && image.getBackcolor() != null)
            //            {
            //               drawingHelper.write("<a:solidFill><a:srgbClr val=\"" + JRColorUtil.getColorHexa(image.getBackcolor()) + "\"/></a:solidFill>\n");
            //            }
            drawingHelper.write("</xdr:spPr>\n");
            drawingHelper.write("</xdr:pic>\n");
            drawingHelper.write("<xdr:clientData/>\n");
            drawingHelper.write("</xdr:twoCellAnchor>\n");

            //            if(startedHyperlink)
            //            {
            //               endHyperlink(false);
            //            }
        }
    }

    //      drawingHelper.write("</w:p>");

    cellHelper.exportFooter();
}

From source file:net.sf.jasperreports.engine.export.ooxml.JRPptxExporter.java

/**
 *
 *///  w ww  .  j a  v  a2  s  . co m
public void exportImage(JRPrintImage image) throws JRException {
    int leftPadding = image.getLineBox().getLeftPadding();
    int topPadding = image.getLineBox().getTopPadding();//FIXMEDOCX maybe consider border thickness
    int rightPadding = image.getLineBox().getRightPadding();
    int bottomPadding = image.getLineBox().getBottomPadding();

    int availableImageWidth = image.getWidth() - leftPadding - rightPadding;
    availableImageWidth = availableImageWidth < 0 ? 0 : availableImageWidth;

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

    Renderable renderer = image.getRenderer();

    if (renderer != null && availableImageWidth > 0 && availableImageHeight > 0) {
        InternalImageProcessor imageProcessor = new InternalImageProcessor(image,
                image.getScaleImageValue() != ScaleImageEnum.FILL_FRAME, availableImageWidth,
                availableImageHeight);

        InternalImageProcessorResult imageProcessorResult = null;

        try {
            imageProcessorResult = imageProcessor.process(renderer);
        } catch (Exception e) {
            Renderable onErrorRenderer = getRendererUtil().handleImageError(e, image.getOnErrorTypeValue());
            if (onErrorRenderer != null) {
                imageProcessorResult = imageProcessor.process(onErrorRenderer);
            }
        }

        if (imageProcessorResult != null)//FIXMEPPTX render background for null images, like other exporters do 
        {
            int width = availableImageWidth;
            int height = availableImageHeight;

            double cropTop = 0;
            double cropLeft = 0;
            double cropBottom = 0;
            double cropRight = 0;

            switch (image.getScaleImageValue()) {
            case FILL_FRAME: {
                width = availableImageWidth;
                height = availableImageHeight;
                //                  cropTop = 100000 * topPadding / availableImageHeight;
                //                  cropLeft = 100000 * leftPadding / availableImageWidth;
                //                  cropBottom = 100000 * bottomPadding / availableImageHeight;
                //                  cropRight = 100000 * rightPadding / availableImageWidth;
                break;
            }
            case CLIP: {
                double normalWidth = availableImageWidth;
                double normalHeight = availableImageHeight;

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

                //                  if (normalWidth > availableImageWidth)
                //                  {
                switch (image.getHorizontalImageAlign()) {
                case RIGHT: {
                    cropLeft = 100000 * (availableImageWidth - normalWidth) / availableImageWidth;
                    cropRight = 0;
                    //                           cropRight = 100000 * rightPadding / availableImageWidth;
                    break;
                }
                case CENTER: {
                    cropLeft = 100000 * (availableImageWidth - normalWidth) / availableImageWidth / 2;
                    cropRight = cropLeft;
                    break;
                }
                case LEFT:
                default: {
                    //                           cropLeft = 100000 * leftPadding / availableImageWidth;
                    cropLeft = 0;
                    cropRight = 100000 * (availableImageWidth - normalWidth) / availableImageWidth;
                    break;
                }
                }
                //                     width = availableImageWidth;
                ////                     cropLeft = cropLeft / 0.75d;
                ////                     cropRight = cropRight / 0.75d;
                //                  }
                //                  else
                //                  {
                //                     width = (int)normalWidth;
                //                  }

                //                  if (normalHeight > availableImageHeight)
                //                  {
                switch (image.getVerticalImageAlign()) {
                case TOP: {
                    //                           cropTop = 100000 * topPadding / availableImageHeight;
                    cropTop = 0;
                    cropBottom = 100000 * (availableImageHeight - normalHeight) / availableImageHeight;
                    break;
                }
                case MIDDLE: {
                    cropTop = 100000 * (availableImageHeight - normalHeight) / availableImageHeight / 2;
                    cropBottom = cropTop;
                    break;
                }
                case BOTTOM:
                default: {
                    cropTop = 100000 * (availableImageHeight - normalHeight) / availableImageHeight;
                    cropBottom = 0;
                    //                           cropBottom = 100000 * bottomPadding / availableImageHeight;
                    break;
                }
                }
                //                     height = availableImageHeight;
                //                     cropTop = cropTop / 0.75d;
                //                     cropBottom = cropBottom / 0.75d;
                //                  }
                //                  else
                //                  {
                //                     height = (int)normalHeight;
                //                  }

                break;
            }
            case RETAIN_SHAPE:
            default: {
                double normalWidth = availableImageWidth;
                double normalHeight = availableImageHeight;

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

                double ratio = normalWidth / normalHeight;

                if (ratio > availableImageWidth / (double) availableImageHeight) {
                    width = availableImageWidth;
                    height = (int) (width / ratio);

                    switch (image.getVerticalImageAlign()) {
                    case TOP: {
                        cropTop = 0;
                        cropBottom = 100000 * (availableImageHeight - height) / availableImageHeight;
                        break;
                    }
                    case MIDDLE: {
                        cropTop = 100000 * (availableImageHeight - height) / availableImageHeight / 2;
                        cropBottom = cropTop;
                        break;
                    }
                    case BOTTOM:
                    default: {
                        cropTop = 100000 * (availableImageHeight - height) / availableImageHeight;
                        cropBottom = 0;
                        break;
                    }
                    }
                } else {
                    height = availableImageHeight;
                    width = (int) (ratio * height);

                    switch (image.getHorizontalImageAlign()) {
                    case RIGHT: {
                        cropLeft = 100000 * (availableImageWidth - width) / availableImageWidth;
                        cropRight = 0;
                        break;
                    }
                    case CENTER: {
                        cropLeft = 100000 * (availableImageWidth - width) / availableImageWidth / 2;
                        cropRight = cropLeft;
                        break;
                    }
                    case LEFT:
                    default: {
                        cropLeft = 0;
                        cropRight = 100000 * (availableImageWidth - width) / availableImageWidth;
                        break;
                    }
                    }
                }
            }
            }

            //            insertPageAnchor();
            //            if (image.getAnchorName() != null)
            //            {
            //               tempBodyWriter.write("<text:bookmark text:name=\"");
            //               tempBodyWriter.write(image.getAnchorName());
            //               tempBodyWriter.write("\"/>");
            //            }

            //            boolean startedHyperlink = startHyperlink(image,false);

            slideRelsHelper.exportImage(imageProcessorResult.imagePath);

            slideHelper.write("<p:pic>\n");
            slideHelper.write("  <p:nvPicPr>\n");
            slideHelper.write("    <p:cNvPr id=\"" + toOOXMLId(image) + "\" name=\"Picture\">\n");

            String href = getHyperlinkURL(image);
            if (href != null) {
                slideHelper.exportHyperlink(href);
            }

            slideHelper.write("    </p:cNvPr>\n");
            slideHelper.write("    <p:cNvPicPr>\n");
            slideHelper.write("      <a:picLocks noChangeAspect=\"1\"/>\n");
            slideHelper.write("    </p:cNvPicPr>\n");
            slideHelper.write("    <p:nvPr/>\n");
            slideHelper.write("  </p:nvPicPr>\n");
            slideHelper.write("<p:blipFill>\n");
            slideHelper.write("<a:blip r:embed=\"" + imageProcessorResult.imagePath + "\"/>");
            slideHelper.write("<a:srcRect");
            ////            if (cropLeft > 0)
            ////            {
            //               slideHelper.write(" l=\"" + (int)(100000 * leftPadding / image.getWidth()) + "\"");
            ////            }
            ////            if (cropTop > 0)
            ////            {
            //               slideHelper.write(" t=\"" + (int)cropTop + "\"");
            ////            }
            ////            if (cropRight > 0)
            ////            {
            //               slideHelper.write(" r=\"" + (int)cropRight + "\"");
            ////            }
            ////            if (cropBottom > 0)
            ////            {
            //               slideHelper.write(" b=\"" + (int)cropBottom + "\"");
            ////            }
            slideHelper.write("/>");
            slideHelper.write("<a:stretch><a:fillRect");
            //            if (cropLeft > 0)
            //            {
            slideHelper.write(" l=\"" + (int) cropLeft + "\"");
            //            }
            //            if (cropTop > 0)
            //            {
            slideHelper.write(" t=\"" + (int) cropTop + "\"");
            //            }
            //            if (cropRight > 0)
            //            {
            slideHelper.write(" r=\"" + (int) cropRight + "\"");
            //            }
            //            if (cropBottom > 0)
            //            {
            slideHelper.write(" b=\"" + (int) cropBottom + "\"");
            //            }
            slideHelper.write("/></a:stretch>\n");
            slideHelper.write("</p:blipFill>\n");
            slideHelper.write("  <p:spPr>\n");
            slideHelper.write("    <a:xfrm>\n");
            slideHelper.write("      <a:off x=\"" + LengthUtil.emu(image.getX() + getOffsetX() + leftPadding)
                    + "\" y=\"" + LengthUtil.emu(image.getY() + getOffsetY() + topPadding) + "\"/>\n");
            slideHelper.write("      <a:ext cx=\"" + LengthUtil.emu(availableImageWidth) + "\" cy=\""
                    + LengthUtil.emu(availableImageHeight) + "\"/>\n");
            slideHelper.write("    </a:xfrm><a:prstGeom prst=\"rect\"><a:avLst/></a:prstGeom>\n");
            if (image.getModeValue() == ModeEnum.OPAQUE && image.getBackcolor() != null) {
                slideHelper.write("<a:solidFill><a:srgbClr val=\""
                        + JRColorUtil.getColorHexa(image.getBackcolor()) + "\"/></a:solidFill>\n");
            }
            JRPen pen = getPptxPen(image.getLineBox());
            if (pen != null) {
                slideHelper.write("  <a:ln w=\"" + LengthUtil.emu(pen.getLineWidth()) + "\"");
                if (LineStyleEnum.DOUBLE.equals(pen.getLineStyleValue())) {
                    slideHelper.write(" cmpd=\"dbl\"");
                }
                slideHelper.write(">\n");
                slideHelper.write("<a:solidFill><a:srgbClr val=\""
                        + JRColorUtil.getColorHexa(pen.getLineColor()) + "\"/></a:solidFill>\n");
                slideHelper.write("<a:prstDash val=\"");
                switch (pen.getLineStyleValue()) {
                case DASHED: {
                    slideHelper.write("dash");
                    break;
                }
                case DOTTED: {
                    slideHelper.write("dot");
                    break;
                }
                case DOUBLE:
                case SOLID:
                default: {
                    slideHelper.write("solid");
                    break;
                }
                }
                slideHelper.write("\"/>\n");
                slideHelper.write("  </a:ln>\n");
            }
            slideHelper.write("  </p:spPr>\n");
            slideHelper.write("  </p:pic>\n");

            //            if(startedHyperlink)
            //            {
            //               endHyperlink(false);
            //            }
        }
    }

    //      docHelper.write("</w:p>");
}

From source file:net.sf.jasperreports.engine.export.JExcelApiMetadataExporter.java

public void exportImage(JRPrintImage element) throws JRException {

    String currentColumnName = element.getPropertiesMap()
            .getProperty(JRXlsAbstractMetadataExporterParameter.PROPERTY_COLUMN_NAME);
    if (currentColumnName != null && currentColumnName.length() > 0) {
        boolean repeatValue = getPropertiesUtil().getBooleanProperty(element,
                JRXlsAbstractMetadataExporterParameter.PROPERTY_REPEAT_VALUE, false);

        setColumnName(currentColumnName);
        setColumnWidth(columnNamesMap.get(currentColumnName), element.getWidth());
        setRowHeight(rowIndex, element.getHeight());

        int topPadding = Math.max(element.getLineBox().getTopPadding().intValue(),
                getImageBorderCorrection(element.getLineBox().getTopPen()));
        int leftPadding = Math.max(element.getLineBox().getLeftPadding().intValue(),
                getImageBorderCorrection(element.getLineBox().getLeftPen()));
        int bottomPadding = Math.max(element.getLineBox().getBottomPadding().intValue(),
                getImageBorderCorrection(element.getLineBox().getBottomPen()));
        int rightPadding = Math.max(element.getLineBox().getRightPadding().intValue(),
                getImageBorderCorrection(element.getLineBox().getRightPen()));

        int availableImageWidth = element.getWidth() - leftPadding - rightPadding;
        availableImageWidth = availableImageWidth < 0 ? 0 : availableImageWidth;

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

        Renderable renderer = element.getRenderable();

        if (renderer != null && availableImageWidth > 0 && availableImageHeight > 0) {
            if (renderer.getTypeValue() == RenderableTypeEnum.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 = RenderableUtil.getInstance(jasperReportsContext)
                        .getOnErrorRendererForImageData(renderer, element.getOnErrorTypeValue());
                if (renderer != null) {
                    renderer = RenderableUtil.getInstance(jasperReportsContext)
                            .getOnErrorRendererForDimension(renderer, element.getOnErrorTypeValue());
                }/*  w  ww  .  j  a  v  a 2 s. c o  m*/
            } 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(jasperReportsContext);
            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;

            switch (element.getScaleImageValue()) {
            case CLIP: {
                int dpi = getPropertiesUtil().getIntegerProperty(Renderable.PROPERTY_IMAGE_DPI, 72);
                double scale = dpi / 72d;

                BufferedImage bi = new BufferedImage((int) (scale * availableImageWidth),
                        (int) (scale * availableImageHeight), BufferedImage.TYPE_INT_ARGB);

                Graphics2D grx = bi.createGraphics();
                grx.scale(scale, scale);
                grx.clip(new Rectangle(0, 0, availableImageWidth, availableImageHeight));

                renderer.render(jasperReportsContext, grx,
                        new Rectangle((int) (xalignFactor * (availableImageWidth - normalWidth)),
                                (int) (yalignFactor * (availableImageHeight - normalHeight)), normalWidth,
                                normalHeight));

                imageData = JRImageLoader.getInstance(jasperReportsContext).loadBytesFromAwtImage(bi,
                        ImageTypeEnum.PNG);

                break;
            }
            case FILL_FRAME: {
                imageData = renderer.getImageData(jasperReportsContext);

                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;
                    }

                    imageData = renderer.getImageData(jasperReportsContext);
                }

                break;
            }
            }

            Pattern mode = this.backgroundMode;
            Colour background = WHITE;

            JxlReportConfiguration configuration = getCurrentItemConfiguration();

            if (!configuration.isIgnoreCellBackground() && element.getBackcolor() != null) {
                mode = Pattern.SOLID;
                background = getWorkbookColour(element.getBackcolor(), true);
            }

            if (element.getModeValue() == ModeEnum.OPAQUE) {
                background = getWorkbookColour(element.getBackcolor(), true);
            }

            Colour forecolor = getWorkbookColour(element.getLineBox().getPen().getLineColor());

            WritableFont cellFont2 = this.getLoadedFont(getDefaultFont(), forecolor.getValue(), getLocale());

            WritableCellFormat cellStyle2 = getLoadedCellStyle(mode, background, cellFont2,
                    new BoxStyle(element), isCellLocked(element));

            addBlankElement(cellStyle2, repeatValue, currentColumnName);
            try {
                int colIndex = columnNamesMap.get(currentColumnName);
                WritableImage image = new WritableImage(colIndex, rowIndex, 1, 1, imageData);
                ImageAnchorTypeEnum imageAnchorType = ImageAnchorTypeEnum.getByName(JRPropertiesUtil
                        .getOwnProperty(element, XlsReportConfiguration.PROPERTY_IMAGE_ANCHOR_TYPE));
                if (imageAnchorType == null) {
                    imageAnchorType = configuration.getImageAnchorType();
                    if (imageAnchorType == null) {
                        imageAnchorType = ImageAnchorTypeEnum.MOVE_NO_SIZE;
                    }
                }
                setAnchorType(image, imageAnchorType);
                sheet.addImage(image);
            } catch (Exception ex) {
                throw new JRException("The cell cannot be added", ex);
            } catch (Error err) {
                throw new JRException("The cell cannot be added", err);
            }
        }
    }
}

From source file:net.sf.jasperreports.engine.export.JRHtmlExporter.java

/**
 *
 *//*  w  ww. ja v  a2s  .co  m*/
protected void exportImage(JRPrintImage image, JRExporterGridCell gridCell) throws JRException, IOException {
    writeCellStart(gridCell);

    StringBuffer styleBuffer = new StringBuffer();

    String horizontalAlignment = CSS_TEXT_ALIGN_LEFT;

    switch (image.getHorizontalAlignmentValue()) {
    case RIGHT: {
        horizontalAlignment = CSS_TEXT_ALIGN_RIGHT;
        break;
    }
    case CENTER: {
        horizontalAlignment = CSS_TEXT_ALIGN_CENTER;
        break;
    }
    case LEFT:
    default: {
        horizontalAlignment = CSS_TEXT_ALIGN_LEFT;
    }
    }

    if (!horizontalAlignment.equals(CSS_TEXT_ALIGN_LEFT)) {
        styleBuffer.append("text-align: ");
        styleBuffer.append(horizontalAlignment);
        styleBuffer.append(";");
    }

    String verticalAlignment = HTML_VERTICAL_ALIGN_TOP;

    switch (image.getVerticalAlignmentValue()) {
    case BOTTOM: {
        verticalAlignment = HTML_VERTICAL_ALIGN_BOTTOM;
        break;
    }
    case MIDDLE: {
        verticalAlignment = HTML_VERTICAL_ALIGN_MIDDLE;
        break;
    }
    case TOP:
    default: {
        verticalAlignment = HTML_VERTICAL_ALIGN_TOP;
    }
    }

    if (!verticalAlignment.equals(HTML_VERTICAL_ALIGN_TOP)) {
        styleBuffer.append(" vertical-align: ");
        styleBuffer.append(verticalAlignment);
        styleBuffer.append(";");
    }

    appendBackcolorStyle(gridCell, styleBuffer);

    boolean addedToStyle = appendBorderStyle(gridCell.getBox(), styleBuffer);
    if (!addedToStyle) {
        appendPen(styleBuffer, image.getLinePen(), null);
    }

    appendPaddingStyle(image.getLineBox(), styleBuffer);

    if (styleBuffer.length() > 0) {
        writer.write(" style=\"");
        writer.write(styleBuffer.toString());
        writer.write("\"");
    }

    writer.write(">");

    if (image.getAnchorName() != null) {
        writer.write("<a name=\"");
        writer.write(image.getAnchorName());
        writer.write("\"/>");
    }

    Renderable renderer = image.getRenderable();
    Renderable originalRenderer = renderer;
    boolean imageMapRenderer = renderer != null && renderer instanceof ImageMapRenderable
            && ((ImageMapRenderable) renderer).hasImageAreaHyperlinks();

    boolean hasHyperlinks = false;

    boolean isUsingImagesToAlign = getCurrentConfiguration().isUsingImagesToAlign();
    if (renderer != null || isUsingImagesToAlign) {
        if (imageMapRenderer) {
            hasHyperlinks = true;
            hyperlinkStarted = false;
        } else {
            hasHyperlinks = startHyperlink(image);
        }

        writer.write("<img");
        String imagePath = null;
        String imageMapName = null;
        List<JRPrintImageAreaHyperlink> imageMapAreas = null;

        ScaleImageEnum scaleImage = image.getScaleImageValue();

        if (renderer != null) {
            if (renderer.getTypeValue() == RenderableTypeEnum.IMAGE
                    && rendererToImagePathMap.containsKey(renderer.getId())) {
                imagePath = rendererToImagePathMap.get(renderer.getId());
            } else {
                if (image.isLazy()) {
                    imagePath = ((JRImageRenderer) renderer).getImageLocation();
                } else {
                    HtmlResourceHandler imageHandler = getImageHandler() == null
                            ? getExporterOutput().getImageHandler()
                            : getImageHandler();
                    if (imageHandler != null) {
                        JRPrintElementIndex imageIndex = getElementIndex(gridCell);
                        String imageName = getImageName(imageIndex);

                        if (renderer.getTypeValue() == RenderableTypeEnum.SVG) {
                            renderer = new JRWrappingSvgRenderer(renderer,
                                    new Dimension(image.getWidth(), image.getHeight()),
                                    ModeEnum.OPAQUE == image.getModeValue() ? image.getBackcolor() : null);
                        }

                        byte[] imageData = renderer.getImageData(jasperReportsContext);

                        if (imageHandler != null) {
                            imageHandler.handleResource(imageName, imageData);

                            imagePath = imageHandler.getResourcePath(imageName);
                        }
                    }
                }

                rendererToImagePathMap.put(renderer.getId(), imagePath);
            }

            if (imageMapRenderer) {
                Rectangle renderingArea = new Rectangle(image.getWidth(), image.getHeight());

                if (renderer.getTypeValue() == RenderableTypeEnum.IMAGE) {
                    imageMapName = imageMaps.get(new Pair<String, Rectangle>(renderer.getId(), renderingArea));
                }

                if (imageMapName == null) {
                    imageMapName = "map_" + getElementIndex(gridCell).toString();
                    imageMapAreas = ((ImageMapRenderable) originalRenderer)
                            .getImageAreaHyperlinks(renderingArea);//FIXMECHART

                    if (renderer.getTypeValue() == RenderableTypeEnum.IMAGE) {
                        imageMaps.put(new Pair<String, Rectangle>(renderer.getId(), renderingArea),
                                imageMapName);
                    }
                }
            }
        } else // ie:    if(isUsingImagesToAlign)
        {
            HtmlResourceHandler imageHandler = getExporterOutput().getImageHandler();
            imagePath = imageHandler == null ? null : imageHandler.getResourcePath("px");
            scaleImage = ScaleImageEnum.FILL_FRAME;
        }

        writer.write(" src=\"");
        if (imagePath != null) {
            writer.write(imagePath);
        }
        writer.write("\"");

        int imageWidth = image.getWidth() - image.getLineBox().getLeftPadding().intValue()
                - image.getLineBox().getRightPadding().intValue();
        if (imageWidth < 0) {
            imageWidth = 0;
        }

        int imageHeight = image.getHeight() - image.getLineBox().getTopPadding().intValue()
                - image.getLineBox().getBottomPadding().intValue();
        if (imageHeight < 0) {
            imageHeight = 0;
        }

        switch (scaleImage) {
        case FILL_FRAME: {
            writer.write(" style=\"width: ");
            writer.write(toSizeUnit(imageWidth));
            writer.write("; height: ");
            writer.write(toSizeUnit(imageHeight));
            writer.write("\"");

            break;
        }
        case CLIP: //FIXMEIMAGE image clip could be achieved by cutting the image and preserving the image type
        case RETAIN_SHAPE:
        default: {
            double normalWidth = imageWidth;
            double normalHeight = imageHeight;

            if (!image.isLazy()) {
                // Image load might fail. 
                Renderable tmpRenderer = RenderableUtil.getInstance(jasperReportsContext)
                        .getOnErrorRendererForDimension(renderer, image.getOnErrorTypeValue());
                Dimension2D dimension = tmpRenderer == null ? null
                        : tmpRenderer.getDimension(jasperReportsContext);
                // If renderer was replaced, ignore image dimension.
                if (tmpRenderer == renderer && dimension != null) {
                    normalWidth = dimension.getWidth();
                    normalHeight = dimension.getHeight();
                }
            }

            if (imageHeight > 0) {
                double ratio = normalWidth / normalHeight;

                if (ratio > (double) imageWidth / (double) imageHeight) {
                    writer.write(" style=\"width: ");
                    writer.write(toSizeUnit(imageWidth));
                    writer.write("\"");
                } else {
                    writer.write(" style=\"height: ");
                    writer.write(toSizeUnit(imageHeight));
                    writer.write("\"");
                }
            }
        }
        }

        if (imageMapName != null) {
            writer.write(" usemap=\"#" + imageMapName + "\"");
        }

        writer.write(" alt=\"\"");

        if (hasHyperlinks) {
            writer.write(" border=\"0\"");
        }

        if (image.getHyperlinkTooltip() != null) {
            writer.write(" title=\"");
            writer.write(JRStringUtil.xmlEncode(image.getHyperlinkTooltip()));
            writer.write("\"");
        }

        writer.write("/>");

        endHyperlink();

        if (imageMapAreas != null) {
            writer.write("\n");
            writeImageMap(imageMapName, image, imageMapAreas);
        }
    }
    writeCellEnd(gridCell);
}

From source file:net.sf.jasperreports.engine.export.JExcelApiExporter.java

public void exportImage(JRPrintImage element, JRExporterGridCell gridCell, int col, int row, int emptyCols,
        int yCutsRow, JRGridLayout layout) throws JRException {
    addMergeRegion(gridCell, col, row);// w w  w.  java2  s.  c  o m

    int topPadding = Math.max(element.getLineBox().getTopPadding().intValue(),
            getImageBorderCorrection(element.getLineBox().getTopPen()));
    int leftPadding = Math.max(element.getLineBox().getLeftPadding().intValue(),
            getImageBorderCorrection(element.getLineBox().getLeftPen()));
    int bottomPadding = Math.max(element.getLineBox().getBottomPadding().intValue(),
            getImageBorderCorrection(element.getLineBox().getBottomPen()));
    int rightPadding = Math.max(element.getLineBox().getRightPadding().intValue(),
            getImageBorderCorrection(element.getLineBox().getRightPen()));

    int availableImageWidth = element.getWidth() - leftPadding - rightPadding;
    availableImageWidth = availableImageWidth < 0 ? 0 : availableImageWidth;

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

    Renderable renderer = element.getRenderable();

    if (renderer != null && availableImageWidth > 0 && availableImageHeight > 0) {
        if (renderer.getTypeValue() == RenderableTypeEnum.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 = RenderableUtil.getInstance(jasperReportsContext).getOnErrorRendererForImageData(renderer,
                    element.getOnErrorTypeValue());
            if (renderer != null) {
                renderer = RenderableUtil.getInstance(jasperReportsContext)
                        .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(jasperReportsContext);
        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: {
            int dpi = getPropertiesUtil().getIntegerProperty(Renderable.PROPERTY_IMAGE_DPI, 72);
            double scale = dpi / 72d;

            BufferedImage bi = new BufferedImage((int) (scale * availableImageWidth),
                    (int) (scale * availableImageHeight), BufferedImage.TYPE_INT_ARGB);

            Graphics2D grx = bi.createGraphics();
            grx.scale(scale, scale);
            grx.clip(new Rectangle(0, 0, availableImageWidth, availableImageHeight));

            renderer.render(jasperReportsContext, grx,
                    new Rectangle((int) (xalignFactor * (availableImageWidth - normalWidth)),
                            (int) (yalignFactor * (availableImageHeight - normalHeight)), normalWidth,
                            normalHeight));

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

            imageData = JRImageLoader.getInstance(jasperReportsContext).loadBytesFromAwtImage(bi,
                    ImageTypeEnum.PNG);

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

            imageData = renderer.getImageData(jasperReportsContext);

            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(jasperReportsContext);
            }

            break;
        }
        }

        Pattern mode = this.backgroundMode;
        Colour background = WHITE;

        JxlReportConfiguration configuration = getCurrentItemConfiguration();

        if (!configuration.isIgnoreCellBackground() && gridCell.getCellBackcolor() != null) {
            mode = Pattern.SOLID;
            background = getWorkbookColour(gridCell.getCellBackcolor(), true);
        }

        if (element.getModeValue() == ModeEnum.OPAQUE) {
            background = getWorkbookColour(element.getBackcolor(), true);
        }

        Colour forecolor = getWorkbookColour(element.getLineBox().getPen().getLineColor());

        WritableFont cellFont2 = this.getLoadedFont(getDefaultFont(), forecolor.getValue(), getLocale());

        WritableCellFormat cellStyle2 = getLoadedCellStyle(mode, background, cellFont2, gridCell,
                isCellLocked(element));

        if (!configuration.isIgnoreAnchors() && element.getAnchorName() != null) {
            int lastCol = Math.max(0, col + gridCell.getColSpan() - 1);
            int lastRow = Math.max(0, row + gridCell.getRowSpan() - 1);
            workbook.addNameArea(element.getAnchorName(), sheet, col, row, lastCol, lastRow);
        }

        Boolean ignoreHyperlink = HyperlinkUtil
                .getIgnoreHyperlink(XlsReportConfiguration.PROPERTY_IGNORE_HYPERLINK, element);
        if (ignoreHyperlink == null) {
            ignoreHyperlink = configuration.isIgnoreHyperlink();
        }
        if (!ignoreHyperlink) {
            exportHyperlink(element, "", gridCell, col, row);
        }

        try {
            sheet.addCell(new Blank(col, row, cellStyle2));
            double leftPos = getColumnRelativePosition(layout, col, leftOffset);
            double topPos = getRowRelativePosition(layout, yCutsRow, topOffset);
            double rightPos = getColumnRelativePosition(layout, col, element.getWidth() - rightOffset);
            double bottomPos = getRowRelativePosition(layout, yCutsRow, element.getHeight() - bottomOffset);
            WritableImage image = new WritableImage(col + leftPos, row + topPos, rightPos - leftPos,
                    bottomPos - topPos, imageData);

            ImageAnchorTypeEnum imageAnchorType = ImageAnchorTypeEnum.getByName(JRPropertiesUtil
                    .getOwnProperty(element, XlsReportConfiguration.PROPERTY_IMAGE_ANCHOR_TYPE));
            if (imageAnchorType == null) {
                imageAnchorType = configuration.getImageAnchorType();
                if (imageAnchorType == null) {
                    imageAnchorType = ImageAnchorTypeEnum.MOVE_NO_SIZE;
                }
            }
            setAnchorType(image, imageAnchorType);
            sheet.addImage(image);
        } catch (Exception ex) {
            throw new JRException("The cell cannot be added", ex);
        } catch (Error err) {
            throw new JRException("The cell cannot be added", err);
        }
    }
}

From source file:com.dlya.facturews.DlyaPdfExporter2.java

/**
 *
 *//*w w  w  .  ja va 2 s .co  m*/
public void exportImage(JRPrintImage printImage) throws DocumentException, IOException, JRException {
    if (printImage.getModeValue() == ModeEnum.OPAQUE) {
        pdfContentByte.setRGBColorFill(printImage.getBackcolor().getRed(), printImage.getBackcolor().getGreen(),
                printImage.getBackcolor().getBlue());
        pdfContentByte.rectangle(printImage.getX() + getOffsetX(),
                jasperPrint.getPageHeight() - printImage.getY() - getOffsetY(), printImage.getWidth(),
                -printImage.getHeight());
        pdfContentByte.fill();
    }

    int topPadding = printImage.getLineBox().getTopPadding().intValue();
    int leftPadding = printImage.getLineBox().getLeftPadding().intValue();
    int bottomPadding = printImage.getLineBox().getBottomPadding().intValue();
    int rightPadding = printImage.getLineBox().getRightPadding().intValue();

    int availableImageWidth = printImage.getWidth() - leftPadding - rightPadding;
    availableImageWidth = (availableImageWidth < 0) ? 0 : availableImageWidth;

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

    Renderable renderer = printImage.getRenderable();

    if (renderer != null && availableImageWidth > 0 && availableImageHeight > 0) {
        if (renderer.getTypeValue() == RenderableTypeEnum.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 = RenderableUtil.getInstance(jasperReportsContext).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.getTypeValue() == RenderableTypeEnum.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 = RenderableUtil.getInstance(jasperReportsContext)
                        .getOnErrorRendererForDimension(renderer, printImage.getOnErrorTypeValue());
                if (renderer == null) {
                    break;
                }

                int normalWidth = availableImageWidth;
                int normalHeight = availableImageHeight;

                Dimension2D dimension = renderer.getDimension(jasperReportsContext);
                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(jasperReportsContext, 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 = loadedImagesMap.get(renderer);
                } else {
                    try {
                        image = com.lowagie.text.Image.getInstance(renderer.getImageData(jasperReportsContext));
                        imageTesterPdfContentByte.addImage(image, 10, 0, 0, 10, 0, 0);
                    } catch (Exception e) {
                        JRImageRenderer tmpRenderer = JRImageRenderer.getOnErrorRendererForImage(
                                jasperReportsContext,
                                JRImageRenderer.getInstance(renderer.getImageData(jasperReportsContext)),
                                printImage.getOnErrorTypeValue());
                        if (tmpRenderer == null) {
                            break;
                        }
                        java.awt.Image awtImage = tmpRenderer.getImage(jasperReportsContext);
                        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 = loadedImagesMap.get(renderer);
                } else {
                    try {
                        image = com.lowagie.text.Image.getInstance(renderer.getImageData(jasperReportsContext));
                        imageTesterPdfContentByte.addImage(image, 10, 0, 0, 10, 0, 0);
                    } catch (Exception e) {
                        JRImageRenderer tmpRenderer = JRImageRenderer.getOnErrorRendererForImage(
                                jasperReportsContext,
                                JRImageRenderer.getInstance(renderer.getImageData(jasperReportsContext)),
                                printImage.getOnErrorTypeValue());
                        if (tmpRenderer == null) {
                            break;
                        }
                        java.awt.Image awtImage = tmpRenderer.getImage(jasperReportsContext);
                        image = com.lowagie.text.Image.getInstance(awtImage, null);
                    }

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

                image.scaleToFit(availableImageWidth, availableImageHeight);

                xoffset = (int) (xalignFactor * (availableImageWidth - image.plainWidth()));
                yoffset = (int) (yalignFactor * (availableImageHeight - image.plainHeight()));

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

                break;
            }
            }

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

                scaledWidth = image.scaledWidth();
                scaledHeight = image.scaledHeight();
            }
        } 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(jasperReportsContext);
            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(jasperReportsContext, g, rectangle);
            g.dispose();

            pdfContentByte.saveState();
            pdfContentByte.addTemplate(template, (float) ratioX, 0f, 0f, (float) ratioY,
                    printImage.getX() + getOffsetX() + xoffset, jasperPrint.getPageHeight() - printImage.getY()
                            - getOffsetY() - (int) normalHeight - yoffset);
            pdfContentByte.restoreState();

            Image image = getPxImage();
            image.scaleAbsolute(availableImageWidth, availableImageHeight);
            chunk = new Chunk(image, 0, 0);
        }

        /*
        image.setAbsolutePosition(
           printImage.getX() + offsetX + borderOffset,
           jasperPrint.getPageHeight() - printImage.getY() - offsetY - image.scaledHeight() - borderOffset
           );
                
        pdfContentByte.addImage(image);
        */

        if (chunk != null) {
            setAnchor(chunk, printImage, printImage);
            setHyperlinkInfo(chunk, printImage);

            //tagHelper.startImage(printImage);

            ColumnText colText = new ColumnText(pdfContentByte);
            int upperY = jasperPrint.getPageHeight() - printImage.getY() - topPadding - getOffsetY() - yoffset;
            int lowerX = printImage.getX() + leftPadding + getOffsetX() + xoffset;
            colText.setSimpleColumn(new Phrase(chunk), lowerX, upperY - scaledHeight, lowerX + scaledWidth,
                    upperY, scaledHeight, Element.ALIGN_LEFT);

            colText.go();

            //tagHelper.endImage();
        }
    }

    if (printImage.getLineBox().getTopPen().getLineWidth().floatValue() <= 0f
            && printImage.getLineBox().getLeftPen().getLineWidth().floatValue() <= 0f
            && printImage.getLineBox().getBottomPen().getLineWidth().floatValue() <= 0f
            && printImage.getLineBox().getRightPen().getLineWidth().floatValue() <= 0f) {
        if (printImage.getLinePen().getLineWidth().floatValue() > 0f) {
            exportPen(printImage.getLinePen(), printImage);
        }
    } else {
        /*   */
        exportBox(printImage.getLineBox(), printImage);
    }
}

From source file:net.sf.jasperreports.engine.export.JRXhtmlExporter.java

/**
 *
 *///from w  w w . j ava  2s  . co  m
protected void exportImage(JRPrintImage image) throws JRException, IOException {
    writer.write("<span");

    appendId(image);

    float xAlignFactor = 0f;

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

    float yAlignFactor = 0f;

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

    StringBuffer styleBuffer = new StringBuffer();
    appendPositionStyle(image, image, styleBuffer);
    appendSizeStyle(image, image, styleBuffer);
    appendBackcolorStyle(image, styleBuffer);

    boolean addedToStyle = appendBorderStyle(image.getLineBox(), styleBuffer);
    if (!addedToStyle) {
        appendPen(styleBuffer, image.getLinePen(), null);
    }

    if (styleBuffer.length() > 0) {
        writer.write(" style=\"");
        writer.write(styleBuffer.toString());
        writer.write("\"");
    }

    writer.write(">");

    if (image.getAnchorName() != null) {
        writer.write("<a name=\"");
        writer.write(image.getAnchorName());
        writer.write("\"></a>");
    }

    Renderable renderer = image.getRenderable();
    Renderable originalRenderer = renderer;
    boolean imageMapRenderer = renderer != null && renderer instanceof ImageMapRenderable
            && ((ImageMapRenderable) 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<JRPrintImageAreaHyperlink> imageMapAreas = null;

        ScaleImageEnum scaleImage = image.getScaleImageValue();

        if (renderer != null) {
            if (renderer.getTypeValue() == RenderableTypeEnum.IMAGE
                    && rendererToImagePathMap.containsKey(renderer.getId())) {
                imagePath = rendererToImagePathMap.get(renderer.getId());
            } else {
                if (image.isLazy()) {
                    imagePath = ((JRImageRenderer) renderer).getImageLocation();
                } else {
                    HtmlResourceHandler imageHandler = getImageHandler() == null
                            ? getExporterOutput().getImageHandler()
                            : getImageHandler();
                    if (imageHandler != null) {
                        JRPrintElementIndex imageIndex = getElementIndex();
                        String imageName = getImageName(imageIndex);

                        if (renderer.getTypeValue() == RenderableTypeEnum.SVG) {
                            renderer = new JRWrappingSvgRenderer(renderer,
                                    new Dimension(image.getWidth(), image.getHeight()),
                                    ModeEnum.OPAQUE == image.getModeValue() ? image.getBackcolor() : null);
                        }

                        byte[] imageData = renderer.getImageData(jasperReportsContext);

                        if (imageHandler != null) {
                            imageHandler.handleResource(imageName, imageData);

                            imagePath = imageHandler.getResourcePath(imageName);
                        }
                    }
                }

                rendererToImagePathMap.put(renderer.getId(), imagePath);
            }

            if (imageMapRenderer) {
                Rectangle renderingArea = new Rectangle(image.getWidth(), image.getHeight());

                if (renderer.getTypeValue() == RenderableTypeEnum.IMAGE) {
                    imageMapName = imageMaps.get(new Pair<String, Rectangle>(renderer.getId(), renderingArea));
                }

                if (imageMapName == null) {
                    imageMapName = "map_" + getElementIndex().toString();
                    imageMapAreas = ((ImageMapRenderable) originalRenderer)
                            .getImageAreaHyperlinks(renderingArea);//FIXMECHART

                    if (renderer.getTypeValue() == RenderableTypeEnum.IMAGE) {
                        imageMaps.put(new Pair<String, Rectangle>(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(), false)
                        + getInsideBorderOffset(box.getRightPen().getLineWidth().floatValue(), true);
                heightDiff = getInsideBorderOffset(box.getTopPen().getLineWidth().floatValue(), false)
                        + getInsideBorderOffset(box.getBottomPen().getLineWidth().floatValue(), true);
            }

            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. 
                Renderable tmpRenderer = RenderableUtil.getInstance(jasperReportsContext)
                        .getOnErrorRendererForDimension(renderer, image.getOnErrorTypeValue());
                Dimension2D dimension = tmpRenderer == null ? null
                        : tmpRenderer.getDimension(jasperReportsContext);
                // 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(), false)
                        + getInsideBorderOffset(box.getRightPen().getLineWidth().floatValue(), true);
                heightDiff = getInsideBorderOffset(box.getTopPen().getLineWidth().floatValue(), false)
                        + getInsideBorderOffset(box.getBottomPen().getLineWidth().floatValue(), true);
            }

            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. 
                Renderable tmpRenderer = RenderableUtil.getInstance(jasperReportsContext)
                        .getOnErrorRendererForDimension(renderer, image.getOnErrorTypeValue());
                Dimension2D dimension = tmpRenderer == null ? null
                        : tmpRenderer.getDimension(jasperReportsContext);
                // 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(), false)
                        + getInsideBorderOffset(box.getRightPen().getLineWidth().floatValue(), true);
                heightDiff = getInsideBorderOffset(box.getTopPen().getLineWidth().floatValue(), false)
                        + getInsideBorderOffset(box.getBottomPen().getLineWidth().floatValue(), true);
            }

            if (availableImageHeight > 0) {
                double ratio = normalWidth / normalHeight;

                if (ratio > (double) availableImageWidth / (double) availableImageHeight) {
                    writer.write(" style=\"position:absolute;left:");
                    writer.write(toSizeUnit(leftDiff));
                    writer.write(";top:");
                    writer.write(toSizeUnit((int) (topDiff + yAlignFactor * (availableImageHeight - heightDiff
                            - (availableImageWidth - widthDiff) / ratio))));
                    writer.write(";width:");
                    writer.write(toSizeUnit(availableImageWidth - widthDiff));
                    writer.write("\"");
                } else {
                    writer.write(" style=\"position:absolute;left:");
                    //writer.write(String.valueOf(leftDiff));
                    writer.write(toSizeUnit((int) (leftDiff + xAlignFactor * (availableImageWidth - widthDiff
                            - ratio * (availableImageHeight - heightDiff)))));
                    writer.write(";top:");
                    writer.write(toSizeUnit(topDiff));
                    writer.write(";height:");
                    writer.write(toSizeUnit(availableImageHeight - heightDiff));
                    writer.write("\"");
                }
            }
        }
        }

        if (imageMapName != null) {
            writer.write(" usemap=\"#" + imageMapName + "\"");
        }

        writer.write(" alt=\"\"");

        if (hasHyperlinks) {
            writer.write(" border=\"0\"");
        }

        if (image.getHyperlinkTooltip() != null) {
            writer.write(" title=\"");
            writer.write(JRStringUtil.xmlEncode(image.getHyperlinkTooltip()));
            writer.write("\"");
        }

        writer.write("/>");

        endHyperlink();

        if (imageMapAreas != null) {
            writer.write("\n");
            writeImageMap(imageMapName, image, imageMapAreas);
        }
    }
    writer.write("</span>\n");
}