From 4ed47c3dd4e24ed8654695655493ac4283ed716b Mon Sep 17 00:00:00 2001 From: "Andreas L. Delmelle" Date: Sat, 19 Jun 2010 19:10:10 +0000 Subject: [PATCH] Minor refactoring and style fixups: - private lineLayoutsList can be a primitive array - eliminate some stray hungarians (inconsistent btw: iFirstIndex, but nEliminableLines...) - inverted principal loop in createLineBreaks(), to make debugging less confusing (and preparation for partial fix of Bug 38264) - compacted some boolean expressions ( (x ? true : false) -> plain x ) - removed some commented code - extracted some code into processUpdates; findHyphenationPoints was getting too long - minor javadoc fixups git-svn-id: https://svn.apache.org/repos/asf/xmlgraphics/fop/trunk@956271 13f79535-47bb-0310-9956-ffa450edef68 --- .../layoutmgr/inline/LineLayoutManager.java | 223 ++++++++---------- 1 file changed, 102 insertions(+), 121 deletions(-) diff --git a/src/java/org/apache/fop/layoutmgr/inline/LineLayoutManager.java b/src/java/org/apache/fop/layoutmgr/inline/LineLayoutManager.java index 0ece70cac..9e17a0368 100644 --- a/src/java/org/apache/fop/layoutmgr/inline/LineLayoutManager.java +++ b/src/java/org/apache/fop/layoutmgr/inline/LineLayoutManager.java @@ -174,7 +174,7 @@ public class LineLayoutManager extends InlineStackingLayoutManager private List knuthParagraphs; private LineLayoutPossibilities lineLayouts; - private List lineLayoutsList; + private LineLayoutPossibilities[] lineLayoutsList; private int ipd = 0; /** * When layout must be re-started due to a change of IPD, there is no need @@ -198,11 +198,11 @@ public class LineLayoutManager extends InlineStackingLayoutManager */ private class Update { private InlineLevelLayoutManager inlineLM; - private int iFirstIndex; + private int firstIndex; public Update(InlineLevelLayoutManager lm, int index) { inlineLM = lm; - iFirstIndex = index; + firstIndex = index; } } @@ -750,11 +750,11 @@ public class LineLayoutManager extends InlineStackingLayoutManager */ private List createLineBreaks(int alignment, LayoutContext context) { // find the optimal line breaking points for each paragraph - ListIterator paragraphsIterator = knuthParagraphs.listIterator(knuthParagraphs.size()); - lineLayoutsList = new ArrayList(knuthParagraphs.size()); + Iterator paragraphsIterator = knuthParagraphs.iterator(); + lineLayoutsList = new LineLayoutPossibilities[knuthParagraphs.size()]; LineLayoutPossibilities llPoss; - while (paragraphsIterator.hasPrevious()) { - KnuthSequence seq = (KnuthSequence) paragraphsIterator.previous(); + for (int i = 0; paragraphsIterator.hasNext(); i++) { + KnuthSequence seq = (KnuthSequence) paragraphsIterator.next(); if (!seq.isInlineSequence()) { // This set of line layout possibilities does not matter; // we only need an entry in lineLayoutsList. @@ -762,7 +762,7 @@ public class LineLayoutManager extends InlineStackingLayoutManager } else { llPoss = findOptimalBreakingPoints(alignment, (Paragraph) seq); } - lineLayoutsList.add(0, llPoss); + lineLayoutsList[i] = llPoss; } setFinished(true); @@ -910,8 +910,7 @@ public class LineLayoutManager extends InlineStackingLayoutManager context)); } - LineLayoutPossibilities llPoss; - llPoss = (LineLayoutPossibilities) lineLayoutsList.get(p); + LineLayoutPossibilities llPoss = lineLayoutsList[p]; KnuthSequence seq = (KnuthSequence) knuthParagraphs.get(p); if (!seq.isInlineSequence()) { @@ -987,137 +986,135 @@ public class LineLayoutManager extends InlineStackingLayoutManager return returnList; } - private void createElements(List list, LineLayoutPossibilities llPoss, Position elementPosition) { /* number of normal, inner lines */ - int nInnerLines = 0; + int innerLines = 0; /* number of lines that can be used in order to fill more space */ - int nOptionalLines = 0; + int optionalLines = 0; /* number of lines that can be used in order to fill more space only if the paragraph is not parted */ - int nConditionalOptionalLines = 0; + int conditionalOptionalLines = 0; /* number of lines that can be omitted in order to fill less space */ - int nEliminableLines = 0; + int eliminableLines = 0; /* number of lines that can be omitted in order to fill less space only if the paragraph is not parted */ - int nConditionalEliminableLines = 0; + int conditionalEliminableLines = 0; /* number of the first unbreakable lines */ - int nFirstLines = fobj.getOrphans(); + int firstLines = fobj.getOrphans(); /* number of the last unbreakable lines */ - int nLastLines = fobj.getWidows(); + int lastLines = fobj.getWidows(); /* sub-sequence used to separate the elements representing different lines */ List breaker = new LinkedList(); /* comment out the next lines in order to test particular situations */ if (fobj.getOrphans() + fobj.getWidows() <= llPoss.getMinLineCount()) { - nInnerLines = llPoss.getMinLineCount() + innerLines = llPoss.getMinLineCount() - (fobj.getOrphans() + fobj.getWidows()); - nOptionalLines = llPoss.getMaxLineCount() + optionalLines = llPoss.getMaxLineCount() - llPoss.getOptLineCount(); - nEliminableLines = llPoss.getOptLineCount() + eliminableLines = llPoss.getOptLineCount() - llPoss.getMinLineCount(); } else if (fobj.getOrphans() + fobj.getWidows() <= llPoss.getOptLineCount()) { - nOptionalLines = llPoss.getMaxLineCount() + optionalLines = llPoss.getMaxLineCount() - llPoss.getOptLineCount(); - nEliminableLines = llPoss.getOptLineCount() + eliminableLines = llPoss.getOptLineCount() - (fobj.getOrphans() + fobj.getWidows()); - nConditionalEliminableLines = (fobj.getOrphans() + fobj.getWidows()) + conditionalEliminableLines = (fobj.getOrphans() + fobj.getWidows()) - llPoss.getMinLineCount(); } else if (fobj.getOrphans() + fobj.getWidows() <= llPoss.getMaxLineCount()) { - nOptionalLines = llPoss.getMaxLineCount() + optionalLines = llPoss.getMaxLineCount() - (fobj.getOrphans() + fobj.getWidows()); - nConditionalOptionalLines = (fobj.getOrphans() + fobj.getWidows()) + conditionalOptionalLines = (fobj.getOrphans() + fobj.getWidows()) - llPoss.getOptLineCount(); - nConditionalEliminableLines = llPoss.getOptLineCount() + conditionalEliminableLines = llPoss.getOptLineCount() - llPoss.getMinLineCount(); - nFirstLines -= nConditionalOptionalLines; + firstLines -= conditionalOptionalLines; } else { - nConditionalOptionalLines = llPoss.getMaxLineCount() + conditionalOptionalLines = llPoss.getMaxLineCount() - llPoss.getOptLineCount(); - nConditionalEliminableLines = llPoss.getOptLineCount() + conditionalEliminableLines = llPoss.getOptLineCount() - llPoss.getMinLineCount(); - nFirstLines = llPoss.getOptLineCount(); - nLastLines = 0; + firstLines = llPoss.getOptLineCount(); + lastLines = 0; } /* comment out the previous lines in order to test particular situations */ /* use these lines to test particular situations - nInnerLines = 0; - nOptionalLines = 1; - nConditionalOptionalLines = 2; - nEliminableLines = 0; - nConditionalEliminableLines = 0; - nFirstLines = 1; - nLastLines = 3; + innerLines = 0; + optionalLines = 1; + conditionalOptionalLines = 2; + eliminableLines = 0; + conditionalEliminableLines = 0; + firstLines = 1; + lastLines = 3; */ - if (nLastLines != 0 - && (nConditionalOptionalLines > 0 || nConditionalEliminableLines > 0)) { + if (lastLines != 0 + && (conditionalOptionalLines > 0 || conditionalEliminableLines > 0)) { breaker.add(new KnuthPenalty(0, KnuthElement.INFINITE, false, elementPosition, false)); - breaker.add(new KnuthGlue(0, -nConditionalOptionalLines * constantLineHeight, - -nConditionalEliminableLines * constantLineHeight, + breaker.add(new KnuthGlue(0, -conditionalOptionalLines * constantLineHeight, + -conditionalEliminableLines * constantLineHeight, Adjustment.LINE_NUMBER_ADJUSTMENT, elementPosition, false)); - breaker.add(new KnuthPenalty(nConditionalOptionalLines * constantLineHeight, + breaker.add(new KnuthPenalty(conditionalOptionalLines * constantLineHeight, 0, false, elementPosition, false)); - breaker.add(new KnuthGlue(0, nConditionalOptionalLines * constantLineHeight, - nConditionalEliminableLines * constantLineHeight, + breaker.add(new KnuthGlue(0, conditionalOptionalLines * constantLineHeight, + conditionalEliminableLines * constantLineHeight, Adjustment.LINE_NUMBER_ADJUSTMENT, elementPosition, false)); - } else if (nLastLines != 0) { + } else if (lastLines != 0) { breaker.add(new KnuthPenalty(0, 0, false, elementPosition, false)); } - //log.debug("first=" + nFirstLines + " inner=" + nInnerLines - // + " optional=" + nOptionalLines + " eliminable=" + nEliminableLines - // + " last=" + nLastLines - // + " (condOpt=" + nConditionalOptionalLines + " condEl=" + nConditionalEliminableLines + ")"); + //log.debug("first=" + firstLines + " inner=" + innerLines + // + " optional=" + optionalLines + " eliminable=" + eliminableLines + // + " last=" + lastLines + // + " (condOpt=" + conditionalOptionalLines + " condEl=" + conditionalEliminableLines + ")"); // creation of the elements: // first group of lines - list.add(new KnuthBox(nFirstLines * constantLineHeight, elementPosition, - (nLastLines == 0 - && nConditionalOptionalLines == 0 - && nConditionalEliminableLines == 0 ? true : false))); - if (nConditionalOptionalLines > 0 - || nConditionalEliminableLines > 0) { + list.add(new KnuthBox(firstLines * constantLineHeight, elementPosition, + (lastLines == 0 + && conditionalOptionalLines == 0 + && conditionalEliminableLines == 0))); + if (conditionalOptionalLines > 0 + || conditionalEliminableLines > 0) { list.add(new KnuthPenalty(0, KnuthElement.INFINITE, false, elementPosition, false)); - list.add(new KnuthGlue(0, nConditionalOptionalLines * constantLineHeight, - nConditionalEliminableLines * constantLineHeight, + list.add(new KnuthGlue(0, conditionalOptionalLines * constantLineHeight, + conditionalEliminableLines * constantLineHeight, Adjustment.LINE_NUMBER_ADJUSTMENT, elementPosition, false)); - list.add(new KnuthBox(0, elementPosition, - (nLastLines == 0 ? true : false))); + list.add(new KnuthBox(0, elementPosition, (lastLines == 0))); } // optional lines - for (int i = 0; i < nOptionalLines; i++) { + for (int i = 0; i < optionalLines; i++) { list.addAll(breaker); list.add(new KnuthBox(0, elementPosition, false)); list.add(new KnuthPenalty(0, KnuthElement.INFINITE, false, elementPosition, false)); - list.add(new KnuthGlue(0, 1 * constantLineHeight, 0, + list.add(new KnuthGlue(0, constantLineHeight, 0, Adjustment.LINE_NUMBER_ADJUSTMENT, elementPosition, false)); list.add(new KnuthBox(0, elementPosition, false)); } // eliminable lines - for (int i = 0; i < nEliminableLines; i++) { + for (int i = 0; i < eliminableLines; i++) { list.addAll(breaker); - list.add(new KnuthBox(1 * constantLineHeight, elementPosition, false)); + list.add(new KnuthBox(constantLineHeight, elementPosition, false)); list.add(new KnuthPenalty(0, KnuthElement.INFINITE, false, elementPosition, false)); - list.add(new KnuthGlue(0, 0, 1 * constantLineHeight, + list.add(new KnuthGlue(0, 0, constantLineHeight, Adjustment.LINE_NUMBER_ADJUSTMENT, elementPosition, false)); list.add(new KnuthBox(0, elementPosition, false)); } // inner lines - for (int i = 0; i < nInnerLines; i++) { + for (int i = 0; i < innerLines; i++) { list.addAll(breaker); - list.add(new KnuthBox(1 * constantLineHeight, elementPosition, false)); + list.add(new KnuthBox(constantLineHeight, elementPosition, false)); } // last group of lines - if (nLastLines > 0) { + if (lastLines > 0) { list.addAll(breaker); - list.add(new KnuthBox(nLastLines * constantLineHeight, + list.add(new KnuthBox(lastLines * constantLineHeight, elementPosition, true)); } } @@ -1170,34 +1167,28 @@ public class LineLayoutManager extends InlineStackingLayoutManager /** {@inheritDoc} */ public int negotiateBPDAdjustment(int adj, KnuthElement lastElement) { LeafPosition pos = (LeafPosition)lastElement.getPosition(); - int totalAdj = adj; //if (lastElement.isPenalty()) { // totalAdj += lastElement.getWidth(); //} //int lineNumberDifference = (int)((double) totalAdj / constantLineHeight); - int lineNumberDifference = (int) Math.round((double) totalAdj / constantLineHeight + int lineNumberDifference = (int) Math.round((double) adj / constantLineHeight + (adj > 0 ? - 0.4 : 0.4)); //log.debug(" LLM> variazione calcolata = " + ((double) totalAdj / constantLineHeight) + " variazione applicata = " + lineNumberDifference); LineLayoutPossibilities llPoss; - llPoss = (LineLayoutPossibilities) lineLayoutsList.get(pos.getLeafPos()); + llPoss = lineLayoutsList[pos.getLeafPos()]; lineNumberDifference = llPoss.applyLineCountAdjustment(lineNumberDifference); return lineNumberDifference * constantLineHeight; } - /** - * {@inheritDoc} - */ + /** {@inheritDoc} */ public void discardSpace(KnuthGlue spaceGlue) { } - /** - * {@inheritDoc} - */ + /** {@inheritDoc} */ public List getChangedKnuthElements(List oldList, int alignment) { List returnList = new LinkedList(); for (int p = 0; p < knuthParagraphs.size(); p++) { - LineLayoutPossibilities llPoss; - llPoss = (LineLayoutPossibilities)lineLayoutsList.get(p); + LineLayoutPossibilities llPoss = lineLayoutsList[p]; //log.debug("demerits of the chosen layout: " + llPoss.getChosenDemerits()); for (int i = 0; i < llPoss.getChosenLineCount(); i++) { if (!((BlockLevelLayoutManager) parentLayoutManager).mustKeepTogether() @@ -1233,24 +1224,23 @@ public class LineLayoutManager extends InlineStackingLayoutManager } /** - * find hyphenation points for every word int the current paragraph - * @ param currPar the paragraph whose words will be hyphenated + * Find hyphenation points for every word in the current paragraph. + * + * @param currPar the paragraph whose words will be hyphenated */ private void findHyphenationPoints(Paragraph currPar) { // hyphenate every word - ListIterator currParIterator - = currPar.listIterator(currPar.ignoreAtStart); + ListIterator currParIterator = currPar.listIterator(currPar.ignoreAtStart); // list of TLM involved in hyphenation List updateList = new LinkedList(); - KnuthElement firstElement = null; - KnuthElement nextElement = null; + KnuthElement firstElement, nextElement; // current InlineLevelLayoutManager InlineLevelLayoutManager currLM = null; // number of KnuthBox elements containing word fragments int boxCount; // number of auxiliary KnuthElements between KnuthBoxes int auxCount; - StringBuffer sbChars = null; + StringBuffer sbChars; // find all hyphenation points while (currParIterator.hasNext()) { @@ -1325,51 +1315,49 @@ public class LineLayoutManager extends InlineStackingLayoutManager } } } + processUpdates(currPar, updateList); + } + private void processUpdates(Paragraph par, List updateList) { // create iterator for the updateList ListIterator updateListIterator = updateList.listIterator(); - Update currUpdate = null; - //int iPreservedElements = 0; - int iAddedElements = 0; - //int iRemovedElements = 0; + Update currUpdate; + int elementsAdded = 0; while (updateListIterator.hasNext()) { // ask the LMs to apply the changes and return // the new KnuthElements to replace the old ones currUpdate = (Update) updateListIterator.next(); - int fromIndex = currUpdate.iFirstIndex; + int fromIndex = currUpdate.firstIndex; int toIndex; if (updateListIterator.hasNext()) { Update nextUpdate = (Update) updateListIterator.next(); - toIndex = nextUpdate.iFirstIndex; + toIndex = nextUpdate.firstIndex; updateListIterator.previous(); } else { // maybe this is not always correct! - toIndex = currPar.size() - currPar.ignoreAtEnd - - iAddedElements; + toIndex = par.size() - par.ignoreAtEnd + - elementsAdded; } // applyChanges() returns true if the LM modifies its data, // so it must return new KnuthElements to replace the old ones - if (((InlineLevelLayoutManager) currUpdate.inlineLM) - .applyChanges(currPar.subList(fromIndex + iAddedElements, - toIndex + iAddedElements))) { + if (currUpdate.inlineLM + .applyChanges(par.subList(fromIndex + elementsAdded, + toIndex + elementsAdded))) { // insert the new KnuthElements - List newElements = null; - newElements - = currUpdate.inlineLM.getChangedKnuthElements - (currPar.subList(fromIndex + iAddedElements, - toIndex + iAddedElements), + List newElements = currUpdate.inlineLM.getChangedKnuthElements + (par.subList(fromIndex + elementsAdded, + toIndex + elementsAdded), /*flaggedPenalty,*/ effectiveAlignment); // remove the old elements - currPar.subList(fromIndex + iAddedElements, - toIndex + iAddedElements).clear(); + par.subList(fromIndex + elementsAdded, + toIndex + elementsAdded).clear(); // insert the new elements - currPar.addAll(fromIndex + iAddedElements, newElements); - iAddedElements += newElements.size() - (toIndex - fromIndex); + par.addAll(fromIndex + elementsAdded, newElements); + elementsAdded += newElements.size() - (toIndex - fromIndex); } } - updateListIterator = null; updateList.clear(); } @@ -1456,7 +1444,7 @@ public class LineLayoutManager extends InlineStackingLayoutManager /** * Add a line with inline content * @param context the context for adding areas - * @param pos the position for which the line is generated + * @param lbp the position for which the line is generated * @param isLastPosition true if this is the last position of this LM */ private void addInlineArea(LayoutContext context, LineBreakPosition lbp, @@ -1598,8 +1586,7 @@ public class LineLayoutManager extends InlineStackingLayoutManager * which creates one line area. */ List positionList = new ArrayList(1); - Position innerPosition; - innerPosition = ((NonLeafPosition) pos).getPosition(); + Position innerPosition = pos.getPosition(); positionList.add(innerPosition); // do we have the last LM? @@ -1634,9 +1621,7 @@ public class LineLayoutManager extends InlineStackingLayoutManager parentLayoutManager.addChildArea(lineArea); } - /** - * {@inheritDoc} - */ + /** {@inheritDoc} */ public void addChildArea(Area childArea) { // Make sure childArea is inline area if (childArea instanceof InlineArea) { @@ -1651,16 +1636,12 @@ public class LineLayoutManager extends InlineStackingLayoutManager // --------- Property Resolution related functions --------- // - /** - * {@inheritDoc} - */ + /** {@inheritDoc} */ public boolean getGeneratesBlockArea() { return true; } - /** - * {@inheritDoc} - */ + /** {@inheritDoc} */ public boolean getGeneratesLineArea() { return true; } -- 2.39.5