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.

KnuthPenalty.java 5.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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.fop.fo.Constants;
  20. /**
  21. * An instance of this class represents information about a feasible
  22. * breaking point; it does not represent any piece of content.
  23. *
  24. * A KnuthPenalty is a feasible breaking point unless its value is infinity;
  25. * a KnuthPenalty whose value is -infinity represents a forced break.
  26. *
  27. * A KnuthPenalty is suppressed, and its width is ignored, if it is not a
  28. * chosen breaking point; for example, a KnuthPenalty representing a
  29. * hyphenation point has a width (the "-" width), which must be ignored if
  30. * that point is not chosen as a breaking point.
  31. *
  32. * Besides the inherited methods and attributes, this class has two more
  33. * attributes and the methods used to get them: the penalty value, which is
  34. * a kind of "aesthetic cost" (the higher the value, the more unsightly the
  35. * breaking point), and a boolean that marks KnuthPenalties which should not
  36. * be chosen as breaking points for consecutive lines.
  37. */
  38. public class KnuthPenalty extends KnuthElement {
  39. /** Used for flagged penalties. See Knuth algorithm. */
  40. public static final int FLAGGED_PENALTY = 50;
  41. private int penalty;
  42. private boolean isFlagged;
  43. private int breakClass = -1;
  44. /**
  45. * Create a new KnuthPenalty.
  46. *
  47. * @param w the width of this penalty
  48. * @param p the penalty value of this penalty
  49. * @param f is this penalty flagged?
  50. * @param pos the Position stored in this penalty
  51. * @param isAuxiliary is this penalty auxiliary?
  52. */
  53. public KnuthPenalty(int w, int p, boolean f, Position pos, boolean isAuxiliary) {
  54. super(w, pos, isAuxiliary);
  55. penalty = p;
  56. isFlagged = f;
  57. }
  58. /**
  59. * Create a new KnuthPenalty.
  60. *
  61. * @param w the width of this penalty
  62. * @param p the penalty value of this penalty
  63. * @param f is this penalty flagged?
  64. * @param breakClass the break class of this penalty (one of
  65. * {@link Constants#EN_AUTO}, {@link Constants#EN_COLUMN}, {@link Constants#EN_PAGE},
  66. * {@link Constants#EN_EVEN_PAGE}, {@link Constants#EN_ODD_PAGE})
  67. * @param pos the Position stored in this penalty
  68. * @param isAuxiliary is this penalty auxiliary?
  69. */
  70. public KnuthPenalty(int w, int p, boolean f,
  71. int breakClass, Position pos, boolean isAuxiliary) {
  72. this(w, p, f, pos, isAuxiliary);
  73. this.breakClass = breakClass;
  74. }
  75. private static String getBreakClassName(int breakClass) {
  76. return AbstractBreaker.getBreakClassName(breakClass);
  77. }
  78. /**
  79. * Get the penalty's value as a {@code java.lang.String}.
  80. * (Mainly used in {@code toString()} methods, to improve readability
  81. * of the trace logs.)
  82. *
  83. * @param penaltyValue the penalty value
  84. * @return the penalty value as a {@code java.lang.String}
  85. */
  86. protected static String valueOf(int penaltyValue) {
  87. String result = (penaltyValue < 0) ? "-" : "";
  88. int tmpValue = Math.abs(penaltyValue);
  89. result += (tmpValue == KnuthElement.INFINITE)
  90. ? "INFINITE"
  91. : String.valueOf(tmpValue);
  92. return result;
  93. }
  94. /** {@inheritDoc} */
  95. public boolean isPenalty() {
  96. return true;
  97. }
  98. /**
  99. * @return the penalty value of this penalty.
  100. */
  101. public int getP() {
  102. return penalty;
  103. }
  104. /**
  105. * Sets a new penalty value.
  106. * @param p the new penalty value
  107. */
  108. public void setP(int p) {
  109. this.penalty = p;
  110. }
  111. /** @return true is this penalty is a flagged one. */
  112. public boolean isFlagged() {
  113. return isFlagged;
  114. }
  115. /** {@inheritDoc} */
  116. public boolean isForcedBreak() {
  117. return penalty == -KnuthElement.INFINITE;
  118. }
  119. /**
  120. * @return the break class of this penalty (EN_AUTO, EN_COLUMN, EN_PAGE, EN_EVEN_PAGE,
  121. * EN_ODD_PAGE)
  122. */
  123. public int getBreakClass() {
  124. return breakClass;
  125. }
  126. /** {@inheritDoc} */
  127. public String toString() {
  128. StringBuffer sb = new StringBuffer(64);
  129. if (isAuxiliary()) {
  130. sb.append("aux. ");
  131. }
  132. sb.append("penalty");
  133. sb.append(" p=");
  134. sb.append(valueOf(this.penalty));
  135. if (this.isFlagged) {
  136. sb.append(" [flagged]");
  137. }
  138. sb.append(" w=");
  139. sb.append(getW());
  140. if (isForcedBreak()) {
  141. sb.append(" (forced break, ")
  142. .append(getBreakClassName(this.breakClass))
  143. .append(")");
  144. } else if (this.penalty >= 0 && this.breakClass != -1) {
  145. //penalty corresponding to a keep constraint
  146. sb.append(" (keep constraint, ")
  147. .append(getBreakClassName(this.breakClass))
  148. .append(")");
  149. }
  150. return sb.toString();
  151. }
  152. }