From: Keiron Liddle Date: Fri, 18 May 2001 10:03:18 +0000 (+0000) Subject: now supports size setting properly X-Git-Tag: PRE_CODEFORMATTING~123 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=c0459ac51d9f792eddadd1c44785722db686bf70;p=xmlgraphics-fop.git now supports size setting properly git-svn-id: https://svn.apache.org/repos/asf/xmlgraphics/fop/trunk@194256 13f79535-47bb-0310-9956-ffa450edef68 --- diff --git a/src/org/apache/fop/svg/PDFDocumentGraphics2D.java b/src/org/apache/fop/svg/PDFDocumentGraphics2D.java index 8f93b1034..06bd07009 100644 --- a/src/org/apache/fop/svg/PDFDocumentGraphics2D.java +++ b/src/org/apache/fop/svg/PDFDocumentGraphics2D.java @@ -7,101 +7,123 @@ package org.apache.fop.svg; import org.apache.fop.pdf.*; -import org.apache.fop.layout.*; import org.apache.fop.fonts.*; -import org.apache.fop.render.pdf.*; -import org.apache.fop.image.*; -import org.apache.fop.datatypes.ColorSpace; -import org.apache.batik.ext.awt.g2d.*; - -import java.text.AttributedCharacterIterator; -import java.awt.*; +import java.awt.Graphics; import java.awt.Font; import java.awt.Image; -import java.awt.image.*; -import java.awt.font.*; -import java.awt.geom.*; -import java.awt.image.renderable.*; -import java.io.*; +import java.awt.Color; +import java.io.OutputStream; +import java.io.IOException; -import java.util.Map; +import org.apache.batik.ext.awt.g2d.GraphicContext; /** - * This concrete implementation of AbstractGraphics2D is a - * simple help to programmers to get started with their own - * implementation of Graphics2D. - * DefaultGraphics2D implements all the abstract methods - * is AbstractGraphics2D and makes it easy to start - * implementing a Graphic2D piece-meal. + * This class is a wrapper for the PDFGraphics2D that + * is used to create a full document around the pdf rendering from + * PDFGraphics2D. * - * @author Vincent Hardy + * @author Keiron Liddle * @version $Id$ - * @see org.apache.batik.ext.awt.g2d.AbstractGraphics2D + * @see org.apache.fop.svg.PDFGraphics2D */ public class PDFDocumentGraphics2D extends PDFGraphics2D { - OutputStream stream; + OutputStream stream; - PDFStream pdfStream; - int width; - int height; + PDFStream pdfStream; + int width; + int height; /** - * Create a new PDFGraphics2D with the given pdf document info. - * This is used to create a Graphics object for use inside an already - * existing document. - * Maybe this could be handled as a subclass (PDFDocumentGraphics2d) + * Create a new PDFDocumentGraphics2D. + * This is used to create a new pdf document of the given height + * and width. + * The resulting document is written to the stream after rendering. + * + * @param textAsShapes set this to true so that text will be rendered + * using curves and not the font. + * @param stream the stream that the final document should be written to. + * @param width the width of the document + * @param height the height of the document */ - public PDFDocumentGraphics2D(boolean textAsShapes, OutputStream stream, int width, int height) - { + public PDFDocumentGraphics2D(boolean textAsShapes, + OutputStream stream, int width, int height) { super(textAsShapes); standalone = true; - this.stream = stream; + this.stream = stream; this.pdfDoc = new PDFDocument(); this.pdfDoc.setProducer("FOP SVG Renderer"); pdfStream = this.pdfDoc.makeStream(); - this.width = width; - this.height = height; + this.width = width; + this.height = height; - currentFontName = ""; - currentFontSize = 0; - currentYPosition = 0; - currentXPosition = 0; -// fontState = fs; + currentFontName = ""; + currentFontSize = 0; + currentYPosition = 0; + currentXPosition = 0; + // fontState = fs; currentStream.write("1 0 0 -1 0 " + height + " cm\n"); // end part /* FontSetup.addToResources(this.pdfDoc, fontInfo); - */ + */ + + } + /** + * Set the dimensions of the svg document that will be drawn. + * This is useful if the dimensions of the svg document are different + * from the pdf document that is to be created. + * The result is scaled so that the svg fits correctly inside the pdf document. + */ + public void setSVGDimension(float w, float h) { + PDFNumber pdfNumber = new PDFNumber(); + currentStream.write("" + pdfNumber.doubleOut(width / w) + " 0 0 " + pdfNumber.doubleOut(height / h) + " 0 0 cm\n"); } - public void finish() throws IOException - { + /** + * Set the background of the pdf document. + * This is used to set the background for the pdf document + * Rather than leaving it as the default white. + */ + public void setBackgroundColor(Color col) { + Color c = col; + currentColour = new PDFColor(c.getRed(), c.getGreen(), c.getBlue()); + currentStream.write("q\n"); + currentStream.write(currentColour.getColorSpaceOut(true)); + + currentStream.write("0 0 " + width + " " + height + " re\n"); + + currentStream.write("f\n"); + currentStream.write("Q\n"); + } + + /** + * The rendering process has finished. + * This should be called after the rendering has completed as there is + * no other indication it is complete. + * This will then write the results to the output stream. + */ + public void finish() throws IOException { pdfStream.add(getString()); PDFResources pdfResources = this.pdfDoc.getResources(); - PDFPage currentPage = this.pdfDoc.makePage(pdfResources, pdfStream, - width, - height, null); + PDFPage currentPage = + this.pdfDoc.makePage(pdfResources, pdfStream, width, + height, null); this.pdfDoc.output(stream); - } - - public String getString() { - return currentStream.toString(); - } + } - public void setGraphicContext(GraphicContext c) - { + public void setGraphicContext(GraphicContext c) { gc = c; } /** * This constructor supports the create method */ - public PDFDocumentGraphics2D(PDFDocumentGraphics2D g){ + public PDFDocumentGraphics2D(PDFDocumentGraphics2D g) { super(g); } @@ -111,7 +133,7 @@ public class PDFDocumentGraphics2D extends PDFGraphics2D { * @return a new graphics context that is a copy of * this graphics context. */ - public Graphics create(){ + public Graphics create() { return new PDFDocumentGraphics2D(this); } diff --git a/src/org/apache/fop/svg/PDFGraphics2D.java b/src/org/apache/fop/svg/PDFGraphics2D.java index 3ac1cd329..544996af5 100644 --- a/src/org/apache/fop/svg/PDFGraphics2D.java +++ b/src/org/apache/fop/svg/PDFGraphics2D.java @@ -35,7 +35,7 @@ import java.util.Map; * is AbstractGraphics2D and makes it easy to start * implementing a Graphic2D piece-meal. * - * @author Vincent Hardy + * @author Keiron Liddle * @version $Id$ * @see org.apache.batik.ext.awt.g2d.AbstractGraphics2D */ diff --git a/src/org/apache/fop/svg/PDFTranscoder.java b/src/org/apache/fop/svg/PDFTranscoder.java index 1079a9fab..8b91723ca 100644 --- a/src/org/apache/fop/svg/PDFTranscoder.java +++ b/src/org/apache/fop/svg/PDFTranscoder.java @@ -67,6 +67,7 @@ import org.apache.batik.transcoder.keys.PaintKey; import org.apache.batik.transcoder.keys.Rectangle2DKey; import org.apache.batik.transcoder.keys.StringKey; import org.apache.batik.transcoder.*; +import org.apache.batik.transcoder.image.*; import org.apache.batik.util.SVGConstants; @@ -96,7 +97,7 @@ import org.w3c.dom.svg.SVGSVGElement; import org.apache.batik.gvt.renderer.StaticRendererFactory; /** - * This class enables to transcode an input to an image of any format. + * This class enables to transcode an input to a pdf document. * *

Two transcoding hints (KEY_WIDTH and * KEY_HEIGHT) can be used to respectively specify the image @@ -119,7 +120,7 @@ import org.apache.batik.gvt.renderer.StaticRendererFactory; * stylesheet, and KEY_PIXEL_TO_MM to specify the pixel to * millimeter conversion factor. * - * @author Thierry Kormann + * @author Keiron Liddle * @version $Id$ */ public class PDFTranscoder extends XMLAbstractTranscoder { @@ -133,8 +134,7 @@ public class PDFTranscoder extends XMLAbstractTranscoder { public PDFTranscoder() { hints.put(KEY_DOCUMENT_ELEMENT_NAMESPACE_URI, SVGConstants.SVG_NAMESPACE_URI); - hints.put(KEY_DOCUMENT_ELEMENT, - SVGConstants.SVG_SVG_TAG); + hints.put(KEY_DOCUMENT_ELEMENT, SVGConstants.SVG_SVG_TAG); hints.put(KEY_DOM_IMPLEMENTATION, SVGDOMImplementation.getDOMImplementation()); } @@ -147,22 +147,21 @@ public class PDFTranscoder extends XMLAbstractTranscoder { * @param output the ouput where to transcode * @exception TranscoderException if an error occured while transcoding */ - protected void transcode(Document document, - String uri, - TranscoderOutput output) - throws TranscoderException { + protected void transcode(Document document, String uri, + TranscoderOutput output) throws TranscoderException { if (!(document instanceof SVGOMDocument)) { throw new TranscoderException( - Messages.formatMessage("notsvg", null)); + Messages.formatMessage("notsvg", null)); } - SVGDocument svgDoc = (SVGDocument)document; + SVGDocument svgDoc = (SVGDocument) document; SVGSVGElement root = svgDoc.getRootElement(); // initialize the SVG document with the appropriate context - String parserClassname = (String)hints.get(KEY_XML_PARSER_CLASSNAME); + String parserClassname = + (String) hints.get(KEY_XML_PARSER_CLASSNAME); DefaultSVGContext svgCtx = new DefaultSVGContext(); svgCtx.setPixelToMM(userAgent.getPixelToMM()); - ((SVGOMDocument)document).setSVGContext(svgCtx); + ((SVGOMDocument) document).setSVGContext(svgCtx); // build the GVT tree GVTBuilder builder = new GVTBuilder(); @@ -176,19 +175,19 @@ public class PDFTranscoder extends XMLAbstractTranscoder { throw new TranscoderException(ex); } // get the 'width' and 'height' attributes of the SVG document - float docWidth = (float)ctx.getDocumentSize().getWidth(); - float docHeight = (float)ctx.getDocumentSize().getHeight(); + float docWidth = (float) ctx.getDocumentSize().getWidth(); + float docHeight = (float) ctx.getDocumentSize().getHeight(); ctx = null; builder = null; // compute the image's width and height according the hints float imgWidth = -1; - if (hints.containsKey(KEY_WIDTH)) { - imgWidth = ((Float)hints.get(KEY_WIDTH)).floatValue(); + if (hints.containsKey(ImageTranscoder.KEY_WIDTH)) { + imgWidth = ((Float) hints.get(ImageTranscoder.KEY_WIDTH)).floatValue(); } float imgHeight = -1; - if (hints.containsKey(KEY_HEIGHT)) { - imgHeight = ((Float)hints.get(KEY_HEIGHT)).floatValue(); + if (hints.containsKey(ImageTranscoder.KEY_HEIGHT)) { + imgHeight = ((Float) hints.get(ImageTranscoder.KEY_HEIGHT)).floatValue(); } float width, height; if (imgWidth > 0 && imgHeight > 0) { @@ -224,12 +223,12 @@ public class PDFTranscoder extends XMLAbstractTranscoder { // we want to keep the document size ratio float d = Math.max(docWidth, docHeight); float dd = Math.max(width, height); - float scale = dd/d; + float scale = dd / d; Px = AffineTransform.getScaleInstance(scale, scale); } // take the AOI into account if any - if (hints.containsKey(KEY_AOI)) { - Rectangle2D aoi = (Rectangle2D)hints.get(KEY_AOI); + if (hints.containsKey(ImageTranscoder.KEY_AOI)) { + Rectangle2D aoi = (Rectangle2D) hints.get(ImageTranscoder.KEY_AOI); // transform the AOI into the image's coordinate system aoi = Px.createTransformedShape(aoi).getBounds2D(); AffineTransform Mx = new AffineTransform(); @@ -244,48 +243,28 @@ public class PDFTranscoder extends XMLAbstractTranscoder { Px.preConcatenate(Mx); } // prepare the image to be painted - int w = (int)width; - int h = (int)height; - - PDFDocumentGraphics2D graphics = new PDFDocumentGraphics2D(true, output.getOutputStream(), w, h); - // GraphicsNodeRenderContext rc = getRenderContext(); - graphics.setGraphicContext(new org.apache.batik.ext.awt.g2d.GraphicContext()); + int w = (int) width; + int h = (int) height; + + PDFDocumentGraphics2D graphics = new PDFDocumentGraphics2D(true, + output.getOutputStream(), w, h); + graphics.setSVGDimension(docWidth, docHeight); + if (hints.containsKey(ImageTranscoder.KEY_BACKGROUND_COLOR)) { + graphics.setBackgroundColor((Color) hints.get(ImageTranscoder.KEY_BACKGROUND_COLOR)); + } + // GraphicsNodeRenderContext rc = getRenderContext(); + graphics.setGraphicContext( + new org.apache.batik.ext.awt.g2d.GraphicContext()); graphics.setRenderingHints(rc.getRenderingHints()); gvtRoot.paint(graphics, rc); try { - graphics.finish(); - } catch (Exception ex) { - ex.printStackTrace(); - throw new TranscoderException(ex); - } - /* - // paint the SVG document using the bridge package - // create the appropriate renderer - ImageRenderer renderer = rendFactory.createImageRenderer(); - renderer.updateOffScreen(w, h); - renderer.setTransform(Px); - renderer.setTree(gvtRoot); - gvtRoot = null; // We're done with it... - - try { - // now we are sure that the aoi is the image size - Shape raoi = new Rectangle2D.Float(0, 0, width, height); - // Warning: the renderer's AOI must be in user space - renderer.repaint(Px.createInverse().createTransformedShape(raoi)); - BufferedImage rend = renderer.getOffScreen(); - renderer = null; // We're done with it... - - // BufferedImage dest = createImage(w, h); - - //Graphics2D g2d = GraphicsUtil.createGraphics(dest); - - //g2d.drawRenderedImage(rend, new AffineTransform()); + graphics.finish(); } catch (Exception ex) { + ex.printStackTrace(); throw new TranscoderException(ex); } - */ } public GraphicsNodeRenderContext getRenderContext() { @@ -293,28 +272,25 @@ public class PDFTranscoder extends XMLAbstractTranscoder { if (nodeRenderContext == null) { RenderingHints hints = new RenderingHints(null); hints.put(RenderingHints.KEY_ANTIALIASING, - RenderingHints.VALUE_ANTIALIAS_ON); + RenderingHints.VALUE_ANTIALIAS_ON); hints.put(RenderingHints.KEY_INTERPOLATION, - RenderingHints.VALUE_INTERPOLATION_BILINEAR); + RenderingHints.VALUE_INTERPOLATION_BILINEAR); FontRenderContext fontRenderContext = - new FontRenderContext(new AffineTransform(), true, true); + new FontRenderContext(new AffineTransform(), true, + true); TextPainter textPainter = new StrokingTextPainter(); GraphicsNodeRableFactory gnrFactory = - new ConcreteGraphicsNodeRableFactory(); - - nodeRenderContext = - new GraphicsNodeRenderContext(new AffineTransform(), - null, - hints, - fontRenderContext, - textPainter, - gnrFactory); - nodeRenderContext.setTextPainter(textPainter); - } + new ConcreteGraphicsNodeRableFactory(); + + nodeRenderContext = new GraphicsNodeRenderContext( + new AffineTransform(), null, hints, + fontRenderContext, textPainter, gnrFactory); + nodeRenderContext.setTextPainter(textPainter); + } return nodeRenderContext; } @@ -327,8 +303,8 @@ public class PDFTranscoder extends XMLAbstractTranscoder { * @param domImpl the DOM Implementation (not used) * @param parserClassname the XML parser classname */ - protected DocumentFactory createDocumentFactory(DOMImplementation domImpl, - String parserClassname) { + protected DocumentFactory createDocumentFactory( + DOMImplementation domImpl, String parserClassname) { return new SAXSVGDocumentFactory(parserClassname); } @@ -375,7 +351,8 @@ public class PDFTranscoder extends XMLAbstractTranscoder { */ public void displayMessage(String message) { try { - getErrorHandler().warning(new TranscoderException(message)); + getErrorHandler().warning( + new TranscoderException(message)); } catch (TranscoderException ex) { throw new RuntimeException(); } @@ -386,8 +363,9 @@ public class PDFTranscoder extends XMLAbstractTranscoder { * TranscodingHints or 0.3528 if any. */ public float getPixelToMM() { - if (getTranscodingHints().containsKey(KEY_PIXEL_TO_MM)) { - return ((Float)getTranscodingHints().get(KEY_PIXEL_TO_MM)).floatValue(); + if (getTranscodingHints().containsKey(ImageTranscoder.KEY_PIXEL_TO_MM)) { + return ( (Float) getTranscodingHints().get( + ImageTranscoder.KEY_PIXEL_TO_MM)).floatValue(); } else { // return 0.3528f; // 72 dpi return 0.26458333333333333333333333333333f; // 96dpi @@ -399,8 +377,8 @@ public class PDFTranscoder extends XMLAbstractTranscoder { * TranscodingHints or "en" (english) if any. */ public String getLanguages() { - if (getTranscodingHints().containsKey(KEY_LANGUAGE)) { - return (String)getTranscodingHints().get(KEY_LANGUAGE); + if (getTranscodingHints().containsKey(ImageTranscoder.KEY_LANGUAGE)) { + return (String) getTranscodingHints().get(ImageTranscoder.KEY_LANGUAGE); } else { return "en"; } @@ -411,14 +389,16 @@ public class PDFTranscoder extends XMLAbstractTranscoder { * TranscodingHints or null if any. */ public String getUserStyleSheetURI() { - return (String)getTranscodingHints().get(KEY_USER_STYLESHEET_URI); + return (String) getTranscodingHints().get( + ImageTranscoder.KEY_USER_STYLESHEET_URI); } /** * Returns the XML parser to use from the TranscodingHints. */ public String getXMLParserClassName() { - return (String)getTranscodingHints().get(KEY_XML_PARSER_CLASSNAME); + return (String) getTranscodingHints().get( + KEY_XML_PARSER_CLASSNAME); } /** @@ -473,9 +453,8 @@ public class PDFTranscoder extends XMLAbstractTranscoder { return false; } - public void registerExtension(BridgeExtension be) - { - } + public void registerExtension(BridgeExtension be) { + } } protected final static Set FEATURES = new HashSet(); @@ -484,174 +463,4 @@ public class PDFTranscoder extends XMLAbstractTranscoder { FEATURES.add(SVGConstants.SVG_ORG_W3C_SVG_LANG_FEATURE); FEATURES.add(SVGConstants.SVG_ORG_W3C_SVG_STATIC_FEATURE); } - - // -------------------------------------------------------------------- - // Keys definition - // -------------------------------------------------------------------- - - /** - * The image width key. - * - * - * - * - * - * - * - * - * - * - * - * - * - * - * - * - *

Key:

KEY_WIDTH

Value:

Float

Default:

The width of the top most svg element

Required:

No

Description:

Specify the width of the image to create.
*/ - public static final TranscodingHints.Key KEY_WIDTH - = new LengthKey(); - - /** - * The image height key. - * - * - * - * - * - * - * - * - * - * - * - * - * - * - * - * - *

Key:

KEY_HEIGHT

Value:

Float

Default:

The height of the top most svg element

Required:

No

Description:

Specify the height of the image to create.
*/ - public static final TranscodingHints.Key KEY_HEIGHT - = new LengthKey(); - - /** - * The area of interest key. - * - * - * - * - * - * - * - * - * - * - * - * - * - * - * - * - *

Key:

KEY_AOI

Value:

Rectangle2D

Default:

The document's size

Required:

No

Description:

Specify the area of interest to render. The - * rectangle coordinates must be specified in pixels and in the - * document coordinates system.
- */ - public static final TranscodingHints.Key KEY_AOI - = new Rectangle2DKey(); - - /** - * The language key. - * - * - * - * - * - * - * - * - * - * - * - * - * - * - * - * - *

Key:

KEY_LANGUAGE

Value:

String

Default:

"en"

Required:

No

Description:

Specify the preferred language of the document. - *
- */ - public static final TranscodingHints.Key KEY_LANGUAGE - = new StringKey(); - - /** - * The user stylesheet URI key. - * - * - * - * - * - * - * - * - * - * - * - * - * - * - * - * - *

Key:

KEY_USER_STYLESHEET_URI

Value:

String

Default:

null

Required:

No

Description:

Specify the user style sheet.
- */ - public static final TranscodingHints.Key KEY_USER_STYLESHEET_URI - = new StringKey(); - - /** - * The pixel to millimeter conversion factor key. - * - * - * - * - * - * - * - * - * - * - * - * - * - * - * - * - *

Key:

KEY_PIXEL_TO_MM

Value:

Float

Default:

0.33

Required:

No

Description:

Specify the pixel to millimeter conversion factor. - *
- */ - public static final TranscodingHints.Key KEY_PIXEL_TO_MM - = new FloatKey(); - - /** - * The image background paint key. - * - * - * - * - * - * - * - * - * - * - * - * - * - * - * - * - *

Key:

KEY_BACKGROUND_COLOR

Value:

Paint

Default:

null

Required:

No

Description:

Specify the background color to use. - * The color is required by opaque image formats and is used by - * image formats that support alpha channel.
- */ - public static final TranscodingHints.Key KEY_BACKGROUND_COLOR - = new PaintKey(); - }