Example usage for java.awt.geom Dimension2D getWidth

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

Introduction

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

Prototype

public abstract double getWidth();

Source Link

Document

Returns the width 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);//from  w w w .ja v  a  2  s.co  m

    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.fonts.type1.AFMFile.java

/**
 * Creates and returns a kerning map for writing mode 0 (ltr) with character codes.
 * @return the kerning map or null if there is no kerning information.
 *//*from  w  w w.  j  av a2s.co m*/
public Map<Integer, Map<Integer, Integer>> createXKerningMapEncoded() {
    if (!hasKerning()) {
        return null;
    }
    Map<Integer, Map<Integer, Integer>> m = new java.util.HashMap<Integer, Map<Integer, Integer>>();
    for (Map.Entry<String, Map<String, Dimension2D>> entryFrom : this.kerningMap.entrySet()) {
        String name1 = entryFrom.getKey();
        AFMCharMetrics chm1 = getChar(name1);
        if (chm1 == null || !chm1.hasCharCode()) {
            continue;
        }
        Map<Integer, Integer> container = null;
        Map<String, Dimension2D> entriesTo = entryFrom.getValue();
        for (Map.Entry<String, Dimension2D> entryTo : entriesTo.entrySet()) {
            String name2 = entryTo.getKey();
            AFMCharMetrics chm2 = getChar(name2);
            if (chm2 == null || !chm2.hasCharCode()) {
                continue;
            }
            if (container == null) {
                Integer k1 = Integer.valueOf(chm1.getCharCode());
                container = m.get(k1);
                if (container == null) {
                    container = new java.util.HashMap<Integer, Integer>();
                    m.put(k1, container);
                }
            }
            Dimension2D dim = entryTo.getValue();
            container.put(Integer.valueOf(chm2.getCharCode()),
                    Integer.valueOf((int) Math.round(dim.getWidth())));
        }
    }
    return m;
}

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");
        }// w  w  w.  j  a  v a2s  .  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.// ww  w.j  av a 2  s .co  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// w w w  .j  a v  a  2  s  . co  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);

}