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.

LengthRange.java 8.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. /*
  2. * $Id: LengthRange.java,v 1.10 2003/03/05 20:38:23 jeremias Exp $
  3. * ============================================================================
  4. * The Apache Software License, Version 1.1
  5. * ============================================================================
  6. *
  7. * Copyright (C) 1999-2003 The Apache Software Foundation. All rights reserved.
  8. *
  9. * Redistribution and use in source and binary forms, with or without modifica-
  10. * tion, are permitted provided that the following conditions are met:
  11. *
  12. * 1. Redistributions of source code must retain the above copyright notice,
  13. * this list of conditions and the following disclaimer.
  14. *
  15. * 2. Redistributions in binary form must reproduce the above copyright notice,
  16. * this list of conditions and the following disclaimer in the documentation
  17. * and/or other materials provided with the distribution.
  18. *
  19. * 3. The end-user documentation included with the redistribution, if any, must
  20. * include the following acknowledgment: "This product includes software
  21. * developed by the Apache Software Foundation (http://www.apache.org/)."
  22. * Alternately, this acknowledgment may appear in the software itself, if
  23. * and wherever such third-party acknowledgments normally appear.
  24. *
  25. * 4. The names "FOP" and "Apache Software Foundation" must not be used to
  26. * endorse or promote products derived from this software without prior
  27. * written permission. For written permission, please contact
  28. * apache@apache.org.
  29. *
  30. * 5. Products derived from this software may not be called "Apache", nor may
  31. * "Apache" appear in their name, without prior written permission of the
  32. * Apache Software Foundation.
  33. *
  34. * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
  35. * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
  36. * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  37. * APACHE SOFTWARE FOUNDATION OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
  38. * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLU-
  39. * DING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
  40. * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
  41. * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  42. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  43. * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  44. * ============================================================================
  45. *
  46. * This software consists of voluntary contributions made by many individuals
  47. * on behalf of the Apache Software Foundation and was originally created by
  48. * James Tauber <jtauber@jtauber.com>. For more information on the Apache
  49. * Software Foundation, please see <http://www.apache.org/>.
  50. */
  51. package org.apache.fop.datatypes;
  52. import org.apache.fop.fo.Property;
  53. /**
  54. * A "progression-dimension" quantity.
  55. * Examples: block-progression-dimension, inline-progression-dimension.
  56. * Corresponds to the triplet min-height, height, max-height (or width).
  57. */
  58. public class LengthRange implements CompoundDatatype {
  59. private Property minimum;
  60. private Property optimum;
  61. private Property maximum;
  62. private static final int MINSET = 1;
  63. private static final int OPTSET = 2;
  64. private static final int MAXSET = 4;
  65. private int bfSet = 0; // bit field
  66. private boolean bChecked = false;
  67. /**
  68. * From CompoundDatatype
  69. * @param sCmpnName component name ("minimum", "maximum", or "optimum")
  70. * which is being set
  71. * @param cmpnValue Property object to be set
  72. * @param bIsDefault true of this is the default value (??)
  73. */
  74. public void setComponent(String sCmpnName, Property cmpnValue,
  75. boolean bIsDefault) {
  76. if (sCmpnName.equals("minimum")) {
  77. setMinimum(cmpnValue, bIsDefault);
  78. } else if (sCmpnName.equals("optimum")) {
  79. setOptimum(cmpnValue, bIsDefault);
  80. } else if (sCmpnName.equals("maximum")) {
  81. setMaximum(cmpnValue, bIsDefault);
  82. }
  83. }
  84. /**
  85. * From CompoundDatatype
  86. * @param sCmpnName component name ("minimum", "maximum", or "optimum")
  87. * for which the length is sought
  88. * @return the requested Property, or null if the component name is invalid
  89. */
  90. public Property getComponent(String sCmpnName) {
  91. if (sCmpnName.equals("minimum")) {
  92. return getMinimum();
  93. } else if (sCmpnName.equals("optimum")) {
  94. return getOptimum();
  95. } else if (sCmpnName.equals("maximum")) {
  96. return getMaximum();
  97. } else {
  98. return null; // SHOULDN'T HAPPEN
  99. }
  100. }
  101. /**
  102. * Set minimum value to min.
  103. * @param minimum A Length value specifying the minimum value for this
  104. * LengthRange.
  105. * @param bIsDefault If true, this is set as a "default" value
  106. * and not a user-specified explicit value.
  107. */
  108. protected void setMinimum(Property minimum, boolean bIsDefault) {
  109. this.minimum = minimum;
  110. if (!bIsDefault) {
  111. bfSet |= MINSET;
  112. }
  113. }
  114. /**
  115. * Set maximum value to max if it is >= optimum or optimum isn't set.
  116. * @param max A Length value specifying the maximum value for this
  117. * @param bIsDefault If true, this is set as a "default" value
  118. * and not a user-specified explicit value.
  119. */
  120. protected void setMaximum(Property max, boolean bIsDefault) {
  121. maximum = max;
  122. if (!bIsDefault) {
  123. bfSet |= MAXSET;
  124. }
  125. }
  126. /**
  127. * Set the optimum value.
  128. * @param opt A Length value specifying the optimum value for this
  129. * @param bIsDefault If true, this is set as a "default" value
  130. * and not a user-specified explicit value.
  131. */
  132. protected void setOptimum(Property opt, boolean bIsDefault) {
  133. optimum = opt;
  134. if (!bIsDefault) {
  135. bfSet |= OPTSET;
  136. }
  137. }
  138. // Minimum is prioritaire, if explicit
  139. private void checkConsistency() {
  140. if (bChecked) {
  141. return;
  142. }
  143. // Make sure max >= min
  144. // Must also control if have any allowed enum values!
  145. /**
  146. * *******************
  147. * if (minimum.mvalue() > maximum.mvalue()) {
  148. * if ((bfSet&MINSET)!=0) {
  149. * // if minimum is explicit, force max to min
  150. * if ((bfSet&MAXSET)!=0) {
  151. * // Warning: min>max, resetting max to min
  152. * log.error("forcing max to min in LengthRange");
  153. * }
  154. * maximum = minimum ;
  155. * }
  156. * else {
  157. * minimum = maximum; // minimum was default value
  158. * }
  159. * }
  160. * // Now make sure opt <= max and opt >= min
  161. * if (optimum.mvalue() > maximum.mvalue()) {
  162. * if ((bfSet&OPTSET)!=0) {
  163. * if ((bfSet&MAXSET)!=0) {
  164. * // Warning: opt > max, resetting opt to max
  165. * log.error("forcing opt to max in LengthRange");
  166. * optimum = maximum ;
  167. * }
  168. * else {
  169. * maximum = optimum; // maximum was default value
  170. * }
  171. * }
  172. * else {
  173. * // opt is default and max is explicit or default
  174. * optimum = maximum ;
  175. * }
  176. * }
  177. * else if (optimum.mvalue() < minimum.mvalue()) {
  178. * if ((bfSet&MINSET)!=0) {
  179. * // if minimum is explicit, force opt to min
  180. * if ((bfSet&OPTSET)!=0) {
  181. * log.error("forcing opt to min in LengthRange");
  182. * }
  183. * optimum = minimum ;
  184. * }
  185. * else {
  186. * minimum = optimum; // minimum was default value
  187. * }
  188. * }
  189. * *******$*******
  190. */
  191. bChecked = true;
  192. }
  193. /**
  194. * @return minimum length
  195. */
  196. public Property getMinimum() {
  197. checkConsistency();
  198. return this.minimum;
  199. }
  200. /**
  201. * @return maximum length
  202. */
  203. public Property getMaximum() {
  204. checkConsistency();
  205. return this.maximum;
  206. }
  207. /**
  208. * @return optimum length
  209. */
  210. public Property getOptimum() {
  211. checkConsistency();
  212. return this.optimum;
  213. }
  214. }