KickJava   Java API By Example, From Geeks To Geeks.

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


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.IdentifierManager;
25 import org.apache.batik.css.engine.value.StringMap;
26 import org.apache.batik.css.engine.value.Value;
27 import org.apache.batik.css.engine.value.ValueConstants;
28 import org.apache.batik.css.engine.value.ValueManager;
29 import org.apache.batik.util.CSSConstants;
30 import org.w3c.css.sac.LexicalUnit;
31 import org.w3c.dom.DOMException JavaDoc;
32 import org.w3c.dom.css.CSSPrimitiveValue;
33
34 /**
35  * This class provides a manager for the 'font-weight' property values.
36  *
37  * @author <a HREF="mailto:stephane@hillion.org">Stephane Hillion</a>
38  * @version $Id: FontWeightManager.java,v 1.7 2005/03/27 08:58:31 cam Exp $
39  */

40 public class FontWeightManager extends IdentifierManager {
41     
42     /**
43      * The identifier values.
44      */

45     protected final static StringMap values = new StringMap();
46     static {
47     values.put(CSSConstants.CSS_ALL_VALUE,
48                    ValueConstants.ALL_VALUE);
49     values.put(CSSConstants.CSS_BOLD_VALUE,
50                    ValueConstants.BOLD_VALUE);
51     values.put(CSSConstants.CSS_BOLDER_VALUE,
52                    ValueConstants.BOLDER_VALUE);
53     values.put(CSSConstants.CSS_LIGHTER_VALUE,
54                    ValueConstants.LIGHTER_VALUE);
55     values.put(CSSConstants.CSS_NORMAL_VALUE,
56                    ValueConstants.NORMAL_VALUE);
57     }
58
59     /**
60      * Implements {@link ValueManager#isInheritedProperty()}.
61      */

62     public boolean isInheritedProperty() {
63     return true;
64     }
65
66     /**
67      * Implements {@link ValueManager#getPropertyName()}.
68      */

69     public String JavaDoc getPropertyName() {
70     return CSSConstants.CSS_FONT_WEIGHT_PROPERTY;
71     }
72     
73     /**
74      * Implements {@link ValueManager#getDefaultValue()}.
75      */

76     public Value getDefaultValue() {
77         return ValueConstants.NORMAL_VALUE;
78     }
79
80     /**
81      * Implements {@link ValueManager#createValue(LexicalUnit,CSSEngine)}.
82      */

83     public Value createValue(LexicalUnit lu, CSSEngine engine)
84         throws DOMException JavaDoc {
85     if (lu.getLexicalUnitType() == LexicalUnit.SAC_INTEGER) {
86         int i = lu.getIntegerValue();
87         switch (i) {
88         case 100:
89         return ValueConstants.NUMBER_100;
90         case 200:
91         return ValueConstants.NUMBER_200;
92         case 300:
93         return ValueConstants.NUMBER_300;
94         case 400:
95         return ValueConstants.NUMBER_400;
96         case 500:
97         return ValueConstants.NUMBER_500;
98         case 600:
99         return ValueConstants.NUMBER_600;
100         case 700:
101         return ValueConstants.NUMBER_700;
102         case 800:
103         return ValueConstants.NUMBER_800;
104         case 900:
105         return ValueConstants.NUMBER_900;
106         }
107             throw createInvalidFloatValueDOMException(i);
108         }
109         return super.createValue(lu, engine);
110     }
111
112     /**
113      * Implements {@link ValueManager#createFloatValue(short,float)}.
114      */

115     public Value createFloatValue(short type, float floatValue)
116         throws DOMException JavaDoc {
117     if (type == CSSPrimitiveValue.CSS_NUMBER) {
118         int i = (int)floatValue;
119         if (floatValue == i) {
120         switch (i) {
121                 case 100:
122                     return ValueConstants.NUMBER_100;
123                 case 200:
124                     return ValueConstants.NUMBER_200;
125                 case 300:
126                     return ValueConstants.NUMBER_300;
127                 case 400:
128                     return ValueConstants.NUMBER_400;
129                 case 500:
130                     return ValueConstants.NUMBER_500;
131                 case 600:
132                     return ValueConstants.NUMBER_600;
133                 case 700:
134                     return ValueConstants.NUMBER_700;
135                 case 800:
136                     return ValueConstants.NUMBER_800;
137                 case 900:
138                     return ValueConstants.NUMBER_900;
139         }
140         }
141     }
142         throw createInvalidFloatValueDOMException(floatValue);
143     }
144
145     /**
146      * Implements {@link
147      * ValueManager#computeValue(CSSStylableElement,String,CSSEngine,int,StyleMap,Value)}.
148      */

149     public Value computeValue(CSSStylableElement elt,
150                               String JavaDoc pseudo,
151                               CSSEngine engine,
152                               int idx,
153                               StyleMap sm,
154                               Value value) {
155         if (value == ValueConstants.BOLDER_VALUE) {
156             sm.putParentRelative(idx, true);
157
158             CSSContext ctx = engine.getCSSContext();
159             CSSStylableElement p = CSSEngine.getParentCSSStylableElement(elt);
160             float fw;
161             if (p == null) {
162                 fw = 400;
163             } else {
164                 Value v = engine.getComputedStyle(p, pseudo, idx);
165                 fw = v.getFloatValue();
166             }
167             return createFontWeight(ctx.getBolderFontWeight(fw));
168         } else if (value == ValueConstants.LIGHTER_VALUE) {
169             sm.putParentRelative(idx, true);
170
171             CSSContext ctx = engine.getCSSContext();
172             CSSStylableElement p = CSSEngine.getParentCSSStylableElement(elt);
173             float fw;
174             if (p == null) {
175                 fw = 400;
176             } else {
177                 Value v = engine.getComputedStyle(p, pseudo, idx);
178                 fw = v.getFloatValue();
179             }
180             return createFontWeight(ctx.getLighterFontWeight(fw));
181         } else if (value == ValueConstants.NORMAL_VALUE) {
182             return ValueConstants.NUMBER_400;
183         } else if (value == ValueConstants.BOLD_VALUE) {
184             return ValueConstants.NUMBER_700;
185         }
186         return value;
187     }
188
189     /**
190      * Returns the CSS value associated with the given font-weight.
191      */

192     protected Value createFontWeight(float f) {
193         switch ((int)f) {
194         case 100:
195             return ValueConstants.NUMBER_100;
196         case 200:
197             return ValueConstants.NUMBER_200;
198         case 300:
199             return ValueConstants.NUMBER_300;
200         case 400:
201             return ValueConstants.NUMBER_400;
202         case 500:
203             return ValueConstants.NUMBER_500;
204         case 600:
205             return ValueConstants.NUMBER_600;
206         case 700:
207             return ValueConstants.NUMBER_700;
208         case 800:
209             return ValueConstants.NUMBER_800;
210         default: // 900
211
return ValueConstants.NUMBER_900;
212         }
213     }
214
215     /**
216      * Implements {@link IdentifierManager#getIdentifiers()}.
217      */

218     public StringMap getIdentifiers() {
219         return values;
220     }
221 }
222
Popular Tags