1 17 18 19 20 package org.apache.fop.layoutmgr; 21 22 import org.apache.fop.traits.SpaceVal; 23 import java.util.ArrayList ; 24 import java.util.List ; 25 import org.apache.fop.traits.MinOptMax; 26 27 32 public class SpaceSpecifier implements Cloneable { 33 34 35 private boolean startsReferenceArea; 36 private boolean hasForcing = false; 37 private List spaceVals = new ArrayList (); 38 39 40 44 public SpaceSpecifier(boolean startsReferenceArea) { 45 this.startsReferenceArea = startsReferenceArea; 46 } 47 48 51 public Object clone() { 52 try { 53 SpaceSpecifier ss = (SpaceSpecifier) super.clone(); 54 ss.startsReferenceArea = startsReferenceArea; 55 ss.hasForcing = hasForcing; 56 ss.spaceVals = new ArrayList (); 58 ss.spaceVals.addAll(spaceVals); 59 return ss; 60 } catch (CloneNotSupportedException cnse) { 61 return null; 62 } 63 64 } 65 66 69 public void clear() { 70 hasForcing = false; 71 spaceVals.clear(); 72 } 73 74 78 public boolean hasSpaces() { 79 return (spaceVals.size() > 0); 80 } 81 82 88 public void addSpace(SpaceVal moreSpace) { 89 if (!startsReferenceArea 90 || !moreSpace.isConditional() 91 || !spaceVals.isEmpty()) { 92 if (moreSpace.isForcing()) { 93 if (!hasForcing) { 94 spaceVals.clear(); 96 hasForcing = true; 97 } 98 spaceVals.add(moreSpace); 99 } else if (!hasForcing) { 100 if (moreSpace.getSpace().min != 0 102 || moreSpace.getSpace().opt != 0 103 || moreSpace.getSpace().max != 0) { 104 spaceVals.add(moreSpace); 105 } 106 } 107 } 108 } 109 110 111 118 public MinOptMax resolve(boolean endsReferenceArea) { 119 int lastIndex = spaceVals.size(); 120 if (endsReferenceArea) { 121 for (; lastIndex > 0; --lastIndex) { 124 SpaceVal spaceVal = (SpaceVal) spaceVals.get(lastIndex - 1); 125 if (!spaceVal.isConditional()) { 126 break; 127 } 128 } 129 } 130 MinOptMax resolvedSpace = new MinOptMax(0); 131 int maxPrecedence = -1; 132 for (int index = 0; index < lastIndex; index++) { 133 SpaceVal spaceVal = (SpaceVal) spaceVals.get(index); 134 if (hasForcing) { 135 resolvedSpace.add(spaceVal.getSpace()); 136 } else if (spaceVal.getPrecedence() > maxPrecedence) { 137 maxPrecedence = spaceVal.getPrecedence(); 138 resolvedSpace = spaceVal.getSpace(); 139 } else if (spaceVal.getPrecedence() == maxPrecedence) { 140 if (spaceVal.getSpace().opt > resolvedSpace.opt) { 141 resolvedSpace = spaceVal.getSpace(); 142 } else if (spaceVal.getSpace().opt == resolvedSpace.opt) { 143 if (resolvedSpace.min < spaceVal.getSpace().min) { 144 resolvedSpace.min = spaceVal.getSpace().min; 145 } 146 if (resolvedSpace.max > spaceVal.getSpace().max) { 147 resolvedSpace.max = spaceVal.getSpace().max; 148 } 149 } 150 } 151 152 } 153 return resolvedSpace; 154 } 155 156 public String toString() { 157 return "Space Specifier (resolved at begin/end of ref. area:):\n" + 158 resolve(false).toString() + "\n" + 159 resolve(true).toString(); 160 } 161 } 162 | Popular Tags |