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

19 package org.apache.batik.css.engine.value.css2;
20
21 import org.apache.batik.css.engine.CSSContext;
22 import org.apache.batik.css.engine.CSSEngine;
23 import org.apache.batik.css.engine.CSSStylableElement;
24 import org.apache.batik.css.engine.StyleMap;
25 import org.apache.batik.css.engine.value.IdentifierManager;
26 import org.apache.batik.css.engine.value.StringMap;
27 import org.apache.batik.css.engine.value.Value;
28 import org.apache.batik.css.engine.value.ValueConstants;
29 import org.apache.batik.css.engine.value.ValueManager;
30 import org.apache.batik.util.CSSConstants;
31 import org.apache.batik.util.SVGTypes;
32
33 import org.w3c.css.sac.LexicalUnit;
34 import org.w3c.dom.DOMException;
35 import org.w3c.dom.css.CSSPrimitiveValue;
36
37 /**
38  * This class provides a manager for the 'font-weight' property values.
39  *
40  * @author <a href="mailto:stephane@hillion.org">Stephane Hillion</a>
41  * @version $Id: FontWeightManager.java 1733416 2016-03-03 07:07:13Z gadams $
42  */

43 public class FontWeightManager extends IdentifierManager {
44
45     /**
46      * The identifier values.
47      */

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

65     public boolean isInheritedProperty() {
66         return true;
67     }
68
69     /**
70      * Implements {@link ValueManager#isAnimatableProperty()}.
71      */

72     public boolean isAnimatableProperty() {
73         return true;
74     }
75
76     /**
77      * Implements {@link ValueManager#isAdditiveProperty()}.
78      */

79     public boolean isAdditiveProperty() {
80         return false;
81     }
82
83     /**
84      * Implements {@link ValueManager#getPropertyType()}.
85      */

86     public int getPropertyType() {
87         return SVGTypes.TYPE_FONT_WEIGHT_VALUE;
88     }
89
90     /**
91      * Implements {@link ValueManager#getPropertyName()}.
92      */

93     public String getPropertyName() {
94         return CSSConstants.CSS_FONT_WEIGHT_PROPERTY;
95     }
96
97     /**
98      * Implements {@link ValueManager#getDefaultValue()}.
99      */

100     public Value getDefaultValue() {
101         return ValueConstants.NORMAL_VALUE;
102     }
103
104     /**
105      * Implements {@link ValueManager#createValue(LexicalUnit,CSSEngine)}.
106      */

107     public Value createValue(LexicalUnit lu, CSSEngine engine)
108         throws DOMException {
109         if (lu.getLexicalUnitType() == LexicalUnit.SAC_INTEGER) {
110             int i = lu.getIntegerValue();
111             switch (i) {
112             case 100:
113                 return ValueConstants.NUMBER_100;
114             case 200:
115                 return ValueConstants.NUMBER_200;
116             case 300:
117                 return ValueConstants.NUMBER_300;
118             case 400:
119                 return ValueConstants.NUMBER_400;
120             case 500:
121                 return ValueConstants.NUMBER_500;
122             case 600:
123                 return ValueConstants.NUMBER_600;
124             case 700:
125                 return ValueConstants.NUMBER_700;
126             case 800:
127                 return ValueConstants.NUMBER_800;
128             case 900:
129                 return ValueConstants.NUMBER_900;
130             }
131             throw createInvalidFloatValueDOMException(i);
132         }
133         return super.createValue(lu, engine);
134     }
135
136     /**
137      * Implements {@link ValueManager#createFloatValue(short,float)}.
138      */

139     public Value createFloatValue(short type, float floatValue)
140         throws DOMException {
141         if (type == CSSPrimitiveValue.CSS_NUMBER) {
142             int i = (int)floatValue;
143             if (floatValue == i) {
144                 switch (i) {
145                 case 100:
146                     return ValueConstants.NUMBER_100;
147                 case 200:
148                     return ValueConstants.NUMBER_200;
149                 case 300:
150                     return ValueConstants.NUMBER_300;
151                 case 400:
152                     return ValueConstants.NUMBER_400;
153                 case 500:
154                     return ValueConstants.NUMBER_500;
155                 case 600:
156                     return ValueConstants.NUMBER_600;
157                 case 700:
158                     return ValueConstants.NUMBER_700;
159                 case 800:
160                     return ValueConstants.NUMBER_800;
161                 case 900:
162                     return ValueConstants.NUMBER_900;
163                 }
164             }
165         }
166         throw createInvalidFloatValueDOMException(floatValue);
167     }
168
169     /**
170      * Implements {@link
171      * ValueManager#computeValue(CSSStylableElement,String,CSSEngine,int,StyleMap,Value)}.
172      */

173     public Value computeValue(CSSStylableElement elt,
174                               String pseudo,
175                               CSSEngine engine,
176                               int idx,
177                               StyleMap sm,
178                               Value value) {
179         if (value == ValueConstants.BOLDER_VALUE) {
180             sm.putParentRelative(idx, true);
181
182             CSSContext ctx = engine.getCSSContext();
183             CSSStylableElement p = CSSEngine.getParentCSSStylableElement(elt);
184             float fw;
185             if (p == null) {
186                 fw = 400;
187             } else {
188                 Value v = engine.getComputedStyle(p, pseudo, idx);
189                 fw = v.getFloatValue();
190             }
191             return createFontWeight(ctx.getBolderFontWeight(fw));
192         } else if (value == ValueConstants.LIGHTER_VALUE) {
193             sm.putParentRelative(idx, true);
194
195             CSSContext ctx = engine.getCSSContext();
196             CSSStylableElement p = CSSEngine.getParentCSSStylableElement(elt);
197             float fw;
198             if (p == null) {
199                 fw = 400;
200             } else {
201                 Value v = engine.getComputedStyle(p, pseudo, idx);
202                 fw = v.getFloatValue();
203             }
204             return createFontWeight(ctx.getLighterFontWeight(fw));
205         } else if (value == ValueConstants.NORMAL_VALUE) {
206             return ValueConstants.NUMBER_400;
207         } else if (value == ValueConstants.BOLD_VALUE) {
208             return ValueConstants.NUMBER_700;
209         }
210         return value;
211     }
212
213     /**
214      * Returns the CSS value associated with the given font-weight.
215      */

216     protected Value createFontWeight(float f) {
217         switch ((int)f) {
218         case 100:
219             return ValueConstants.NUMBER_100;
220         case 200:
221             return ValueConstants.NUMBER_200;
222         case 300:
223             return ValueConstants.NUMBER_300;
224         case 400:
225             return ValueConstants.NUMBER_400;
226         case 500:
227             return ValueConstants.NUMBER_500;
228         case 600:
229             return ValueConstants.NUMBER_600;
230         case 700:
231             return ValueConstants.NUMBER_700;
232         case 800:
233             return ValueConstants.NUMBER_800;
234         default// 900
235             return ValueConstants.NUMBER_900;
236         }
237     }
238
239     /**
240      * Implements {@link IdentifierManager#getIdentifiers()}.
241      */

242     public StringMap getIdentifiers() {
243         return values;
244     }
245 }
246