KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > batik > css > engine > value > css2 > FontSizeManager


1 /*
2
3    Copyright 2002-2003 The Apache Software Foundation
4
5    Licensed under the Apache License, Version 2.0 (the "License");
6    you may not use this file except in compliance with the License.
7    You may obtain a copy of the License at
8
9        http://www.apache.org/licenses/LICENSE-2.0
10
11    Unless required by applicable law or agreed to in writing, software
12    distributed under the License is distributed on an "AS IS" BASIS,
13    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14    See the License for the specific language governing permissions and
15    limitations under the License.
16
17  */

18 package org.apache.batik.css.engine.value.css2;
19
20 import org.apache.batik.css.engine.CSSContext;
21 import org.apache.batik.css.engine.CSSEngine;
22 import org.apache.batik.css.engine.CSSStylableElement;
23 import org.apache.batik.css.engine.StyleMap;
24 import org.apache.batik.css.engine.value.FloatValue;
25 import org.apache.batik.css.engine.value.IdentifierManager;
26 import org.apache.batik.css.engine.value.LengthManager;
27 import org.apache.batik.css.engine.value.StringMap;
28 import org.apache.batik.css.engine.value.Value;
29 import org.apache.batik.css.engine.value.ValueConstants;
30 import org.apache.batik.css.engine.value.ValueManager;
31 import org.apache.batik.util.CSSConstants;
32 import org.w3c.css.sac.LexicalUnit;
33 import org.w3c.dom.DOMException JavaDoc;
34 import org.w3c.dom.css.CSSPrimitiveValue;
35
36 /**
37  * This class provides a manager for the 'font-size' property values.
38  *
39  * @author <a HREF="mailto:stephane@hillion.org">Stephane Hillion</a>
40  * @version $Id: FontSizeManager.java,v 1.11 2005/03/27 08:58:31 cam Exp $
41  */

42 public class FontSizeManager extends LengthManager {
43     
44     /**
45      * The identifier values.
46      */

47     protected final static StringMap values = new StringMap();
48     static {
49         values.put(CSSConstants.CSS_ALL_VALUE,
50                    ValueConstants.ALL_VALUE);
51         values.put(CSSConstants.CSS_LARGE_VALUE,
52                    ValueConstants.LARGE_VALUE);
53         values.put(CSSConstants.CSS_LARGER_VALUE,
54                    ValueConstants.LARGER_VALUE);
55         values.put(CSSConstants.CSS_MEDIUM_VALUE,
56                    ValueConstants.MEDIUM_VALUE);
57         values.put(CSSConstants.CSS_SMALL_VALUE,
58                    ValueConstants.SMALL_VALUE);
59         values.put(CSSConstants.CSS_SMALLER_VALUE,
60                    ValueConstants.SMALLER_VALUE);
61         values.put(CSSConstants.CSS_X_LARGE_VALUE,
62                    ValueConstants.X_LARGE_VALUE);
63         values.put(CSSConstants.CSS_X_SMALL_VALUE,
64                    ValueConstants.X_SMALL_VALUE);
65         values.put(CSSConstants.CSS_XX_LARGE_VALUE,
66                    ValueConstants.XX_LARGE_VALUE);
67         values.put(CSSConstants.CSS_XX_SMALL_VALUE,
68                    ValueConstants.XX_SMALL_VALUE);
69     }
70
71     /**
72      * Implements {@link IdentifierManager#getIdentifiers()}.
73      */

74     public StringMap getIdentifiers() {
75         return values;
76     }
77
78     /**
79      * Implements {@link ValueManager#isInheritedProperty()}.
80      */

81     public boolean isInheritedProperty() {
82     return true;
83     }
84
85     /**
86      * Implements {@link ValueManager#getPropertyName()}.
87      */

88     public String JavaDoc getPropertyName() {
89     return CSSConstants.CSS_FONT_SIZE_PROPERTY;
90     }
91     
92     /**
93      * Implements {@link ValueManager#getDefaultValue()}.
94      */

95     public Value getDefaultValue() {
96         return ValueConstants.MEDIUM_VALUE;
97     }
98
99     /**
100      * Implements {@link ValueManager#createValue(LexicalUnit,CSSEngine)}.
101      */

102     public Value createValue(LexicalUnit lu, CSSEngine engine)
103         throws DOMException JavaDoc {
104     switch (lu.getLexicalUnitType()) {
105     case LexicalUnit.SAC_INHERIT:
106         return ValueConstants.INHERIT_VALUE;
107
108     case LexicalUnit.SAC_IDENT:
109             String JavaDoc s = lu.getStringValue().toLowerCase().intern();
110             Object JavaDoc v = values.get(s);
111             if (v == null) {
112                 throw createInvalidIdentifierDOMException(s);
113             }
114             return (Value)v;
115         default:
116             break;
117         }
118         return super.createValue(lu, engine);
119     }
120
121     /**
122      * Implements {@link
123      * ValueManager#createStringValue(short,String,CSSEngine)}.
124      */

125     public Value createStringValue(short type, String JavaDoc value, CSSEngine engine)
126         throws DOMException JavaDoc {
127         if (type != CSSPrimitiveValue.CSS_IDENT) {
128             throw createInvalidStringTypeDOMException(type);
129         }
130         Object JavaDoc v = values.get(value.toLowerCase().intern());
131         if (v == null) {
132             throw createInvalidIdentifierDOMException(value);
133         }
134         return (Value)v;
135     }
136
137     /**
138      * Implements {@link
139      * ValueManager#computeValue(CSSStylableElement,String,CSSEngine,int,StyleMap,Value)}.
140      */

141     public Value computeValue(CSSStylableElement elt,
142                               String JavaDoc pseudo,
143                               CSSEngine engine,
144                               int idx,
145                               StyleMap sm,
146                               Value value) {
147         float scale = 1.0f;
148         boolean doParentRelative = false;
149
150         switch (value.getPrimitiveType()) {
151         case CSSPrimitiveValue.CSS_NUMBER:
152         case CSSPrimitiveValue.CSS_PX:
153             return value;
154
155         case CSSPrimitiveValue.CSS_MM:
156             CSSContext ctx = engine.getCSSContext();
157             float v = value.getFloatValue();
158             return new FloatValue(CSSPrimitiveValue.CSS_NUMBER,
159                                   v / ctx.getPixelUnitToMillimeter());
160
161         case CSSPrimitiveValue.CSS_CM:
162             ctx = engine.getCSSContext();
163             v = value.getFloatValue();
164             return new FloatValue(CSSPrimitiveValue.CSS_NUMBER,
165                                   v * 10f / ctx.getPixelUnitToMillimeter());
166
167         case CSSPrimitiveValue.CSS_IN:
168             ctx = engine.getCSSContext();
169             v = value.getFloatValue();
170             return new FloatValue(CSSPrimitiveValue.CSS_NUMBER,
171                                   v * 25.4f / ctx.getPixelUnitToMillimeter());
172
173         case CSSPrimitiveValue.CSS_PT:
174             ctx = engine.getCSSContext();
175             v = value.getFloatValue();
176             return new FloatValue(CSSPrimitiveValue.CSS_NUMBER,
177                                   v * 25.4f /
178                                   (72f * ctx.getPixelUnitToMillimeter()));
179
180         case CSSPrimitiveValue.CSS_PC:
181             ctx = engine.getCSSContext();
182             v = value.getFloatValue();
183             return new FloatValue(CSSPrimitiveValue.CSS_NUMBER,
184                                   (v * 25.4f /
185                                    (6f * ctx.getPixelUnitToMillimeter())));
186
187         case CSSPrimitiveValue.CSS_EMS:
188             doParentRelative = true;
189             scale = value.getFloatValue();
190             break;
191         case CSSPrimitiveValue.CSS_EXS:
192             doParentRelative = true;
193             scale = value.getFloatValue()*0.5f; // !!! x-height
194
break;
195         case CSSPrimitiveValue.CSS_PERCENTAGE:
196             doParentRelative = true;
197             scale = value.getFloatValue()*0.01f;
198             break;
199         default:
200         }
201
202         if (value == ValueConstants.LARGER_VALUE) {
203             doParentRelative = true;
204             scale = 1.2f;
205         } else if (value == ValueConstants.SMALLER_VALUE) {
206             doParentRelative = true;
207             scale = 1/1.2f;
208         }
209
210         if (doParentRelative) {
211             sm.putParentRelative(idx, true);
212
213             CSSStylableElement p;
214             p = CSSEngine.getParentCSSStylableElement(elt);
215             float fs;
216             if (p == null) {
217                 CSSContext ctx = engine.getCSSContext();
218                 fs = ctx.getMediumFontSize();
219             } else {
220                 fs = engine.getComputedStyle(p, null, idx).getFloatValue();
221             }
222             return new FloatValue(CSSPrimitiveValue.CSS_NUMBER, fs * scale);
223         }
224         
225         // absolute identifiers
226
CSSContext ctx = engine.getCSSContext();
227         float fs = ctx.getMediumFontSize();
228         String JavaDoc s = value.getStringValue();
229         switch (s.charAt(0)) {
230         case 'm':
231             break;
232
233         case 's':
234             fs = (float)(fs / 1.2);
235             break;
236
237         case 'l':
238             fs = (float)(fs * 1.2);
239             break;
240
241         default: // 'x'
242
switch (s.charAt(1)) {
243             case 'x':
244                 switch (s.charAt(3)) {
245                 case 's':
246                     fs = (float)(((fs / 1.2) / 1.2) / 1.2);
247                     break;
248
249                 default: // 'l'
250
fs = (float)(fs * 1.2 * 1.2 * 1.2);
251                 }
252                 break;
253
254             default: // '-'
255
switch (s.charAt(2)) {
256                 case 's':
257                     fs = (float)((fs / 1.2) / 1.2);
258                     break;
259
260                 default: // 'l'
261
fs = (float)(fs * 1.2 * 1.2);
262                 }
263             }
264         }
265         return new FloatValue(CSSPrimitiveValue.CSS_NUMBER, fs);
266     }
267
268
269     /**
270      * Indicates the orientation of the property associated with
271      * this manager.
272      */

273     protected int getOrientation() {
274         return VERTICAL_ORIENTATION; // Not used
275
}
276 }
277
Popular Tags