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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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. /** Dummy, zero-width penalty */
  42. public static final KnuthPenalty DUMMY_ZERO_PENALTY
  43. = new KnuthPenalty(0, 0, false, null, true);
  44. private int penalty;
  45. private boolean penaltyFlagged;
  46. private int breakClass = -1;
  47. /**
  48. * Create a new KnuthPenalty.
  49. *
  50. * @param width the width of this penalty
  51. * @param penalty the penalty value of this penalty
  52. * @param penaltyFlagged is this penalty flagged?
  53. * @param pos the Position stored in this penalty
  54. * @param auxiliary is this penalty auxiliary?
  55. */
  56. public KnuthPenalty(int width, int penalty, boolean penaltyFlagged, Position pos,
  57. boolean auxiliary) {
  58. super(width, pos, auxiliary);
  59. this.penalty = penalty;
  60. this.penaltyFlagged = penaltyFlagged;
  61. }
  62. /**
  63. * Create a new KnuthPenalty.
  64. *
  65. * @param width the width of this penalty
  66. * @param penalty the penalty value of this penalty
  67. * @param penaltyFlagged is this penalty flagged?
  68. * @param breakClass the break class of this penalty (one of
  69. * {@link Constants#EN_AUTO}, {@link Constants#EN_COLUMN}, {@link Constants#EN_PAGE},
  70. * {@link Constants#EN_EVEN_PAGE}, {@link Constants#EN_ODD_PAGE})
  71. * @param pos the Position stored in this penalty
  72. * @param isAuxiliary is this penalty auxiliary?
  73. */
  74. public KnuthPenalty(int width, int penalty, boolean penaltyFlagged, int breakClass,
  75. Position pos, boolean isAuxiliary) {
  76. this(width, penalty, penaltyFlagged, pos, isAuxiliary);
  77. this.breakClass = breakClass;
  78. }
  79. private static String getBreakClassName(int breakClass) {
  80. return AbstractBreaker.getBreakClassName(breakClass);
  81. }
  82. /**
  83. * Get the penalty's value as a {@link java.lang.String}.
  84. * (Mainly used in {@link #toString()} methods, to improve readability
  85. * of the trace logs.)
  86. *
  87. * TODO: shouldn't be penalty a class of its own?
  88. *
  89. * @param penaltyValue the penalty value
  90. * @return the penalty value as a {@link java.lang.String}
  91. */
  92. protected static String valueOf(int penaltyValue) {
  93. String result = (penaltyValue < 0) ? "-" : "";
  94. int tmpValue = Math.abs(penaltyValue);
  95. result += (tmpValue == KnuthElement.INFINITE)
  96. ? "INFINITE"
  97. : String.valueOf(tmpValue);
  98. return result;
  99. }
  100. /** {@inheritDoc} */
  101. public boolean isPenalty() {
  102. return true;
  103. }
  104. /**
  105. * @return the penalty value of this penalty.
  106. */
  107. public int getPenalty() {
  108. return penalty;
  109. }
  110. /**
  111. * Sets a new penalty value.
  112. * @param penalty the new penalty value
  113. */
  114. public void setPenalty(int penalty) {
  115. this.penalty = penalty;
  116. }
  117. /** @return true is this penalty is a flagged one. */
  118. public boolean isPenaltyFlagged() {
  119. return penaltyFlagged;
  120. }
  121. /** {@inheritDoc} */
  122. public boolean isForcedBreak() {
  123. return penalty == -KnuthElement.INFINITE;
  124. }
  125. /**
  126. * @return the break class of this penalty (EN_AUTO, EN_COLUMN, EN_PAGE, EN_EVEN_PAGE,
  127. * EN_ODD_PAGE)
  128. */
  129. public int getBreakClass() {
  130. return breakClass;
  131. }
  132. /** {@inheritDoc} */
  133. public String toString() {
  134. StringBuffer buffer = new StringBuffer(64);
  135. if (isAuxiliary()) {
  136. buffer.append("aux. ");
  137. }
  138. buffer.append("penalty");
  139. buffer.append(" p=");
  140. buffer.append(valueOf(this.penalty));
  141. if (this.penaltyFlagged) {
  142. buffer.append(" [flagged]");
  143. }
  144. buffer.append(" w=");
  145. buffer.append(getWidth());
  146. if (isForcedBreak()) {
  147. buffer.append(" (forced break, ")
  148. .append(getBreakClassName(this.breakClass))
  149. .append(")");
  150. } else if (this.penalty >= 0 && this.breakClass != -1) {
  151. //penalty corresponding to a keep constraint
  152. buffer.append(" (keep constraint, ")
  153. .append(getBreakClassName(this.breakClass))
  154. .append(")");
  155. }
  156. return buffer.toString();
  157. }
  158. }