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 5.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. /*
  2. * Licensed to the Apache Software Foundation (ASF) under one or more
  3. * contributor license agreements. See the NOTICE file distributed with
  4. * this work for additional information regarding copyright ownership.
  5. * The ASF licenses this file to You under the Apache License, Version 2.0
  6. * (the "License"); you may not use this file except in compliance with
  7. * the License. You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. /* $Id$ */
  18. package org.apache.fop.layoutmgr;
  19. import org.apache.commons.logging.Log;
  20. import org.apache.commons.logging.LogFactory;
  21. import org.apache.fop.fo.Constants;
  22. import org.apache.fop.traits.MinOptMax;
  23. /**
  24. * This is a the breaking algorithm that is responsible for balancing columns in multi-column
  25. * layout.
  26. */
  27. public class BalancingColumnBreakingAlgorithm extends PageBreakingAlgorithm {
  28. private static final Log LOG = LogFactory.getLog(BalancingColumnBreakingAlgorithm.class);
  29. private int columnCount;
  30. private int fullLen;
  31. private int idealPartLen;
  32. /**
  33. * Construct a balancing column breaking algorithm.
  34. * @param topLevelLM the top level layout manager
  35. * @param pageProvider the page provider
  36. * @param layoutListener the layout listener
  37. * @param alignment alignment of the paragraph/page. One of {@link Constants#EN_START},
  38. * {@link Constants#EN_JUSTIFY}, {@link Constants#EN_CENTER},
  39. * {@link Constants#EN_END}.
  40. * For pages, {@link Constants#EN_BEFORE} and {@link Constants#EN_AFTER}
  41. * are mapped to the corresponding inline properties,
  42. * {@link Constants#EN_START} and {@link Constants#EN_END}.
  43. * @param alignmentLast alignment of the paragraph's last line
  44. * @param footnoteSeparatorLength length of footnote separator
  45. * @param partOverflowRecovery {@code true} if too long elements should be moved to
  46. * the next line/part
  47. * @param columnCount number of columns
  48. * @see PageBreakingAlgorithm
  49. */
  50. public BalancingColumnBreakingAlgorithm( // CSOK: ParameterNumber
  51. LayoutManager topLevelLM,
  52. PageProvider pageProvider,
  53. PageBreakingLayoutListener layoutListener,
  54. int alignment, int alignmentLast,
  55. MinOptMax footnoteSeparatorLength,
  56. boolean partOverflowRecovery,
  57. int columnCount) {
  58. super(topLevelLM, pageProvider, layoutListener,
  59. alignment, alignmentLast,
  60. footnoteSeparatorLength, partOverflowRecovery, false, false);
  61. this.columnCount = columnCount;
  62. this.considerTooShort = true; //This is important!
  63. }
  64. /** {@inheritDoc} */
  65. protected double computeDemerits(KnuthNode activeNode,
  66. KnuthElement element, int fitnessClass, double r) {
  67. double dem = super.computeDemerits(activeNode, element, fitnessClass, r);
  68. if (LOG.isTraceEnabled()) {
  69. LOG.trace("original demerit=" + dem + " " + totalWidth
  70. + " line=" + activeNode.line + "/" + columnCount
  71. + " pos=" + activeNode.position + "/" + (par.size() - 1));
  72. }
  73. int remParts = columnCount - activeNode.line;
  74. int curPos = par.indexOf(element);
  75. if (fullLen == 0) {
  76. fullLen = ElementListUtils.calcContentLength(par, activeNode.position, par.size() - 1);
  77. this.idealPartLen = (fullLen / columnCount);
  78. }
  79. int partLen = ElementListUtils.calcContentLength(par, activeNode.position, curPos - 1);
  80. int restLen = ElementListUtils.calcContentLength(par, curPos - 1, par.size() - 1);
  81. int avgRestLen = 0;
  82. if (remParts > 0) {
  83. avgRestLen = restLen / remParts;
  84. }
  85. if (LOG.isTraceEnabled()) {
  86. LOG.trace("remaining parts: " + remParts + " rest len: " + restLen
  87. + " avg=" + avgRestLen);
  88. }
  89. double balance = (idealPartLen - partLen) / 1000f;
  90. if (LOG.isTraceEnabled()) {
  91. LOG.trace("balance=" + balance);
  92. }
  93. double absBalance = Math.abs(balance);
  94. dem = absBalance;
  95. //Step 1: This does the rough balancing
  96. if (columnCount > 2) {
  97. if (balance > 0) {
  98. //shorter parts are less desired than longer ones
  99. dem = dem * 1.2f;
  100. }
  101. } else {
  102. if (balance < 0) {
  103. //shorter parts are less desired than longer ones
  104. dem = dem * 1.2f;
  105. }
  106. }
  107. //Step 2: This helps keep the trailing parts shorter than the previous ones
  108. dem += (avgRestLen) / 1000f;
  109. if (activeNode.line >= columnCount) {
  110. //We don't want more columns than available
  111. dem = Double.MAX_VALUE;
  112. }
  113. if (LOG.isTraceEnabled()) {
  114. LOG.trace("effective dem=" + dem + " " + totalWidth);
  115. }
  116. return dem;
  117. }
  118. }