Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

KnuthPenalty.java 5.9KB

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