001: /*
002:
003: ============================================================================
004: The Apache Software License, Version 1.1
005: ============================================================================
006:
007: Copyright (C) 1999-2003 The Apache Software Foundation. All rights reserved.
008:
009: Redistribution and use in source and binary forms, with or without modifica-
010: tion, are permitted provided that the following conditions are met:
011:
012: 1. Redistributions of source code must retain the above copyright notice,
013: this list of conditions and the following disclaimer.
014:
015: 2. Redistributions in binary form must reproduce the above copyright notice,
016: this list of conditions and the following disclaimer in the documentation
017: and/or other materials provided with the distribution.
018:
019: 3. The end-user documentation included with the redistribution, if any, must
020: include the following acknowledgment: "This product includes software
021: developed by the Apache Software Foundation (http://www.apache.org/)."
022: Alternately, this acknowledgment may appear in the software itself, if
023: and wherever such third-party acknowledgments normally appear.
024:
025: 4. The names "Batik" and "Apache Software Foundation" must not be
026: used to endorse or promote products derived from this software without
027: prior written permission. For written permission, please contact
028: apache@apache.org.
029:
030: 5. Products derived from this software may not be called "Apache", nor may
031: "Apache" appear in their name, without prior written permission of the
032: Apache Software Foundation.
033:
034: THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
035: INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
036: FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
037: APACHE SOFTWARE FOUNDATION OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
038: INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLU-
039: DING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
040: OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
041: ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
042: (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
043: THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
044:
045: This software consists of voluntary contributions made by many individuals
046: on behalf of the Apache Software Foundation. For more information on the
047: Apache Software Foundation, please see <http://www.apache.org/>.
048:
049: */
050:
051: package org.apache.batik.css.engine.value.css2;
052:
053: import org.apache.batik.css.engine.CSSContext;
054: import org.apache.batik.css.engine.CSSEngine;
055: import org.apache.batik.css.engine.CSSStylableElement;
056: import org.apache.batik.css.engine.StyleMap;
057: import org.apache.batik.css.engine.value.IdentifierManager;
058: import org.apache.batik.css.engine.value.StringMap;
059: import org.apache.batik.css.engine.value.Value;
060: import org.apache.batik.css.engine.value.ValueConstants;
061: import org.apache.batik.util.CSSConstants;
062: import org.w3c.css.sac.LexicalUnit;
063: import org.w3c.dom.DOMException;
064: import org.w3c.dom.css.CSSPrimitiveValue;
065:
066: /**
067: * This class provides a manager for the 'font-weight' property values.
068: *
069: * @author <a href="mailto:stephane@hillion.org">Stephane Hillion</a>
070: * @version $Id$
071: */
072: public class FontWeightManager extends IdentifierManager {
073:
074: /**
075: * The identifier values.
076: */
077: public final static StringMap values = new StringMap();
078: static {
079: // BEGIN RAVE MODIFICATIONS
080: // ALL is not a valid CSS 2 option for font-weight
081: //values.put(CSSConstants.CSS_ALL_VALUE,
082: // ValueConstants.ALL_VALUE);
083: // END RAVE MODIFICATIONS
084: values.put(CSSConstants.CSS_BOLD_VALUE,
085: ValueConstants.BOLD_VALUE);
086: values.put(CSSConstants.CSS_BOLDER_VALUE,
087: ValueConstants.BOLDER_VALUE);
088: values.put(CSSConstants.CSS_LIGHTER_VALUE,
089: ValueConstants.LIGHTER_VALUE);
090: values.put(CSSConstants.CSS_NORMAL_VALUE,
091: ValueConstants.NORMAL_VALUE);
092: }
093:
094: /**
095: * Implements {@link ValueManager#isInheritedProperty()}.
096: */
097: public boolean isInheritedProperty() {
098: return true;
099: }
100:
101: /**
102: * Implements {@link ValueManager#getPropertyName()}.
103: */
104: public String getPropertyName() {
105: return CSSConstants.CSS_FONT_WEIGHT_PROPERTY;
106: }
107:
108: /**
109: * Implements {@link ValueManager#getDefaultValue()}.
110: */
111: public Value getDefaultValue() {
112: return ValueConstants.NORMAL_VALUE;
113: }
114:
115: /**
116: * Implements {@link ValueManager#createValue(LexicalUnit,CSSEngine)}.
117: */
118: public Value createValue(LexicalUnit lu, CSSEngine engine)
119: throws DOMException {
120: if (lu.getLexicalUnitType() == LexicalUnit.SAC_INTEGER) {
121: int i = lu.getIntegerValue();
122: switch (i) {
123: case 100:
124: return ValueConstants.NUMBER_100;
125: case 200:
126: return ValueConstants.NUMBER_200;
127: case 300:
128: return ValueConstants.NUMBER_300;
129: case 400:
130: return ValueConstants.NUMBER_400;
131: case 500:
132: return ValueConstants.NUMBER_500;
133: case 600:
134: return ValueConstants.NUMBER_600;
135: case 700:
136: return ValueConstants.NUMBER_700;
137: case 800:
138: return ValueConstants.NUMBER_800;
139: case 900:
140: return ValueConstants.NUMBER_900;
141: }
142: throw createInvalidFloatValueDOMException(i, engine);
143: }
144: return super .createValue(lu, engine);
145: }
146:
147: /**
148: * Implements {@link ValueManager#createFloatValue(short,float)}.
149: */
150: public Value createFloatValue(short type, float floatValue,
151: CSSEngine engine) throws DOMException {
152: if (type == CSSPrimitiveValue.CSS_NUMBER) {
153: int i = (int) floatValue;
154: if (floatValue == i) {
155: switch (i) {
156: case 100:
157: return ValueConstants.NUMBER_100;
158: case 200:
159: return ValueConstants.NUMBER_200;
160: case 300:
161: return ValueConstants.NUMBER_300;
162: case 400:
163: return ValueConstants.NUMBER_400;
164: case 500:
165: return ValueConstants.NUMBER_500;
166: case 600:
167: return ValueConstants.NUMBER_600;
168: case 700:
169: return ValueConstants.NUMBER_700;
170: case 800:
171: return ValueConstants.NUMBER_800;
172: case 900:
173: return ValueConstants.NUMBER_900;
174: }
175: }
176: }
177: throw createInvalidFloatValueDOMException(floatValue, engine);
178: }
179:
180: /**
181: * Implements {@link
182: * ValueManager#computeValue(CSSStylableElement,String,CSSEngine,int,StyleMap,Value)}.
183: */
184: public Value computeValue(CSSStylableElement elt, String pseudo,
185: CSSEngine engine, int idx, StyleMap sm, Value value) {
186: if (value == ValueConstants.BOLDER_VALUE) {
187: sm.putParentRelative(idx, true);
188:
189: CSSContext ctx = engine.getCSSContext();
190: CSSStylableElement p = CSSEngine
191: .getParentCSSStylableElement(elt);
192: float fw;
193: if (p == null) {
194: fw = 400;
195: } else {
196: Value v = engine.getComputedStyle(p, pseudo, idx);
197: fw = v.getFloatValue();
198: }
199: return createFontWeight(ctx.getBolderFontWeight(fw));
200: } else if (value == ValueConstants.LIGHTER_VALUE) {
201: sm.putParentRelative(idx, true);
202:
203: CSSContext ctx = engine.getCSSContext();
204: CSSStylableElement p = CSSEngine
205: .getParentCSSStylableElement(elt);
206: float fw;
207: if (p == null) {
208: fw = 400;
209: } else {
210: Value v = engine.getComputedStyle(p, pseudo, idx);
211: fw = v.getFloatValue();
212: }
213: return createFontWeight(ctx.getLighterFontWeight(fw));
214: } else if (value == ValueConstants.NORMAL_VALUE) {
215: return ValueConstants.NUMBER_400;
216: } else if (value == ValueConstants.BOLD_VALUE) {
217: return ValueConstants.NUMBER_700;
218: }
219: return value;
220: }
221:
222: /**
223: * Returns the CSS value associated with the given font-weight.
224: */
225: protected Value createFontWeight(float f) {
226: switch ((int) f) {
227: case 100:
228: return ValueConstants.NUMBER_100;
229: case 200:
230: return ValueConstants.NUMBER_200;
231: case 300:
232: return ValueConstants.NUMBER_300;
233: case 400:
234: return ValueConstants.NUMBER_400;
235: case 500:
236: return ValueConstants.NUMBER_500;
237: case 600:
238: return ValueConstants.NUMBER_600;
239: case 700:
240: return ValueConstants.NUMBER_700;
241: case 800:
242: return ValueConstants.NUMBER_800;
243: default: // 900
244: return ValueConstants.NUMBER_900;
245: }
246: }
247:
248: /**
249: * Implements {@link IdentifierManager#getIdentifiers()}.
250: */
251: protected StringMap getIdentifiers() {
252: return values;
253: }
254: }
|