OpenCores
URL https://opencores.org/ocsvn/openrisc/openrisc/trunk

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [gnu-dev/] [or1k-gcc/] [libjava/] [classpath/] [gnu/] [javax/] [swing/] [text/] [html/] [css/] [Length.java] - Blame information for rev 769

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 769 jeremybenn
/* Length.java -- Converts CSS length values
2
   Copyright (C) 2006 Free Software Foundation, Inc.
3
 
4
This file is part of GNU Classpath.
5
 
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.
10
 
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.
15
 
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.
20
 
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.
25
 
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. */
37
 
38
 
39
package gnu.javax.swing.text.html.css;
40
 
41
/**
42
 * Converts CSS length values to usable length values.
43
 *
44
 * @author Roman Kennke (kennke@aicas.com)
45
 */
46
public class Length
47
{
48
 
49
  /**
50
   * The original value.
51
   */
52
  private String value;
53
 
54
  /**
55
   * The converted value.
56
   */
57
  protected float floatValue;
58
 
59
  /**
60
   * Indicates when the value is a percentage value.
61
   */
62
  private boolean isPercentage;
63
 
64
  /**
65
   * Indicates a length value that is relative to the font size (em).
66
   */
67
  private boolean isFontEMRelative;
68
 
69
  /**
70
   * Indicates a length value that is relative to the font size (ex).
71
   */
72
  private boolean isFontEXRelative;
73
 
74
  /**
75
   * The EM base size.
76
   */
77
  private float emBase;
78
 
79
  /**
80
   * The EX base size.
81
   */
82
  private float exBase;
83
 
84
  /**
85
   * Creates a new length converter instance.
86
   *
87
   * @param val the CSS value
88
   */
89
  public Length(String val)
90
  {
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
100
      {
101
        floatValue = 0.0F;
102
        if (i != -1)
103
          {
104
            String sub = value.substring(0, i);
105
            floatValue = Float.parseFloat(sub);
106
          }
107
        else if (percent != -1)
108
          {
109
            isPercentage = true;
110
            String sub = value.substring(0, percent);
111
            floatValue = Float.parseFloat(sub) / 100;
112
          }
113
        else if (em != -1)
114
          {
115
            isFontEMRelative = true;
116
            String sub = value.substring(0, em);
117
            floatValue = Float.parseFloat(sub);
118
          }
119
        else if (ex != -1)
120
          {
121
            isFontEXRelative = true;
122
            String sub = value.substring(0, ex);
123
            floatValue = Float.parseFloat(sub);
124
          }
125
        else
126
          {
127
            floatValue = Float.parseFloat(value);
128
          }
129
      }
130
    catch (NumberFormatException exc)
131
      {
132
        // Don't let such small problems interrupt CSS parsing.
133
        System.err.println("couldn't parse: " + val);
134
      }
135
  }
136
 
137
  /**
138
   * Returns the value converted to pixels.
139
   *
140
   * @return the value converted to pixels
141
   */
142
  public float getValue()
143
  {
144
    return floatValue;
145
  }
146
 
147
  /**
148
   * Returns the absolute span for the case when this length value is
149
   * a relative value.
150
   *
151
   * @param base the base span
152
   *
153
   * @return the absolute span
154
   */
155
  public float getValue(float base)
156
  {
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;
165
  }
166
 
167
  /**
168
   * Sets the font relative EM base.
169
   *
170
   * @param base the font relative EM base
171
   */
172
  public void setEMBase(float base)
173
  {
174
    emBase = base;
175
  }
176
 
177
  /**
178
   * Sets the font relative EX base.
179
   *
180
   * @param base the font relative EX base
181
   */
182
  public void setEXBase(float base)
183
  {
184
    exBase = base;
185
  }
186
 
187
  /**
188
   * Sets the font relative base values.
189
   *
190
   * @param emBase the EM base
191
   * @param exBase the EX base
192
   */
193
  public void setFontBases(float emBase, float exBase)
194
  {
195
    setEMBase(emBase);
196
    setEXBase(exBase);
197
  }
198
 
199
  /**
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.
203
   *
204
   * @return true when this length value is an ex font relative value
205
   */
206
  public boolean isFontEMRelative()
207
  {
208
    return isFontEMRelative;
209
  }
210
 
211
  /**
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.
215
   *
216
   * @return true when this length value is an ex font relative value
217
   */
218
  public boolean isFontEXRelative()
219
  {
220
    return isFontEXRelative;
221
  }
222
 
223
  /**
224
   * Returns <code>true</code> when the length value is a percentage
225
   * value, <code>false</code> otherwise.
226
   *
227
   * @return <code>true</code> when the length value is a percentage
228
   *         value, <code>false</code> otherwise
229
   */
230
  public boolean isPercentage()
231
  {
232
    return isPercentage;
233
  }
234
 
235
  /**
236
   * Checks if the specified value makes up a valid length value.
237
   *
238
   * @param value the value to check
239
   *
240
   * @return <code>true</code> if the value is a valid length
241
   */
242
  public static boolean isValid(String value)
243
  {
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('%');
249
    try
250
      {
251
        if (px != -1)
252
          {
253
            Integer.parseInt(value.substring(0, px));
254
          }
255
        else if (em != -1)
256
          {
257
            Integer.parseInt(value.substring(0, em));
258
          }
259
        else if (ex != -1)
260
          {
261
            Integer.parseInt(value.substring(0, ex));
262
          }
263
        else if (pc != -1)
264
          {
265
            Integer.parseInt(value.substring(0, ex));
266
          }
267
        else
268
          {
269
            Integer.parseInt(value);
270
          }
271
      }
272
    catch (NumberFormatException nfe)
273
      {
274
        isValid = false;
275
      }
276
    return isValid;
277
  }
278
 
279
  public String toString()
280
  {
281
    return value;
282
  }
283
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.