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)
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
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
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
;
42 * Converts CSS length values to usable length values.
44 * @author Roman Kennke (kennke@aicas.com)
55 * The converted value.
57 protected float floatValue
;
60 * Indicates when the value is a percentage value.
62 private boolean isPercentage
;
65 * Indicates a length value that is relative to the font size (em).
67 private boolean isFontEMRelative
;
70 * Indicates a length value that is relative to the font size (ex).
72 private boolean isFontEXRelative
;
85 * Creates a new length converter instance.
87 * @param val the CSS value
89 public Length(String val
)
91 isFontEMRelative
= false;
92 isFontEXRelative
= false;
95 int i
= value
.indexOf("px");
96 int percent
= value
.indexOf("%");
97 int em
= value
.indexOf("em");
98 int ex
= value
.indexOf("ex");
104 String sub
= value
.substring(0, i
);
105 floatValue
= Float
.parseFloat(sub
);
107 else if (percent
!= -1)
110 String sub
= value
.substring(0, percent
);
111 floatValue
= Float
.parseFloat(sub
) / 100;
115 isFontEMRelative
= true;
116 String sub
= value
.substring(0, em
);
117 floatValue
= Float
.parseFloat(sub
);
121 isFontEXRelative
= true;
122 String sub
= value
.substring(0, ex
);
123 floatValue
= Float
.parseFloat(sub
);
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()
148 * Returns the absolute span for the case when this length value is
151 * @param base the base span
153 * @return the absolute span
155 public float getValue(float base
)
157 float span
= floatValue
;
160 else if (isFontEMRelative
)
162 else if (isFontEXRelative
)
168 * Sets the font relative EM base.
170 * @param base the font relative EM base
172 public void setEMBase(float base
)
178 * Sets the font relative EX base.
180 * @param base the font relative EX base
182 public void setEXBase(float 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
)
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
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
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()
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('%');
253 Integer
.parseInt(value
.substring(0, px
));
257 Integer
.parseInt(value
.substring(0, em
));
261 Integer
.parseInt(value
.substring(0, ex
));
265 Integer
.parseInt(value
.substring(0, ex
));
269 Integer
.parseInt(value
);
272 catch (NumberFormatException nfe
)
279 public String
toString()