diff options
Diffstat (limited to 'src/java/org/apache/fop/render/afp/AFPSVGHandler.java')
-rw-r--r-- | src/java/org/apache/fop/render/afp/AFPSVGHandler.java | 230 |
1 files changed, 93 insertions, 137 deletions
diff --git a/src/java/org/apache/fop/render/afp/AFPSVGHandler.java b/src/java/org/apache/fop/render/afp/AFPSVGHandler.java index 3bdab289c..2800686d9 100644 --- a/src/java/org/apache/fop/render/afp/AFPSVGHandler.java +++ b/src/java/org/apache/fop/render/afp/AFPSVGHandler.java @@ -22,39 +22,32 @@ package org.apache.fop.render.afp; // FOP import java.awt.Dimension; import java.awt.geom.AffineTransform; -import java.awt.geom.Dimension2D; import java.io.IOException; -import java.util.Map; -import org.apache.avalon.framework.configuration.Configuration; import org.apache.batik.bridge.BridgeContext; -import org.apache.batik.bridge.BridgeException; -import org.apache.batik.bridge.GVTBuilder; -import org.apache.batik.dom.AbstractDocument; import org.apache.batik.dom.svg.SVGDOMImplementation; import org.apache.batik.gvt.GraphicsNode; -import org.apache.fop.afp.AFPForeignAttributeReader; import org.apache.fop.afp.AFPGraphics2D; import org.apache.fop.afp.AFPGraphicsObjectInfo; import org.apache.fop.afp.AFPObjectAreaInfo; import org.apache.fop.afp.AFPPaintingState; import org.apache.fop.afp.AFPResourceInfo; -import org.apache.fop.afp.AFPResourceLevel; import org.apache.fop.afp.AFPResourceManager; -import org.apache.fop.afp.AFPTextElementBridge; -import org.apache.fop.afp.AFPTextHandler; -import org.apache.fop.afp.AFPTextPainter; import org.apache.fop.afp.AFPUnitConverter; import org.apache.fop.afp.Graphics2DImagePainterGOCA; +import org.apache.fop.afp.svg.AFPBridgeContext; +import org.apache.fop.apps.FOUserAgent; import org.apache.fop.fonts.FontInfo; import org.apache.fop.render.AbstractGenericSVGHandler; import org.apache.fop.render.Renderer; import org.apache.fop.render.RendererContext; -import org.apache.fop.render.RendererContextConstants; import org.apache.fop.render.RendererContext.RendererContextWrapper; import org.apache.fop.svg.SVGEventProducer; import org.apache.fop.svg.SVGUserAgent; +import org.apache.xmlgraphics.image.loader.ImageManager; +import org.apache.xmlgraphics.image.loader.ImageSessionContext; import org.apache.xmlgraphics.java2d.Graphics2DImagePainter; +import org.apache.xmlgraphics.util.MimeConstants; import org.w3c.dom.Document; /** @@ -74,177 +67,138 @@ public class AFPSVGHandler extends AbstractGenericSVGHandler { } } - /** - * Get the AFP information from the render context. - * - * @param context the renderer context - * @return the AFP information retrieved from the context - */ - public static AFPInfo getAFPInfo(RendererContext context) { - AFPInfo afpi = new AFPInfo(); - afpi.setWidth(((Integer)context.getProperty(WIDTH)).intValue()); - afpi.setHeight(((Integer)context.getProperty(HEIGHT)).intValue()); - afpi.setX(((Integer)context.getProperty(XPOS)).intValue()); - afpi.setY(((Integer)context.getProperty(YPOS)).intValue()); - afpi.setHandlerConfiguration((Configuration)context.getProperty(HANDLER_CONFIGURATION)); - afpi.setFontInfo((org.apache.fop.fonts.FontInfo)context.getProperty( - AFPRendererContextConstants.AFP_FONT_INFO)); - afpi.setPaintingState((AFPPaintingState)context.getProperty( - AFPRendererContextConstants.AFP_PAINTING_STATE)); - afpi.setResourceManager(((AFPResourceManager)context.getProperty( - AFPRendererContextConstants.AFP_RESOURCE_MANAGER))); - - Map foreignAttributes = (Map)context.getProperty(RendererContextConstants.FOREIGN_ATTRIBUTES); - if (foreignAttributes != null) { - String conversionMode = (String)foreignAttributes.get(CONVERSION_MODE); - boolean paintAsBitmap = BITMAP.equalsIgnoreCase(conversionMode); - afpi.setPaintAsBitmap(paintAsBitmap); - - AFPForeignAttributeReader foreignAttributeReader = new AFPForeignAttributeReader(); - AFPResourceInfo resourceInfo = foreignAttributeReader.getResourceInfo(foreignAttributes); - // default to inline level if painted as GOCA - if (!resourceInfo.levelChanged() && !paintAsBitmap) { - resourceInfo.setLevel(new AFPResourceLevel(AFPResourceLevel.INLINE)); - } - afpi.setResourceInfo(resourceInfo); - } - return afpi; - } - private static final int X = 0; private static final int Y = 1; /** * Render the SVG document. * - * @param context the renderer context + * @param rendererContext the renderer context * @param doc the SVG document * @throws IOException In case of an I/O error while painting the image */ - protected void renderSVGDocument(final RendererContext context, + protected void renderSVGDocument(final RendererContext rendererContext, final Document doc) throws IOException { - AFPInfo afpInfo = getAFPInfo(context); + AFPRendererContext afpRendererContext = (AFPRendererContext)rendererContext; + AFPInfo afpInfo = afpRendererContext.getInfo(); this.paintAsBitmap = afpInfo.paintAsBitmap(); + FOUserAgent userAgent = rendererContext.getUserAgent(); + // fallback paint as bitmap + String uri = getDocumentURI(doc); if (paintAsBitmap) { try { - super.renderSVGDocument(context, doc); + super.renderSVGDocument(rendererContext, doc); } catch (IOException ioe) { SVGEventProducer eventProducer = SVGEventProducer.Provider.get( - context.getUserAgent().getEventBroadcaster()); - eventProducer.svgRenderingError(this, ioe, getDocumentURI(doc)); + userAgent.getEventBroadcaster()); + eventProducer.svgRenderingError(this, ioe, uri); } return; } - String uri = ((AbstractDocument)doc).getDocumentURI(); - AFPPaintingState paintingState = afpInfo.getPaintingState(); + // Create a new AFPGraphics2D + final boolean textAsShapes = false; + AFPGraphics2D g2d = afpInfo.createGraphics2D(textAsShapes); + + AFPPaintingState paintingState = g2d.getPaintingState(); paintingState.setImageUri(uri); + // Create an AFPBridgeContext + BridgeContext bridgeContext = createBridgeContext(userAgent, g2d); + + // Build the SVG DOM and provide the painter with it + GraphicsNode root = buildGraphicsNode(userAgent, bridgeContext, doc); + + // Create Graphics2DImagePainter + final RendererContextWrapper wrappedContext + = RendererContext.wrapRendererContext(rendererContext); + Dimension imageSize = getImageSize(wrappedContext); + Graphics2DImagePainter painter + = createGrapics2DImagePainter(bridgeContext, root, imageSize); + + // Create AFPObjectAreaInfo + RendererContextWrapper rctx = RendererContext.wrapRendererContext(rendererContext); + int x = rctx.getCurrentXPosition(); + int y = rctx.getCurrentYPosition(); + int width = afpInfo.getWidth(); + int height = afpInfo.getHeight(); + int resolution = afpInfo.getResolution(); + + paintingState.push(); // save + + AFPObjectAreaInfo objectAreaInfo + = createObjectAreaInfo(paintingState, x, y, width, height, resolution); + + // Create AFPGraphicsObjectInfo + AFPResourceInfo resourceInfo = afpInfo.getResourceInfo(); + AFPGraphicsObjectInfo graphicsObjectInfo = createGraphicsObjectInfo( + paintingState, painter, userAgent, resourceInfo, g2d); + graphicsObjectInfo.setObjectAreaInfo(objectAreaInfo); + + // Create the GOCA GraphicsObject in the DataStream + AFPResourceManager resourceManager = afpInfo.getResourceManager(); + resourceManager.createObject(graphicsObjectInfo); + + paintingState.pop(); // resume + } + + private AFPObjectAreaInfo createObjectAreaInfo(AFPPaintingState paintingState, + int x, int y, int width, int height, int resolution) { // set the data object parameters AFPObjectAreaInfo objectAreaInfo = new AFPObjectAreaInfo(); - RendererContextWrapper rctx = RendererContext.wrapRendererContext(context); - int currx = rctx.getCurrentXPosition(); - int curry = rctx.getCurrentYPosition(); - float[] srcPts = {currx, curry}; - - AFPUnitConverter unitConv = paintingState.getUnitConverter(); - int[] coords = unitConv.mpts2units(srcPts); - objectAreaInfo.setX(coords[X]); - objectAreaInfo.setY(coords[Y]); + AffineTransform at = paintingState.getData().getTransform(); + at.translate(x, y); + objectAreaInfo.setX((int)Math.round(at.getTranslateX())); + objectAreaInfo.setY((int)Math.round(at.getTranslateY())); - int resolution = afpInfo.getResolution(); objectAreaInfo.setWidthRes(resolution); objectAreaInfo.setHeightRes(resolution); - int width = Math.round(unitConv.mpt2units(afpInfo.getWidth())); - objectAreaInfo.setWidth(width); - - int height = Math.round(unitConv.mpt2units(afpInfo.getHeight())); - objectAreaInfo.setHeight(height); + AFPUnitConverter unitConv = paintingState.getUnitConverter(); + objectAreaInfo.setWidth(Math.round(unitConv.mpt2units(width))); + objectAreaInfo.setHeight(Math.round(unitConv.mpt2units(height))); int rotation = paintingState.getRotation(); objectAreaInfo.setRotation(rotation); - AFPGraphicsObjectInfo graphicsObjectInfo = new AFPGraphicsObjectInfo(); - graphicsObjectInfo.setUri(uri); + return objectAreaInfo; + } - // Configure Graphics2D implementation - final boolean textAsShapes = false; - AFPGraphics2D g2d = new AFPGraphics2D(textAsShapes); + private AFPGraphicsObjectInfo createGraphicsObjectInfo(AFPPaintingState paintingState, Graphics2DImagePainter painter, + FOUserAgent userAgent, AFPResourceInfo resourceInfo, AFPGraphics2D g2d) { + AFPGraphicsObjectInfo graphicsObjectInfo = new AFPGraphicsObjectInfo(); - g2d.setPaintingState(paintingState); + String uri = paintingState.getImageUri(); + graphicsObjectInfo.setUri(uri); - AFPResourceManager resourceManager = afpInfo.getResourceManager(); - g2d.setResourceManager(resourceManager); + graphicsObjectInfo.setMimeType(MimeConstants.MIME_AFP_GOCA); - AFPResourceInfo resourceInfo = afpInfo.getResourceInfo(); - g2d.setResourceInfo(resourceInfo); graphicsObjectInfo.setResourceInfo(resourceInfo); - g2d.setGraphicContext(new org.apache.xmlgraphics.java2d.GraphicContext()); - - FontInfo fontInfo = afpInfo.getFontInfo(); - g2d.setFontInfo(fontInfo); - - // Configure GraphicsObjectPainter with the Graphics2D implementation - GraphicsObjectPainterAFP painter = new GraphicsObjectPainterAFP(g2d); - (graphicsObjectInfo).setPainter(painter); + graphicsObjectInfo.setPainter(painter); - // Controls whether text painted by Batik is generated using text or path operations - SVGUserAgent svgUserAgent - = new SVGUserAgent(context.getUserAgent(), new AffineTransform()); - BridgeContext ctx = new BridgeContext(svgUserAgent); - if (!afpInfo.strokeText()) { - AFPTextHandler textHandler = new AFPTextHandler(g2d); - g2d.setCustomTextHandler(textHandler); - AFPTextPainter textPainter = new AFPTextPainter(textHandler); - ctx.setTextPainter(textPainter); - AFPTextElementBridge tBridge = new AFPTextElementBridge(textPainter); - ctx.putBridge(tBridge); - } + // Set the afp graphics 2d implementation + graphicsObjectInfo.setGraphics2D(g2d); - // Build the SVG DOM and provide the painter with it - GraphicsNode root; - GVTBuilder builder = new GVTBuilder(); - try { - root = builder.build(ctx, doc); - painter.setGraphicsNode(root); - } catch (BridgeException e) { - SVGEventProducer eventProducer = SVGEventProducer.Provider.get( - context.getUserAgent().getEventBroadcaster()); - eventProducer.svgNotBuilt(this, e, uri); - return; - } + return graphicsObjectInfo; + } - // convert to afp inches - Dimension2D dim = ctx.getDocumentSize(); - double w = dim.getWidth() * 1000f; - double h = dim.getHeight() * 1000f; - double wx = (afpInfo.getWidth() / w); - double hx = (afpInfo.getHeight() / h); - double scaleX = unitConv.pt2units((float)wx); - double scaleY = unitConv.pt2units((float)hx); - double yOffset = unitConv.mpt2units(afpInfo.getHeight()); - - // Transformation matrix that establishes the local coordinate system - // for the SVG graphic in relation to the current coordinate system - // (note: y axis is inverted) - AffineTransform trans = new AffineTransform(scaleX, 0, 0, -scaleY, 0, yOffset); - g2d.setTransform(trans); + public static BridgeContext createBridgeContext(FOUserAgent userAgent, AFPGraphics2D g2d) { + ImageManager imageManager = userAgent.getFactory().getImageManager(); - // Set the afp graphics 2d implementation - graphicsObjectInfo.setGraphics2D(g2d); + SVGUserAgent svgUserAgent + = new SVGUserAgent(userAgent, new AffineTransform()); - // Set the object area info - graphicsObjectInfo.setObjectAreaInfo(objectAreaInfo); + ImageSessionContext imageSessionContext = userAgent.getImageSessionContext(); - // Create the graphics object - resourceManager.createObject(graphicsObjectInfo); + FontInfo fontInfo = g2d.getFontInfo(); + return new AFPBridgeContext(svgUserAgent, fontInfo, imageManager, imageSessionContext, + new AffineTransform(), g2d); } /** {@inheritDoc} */ @@ -259,11 +213,13 @@ public class AFPSVGHandler extends AbstractGenericSVGHandler { } /** {@inheritDoc} */ - protected Graphics2DImagePainter createPainter(BridgeContext ctx, GraphicsNode root, Dimension imageSize) { + protected Graphics2DImagePainter createGrapics2DImagePainter(BridgeContext ctx, GraphicsNode root, Dimension imageSize) { Graphics2DImagePainter painter = null; if (paintAsBitmap()) { - painter = super.createPainter(root, ctx, imageSize); + // paint as IOCA Image + painter = super.createGraphics2DImagePainter(root, ctx, imageSize); } else { + // paint as GOCA Graphics painter = new Graphics2DImagePainterGOCA(root, ctx, imageSize); } return painter; |