keiron      2002/07/01 07:42:43

  Modified:    src/org/apache/fop/pdf PDFDocument.java
               src/org/apache/fop/render/pdf PDFRenderer.java
                        PDFXMLHandler.java
               src/org/apache/fop/svg PDFDocumentGraphics2D.java
                        PDFGraphics2D.java PDFTranscoder.java
  Log:
  progressively output pdf objects to reduce memory usage
  
  Revision  Changes    Path
  1.44      +11 -41    xml-fop/src/org/apache/fop/pdf/PDFDocument.java
  
  Index: PDFDocument.java
  ===================================================================
  RCS file: /home/cvs/xml-fop/src/org/apache/fop/pdf/PDFDocument.java,v
  retrieving revision 1.43
  retrieving revision 1.44
  diff -u -r1.43 -r1.44
  --- PDFDocument.java  1 Jul 2002 10:39:29 -0000       1.43
  +++ PDFDocument.java  1 Jul 2002 14:42:42 -0000       1.44
  @@ -140,11 +140,6 @@
        */
       protected HashMap fontMap = new HashMap();
   
  -    /**
  -     * the objects themselves
  -     */
  -    protected ArrayList pendingLinks = null;
  -
       protected HashMap filterMap = new HashMap();
   
       /**
  @@ -1015,20 +1010,6 @@
       public void addPage(PDFPage page) {
           /* add it to the list of objects */
           this.objects.add(page);
  -
  -        if(pendingLinks != null) {
  -            for(Iterator iter = pendingLinks.iterator(); iter.hasNext(); ) {
  -                PendingLink pl = (PendingLink)iter.next();
  -                PDFGoTo gt = new PDFGoTo(++this.objectcount,
  -                                         page.referencePDF());
  -                gt.setDestination(pl.dest);
  -                addTrailerObject(gt);
  -                PDFInternalLink internalLink =
  -                                 new PDFInternalLink(gt.referencePDF());
  -                pl.link.setAction(internalLink);
  -            }
  -            pendingLinks = null;
  -        }
       }
   
       /**
  @@ -1096,25 +1077,6 @@
           this.trailerObjects.add(object);
       }
   
  -    class PendingLink {
  -        PDFLink link;
  -        String dest;
  -    }
  -
  -    public PDFLink makeLinkCurrentPage(Rectangle rect, String dest) {
  -        PDFLink link = new PDFLink(++this.objectcount, rect);
  -        this.objects.add(link);
  -        PendingLink pl = new PendingLink();
  -        pl.link = link;
  -        pl.dest = dest;
  -        if(pendingLinks == null) {
  -            pendingLinks = new ArrayList();
  -        }
  -        pendingLinks.add(pl);
  -
  -        return link;
  -    }
  -
       public PDFLink makeLink(Rectangle rect, String page, String dest) {
           PDFLink link = new PDFLink(++this.objectcount, rect);
           this.objects.add(link);
  @@ -1142,7 +1104,7 @@
        *
        * @return the stream object created
        */
  -    public PDFStream makeStream(String type) {
  +    public PDFStream makeStream(String type, boolean add) {
   
           /*
            * create a PDFStream with the next object number and add it
  @@ -1152,10 +1114,18 @@
           PDFStream obj = new PDFStream(++this.objectcount);
           obj.addDefaultFilters(filterMap, type);
   
  -        this.objects.add(obj);
  +        if(add) {
  +            this.objects.add(obj);
  +        }
           return obj;
       }
   
  +    /**
  +     * add a stream object
  +     */
  +    public void addStream(PDFStream obj) {
  +        this.objects.add(obj);
  +    }
   
       /**
        * make an annotation list object
  
  
  
  1.107     +10 -2     xml-fop/src/org/apache/fop/render/pdf/PDFRenderer.java
  
  Index: PDFRenderer.java
  ===================================================================
  RCS file: /home/cvs/xml-fop/src/org/apache/fop/render/pdf/PDFRenderer.java,v
  retrieving revision 1.106
  retrieving revision 1.107
  diff -u -r1.106 -r1.107
  --- PDFRenderer.java  28 Jun 2002 10:09:06 -0000      1.106
  +++ PDFRenderer.java  1 Jul 2002 14:42:42 -0000       1.107
  @@ -217,7 +217,7 @@
                                              (int) Math.round(w / 1000), (int) 
Math.round(h / 1000));
               pageReferences.put(page, currentPage.referencePDF());
           }
  -        currentStream = this.pdfDoc.makeStream(PDFStream.CONTENT_FILTER);
  +        currentStream = this.pdfDoc.makeStream(PDFStream.CONTENT_FILTER, false);
   
           currentState = new PDFState();
           currentState.setTransform(new AffineTransform(1, 0, 0, -1, 0, (int) 
Math.round(pageHeight / 1000)));
  @@ -231,6 +231,7 @@
   
           //currentStream.add("ET\n");
   
  +        this.pdfDoc.addStream(currentStream);
           currentPage.setContents(currentStream);
           this.pdfDoc.addPage(currentPage);
           this.pdfDoc.output(ostream);
  @@ -526,6 +527,12 @@
                                 + xobj + " Do\nQ\nBT\n");
           }
   
  +        // output new data
  +        try {
  +            this.pdfDoc.output(ostream);
  +        } catch(IOException ioe) {
  +
  +        }
       }
   
       public void renderForeignObject(ForeignObject fo) {
  @@ -540,6 +547,7 @@
           context.setUserAgent(userAgent);
   
           context.setProperty(PDFXMLHandler.PDF_DOCUMENT, pdfDoc);
  +        context.setProperty(PDFXMLHandler.OUTPUT_STREAM, ostream);
           context.setProperty(PDFXMLHandler.PDF_STATE, currentState);
           context.setProperty(PDFXMLHandler.PDF_PAGE, currentPage);
           context.setProperty(PDFXMLHandler.PDF_STREAM, currentStream);
  
  
  
  1.6       +7 -3      xml-fop/src/org/apache/fop/render/pdf/PDFXMLHandler.java
  
  Index: PDFXMLHandler.java
  ===================================================================
  RCS file: /home/cvs/xml-fop/src/org/apache/fop/render/pdf/PDFXMLHandler.java,v
  retrieving revision 1.5
  retrieving revision 1.6
  diff -u -r1.5 -r1.6
  --- PDFXMLHandler.java        18 Jun 2002 13:42:56 -0000      1.5
  +++ PDFXMLHandler.java        1 Jul 2002 14:42:43 -0000       1.6
  @@ -22,8 +22,8 @@
   import org.w3c.dom.NamedNodeMap;
   import org.w3c.dom.Attr;
   
  -import java.io.Writer;
   import java.io.IOException;
  +import java.io.OutputStream;
   
   import org.apache.batik.bridge.*;
   import org.apache.batik.swing.svg.*;
  @@ -44,6 +44,7 @@
    */
   public class PDFXMLHandler implements XMLHandler {
   public static final String PDF_DOCUMENT = "pdfDoc";
  +public static final String OUTPUT_STREAM = "outputStream";
   public static final String PDF_STATE = "pdfState";
   public static final String PDF_PAGE = "pdfPage";
   public static final String PDF_STREAM = "pdfStream";
  @@ -73,6 +74,7 @@
       public static PDFInfo getPDFInfo(RendererContext context) {
           PDFInfo pdfi = new PDFInfo();
           pdfi.pdfDoc = (PDFDocument)context.getProperty(PDF_DOCUMENT);
  +        pdfi.outputStream = (OutputStream)context.getProperty(OUTPUT_STREAM);
           pdfi.pdfState = (PDFState)context.getProperty(PDF_STATE);
           pdfi.pdfPage = (PDFPage)context.getProperty(PDF_PAGE);
           pdfi.currentStream = (PDFStream)context.getProperty(PDF_STREAM);
  @@ -88,6 +90,7 @@
   
       public static class PDFInfo {
           PDFDocument pdfDoc;
  +        OutputStream outputStream;
           PDFState pdfState;
           PDFPage pdfPage;
           public PDFStream currentStream;
  @@ -164,7 +167,7 @@
               }
   
               PDFGraphics2D graphics = new PDFGraphics2D(true, pdfInfo.fs, 
pdfInfo.pdfDoc,
  -                                     pdfInfo.pdfPage, pdfInfo.currentFontName,
  +                                     pdfInfo.pdfPage, 
pdfInfo.pdfPage.referencePDF(), pdfInfo.currentFontName,
                                        pdfInfo.currentFontSize,
                                        pdfInfo.currentXPosition,
                                        pdfInfo.currentYPosition);
  @@ -175,6 +178,7 @@
               transform.translate(xOffset / 1000f, yOffset / 1000f);
               pdfInfo.pdfState.setTransform(transform);
               graphics.setPDFState(pdfInfo.pdfState);
  +            graphics.setOutputStream(pdfInfo.outputStream);
               try {
                   root.paint(graphics);
                   pdfInfo.currentStream.add(graphics.getString());
  
  
  
  1.18      +17 -12    xml-fop/src/org/apache/fop/svg/PDFDocumentGraphics2D.java
  
  Index: PDFDocumentGraphics2D.java
  ===================================================================
  RCS file: /home/cvs/xml-fop/src/org/apache/fop/svg/PDFDocumentGraphics2D.java,v
  retrieving revision 1.17
  retrieving revision 1.18
  diff -u -r1.17 -r1.18
  --- PDFDocumentGraphics2D.java        28 Jun 2002 10:09:07 -0000      1.17
  +++ PDFDocumentGraphics2D.java        1 Jul 2002 14:42:43 -0000       1.18
  @@ -25,6 +25,9 @@
   
   import org.apache.batik.ext.awt.g2d.GraphicContext;
   
  +import org.apache.fop.image.JpegImage;
  +import java.awt.image.ImageObserver;
  +
   /**
    * This class is a wrapper for the <tt>PDFGraphics2D</tt> that
    * is used to create a full document around the pdf rendering from
  @@ -35,8 +38,6 @@
    * @see org.apache.fop.svg.PDFGraphics2D
    */
   public class PDFDocumentGraphics2D extends PDFGraphics2D {
  -    OutputStream stream;
  -
       PDFPage currentPage;
       PDFStream pdfStream;
       int width;
  @@ -70,7 +71,6 @@
           standalone = true;
           this.pdfDoc = new PDFDocument();
           this.pdfDoc.setProducer("FOP SVG Renderer");
  -        pdfStream = this.pdfDoc.makeStream(PDFStream.CONTENT_FILTER);
   
           graphicsState = new PDFState();
   
  @@ -78,18 +78,24 @@
           currentFontSize = 0;
           currentYPosition = 0;
           currentXPosition = 0;
  +
  +        pdfStream = this.pdfDoc.makeStream(PDFStream.CONTENT_FILTER, false);
       }
   
  -    void setupDocument(OutputStream stream, int width, int height) {
  +    void setupDocument(OutputStream stream, int width, int height) throws 
IOException {
           this.width = width;
           this.height = height;
  -        this.stream = stream;
   
           PDFResources pdfResources = this.pdfDoc.getResources();
           currentPage = this.pdfDoc.makePage(pdfResources,
                                                      width, height);
           resourceContext = currentPage;
  +        pageRef = currentPage.referencePDF();
           currentStream.write("1 0 0 -1 0 " + height + " cm\n");
  +
  +        pdfDoc.outputHeader(stream);
  +
  +        setOutputStream(stream);
       }
   
       /**
  @@ -105,7 +111,7 @@
        * @param height the height of the document
        */
       public PDFDocumentGraphics2D(boolean textAsShapes, OutputStream stream,
  -                                 int width, int height) {
  +                                 int width, int height) throws IOException {
           this(textAsShapes);
           setupDocument(stream, width, height);
       }
  @@ -154,15 +160,14 @@
        */
       public void finish() throws IOException {
           pdfStream.add(getString());
  -        PDFResources pdfResources = this.pdfDoc.getResources();
  +        this.pdfDoc.addStream(pdfStream);
           currentPage.setContents(pdfStream);
           this.pdfDoc.addPage(currentPage);
           if (fontInfo != null) {
               FontSetup.addToResources(pdfDoc, pdfDoc.getResources(), fontInfo);
           }
  -        pdfDoc.outputHeader(stream);
  -        this.pdfDoc.output(stream);
  -        pdfDoc.outputTrailer(stream);
  +        this.pdfDoc.output(outputStream);
  +        pdfDoc.outputTrailer(outputStream);
       }
   
       /**
  
  
  
  1.34      +48 -6     xml-fop/src/org/apache/fop/svg/PDFGraphics2D.java
  
  Index: PDFGraphics2D.java
  ===================================================================
  RCS file: /home/cvs/xml-fop/src/org/apache/fop/svg/PDFGraphics2D.java,v
  retrieving revision 1.33
  retrieving revision 1.34
  diff -u -r1.33 -r1.34
  --- PDFGraphics2D.java        1 Jul 2002 10:33:30 -0000       1.33
  +++ PDFGraphics2D.java        1 Jul 2002 14:42:43 -0000       1.34
  @@ -59,6 +59,7 @@
        */
       protected PDFDocument pdfDoc;
       protected PDFResourceContext resourceContext;
  +    protected String pageRef;
   
       /**
        * the current state of the pdf graphics
  @@ -94,6 +95,14 @@
       protected int currentXPosition = 0;
   
       /**
  +     * The output stream for the pdf document.
  +     * If this is set then it can progressively output
  +     * the pdf document objects to reduce memory.
  +     * Especially with images.
  +     */
  +    protected OutputStream outputStream = null;
  +
  +    /**
        * A registry of images that have already been drawn. They are mapped to 
        * a structure with the PDF xObjectNum, width and height. This
        * prevents multiple copies from being stored, which can greatly
  @@ -113,7 +122,7 @@
        * existing document.
        */
       public PDFGraphics2D(boolean textAsShapes, FontState fs, PDFDocument doc,
  -                         PDFResourceContext page, String font, float size, int 
xpos, int ypos) {
  +                         PDFResourceContext page, String pref, String font, float 
size, int xpos, int ypos) {
           super(textAsShapes);
           pdfDoc = doc;
           resourceContext = page;
  @@ -122,6 +131,7 @@
           currentYPosition = ypos;
           currentXPosition = xpos;
           fontState = fs;
  +        pageRef = pref;
           graphicsState = new PDFState();
       }
   
  @@ -133,6 +143,10 @@
           graphicsState = state;
       }
   
  +    public void setOutputStream(OutputStream os) {
  +        outputStream = os;
  +    }
  +
       public String getString() {
           return currentStream.toString();
       }
  @@ -175,8 +189,7 @@
   
           if(linkType != PDFLink.EXTERNAL) {
               String pdfdest = "/FitR " + dest;
  -            // TODO use page ref instead
  -            resourceContext.addAnnotation(pdfDoc.makeLinkCurrentPage(rect, 
pdfdest));
  +            resourceContext.addAnnotation(pdfDoc.makeLink(rect, pageRef, pdfdest));
           } else {
               resourceContext.addAnnotation(pdfDoc.makeLink(rect,
                                                    dest, linkType));
  @@ -202,6 +215,13 @@
                             + x + " "
                             + (y + height) + " cm\n" + "/Im"
                             + xObjectNum + " Do\nQ\n");
  +
  +        if(outputStream != null) {
  +            try {
  +                this.pdfDoc.output(outputStream);
  +            } catch(IOException ioe) {
  +            }
  +        }
       }
   
       /**
  @@ -319,6 +339,13 @@
                   fopimg.setColorSpace(new PDFColorSpace(PDFColorSpace.DEVICE_GRAY));
                   PDFXObject xobj = pdfDoc.addImage(resourceContext, fopimg);
                   ref = xobj.referencePDF();
  +
  +                if(outputStream != null) {
  +                    try {
  +                        this.pdfDoc.output(outputStream);
  +                    } catch(IOException ioe) {
  +                    }
  +                }
               } else {
                   mask = null;
               }
  @@ -327,6 +354,13 @@
               fopimg.setTransparent(new PDFColor(255, 255, 255));
               imageInfo.xObjectNum = pdfDoc.addImage(resourceContext, 
fopimg).getXNumber();
               imageInfos.put(img, imageInfo);
  +
  +            if(outputStream != null) {
  +                try {
  +                    this.pdfDoc.output(outputStream);
  +                } catch(IOException ioe) {
  +                }
  +            }
           }
   
           // now do any transformation required and add the actual image
  @@ -712,11 +746,12 @@
               PDFResources res = pdfDoc.makeResources();
               PDFResourceContext context = new PDFResourceContext(0, pdfDoc, res);
               PDFGraphics2D pattGraphic = new PDFGraphics2D(textAsShapes, fs,
  -                                            pdfDoc, context,
  +                                            pdfDoc, context, pageRef,
                                               currentFontName, currentFontSize,
                                               currentYPosition, currentXPosition);
               pattGraphic.gc = (GraphicContext)this.gc.clone();
               pattGraphic.gc.validateTransformStack();
  +            pattGraphic.setOutputStream(outputStream);
   
               GraphicsNode gn = pp.getGraphicsNode();
               gn.paint(pattGraphic);
  @@ -753,6 +788,13 @@
                                       translate, null, pattStream.getBuffer());
   
               currentStream.write(myPat.getColorSpaceOut(fill));
  +
  +            if(outputStream != null) {
  +                try {
  +                    this.pdfDoc.output(outputStream);
  +                } catch(IOException ioe) {
  +                } 
  +            }
   
           }
       }
  
  
  
  1.19      +10 -5     xml-fop/src/org/apache/fop/svg/PDFTranscoder.java
  
  Index: PDFTranscoder.java
  ===================================================================
  RCS file: /home/cvs/xml-fop/src/org/apache/fop/svg/PDFTranscoder.java,v
  retrieving revision 1.18
  retrieving revision 1.19
  diff -u -r1.18 -r1.19
  --- PDFTranscoder.java        18 Jun 2002 13:42:56 -0000      1.18
  +++ PDFTranscoder.java        1 Jul 2002 14:42:43 -0000       1.19
  @@ -32,6 +32,8 @@
   import java.util.HashSet;
   import java.util.Set;
   
  +import java.io.IOException;
  +
   import org.apache.batik.transcoder.*;
   
   import org.apache.batik.bridge.BridgeContext;
  @@ -266,7 +268,11 @@
           int w = (int)width;
           int h = (int)height;
   
  -        graphics.setupDocument(output.getOutputStream(), w, h);
  +        try {
  +            graphics.setupDocument(output.getOutputStream(), w, h);
  +        } catch (IOException ex) {
  +            throw new TranscoderException(ex);
  +        }
           graphics.setSVGDimension(docWidth, docHeight);
           currentTransform.setTransform(1, 0, 0, -1, 0, height);
           /*if (!stroke) {
  @@ -283,8 +289,7 @@
   
           try {
               graphics.finish();
  -        } catch (Exception ex) {
  -            ex.printStackTrace();
  +        } catch (IOException ex) {
               throw new TranscoderException(ex);
           }
       }
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to