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:net.sf.jasperreports.engine.export.HtmlExporter.java

protected void writeImage(JRPrintImage image, TableCell cell) throws IOException, JRException {
    startCell(image, cell);/*w w  w  . j av  a 2 s.c om*/

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

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

    String horizontalAlignment = getImageHorizontalAlignmentStyle(image);
    String verticalAlignment = getImageVerticalAlignmentStyle(image);

    StringBuilder styleBuffer = new StringBuilder();
    ScaleImageEnum scaleImage = image.getScaleImageValue();
    if (scaleImage != ScaleImageEnum.CLIP) {
        // clipped images are absolutely positioned within a div
        if (!horizontalAlignment.equals(CSS_TEXT_ALIGN_LEFT)) {
            styleBuffer.append("text-align: ");
            styleBuffer.append(horizontalAlignment);
            styleBuffer.append(";");
        }

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

    Renderable renderer = image.getRenderer();

    boolean isLazy = RendererUtil.isLazy(renderer);

    if (isLazy || (scaleImage == ScaleImageEnum.CLIP && availableImageHeight > 0)) {
        // some browsers need td height so that height: 100% works on the div used for clipped images.
        // we're using the height without paddings because that's closest to the HTML size model.
        styleBuffer.append("height: ");
        styleBuffer.append(toSizeUnit(availableImageHeight));
        styleBuffer.append("; ");
    }

    appendElementCellGenericStyle(cell, styleBuffer);
    appendBackcolorStyle(cell, styleBuffer);

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

    appendPaddingStyle(image.getLineBox(), styleBuffer);

    writeStyle(styleBuffer);

    finishStartCell();

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

    if (image.getBookmarkLevel() != JRAnchor.NO_BOOKMARK) {
        writer.write("<a name=\"");
        writer.write(
                JR_BOOKMARK_ANCHOR_PREFIX + reportIndex + "_" + pageIndex + "_" + cell.getElementAddress());
        writer.write("\"/>");
    }

    if (renderer != null) {
        boolean useBackgroundLazyImage = isLazy
                && ((scaleImage == ScaleImageEnum.RETAIN_SHAPE || scaleImage == ScaleImageEnum.REAL_HEIGHT
                        || scaleImage == ScaleImageEnum.REAL_SIZE)
                        || !(image.getHorizontalImageAlign() == HorizontalImageAlignEnum.LEFT
                                && image.getVerticalImageAlign() == VerticalImageAlignEnum.TOP))
                && isUseBackgroundImageToAlign(image);

        boolean useDiv = (scaleImage == ScaleImageEnum.CLIP || useBackgroundLazyImage);
        if (useDiv) {
            writer.write("<div style=\"width: 100%; height: 100%; position: relative; overflow: hidden;\">\n");
        }

        boolean hasAreaHyperlinks = renderer instanceof AreaHyperlinksRenderable
                && ((AreaHyperlinksRenderable) renderer).hasImageAreaHyperlinks();

        boolean hasHyperlinks = false;

        boolean hyperlinkStarted;
        if (hasAreaHyperlinks) {
            hyperlinkStarted = false;
            hasHyperlinks = true;
        } else {
            hyperlinkStarted = startHyperlink(image);
            hasHyperlinks = hyperlinkStarted;
        }

        String imageMapName = null;
        List<JRPrintImageAreaHyperlink> imageMapAreas = null;

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

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

            if (imageMapName == null) {
                Renderable originalRenderer = image.getRenderer();
                imageMapName = "map_" + getElementIndex(cell).toString() + "-" + originalRenderer.getId();//use renderer.getId()?
                imageMapAreas = ((AreaHyperlinksRenderable) originalRenderer)
                        .getImageAreaHyperlinks(renderingArea);//FIXMECHART

                if (renderer instanceof DataRenderable) {
                    imageMaps.put(new Pair<String, Rectangle>(renderer.getId(), renderingArea), imageMapName);
                }
            }
        }

        InternalImageProcessor imageProcessor = new InternalImageProcessor(image, isLazy,
                !useBackgroundLazyImage && scaleImage != ScaleImageEnum.FILL_FRAME && !isLazy, cell,
                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) {
            if (useBackgroundLazyImage) {
                writer.write("<div style=\"width: 100%; height: 100%; background-image: url('");
                String imagePath = imageProcessorResult.imageSource;
                if (imagePath != null) {
                    writer.write(JRStringUtil.encodeXmlAttribute(imagePath));
                }
                writer.write("'); background-repeat: no-repeat; background-position: " + horizontalAlignment
                        + " " + (image.getVerticalImageAlign() == VerticalImageAlignEnum.MIDDLE ? "center"
                                : verticalAlignment)
                        + ";background-size: ");

                switch (scaleImage) {
                case FILL_FRAME: {
                    writer.write("100% 100%");
                    break;
                }
                case CLIP: {
                    writer.write("auto");
                    break;
                }
                case RETAIN_SHAPE:
                default: {
                    writer.write("contain");
                }
                }
                writer.write(";\"></div>");
            } else if (imageProcessorResult.isEmbededSvgData) {
                writer.write("<svg");

                switch (scaleImage) {
                case FILL_FRAME: {
                    Dimension2D dimension = imageProcessorResult.dimension;
                    if (dimension != null) {
                        writer.write(" viewBox=\"0 0 ");
                        writer.write(String.valueOf(dimension.getWidth()));
                        writer.write(" ");
                        writer.write(String.valueOf(dimension.getHeight()));
                        writer.write("\"");
                    }

                    writer.write(" width=\"");
                    writer.write(String.valueOf(availableImageWidth));
                    writer.write("\"");
                    writer.write(" height=\"");
                    writer.write(String.valueOf(availableImageHeight));
                    writer.write("\"");
                    writer.write(" preserveAspectRatio=\"none\"");

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

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

                        writer.write(" viewBox=\"");
                        writer.write(String.valueOf((int) (ImageUtil.getXAlignFactor(image)
                                * (normalWidth - availableImageWidth))));
                        writer.write(" ");
                        writer.write(String.valueOf((int) (ImageUtil.getYAlignFactor(image)
                                * (normalHeight - availableImageHeight))));
                        writer.write(" ");
                        writer.write(String.valueOf(availableImageWidth));
                        writer.write(" ");
                        writer.write(String.valueOf(availableImageHeight));
                        writer.write("\"");
                    }

                    writer.write(" width=\"");
                    writer.write(String.valueOf(availableImageWidth));
                    writer.write("\"");
                    writer.write(" height=\"");
                    writer.write(String.valueOf(availableImageHeight));
                    writer.write("\"");

                    break;
                }
                case RETAIN_SHAPE:
                default: {
                    //considering the IF above, if we get here, then for sure isLazy() is false, so we can ask the renderer for its dimension
                    if (availableImageHeight > 0) {
                        double normalWidth = availableImageWidth;
                        double normalHeight = availableImageHeight;

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

                            writer.write(" viewBox=\"0 0 ");
                            writer.write(String.valueOf(normalWidth));
                            writer.write(" ");
                            writer.write(String.valueOf(normalHeight));
                            writer.write("\"");
                        }

                        double ratio = normalWidth / normalHeight;

                        if (ratio > (double) availableImageWidth / (double) availableImageHeight) {
                            writer.write(" width=\"");
                            writer.write(String.valueOf(availableImageWidth));
                            writer.write("\"");
                        } else {
                            writer.write(" height=\"");
                            writer.write(String.valueOf(availableImageHeight));
                            writer.write("\"");
                        }
                    }
                }
                }

                writer.write("><g>\n");
                writer.write(imageProcessorResult.imageSource);
                writer.write("</g></svg>");
            } else {
                writer.write("<img");
                writer.write(" src=\"");
                String imagePath = imageProcessorResult.imageSource;
                if (imagePath != null) {
                    writer.write(JRStringUtil.encodeXmlAttribute(imagePath));
                }
                writer.write("\"");

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

                    break;
                }
                case CLIP: {
                    int positionLeft;
                    int positionTop;

                    HorizontalImageAlignEnum horizontalAlign = image.getHorizontalImageAlign();
                    VerticalImageAlignEnum verticalAlign = image.getVerticalImageAlign();
                    if (isLazy || (horizontalAlign == HorizontalImageAlignEnum.LEFT
                            && verticalAlign == VerticalImageAlignEnum.TOP)) {
                        // no need to compute anything
                        positionLeft = 0;
                        positionTop = 0;
                    } else {
                        double normalWidth = availableImageWidth;
                        double normalHeight = availableImageHeight;

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

                        // these calculations assume that the image td does not stretch due to other cells.
                        // when that happens, the image will not be properly aligned.
                        positionLeft = (int) (ImageUtil.getXAlignFactor(horizontalAlign)
                                * (availableImageWidth - normalWidth));
                        positionTop = (int) (ImageUtil.getYAlignFactor(verticalAlign)
                                * (availableImageHeight - normalHeight));
                    }

                    writer.write(" style=\"position: absolute; left:");
                    writer.write(toSizeUnit(positionLeft));
                    writer.write("; top: ");
                    writer.write(toSizeUnit(positionTop));
                    // not setting width, height and clip as it doesn't seem needed plus it fixes clip for lazy images
                    writer.write(";\"");

                    break;
                }
                case RETAIN_SHAPE:
                default: {
                    //considering the IF above, if we get here, then for sure isLazy() is false, so we can ask the renderer for its dimension
                    if (availableImageHeight > 0) {
                        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 > (double) availableImageWidth / (double) availableImageHeight) {
                            writer.write(" style=\"width: ");
                            writer.write(toSizeUnit(availableImageWidth));
                            writer.write("\"");
                        } else {
                            writer.write(" style=\"height: ");
                            writer.write(toSizeUnit(availableImageHeight));
                            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.encodeXmlAttribute(image.getHyperlinkTooltip()));
                    writer.write("\"");
                }

                writer.write("/>");
            }
        }

        if (hyperlinkStarted) {
            endHyperlink();
        }

        if (useDiv) {
            writer.write("</div>");
        }

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

    endCell();
}

From source file:org.apache.fop.render.ps.ResourceHandler.java

private static FormGenerator createMissingForm(String formName, final Dimension2D dimensions) {
    FormGenerator formGen = new FormGenerator(formName, null, dimensions) {

        protected void generatePaintProc(PSGenerator gen) throws IOException {
            gen.writeln("0 setgray");
            gen.writeln("0 setlinewidth");
            String w = gen.formatDouble(dimensions.getWidth());
            String h = gen.formatDouble(dimensions.getHeight());
            gen.writeln(w + " " + h + " scale");
            gen.writeln("0 0 1 1 rectstroke");
            gen.writeln("newpath");
            gen.writeln("0 0 moveto");
            gen.writeln("1 1 lineto");
            gen.writeln("stroke");
            gen.writeln("newpath");
            gen.writeln("0 1 moveto");
            gen.writeln("1 0 lineto");
            gen.writeln("stroke");
        }/*from ww  w.j  a va2 s .c o m*/

    };
    return formGen;
}

From source file:org.apache.xmlgraphics.ps.PSImageUtils.java

/**
 * Generates commands to modify the current transformation matrix so an image fits
 * into a given rectangle./*w  w w . jav  a2 s.c  o m*/
 * @param gen the PostScript generator
 * @param imageDimensions the image's dimensions
 * @param targetRect the target rectangle
 * @throws IOException if an I/O error occurs
 */
public static void translateAndScale(PSGenerator gen, Dimension2D imageDimensions, Rectangle2D targetRect)
        throws IOException {
    gen.writeln(gen.formatDouble(targetRect.getX()) + " " + gen.formatDouble(targetRect.getY()) + " translate");
    if (imageDimensions == null) {
        imageDimensions = new Dimension(1, 1);
    }
    double sx = targetRect.getWidth() / imageDimensions.getWidth();
    double sy = targetRect.getHeight() / imageDimensions.getHeight();
    if (sx != 1 || sy != 1) {
        gen.writeln(gen.formatDouble(sx) + " " + gen.formatDouble(sy) + " scale");
    }
}

From source file:org.uva.itast.blended.omr.scanners.SolidSquareMarkScanner.java

/**
 * @param pageImage/*from www.j av  a2 s. c o m*/
 */
public void putEmphasisMarkOnImage(PageImage pageImage, Color color) {

    Graphics2D g = pageImage.getReportingGraphics();
    // int centerColor=imagen.getRGB(maxsimX, maxsimY);
    // g.setXORMode(new Color(centerColor));
    // g.setColor(Color.RED);
    // g.fillOval(maxsimX - markWidth/2, maxsimY - markHeight/2, markWidth,
    // markHeight);
    // g.setPaintMode();
    Dimension2D markDimsPx = pageImage.sizeInPixels(new Size(markWidth, markHeight));
    int markWidth = (int) markDimsPx.getWidth();
    int markHeight = (int) markDimsPx.getHeight();
    g.setColor(color);
    AffineTransform t = g.getTransform();
    g.drawLine(maxsimX, maxsimY - markHeight / 2 - 1, maxsimX,
            maxsimY - markHeight / 2 - (int) (20 / t.getScaleY()));
    Polygon arrowHead = new Polygon();
    arrowHead.addPoint(maxsimX, (int) (maxsimY - markHeight / 2 - 1 / t.getScaleY()));
    arrowHead.addPoint((int) (maxsimX - 6 / t.getScaleX()),
            (int) (maxsimY - markHeight / 2 - 6 / t.getScaleY()));
    arrowHead.addPoint((int) (maxsimX + 6 / t.getScaleX()),
            (int) (maxsimY - markHeight / 2 - 6 / t.getScaleY()));
    g.fillPolygon(arrowHead);

    g.setStroke(new BasicStroke(2, BasicStroke.CAP_SQUARE, BasicStroke.JOIN_ROUND, 1,
            new float[] { (float) (3 / t.getScaleX()), (float) (3 / t.getScaleY()) }, 0));
    g.drawRect(maxsimX - markWidth / 2 - 1, maxsimY - markHeight / 2 - 1, markWidth + 1, markHeight + 1);

}