1 17 18 19 20 package org.apache.fop.layoutmgr; 21 22 import org.apache.commons.logging.Log; 23 import org.apache.commons.logging.LogFactory; 24 import org.apache.fop.layoutmgr.PageBreakingAlgorithm.PageBreakingLayoutListener; 25 import org.apache.fop.traits.MinOptMax; 26 27 31 public class BalancingColumnBreakingAlgorithm extends PageBreakingAlgorithm { 32 33 private Log log = LogFactory.getLog(BalancingColumnBreakingAlgorithm.class); 34 35 private int columnCount; 36 private int fullLen; 37 private int idealPartLen; 38 39 public BalancingColumnBreakingAlgorithm(LayoutManager topLevelLM, 40 PageSequenceLayoutManager.PageProvider pageProvider, 41 PageBreakingLayoutListener layoutListener, 42 int alignment, int alignmentLast, 43 MinOptMax footnoteSeparatorLength, 44 boolean partOverflowRecovery, 45 int columnCount) { 46 super(topLevelLM, pageProvider, layoutListener, 47 alignment, alignmentLast, 48 footnoteSeparatorLength, partOverflowRecovery, false, false); 49 this.columnCount = columnCount; 50 this.considerTooShort = true; } 52 53 54 protected double computeDemerits(KnuthNode activeNode, 55 KnuthElement element, int fitnessClass, double r) { 56 double dem = super.computeDemerits(activeNode, element, fitnessClass, r); 57 if (log.isTraceEnabled()) { 58 log.trace("original demerit=" + dem + " " + totalWidth 59 + " line=" + activeNode.line + "/" + columnCount 60 + " pos=" + activeNode.position + "/" + (par.size() - 1)); 61 } 62 int remParts = columnCount - activeNode.line; 63 int curPos = par.indexOf(element); 64 if (fullLen == 0) { 65 fullLen = ElementListUtils.calcContentLength(par, activeNode.position, par.size() - 1); 66 this.idealPartLen = (fullLen / columnCount); 67 } 68 int partLen = ElementListUtils.calcContentLength(par, activeNode.position, curPos - 1); 69 int restLen = ElementListUtils.calcContentLength(par, curPos - 1, par.size() - 1); 70 int avgRestLen = 0; 71 if (remParts > 0) { 72 avgRestLen = restLen / remParts; 73 } 74 if (log.isTraceEnabled()) { 75 log.trace("remaining parts: " + remParts + " rest len: " + restLen 76 + " avg=" + avgRestLen); 77 } 78 double balance = (idealPartLen - partLen) / 1000f; 79 if (log.isTraceEnabled()) { 80 log.trace("balance=" + balance); 81 } 82 double absBalance = Math.abs(balance); 83 dem = absBalance; 84 if (columnCount > 2) { 86 if (balance > 0) { 87 dem = dem * 1.2f; 89 } 90 } else { 91 if (balance < 0) { 92 dem = dem * 1.2f; 94 } 95 } 96 dem += (avgRestLen) / 1000f; 98 99 if (activeNode.line >= columnCount) { 100 dem = Double.MAX_VALUE; 102 } 103 if (log.isTraceEnabled()) { 104 log.trace("effective dem=" + dem + " " + totalWidth); 105 } 106 return dem; 107 } 108 } 109 | Popular Tags |