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

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