Example usage for java.awt.geom AffineTransform isIdentity

List of usage examples for java.awt.geom AffineTransform isIdentity

Introduction

In this page you can find the example usage for java.awt.geom AffineTransform isIdentity.

Prototype

public boolean isIdentity() 

Source Link

Document

Returns true if this AffineTransform is an identity transform.

Usage

From source file:DefaultGraphics2D.java

/**
 * Sets the <code>Transform</code> in the <code>Graphics2D</code> context.
 * /*from   w ww.jav  a2  s.  c om*/
 * @param Tx
 *          the <code>AffineTransform</code> object to be used in the
 *          rendering process
 * @see #transform
 * @see AffineTransform
 */
public void setTransform(AffineTransform Tx) {
    transform = new AffineTransform(Tx);
    invalidateTransformStack();
    if (!Tx.isIdentity())
        transformStack.add(TransformStackElement.createGeneralTransformElement(Tx));
}

From source file:org.apache.fop.render.intermediate.IFRenderer.java

/** {@inheritDoc} */
protected void concatenateTransformationMatrix(AffineTransform at) {
    if (!at.isIdentity()) {
        concatenateTransformationMatrixMpt(ptToMpt(at), false);
    }// w  w w . j a  va  2 s  .com
}

From source file:org.apache.fop.render.intermediate.IFRenderer.java

private void concatenateTransformationMatrixMpt(AffineTransform at, boolean force) {
    if (force || !at.isIdentity()) {
        if (log.isTraceEnabled()) {
            log.trace("-----concatenateTransformationMatrix: " + at);
        }/*  www .  jav a 2 s .c  om*/
        IFGraphicContext.Group group = new IFGraphicContext.Group(at);
        pushGroup(group);
    }
}

From source file:org.apache.fop.render.java2d.Java2DSVGHandler.java

/** {@inheritDoc} */
protected void renderSVGDocument(RendererContext context, Document doc) {
    Java2DInfo info = getJava2DInfo(context);
    if (log.isDebugEnabled()) {
        log.debug("renderSVGDocument(" + context + ", " + doc + ", " + info + ")");
    }/*from  w  w  w  .  j a va 2s .  c o  m*/

    // fallback paint as bitmap
    if (info.paintAsBitmap) {
        try {
            super.renderSVGDocument(context, doc);
        } catch (IOException ioe) {
            SVGEventProducer eventProducer = SVGEventProducer.Provider
                    .get(context.getUserAgent().getEventBroadcaster());
            eventProducer.svgRenderingError(this, ioe, getDocumentURI(doc));
        }
        return;
    }

    int x = info.currentXPosition;
    int y = info.currentYPosition;

    SVGUserAgent ua = new SVGUserAgent(context.getUserAgent(), new AffineTransform());

    BridgeContext ctx = new BridgeContext(ua);

    //Cloning SVG DOM as Batik attaches non-thread-safe facilities (like the CSS engine)
    //to it.
    Document clonedDoc = BatikUtil.cloneSVGDocument(doc);

    GraphicsNode root;
    try {
        GVTBuilder builder = new GVTBuilder();
        root = builder.build(ctx, clonedDoc);
    } catch (Exception e) {
        SVGEventProducer eventProducer = SVGEventProducer.Provider
                .get(context.getUserAgent().getEventBroadcaster());
        eventProducer.svgNotBuilt(this, e, getDocumentURI(doc));
        return;
    }

    // If no viewbox is defined in the svg file, a viewbox of 100x100 is
    // assumed, as defined in SVGUserAgent.getViewportSize()
    float iw = (float) ctx.getDocumentSize().getWidth() * 1000f;
    float ih = (float) ctx.getDocumentSize().getHeight() * 1000f;

    float w = info.width;
    float h = info.height;

    AffineTransform origTransform = info.state.getGraph().getTransform();

    // correct integer roundoff
    info.state.getGraph().translate(x / 1000f, y / 1000f);

    //SVGSVGElement svg = ((SVGDocument) doc).getRootElement();
    // Aspect ratio preserved by layout engine, not here
    AffineTransform at = AffineTransform.getScaleInstance(w / iw, h / ih);
    if (!at.isIdentity()) {
        info.state.getGraph().transform(at);
    }

    try {
        root.paint(info.state.getGraph());
    } catch (Exception e) {
        SVGEventProducer eventProducer = SVGEventProducer.Provider
                .get(context.getUserAgent().getEventBroadcaster());
        eventProducer.svgRenderingError(this, e, getDocumentURI(doc));
    }

    info.state.getGraph().setTransform(origTransform);
}

From source file:org.apache.fop.render.pcl.PCLPainter.java

private void concatenateTransformationMatrix(AffineTransform transform) throws IOException {
    if (!transform.isIdentity()) {
        graphicContext.transform(transform);
        changePrintDirection();/*  w w w .  ja  va2s .  c o m*/
    }
}

From source file:org.apache.fop.render.pcl.PCLRenderer.java

/** {@inheritDoc} */
protected void renderReferenceArea(Block block) {
    //TODO This is the same code as in AbstractPathOrientedRenderer
    //So there's some optimization potential but not otherwise PCLRenderer is a little
    //difficult to derive from AbstractPathOrientedRenderer. Maybe an additional layer
    //between PrintRenderer and AbstractPathOrientedRenderer is necessary.

    // save position and offset
    int saveIP = currentIPPosition;
    int saveBP = currentBPPosition;

    //Establish a new coordinate system
    AffineTransform at = new AffineTransform();
    at.translate(currentIPPosition, currentBPPosition);
    at.translate(block.getXOffset(), block.getYOffset());
    at.translate(0, block.getSpaceBefore());

    if (!at.isIdentity()) {
        saveGraphicsState();/*from   w ww  . ja  va  2 s  . co  m*/
        concatenateTransformationMatrix(UnitConv.mptToPt(at));
    }

    currentIPPosition = 0;
    currentBPPosition = 0;
    handleBlockTraits(block);

    List children = block.getChildAreas();
    if (children != null) {
        renderBlocks(block, children);
    }

    if (!at.isIdentity()) {
        restoreGraphicsState();
    }

    // stacked and relative blocks effect stacking
    currentIPPosition = saveIP;
    currentBPPosition = saveBP;
}

From source file:org.apache.fop.render.pcl.PCLRenderer.java

/** {@inheritDoc} */
protected void renderFlow(NormalFlow flow) {
    //TODO This is the same code as in AbstractPathOrientedRenderer
    //So there's some optimization potential but not otherwise PCLRenderer is a little
    //difficult to derive from AbstractPathOrientedRenderer. Maybe an additional layer
    //between PrintRenderer and AbstractPathOrientedRenderer is necessary.

    // save position and offset
    int saveIP = currentIPPosition;
    int saveBP = currentBPPosition;

    //Establish a new coordinate system
    AffineTransform at = new AffineTransform();
    at.translate(currentIPPosition, currentBPPosition);

    if (!at.isIdentity()) {
        saveGraphicsState();// w  ww  .  jav a  2s .  c o m
        concatenateTransformationMatrix(UnitConv.mptToPt(at));
    }

    currentIPPosition = 0;
    currentBPPosition = 0;
    super.renderFlow(flow);

    if (!at.isIdentity()) {
        restoreGraphicsState();
    }

    // stacked and relative blocks effect stacking
    currentIPPosition = saveIP;
    currentBPPosition = saveBP;
}

From source file:org.apache.fop.render.pcl.PCLRenderer.java

/**
 * Concatenates the current transformation matrix with the given one, therefore establishing
 * a new coordinate system.//from  w ww .  j a va  2 s  .c  o m
 * @param at the transformation matrix to process (coordinates in points)
 */
protected void concatenateTransformationMatrix(AffineTransform at) {
    if (!at.isIdentity()) {
        graphicContext.transform(UnitConv.ptToMpt(at));
        changePrintDirection();
    }
}

From source file:org.apache.fop.render.pdf.PDFImageHandlerSVG.java

/** {@inheritDoc} */
public void handleImage(RenderingContext context, // CSOK: MethodLength
        Image image, Rectangle pos) throws IOException {
    PDFRenderingContext pdfContext = (PDFRenderingContext) context;
    PDFContentGenerator generator = pdfContext.getGenerator();
    ImageXMLDOM imageSVG = (ImageXMLDOM) image;

    FOUserAgent userAgent = context.getUserAgent();
    final float deviceResolution = userAgent.getTargetResolution();
    if (log.isDebugEnabled()) {
        log.debug("Generating SVG at " + deviceResolution + "dpi.");
    }/*from  w w w .ja va2 s .  c  om*/

    final float uaResolution = userAgent.getSourceResolution();
    SVGUserAgent ua = new SVGUserAgent(userAgent, new AffineTransform());

    GVTBuilder builder = new GVTBuilder();

    //Controls whether text painted by Batik is generated using text or path operations
    boolean strokeText = false;
    //TODO connect with configuration elsewhere.

    BridgeContext ctx = new PDFBridgeContext(ua, (strokeText ? null : pdfContext.getFontInfo()),
            userAgent.getFactory().getImageManager(), userAgent.getImageSessionContext(),
            new AffineTransform());

    //Cloning SVG DOM as Batik attaches non-thread-safe facilities (like the CSS engine)
    //to it.
    Document clonedDoc = BatikUtil.cloneSVGDocument(imageSVG.getDocument());

    GraphicsNode root;
    try {
        root = builder.build(ctx, clonedDoc);
        builder = null;
    } catch (Exception e) {
        SVGEventProducer eventProducer = SVGEventProducer.Provider
                .get(context.getUserAgent().getEventBroadcaster());
        eventProducer.svgNotBuilt(this, e, image.getInfo().getOriginalURI());
        return;
    }
    // get the 'width' and 'height' attributes of the SVG document
    float w = image.getSize().getWidthMpt();
    float h = image.getSize().getHeightMpt();

    float sx = pos.width / w;
    float sy = pos.height / h;

    //Scaling and translation for the bounding box of the image
    AffineTransform scaling = new AffineTransform(sx, 0, 0, sy, pos.x / 1000f, pos.y / 1000f);
    double sourceScale = UnitConv.IN2PT / uaResolution;
    scaling.scale(sourceScale, sourceScale);

    //Scale for higher resolution on-the-fly images from Batik
    AffineTransform resolutionScaling = new AffineTransform();
    double targetScale = uaResolution / deviceResolution;
    resolutionScaling.scale(targetScale, targetScale);
    resolutionScaling.scale(1.0 / sx, 1.0 / sy);

    //Transformation matrix that establishes the local coordinate system for the SVG graphic
    //in relation to the current coordinate system
    AffineTransform imageTransform = new AffineTransform();
    imageTransform.concatenate(scaling);
    imageTransform.concatenate(resolutionScaling);

    if (log.isTraceEnabled()) {
        log.trace("nat size: " + w + "/" + h);
        log.trace("req size: " + pos.width + "/" + pos.height);
        log.trace("source res: " + uaResolution + ", targetRes: " + deviceResolution + " --> target scaling: "
                + targetScale);
        log.trace(image.getSize());
        log.trace("sx: " + sx + ", sy: " + sy);
        log.trace("scaling: " + scaling);
        log.trace("resolution scaling: " + resolutionScaling);
        log.trace("image transform: " + resolutionScaling);
    }

    /*
     * Clip to the svg area.
     * Note: To have the svg overlay (under) a text area then use
     * an fo:block-container
     */
    if (log.isTraceEnabled()) {
        generator.comment("SVG setup");
    }
    generator.saveGraphicsState();
    if (context.getUserAgent().isAccessibilityEnabled()) {
        MarkedContentInfo mci = pdfContext.getMarkedContentInfo();
        generator.beginMarkedContentSequence(mci.tag, mci.mcid);
    }
    generator.updateColor(Color.black, false, null);
    generator.updateColor(Color.black, true, null);

    if (!scaling.isIdentity()) {
        if (log.isTraceEnabled()) {
            generator.comment("viewbox");
        }
        generator.add(CTMHelper.toPDFString(scaling, false) + " cm\n");
    }

    //SVGSVGElement svg = ((SVGDocument)doc).getRootElement();

    PDFGraphics2D graphics = new PDFGraphics2D(true, pdfContext.getFontInfo(), generator.getDocument(),
            generator.getResourceContext(), pdfContext.getPage().referencePDF(), "", 0);
    graphics.setGraphicContext(new org.apache.xmlgraphics.java2d.GraphicContext());

    if (!resolutionScaling.isIdentity()) {
        if (log.isTraceEnabled()) {
            generator.comment("resolution scaling for " + uaResolution + " -> " + deviceResolution);
        }
        generator.add(CTMHelper.toPDFString(resolutionScaling, false) + " cm\n");
        graphics.scale(1.0 / resolutionScaling.getScaleX(), 1.0 / resolutionScaling.getScaleY());
    }

    if (log.isTraceEnabled()) {
        generator.comment("SVG start");
    }

    //Save state and update coordinate system for the SVG image
    generator.getState().save();
    generator.getState().concatenate(imageTransform);

    //Now that we have the complete transformation matrix for the image, we can update the
    //transformation matrix for the AElementBridge.
    PDFAElementBridge aBridge = (PDFAElementBridge) ctx.getBridge(SVGDOMImplementation.SVG_NAMESPACE_URI,
            SVGConstants.SVG_A_TAG);
    aBridge.getCurrentTransform().setTransform(generator.getState().getTransform());

    graphics.setPaintingState(generator.getState());
    graphics.setOutputStream(generator.getOutputStream());
    try {
        root.paint(graphics);
        ctx.dispose();
        generator.add(graphics.getString());
    } catch (Exception e) {
        SVGEventProducer eventProducer = SVGEventProducer.Provider
                .get(context.getUserAgent().getEventBroadcaster());
        eventProducer.svgRenderingError(this, e, image.getInfo().getOriginalURI());
    }
    generator.getState().restore();
    if (context.getUserAgent().isAccessibilityEnabled()) {
        generator.restoreGraphicsStateAccess();
    } else {
        generator.restoreGraphicsState();
    }
    if (log.isTraceEnabled()) {
        generator.comment("SVG end");
    }
}

From source file:org.apache.pdfbox.pdmodel.font.PDSimpleFont.java

/**
 * This will draw a string on a canvas using the font.
 *
 * @param g2d The graphics to draw onto.
 * @param at The transformation matrix with all information for scaling and shearing of the font.
 * @param x The x coordinate to draw at.
 * @param y The y coordinate to draw at.
 * @param glyphs The GlyphVector containing the glyphs to be drawn.
 *
 *///from  w  w  w  . j a v a2  s. co  m
protected void writeFont(final Graphics2D g2d, final AffineTransform at, final float x, final float y,
        final GlyphVector glyphs) {
    // check if we have a rotation
    if (!at.isIdentity()) {
        try {
            AffineTransform atInv = at.createInverse();
            // do only apply the size of the transform, rotation will be realized by rotating the graphics,
            // otherwise the hp printers will not render the font
            // apply the transformation to the graphics, which should be the same as applying the
            // transformation itself to the text
            g2d.transform(at);
            // translate the coordinates
            Point2D.Float newXy = new Point2D.Float(x, y);
            atInv.transform(new Point2D.Float(x, y), newXy);
            g2d.drawGlyphVector(glyphs, (float) newXy.getX(), (float) newXy.getY());
            // restore the original transformation
            g2d.transform(atInv);
        } catch (NoninvertibleTransformException e) {
            LOG.error("Error in " + getClass().getName() + ".writeFont", e);
        }
    } else {
        g2d.drawGlyphVector(glyphs, x, y);
    }
}