001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: /* $Id: FixedLength.java 563951 2007-08-08 17:37:33Z vhennebert $ */
019:
020: package org.apache.fop.fo.properties;
021:
022: import org.apache.fop.datatypes.PercentBaseContext;
023:
024: /**
025: * An absolute length quantity in XSL
026: */
027: public class FixedLength extends LengthProperty {
028:
029: /** cache holding all canonical FixedLength instances */
030: private static final PropertyCache cache = new PropertyCache();
031:
032: private int millipoints;
033:
034: /**
035: * Set the length given a number of units and a unit name.
036: * @param numUnits quantity of input units
037: * @param units input unit specifier (in, cm, etc.)
038: */
039: private FixedLength(double numUnits, String units) {
040: convert(numUnits, units);
041: }
042:
043: /**
044: * Return the canonical FixedLength instance corresponding
045: * to the computed value
046: * @param numUnits input units
047: * @param units unit specifier
048: * @return
049: */
050: public static FixedLength getInstance(double numUnits, String units) {
051: return (FixedLength) cache.fetch(new FixedLength(numUnits,
052: units));
053:
054: }
055:
056: /**
057: * @param baseUnits the length as a number of base units (millipoints)
058: */
059: public FixedLength(int baseUnits) {
060: millipoints = baseUnits;
061: }
062:
063: /**
064: * Convert the given length to a dimensionless integer representing
065: * a whole number of base units (milli-points).
066: * @param dvalue quantity of input units
067: * @param unit input unit specifier (in, cm, etc.)
068: */
069: protected void convert(double dvalue, String unit) {
070: // TODO: the whole routine smells fishy.
071:
072: int assumedResolution = 1; // points/pixel = 72dpi
073:
074: if (unit.equals("in")) {
075: dvalue = dvalue * 72;
076: } else if (unit.equals("cm")) {
077: dvalue = dvalue * 28.3464567;
078: } else if (unit.equals("mm")) {
079: dvalue = dvalue * 2.83464567;
080: } else if (unit.equals("pt")) {
081: // Do nothing.
082: // dvalue = dvalue;
083: } else if (unit.equals("mpt")) { //mpt is non-standard!!! mpt=millipoints
084: // TODO: this seems to be wrong.
085: // Do nothing.
086: // dvalue = dvalue;
087: } else if (unit.equals("pc")) {
088: dvalue = dvalue * 12;
089: /*
090: * } else if (unit.equals("em")) {
091: * dvalue = dvalue * fontsize;
092: */
093: } else if (unit.equals("px")) {
094: // TODO: get resolution from user agent?
095: dvalue = dvalue * assumedResolution;
096: } else {
097: dvalue = 0;
098: log.error("Unknown length unit '" + unit + "'");
099: }
100: if (unit.equals("mpt")) {
101: millipoints = (int) dvalue;
102: } else {
103: millipoints = (int) (dvalue * 1000);
104: }
105: }
106:
107: /**
108: * @see org.apache.fop.datatypes.Numeric#getValue()
109: */
110: public int getValue() {
111: return millipoints;
112: }
113:
114: /**
115: * @see org.apache.fop.datatypes.Numeric#getValue(PercentBaseContext)
116: */
117: public int getValue(PercentBaseContext context) {
118: return millipoints;
119: }
120:
121: /**
122: * @see org.apache.fop.datatypes.Numeric#getNumericValue()
123: */
124: public double getNumericValue() {
125: return millipoints;
126: }
127:
128: /**
129: * @see org.apache.fop.datatypes.Numeric#getNumericValue(PercentBaseContext)
130: */
131: public double getNumericValue(PercentBaseContext context) {
132: return millipoints;
133: }
134:
135: /**
136: * Return true since FixedLength are always absolute.
137: * @see org.apache.fop.datatypes.Numeric#isAbsolute()
138: */
139: public boolean isAbsolute() {
140: return true;
141: }
142:
143: /**
144: * @see java.lang.Object#toString()
145: */
146: public String toString() {
147: return millipoints + "mpt";
148: }
149:
150: /**
151: * @see java.lang.Object#equals(Object)
152: */
153: public boolean equals(Object obj) {
154: if (obj instanceof FixedLength) {
155: return (((FixedLength) obj).millipoints == this .millipoints);
156: } else {
157: return false;
158: }
159: }
160:
161: /**
162: * @see java.lang.Object#hashCode()
163: */
164: public int hashCode() {
165: return millipoints;
166: }
167: }
|