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