KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > fop > fo > properties > FixedLength


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17
18 /* $Id: FixedLength.java 426576 2006-07-28 15:44:37Z jeremias $ */
19
20 package org.apache.fop.fo.properties;
21
22 import org.apache.fop.datatypes.PercentBaseContext;
23
24 /**
25  * An absolute length quantity in XSL
26  */

27 public class FixedLength extends LengthProperty {
28     private int millipoints;
29
30     /**
31      * Set the length given
32      * @param numRelUnits the number of relative units
33      * @param iCurFontSize the current font size in base units.
34      */

35     public FixedLength(double numRelUnits, int iCurFontSize) {
36         millipoints = (int) (numRelUnits * (double)iCurFontSize);
37     }
38
39     /**
40      * Set the length given a number of units and a unit name.
41      * @param numUnits quantity of input units
42      * @param units input unit specifier (in, cm, etc.)
43      */

44     public FixedLength(double numUnits, String JavaDoc units) {
45         convert(numUnits, units);
46     }
47
48     /**
49      * @param baseUnits the length as a number of base units (millipoints)
50      */

51     public FixedLength(int baseUnits) {
52         millipoints = baseUnits;
53     }
54
55     /**
56      * Convert the given length to a dimensionless integer representing
57      * a whole number of base units (milli-points).
58      * @param dvalue quantity of input units
59      * @param unit input unit specifier (in, cm, etc.)
60      */

61     protected void convert(double dvalue, String JavaDoc unit) {
62         // TODO: the whole routine smells fishy.
63

64         int assumedResolution = 1; // points/pixel = 72dpi
65

66         if (unit.equals("in")) {
67             dvalue = dvalue * 72;
68         } else if (unit.equals("cm")) {
69             dvalue = dvalue * 28.3464567;
70         } else if (unit.equals("mm")) {
71             dvalue = dvalue * 2.83464567;
72         } else if (unit.equals("pt")) {
73             // Do nothing.
74
// dvalue = dvalue;
75
} else if (unit.equals("mpt")) { //mpt is non-standard!!! mpt=millipoints
76
// TODO: this seems to be wrong.
77
// Do nothing.
78
// dvalue = dvalue;
79
} else if (unit.equals("pc")) {
80             dvalue = dvalue * 12;
81             /*
82              * } else if (unit.equals("em")) {
83              * dvalue = dvalue * fontsize;
84              */

85         } else if (unit.equals("px")) {
86             // TODO: get resolution from user agent?
87
dvalue = dvalue * assumedResolution;
88         } else {
89             dvalue = 0;
90             log.error("Unknown length unit '" + unit + "'");
91         }
92         if (unit.equals("mpt")) {
93             millipoints = (int)dvalue;
94         } else {
95             millipoints = (int)(dvalue * 1000);
96         }
97     }
98
99     /**
100      * @see org.apache.fop.datatypes.Numeric#getValue()
101      */

102     public int getValue() {
103         return millipoints;
104     }
105
106     /**
107      * @see org.apache.fop.datatypes.Numeric#getValue(PercentBaseContext)
108      */

109     public int getValue(PercentBaseContext context) {
110         return millipoints;
111     }
112
113     /**
114      * @see org.apache.fop.datatypes.Numeric#getNumericValue()
115      */

116     public double getNumericValue() {
117         return millipoints;
118     }
119
120     /**
121      * @see org.apache.fop.datatypes.Numeric#getNumericValue(PercentBaseContext)
122      */

123     public double getNumericValue(PercentBaseContext context) {
124         return millipoints;
125     }
126
127     /**
128      * Return true since FixedLength are always absolute.
129      * @see org.apache.fop.datatypes.Numeric#isAbsolute()
130      */

131     public boolean isAbsolute() {
132         return true;
133     }
134
135     /**
136      * @see java.lang.Object#toString()
137      */

138     public String JavaDoc toString() {
139         return millipoints + "mpt";
140     }
141
142 }
143
144
Popular Tags