1 | /*
|
---|
2 | * NumberWithUnits.java
|
---|
3 | *
|
---|
4 | *
|
---|
5 | * The Salamander Project - 2D and 3D graphics libraries in Java
|
---|
6 | * Copyright (C) 2004 Mark McKay
|
---|
7 | *
|
---|
8 | * This library is free software; you can redistribute it and/or
|
---|
9 | * modify it under the terms of the GNU Lesser General Public
|
---|
10 | * License as published by the Free Software Foundation; either
|
---|
11 | * version 2.1 of the License, or (at your option) any later version.
|
---|
12 | *
|
---|
13 | * This library is distributed in the hope that it will be useful,
|
---|
14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
---|
16 | * Lesser General Public License for more details.
|
---|
17 | *
|
---|
18 | * You should have received a copy of the GNU Lesser General Public
|
---|
19 | * License along with this library; if not, write to the Free Software
|
---|
20 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
---|
21 | *
|
---|
22 | * Mark McKay can be contacted at mark@kitfox.com. Salamander and other
|
---|
23 | * projects can be found at http://www.kitfox.com
|
---|
24 | *
|
---|
25 | * Created on February 18, 2004, 2:43 PM
|
---|
26 | */
|
---|
27 |
|
---|
28 | package com.kitfox.svg.xml;
|
---|
29 |
|
---|
30 | import java.io.Serializable;
|
---|
31 |
|
---|
32 | /**
|
---|
33 | * @author Mark McKay
|
---|
34 | * @author <a href="mailto:mark@kitfox.com">Mark McKay</a>
|
---|
35 | */
|
---|
36 | public class NumberWithUnits implements Serializable
|
---|
37 | {
|
---|
38 | public static final long serialVersionUID = 0;
|
---|
39 |
|
---|
40 | public static final int UT_UNITLESS = 0;
|
---|
41 | public static final int UT_PX = 1; //Pixels
|
---|
42 | public static final int UT_CM = 2; //Centimeters
|
---|
43 | public static final int UT_MM = 3; //Millimeters
|
---|
44 | public static final int UT_IN = 4; //Inches
|
---|
45 | public static final int UT_EM = 5; //Default font height
|
---|
46 | public static final int UT_EX = 6; //Height of character 'x' in default font
|
---|
47 | public static final int UT_PT = 7; //Points - 1/72 of an inch
|
---|
48 | public static final int UT_PC = 8; //Picas - 1/6 of an inch
|
---|
49 | public static final int UT_PERCENT = 9; //Percent - relative width
|
---|
50 |
|
---|
51 | float value = 0f;
|
---|
52 | int unitType = UT_UNITLESS;
|
---|
53 |
|
---|
54 | /** Creates a new instance of NumberWithUnits */
|
---|
55 | public NumberWithUnits()
|
---|
56 | {
|
---|
57 | }
|
---|
58 |
|
---|
59 | public NumberWithUnits(String value)
|
---|
60 | {
|
---|
61 | set(value);
|
---|
62 | }
|
---|
63 |
|
---|
64 | public NumberWithUnits(float value, int unitType)
|
---|
65 | {
|
---|
66 | this.value = value;
|
---|
67 | this.unitType = unitType;
|
---|
68 | }
|
---|
69 |
|
---|
70 | public float getValue() { return value; }
|
---|
71 | public int getUnits() { return unitType; }
|
---|
72 |
|
---|
73 | public void set(String value)
|
---|
74 | {
|
---|
75 | this.value = XMLParseUtil.findFloat(value);
|
---|
76 | unitType = UT_UNITLESS;
|
---|
77 |
|
---|
78 | if (value.indexOf("px") != -1) { unitType = UT_PX; return; }
|
---|
79 | if (value.indexOf("cm") != -1) { unitType = UT_CM; return; }
|
---|
80 | if (value.indexOf("mm") != -1) { unitType = UT_MM; return; }
|
---|
81 | if (value.indexOf("in") != -1) { unitType = UT_IN; return; }
|
---|
82 | if (value.indexOf("em") != -1) { unitType = UT_EM; return; }
|
---|
83 | if (value.indexOf("ex") != -1) { unitType = UT_EX; return; }
|
---|
84 | if (value.indexOf("pt") != -1) { unitType = UT_PT; return; }
|
---|
85 | if (value.indexOf("pc") != -1) { unitType = UT_PC; return; }
|
---|
86 | if (value.indexOf("%") != -1) { unitType = UT_PERCENT; return; }
|
---|
87 | }
|
---|
88 |
|
---|
89 | public static String unitsAsString(int unitIdx)
|
---|
90 | {
|
---|
91 | switch (unitIdx)
|
---|
92 | {
|
---|
93 | default:
|
---|
94 | return "";
|
---|
95 | case UT_PX:
|
---|
96 | return "px";
|
---|
97 | case UT_CM:
|
---|
98 | return "cm";
|
---|
99 | 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 | }
|
---|