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.

SpaceSpecifier.java 5.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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 org.apache.fop.traits.SpaceVal;
  20. import java.util.ArrayList;
  21. import java.util.List;
  22. import org.apache.fop.traits.MinOptMax;
  23. /**
  24. * Accumulate a sequence of space-specifiers (XSL space type) on
  25. * areas with a stacking constraint. Provide a way to resolve these into
  26. * a single MinOptMax value.
  27. */
  28. public class SpaceSpecifier implements Cloneable {
  29. private boolean startsReferenceArea;
  30. private boolean hasForcing = false;
  31. private List spaceVals = new ArrayList();
  32. /**
  33. * Creates a new SpaceSpecifier.
  34. * @param startsReferenceArea true if it starts a new reference area
  35. */
  36. public SpaceSpecifier(boolean startsReferenceArea) {
  37. this.startsReferenceArea = startsReferenceArea;
  38. }
  39. /**
  40. * {@inheritDoc}
  41. */
  42. public Object clone() {
  43. try {
  44. SpaceSpecifier ss = (SpaceSpecifier) super.clone();
  45. ss.startsReferenceArea = startsReferenceArea;
  46. ss.hasForcing = hasForcing;
  47. // Clone the vector, but share the objects in it!
  48. ss.spaceVals = new ArrayList();
  49. ss.spaceVals.addAll(spaceVals);
  50. return ss;
  51. } catch (CloneNotSupportedException cnse) {
  52. return null;
  53. }
  54. }
  55. /**
  56. * Clear all space specifiers
  57. */
  58. public void clear() {
  59. hasForcing = false;
  60. spaceVals.clear();
  61. }
  62. /**
  63. * Indicates whether any space-specifiers have been added.
  64. * @return true if any space-specifiers have been added.
  65. */
  66. public boolean hasSpaces() {
  67. return !spaceVals.isEmpty();
  68. }
  69. /**
  70. * Add a new space to the sequence. If this sequence starts a reference
  71. * area, and the added space is conditional, and there are no
  72. * non-conditional values in the sequence yet, then ignore it. Otherwise
  73. * add it to the sequence.
  74. *
  75. * @param space the space to add.
  76. */
  77. public void addSpace(SpaceVal space) {
  78. if (!startsReferenceArea || !space.isConditional() || hasSpaces()) {
  79. if (space.isForcing()) {
  80. if (!hasForcing) {
  81. // Remove all other values (must all be non-forcing)
  82. spaceVals.clear();
  83. hasForcing = true;
  84. }
  85. spaceVals.add(space);
  86. } else if (!hasForcing) {
  87. // Don't bother adding all 0 space-specifier if not forcing
  88. if (space.getSpace().isNonZero()) {
  89. spaceVals.add(space);
  90. }
  91. }
  92. }
  93. }
  94. /**
  95. * Resolve the current sequence of space-specifiers, accounting for forcing values.
  96. *
  97. * @param endsReferenceArea whether the sequence should be resolved at the trailing edge of
  98. * reference area.
  99. * @return the resolved value as a min/opt/max triple.
  100. */
  101. public MinOptMax resolve(boolean endsReferenceArea) {
  102. int lastIndex = spaceVals.size();
  103. if (endsReferenceArea) {
  104. // Start from the end and count conditional specifiers
  105. // Stop at first non-conditional
  106. for (; lastIndex > 0; --lastIndex) {
  107. SpaceVal spaceVal = (SpaceVal) spaceVals.get(lastIndex - 1);
  108. if (!spaceVal.isConditional()) {
  109. break;
  110. }
  111. }
  112. }
  113. MinOptMax resolvedSpace = MinOptMax.ZERO;
  114. int maxPrecedence = -1;
  115. for (int index = 0; index < lastIndex; index++) {
  116. SpaceVal spaceVal = (SpaceVal) spaceVals.get(index);
  117. MinOptMax space = spaceVal.getSpace();
  118. if (hasForcing) {
  119. resolvedSpace = resolvedSpace.plus(space);
  120. } else {
  121. int precedence = spaceVal.getPrecedence();
  122. if (precedence > maxPrecedence) {
  123. maxPrecedence = precedence;
  124. resolvedSpace = space;
  125. } else if (precedence == maxPrecedence) {
  126. if (space.getOpt() > resolvedSpace.getOpt()) {
  127. resolvedSpace = space;
  128. } else if (space.getOpt() == resolvedSpace.getOpt()) {
  129. if (resolvedSpace.getMin() < space.getMin()) {
  130. resolvedSpace = MinOptMax.getInstance(space.getMin(),
  131. resolvedSpace.getOpt(), resolvedSpace.getMax());
  132. }
  133. if (resolvedSpace.getMax() > space.getMax()) {
  134. resolvedSpace = MinOptMax.getInstance(resolvedSpace.getMin(),
  135. resolvedSpace.getOpt(), space.getMax());
  136. }
  137. }
  138. }
  139. }
  140. }
  141. return resolvedSpace;
  142. }
  143. /** {@inheritDoc} */
  144. public String toString() {
  145. return "Space Specifier (resolved at begin/end of ref. area:):\n"
  146. + resolve(false) + "\n" + resolve(true);
  147. }
  148. }