From: Jeremias Maerki Date: Fri, 8 Jul 2005 12:50:49 +0000 (+0000) Subject: Column balancing seems to work now. X-Git-Tag: fop-0_90-alpha1~501 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=c14bf42f72bd4053ed7884a9e44463515d47b9f3;p=xmlgraphics-fop.git Column balancing seems to work now. I had to modify BreakingAlgorithm to add best records for parts which are too short. Furthermore, it took an additional computed value to tweak the balancing. It effectively makes shorter trailing parts preferred. git-svn-id: https://svn.apache.org/repos/asf/xmlgraphics/fop/trunk@209748 13f79535-47bb-0310-9956-ffa450edef68 --- diff --git a/src/java/org/apache/fop/layoutmgr/BalancingColumnBreakingAlgorithm.java b/src/java/org/apache/fop/layoutmgr/BalancingColumnBreakingAlgorithm.java index d6a154db1..3a3c343c6 100644 --- a/src/java/org/apache/fop/layoutmgr/BalancingColumnBreakingAlgorithm.java +++ b/src/java/org/apache/fop/layoutmgr/BalancingColumnBreakingAlgorithm.java @@ -38,33 +38,55 @@ public class BalancingColumnBreakingAlgorithm extends PageBreakingAlgorithm { super(topLevelLM, pvProvider, alignment, alignmentLast, fnSeparatorLength, partOverflowRecovery); this.columnCount = columnCount; + this.considerTooShort = true; //This is important! } /** @see org.apache.fop.layoutmgr.BreakingAlgorithm */ protected double computeDemerits(KnuthNode activeNode, KnuthElement element, int fitnessClass, double r) { double dem = super.computeDemerits(activeNode, element, fitnessClass, r); - log.trace("original demerit=" + dem + " " + totalWidth); + if (log.isTraceEnabled()) { + log.trace("original demerit=" + dem + " " + totalWidth + + " line=" + activeNode.line); + } + int remParts = columnCount - activeNode.line; int curPos = par.indexOf(element); if (fullLen == 0) { fullLen = ElementListUtils.calcContentLength(par); } int partLen = ElementListUtils.calcContentLength(par, activeNode.position, curPos - 1); + int restLen = ElementListUtils.calcContentLength(par, curPos - 1, par.size() - 1); + int avgRestLen = 0; + if (remParts > 0) { + avgRestLen = restLen / remParts; + } + if (log.isTraceEnabled()) { + log.trace("remaining parts: " + remParts + " rest len: " + restLen + + " avg=" + avgRestLen); + } int meanColumnLen = (fullLen / columnCount); double balance = (meanColumnLen - partLen) / 1000f; - log.trace("balance=" + balance); + if (log.isTraceEnabled()) { + log.trace("balance=" + balance); + } double absBalance = Math.abs(balance); + //Step 1: This does the rough balancing if (balance <= 0) { - dem = absBalance * absBalance; + dem = absBalance; } else { //shorter parts are less desired than longer ones - dem = absBalance * absBalance * 2; + dem = absBalance * 1.2f; } + //Step 2: This helps keep the trailing parts shorter than the previous ones + dem += (avgRestLen) / 1000f; + if (activeNode.line >= columnCount) { //We don't want more columns than available dem = Double.MAX_VALUE; } - log.trace("effective dem=" + dem + " " + totalWidth); + if (log.isTraceEnabled()) { + log.trace("effective dem=" + dem + " " + totalWidth); + } return dem; } } diff --git a/src/java/org/apache/fop/layoutmgr/BreakingAlgorithm.java b/src/java/org/apache/fop/layoutmgr/BreakingAlgorithm.java index 0128eccbc..98ba692b3 100644 --- a/src/java/org/apache/fop/layoutmgr/BreakingAlgorithm.java +++ b/src/java/org/apache/fop/layoutmgr/BreakingAlgorithm.java @@ -71,6 +71,8 @@ public abstract class BreakingAlgorithm { */ protected int lineWidth = -1; private boolean force = false; + /** If set to true, doesn't ignore break possibilities which are definitely too short. */ + protected boolean considerTooShort = false; protected KnuthNode lastDeactivatedNode = null; private KnuthNode lastTooLong; @@ -556,6 +558,12 @@ public abstract class BreakingAlgorithm { } } else { if (lastTooShort == null || demerits <= lastTooShort.totalDemerits) { + if (considerTooShort) { + //consider possibilities which are too short + best.addRecord(demerits, node, r, + availableShrink, availableStretch, + difference, fitnessClass); + } lastTooShort = createNode(elementIdx, line + 1, fitnessClass, totalWidth, totalStretch, totalShrink, r, availableShrink, availableStretch,