1 17 18 19 20 package org.apache.fop.fo.properties; 21 22 import org.apache.fop.datatypes.CompoundDatatype; 23 import org.apache.fop.datatypes.Length; 24 import org.apache.fop.datatypes.PercentBaseContext; 25 import org.apache.fop.fo.FObj; 26 import org.apache.fop.fo.PropertyList; 27 import org.apache.fop.fo.expr.PropertyException; 28 29 32 public class LengthRangeProperty extends Property implements CompoundDatatype { 33 private Property minimum; 34 private Property optimum; 35 private Property maximum; 36 private static final int MINSET = 1; 37 private static final int OPTSET = 2; 38 private static final int MAXSET = 4; 39 private int bfSet = 0; private boolean consistent = false; 41 42 45 public static class Maker extends CompoundPropertyMaker { 46 47 50 public Maker(int propId) { 51 super(propId); 52 } 53 54 58 public Property makeNewProperty() { 59 return new LengthRangeProperty(); 60 } 61 62 private boolean isNegativeLength(Length len) { 63 return ((len instanceof PercentLength 64 && ((PercentLength) len).getPercentage() < 0) 65 || (len.isAbsolute() && len.getValue() < 0)); 66 } 67 68 71 public Property convertProperty(Property p, 72 PropertyList propertyList, FObj fo) 73 throws PropertyException { 74 75 if (p instanceof LengthRangeProperty) { 76 return p; 77 } 78 79 if (this.propId == PR_BLOCK_PROGRESSION_DIMENSION 80 || this.propId == PR_INLINE_PROGRESSION_DIMENSION) { 81 Length len = p.getLength(); 82 if (len != null) { 83 if (isNegativeLength(len)) { 84 log.warn(FObj.decorateWithContextInfo( 85 "Replaced negative value (" + len + ") for " + getName() 86 + " with 0mpt", fo)); 87 p = new FixedLength(0); 88 } 89 } 90 } 91 92 return super.convertProperty(p, propertyList, fo); 93 } 94 95 96 99 protected Property setSubprop(Property baseProperty, int subpropertyId, 100 Property subproperty) { 101 CompoundDatatype val = (CompoundDatatype) baseProperty.getObject(); 102 if (this.propId == PR_BLOCK_PROGRESSION_DIMENSION 103 || this.propId == PR_INLINE_PROGRESSION_DIMENSION) { 104 Length len = subproperty.getLength(); 105 if (len != null) { 106 if (isNegativeLength(len)) { 107 log.warn("Replaced negative value (" + len + ") for " + getName() 108 + " with 0mpt"); 109 val.setComponent(subpropertyId, 110 new FixedLength(0), false); 111 return baseProperty; 112 } 113 } 114 } 115 val.setComponent(subpropertyId, subproperty, false); 116 return baseProperty; 117 } 118 119 } 120 121 122 123 126 public void setComponent(int cmpId, Property cmpnValue, 127 boolean bIsDefault) { 128 if (cmpId == CP_MINIMUM) { 129 setMinimum(cmpnValue, bIsDefault); 130 } else if (cmpId == CP_OPTIMUM) { 131 setOptimum(cmpnValue, bIsDefault); 132 } else if (cmpId == CP_MAXIMUM) { 133 setMaximum(cmpnValue, bIsDefault); 134 } 135 } 136 137 140 public Property getComponent(int cmpId) { 141 if (cmpId == CP_MINIMUM) { 142 return getMinimum(null); 143 } else if (cmpId == CP_OPTIMUM) { 144 return getOptimum(null); 145 } else if (cmpId == CP_MAXIMUM) { 146 return getMaximum(null); 147 } else { 148 return null; } 150 } 151 152 159 protected void setMinimum(Property minimum, boolean bIsDefault) { 160 this.minimum = minimum; 161 if (!bIsDefault) { 162 bfSet |= MINSET; 163 } 164 consistent = false; 165 } 166 167 168 174 protected void setMaximum(Property max, boolean bIsDefault) { 175 maximum = max; 176 if (!bIsDefault) { 177 bfSet |= MAXSET; 178 } 179 consistent = false; 180 } 181 182 183 189 protected void setOptimum(Property opt, boolean bIsDefault) { 190 optimum = opt; 191 if (!bIsDefault) { 192 bfSet |= OPTSET; 193 } 194 consistent = false; 195 } 196 197 private void checkConsistency(PercentBaseContext context) { 199 if (consistent) { 200 return; 201 } 202 if (context == null) { 203 return; 204 } 205 208 if (!minimum.isAuto() && !maximum.isAuto() 209 && minimum.getLength().getValue(context) > maximum.getLength().getValue(context)) { 210 if ((bfSet & MINSET) != 0) { 211 if ((bfSet & MAXSET) != 0) { 213 log.error("forcing max to min in LengthRange"); 215 } 216 maximum = minimum; 217 } else { 218 minimum = maximum; } 220 } 221 if (!optimum.isAuto() && !maximum.isAuto() 223 && optimum.getLength().getValue(context) > maximum.getLength().getValue(context)) { 224 if ((bfSet & OPTSET) != 0) { 225 if ((bfSet & MAXSET) != 0) { 226 log.error("forcing opt to max in LengthRange"); 228 optimum = maximum; 229 } else { 230 maximum = optimum; } 232 } else { 233 optimum = maximum; 235 } 236 } else if (!optimum.isAuto() && !minimum.isAuto() 237 && optimum.getLength().getValue(context) 238 < minimum.getLength().getValue(context)) { 239 if ((bfSet & MINSET) != 0) { 240 if ((bfSet & OPTSET) != 0) { 242 log.error("forcing opt to min in LengthRange"); 243 } 244 optimum = minimum; 245 } else { 246 minimum = optimum; } 248 } 249 250 consistent = true; 251 } 252 253 257 public Property getMinimum(PercentBaseContext context) { 258 checkConsistency(context); 259 return this.minimum; 260 } 261 262 266 public Property getMaximum(PercentBaseContext context) { 267 checkConsistency(context); 268 return this.maximum; 269 } 270 271 275 public Property getOptimum(PercentBaseContext context) { 276 checkConsistency(context); 277 return this.optimum; 278 } 279 280 281 public String toString() { 282 return "LengthRange[" 283 + "min:" + getMinimum(null).getObject() 284 + ", max:" + getMaximum(null).getObject() 285 + ", opt:" + getOptimum(null).getObject() + "]"; 286 } 287 288 291 public LengthRangeProperty getLengthRange() { 292 return this; 293 } 294 295 298 public Object getObject() { 299 return this; 300 } 301 302 } 303 | Popular Tags |