Initialize
[sdk/ide/product.git] / org.eclipse.jst.pagedesigner / src / org / eclipse / jst / pagedesigner / css2 / property / FontSizeMeta.java
1 /*******************************************************************************\r
2  * Copyright (c) 2006 Sybase, Inc. and others.\r
3  *\r
4  * All rights reserved. This program and the accompanying materials\r
5  * are made available under the terms of the Eclipse Public License v1.0\r
6  * which accompanies this distribution, and is available at\r
7  * http://www.eclipse.org/legal/epl-v10.html\r
8  *\r
9  * Contributors:\r
10  *     Sybase, Inc. - initial API and implementation\r
11  *******************************************************************************/\r
12 package org.eclipse.jst.pagedesigner.css2.property;\r
13 \r
14 import org.eclipse.jst.pagedesigner.IHTMLConstants;\r
15 import org.eclipse.jst.pagedesigner.css2.ICSSStyle;\r
16 import org.eclipse.jst.pagedesigner.css2.font.ICSSFont;\r
17 import org.eclipse.jst.pagedesigner.css2.value.Length;\r
18 import org.eclipse.jst.pagedesigner.utils.DOMUtil;\r
19 import org.w3c.dom.Element;\r
20 import org.w3c.dom.css.CSSValue;\r
21 \r
22 /**\r
23  * The result value should be Length object only.\r
24  * \r
25  * @author mengbo\r
26  */\r
27 // XXX: in the future, we may add "unit" into Length, the one unit will be\r
28 // "MEDIUM"\r
29 // and specific for font. Means the relative size to MEDIUM. Then it is possible\r
30 // for\r
31 // the caller to decide the actual font size based on the style specific MEDIUM\r
32 // size.\r
33 public class FontSizeMeta extends LengthMeta {\r
34         private static final String[] KEYWORDS = { ICSSPropertyID.VAL_XX_SMALL,\r
35                         ICSSPropertyID.VAL_X_SMALL, ICSSPropertyID.VAL_SMALL,\r
36                         ICSSPropertyID.VAL_MEDIUM, ICSSPropertyID.VAL_LARGE,\r
37                         ICSSPropertyID.VAL_X_LARGE, ICSSPropertyID.VAL_XX_LARGE,\r
38                         ICSSPropertyID.VAL_LARGER, ICSSPropertyID.VAL_SMALLER };\r
39 \r
40         /**\r
41          * font size\r
42          */\r
43         public static final double MEDIUM_VAL_INT = 16;\r
44 \r
45         private static final double SCALING_FACTOR = 1.2;\r
46 \r
47         private static final double FACTORS[] = { 0.6, 0.89, 1, 1.2, 1.5, 2.0, 3.0 };\r
48 \r
49         // The scaling factors in IE is different from CSS definition. Here we\r
50         // follow IE.\r
51         private static final double CSS_ABSOLUTE_FACTORS[] = { 0.63, 0.82, 1, 1.12,\r
52                         1.5, 2.0, 3.0 };\r
53 \r
54         private static final int MIN_SIZE_FOR_SMALLER = 1;\r
55 \r
56         private static final int MIN_SIZE_FOR_LARGER = 9;\r
57 \r
58         /**\r
59          * Default constructor\r
60          */\r
61         public FontSizeMeta() {\r
62                 super(true, new Length((int) MEDIUM_VAL_INT, false));\r
63         }\r
64 \r
65         /*\r
66          * (non-Javadoc)\r
67          * \r
68          * @see org.eclipse.jst.pagedesigner.css2.property.LengthMeta#getBaseFont(org.eclipse.jst.pagedesigner.css2.property.AbstractStyle)\r
69          */\r
70         protected ICSSFont getBaseFont(ICSSStyle style) {\r
71                 return style.getParentStyle().getCSSFont();\r
72         }\r
73 \r
74         /*\r
75          * (non-Javadoc)\r
76          * \r
77          * @see org.eclipse.jst.pagedesigner.css2.property.CSSPropertyMeta#getKeywordValues()\r
78          */\r
79         protected String[] getKeywordValues() {\r
80                 return KEYWORDS;\r
81         }\r
82 \r
83         /*\r
84          * (non-Javadoc)\r
85          * \r
86          * @see org.eclipse.jst.pagedesigner.css2.property.CSSPropertyMeta#getPercentageType()\r
87          */\r
88         public int getPercentageType() {\r
89                 return PERCENTAGE_FONT;\r
90         }\r
91 \r
92         /*\r
93          * (non-Javadoc)\r
94          * \r
95          * @see org.eclipse.jst.pagedesigner.css2.property.LengthMeta#calculateCSSValueResult(org.w3c.dom.css.CSSValue,\r
96          *      java.lang.String,\r
97          *      org.eclipse.jst.pagedesigner.css2.style.AbstractStyle)\r
98          */\r
99         public Object calculateCSSValueResult(CSSValue value, String propertyName,\r
100                         ICSSStyle style) {\r
101                 Object obj = super.calculateCSSValueResult(value, propertyName, style);\r
102                 if (obj instanceof Length) {\r
103                         return obj;\r
104                 }\r
105                 // otherwise, it is keyword.\r
106                 return convertKeyword(obj, style);\r
107         }\r
108 \r
109         /**\r
110          * @param fontsizeobj\r
111          * @param style\r
112          * @return the length\r
113          */\r
114         protected Length convertKeyword(Object fontsizeobj, ICSSStyle style) {\r
115                 int fontsize;\r
116                 int parentfontsize = style.getParentStyle().getCSSFont().getFontSize();\r
117                 if (fontsizeobj instanceof Length) {\r
118                         fontsize = ((Length) fontsizeobj).getValue();\r
119                 } else if (ICSSPropertyID.VAL_XX_SMALL.equals(fontsizeobj)) {\r
120                         fontsize = (int) (MEDIUM_VAL_INT * CSS_ABSOLUTE_FACTORS[0]);\r
121                 } else if (ICSSPropertyID.VAL_X_SMALL.equals(fontsizeobj)) {\r
122                         fontsize = (int) (MEDIUM_VAL_INT * CSS_ABSOLUTE_FACTORS[1]);\r
123                 } else if (ICSSPropertyID.VAL_SMALL.equals(fontsizeobj)) {\r
124                         fontsize = (int) (MEDIUM_VAL_INT * CSS_ABSOLUTE_FACTORS[2]);\r
125                 } else if (ICSSPropertyID.VAL_MEDIUM.equals(fontsizeobj)) {\r
126                         fontsize = (int) Math.round(MEDIUM_VAL_INT\r
127                                         * CSS_ABSOLUTE_FACTORS[3]);\r
128                 } else if (ICSSPropertyID.VAL_LARGE.equals(fontsizeobj)) {\r
129                         fontsize = (int) (MEDIUM_VAL_INT * CSS_ABSOLUTE_FACTORS[4]);\r
130                 } else if (ICSSPropertyID.VAL_X_LARGE.equals(fontsizeobj)) {\r
131                         fontsize = (int) (MEDIUM_VAL_INT * CSS_ABSOLUTE_FACTORS[5]);\r
132                 } else if (ICSSPropertyID.VAL_XX_LARGE.equals(fontsizeobj)) {\r
133                         fontsize = (int) (MEDIUM_VAL_INT * CSS_ABSOLUTE_FACTORS[6]);\r
134                 } else if (ICSSPropertyID.VAL_SMALLER.equals(fontsizeobj)) {\r
135                         fontsize = (int) (parentfontsize / SCALING_FACTOR);\r
136                         if (fontsize < MIN_SIZE_FOR_SMALLER) {\r
137                                 fontsize = MIN_SIZE_FOR_SMALLER;\r
138                         }\r
139                 } else if (ICSSPropertyID.VAL_LARGER.equals(fontsizeobj)) {\r
140                         fontsize = (int) (parentfontsize * SCALING_FACTOR);\r
141                         if (fontsize < MIN_SIZE_FOR_LARGER) {\r
142                                 fontsize = MIN_SIZE_FOR_LARGER;\r
143                         }\r
144                 } else {\r
145                         fontsize = parentfontsize;\r
146                 }\r
147 \r
148                 return new Length(fontsize, false);\r
149 \r
150         }\r
151 \r
152         /*\r
153          * (non-Javadoc)\r
154          * \r
155          * @see org.eclipse.jst.pagedesigner.css2.property.CSSPropertyMeta#calculateHTMLAttributeOverride(org.w3c.dom.Element,\r
156          *      java.lang.String, java.lang.String,\r
157          *      org.eclipse.jst.pagedesigner.css2.ICSSStyle)\r
158          */\r
159         public Object calculateHTMLAttributeOverride(Element element,\r
160                         String htmltag, String propertyName, ICSSStyle style) {\r
161                 if (IHTMLConstants.TAG_FONT.equalsIgnoreCase(htmltag)\r
162                                 || ICSSPropertyID.ATTR_BASEFONT.equalsIgnoreCase(htmltag)) {\r
163                         String size = DOMUtil.getAttributeIgnoreCase(element,\r
164                                         ICSSPropertyID.ATTR_SIZE);\r
165                         if (size != null) {\r
166                                 size = size.trim();\r
167                                 try {\r
168                                         int fontSize = 0;\r
169                                         if (size.startsWith("+")) //$NON-NLS-1$\r
170                                         {\r
171                                                 fontSize = Integer.parseInt(size.substring(1)) + 3;\r
172                                         } else if (size.startsWith("-")) //$NON-NLS-1$\r
173                                         {\r
174                                                 fontSize = 3 - Integer.parseInt(size.substring(1));\r
175                                         } else {\r
176                                                 fontSize = Integer.parseInt(size);\r
177                                         }\r
178                                         if (fontSize < 1) {\r
179                                                 fontSize = 1;\r
180                                         }\r
181                                         if (fontSize > 7) {\r
182                                                 fontSize = 7;\r
183                                         }\r
184                                         return new Length((int) (Math.round(FACTORS[fontSize - 1]\r
185                                                         * MEDIUM_VAL_INT)), false);\r
186                                 } catch (Exception ex) {\r
187                                         // Error in tag font attr calculating.\r
188                                         // _log.error("Error.FontSizeMeta.0", ex); //$NON-NLS-1$\r
189                                         return null;\r
190                                 }\r
191                         }\r
192                 }\r
193                 return null;\r
194         }\r
195 }\r