From d2d04b82a13aa73cd13c06ddc8f16b00de2b7c18 Mon Sep 17 00:00:00 2001 From: "Andreas L. Delmelle" Date: Sun, 6 Feb 2011 01:17:57 +0000 Subject: [PATCH] Further cleanup: reduce some code duplication in BlockStackingLM's getNextKnuthElements() variants git-svn-id: https://svn.apache.org/repos/asf/xmlgraphics/fop/trunk@1067567 13f79535-47bb-0310-9956-ffa450edef68 --- .../fop/layoutmgr/BlockLayoutManager.java | 8 +- .../layoutmgr/BlockStackingLayoutManager.java | 208 +++++------------- 2 files changed, 51 insertions(+), 165 deletions(-) diff --git a/src/java/org/apache/fop/layoutmgr/BlockLayoutManager.java b/src/java/org/apache/fop/layoutmgr/BlockLayoutManager.java index bfcc44b3c..a7361ae31 100644 --- a/src/java/org/apache/fop/layoutmgr/BlockLayoutManager.java +++ b/src/java/org/apache/fop/layoutmgr/BlockLayoutManager.java @@ -110,12 +110,8 @@ public class BlockLayoutManager extends BlockStackingLayoutManager public List getNextKnuthElements(LayoutContext context, int alignment, Stack lmStack, Position restartPosition, LayoutManager restartAtLM) { resetSpaces(); - if (lmStack == null) { - return super.getNextKnuthElements(context, alignment); - } else { - return super.getNextKnuthElements(context, alignment, lmStack, restartPosition, - restartAtLM); - } + return super.getNextKnuthElements( + context, alignment, lmStack, restartPosition, restartAtLM); } private void resetSpaces() { diff --git a/src/java/org/apache/fop/layoutmgr/BlockStackingLayoutManager.java b/src/java/org/apache/fop/layoutmgr/BlockStackingLayoutManager.java index 4f0ab9ddb..ee43be6fa 100644 --- a/src/java/org/apache/fop/layoutmgr/BlockStackingLayoutManager.java +++ b/src/java/org/apache/fop/layoutmgr/BlockStackingLayoutManager.java @@ -238,6 +238,14 @@ public abstract class BlockStackingLayoutManager extends AbstractLayoutManager /** {@inheritDoc} */ @Override public List getNextKnuthElements(LayoutContext context, int alignment) { + return getNextKnuthElements(context, alignment, null, null, null); + } + + /** {@inheritDoc} */ + @Override + public List getNextKnuthElements // CSOK: MethodLength + (LayoutContext context, int alignment, Stack lmStack, + Position restartPosition, LayoutManager restartAtLM) { referenceIPD = context.getRefIPD(); updateContentAreaIPDwithOverconstrainedAdjust(); @@ -267,33 +275,41 @@ public abstract class BlockStackingLayoutManager extends AbstractLayoutManager //Used to indicate a special break-after case when all content has already been generated. BreakElement forcedBreakAfterLast = null; + LayoutContext childLC = new LayoutContext(0); + List childElements; LayoutManager currentChildLM; - while ((currentChildLM = getChildLM()) != null) { - LayoutContext childLC = new LayoutContext(0); - - List childrenElements - = getNextChildElements(currentChildLM, context, childLC, alignment); - + if (lmStack != null) { + if (lmStack.isEmpty()) { + assert restartAtLM != null && restartAtLM.getParent() == this; + currentChildLM = restartAtLM; + currentChildLM.reset(); + setCurrentChildLM(currentChildLM); + childElements = getNextChildElements(currentChildLM, context, childLC, alignment); + } else { + currentChildLM = (LayoutManager) lmStack.pop(); + setCurrentChildLM(currentChildLM); + childElements = getNextChildElements(currentChildLM, context, childLC, alignment, + lmStack, restartPosition, restartAtLM); + } if (contentList.isEmpty()) { //Propagate keep-with-previous up from the first child context.updateKeepWithPreviousPending(childLC.getKeepWithPreviousPending()); } - if (childrenElements != null && !childrenElements.isEmpty()) { + if (childElements != null && !childElements.isEmpty()) { if (!contentList.isEmpty() - && !ElementListUtils.startsWithForcedBreak(childrenElements)) { + && !ElementListUtils.startsWithForcedBreak(childElements)) { // there is a block handled by prevLM before the one // handled by curLM, and the one handled // by the current LM does not begin with a break addInBetweenBreak(contentList, context, childLC); } - if (childrenElements.size() == 1 - && ElementListUtils.startsWithForcedBreak(childrenElements)) { + if (childElements.size() == 1 + && ElementListUtils.startsWithForcedBreak(childElements)) { if (currentChildLM.isFinished() && !hasNextChildLM()) { // a descendant of this block has break-before - forcedBreakAfterLast = (BreakElement) childrenElements.get(0); + forcedBreakAfterLast = (BreakElement) childElements.get(0); context.clearPendingMarks(); - break; } if (contentList.isEmpty()) { @@ -302,19 +318,18 @@ public abstract class BlockStackingLayoutManager extends AbstractLayoutManager elements.add(makeAuxiliaryZeroWidthBox()); } // a descendant of this block has break-before - contentList.addAll(childrenElements); + contentList.addAll(childElements); wrapPositionElements(contentList, elements); return elements; } else { - contentList.addAll(childrenElements); - if (ElementListUtils.endsWithForcedBreak(childrenElements)) { + contentList.addAll(childElements); + if (ElementListUtils.endsWithForcedBreak(childElements)) { // a descendant of this block has break-after if (currentChildLM.isFinished() && !hasNextChildLM()) { forcedBreakAfterLast = (BreakElement) ListUtil.removeLast(contentList); context.clearPendingMarks(); - break; } wrapPositionElements(contentList, elements); @@ -326,160 +341,34 @@ public abstract class BlockStackingLayoutManager extends AbstractLayoutManager } } - if (!contentList.isEmpty()) { - wrapPositionElements(contentList, elements); - } else if (forcedBreakAfterLast == null) { - // Empty fo:block, zero-length box makes sure the IDs and/or markers - // are registered. - elements.add(makeAuxiliaryZeroWidthBox()); - } - - addKnuthElementsForBorderPaddingAfter(elements, true); - addKnuthElementsForSpaceAfter(elements, alignment); - - //All child content is processed. Only break-after can occur now, so... - context.clearPendingMarks(); - if (forcedBreakAfterLast == null) { - addKnuthElementsForBreakAfter(elements, context); - } else { - forcedBreakAfterLast.clearPendingMarks(); - elements.add(forcedBreakAfterLast); - } - - context.updateKeepWithNextPending(getKeepWithNext()); - - setFinished(true); - - return elements; - } - - /** {@inheritDoc} */ - @Override - public List getNextKnuthElements // CSOK: MethodLength - (LayoutContext context, int alignment, Stack lmStack, - Position restartPosition, LayoutManager restartAtLM) { - referenceIPD = context.getRefIPD(); - updateContentAreaIPDwithOverconstrainedAdjust(); - - List contentList = new LinkedList(); - List elements = new LinkedList(); - - if (!breakBeforeServed) { - breakBeforeServed = true; - if (!context.suppressBreakBefore()) { - if (addKnuthElementsForBreakBefore(elements, context)) { - return elements; - } - } - } - - if (!firstVisibleMarkServed) { - addKnuthElementsForSpaceBefore(elements, alignment); - context.updateKeepWithPreviousPending(getKeepWithPrevious()); - } - - addKnuthElementsForBorderPaddingBefore(elements, !firstVisibleMarkServed); - firstVisibleMarkServed = true; - - //Spaces, border and padding to be repeated at each break - addPendingMarks(context); - - //Used to indicate a special break-after case when all content has already been generated. - BreakElement forcedBreakAfterLast = null; - - LayoutContext childLC = new LayoutContext(0); - List childrenElements; - LayoutManager currentChildLM; - if (lmStack.isEmpty()) { - assert restartAtLM != null && restartAtLM.getParent() == this; - currentChildLM = restartAtLM; - currentChildLM.reset(); - setCurrentChildLM(currentChildLM); - - childrenElements = getNextChildElements(currentChildLM, context, childLC, - alignment); - } else { - currentChildLM = (LayoutManager) lmStack.pop(); - setCurrentChildLM(currentChildLM); - childrenElements = getNextChildElements(currentChildLM, context, childLC, alignment, - lmStack, restartPosition, restartAtLM); - } - - if (contentList.isEmpty()) { - //Propagate keep-with-previous up from the first child - context.updateKeepWithPreviousPending(childLC.getKeepWithPreviousPending()); - } - if (childrenElements != null && !childrenElements.isEmpty()) { - if (!contentList.isEmpty() - && !ElementListUtils.startsWithForcedBreak(childrenElements)) { - // there is a block handled by prevLM before the one - // handled by curLM, and the one handled - // by the current LM does not begin with a break - addInBetweenBreak(contentList, context, childLC); - } - if (childrenElements.size() == 1 - && ElementListUtils.startsWithForcedBreak(childrenElements)) { - - if (currentChildLM.isFinished() && !hasNextChildLM()) { - // a descendant of this block has break-before - forcedBreakAfterLast = (BreakElement) childrenElements.get(0); - context.clearPendingMarks(); - } - - if (contentList.isEmpty()) { - // Empty fo:block, zero-length box makes sure the IDs and/or markers - // are registered and borders/padding are painted. - elements.add(makeAuxiliaryZeroWidthBox()); - } - // a descendant of this block has break-before - contentList.addAll(childrenElements); - - wrapPositionElements(contentList, elements); - - return elements; - } else { - contentList.addAll(childrenElements); - if (ElementListUtils.endsWithForcedBreak(childrenElements)) { - // a descendant of this block has break-after - if (currentChildLM.isFinished() && !hasNextChildLM()) { - forcedBreakAfterLast = (BreakElement) ListUtil.removeLast(contentList); - context.clearPendingMarks(); - } - - wrapPositionElements(contentList, elements); - - return elements; - } - } - context.updateKeepWithNextPending(childLC.getKeepWithNextPending()); - } - while ((currentChildLM = getChildLM()) != null) { - currentChildLM.reset(); // TODO won't work with forced breaks + if (lmStack != null) { + currentChildLM.reset(); // TODO won't work with forced breaks + } childLC = new LayoutContext(0); - childrenElements = getNextChildElements(currentChildLM, context, childLC, + childElements = getNextChildElements(currentChildLM, context, childLC, alignment); if (contentList.isEmpty()) { //Propagate keep-with-previous up from the first child context.updateKeepWithPreviousPending(childLC.getKeepWithPreviousPending()); } - if (childrenElements != null && !childrenElements.isEmpty()) { + if (childElements != null && !childElements.isEmpty()) { if (!contentList.isEmpty() - && !ElementListUtils.startsWithForcedBreak(childrenElements)) { + && !ElementListUtils.startsWithForcedBreak(childElements)) { // there is a block handled by prevLM before the one // handled by curLM, and the one handled // by the current LM does not begin with a break addInBetweenBreak(contentList, context, childLC); } - if (childrenElements.size() == 1 - && ElementListUtils.startsWithForcedBreak(childrenElements)) { + if (childElements.size() == 1 + && ElementListUtils.startsWithForcedBreak(childElements)) { if (currentChildLM.isFinished() && !hasNextChildLM()) { // a descendant of this block has break-before - forcedBreakAfterLast = (BreakElement) childrenElements.get(0); + forcedBreakAfterLast = (BreakElement) childElements.get(0); context.clearPendingMarks(); break; } @@ -490,14 +379,14 @@ public abstract class BlockStackingLayoutManager extends AbstractLayoutManager elements.add(makeAuxiliaryZeroWidthBox()); } // a descendant of this block has break-before - contentList.addAll(childrenElements); + contentList.addAll(childElements); wrapPositionElements(contentList, elements); return elements; } else { - contentList.addAll(childrenElements); - if (ElementListUtils.endsWithForcedBreak(childrenElements)) { + contentList.addAll(childElements); + if (ElementListUtils.endsWithForcedBreak(childElements)) { // a descendant of this block has break-after if (currentChildLM.isFinished() && !hasNextChildLM()) { forcedBreakAfterLast = (BreakElement) ListUtil.removeLast(contentList); @@ -780,8 +669,7 @@ public abstract class BlockStackingLayoutManager extends AbstractLayoutManager } // create the iterator - List workList = oldList; - ListIterator workListIterator = workList.listIterator(); + ListIterator workListIterator = oldList.listIterator(); while (workListIterator.hasNext()) { currElement = workListIterator.next(); if (prevElement != null @@ -793,8 +681,10 @@ public abstract class BlockStackingLayoutManager extends AbstractLayoutManager = (BlockLevelLayoutManager)currElement.getLayoutManager(); boolean somethingAdded = false; if (prevLM != this) { - returnedList.addAll(prevLM.getChangedKnuthElements(workList.subList( - fromIndex, workListIterator.previousIndex()), alignment)); + returnedList.addAll( + prevLM.getChangedKnuthElements( + oldList.subList(fromIndex, workListIterator.previousIndex()), + alignment)); somethingAdded = true; } else { // do nothing @@ -824,7 +714,7 @@ public abstract class BlockStackingLayoutManager extends AbstractLayoutManager LayoutManager currLM = currElement.getLayoutManager(); if (currLM != this) { returnedList.addAll(currLM.getChangedKnuthElements( - workList.subList(fromIndex, workList.size()), alignment)); + oldList.subList(fromIndex, oldList.size()), alignment)); } else { // there are no more elements to add // remove the last penalty added to returnedList -- 2.39.5