Browse Source

Consolidated formatPageSequence() into FOTreeHandler.


git-svn-id: https://svn.apache.org/repos/asf/xmlgraphics/fop/trunk@197699 13f79535-47bb-0310-9956-ffa450edef68
tags/Root_Temp_KnuthStylePageBreaking
Glen Mazza 20 years ago
parent
commit
573952b433

+ 1
- 119
src/java/org/apache/fop/apps/Document.java View File

@@ -20,7 +20,6 @@ package org.apache.fop.apps;

// Java
import java.util.Map;
import java.io.IOException;
import java.util.Set;
import java.util.HashSet;

@@ -28,20 +27,12 @@ import java.util.HashSet;
import org.apache.fop.area.AreaTree;
import org.apache.fop.area.AreaTreeControl;
import org.apache.fop.area.AreaTreeModel;
import org.apache.fop.area.Title;

import org.apache.fop.fo.FOInputHandler;
import org.apache.fop.fo.FOTreeControl;
import org.apache.fop.fo.FOTreeEvent;
import org.apache.fop.fo.FOTreeListener;
import org.apache.fop.fo.extensions.Bookmarks;
import org.apache.fop.fo.pagination.PageSequence;
import org.apache.fop.fonts.FontInfo;
import org.apache.fop.layoutmgr.AddLMVisitor;
import org.apache.fop.layoutmgr.ContentLayoutManager;
import org.apache.fop.layoutmgr.InlineStackingLayoutManager;
import org.apache.fop.layoutmgr.LMiter;
import org.apache.fop.layoutmgr.PageLayoutManager;


import org.apache.commons.logging.Log;
@@ -53,8 +44,7 @@ import org.xml.sax.SAXException;
* Class storing information for the FOP Document being processed, and managing
* the processing of it.
*/
public class Document implements FOTreeControl, FOTreeListener,
AreaTreeControl {
public class Document implements FOTreeControl, AreaTreeControl {
/** The parent Driver object */
private Driver driver;
@@ -111,34 +101,6 @@ public class Document implements FOTreeControl, FOTreeListener,
return driver;
}

/**
* Required by the FOTreeListener interface. It handles an
* FOTreeEvent that is fired when a PageSequence object has been completed.
* @param event the FOTreeEvent that was fired
* @throws FOPException for errors in building the PageSequence
*/
public void foPageSequenceComplete (FOTreeEvent event) throws FOPException {
PageSequence pageSeq = event.getPageSequence();
areaTree.addBookmarksToAreaTree();
formatPageSequence(pageSeq, areaTree);
}

/**
* Required by the FOTreeListener interface. It handles an FOTreeEvent that
* is fired when the Document has been completely parsed.
* @param event the FOTreeEvent that was fired
* @throws SAXException for parsing errors
*/
public void foDocumentComplete (FOTreeEvent event) throws SAXException {
//processAreaTree(atModel);
try {
areaTree.endDocument();
driver.getRenderer().stopRenderer();
} catch (IOException ex) {
throw new SAXException(ex);
}
}

/**
* Get the area tree for this layout handler.
*
@@ -180,86 +142,6 @@ public class Document implements FOTreeControl, FOTreeListener,
return foInputHandler;
}

/**
* Runs the formatting of this page sequence into the given area tree
*
* @param pageSeq the PageSequence to be formatted
* @param areaTree the area tree to format this page sequence into
* @throws FOPException if there is an error formatting the contents
*/
private void formatPageSequence(PageSequence pageSeq, AreaTree areaTree)
throws FOPException {
Title title = null;
if (pageSeq.getTitleFO() != null) {
title = getTitleArea(pageSeq.getTitleFO());
}
areaTree.startPageSequence(title);
// Make a new PageLayoutManager and a FlowLayoutManager
// Run the PLM in a thread
// Wait for them to finish.

// If no main flow, nothing to layout!
if (pageSeq.getMainFlow() == null) {
return;
}

// Initialize if already used?
// this.layoutMasterSet.resetPageMasters();
if (pageSeq.getPageSequenceMaster() != null) {
pageSeq.getPageSequenceMaster().reset();
}

pageSeq.initPageNumber();

// This will layout pages and add them to the area tree
PageLayoutManager pageLM = new PageLayoutManager(areaTree, pageSeq, this);
pageLM.setPageCounting(pageSeq.getCurrentPageNumber(),
pageSeq.getPageNumberGenerator());

// For now, skip the threading and just call run directly.
pageLM.run();

// Thread layoutThread = new Thread(pageLM);
// layoutThread.start();
// log.debug("Layout thread started");
// // wait on both managers
// try {
// layoutThread.join();
// log.debug("Layout thread done");
// } catch (InterruptedException ie) {
// log.error("PageSequence.format() interrupted waiting on layout");
// }
pageSeq.setCurrentPageNumber(pageLM.getPageCount());
// Tell the root the last page number we created.
pageSeq.getRoot().setRunningPageNumberCounter(pageSeq.getCurrentPageNumber());
}

/**
* @return the Title area
*/
public org.apache.fop.area.Title getTitleArea(org.apache.fop.fo.pagination.Title foTitle) {
// use special layout manager to add the inline areas
// to the Title.
InlineStackingLayoutManager lm;
lm = new InlineStackingLayoutManager(foTitle);
lm.setLMiter(new LMiter(lm, foTitle.children.listIterator()));
lm.initialize();

// get breaks then add areas to title
org.apache.fop.area.Title title =
new org.apache.fop.area.Title();

ContentLayoutManager clm = new ContentLayoutManager(title);
clm.setUserAgent(foTitle.getUserAgent());
lm.setParent(clm);

clm.fillArea(lm);

return title;
}

/**
* Public accessor to set the AddLMVisitor object that should be used.
* This allows subclasses of AddLMVisitor to be used, which can be useful

+ 8
- 2
src/java/org/apache/fop/apps/Driver.java View File

@@ -518,7 +518,6 @@ public class Driver {
public void startDocument() throws SAXException {
if (foInputHandler instanceof FOTreeHandler) {
FOTreeHandler foTreeHandler = (FOTreeHandler)foInputHandler;
foTreeHandler.addFOTreeListener(currentDocument);
}
super.startDocument();
}
@@ -527,7 +526,6 @@ public class Driver {
super.endDocument();
if (foInputHandler instanceof FOTreeHandler) {
FOTreeHandler foTreeHandler = (FOTreeHandler)foInputHandler;
foTreeHandler.removeFOTreeListener(currentDocument);
}
}
@@ -638,4 +636,12 @@ public class Driver {
}
}

/**
* Public accessor for getting the document used by this generation
* @return the current Document object
*/
public Document getCurrentDocument() {
return currentDocument;
}
}

+ 28
- 9
src/java/org/apache/fop/fo/FOInputHandler.java View File

@@ -19,7 +19,10 @@
package org.apache.fop.fo;

// FOP
import org.apache.fop.apps.Document;
import org.apache.fop.apps.Driver;
import org.apache.fop.apps.FOPException;
import org.apache.fop.area.AreaTree;
import org.apache.fop.fo.flow.BasicLink;
import org.apache.fop.fo.flow.Block;
import org.apache.fop.fo.flow.ExternalGraphic;
@@ -54,10 +57,11 @@ import org.xml.sax.SAXException;
* as our MIF and RTF output targets.
*/
public abstract class FOInputHandler {
/**
* The FOTreeControl object that is controlling the FO Tree being built
* The Document object that is controlling the FO Tree being built
*/
public FOTreeControl foTreeControl = null;
public Document doc = null;

/**
* logging instance
@@ -66,11 +70,11 @@ public abstract class FOInputHandler {

/**
* Main constructor
* @param foTreeControl the FOTreeControl implementation that is controlling
* @param document the apps.Document implementation that is controlling
* the FO Tree being built
*/
public FOInputHandler(FOTreeControl foTreeControl) {
this.foTreeControl = foTreeControl;
public FOInputHandler(Document document) {
doc = document;
}

/**
@@ -90,11 +94,26 @@ public abstract class FOInputHandler {
}

/**
* Returns the FontInfo object associated with this FOInputHandler.
* @return the FontInof object
* Returns the FOTreeControl object associated with this FOInputHandler.
* @return the FOTreeControl object
*/
public Document getDocument() {
return doc;
}

/**
* @return the current Area Tree object
*/
public AreaTree getAreaTree() {
return doc.getAreaTree();
}
/**
* Returns the Driver object associated with this FOInputHandler.
* @return the Driver object
*/
public FOTreeControl getFontInfo() {
return null;
public Driver getDriver() {
return doc.getDriver();
}

/**

+ 1
- 1
src/java/org/apache/fop/fo/FOTreeControl.java View File

@@ -72,5 +72,5 @@ public interface FOTreeControl {
* @return the FontInfo object associated with this FOTree
*/
FontInfo getFontInfo();
}

+ 0
- 60
src/java/org/apache/fop/fo/FOTreeEvent.java View File

@@ -1,60 +0,0 @@
/*
* Copyright 1999-2004 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.fo;

//java
import java.util.EventObject;

//fop
import org.apache.fop.fo.pagination.PageSequence;

/**
* An Event used for notification that various stages of the building of an
* FO tree have been completed. Specifically, these are currently used to
* notify Driver when a PageSequence has been completed.
*/

public class FOTreeEvent extends EventObject {

private PageSequence pageSeq;

/**
* Constructor captures the object that fired the event.
* @param source the Object that fired the event.
*/
public FOTreeEvent (Object source) {
super(source);
}

/**
* Sets the PageSequence object for this event.
* @param pageSeq the PageSequence object attached to this event.
*/
public void setPageSequence(PageSequence pageSeq) {
this.pageSeq = pageSeq;
}

/**
* @return the PageSequence object attached to this event.
*/
public PageSequence getPageSequence () {
return pageSeq;
}
}


+ 89
- 56
src/java/org/apache/fop/fo/FOTreeHandler.java View File

@@ -19,6 +19,7 @@
package org.apache.fop.fo;

// Java
import java.io.IOException;
import java.util.HashSet;
import java.util.Iterator;

@@ -26,7 +27,11 @@ import java.util.Iterator;
import org.xml.sax.SAXException;

// FOP
import org.apache.fop.apps.Document;
import org.apache.fop.apps.FOPException;
import org.apache.fop.area.AreaTree;
import org.apache.fop.area.Title;
import org.apache.fop.fo.extensions.Bookmarks;
import org.apache.fop.fo.flow.BasicLink;
import org.apache.fop.fo.flow.Block;
import org.apache.fop.fo.flow.ExternalGraphic;
@@ -45,6 +50,11 @@ import org.apache.fop.fo.flow.TableCell;
import org.apache.fop.fo.flow.TableRow;
import org.apache.fop.fo.pagination.Flow;
import org.apache.fop.fo.pagination.PageSequence;
import org.apache.fop.layoutmgr.ContentLayoutManager;
import org.apache.fop.layoutmgr.InlineStackingLayoutManager;
import org.apache.fop.layoutmgr.LMiter;
import org.apache.fop.layoutmgr.PageLayoutManager;


/**
* Defines how SAX events specific to XSL-FO input should be handled when
@@ -80,12 +90,6 @@ public class FOTreeHandler extends FOInputHandler {
*/
private long startTime;

/**
* Collection of objects that have registered to be notified about
* FOTreeEvent firings.
*/
private HashSet foTreeListeners = new HashSet();

/**
* Main constructor
* @param foTreeControl the FOTreeControl implementation that governs this
@@ -93,8 +97,8 @@ public class FOTreeHandler extends FOInputHandler {
* @param store if true then use the store pages model and keep the
* area tree in memory
*/
public FOTreeHandler(FOTreeControl foTreeControl, boolean store) {
super(foTreeControl);
public FOTreeHandler(Document doc, boolean store) {
super(doc);
if (collectStatistics) {
runtime = Runtime.getRuntime();
}
@@ -125,7 +129,12 @@ public class FOTreeHandler extends FOInputHandler {
* @throws SAXException if there is some error
*/
public void endDocument() throws SAXException {
notifyDocumentComplete();
try {
getAreaTree().endDocument();
getDriver().getRenderer().stopRenderer();
} catch (IOException ex) {
throw new SAXException(ex);
}

if (collectStatistics) {
if (MEM_PROFILE_WITH_GC) {
@@ -184,7 +193,9 @@ public class FOTreeHandler extends FOInputHandler {
logger.debug("Current heap size: " + (memoryNow / 1024L) + "Kb");
}
}
notifyPageSequenceComplete(pageSequence);

getAreaTree().addBookmarksToAreaTree();
formatPageSequence(pageSequence, getAreaTree());
}

/**
@@ -457,66 +468,88 @@ public class FOTreeHandler extends FOInputHandler {
* @return the font information
*/
public FOTreeControl getFontInfo() {
return foTreeControl;
return doc;
}

/**
* Add an object to the collection of objects that should be notified about
* FOTreeEvent firings.
* @param listener the Object which should be notified
* Runs the formatting of this page sequence into the given area tree
*
* @param pageSeq the PageSequence to be formatted
* @param areaTree the area tree to format this page sequence into
* @throws FOPException if there is an error formatting the contents
*/
public void addFOTreeListener (FOTreeListener listener) {
if (listener == null) {
return;
private void formatPageSequence(PageSequence pageSeq, AreaTree areaTree)
throws FOPException {
Title title = null;
if (pageSeq.getTitleFO() != null) {
title = getTitleArea(pageSeq.getTitleFO());
}
foTreeListeners.add(listener);
}
areaTree.startPageSequence(title);
// Make a new PageLayoutManager and a FlowLayoutManager
// Run the PLM in a thread
// Wait for them to finish.

/**
* Remove an object from the collection of objects that should be notified
* about FOTreeEvent firings.
* @param listener the Object which should no longer be notified
*/
public void removeFOTreeListener (FOTreeListener listener) {
if (listener == null) {
// If no main flow, nothing to layout!
if (pageSeq.getMainFlow() == null) {
return;
}
foTreeListeners.remove(listener);
}

/**
* Notify all objects in the foTreeListeners that a "Page Sequence Complete"
* FOTreeEvent has been fired.
* @param eventType integer indicating which type of event is created
* @param event the Event object that should be passed to the listeners
*/
private void notifyPageSequenceComplete(PageSequence pageSequence)
throws FOPException {
FOTreeEvent event = new FOTreeEvent(this);
event.setPageSequence(pageSequence);
Iterator iterator = foTreeListeners.iterator();
FOTreeListener foTreeListenerItem = null;
while (iterator.hasNext()) {
foTreeListenerItem = (FOTreeListener)iterator.next();
foTreeListenerItem.foPageSequenceComplete(event);
// Initialize if already used?
// this.layoutMasterSet.resetPageMasters();
if (pageSeq.getPageSequenceMaster() != null) {
pageSeq.getPageSequenceMaster().reset();
}

pageSeq.initPageNumber();

// This will layout pages and add them to the area tree
PageLayoutManager pageLM = new PageLayoutManager(areaTree, pageSeq,
getDocument());
pageLM.setPageCounting(pageSeq.getCurrentPageNumber(),
pageSeq.getPageNumberGenerator());

// For now, skip the threading and just call run directly.
pageLM.run();

// Thread layoutThread = new Thread(pageLM);
// layoutThread.start();
// log.debug("Layout thread started");
// // wait on both managers
// try {
// layoutThread.join();
// log.debug("Layout thread done");
// } catch (InterruptedException ie) {
// log.error("PageSequence.format() interrupted waiting on layout");
// }
pageSeq.setCurrentPageNumber(pageLM.getPageCount());
// Tell the root the last page number we created.
pageSeq.getRoot().setRunningPageNumberCounter(pageSeq.getCurrentPageNumber());
}

/**
* Notify all objects in the foTreeListeners that a "Document Complete"
* FOTreeEvent has been fired.
* @param eventType integer indicating which type of event is created
* @param event the Event object that should be passed to the listeners
* @return the Title area
*/
private void notifyDocumentComplete()
throws SAXException {
FOTreeEvent event = new FOTreeEvent(this);
Iterator iterator = foTreeListeners.iterator();
FOTreeListener foTreeListenerItem = null;
while (iterator.hasNext()) {
foTreeListenerItem = (FOTreeListener)iterator.next();
foTreeListenerItem.foDocumentComplete(event);
}
private org.apache.fop.area.Title getTitleArea(org.apache.fop.fo.pagination.Title foTitle) {
// use special layout manager to add the inline areas
// to the Title.
InlineStackingLayoutManager lm;
lm = new InlineStackingLayoutManager(foTitle);
lm.setLMiter(new LMiter(lm, foTitle.children.listIterator()));
lm.initialize();

// get breaks then add areas to title
org.apache.fop.area.Title title =
new org.apache.fop.area.Title();

ContentLayoutManager clm = new ContentLayoutManager(title);
clm.setUserAgent(foTitle.getUserAgent());
lm.setParent(clm);

clm.fillArea(lm);

return title;
}

/**

+ 0
- 56
src/java/org/apache/fop/fo/FOTreeListener.java View File

@@ -1,56 +0,0 @@
/*
* Copyright 1999-2004 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.fo;

//java
import java.util.EventListener;

//sax
import org.xml.sax.SAXException;

//fop
import org.apache.fop.apps.FOPException;

/**
* An interface for classes that need to handle FOTreeEvent objects as they
* are fired.
* The key benefit to using this interface is that the implementation can handle
* PageSequence objects at a higher level, rather than dealing directly with
* a lower-level SAX event.
* @see FOTreeEvent
*/

public interface FOTreeListener extends EventListener {

/**
* Method for handling a completed PageSequence object.
* @param e the FOTreeEvent that has been fired
* @throws FOPException for invalid input
*/
void foPageSequenceComplete (FOTreeEvent e) throws FOPException;

/**
* Method for handling the end of the document.
* @param e the FOTreeEvent that has been fired
* @throws SAXException for parsing error
*/
void foDocumentComplete (FOTreeEvent e) throws SAXException;

}


+ 1
- 1
src/java/org/apache/fop/fo/pagination/Title.java View File

@@ -59,7 +59,7 @@ public class Title extends FObjMixed {
CommonBackground bProps = propMgr.getBackgroundProps();

// Common Font Properties
Font fontState = propMgr.getFontState(getFOTreeControl().getFOInputHandler().getFontInfo());
Font fontState = propMgr.getFontState(getFOTreeControl());

// Common Margin Properties-Inline
CommonMarginInline mProps = propMgr.getMarginInlineProps();

Loading…
Cancel
Save