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.2KB

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