]> source.dussan.org Git - xmlgraphics-fop.git/commitdiff
Column balancing seems to work now.
authorJeremias Maerki <jeremias@apache.org>
Fri, 8 Jul 2005 12:50:49 +0000 (12:50 +0000)
committerJeremias Maerki <jeremias@apache.org>
Fri, 8 Jul 2005 12:50:49 +0000 (12:50 +0000)
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

src/java/org/apache/fop/layoutmgr/BalancingColumnBreakingAlgorithm.java
src/java/org/apache/fop/layoutmgr/BreakingAlgorithm.java

index d6a154db147791288887a0eb2f6824810b9b2661..3a3c343c67d62429be8cb5a8bdcfc9a269729fed 100644 (file)
@@ -38,33 +38,55 @@ public class BalancingColumnBreakingAlgorithm extends PageBreakingAlgorithm {
         super(topLevelLM, pvProvider, alignment, alignmentLast, \r
                 fnSeparatorLength, partOverflowRecovery);\r
         this.columnCount = columnCount;\r
+        this.considerTooShort = true; //This is important!\r
     }\r
     \r
     /** @see org.apache.fop.layoutmgr.BreakingAlgorithm */\r
     protected double computeDemerits(KnuthNode activeNode,\r
             KnuthElement element, int fitnessClass, double r) {\r
         double dem = super.computeDemerits(activeNode, element, fitnessClass, r);\r
-        log.trace("original demerit=" + dem + " " + totalWidth);\r
+        if (log.isTraceEnabled()) {\r
+            log.trace("original demerit=" + dem + " " + totalWidth \r
+                    + " line=" + activeNode.line);\r
+        }\r
+        int remParts = columnCount - activeNode.line;\r
         int curPos = par.indexOf(element);\r
         if (fullLen == 0) {\r
             fullLen = ElementListUtils.calcContentLength(par);\r
         }\r
         int partLen = ElementListUtils.calcContentLength(par, activeNode.position, curPos - 1);\r
+        int restLen = ElementListUtils.calcContentLength(par, curPos - 1, par.size() - 1);\r
+        int avgRestLen = 0;\r
+        if (remParts > 0) {\r
+            avgRestLen = restLen / remParts;\r
+        }\r
+        if (log.isTraceEnabled()) {\r
+            log.trace("remaining parts: " + remParts + " rest len: " + restLen \r
+                    + " avg=" + avgRestLen);\r
+        }\r
         int meanColumnLen = (fullLen / columnCount);\r
         double balance = (meanColumnLen - partLen) / 1000f;\r
-        log.trace("balance=" + balance);\r
+        if (log.isTraceEnabled()) {\r
+            log.trace("balance=" + balance);\r
+        }\r
         double absBalance = Math.abs(balance);\r
+        //Step 1: This does the rough balancing\r
         if (balance <= 0) {\r
-            dem = absBalance * absBalance;\r
+            dem = absBalance;\r
         } else {\r
             //shorter parts are less desired than longer ones\r
-            dem = absBalance * absBalance * 2;\r
+            dem = absBalance * 1.2f;\r
         }\r
+        //Step 2: This helps keep the trailing parts shorter than the previous ones \r
+        dem += (avgRestLen) / 1000f;\r
+        \r
         if (activeNode.line >= columnCount) {\r
             //We don't want more columns than available\r
             dem = Double.MAX_VALUE;\r
         }\r
-        log.trace("effective dem=" + dem + " " + totalWidth);\r
+        if (log.isTraceEnabled()) {\r
+            log.trace("effective dem=" + dem + " " + totalWidth);\r
+        }\r
         return dem;\r
     }\r
 }\r
index 0128eccbc74dce0d477ef0774db521ccba0700ea..98ba692b337ea1c404534f0a39024cae581c23f0 100644 (file)
@@ -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,