KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2
3    Copyright 2004 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
19 package org.apache.batik.css.engine.value.css2;
20
21 import java.util.HashSet JavaDoc;
22 import java.util.Set JavaDoc;
23
24 import org.apache.batik.css.engine.CSSEngine;
25 import org.apache.batik.css.engine.value.ValueManager;
26 import org.apache.batik.css.engine.value.IdentifierManager;
27 import org.apache.batik.css.engine.value.AbstractValueFactory;
28 import org.apache.batik.css.engine.value.ShorthandManager;
29 import org.apache.batik.css.engine.value.StringMap;
30 import org.apache.batik.css.parser.CSSLexicalUnit;
31 import org.apache.batik.util.CSSConstants;
32 import org.w3c.css.sac.LexicalUnit;
33 import org.w3c.dom.DOMException JavaDoc;
34
35 /**
36  * This class provides support for the CSS2 'font' shorthand property.
37  *
38  * The form of this property is:
39  * [ [ <font-style> || <font-variant> || <font-weight> ]?
40  * <font-size> [ / <line-height> ]? <font-family> ] |
41  * caption | icon | menu | message-box | small-caption |
42  * status-bar | inherit
43  *
44  * It is worth noting that there is a potential ambiguity
45  * between font-size and font-weight since in SVG they can both
46  * be unitless. This is solved by considering the 'last' number
47  * before an 'ident' or '/' to be font-size and any preceeding
48  * number to be font-weight.
49  *
50  * @author <a HREF="mailto:deweese@apache.org">deweese</a>
51  * @version $Id: FontShorthandManager.java,v 1.2 2005/03/27 08:58:31 cam Exp $
52  */

53 public class FontShorthandManager
54     extends AbstractValueFactory
55     implements ShorthandManager {
56
57     public FontShorthandManager() { }
58
59     /**
60      * Implements {@link ValueManager#getPropertyName()}.
61      */

62     public String JavaDoc getPropertyName() {
63     return CSSConstants.CSS_FONT_PROPERTY;
64     }
65
66     static LexicalUnit NORMAL_LU = CSSLexicalUnit.createString
67         (LexicalUnit.SAC_IDENT, CSSConstants.CSS_NORMAL_VALUE, null);
68     static LexicalUnit BOLD_LU = CSSLexicalUnit.createString
69         (LexicalUnit.SAC_IDENT, CSSConstants.CSS_BOLD_VALUE, null);
70     
71     static LexicalUnit MEDIUM_LU = CSSLexicalUnit.createString
72         (LexicalUnit.SAC_IDENT, CSSConstants.CSS_MEDIUM_VALUE, null);
73
74     static LexicalUnit SZ_10PT_LU = CSSLexicalUnit.createFloat
75         (LexicalUnit.SAC_POINT, 10, null);
76     static LexicalUnit SZ_8PT_LU = CSSLexicalUnit.createFloat
77         (LexicalUnit.SAC_POINT, 8, null);
78
79
80     static LexicalUnit FONT_FAMILY_LU;
81     static {
82         LexicalUnit lu;
83         FONT_FAMILY_LU = CSSLexicalUnit.createString
84             (LexicalUnit.SAC_IDENT, "Dialog", null);
85         lu = CSSLexicalUnit.createString
86             (LexicalUnit.SAC_IDENT, "Helvetica", FONT_FAMILY_LU);
87         CSSLexicalUnit.createString
88             (LexicalUnit.SAC_IDENT,
89              CSSConstants.CSS_SANS_SERIF_VALUE, lu);
90     }
91
92     protected final static Set JavaDoc values = new HashSet JavaDoc();
93     static {
94     values.add(CSSConstants.CSS_CAPTION_VALUE);
95     values.add(CSSConstants.CSS_ICON_VALUE);
96     values.add(CSSConstants.CSS_MENU_VALUE);
97     values.add(CSSConstants.CSS_MESSAGE_BOX_VALUE);
98     values.add(CSSConstants.CSS_SMALL_CAPTION_VALUE);
99     values.add(CSSConstants.CSS_STATUS_BAR_VALUE);
100     }
101
102     public void handleSystemFont(CSSEngine eng,
103                                  ShorthandManager.PropertyHandler ph,
104                                  String JavaDoc s,
105                                  boolean imp) {
106         
107         LexicalUnit fontStyle = NORMAL_LU;
108         LexicalUnit fontVariant = NORMAL_LU;
109         LexicalUnit fontWeight = NORMAL_LU;
110         LexicalUnit lineHeight = NORMAL_LU;
111         LexicalUnit fontFamily = FONT_FAMILY_LU;
112
113         LexicalUnit fontSize;
114         if (s.equals(CSSConstants.CSS_SMALL_CAPTION_VALUE)) {
115             fontSize = SZ_8PT_LU;
116         } else {
117             fontSize = SZ_10PT_LU;
118         }
119         ph.property(CSSConstants.CSS_FONT_FAMILY_PROPERTY, fontFamily, imp);
120         ph.property(CSSConstants.CSS_FONT_STYLE_PROPERTY, fontStyle, imp);
121         ph.property(CSSConstants.CSS_FONT_VARIANT_PROPERTY, fontVariant, imp);
122         ph.property(CSSConstants.CSS_FONT_WEIGHT_PROPERTY, fontWeight, imp);
123         ph.property(CSSConstants.CSS_FONT_SIZE_PROPERTY, fontSize, imp);
124         ph.property(CSSConstants.CSS_LINE_HEIGHT_PROPERTY, lineHeight, imp);
125     }
126
127     /**
128      * Implements {@link ShorthandManager#setValues(CSSEngine,ShorthandManager.PropertyHandler,LexicalUnit,boolean)}.
129      */

130     public void setValues(CSSEngine eng,
131                           ShorthandManager.PropertyHandler ph,
132                           LexicalUnit lu,
133                           boolean imp) {
134         switch (lu.getLexicalUnitType()) {
135         case LexicalUnit.SAC_INHERIT: return;
136         case LexicalUnit.SAC_IDENT: {
137             String JavaDoc s= lu.getStringValue().toLowerCase();
138             if (values.contains(s)) {
139                 handleSystemFont(eng, ph, s, imp);
140                 return;
141             }
142         }
143         }
144
145         LexicalUnit fontStyle = null;
146         LexicalUnit fontVariant = null;
147         LexicalUnit fontWeight = null;
148         LexicalUnit fontSize = null;
149         LexicalUnit lineHeight = null;
150         LexicalUnit fontFamily = null;
151
152         ValueManager[]vMgrs = eng.getValueManagers();
153         int fst, fv, fw, fsz, lh, ff;
154         fst = eng.getPropertyIndex(CSSConstants.CSS_FONT_STYLE_PROPERTY);
155         fv = eng.getPropertyIndex(CSSConstants.CSS_FONT_VARIANT_PROPERTY);
156         fw = eng.getPropertyIndex(CSSConstants.CSS_FONT_WEIGHT_PROPERTY);
157         fsz = eng.getPropertyIndex(CSSConstants.CSS_FONT_SIZE_PROPERTY);
158         lh = eng.getPropertyIndex(CSSConstants.CSS_LINE_HEIGHT_PROPERTY);
159         ff = eng.getPropertyIndex(CSSConstants.CSS_FONT_FAMILY_PROPERTY);
160
161         IdentifierManager fstVM = (IdentifierManager)vMgrs[fst];
162         IdentifierManager fvVM = (IdentifierManager)vMgrs[fv];
163         IdentifierManager fwVM = (IdentifierManager)vMgrs[fw];
164         FontSizeManager fszVM = (FontSizeManager)vMgrs[fsz];
165         ValueManager ffVM = vMgrs[ff];
166
167         StringMap fstSM = fstVM.getIdentifiers();
168         StringMap fvSM = fvVM.getIdentifiers();
169         StringMap fwSM = fwVM.getIdentifiers();
170         StringMap fszSM = fszVM.getIdentifiers();
171
172
173         // Check for font-style, font-varient, & font-weight
174
// These are all optional.
175

176         boolean svwDone= false;
177         LexicalUnit intLU = null;;
178         while (!svwDone && (lu != null)) {
179             switch (lu.getLexicalUnitType()) {
180             case LexicalUnit.SAC_IDENT: {
181                 String JavaDoc s= lu.getStringValue().toLowerCase().intern();
182                 if ((fontStyle == null) && (fstSM.get(s) != null)) {
183                     fontStyle = lu;
184                     if (intLU != null) {
185                         if (fontWeight == null) {
186                             fontWeight = intLU;
187                             intLU = null;
188                         } else
189                             throw createInvalidLexicalUnitDOMException
190                                 (intLU.getLexicalUnitType());
191                     }
192                     break;
193                 }
194
195                 if ((fontVariant == null) && (fvSM.get(s) != null)) {
196                     fontVariant = lu;
197                     if (intLU != null) {
198                         if (fontWeight == null) {
199                             fontWeight = intLU;
200                             intLU = null;
201                         } else
202                             throw createInvalidLexicalUnitDOMException
203                                 (intLU.getLexicalUnitType());
204                     }
205                     break;
206                 }
207
208                 if ((intLU == null) && (fontWeight == null) &&
209                     (fwSM.get(s) != null)) {
210                     fontWeight = lu;
211                     break;
212                 }
213
214                 svwDone = true;
215                 break;
216             }
217             case LexicalUnit.SAC_INTEGER:
218                 if ((intLU == null) && (fontWeight == null)) {
219                     intLU = lu;
220                     break;
221                 }
222                 svwDone = true;
223                 break;
224
225             default: // All other must be size,'/line-height', family
226
svwDone = true;
227                 break;
228             }
229             if (!svwDone) lu = lu.getNextLexicalUnit();
230         }
231
232         // Must have font-size.
233
if (lu == null)
234             throw createMalformedLexicalUnitDOMException();
235
236         // Now we need to get font-size
237
switch (lu.getLexicalUnitType()) {
238         case LexicalUnit.SAC_IDENT: {
239             String JavaDoc s= lu.getStringValue().toLowerCase().intern();
240             if (fszSM.get(s) != null) {
241                 fontSize = lu; // This is a font-size ident.
242
lu = lu.getNextLexicalUnit();
243             }
244         }
245             break;
246
247     case LexicalUnit.SAC_EM:
248     case LexicalUnit.SAC_EX:
249     case LexicalUnit.SAC_PIXEL:
250     case LexicalUnit.SAC_CENTIMETER:
251     case LexicalUnit.SAC_MILLIMETER:
252     case LexicalUnit.SAC_INCH:
253     case LexicalUnit.SAC_POINT:
254     case LexicalUnit.SAC_PICA:
255     case LexicalUnit.SAC_INTEGER:
256     case LexicalUnit.SAC_REAL:
257     case LexicalUnit.SAC_PERCENTAGE:
258             fontSize = lu;
259             lu = lu.getNextLexicalUnit();
260             break;
261         }
262         
263
264         if (fontSize == null) {
265             // We must have a font-size so see if we can use intLU...
266
if (intLU != null) {
267                 fontSize = intLU; // Yup!
268
intLU = null;
269             } else {
270                 throw createInvalidLexicalUnitDOMException
271                     (lu.getLexicalUnitType());
272             }
273         }
274
275         if (intLU != null) {
276             // We have a intLU left see if we can use it as font-weight
277
if (fontWeight == null) {
278                 fontWeight = intLU; // use intLU as font-weight.
279
} else {
280                 // we have an 'extra' integer in property.
281
throw createInvalidLexicalUnitDOMException
282                     (intLU.getLexicalUnitType());
283             }
284         }
285         
286         // Must have Font-Family, so if it's null now we are done!
287
if (lu == null)
288             throw createMalformedLexicalUnitDOMException();
289
290         // Now at this point we want to look for
291
// line-height.
292
switch (lu.getLexicalUnitType()) {
293         case LexicalUnit.SAC_OPERATOR_SLASH: // we have line-height
294
lu = lu.getNextLexicalUnit();
295             if (lu == null) // OOPS!
296
throw createMalformedLexicalUnitDOMException();
297             lineHeight = lu;
298             lu = lu.getNextLexicalUnit();
299             break;
300         }
301
302         // Must have Font-Family, so if it's null now we are done!
303
if (lu == null)
304             throw createMalformedLexicalUnitDOMException();
305         fontFamily = lu;
306
307         if (fontStyle == null) fontStyle = NORMAL_LU;
308         if (fontVariant == null) fontVariant = NORMAL_LU;
309         if (fontWeight == null) fontWeight = NORMAL_LU;
310         if (lineHeight == null) lineHeight = NORMAL_LU;
311
312         ph.property(CSSConstants.CSS_FONT_FAMILY_PROPERTY, fontFamily, imp);
313         ph.property(CSSConstants.CSS_FONT_STYLE_PROPERTY, fontStyle, imp);
314         ph.property(CSSConstants.CSS_FONT_VARIANT_PROPERTY, fontVariant, imp);
315         ph.property(CSSConstants.CSS_FONT_WEIGHT_PROPERTY, fontWeight, imp);
316         ph.property(CSSConstants.CSS_FONT_SIZE_PROPERTY, fontSize, imp);
317         if (lh!=-1)
318             ph.property(CSSConstants.CSS_LINE_HEIGHT_PROPERTY,
319                         lineHeight, imp);
320     }
321 }
322
Popular Tags