]> source.dussan.org Git - xmlgraphics-fop.git/commitdiff
My first laughable attempt at a page breaker for balancing columns. Doesn't work...
authorJeremias Maerki <jeremias@apache.org>
Wed, 29 Jun 2005 13:14:55 +0000 (13:14 +0000)
committerJeremias Maerki <jeremias@apache.org>
Wed, 29 Jun 2005 13:14:55 +0000 (13:14 +0000)
But it's better than nothing to start with.

git-svn-id: https://svn.apache.org/repos/asf/xmlgraphics/fop/trunk@202367 13f79535-47bb-0310-9956-ffa450edef68

src/java/org/apache/fop/layoutmgr/BalancingColumnBreakingAlgorithm.java [new file with mode: 0644]

diff --git a/src/java/org/apache/fop/layoutmgr/BalancingColumnBreakingAlgorithm.java b/src/java/org/apache/fop/layoutmgr/BalancingColumnBreakingAlgorithm.java
new file mode 100644 (file)
index 0000000..d6a154d
--- /dev/null
@@ -0,0 +1,70 @@
+/*\r
+ * Copyright 2005 The Apache Software Foundation.\r
+ *\r
+ * Licensed under the Apache License, Version 2.0 (the "License");\r
+ * you may not use this file except in compliance with the License.\r
+ * You may obtain a copy of the License at\r
+ *\r
+ *      http://www.apache.org/licenses/LICENSE-2.0\r
+ *\r
+ * Unless required by applicable law or agreed to in writing, software\r
+ * distributed under the License is distributed on an "AS IS" BASIS,\r
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
+ * See the License for the specific language governing permissions and\r
+ * limitations under the License.\r
+ */\r
+\r
+/* $Id$ */\r
+\r
+package org.apache.fop.layoutmgr;\r
+\r
+import org.apache.fop.traits.MinOptMax;\r
+\r
+/**\r
+ * This is a the breaking algorithm that is responsible for balancing columns in multi-column\r
+ * layout.\r
+ */\r
+public class BalancingColumnBreakingAlgorithm extends PageBreakingAlgorithm {\r
+\r
+    private int columnCount;\r
+    private int fullLen;\r
+    \r
+    public BalancingColumnBreakingAlgorithm(LayoutManager topLevelLM,\r
+            PageSequenceLayoutManager.PageViewportProvider pvProvider,\r
+            int alignment, int alignmentLast,\r
+            MinOptMax fnSeparatorLength,\r
+            boolean partOverflowRecovery,\r
+            int columnCount) {\r
+        super(topLevelLM, pvProvider, alignment, alignmentLast, \r
+                fnSeparatorLength, partOverflowRecovery);\r
+        this.columnCount = columnCount;\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
+        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 meanColumnLen = (fullLen / columnCount);\r
+        double balance = (meanColumnLen - partLen) / 1000f;\r
+        log.trace("balance=" + balance);\r
+        double absBalance = Math.abs(balance);\r
+        if (balance <= 0) {\r
+            dem = absBalance * absBalance;\r
+        } else {\r
+            //shorter parts are less desired than longer ones\r
+            dem = absBalance * absBalance * 2;\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
+        return dem;\r
+    }\r
+}\r