You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

BalancingColumnBreakingAlgorithm.java 3.8KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. /*
  2. * Copyright 2005 The Apache Software Foundation.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. /* $Id$ */
  17. package org.apache.fop.layoutmgr;
  18. import org.apache.commons.logging.Log;
  19. import org.apache.commons.logging.LogFactory;
  20. import org.apache.fop.traits.MinOptMax;
  21. /**
  22. * This is a the breaking algorithm that is responsible for balancing columns in multi-column
  23. * layout.
  24. */
  25. public class BalancingColumnBreakingAlgorithm extends PageBreakingAlgorithm {
  26. private Log log = LogFactory.getLog(BalancingColumnBreakingAlgorithm.class);
  27. private int columnCount;
  28. private int fullLen;
  29. private int idealPartLen;
  30. public BalancingColumnBreakingAlgorithm(LayoutManager topLevelLM,
  31. PageSequenceLayoutManager.PageViewportProvider pvProvider,
  32. int alignment, int alignmentLast,
  33. MinOptMax footnoteSeparatorLength,
  34. boolean partOverflowRecovery,
  35. int columnCount) {
  36. super(topLevelLM, pvProvider, alignment, alignmentLast,
  37. footnoteSeparatorLength, partOverflowRecovery);
  38. this.columnCount = columnCount;
  39. this.considerTooShort = true; //This is important!
  40. }
  41. /** @see org.apache.fop.layoutmgr.BreakingAlgorithm */
  42. protected double computeDemerits(KnuthNode activeNode,
  43. KnuthElement element, int fitnessClass, double r) {
  44. double dem = super.computeDemerits(activeNode, element, fitnessClass, r);
  45. if (log.isTraceEnabled()) {
  46. log.trace("original demerit=" + dem + " " + totalWidth
  47. + " line=" + activeNode.line + "/" + columnCount
  48. + " pos=" + activeNode.position + "/" + (par.size() - 1));
  49. }
  50. int remParts = columnCount - activeNode.line;
  51. int curPos = par.indexOf(element);
  52. if (fullLen == 0) {
  53. fullLen = ElementListUtils.calcContentLength(par, activeNode.position, par.size() - 1);
  54. this.idealPartLen = (fullLen / columnCount);
  55. }
  56. int partLen = ElementListUtils.calcContentLength(par, activeNode.position, curPos - 1);
  57. int restLen = ElementListUtils.calcContentLength(par, curPos - 1, par.size() - 1);
  58. int avgRestLen = 0;
  59. if (remParts > 0) {
  60. avgRestLen = restLen / remParts;
  61. }
  62. if (log.isTraceEnabled()) {
  63. log.trace("remaining parts: " + remParts + " rest len: " + restLen
  64. + " avg=" + avgRestLen);
  65. }
  66. double balance = (idealPartLen - partLen) / 1000f;
  67. if (log.isTraceEnabled()) {
  68. log.trace("balance=" + balance);
  69. }
  70. double absBalance = Math.abs(balance);
  71. //Step 1: This does the rough balancing
  72. if (balance <= 0) {
  73. dem = absBalance;
  74. } else {
  75. //shorter parts are less desired than longer ones
  76. dem = absBalance * 1.2f;
  77. }
  78. //Step 2: This helps keep the trailing parts shorter than the previous ones
  79. dem += (avgRestLen) / 1000f;
  80. if (activeNode.line >= columnCount) {
  81. //We don't want more columns than available
  82. dem = Double.MAX_VALUE;
  83. }
  84. if (log.isTraceEnabled()) {
  85. log.trace("effective dem=" + dem + " " + totalWidth);
  86. }
  87. return dem;
  88. }
  89. }