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.

BreakElement.java 6.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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 java.util.List;
  20. import org.apache.fop.fo.Constants;
  21. /**
  22. * This class represents an unresolved break possibility.
  23. */
  24. public class BreakElement extends UnresolvedListElement {
  25. private int penaltyWidth;
  26. private int penaltyValue;
  27. private int breakClass = -1;
  28. private List pendingBeforeMarks;
  29. private List pendingAfterMarks;
  30. /**
  31. * Main constructor
  32. * @param position the Position instance needed by the addAreas stage of the LMs.
  33. * @param penaltyValue the penalty value for the penalty element to be constructed
  34. * @param context the layout context which contains the pending conditional elements
  35. */
  36. public BreakElement(Position position, int penaltyValue, LayoutContext context) {
  37. this(position, penaltyValue, -1, context);
  38. }
  39. /**
  40. * Create a new BreakElement for the given {@code position}, {@code penaltyValue}
  41. * and {@code breakClass}. (Used principally to generate break-possibilities in
  42. * ranges of content that must be kept together within the context corresponding
  43. * to the {@code breakClass}; expected to be one of {@link Constants#EN_AUTO},
  44. * {@link Constants#EN_LINE}, {@link Constants#EN_COLUMN} or {@link Constants#EN_PAGE})
  45. * @param position the corresponding {@link Position}
  46. * @param penaltyValue the penalty value
  47. * @param breakClass the break class
  48. * @param context the {@link LayoutContext}
  49. */
  50. public BreakElement(Position position, int penaltyValue, int breakClass, LayoutContext context) {
  51. this(position, 0, penaltyValue, breakClass, context);
  52. }
  53. /**
  54. * Constructor for hard breaks.
  55. *
  56. * @param position the Position instance needed by the addAreas stage of the LMs.
  57. * @param penaltyWidth the penalty width
  58. * @param penaltyValue the penalty value for the penalty element to be constructed
  59. * @param breakClass the break class of this penalty (one of {@link Constants#EN_AUTO},
  60. * {@link Constants#EN_COLUMN}, {@link Constants#EN_PAGE},
  61. * {@link Constants#EN_EVEN_PAGE}, {@link Constants#EN_ODD_PAGE})
  62. * @param context the layout context which contains the pending conditional elements
  63. */
  64. public BreakElement(Position position, int penaltyWidth, int penaltyValue,
  65. int breakClass, LayoutContext context) {
  66. super(position);
  67. this.penaltyWidth = penaltyWidth;
  68. this.penaltyValue = penaltyValue;
  69. this.breakClass = breakClass;
  70. this.pendingBeforeMarks = context.getPendingBeforeMarks();
  71. this.pendingAfterMarks = context.getPendingAfterMarks();
  72. }
  73. private static String getBreakClassName(int breakClass) {
  74. return AbstractBreaker.getBreakClassName(breakClass);
  75. }
  76. /** {@inheritDoc} */
  77. public boolean isConditional() {
  78. return false; //Does not really apply here
  79. }
  80. /** {@inheritDoc} */
  81. /*
  82. public boolean isPenalty() {
  83. return true; //not entirely true but a BreakElement will generate a penalty later
  84. }*/
  85. /** @return the penalty width */
  86. public int getPenaltyWidth() {
  87. return this.penaltyWidth;
  88. }
  89. /** @return the penalty value */
  90. public int getPenaltyValue() {
  91. return this.penaltyValue;
  92. }
  93. /**
  94. * Sets the penalty value.
  95. * @param p the new penalty value
  96. */
  97. public void setPenaltyValue(int p) {
  98. this.penaltyValue = p;
  99. }
  100. /** {@inheritDoc} */
  101. public boolean isForcedBreak() {
  102. return penaltyValue == -KnuthElement.INFINITE;
  103. }
  104. /**
  105. * Returns the break class of this penalty.
  106. *
  107. * @return one of {@link Constants#EN_AUTO}, {@link Constants#EN_COLUMN},
  108. * {@link Constants#EN_PAGE}, {@link Constants#EN_EVEN_PAGE},
  109. * {@link Constants#EN_ODD_PAGE}
  110. */
  111. public int getBreakClass() {
  112. return breakClass;
  113. }
  114. /**
  115. * Sets the break class.
  116. *
  117. * @param breakClass one of {@link Constants#EN_AUTO}, {@link Constants#EN_COLUMN},
  118. * {@link Constants#EN_PAGE}, {@link Constants#EN_EVEN_PAGE},
  119. * {@link Constants#EN_ODD_PAGE}
  120. */
  121. public void setBreakClass(int breakClass) {
  122. this.breakClass = breakClass;
  123. }
  124. /** @return the pending border and padding elements at the before edge */
  125. public List getPendingBeforeMarks() {
  126. return this.pendingBeforeMarks;
  127. }
  128. /** @return the pending border and padding elements at the after edge */
  129. public List getPendingAfterMarks() {
  130. return this.pendingAfterMarks;
  131. }
  132. /**
  133. * Clears all pending marks associated with this break element. This is used in break
  134. * cases where we only know very late if the break is actually after all the content
  135. * of an FO has been generated.
  136. */
  137. public void clearPendingMarks() {
  138. this.pendingBeforeMarks = null;
  139. this.pendingAfterMarks = null;
  140. }
  141. /** {@inheritDoc} */
  142. public String toString() {
  143. StringBuffer sb = new StringBuffer(64);
  144. sb.append("BreakPossibility[p:");
  145. sb.append(KnuthPenalty.valueOf(this.penaltyValue));
  146. if (isForcedBreak()) {
  147. sb.append(" (forced break, ")
  148. .append(getBreakClassName(this.breakClass))
  149. .append(")");
  150. } else if (this.penaltyValue >= 0 && this.breakClass != -1) {
  151. sb.append(" (keep constraint, ")
  152. .append(getBreakClassName(this.breakClass))
  153. .append(")");
  154. }
  155. sb.append("; w:");
  156. sb.append(penaltyWidth);
  157. sb.append("]");
  158. return sb.toString();
  159. }
  160. }