123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173 |
- /*
- * $Id$
- * Copyright (C) 2001 The Apache Software Foundation. All rights reserved.
- * For details on use and redistribution please refer to the
- * LICENSE file included with these sources.
- */
-
- package org.apache.fop.render.pdf;
-
- // FOP
- import org.apache.fop.render.PrintRenderer;
- import org.apache.fop.image.ImageArea;
- import org.apache.fop.image.FopImage;
- import org.apache.fop.apps.FOPException;
- import org.apache.fop.fo.properties.*;
- import org.apache.fop.datatypes.*;
- import org.apache.fop.pdf.*;
- import org.apache.fop.image.*;
- import org.apache.fop.extensions.*;
- import org.apache.fop.datatypes.IDReferences;
- import org.apache.fop.render.pdf.fonts.LazyFont;
-
- import org.apache.fop.area.*;
- import org.apache.fop.area.inline.*;
-
- import org.w3c.dom.Document;
-
- // Java
- import java.io.IOException;
- import java.io.OutputStream;
-
- import java.awt.geom.Rectangle2D;
-
- /*
- TODO:
-
- viewport clipping
- word rendering and optimistion
- pdf state optimistation
- line and border
- leader
- background pattern
- user agent xml (svg) rendering
- orientation
- writing mode
- text decoration
-
- */
-
- /**
- * Renderer that renders areas to PDF
- *
- */
- public class PDFRenderer extends PrintRenderer {
-
- /**
- * the PDF Document being created
- */
- protected PDFDocument pdfDoc;
-
- protected String producer;
-
- protected OutputStream ostream;
-
- /**
- * the /Resources object of the PDF document being created
- */
- protected PDFResources pdfResources;
-
- /**
- * the current stream to add PDF commands to
- */
- PDFStream currentStream;
-
- /**
- * the current annotation list to add annotations to
- */
- PDFAnnotList currentAnnotList;
-
- /**
- * the current page to add annotations to
- */
- PDFPage currentPage;
-
- PDFColor currentColor;
-
- /**
- * true if a TJ command is left to be written
- */
- boolean textOpen = false;
-
- /**
- * the previous Y coordinate of the last word written.
- * Used to decide if we can draw the next word on the same line.
- */
- int prevWordY = 0;
-
- /**
- * the previous X coordinate of the last word written.
- * used to calculate how much space between two words
- */
- int prevWordX = 0;
-
- /**
- * The width of the previous word. Used to calculate space between
- */
- int prevWordWidth = 0;
-
- /**
- * reusable word area string buffer to reduce memory usage
- */
- private StringBuffer _wordAreaPDF = new StringBuffer();
-
- /**
- * create the PDF renderer
- */
- public PDFRenderer() {
- }
-
- /**
- * set the PDF document's producer
- *
- * @param producer string indicating application producing PDF
- */
- public void setProducer(String prod) {
- producer = prod;
- }
-
- public void startRenderer(OutputStream stream) throws IOException {
- ostream = stream;
- this.pdfDoc = new PDFDocument();
- this.pdfDoc.setProducer(producer);
- pdfDoc.outputHeader(stream);
- }
-
- public void stopRenderer() throws IOException {
- FontSetup.addToResources(this.pdfDoc, fontInfo);
- pdfDoc.outputTrailer(ostream);
-
- this.pdfDoc = null;
- ostream = null;
- }
-
- /**
- * This method creates a pdf stream for the current page
- * uses it as the contents of a new page. The page is wriiten
- * immediately to the output stream.
- */
- public void renderPage(PageViewport page) throws IOException,
- FOPException {
-
- this.pdfResources = this.pdfDoc.getResources();
-
- currentStream = this.pdfDoc.makeStream();
- currentStream.add("BT\n");
-
- Page p = page.getPage();
- renderPageAreas(p);
-
- currentStream.add("ET\n");
-
- Rectangle2D bounds = page.getViewArea();
- double w = bounds.getWidth();
- double h = bounds.getHeight();
- currentPage = this.pdfDoc.makePage(this.pdfResources, currentStream,
- (int) Math.round(w / 1000), (int) Math.round(h / 1000));
-
- this.pdfDoc.output(ostream);
-
- }
-
-
- }
|