Browse Source

Fix to make the Graphics.create() method (calls a copy constructor) work.

Extracted stuff into a PDFContext class which gets passed to each copy of PDFDocumentGraphics2D, so the state is uniform over all copies.

git-svn-id: https://svn.apache.org/repos/asf/xmlgraphics/fop/trunk@326600 13f79535-47bb-0310-9956-ffa450edef68
tags/fop-0_90-alpha1
Jeremias Maerki 18 years ago
parent
commit
4d2cb06d14

+ 80
- 0
src/java/org/apache/fop/svg/PDFContext.java View File

/*
* Copyright 2005 The Apache Software Foundation.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

/* $Id$ */

package org.apache.fop.svg;

import java.util.List;

import org.apache.fop.pdf.PDFPage;

/**
* Context class which holds state information which should remain in sync over multiple instances
* of PDFDocumentGraphics2D.
*/
public class PDFContext {

private PDFPage currentPage;
private List fontList;

/** number of pages generated */
private int pagecount;
/**
* Sets the font list as creates by the FontSetup class.
* @param list the font list
*/
public void setFontList(List list) {
this.fontList = list;
}

/** @return the font list */
public List getFontList() {
return this.fontList;
}

/** @return true if a page is set up for painting. */
public boolean isPagePending() {
return this.currentPage != null;
}

/**
* After this call, there's no current page.
*/
public void clearCurrentPage() {
currentPage = null;
}

/** @return the current page or null if there is none */
public PDFPage getCurrentPage() {
return this.currentPage;
}

/**
* Sets the current page
* @param page the page
*/
public void setCurrentPage(PDFPage page) {
this.currentPage = page;
}

/** Notifies the context to increase the page count. */
public void increasePageCount() {
this.pagecount++;
}

}

+ 39
- 51
src/java/org/apache/fop/svg/PDFDocumentGraphics2D.java View File

/* /*
* Copyright 1999-2004 The Apache Software Foundation.
* Copyright 1999-2005 The Apache Software Foundation.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
import org.apache.avalon.framework.configuration.Configurable; import org.apache.avalon.framework.configuration.Configurable;
import org.apache.avalon.framework.configuration.Configuration; import org.apache.avalon.framework.configuration.Configuration;
import org.apache.avalon.framework.configuration.ConfigurationException; import org.apache.avalon.framework.configuration.ConfigurationException;
import org.apache.commons.logging.impl.SimpleLog;
import org.apache.commons.logging.Log;


import java.awt.Graphics; import java.awt.Graphics;
import java.awt.Font; import java.awt.Font;
import java.io.OutputStream; import java.io.OutputStream;
import java.io.IOException; import java.io.IOException;
import java.io.StringWriter; import java.io.StringWriter;
import java.util.List;


/** /**
* This class is a wrapper for the <tt>PDFGraphics2D</tt> that * This class is a wrapper for the <tt>PDFGraphics2D</tt> that
* <tt>PDFGraphics2D</tt>. * <tt>PDFGraphics2D</tt>.
* *
* @author <a href="mailto:keiron@aftexsw.com">Keiron Liddle</a> * @author <a href="mailto:keiron@aftexsw.com">Keiron Liddle</a>
* @version $Id: PDFDocumentGraphics2D.java,v 1.27 2003/03/07 09:51:26 jeremias Exp $
* @version $Id$
* @see org.apache.fop.svg.PDFGraphics2D * @see org.apache.fop.svg.PDFGraphics2D
*/ */
public class PDFDocumentGraphics2D extends PDFGraphics2D public class PDFDocumentGraphics2D extends PDFGraphics2D
implements Configurable, Initializable { implements Configurable, Initializable {


private PDFPage currentPage;
private PDFContext pdfContext;


private int width; private int width;
private int height; private int height;
private float svgWidth; private float svgWidth;
private float svgHeight; private float svgHeight;


private List fontList;

/** number of pages generated */
protected int pagecount;
/** indicates whether a page is currently being generated */
protected boolean pagePending;
/** Initial clipping area, used to restore to original setting when a new page is started. */ /** Initial clipping area, used to restore to original setting when a new page is started. */
protected Shape initialClip; protected Shape initialClip;
/** /**
*/ */
protected AffineTransform initialTransform; protected AffineTransform initialTransform;


private Log logger;

//Avalon component //Avalon component
private Configuration cfg; private Configuration cfg;


public PDFDocumentGraphics2D(boolean textAsShapes) { public PDFDocumentGraphics2D(boolean textAsShapes) {
super(textAsShapes); super(textAsShapes);


this.pdfContext = new PDFContext();
if (!textAsShapes) { if (!textAsShapes) {
fontInfo = new FontInfo(); fontInfo = new FontInfo();
FontSetup.setup(fontInfo, null); FontSetup.setup(fontInfo, null);
* This constructor is Avalon-style. * This constructor is Avalon-style.
*/ */
public PDFDocumentGraphics2D() { public PDFDocumentGraphics2D() {
super(false);
}

public void setLogger(Log logger) {
this.logger = logger;
}

/**
* Returns the logger.
* @return Logger the logger
*/
protected final Log getLogger() {
if (this.logger == null) {
this.logger = new SimpleLog("FOP/PDF");
((SimpleLog) logger).setLevel(SimpleLog.LOG_LEVEL_INFO);
}
return this.logger;
this(false);
} }


/** /**
*/ */
public void configure(Configuration cfg) throws ConfigurationException { public void configure(Configuration cfg) throws ConfigurationException {
this.cfg = cfg; this.cfg = cfg;
this.fontList = FontSetup.buildFontListFromConfiguration(cfg);
this.pdfContext.setFontList(FontSetup.buildFontListFromConfiguration(cfg));
} }


/** /**
public void initialize() throws Exception { public void initialize() throws Exception {
if (this.fontInfo == null) { if (this.fontInfo == null) {
fontInfo = new FontInfo(); fontInfo = new FontInfo();
FontSetup.setup(fontInfo, this.fontList);
FontSetup.setup(fontInfo, this.pdfContext.getFontList());
//FontState fontState = new FontState("Helvetica", "normal", //FontState fontState = new FontState("Helvetica", "normal",
// FontInfo.NORMAL, 12, 0); // FontInfo.NORMAL, 12, 0);
} }
currentStream.write("Q\n"); currentStream.write("Q\n");
} }


/**
* Is called to prepare the PDFDocumentGraphics2D for the next page to be painted. Basically,
* this closes the current page. A new page is prepared as soon as painting starts.
*/
public void nextPage() { public void nextPage() {
closePage(); closePage();
} }
/**
* Closes the current page and adds it to the PDF file.
*/
protected void closePage() { protected void closePage() {
if (!this.pagePending) {
if (!pdfContext.isPagePending()) {
return; //ignore return; //ignore
} }
//Finish page //Finish page
PDFStream pdfStream = this.pdfDoc.getFactory().makeStream(PDFFilterList.CONTENT_FILTER, false);
PDFStream pdfStream = this.pdfDoc.getFactory().makeStream(
PDFFilterList.CONTENT_FILTER, false);
pdfStream.add(getString()); pdfStream.add(getString());
currentStream = null; currentStream = null;
this.pdfDoc.registerObject(pdfStream); this.pdfDoc.registerObject(pdfStream);
currentPage.setContents(pdfStream);
PDFAnnotList annots = currentPage.getAnnotations();
pdfContext.getCurrentPage().setContents(pdfStream);
PDFAnnotList annots = pdfContext.getCurrentPage().getAnnotations();
if (annots != null) { if (annots != null) {
this.pdfDoc.addObject(annots); this.pdfDoc.addObject(annots);
} }
this.pdfDoc.addObject(currentPage);
this.currentPage = null;

this.pagePending = false;
this.pdfDoc.addObject(pdfContext.getCurrentPage());
pdfContext.clearCurrentPage();
} }
/** {@inheritDoc} */ /** {@inheritDoc} */
protected void preparePainting() { protected void preparePainting() {
if (this.pagePending) {
if (pdfContext.isPagePending()) {
return; return;
} }
try { try {
} }
} }


/**
* Called to prepare a new page
* @throws IOException if starting the new page fails due to I/O errors.
*/
protected void startPage() throws IOException { protected void startPage() throws IOException {
if (this.pagePending) {
if (pdfContext.isPagePending()) {
throw new IllegalStateException("Close page first before starting another"); throw new IllegalStateException("Close page first before starting another");
} }
//Start page //Start page
} }
PDFResources pdfResources = this.pdfDoc.getResources(); PDFResources pdfResources = this.pdfDoc.getResources();
currentPage = this.pdfDoc.getFactory().makePage(pdfResources,
width, height);
resourceContext = currentPage;
pageRef = currentPage.referencePDF();
PDFPage page = this.pdfDoc.getFactory().makePage(pdfResources,
width, height);
resourceContext = page;
pdfContext.setCurrentPage(page);
pageRef = page.referencePDF();
graphicsState.setTransform(new AffineTransform(1.0, 0.0, 0.0, -1.0, 0.0, (double)height)); graphicsState.setTransform(new AffineTransform(1.0, 0.0, 0.0, -1.0, 0.0, (double)height));
currentStream.write("1 0 0 -1 0 " + height + " cm\n"); currentStream.write("1 0 0 -1 0 " + height + " cm\n");
if (svgWidth != 0) { if (svgWidth != 0) {
+ PDFNumber.doubleOut(height / svgHeight) + " 0 0 cm\n"); + PDFNumber.doubleOut(height / svgHeight) + " 0 0 cm\n");
} }


this.pagecount++;
this.pagePending = true;
pdfContext.increasePageCount();
} }
*/ */
public PDFDocumentGraphics2D(PDFDocumentGraphics2D g) { public PDFDocumentGraphics2D(PDFDocumentGraphics2D g) {
super(g); super(g);
this.pdfContext = g.pdfContext;
this.cfg = g.cfg;
this.width = g.width;
this.height = g.height;
this.svgWidth = g.svgWidth;
this.svgHeight = g.svgHeight;
} }


/** /**

+ 11
- 0
src/java/org/apache/fop/svg/PDFGraphics2D.java View File

*/ */
public PDFGraphics2D(PDFGraphics2D g) { public PDFGraphics2D(PDFGraphics2D g) {
super(g); super(g);
this.pdfDoc = g.pdfDoc;
this.resourceContext = g.resourceContext;
this.currentFontName = g.currentFontName;
this.currentFontSize = g.currentFontSize;
this.fontInfo = g.fontInfo;
this.pageRef = g.pageRef;
this.graphicsState = g.graphicsState;
this.currentStream = g.currentStream;
this.jpegCount = g.jpegCount;
this.outputStream = g.outputStream;
this.ovFontState = g.ovFontState;
} }


/** /**

+ 2
- 3
src/java/org/apache/fop/svg/PDFTranscoder.java View File

/* /*
* Copyright 1999-2004 The Apache Software Foundation.
* Copyright 1999-2005 The Apache Software Foundation.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
* millimeter conversion factor. * millimeter conversion factor.
* *
* @author <a href="mailto:keiron@aftexsw.com">Keiron Liddle</a> * @author <a href="mailto:keiron@aftexsw.com">Keiron Liddle</a>
* @version $Id: PDFTranscoder.java,v 1.24 2003/03/07 09:51:26 jeremias Exp $
* @version $Id$
*/ */
public class PDFTranscoder extends AbstractFOPTranscoder public class PDFTranscoder extends AbstractFOPTranscoder
implements Configurable { implements Configurable {
throws TranscoderException { throws TranscoderException {


graphics = new PDFDocumentGraphics2D(); graphics = new PDFDocumentGraphics2D();
graphics.setLogger(getLogger());
try { try {
if (this.cfg != null) { if (this.cfg != null) {

Loading…
Cancel
Save