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.

Numeric.java 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406
  1. /*-- $Id$ --
  2. ============================================================================
  3. The Apache Software License, Version 1.1
  4. ============================================================================
  5. Copyright (C) 1999 The Apache Software Foundation. All rights reserved.
  6. Redistribution and use in source and binary forms, with or without modifica-
  7. tion, are permitted provided that the following conditions are met:
  8. 1. Redistributions of source code must retain the above copyright notice,
  9. this list of conditions and the following disclaimer.
  10. 2. Redistributions in binary form must reproduce the above copyright notice,
  11. this list of conditions and the following disclaimer in the documentation
  12. and/or other materials provided with the distribution.
  13. 3. The end-user documentation included with the redistribution, if any, must
  14. include the following acknowledgment: "This product includes software
  15. developed by the Apache Software Foundation (http://www.apache.org/)."
  16. Alternately, this acknowledgment may appear in the software itself, if
  17. and wherever such third-party acknowledgments normally appear.
  18. 4. The names "FOP" and "Apache Software Foundation" must not be used to
  19. endorse or promote products derived from this software without prior
  20. written permission. For written permission, please contact
  21. apache@apache.org.
  22. 5. Products derived from this software may not be called "Apache", nor may
  23. "Apache" appear in their name, without prior written permission of the
  24. Apache Software Foundation.
  25. THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
  26. INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
  27. FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  28. APACHE SOFTWARE FOUNDATION OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
  29. INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLU-
  30. DING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
  31. OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
  32. ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  33. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  34. THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  35. This software consists of voluntary contributions made by many individuals
  36. on behalf of the Apache Software Foundation and was originally created by
  37. James Tauber <jtauber@jtauber.com>. For more information on the Apache
  38. Software Foundation, please see <http://www.apache.org/>.
  39. */
  40. package org.apache.fop.fo.expr;
  41. import org.apache.fop.fo.Property;
  42. import org.apache.fop.datatypes.Length;
  43. import org.apache.fop.datatypes.PercentLength;
  44. import org.apache.fop.datatypes.LinearCombinationLength;
  45. import org.apache.fop.datatypes.MixedLength;
  46. import org.apache.fop.datatypes.TableColLength;
  47. import org.apache.fop.datatypes.PercentBase;
  48. /**
  49. * Represents a "numeric" value as defined by the XSL FO Specification.
  50. * This consists of one or more kinds of value specifications, from
  51. * absolute numbers (units power of 0) to lengths (unit power of 1),
  52. * relative lengths (ems), percentage lengths.
  53. * A Numeric can be constructed from other Property types representing
  54. * Numbers or Length-type values.
  55. * Numeric provides methods to return Number and Length values based on
  56. * its current value.
  57. * It supports basic arithmetic operations involving Numerics.
  58. */
  59. public class Numeric {
  60. // Bit fields
  61. public static final int ABS_LENGTH = 1; // abs units (or number)
  62. public static final int PC_LENGTH=2; // Percentage
  63. public static final int TCOL_LENGTH=4; // Table units
  64. private int valType ;
  65. private double absValue ;
  66. private double pcValue ;
  67. private PercentBase pcBase=null; // base value for PC_LENGTH component
  68. private double tcolValue;
  69. private int dim;
  70. /**
  71. * Construct a Numeric object by specifying one or more components,
  72. * including absolute length, percent length, table units.
  73. * @param valType A combination of bits representing the value types.
  74. * @param absValue The value of a Number or resolved Length value if
  75. * the ABS_LENGTH flag is set.
  76. * @param pcValue The decimal percent value if the PC_LENGTH flag is set
  77. * @param tcolValue The decimal table unit value if the TCOL_LENGTH flag
  78. * is set.
  79. * @param dim The dimension of the value. 0 for a Number, 1 for a Length
  80. * (any type), >1, <0 if Lengths have been multiplied or divided.
  81. * @pcBase The PercentBase object used to calculate an actual value for
  82. * a PC_LENGTH.
  83. */
  84. protected Numeric(int valType, double absValue,
  85. double pcValue, double tcolValue, int dim,
  86. PercentBase pcBase) {
  87. this.valType = valType;
  88. this.absValue = absValue;
  89. this.pcValue = pcValue;
  90. this.tcolValue = tcolValue;
  91. this.dim = dim;
  92. this.pcBase = pcBase;
  93. }
  94. /**
  95. * Construct a Numeric object of dimension 0 from a double.
  96. * @param valType A combination of bits representing the value types.
  97. * @param absValue The value of a Number or resolved Length value.
  98. */
  99. /***
  100. protected Numeric(int valType, double absValue) {
  101. this.valType = valType;
  102. this.absValue = absValue;
  103. }
  104. ***/
  105. /**
  106. * Construct a Numeric object from a Number.
  107. * @param num The number.
  108. */
  109. public Numeric(Number num) {
  110. this(ABS_LENGTH, num.doubleValue(), 0.0, 0.0, 0, null);
  111. }
  112. /**
  113. * Construct a Numeric object from a Length.
  114. * @param l The Length.
  115. */
  116. public Numeric(Length l) {
  117. this(ABS_LENGTH, (double)l.mvalue(), 0.0, 0.0, 1, null);
  118. }
  119. /**
  120. * Construct a Numeric object from a PercentLength.
  121. * @param pclen The PercentLength.
  122. */
  123. public Numeric(PercentLength pclen) {
  124. this(PC_LENGTH, 0.0, pclen.value(), 0.0, 1, pclen.getBaseLength());
  125. }
  126. /**
  127. * Construct a Numeric object from a TableColLength.
  128. * @param tclen The TableColLength.
  129. */
  130. public Numeric(TableColLength tclen) {
  131. this(TCOL_LENGTH, 0.0, 0.0, tclen.getTableUnits(), 1, null);
  132. }
  133. /**
  134. * Return the current value as a Length if possible. This constructs
  135. * a new Length or Length subclass based on the current value type
  136. * of the Numeric.
  137. * If the stored value has a unit dimension other than 1, null
  138. * is returned.
  139. */
  140. public Length asLength() {
  141. if (dim == 1) {
  142. if (valType == ABS_LENGTH) {
  143. return new Length((int)absValue);
  144. }
  145. PercentLength pclen = null;
  146. if ((valType & PC_LENGTH)!=0) {
  147. pclen = new PercentLength(pcValue, pcBase);
  148. if (valType == PC_LENGTH)
  149. return pclen;
  150. }
  151. if ((valType & TCOL_LENGTH) != 0) {
  152. return new TableColLength((int)absValue, pclen, tcolValue);
  153. }
  154. return new MixedLength((int)absValue, pclen);
  155. }
  156. else {
  157. // or throw exception???
  158. // can't make Length if dimension != 1
  159. return null;
  160. }
  161. }
  162. /**
  163. * Return the current value as a Number if possible.
  164. * Calls asDouble().
  165. */
  166. public Number asNumber() {
  167. return asDouble();
  168. }
  169. public Double asDouble() {
  170. if (dim == 0 && valType==ABS_LENGTH) {
  171. return new Double(absValue);
  172. }
  173. else {
  174. // or throw exception???
  175. // can't make Number if dimension != 0
  176. return null;
  177. }
  178. }
  179. /**
  180. * Return the current value as a Integer if possible.
  181. * If the unit dimension is 0 and the value type is ABSOLUTE, an Integer
  182. * is returned. Otherwise null is returned. Note: the current value is
  183. * truncated if necessary to make an integer value.
  184. */
  185. /**
  186. public Integer asInteger() {
  187. if (dim == 0 && valType==ABS_LENGTH) {
  188. return new Integer((int)absValue);
  189. }
  190. else {
  191. // or throw exception???
  192. // can't make Number if dimension != 0
  193. return null;
  194. }
  195. }
  196. **/
  197. /**
  198. * Return a boolean value indiciating whether the currently stored
  199. * value consists of different "types" of values (absolute, percent,
  200. * and/or table-unit.)
  201. */
  202. private boolean isMixedType() {
  203. int ntype = 0;
  204. for (int t = valType; t!=0; t=t>>1) {
  205. if ((t & 1) != 0) ++ntype;
  206. }
  207. return ntype>1;
  208. }
  209. /**
  210. * Subtract the operand from the current value and return a new Numeric
  211. * representing the result.
  212. * @param op The value to subtract.
  213. * @return A Numeric representing the result.
  214. * @throws PropertyException If the dimension of the operand is different
  215. * from the dimension of this Numeric.
  216. */
  217. public Numeric subtract(Numeric op) throws PropertyException {
  218. // Check of same dimension
  219. // Add together absolute and table units
  220. // What about percentages??? Treat as colUnits if they can't be
  221. // in same property!
  222. if (dim == op.dim) {
  223. PercentBase npcBase = ((valType & PC_LENGTH)!=0)? pcBase : op.pcBase;
  224. // Subtract each type of value
  225. return new Numeric(valType|op.valType, absValue-op.absValue,
  226. pcValue-op.pcValue,
  227. tcolValue-op.tcolValue,dim, npcBase);
  228. }
  229. else {
  230. throw new PropertyException("Can't add Numerics of different dimensions");
  231. }
  232. }
  233. /**
  234. * Add the operand from the current value and return a new Numeric
  235. * representing the result.
  236. * @param op The value to add.
  237. * @return A Numeric representing the result.
  238. * @throws PropertyException If the dimension of the operand is different
  239. * from the dimension of this Numeric.
  240. */
  241. public Numeric add(Numeric op) throws PropertyException {
  242. // Check of same dimension
  243. // Add together absolute and table units
  244. // What about percentages??? Treat as colUnits if they can't be
  245. // in same property!
  246. if (dim == op.dim) {
  247. PercentBase npcBase = ((valType & PC_LENGTH)!=0)? pcBase : op.pcBase;
  248. // Add each type of value
  249. return new Numeric(valType | op.valType, absValue+op.absValue,
  250. pcValue+op.pcValue,
  251. tcolValue+op.tcolValue, dim, npcBase);
  252. }
  253. else {
  254. throw new PropertyException("Can't add Numerics of different dimensions");
  255. }
  256. }
  257. /**
  258. * Multiply the the current value by the operand and return a new Numeric
  259. * representing the result.
  260. * @param op The multiplier.
  261. * @return A Numeric representing the result.
  262. * @throws PropertyException If both Numerics have "mixed" type.
  263. */
  264. public Numeric multiply(Numeric op) throws PropertyException {
  265. // Multiply together absolute units and add dimensions (exponents)
  266. // What about percentages??? Treat as colUnits if they can't be
  267. // in same property!
  268. if (dim == 0) {
  269. // This is a dimensionless quantity, ie. a "Number"
  270. return new Numeric(op.valType, absValue*op.absValue,
  271. absValue*op.pcValue,
  272. absValue*op.tcolValue, op.dim,
  273. op.pcBase);
  274. }
  275. else if (op.dim == 0) {
  276. double opval = op.absValue;
  277. return new Numeric(valType, opval*absValue,
  278. opval*pcValue, opval*tcolValue, dim, pcBase);
  279. }
  280. else if (valType == op.valType && !isMixedType()) {
  281. // Check same relbase and pcbase ???
  282. PercentBase npcBase = ((valType & PC_LENGTH)!=0)? pcBase : op.pcBase;
  283. return new Numeric(valType, absValue * op.absValue,
  284. pcValue*op.pcValue,
  285. tcolValue*op.tcolValue,dim+op.dim,
  286. npcBase);
  287. }
  288. else {
  289. throw new PropertyException("Can't multiply mixed Numerics");
  290. }
  291. }
  292. /**
  293. * Divide the the current value by the operand and return a new Numeric
  294. * representing the result.
  295. * @param op The divisor.
  296. * @return A Numeric representing the result.
  297. * @throws PropertyException If both Numerics have "mixed" type.
  298. */
  299. public Numeric divide(Numeric op) throws PropertyException {
  300. // Multiply together absolute units and add dimensions (exponents)
  301. // What about percentages??? Treat as colUnits if they can't be
  302. // in same property!
  303. if (dim == 0) {
  304. // This is a dimensionless quantity, ie. a "Number"
  305. return new Numeric(op.valType, absValue/op.absValue,
  306. absValue/op.pcValue,
  307. absValue/op.tcolValue, -op.dim, op.pcBase);
  308. }
  309. else if (op.dim == 0) {
  310. double opval = op.absValue;
  311. return new Numeric(valType, absValue/opval,
  312. pcValue/opval, tcolValue/opval, dim, pcBase);
  313. }
  314. else if (valType == op.valType && !isMixedType()) {
  315. PercentBase npcBase = ((valType & PC_LENGTH)!=0)? pcBase : op.pcBase;
  316. return new Numeric(valType,
  317. (valType==ABS_LENGTH? absValue/op.absValue:0.0),
  318. (valType==PC_LENGTH? pcValue/op.pcValue:0.0),
  319. (valType==TCOL_LENGTH? tcolValue/op.tcolValue:0.0),
  320. dim-op.dim, npcBase);
  321. }
  322. else {
  323. throw new PropertyException("Can't divide mixed Numerics.");
  324. }
  325. }
  326. /**
  327. * Return the absolute value of this Numeric.
  328. * @return A new Numeric object representing the absolute value.
  329. */
  330. public Numeric abs() {
  331. return new Numeric(valType, Math.abs(absValue),
  332. Math.abs(pcValue), Math.abs(tcolValue), dim, pcBase);
  333. }
  334. /**
  335. * Return a Numeric which is the maximum of the current value and the
  336. * operand.
  337. * @throws PropertyException If the dimensions or value types of the
  338. * object and the operand are different.
  339. */
  340. public Numeric max(Numeric op) throws PropertyException {
  341. double rslt = 0.0;
  342. // Only compare if have same dimension and value type!
  343. if (dim == op.dim && valType == op.valType && !isMixedType()) {
  344. if (valType==ABS_LENGTH) rslt = absValue - op.absValue;
  345. else if (valType==PC_LENGTH) rslt = pcValue - op.pcValue;
  346. else if (valType==TCOL_LENGTH) rslt = tcolValue - op.tcolValue;
  347. if (rslt > 0.0)
  348. return this;
  349. else return op;
  350. }
  351. throw new PropertyException("Arguments to max() must have same dimension and value type.");
  352. }
  353. /**
  354. * Return a Numeric which is the minimum of the current value and the
  355. * operand.
  356. * @throws PropertyException If the dimensions or value types of the
  357. * object and the operand are different.
  358. */
  359. public Numeric min(Numeric op) throws PropertyException {
  360. double rslt = 0.0;
  361. // Only compare if have same dimension and value type!
  362. if (dim == op.dim && valType == op.valType && !isMixedType()) {
  363. if (valType==ABS_LENGTH) rslt = absValue - op.absValue;
  364. else if (valType==PC_LENGTH) rslt = pcValue - op.pcValue;
  365. else if (valType==TCOL_LENGTH) rslt = tcolValue - op.tcolValue;
  366. if (rslt > 0.0)
  367. return op;
  368. else return this;
  369. }
  370. throw new PropertyException("Arguments to min() must have same dimension and value type.");
  371. }
  372. }