1 /* FontSize.java -- Converts CSS font size values into real 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 font-size values into real (point) values.
44 * @author Roman Kennke (kennke@aicas.com)
55 * The actual font size.
60 * The index of one of the standard sizes that this font size maps to.
61 * This is -1 if this fontsize doesn't map to one of the standard sizes.
65 private int sizeIndex;
68 * True when this font size is relative.
70 private boolean isRelative;
73 * The default size for 'medium' absolute size. The other absolute sizes
74 * are calculated from this.
76 public static final int DEFAULT_FONT_SIZE = 12;
79 * The scaling factors relative to the medium size. Medium is at index 2.
81 private static final double[] SCALE = {0.8, 0.9, 1.0, 1.2, 1.4, 1.6, 1.8 };
84 * Creates a new FontSize for the specified value.
86 * @param val the value to convert
88 public FontSize(String val)
97 * Returns the font size value.
99 * @return the font size value
101 public int getValue(int p)
108 public int getValue()
115 * Returns the converted real value in point.
117 * @return the converted real value in point
119 private int mapValue()
122 if (value.contains("pt"))
123 intVal = mapPoints();
124 else if (value.contains("px"))
125 intVal = mapPixels();
126 else if (value.contains("em") || value.contains("%")
127 || value.contains("larger") || value.contains("smaller"))
133 intVal = mapAbsolute();
138 * Maps point values ('XXXpt').
140 * @return the real font size
142 private int mapPoints()
144 int end = value.indexOf("pt");
145 String number = value.substring(0, end);
146 int intVal = Integer.parseInt(number);
151 * Maps pixel values ('XXXpx').
153 * @return the real font size
155 private int mapPixels()
157 int end = value.indexOf("px");
159 end = value.length();
160 String number = value.substring(0, end);
163 int intVal = Integer.parseInt(number);
166 catch (NumberFormatException ex)
168 return DEFAULT_FONT_SIZE;
172 private int mapPercent(int par)
174 int end = value.indexOf("%");
176 end = value.length();
177 String number = value.substring(0, end);
180 int intVal = Integer.parseInt(number);
181 return intVal * par / 100;
183 catch (NumberFormatException ex)
185 System.err.println("couldn't map value: '" + value + "'");
186 return DEFAULT_FONT_SIZE;
190 private int mapEM(int par)
192 int end = value.indexOf("em");
194 end = value.length();
195 String number = value.substring(0, end);
198 float factor = Float.parseFloat(number);
199 // FIXME: Should be relative to the parent element's size.
200 return (int) (factor * par);
202 catch (NumberFormatException ex)
204 return DEFAULT_FONT_SIZE;
208 private int mapSmaller(int par)
210 return (int) (par * 0.9);
213 private int mapLarger(int par)
215 return (int) (par * 0.9);
219 * Maps absolute font-size values.
221 * @return the real value
223 private int mapAbsolute()
226 if (value.equals("xx-small") || value.equals("x-small"))
228 else if (value.equals("small"))
230 else if (value.equals("medium"))
232 else if (value.equals("large"))
234 else if (value.equals("x-large"))
236 else if (value.equals("xx-large"))
240 double scale = SCALE[index];
241 // FIXME: Scale the real medium size of the document, rather than the
243 int intVal = (int) (scale * DEFAULT_FONT_SIZE);
249 * Returns the string representation.
251 public String toString()
256 private int mapRelative(int par)
258 if (value.indexOf('%') != -1)
259 size = mapPercent(par);
260 else if (value.indexOf("em") != -1)
262 else if (value.indexOf("larger") != -1)
263 size = mapLarger(par);
264 else if (value.indexOf("smaller") != -1)
265 size = mapSmaller(par);
269 public boolean isRelative()