Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

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