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.

ElementListUtils.java 9.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  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 java.util.ListIterator;
  21. import org.apache.fop.traits.MinOptMax;
  22. import org.apache.fop.util.ListUtil;
  23. /**
  24. * Utilities for Knuth element lists.
  25. */
  26. public final class ElementListUtils {
  27. private ElementListUtils() {
  28. // Utility class.
  29. }
  30. /**
  31. * Removes legal breaks in an element list. A constraint can be specified to limit the
  32. * range in which the breaks are removed. Legal breaks occuring before at least
  33. * constraint.opt space is filled will be removed.
  34. * @param elements the element list
  35. * @param constraint min/opt/max value to restrict the range in which the breaks are removed.
  36. * @return true if the opt constraint is bigger than the list contents
  37. */
  38. public static boolean removeLegalBreaks(List elements, MinOptMax constraint) {
  39. return removeLegalBreaks(elements, constraint.opt);
  40. }
  41. /**
  42. * Removes legal breaks in an element list. A constraint can be specified to limit the
  43. * range in which the breaks are removed. Legal breaks occuring before at least
  44. * constraint space is filled will be removed.
  45. * @param elements the element list
  46. * @param constraint value to restrict the range in which the breaks are removed.
  47. * @return true if the constraint is bigger than the list contents
  48. */
  49. public static boolean removeLegalBreaks(List elements, int constraint) {
  50. int len = 0;
  51. ListIterator iter = elements.listIterator();
  52. while (iter.hasNext()) {
  53. ListElement el = (ListElement)iter.next();
  54. if (el.isPenalty()) {
  55. KnuthPenalty penalty = (KnuthPenalty)el;
  56. //Convert all penalties to break inhibitors
  57. if (penalty.getP() < KnuthPenalty.INFINITE) {
  58. iter.set(new KnuthPenalty(penalty.getW(), KnuthPenalty.INFINITE,
  59. penalty.isFlagged(), penalty.getPosition(), penalty.isAuxiliary()));
  60. }
  61. } else if (el.isGlue()) {
  62. KnuthGlue glue = (KnuthGlue)el;
  63. len += glue.getW();
  64. iter.previous();
  65. el = (ListElement)iter.previous();
  66. iter.next();
  67. if (el.isBox()) {
  68. iter.add(new KnuthPenalty(0, KnuthPenalty.INFINITE, false,
  69. null, false));
  70. }
  71. iter.next();
  72. } else if (el instanceof BreakElement) {
  73. BreakElement breakEl = (BreakElement)el;
  74. if (breakEl.getPenaltyValue() < KnuthPenalty.INFINITE) {
  75. breakEl.setPenaltyValue(KnuthPenalty.INFINITE);
  76. }
  77. } else {
  78. KnuthElement kel = (KnuthElement)el;
  79. len += kel.getW();
  80. }
  81. if (len >= constraint) {
  82. return false;
  83. }
  84. }
  85. return true;
  86. }
  87. /**
  88. * Removes legal breaks in an element list. A constraint can be specified to limit the
  89. * range in which the breaks are removed. Legal breaks within the space specified through the
  90. * constraint (starting from the end of the element list) will be removed.
  91. * @param elements the element list
  92. * @param constraint value to restrict the range in which the breaks are removed.
  93. * @return true if the constraint is bigger than the list contents
  94. */
  95. public static boolean removeLegalBreaksFromEnd(List elements, int constraint) {
  96. int len = 0;
  97. ListIterator i = elements.listIterator(elements.size());
  98. while (i.hasPrevious()) {
  99. ListElement el = (ListElement)i.previous();
  100. if (el.isPenalty()) {
  101. KnuthPenalty penalty = (KnuthPenalty)el;
  102. //Convert all penalties to break inhibitors
  103. if (penalty.getP() < KnuthPenalty.INFINITE) {
  104. i.set(new KnuthPenalty(penalty.getW(), KnuthPenalty.INFINITE,
  105. penalty.isFlagged(), penalty.getPosition(), penalty.isAuxiliary()));
  106. }
  107. } else if (el.isGlue()) {
  108. KnuthGlue glue = (KnuthGlue)el;
  109. len += glue.getW();
  110. el = (ListElement)i.previous();
  111. i.next();
  112. if (el.isBox()) {
  113. i.add(new KnuthPenalty(0, KnuthPenalty.INFINITE, false,
  114. null, false));
  115. }
  116. } else if (el.isUnresolvedElement()) {
  117. if (el instanceof BreakElement) {
  118. BreakElement breakEl = (BreakElement)el;
  119. if (breakEl.getPenaltyValue() < KnuthPenalty.INFINITE) {
  120. breakEl.setPenaltyValue(KnuthPenalty.INFINITE);
  121. }
  122. } else if (el instanceof UnresolvedListElementWithLength) {
  123. UnresolvedListElementWithLength uel = (UnresolvedListElementWithLength)el;
  124. len += uel.getLength().opt;
  125. }
  126. } else {
  127. KnuthElement kel = (KnuthElement)el;
  128. len += kel.getW();
  129. }
  130. if (len >= constraint) {
  131. return false;
  132. }
  133. }
  134. return true;
  135. }
  136. /**
  137. * Calculates the content length of the given element list. Warning: It doesn't take any
  138. * stretch and shrink possibilities into account.
  139. * @param elems the element list
  140. * @param start element at which to start
  141. * @param end element at which to stop
  142. * @return the content length
  143. */
  144. public static int calcContentLength(List elems, int start, int end) {
  145. ListIterator iter = elems.listIterator(start);
  146. int count = end - start + 1;
  147. int len = 0;
  148. while (iter.hasNext()) {
  149. ListElement el = (ListElement)iter.next();
  150. if (el.isBox()) {
  151. len += ((KnuthElement)el).getW();
  152. } else if (el.isGlue()) {
  153. len += ((KnuthElement)el).getW();
  154. } else {
  155. //log.debug("Ignoring penalty: " + el);
  156. //ignore penalties
  157. }
  158. count--;
  159. if (count == 0) {
  160. break;
  161. }
  162. }
  163. return len;
  164. }
  165. /**
  166. * Calculates the content length of the given element list. Warning: It doesn't take any
  167. * stretch and shrink possibilities into account.
  168. * @param elems the element list
  169. * @return the content length
  170. */
  171. public static int calcContentLength(List elems) {
  172. return calcContentLength(elems, 0, elems.size() - 1);
  173. }
  174. /**
  175. * Indicates whether the given element list ends with a forced break.
  176. * @param elems the element list
  177. * @return true if the list ends with a forced break
  178. */
  179. public static boolean endsWithForcedBreak(List elems) {
  180. ListElement last = (ListElement) ListUtil.getLast(elems);
  181. return last.isForcedBreak();
  182. }
  183. /**
  184. * Indicates whether the given element list ends with a penalty with a non-infinite penalty
  185. * value.
  186. * @param elems the element list
  187. * @return true if the list ends with a non-infinite penalty
  188. */
  189. public static boolean endsWithNonInfinitePenalty(List elems) {
  190. ListElement last = (ListElement) ListUtil.getLast(elems);
  191. if (last.isPenalty() && ((KnuthPenalty)last).getP() < KnuthElement.INFINITE) {
  192. return true;
  193. } else if (last instanceof BreakElement
  194. && ((BreakElement)last).getPenaltyValue() < KnuthElement.INFINITE) {
  195. return true;
  196. }
  197. return false;
  198. }
  199. /**
  200. * Determines the position of the previous break before the start index on an
  201. * element list.
  202. * @param elems the element list
  203. * @param startIndex the start index
  204. * @return the position of the previous break, or -1 if there was no previous break
  205. */
  206. public static int determinePreviousBreak(List elems, int startIndex) {
  207. int prevBreak = startIndex - 1;
  208. while (prevBreak >= 0) {
  209. KnuthElement el = (KnuthElement)elems.get(prevBreak);
  210. if (el.isPenalty() && el.getP() < KnuthElement.INFINITE) {
  211. break;
  212. }
  213. prevBreak--;
  214. }
  215. return prevBreak;
  216. }
  217. }