001: /*
002: * Copyright (c) 2007, intarsys consulting GmbH
003: *
004: * Redistribution and use in source and binary forms, with or without
005: * modification, are permitted provided that the following conditions are met:
006: *
007: * - Redistributions of source code must retain the above copyright notice,
008: * this list of conditions and the following disclaimer.
009: *
010: * - Redistributions in binary form must reproduce the above copyright notice,
011: * this list of conditions and the following disclaimer in the documentation
012: * and/or other materials provided with the distribution.
013: *
014: * - Neither the name of intarsys nor the names of its contributors may be used
015: * to endorse or promote products derived from this software without specific
016: * prior written permission.
017: *
018: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
019: * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
020: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
021: * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
022: * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
023: * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
024: * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
025: * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
026: * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
027: * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
028: * POSSIBILITY OF SUCH DAMAGE.
029: */
030: package de.intarsys.pdf.cos;
031:
032: /**
033: * Represents floating point numbers in pdf.
034: */
035: public class COSFixed extends COSNumber {
036: static public final int DEFAULT_PRECISION = 5;
037:
038: static public COSFixed create(byte[] bytes, int start, int length) {
039: long result = 0;
040: long decimal = 1;
041: int end = start + length;
042: boolean negative = false;
043: boolean point = false;
044: int precision = 0;
045: int i = start;
046: byte prefix = bytes[i];
047: if (prefix == '+') {
048: i++;
049: } else if (prefix == '-') {
050: negative = true;
051: i++;
052: }
053: for (; i < end; i++) {
054: byte digit = bytes[i];
055: if (digit == '.') {
056: point = true;
057: } else {
058: result = ((result * 10) + digit) - '0';
059: if (point) {
060: decimal = decimal * 10;
061: precision++;
062: }
063: }
064: }
065: if (negative) {
066: return new COSFixed(
067: -(float) ((double) result / (double) decimal),
068: precision);
069: }
070: return new COSFixed(
071: (float) ((double) result / (double) decimal), precision);
072: }
073:
074: static public COSFixed create(double value) {
075: return new COSFixed((float) value, DEFAULT_PRECISION);
076: }
077:
078: static public COSFixed create(double value, int precision) {
079: return new COSFixed((float) value, precision);
080: }
081:
082: static public COSFixed create(float value) {
083: return new COSFixed(value, DEFAULT_PRECISION);
084: }
085:
086: static public COSFixed create(float value, int precision) {
087: return new COSFixed(value, precision);
088: }
089:
090: private final float floatValue;
091:
092: private int precision;
093:
094: protected COSFixed(COSObject object) {
095: super (object);
096: COSFixed fixed = (COSFixed) object;
097: this .floatValue = fixed.floatValue;
098: this .precision = fixed.precision;
099: }
100:
101: protected COSFixed(float value, int precision) {
102: this .floatValue = value;
103: this .precision = precision;
104: }
105:
106: /*
107: * (non-Javadoc)
108: *
109: * @see de.intarsys.pdf.cos.COSObject#accept(de.intarsys.pdf.cos.ICOSObjectVisitor)
110: */
111: public java.lang.Object accept(ICOSObjectVisitor visitor)
112: throws COSVisitorException {
113: return visitor.visitFromFixed(this );
114: }
115:
116: /*
117: * (non-Javadoc)
118: *
119: * @see de.intarsys.pdf.cos.COSObject#asFixed()
120: */
121: public COSFixed asFixed() {
122: return this ;
123: }
124:
125: /*
126: * (non-Javadoc)
127: *
128: * @see de.intarsys.pdf.cos.COSObject#basicToString()
129: */
130: protected String basicToString() {
131: return String.valueOf(floatValue);
132: }
133:
134: protected COSObject copyBasic() {
135: return create(floatValue, precision);
136: }
137:
138: /*
139: * (non-Javadoc)
140: *
141: * @see java.lang.Object#equals(java.lang.Object)
142: */
143: public boolean equals(Object o) {
144: if (!(o instanceof COSFixed)) {
145: return false;
146: }
147: return floatValue == ((COSFixed) o).floatValue;
148: }
149:
150: /*
151: * (non-Javadoc)
152: *
153: * @see de.intarsys.pdf.cos.COSNumber#floatValue()
154: */
155: public float floatValue() {
156: return floatValue;
157: }
158:
159: /**
160: * The precision (digits after period) for this.
161: *
162: * @return The precision (digits after period) for this.
163: */
164: public int getPrecision() {
165: return precision;
166: }
167:
168: /*
169: * (non-Javadoc)
170: *
171: * @see java.lang.Object#hashCode()
172: */
173: public int hashCode() {
174: return Float.floatToIntBits(floatValue);
175: }
176:
177: /*
178: * (non-Javadoc)
179: *
180: * @see de.intarsys.pdf.cos.COSNumber#intValue()
181: */
182: public int intValue() {
183: return (int) floatValue;
184: }
185:
186: /*
187: * (non-Javadoc)
188: *
189: * @see de.intarsys.pdf.cos.COSObject#restoreState(java.lang.Object)
190: */
191: public void restoreState(Object object) {
192: super .restoreState(object);
193: COSFixed fixed = (COSFixed) object;
194: this .precision = fixed.precision;
195: }
196:
197: /*
198: * (non-Javadoc)
199: *
200: * @see de.intarsys.tools.objectsession.ISaveStateSupport#saveState()
201: */
202: public Object saveState() {
203: return new COSFixed(this );
204: }
205:
206: /**
207: * Assign the precision for this.
208: *
209: * @param precision
210: * The new precision.
211: */
212: public void setPrecision(int precision) {
213: this.precision = precision;
214: }
215: }
|