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 7.2KB

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