From 7ff677faf4d40f711d0c36ed38c612947127fde5 Mon Sep 17 00:00:00 2001 From: Glen Mazza Date: Thu, 17 Feb 2005 00:27:54 +0000 Subject: [PATCH] PR: Obtained from: Submitted by: Reviewed by: Cleanup of page number handling throughout application. Removed old code from PageSequence.java. git-svn-id: https://svn.apache.org/repos/asf/xmlgraphics/fop/trunk@198434 13f79535-47bb-0310-9956-ffa450edef68 --- .../org/apache/fop/area/AreaTreeHandler.java | 2 +- .../fop/fo/pagination/PageSequence.java | 274 ++---------------- .../org/apache/fop/fo/pagination/Root.java | 43 ++- .../fop/layoutmgr/AbstractLayoutManager.java | 4 +- .../fop/layoutmgr/ContentLayoutManager.java | 4 +- .../apache/fop/layoutmgr/LayoutManager.java | 2 +- .../layoutmgr/PageNumberLayoutManager.java | 2 +- .../layoutmgr/PageSequenceLayoutManager.java | 28 +- 8 files changed, 85 insertions(+), 274 deletions(-) diff --git a/src/java/org/apache/fop/area/AreaTreeHandler.java b/src/java/org/apache/fop/area/AreaTreeHandler.java index f51db4e41..2d6328f13 100644 --- a/src/java/org/apache/fop/area/AreaTreeHandler.java +++ b/src/java/org/apache/fop/area/AreaTreeHandler.java @@ -264,7 +264,7 @@ public class AreaTreeHandler extends FOEventHandler { long memoryNow = runtime.totalMemory() - runtime.freeMemory(); long memoryUsed = (memoryNow - initialMemory) / 1024L; long timeUsed = System.currentTimeMillis() - startTime; - int pageCount = rootFObj.getRunningPageNumberCounter(); + int pageCount = rootFObj.getTotalPagesGenerated(); log.debug("Initial heap size: " + (initialMemory / 1024L) + "Kb"); log.debug("Current heap size: " + (memoryNow / 1024L) + "Kb"); log.debug("Total memory used: " + memoryUsed + "Kb"); diff --git a/src/java/org/apache/fop/fo/pagination/PageSequence.java b/src/java/org/apache/fop/fo/pagination/PageSequence.java index 2dce9b1b9..21974cf06 100644 --- a/src/java/org/apache/fop/fo/pagination/PageSequence.java +++ b/src/java/org/apache/fop/fo/pagination/PageSequence.java @@ -49,11 +49,6 @@ public class PageSequence extends FObj { private String masterReference; // End of property values - // - // initial-page-number types - // - public static final int EXPLICIT = 0; - /** * The parent root object */ @@ -76,32 +71,12 @@ public class PageSequence extends FObj { // private boolean isFlowSet = false; - // for structure handler - private boolean sequenceStarted = false; - - // - // state attributes used during layout - // - // page number and related formatting variables - public int currentPageNumber = 0; + public int startingPageNumber = 0; private int explicitFirstNumber = 0; // explicitly specified - public int firstPageNumber = 0; // actual private PageNumberGenerator pageNumberGenerator; - - private int pageCount = 0; private boolean isForcing = false; - /** - * specifies page numbering type (auto|auto-even|auto-odd|explicit) - */ - public int pageNumberType; - - /** - * used to determine whether to calculate auto, auto-even, auto-odd - */ - private boolean thisIsFirstPage; - /** * The currentSimplePageMaster is either the page master for the * whole page sequence if master-reference refers to a simple-page-master, @@ -155,18 +130,6 @@ public class PageSequence extends FObj { layoutMasterSet = root.getLayoutMasterSet(); flowMap = new HashMap(); - // we are now on the first page of the page sequence - thisIsFirstPage = true; - - if (initialPageNumber.getEnum() != 0) { - // auto | auto-odd | auto-even. - pageNumberType = initialPageNumber.getEnum(); - } else { - pageNumberType = EXPLICIT; - int pageStart = initialPageNumber.getValue(); - this.explicitFirstNumber = (pageStart > 0) ? pageStart : 1; - } - this.simplePageMaster = this.layoutMasterSet.getSimplePageMaster(masterReference); if (this.simplePageMaster == null) { @@ -186,9 +149,8 @@ public class PageSequence extends FObj { new PageNumberGenerator(format, groupingSeparator, groupingSize, letterValue); checkId(id); - //call startStructuredPageSequence to ensure, that startPageSequence is called - //before startFlow. - startStructuredPageSequence(); + initPageNumber(); + getFOEventHandler().startPageSequence(this); } /** @@ -248,13 +210,11 @@ public class PageSequence extends FObj { } else if (childId == FO_FLOW) { this.mainFlow = (Flow) child; addFlow(mainFlow); - startStructuredPageSequence(); super.addChildNode(child); // For getChildren } else if (childId == FO_STATIC_CONTENT) { addFlow((StaticContent) child); String flowName = ((StaticContent) child).getFlowName(); flowMap.put(flowName, child); - startStructuredPageSequence(); } } @@ -283,199 +243,35 @@ public class PageSequence extends FObj { } } - /** - * Start the page-sequence logic in the Structured Handler - */ - private void startStructuredPageSequence() { - if (!sequenceStarted) { - getFOEventHandler().startPageSequence(this); - sequenceStarted = true; - } - } - /** * Initialize the current page number for the start of the page sequence. */ - public void initPageNumber() { - this.currentPageNumber = this.root.getRunningPageNumberCounter() + 1; - - if (this.pageNumberType == EN_AUTO_ODD) { - // Next page but force odd. May force empty page creation! - // Whose master is used for this??? Assume no. - // Use force-page-count = auto - // on preceding page-sequence to make sure that there is no gap! - if (currentPageNumber % 2 == 0) { - this.currentPageNumber++; - } - } else if (pageNumberType == EN_AUTO_EVEN) { - if (currentPageNumber % 2 == 1) { - this.currentPageNumber++; + private void initPageNumber() { + int pageNumberType = 0; + + if (initialPageNumber.getEnum() != 0) { + // auto | auto-odd | auto-even. + startingPageNumber = root.getEndingPageNumberOfPreviousSequence() + 1; + pageNumberType = initialPageNumber.getEnum(); + if (pageNumberType == EN_AUTO_ODD) { + // Next page but force odd. May force empty page creation! + // Whose master is used for this??? Assume no. + // Use force-page-count = auto + // on preceding page-sequence to make sure that there is no gap! + if (startingPageNumber % 2 == 0) { + startingPageNumber++; + } + } else if (pageNumberType == EN_AUTO_EVEN) { + if (startingPageNumber % 2 == 1) { + startingPageNumber++; + } } - } else if (pageNumberType == EXPLICIT) { - this.currentPageNumber = this.explicitFirstNumber; + } else { // for explicit page number + int pageStart = initialPageNumber.getValue(); + startingPageNumber = (pageStart > 0) ? pageStart : 1; // spec rule } - this.firstPageNumber = this.currentPageNumber; } - /** - * Creates a new page area for the given parameters - * @param areaTree the area tree the page should be contained in - * @param firstAvailPageNumber the page number for this page - * @param isFirstPage true when this is the first page in the sequence - * @param isEmptyPage true if this page will be empty - * (e.g. forced even or odd break) - * @return a Page layout object based on the page master selected - * from the params - * @todo modify the other methods to use even/odd flag and bIsLast - */ -// private PageViewport makePage(int firstAvailPageNumber, -// boolean isFirstPage, boolean bIsLast, -// boolean isEmptyPage) throws FOPException { -// // layout this page sequence - -// // while there is still stuff in the flow, ask the -// // layoutMasterSet for a new page - -// // page number is 0-indexed -// PageMaster pageMaster = getNextPageMaster(masterName, -// firstAvailPageNumber, -// isFirstPage, isEmptyPage); - -// // a legal alternative is to use the last sub-sequence -// // specification which should be handled in getNextSubsequence. -// // That's not done here. -// if (pageMaster == null) { -// throw new FOPException("page masters exhausted. Cannot recover."); -// } -// PageViewport p = pageMaster.makePage(); -// return p; -// } - - /** - * Returns the next SubSequenceSpecifier for the given page sequence master. - * The result is bassed on the current state of this page sequence. - */ -// private SubSequenceSpecifier getNextSubsequence(PageSequenceMaster master) { -// if (master.getSubSequenceSpecifierCount() -// > currentSubsequenceNumber + 1) { - -// currentSubsequence = -// master.getSubSequenceSpecifier(currentSubsequenceNumber + 1); -// currentSubsequenceNumber++; -// return currentSubsequence; -// } else { -// return null; -// } -// } - - /** - * Returns the next simple page master for the given sequence master, page number and - * other state information - */ -// private SimplePageMaster getNextSimplePageMaster(PageSequenceMaster sequenceMaster, -// int pageNumber, boolean thisIsFirstPage, -// boolean isEmptyPage) { -// // handle forcing -// if (isForcing) { -// String nextPageMaster = getNextPageMasterName(sequenceMaster, -// pageNumber, false, true); -// return this.layoutMasterSet.getSimplePageMaster(nextPageMaster); -// } -// String nextPageMaster = getNextPageMasterName(sequenceMaster, -// pageNumber, thisIsFirstPage, isEmptyPage); -// return this.layoutMasterSet.getSimplePageMaster(nextPageMaster); - -// } - - /** - * Get the next page master name. - * This gets the name of the next page master. If the sequence - * is exhausted then an error is indicated and the last page - * master name is used. - */ -// private String getNextPageMasterName(PageSequenceMaster sequenceMaster, -// int pageNumber, -// boolean thisIsFirstPage, -// boolean isEmptyPage) { - -// if (null == currentSubsequence) { -// currentSubsequence = getNextSubsequence(sequenceMaster); -// } - -// String nextPageMaster = -// currentSubsequence.getNextPageMaster(pageNumber, -// thisIsFirstPage, -// isEmptyPage); - - -// if (null == nextPageMaster -// || isFlowForMasterNameDone(currentPageMasterName)) { -// SubSequenceSpecifier nextSubsequence = -// getNextSubsequence(sequenceMaster); -// if (nextSubsequence == null) { -// getLogger().error("Page subsequences exhausted. Using previous subsequence."); -// thisIsFirstPage = -// true; // this becomes the first page in the new (old really) page master -// currentSubsequence.reset(); - -// // we leave currentSubsequence alone -// } -// else { -// currentSubsequence = nextSubsequence; -// } - -// nextPageMaster = -// currentSubsequence.getNextPageMaster(pageNumber, -// thisIsFirstPage, -// isEmptyPage); -// } -// currentPageMasterName = nextPageMaster; - -// return nextPageMaster; - -// } - -// private SimplePageMaster getCurrentSimplePageMaster() { -// return this.layoutMasterSet.getSimplePageMaster(currentPageMasterName); -// } - -// private String getCurrentPageMasterName() { -// return currentPageMasterName; -// } - - // refactored from LayoutMasterSet -// private PageMaster getNextPageMaster(String pageSequenceName, -// int pageNumber, -// boolean thisIsFirstPage, -// boolean isEmptyPage) throws FOPException { -// PageMaster pageMaster = null; - -// // see if there is a page master sequence for this master name -// PageSequenceMaster sequenceMaster = -// this.layoutMasterSet.getPageSequenceMaster(pageSequenceName); - -// if (sequenceMaster != null) { -// pageMaster = getNextSimplePageMaster(sequenceMaster, -// pageNumber, -// thisIsFirstPage, -// isEmptyPage).getPageMaster(); - -// } else { // otherwise see if there's a simple master by the given name -// SimplePageMaster simpleMaster = -// this.layoutMasterSet.getSimplePageMaster(pageSequenceName); -// if (simpleMaster == null) { -// throw new FOPException("'master-reference' for 'fo:page-sequence'" -// + "matches no 'simple-page-master'" -// + " or 'page-sequence-master'"); -// } -// currentPageMasterName = pageSequenceName; - -// pageMaster = simpleMaster.getNextPageMaster(); -// } -// return pageMaster; -// } - - // /** // * Returns true when there is more flow elements left to lay out. // */ @@ -545,12 +341,12 @@ public class PageSequence extends FObj { // } /** - * Get the current page number for this page sequence. + * Get the starting page number for this page sequence. * - * @return the current page number + * @return the starting page number */ - public int getCurrentPageNumber() { - return currentPageNumber; + public int getStartingPageNumber() { + return startingPageNumber; } // private void forcePage(AreaTree areaTree, int firstAvailPageNumber) { @@ -688,18 +484,6 @@ public class PageSequence extends FObj { return pageNumberGenerator.makeFormattedPageNumber(pageNumber); } - /** - * Public accessor for setting the currentPageNumber. - * @param currentPageNumber the value to which currentPageNumber should be - * set. - */ - public void setCurrentPageNumber(int currentPageNumber) { - this.currentPageNumber = currentPageNumber; - - // Tell the root the last page number we created. - root.setRunningPageNumberCounter(currentPageNumber); - } - /** * Public accessor for the ancestor Root. * @return the ancestor Root diff --git a/src/java/org/apache/fop/fo/pagination/Root.java b/src/java/org/apache/fop/fo/pagination/Root.java index 639eab7f9..556fc1ab8 100644 --- a/src/java/org/apache/fop/fo/pagination/Root.java +++ b/src/java/org/apache/fop/fo/pagination/Root.java @@ -51,7 +51,8 @@ public class Root extends FObj { /** * Keeps count of page number from over PageSequence instances */ - private int runningPageNumberCounter = 0; + private int endingPageNumberOfPreviousSequence = 0; + private int totalPagesGenerated = 0; /** * FOEventHandler object for this FO Tree @@ -150,19 +151,43 @@ public class Root extends FObj { } /** - * Returns the number of pages generated (over all PageSequence instances). - * @return the number of pages + * Gets the last page number generated by the previous page-sequence + * @return the last page number, 0 if no page sequences yet generated */ - public int getRunningPageNumberCounter() { - return this.runningPageNumberCounter; + public int getEndingPageNumberOfPreviousSequence() { + return endingPageNumberOfPreviousSequence; } /** - * Sets the overall page number counter. - * @param count the new page count + * Sets the last page number by the just-finished page-sequence + * @param lastPageNumber the last page number of the sequence */ - public void setRunningPageNumberCounter(int count) { - this.runningPageNumberCounter = count; + public void setEndingPageNumberOfPreviousSequence(int lastPageNumber) { + endingPageNumberOfPreviousSequence = lastPageNumber; + } + + /** + * Returns the total number of pages generated by FOP + * (May not equal endingPageNumberOfPreviousSequence due to + * initial-page-number property on fo:page-sequences.) + * @return the last page number, 0 if no page sequences yet generated + */ + public int getTotalPagesGenerated() { + return totalPagesGenerated; + } + + /** + * Report additional pages generated to increase the totalPagesGenerated counter + * @param lastPageNumber the last page number of the sequence + * @throws IllegalArgumentException for negative additional page counts + */ + public void reportAdditionalPagesGenerated(int additionalPages) { + if (additionalPages >= 0) { + totalPagesGenerated += additionalPages; + } else { + throw new IllegalArgumentException( + "Number of additional pages must be zero or greater."); + } } /** diff --git a/src/java/org/apache/fop/layoutmgr/AbstractLayoutManager.java b/src/java/org/apache/fop/layoutmgr/AbstractLayoutManager.java index 4edd78c3e..4e6c6221f 100644 --- a/src/java/org/apache/fop/layoutmgr/AbstractLayoutManager.java +++ b/src/java/org/apache/fop/layoutmgr/AbstractLayoutManager.java @@ -314,8 +314,8 @@ public abstract class AbstractLayoutManager implements LayoutManager, Constants * * @see org.apache.fop.layoutmgr.LayoutManager */ - public String getCurrentPageNumber() { - return parentLM.getCurrentPageNumber(); + public String getCurrentPageNumberString() { + return parentLM.getCurrentPageNumberString(); } /** diff --git a/src/java/org/apache/fop/layoutmgr/ContentLayoutManager.java b/src/java/org/apache/fop/layoutmgr/ContentLayoutManager.java index 52d351a2e..06cd94230 100644 --- a/src/java/org/apache/fop/layoutmgr/ContentLayoutManager.java +++ b/src/java/org/apache/fop/layoutmgr/ContentLayoutManager.java @@ -234,8 +234,8 @@ public class ContentLayoutManager implements InlineLevelLayoutManager { Position bp2) { } /** @see org.apache.fop.layoutmgr.LayoutManager */ - public String getCurrentPageNumber() { - return parentLM.getCurrentPageNumber(); + public String getCurrentPageNumberString() { + return parentLM.getCurrentPageNumberString(); } /** @see org.apache.fop.layoutmgr.LayoutManager */ diff --git a/src/java/org/apache/fop/layoutmgr/LayoutManager.java b/src/java/org/apache/fop/layoutmgr/LayoutManager.java index edd283dbc..93b674281 100644 --- a/src/java/org/apache/fop/layoutmgr/LayoutManager.java +++ b/src/java/org/apache/fop/layoutmgr/LayoutManager.java @@ -172,7 +172,7 @@ public interface LayoutManager { * * @return the string for the current page number */ - String getCurrentPageNumber(); + String getCurrentPageNumberString(); /** * Resolve the id reference. diff --git a/src/java/org/apache/fop/layoutmgr/PageNumberLayoutManager.java b/src/java/org/apache/fop/layoutmgr/PageNumberLayoutManager.java index e2b8d6935..89298bb69 100644 --- a/src/java/org/apache/fop/layoutmgr/PageNumberLayoutManager.java +++ b/src/java/org/apache/fop/layoutmgr/PageNumberLayoutManager.java @@ -46,7 +46,7 @@ public class PageNumberLayoutManager extends LeafNodeLayoutManager { public InlineArea get(LayoutContext context) { // get page string from parent, build area TextArea inline = new TextArea(); - String str = parentLM.getCurrentPageNumber(); + String str = parentLM.getCurrentPageNumberString(); int width = 0; for (int count = 0; count < str.length(); count++) { width += font.getCharWidth(str.charAt(count)); diff --git a/src/java/org/apache/fop/layoutmgr/PageSequenceLayoutManager.java b/src/java/org/apache/fop/layoutmgr/PageSequenceLayoutManager.java index ce86164e9..922a9e207 100644 --- a/src/java/org/apache/fop/layoutmgr/PageSequenceLayoutManager.java +++ b/src/java/org/apache/fop/layoutmgr/PageSequenceLayoutManager.java @@ -75,7 +75,8 @@ public class PageSequenceLayoutManager extends AbstractLayoutManager { } } - private int pageCount = 1; + private int startPageNum = 0; + private int currentPageNum = 0; private String pageNumberString; private boolean isFirstPage = true; @@ -177,9 +178,9 @@ public class PageSequenceLayoutManager extends AbstractLayoutManager { * which creates and adds all the pages to the area tree. */ public void activateLayout() { - pageSeq.initPageNumber(); - pageCount = pageSeq.getCurrentPageNumber(); - pageNumberString = pageSeq.makeFormattedPageNumber(pageCount); + startPageNum = pageSeq.getStartingPageNumber(); + currentPageNum = startPageNum; + pageNumberString = pageSeq.makeFormattedPageNumber(currentPageNum); LineArea title = null; if (pageSeq.getTitleFO() != null) { @@ -203,14 +204,15 @@ public class PageSequenceLayoutManager extends AbstractLayoutManager { // finish page and add to area tree finishPage(); - pageCount++; - pageNumberString = pageSeq.makeFormattedPageNumber(pageCount); + currentPageNum++; + pageNumberString = pageSeq.makeFormattedPageNumber(currentPageNum); } } - pageCount--; + // TODO: Don't decrement currentPageNum when no pages are generated + currentPageNum--; log.debug("Ending layout"); finishPage(); - pageSeq.setCurrentPageNumber(pageCount); + pageSeq.getRoot().reportAdditionalPagesGenerated((currentPageNum - startPageNum) + 1); } /** @see org.apache.fop.layoutmgr.LayoutManager#isBogus() */ @@ -257,7 +259,7 @@ public class PageSequenceLayoutManager extends AbstractLayoutManager { * * @return the formatted page number string */ - public String getCurrentPageNumber() { + public String getCurrentPageNumberString() { return pageNumberString; } @@ -464,7 +466,7 @@ public class PageSequenceLayoutManager extends AbstractLayoutManager { try { // create a new page currentSimplePageMaster = pageSeq.getSimplePageMasterToUse( - pageCount, isFirstPage, bIsBlank); + currentPageNum, isFirstPage, bIsBlank); Region body = currentSimplePageMaster.getRegion(FO_REGION_BODY); if (!pageSeq.getMainFlow().getFlowName().equals(body.getRegionName())) { throw new FOPException("Flow '" + pageSeq.getMainFlow().getFlowName() @@ -477,7 +479,7 @@ public class PageSequenceLayoutManager extends AbstractLayoutManager { throw new IllegalArgumentException("Cannot create page: " + fopex.getMessage()); } - curPage.setPageNumberString(getCurrentPageNumber()); + curPage.setPageNumberString(pageNumberString); if (log.isDebugEnabled()) { log.debug("[" + curPage.getPageNumberString() + "]"); } @@ -669,7 +671,7 @@ public class PageSequenceLayoutManager extends AbstractLayoutManager { } else { /* IF we are on the kind of page we need, we'll need a new page. */ - if (pageCount%2 != 0) { + if (currentPageNum%2 != 0) { // Current page is odd return (breakValue == Constants.EN_ODD_PAGE); } @@ -687,7 +689,7 @@ public class PageSequenceLayoutManager extends AbstractLayoutManager { if (breakValue == Constants.EN_PAGE) { return false; } - else if (pageCount%2 != 0) { + else if (currentPageNum%2 != 0) { // Current page is odd return (breakValue == Constants.EN_EVEN_PAGE); } -- 2.39.5