From 3411937d72c11789edcec519e456dd3a0a1595f9 Mon Sep 17 00:00:00 2001 From: Vincent Hennebert Date: Thu, 12 Jul 2012 13:25:34 +0000 Subject: [PATCH] Bugfix: When restarting layout for the last page, discard glues and penalties at the beginning of the restarted Knuth sequence. git-svn-id: https://svn.apache.org/repos/asf/xmlgraphics/fop/trunk@1360665 13f79535-47bb-0310-9956-ffa450edef68 --- .../apache/fop/layoutmgr/AbstractBreaker.java | 12 ++--- .../fop/layoutmgr/BreakingAlgorithm.java | 9 ++-- .../apache/fop/layoutmgr/KnuthSequence.java | 51 ++++++------------- .../org/apache/fop/layoutmgr/PageBreaker.java | 2 +- status.xml | 4 ++ .../fop/intermediate/IFParserTestCase.java | 17 +------ .../layoutengine/LayoutEngineTestUtils.java | 7 +++ 7 files changed, 38 insertions(+), 64 deletions(-) diff --git a/src/java/org/apache/fop/layoutmgr/AbstractBreaker.java b/src/java/org/apache/fop/layoutmgr/AbstractBreaker.java index 5fa411101..0f7dda3e7 100644 --- a/src/java/org/apache/fop/layoutmgr/AbstractBreaker.java +++ b/src/java/org/apache/fop/layoutmgr/AbstractBreaker.java @@ -492,7 +492,6 @@ public abstract class AbstractBreaker { protected void addAreas(PageBreakingAlgorithm alg, int startPart, int partCount, BlockSequence originalList, BlockSequence effectiveList) { LayoutContext childLC; - // add areas int startElementIndex = 0; int endElementIndex = 0; int lastBreak = -1; @@ -550,12 +549,7 @@ public abstract class AbstractBreaker { // ignore KnuthGlue and KnuthPenalty objects // at the beginning of the line - ListIterator effectiveListIterator - = effectiveList.listIterator(startElementIndex); - while (effectiveListIterator.hasNext() - && !(effectiveListIterator.next()).isBox()) { - startElementIndex++; - } + startElementIndex = alg.par.getFirstBoxIndex(startElementIndex); if (startElementIndex <= endElementIndex) { if (log.isDebugEnabled()) { @@ -576,7 +570,9 @@ public abstract class AbstractBreaker { && p < (partCount - 1)) { // count the boxes whose width is not 0 int boxCount = 0; - effectiveListIterator = effectiveList.listIterator(startElementIndex); + @SuppressWarnings("unchecked") + ListIterator effectiveListIterator = effectiveList + .listIterator(startElementIndex); while (effectiveListIterator.nextIndex() <= endElementIndex) { KnuthElement tempEl = effectiveListIterator.next(); if (tempEl.isBox() && tempEl.getWidth() > 0) { diff --git a/src/java/org/apache/fop/layoutmgr/BreakingAlgorithm.java b/src/java/org/apache/fop/layoutmgr/BreakingAlgorithm.java index 7e55c31f6..3830878fe 100644 --- a/src/java/org/apache/fop/layoutmgr/BreakingAlgorithm.java +++ b/src/java/org/apache/fop/layoutmgr/BreakingAlgorithm.java @@ -532,14 +532,15 @@ public abstract class BreakingAlgorithm { // index of the first KnuthBox in the sequence, in case of non-centered // alignment. For centered alignment, we need to take into account preceding // penalties+glues used for the filler spaces - int firstBoxIndex = startIndex; + int previousPosition = startIndex; if (alignment != Constants.EN_CENTER) { - firstBoxIndex = par.getFirstBoxIndex(startIndex); + int firstBoxIndex = par.getFirstBoxIndex(startIndex); + previousPosition = (firstBoxIndex >= par.size()) ? startIndex : firstBoxIndex - 1; } - firstBoxIndex = (firstBoxIndex < 0) ? 0 : firstBoxIndex; + previousPosition = (previousPosition < 0) ? 0 : previousPosition; // create an active node representing the starting point - addNode(0, createNode(firstBoxIndex, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, null)); + addNode(0, createNode(previousPosition, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, null)); KnuthNode lastForced = getNode(0); if (log.isTraceEnabled()) { diff --git a/src/java/org/apache/fop/layoutmgr/KnuthSequence.java b/src/java/org/apache/fop/layoutmgr/KnuthSequence.java index d568da45e..8d5e77ae0 100644 --- a/src/java/org/apache/fop/layoutmgr/KnuthSequence.java +++ b/src/java/org/apache/fop/layoutmgr/KnuthSequence.java @@ -20,6 +20,7 @@ package org.apache.fop.layoutmgr; import java.util.ArrayList; +import java.util.Iterator; import java.util.List; import java.util.ListIterator; @@ -159,46 +160,26 @@ public abstract class KnuthSequence extends ArrayList { : (ListElement) get(index); } - /** @return the position index of the first box in this sequence */ - protected int getFirstBoxIndex() { - if (isEmpty()) { - return -1; - } else { - return getFirstBoxIndex(0); - } - } - /** - * Get the position index of the first box in this sequence, - * starting at the given index. If there is no box after the - * passed {@code startIndex}, the starting index itself is returned. - * @param startIndex the starting index for the lookup - * @return the absolute position index of the next box element + * Returns the position index of the first box in this sequence, starting at the given + * index. If {@code startIndex} is outside the bounds of this sequence, it is + * returned. + * + * @param startIndex the index from which to start the lookup + * @return the index of the next box element, {@link #size()} if there is no such + * element, {@code startIndex} if {@code (startIndex < 0 || startIndex >= size())} */ protected int getFirstBoxIndex(int startIndex) { - if (isEmpty() || startIndex < 0 || startIndex >= size()) { - return -1; + if (startIndex < 0 || startIndex >= size()) { + return startIndex; } else { - ListElement element = null; - int posIndex = startIndex; - int lastIndex = size(); - while ( posIndex < lastIndex ) { - element = getElement(posIndex); - if ( !element.isBox() ) { - posIndex++; - } else { - break; - } - } - if ( posIndex != startIndex ) { - if ( ( element != null ) && element.isBox() ) { - return posIndex - 1; - } else { - return startIndex; - } - } else { - return startIndex; + int boxIndex = startIndex; + @SuppressWarnings("unchecked") + Iterator iter = listIterator(startIndex); + while (iter.hasNext() && !iter.next().isBox()) { + boxIndex++; } + return boxIndex; } } diff --git a/src/java/org/apache/fop/layoutmgr/PageBreaker.java b/src/java/org/apache/fop/layoutmgr/PageBreaker.java index 8fd131a73..777180c55 100644 --- a/src/java/org/apache/fop/layoutmgr/PageBreaker.java +++ b/src/java/org/apache/fop/layoutmgr/PageBreaker.java @@ -330,7 +330,7 @@ public class PageBreaker extends AbstractBreaker { //Get page break from which we restart PageBreakPosition pbp = (PageBreakPosition) alg.getPageBreaks().get(restartPoint - 1); - newStartPos = pbp.getLeafPos() + 1; + newStartPos = alg.par.getFirstBoxIndex(pbp.getLeafPos() + 1); //Handle page break right here to avoid any side-effects if (newStartPos > 0) { handleBreakTrait(Constants.EN_PAGE); diff --git a/status.xml b/status.xml index bd337df9a..3a433044f 100644 --- a/status.xml +++ b/status.xml @@ -63,6 +63,10 @@ documents. Example: the fix of marks layering will be such a case when it's done. --> + + When restarting layout for the last page, discard glues and penalties at the beginning of + the restarted Knuth sequence. + Fix errors and warnings in example files. Add build.xml for documentation examples. diff --git a/test/java/org/apache/fop/intermediate/IFParserTestCase.java b/test/java/org/apache/fop/intermediate/IFParserTestCase.java index 45aa8ab0f..9a08816c6 100644 --- a/test/java/org/apache/fop/intermediate/IFParserTestCase.java +++ b/test/java/org/apache/fop/intermediate/IFParserTestCase.java @@ -48,9 +48,6 @@ import org.apache.fop.render.intermediate.IFSerializer; @RunWith(Parameterized.class) public class IFParserTestCase extends AbstractIFTest { - /** Set this to true to get the correspondence between test number and test file. */ - private static final boolean DEBUG = false; - /** * Gets the parameters for this test * @@ -59,19 +56,7 @@ public class IFParserTestCase extends AbstractIFTest { */ @Parameters public static Collection getParameters() throws IOException { - Collection testFiles = LayoutEngineTestUtils.getLayoutTestFiles(); - if (DEBUG) { - printFiles(testFiles); - } - return testFiles; - } - - private static void printFiles(Collection files) { - int index = 0; - for (File[] file : files) { - assert file.length == 1; - System.out.println(String.format("%3d %s", index++, file[0])); - } + return LayoutEngineTestUtils.getLayoutTestFiles(); } /** diff --git a/test/java/org/apache/fop/layoutengine/LayoutEngineTestUtils.java b/test/java/org/apache/fop/layoutengine/LayoutEngineTestUtils.java index 935b86c3b..fb7f07023 100644 --- a/test/java/org/apache/fop/layoutengine/LayoutEngineTestUtils.java +++ b/test/java/org/apache/fop/layoutengine/LayoutEngineTestUtils.java @@ -51,6 +51,9 @@ import org.apache.commons.io.filefilter.TrueFileFilter; */ public final class LayoutEngineTestUtils { + /** Set this to true to get the correspondence between test number and test file. */ + private static final boolean DEBUG = false; + private LayoutEngineTestUtils() { } @@ -157,8 +160,12 @@ public final class LayoutEngineTestUtils { } Collection parametersForJUnit4 = new ArrayList(); + int index = 0; for (File f : files) { parametersForJUnit4.add(new File[] { f }); + if (DEBUG) { + System.out.println(String.format("%3d %s", index++, f)); + } } return parametersForJUnit4; -- 2.39.5