Reset branch to trunk.
[official-gcc.git] / trunk / libjava / classpath / gnu / javax / swing / text / html / css / Length.java
blob06fa36e3d29303d7e0af606678f133c1c898efa0
1 /* Length.java -- Converts CSS length values
2 Copyright (C) 2006 Free Software Foundation, Inc.
4 This file is part of GNU Classpath.
6 GNU Classpath is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
11 GNU Classpath is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Classpath; see the file COPYING. If not, write to the
18 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19 02110-1301 USA.
21 Linking this library statically or dynamically with other modules is
22 making a combined work based on this library. Thus, the terms and
23 conditions of the GNU General Public License cover the whole
24 combination.
26 As a special exception, the copyright holders of this library give you
27 permission to link this library with independent modules to produce an
28 executable, regardless of the license terms of these independent
29 modules, and to copy and distribute the resulting executable under
30 terms of your choice, provided that you also meet, for each linked
31 independent module, the terms and conditions of the license of that
32 module. An independent module is a module which is not derived from
33 or based on this library. If you modify this library, you may extend
34 this exception to your version of the library, but you are not
35 obligated to do so. If you do not wish to do so, delete this
36 exception statement from your version. */
39 package gnu.javax.swing.text.html.css;
41 /**
42 * Converts CSS length values to usable length values.
44 * @author Roman Kennke (kennke@aicas.com)
46 public class Length
49 /**
50 * The original value.
52 private String value;
54 /**
55 * The converted value.
57 protected float floatValue;
59 /**
60 * Indicates when the value is a percentage value.
62 private boolean isPercentage;
64 /**
65 * Indicates a length value that is relative to the font size (em).
67 private boolean isFontEMRelative;
69 /**
70 * Indicates a length value that is relative to the font size (ex).
72 private boolean isFontEXRelative;
74 /**
75 * The EM base size.
77 private float emBase;
79 /**
80 * The EX base size.
82 private float exBase;
84 /**
85 * Creates a new length converter instance.
87 * @param val the CSS value
89 public Length(String val)
91 isFontEMRelative = false;
92 isFontEXRelative = false;
93 isPercentage = false;
94 value = val;
95 int i = value.indexOf("px");
96 int percent = value.indexOf("%");
97 int em = value.indexOf("em");
98 int ex = value.indexOf("ex");
99 try
101 floatValue = 0.0F;
102 if (i != -1)
104 String sub = value.substring(0, i);
105 floatValue = Float.parseFloat(sub);
107 else if (percent != -1)
109 isPercentage = true;
110 String sub = value.substring(0, percent);
111 floatValue = Float.parseFloat(sub) / 100;
113 else if (em != -1)
115 isFontEMRelative = true;
116 String sub = value.substring(0, em);
117 floatValue = Float.parseFloat(sub);
119 else if (ex != -1)
121 isFontEXRelative = true;
122 String sub = value.substring(0, ex);
123 floatValue = Float.parseFloat(sub);
125 else
127 floatValue = Float.parseFloat(value);
130 catch (NumberFormatException exc)
132 // Don't let such small problems interrupt CSS parsing.
133 System.err.println("couldn't parse: " + val);
138 * Returns the value converted to pixels.
140 * @return the value converted to pixels
142 public float getValue()
144 return floatValue;
148 * Returns the absolute span for the case when this length value is
149 * a relative value.
151 * @param base the base span
153 * @return the absolute span
155 public float getValue(float base)
157 float span = floatValue;
158 if (isPercentage)
159 span *= base;
160 else if (isFontEMRelative)
161 span *= emBase;
162 else if (isFontEXRelative)
163 span *= exBase;
164 return span;
168 * Sets the font relative EM base.
170 * @param base the font relative EM base
172 public void setEMBase(float base)
174 emBase = base;
178 * Sets the font relative EX base.
180 * @param base the font relative EX base
182 public void setEXBase(float base)
184 exBase = base;
188 * Sets the font relative base values.
190 * @param emBase the EM base
191 * @param exBase the EX base
193 public void setFontBases(float emBase, float exBase)
195 setEMBase(emBase);
196 setEXBase(exBase);
200 * Returns true when this length value is an em font relative value. In
201 * order to get correct results, you need the exBase property set up
202 * correctly.
204 * @return true when this length value is an ex font relative value
206 public boolean isFontEMRelative()
208 return isFontEMRelative;
212 * Returns true when this length value is an ex font relative value. In
213 * order to get correct results, you need the emBase property set up
214 * correctly.
216 * @return true when this length value is an ex font relative value
218 public boolean isFontEXRelative()
220 return isFontEXRelative;
224 * Returns <code>true</code> when the length value is a percentage
225 * value, <code>false</code> otherwise.
227 * @return <code>true</code> when the length value is a percentage
228 * value, <code>false</code> otherwise
230 public boolean isPercentage()
232 return isPercentage;
236 * Checks if the specified value makes up a valid length value.
238 * @param value the value to check
240 * @return <code>true</code> if the value is a valid length
242 public static boolean isValid(String value)
244 boolean isValid = true;
245 int px = value.indexOf("px");
246 int em = value.indexOf("em");
247 int ex = value.indexOf("ex");
248 int pc = value.indexOf('%');
251 if (px != -1)
253 Integer.parseInt(value.substring(0, px));
255 else if (em != -1)
257 Integer.parseInt(value.substring(0, em));
259 else if (ex != -1)
261 Integer.parseInt(value.substring(0, ex));
263 else if (pc != -1)
265 Integer.parseInt(value.substring(0, ex));
267 else
269 Integer.parseInt(value);
272 catch (NumberFormatException nfe)
274 isValid = false;
276 return isValid;
279 public String toString()
281 return value;