1 /*******************************************************************************
\r
2 * Copyright (c) 2006 Sybase, Inc. and others.
\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
10 * Sybase, Inc. - initial API and implementation
\r
11 *******************************************************************************/
\r
12 package org.eclipse.jst.pagedesigner.css2.property;
\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
23 * The result value should be Length object only.
\r
27 // XXX: in the future, we may add "unit" into Length, the one unit will be
\r
29 // and specific for font. Means the relative size to MEDIUM. Then it is possible
\r
31 // the caller to decide the actual font size based on the style specific MEDIUM
\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
43 public static final double MEDIUM_VAL_INT = 16;
\r
45 private static final double SCALING_FACTOR = 1.2;
\r
47 private static final double FACTORS[] = { 0.6, 0.89, 1, 1.2, 1.5, 2.0, 3.0 };
\r
49 // The scaling factors in IE is different from CSS definition. Here we
\r
51 private static final double CSS_ABSOLUTE_FACTORS[] = { 0.63, 0.82, 1, 1.12,
\r
54 private static final int MIN_SIZE_FOR_SMALLER = 1;
\r
56 private static final int MIN_SIZE_FOR_LARGER = 9;
\r
59 * Default constructor
\r
61 public FontSizeMeta() {
\r
62 super(true, new Length((int) MEDIUM_VAL_INT, false));
\r
68 * @see org.eclipse.jst.pagedesigner.css2.property.LengthMeta#getBaseFont(org.eclipse.jst.pagedesigner.css2.property.AbstractStyle)
\r
70 protected ICSSFont getBaseFont(ICSSStyle style) {
\r
71 return style.getParentStyle().getCSSFont();
\r
77 * @see org.eclipse.jst.pagedesigner.css2.property.CSSPropertyMeta#getKeywordValues()
\r
79 protected String[] getKeywordValues() {
\r
86 * @see org.eclipse.jst.pagedesigner.css2.property.CSSPropertyMeta#getPercentageType()
\r
88 public int getPercentageType() {
\r
89 return PERCENTAGE_FONT;
\r
95 * @see org.eclipse.jst.pagedesigner.css2.property.LengthMeta#calculateCSSValueResult(org.w3c.dom.css.CSSValue,
\r
97 * org.eclipse.jst.pagedesigner.css2.style.AbstractStyle)
\r
99 public Object calculateCSSValueResult(CSSValue value, String propertyName,
\r
101 Object obj = super.calculateCSSValueResult(value, propertyName, style);
\r
102 if (obj instanceof Length) {
\r
105 // otherwise, it is keyword.
\r
106 return convertKeyword(obj, style);
\r
110 * @param fontsizeobj
\r
112 * @return the length
\r
114 protected Length convertKeyword(Object fontsizeobj, ICSSStyle style) {
\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
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
145 fontsize = parentfontsize;
\r
148 return new Length(fontsize, false);
\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
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
169 if (size.startsWith("+")) //$NON-NLS-1$
\r
171 fontSize = Integer.parseInt(size.substring(1)) + 3;
\r
172 } else if (size.startsWith("-")) //$NON-NLS-1$
\r
174 fontSize = 3 - Integer.parseInt(size.substring(1));
\r
176 fontSize = Integer.parseInt(size);
\r
178 if (fontSize < 1) {
\r
181 if (fontSize > 7) {
\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