001 /*
002 * NumberWithUnits.java
003 *
004 *
005 * The Salamander Project - 2D and 3D graphics libraries in Java
006 * Copyright (C) 2004 Mark McKay
007 *
008 * This library is free software; you can redistribute it and/or
009 * modify it under the terms of the GNU Lesser General Public
010 * License as published by the Free Software Foundation; either
011 * version 2.1 of the License, or (at your option) any later version.
012 *
013 * This library is distributed in the hope that it will be useful,
014 * but WITHOUT ANY WARRANTY; without even the implied warranty of
015 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
016 * Lesser General Public License for more details.
017 *
018 * You should have received a copy of the GNU Lesser General Public
019 * License along with this library; if not, write to the Free Software
020 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
021 *
022 * Mark McKay can be contacted at mark@kitfox.com. Salamander and other
023 * projects can be found at http://www.kitfox.com
024 *
025 * Created on February 18, 2004, 2:43 PM
026 */
027
028 package com.kitfox.svg.xml;
029
030 import java.io.Serializable;
031
032 /**
033 * @author Mark McKay
034 * @author <a href="mailto:mark@kitfox.com">Mark McKay</a>
035 */
036 public class NumberWithUnits implements Serializable
037 {
038 public static final long serialVersionUID = 0;
039
040 public static final int UT_UNITLESS = 0;
041 public static final int UT_PX = 1; //Pixels
042 public static final int UT_CM = 2; //Centimeters
043 public static final int UT_MM = 3; //Millimeters
044 public static final int UT_IN = 4; //Inches
045 public static final int UT_EM = 5; //Default font height
046 public static final int UT_EX = 6; //Height of character 'x' in default font
047 public static final int UT_PT = 7; //Points - 1/72 of an inch
048 public static final int UT_PC = 8; //Picas - 1/6 of an inch
049 public static final int UT_PERCENT = 9; //Percent - relative width
050
051 float value = 0f;
052 int unitType = UT_UNITLESS;
053
054 /** Creates a new instance of NumberWithUnits */
055 public NumberWithUnits()
056 {
057 }
058
059 public NumberWithUnits(String value)
060 {
061 set(value);
062 }
063
064 public NumberWithUnits(float value, int unitType)
065 {
066 this.value = value;
067 this.unitType = unitType;
068 }
069
070 public float getValue() { return value; }
071 public int getUnits() { return unitType; }
072
073 public void set(String value)
074 {
075 this.value = XMLParseUtil.findFloat(value);
076 unitType = UT_UNITLESS;
077
078 if (value.indexOf("px") != -1) { unitType = UT_PX; return; }
079 if (value.indexOf("cm") != -1) { unitType = UT_CM; return; }
080 if (value.indexOf("mm") != -1) { unitType = UT_MM; return; }
081 if (value.indexOf("in") != -1) { unitType = UT_IN; return; }
082 if (value.indexOf("em") != -1) { unitType = UT_EM; return; }
083 if (value.indexOf("ex") != -1) { unitType = UT_EX; return; }
084 if (value.indexOf("pt") != -1) { unitType = UT_PT; return; }
085 if (value.indexOf("pc") != -1) { unitType = UT_PC; return; }
086 if (value.indexOf("%") != -1) { unitType = UT_PERCENT; return; }
087 }
088
089 public static String unitsAsString(int unitIdx)
090 {
091 switch (unitIdx)
092 {
093 default:
094 return "";
095 case UT_PX:
096 return "px";
097 case UT_CM:
098 return "cm";
099 case UT_MM:
100 return "mm";
101 case UT_IN:
102 return "in";
103 case UT_EM:
104 return "em";
105 case UT_EX:
106 return "ex";
107 case UT_PT:
108 return "pt";
109 case UT_PC:
110 return "pc";
111 case UT_PERCENT:
112 return "%";
113 }
114 }
115
116 public String toString()
117 {
118 return "" + value + unitsAsString(unitType);
119 }
120
121 public boolean equals(Object obj)
122 {
123 if (obj == null) {
124 return false;
125 }
126 if (getClass() != obj.getClass()) {
127 return false;
128 }
129 final NumberWithUnits other = (NumberWithUnits) obj;
130 if (Float.floatToIntBits(this.value) != Float.floatToIntBits(other.value)) {
131 return false;
132 }
133 if (this.unitType != other.unitType) {
134 return false;
135 }
136 return true;
137 }
138
139 public int hashCode()
140 {
141 int hash = 5;
142 hash = 37 * hash + Float.floatToIntBits(this.value);
143 hash = 37 * hash + this.unitType;
144 return hash;
145 }
146
147
148 }