--- /dev/null
+/*\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