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

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