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.FloatValue;
058: import org.apache.batik.css.engine.value.LengthManager;
059: import org.apache.batik.css.engine.value.StringMap;
060: import org.apache.batik.css.engine.value.Value;
061: import org.apache.batik.css.engine.value.ValueConstants;
062: import org.apache.batik.util.CSSConstants;
063: import org.w3c.css.sac.LexicalUnit;
064: import org.w3c.dom.DOMException;
065: import org.w3c.dom.css.CSSPrimitiveValue;
066:
067: /**
068: * This class provides a manager for the 'font-size' property values.
069: *
070: * @author <a href="mailto:stephane@hillion.org">Stephane Hillion</a>
071: * @version $Id$
072: */
073: public class FontSizeManager extends LengthManager {
074:
075: /**
076: * The identifier values.
077: */
078: public final static StringMap values = new StringMap();
079: static {
080: // BEGIN RAVE MODIFICATIONS
081: // ALL is not a valid CSS 2 option for font-weight
082: //values.put(CSSConstants.CSS_ALL_VALUE,
083: // ValueConstants.ALL_VALUE);
084: // END RAVE MODIFICATIONS
085: values.put(CSSConstants.CSS_LARGE_VALUE,
086: ValueConstants.LARGE_VALUE);
087: values.put(CSSConstants.CSS_LARGER_VALUE,
088: ValueConstants.LARGER_VALUE);
089: values.put(CSSConstants.CSS_MEDIUM_VALUE,
090: ValueConstants.MEDIUM_VALUE);
091: values.put(CSSConstants.CSS_SMALL_VALUE,
092: ValueConstants.SMALL_VALUE);
093: values.put(CSSConstants.CSS_SMALLER_VALUE,
094: ValueConstants.SMALLER_VALUE);
095: values.put(CSSConstants.CSS_X_LARGE_VALUE,
096: ValueConstants.X_LARGE_VALUE);
097: values.put(CSSConstants.CSS_X_SMALL_VALUE,
098: ValueConstants.X_SMALL_VALUE);
099: values.put(CSSConstants.CSS_XX_LARGE_VALUE,
100: ValueConstants.XX_LARGE_VALUE);
101: values.put(CSSConstants.CSS_XX_SMALL_VALUE,
102: ValueConstants.XX_SMALL_VALUE);
103: }
104:
105: /**
106: * Implements {@link ValueManager#isInheritedProperty()}.
107: */
108: public boolean isInheritedProperty() {
109: return true;
110: }
111:
112: /**
113: * Implements {@link ValueManager#getPropertyName()}.
114: */
115: public String getPropertyName() {
116: return CSSConstants.CSS_FONT_SIZE_PROPERTY;
117: }
118:
119: /**
120: * Implements {@link ValueManager#getDefaultValue()}.
121: */
122: public Value getDefaultValue() {
123: return ValueConstants.MEDIUM_VALUE;
124: }
125:
126: /**
127: * Implements {@link ValueManager#createValue(LexicalUnit,CSSEngine)}.
128: */
129: public Value createValue(LexicalUnit lu, CSSEngine engine)
130: throws DOMException {
131: switch (lu.getLexicalUnitType()) {
132: case LexicalUnit.SAC_INHERIT:
133: return ValueConstants.INHERIT_VALUE;
134:
135: case LexicalUnit.SAC_IDENT:
136: String s = lu.getStringValue().toLowerCase().intern();
137: Object v = values.get(s);
138: if (v == null) {
139: throw createInvalidIdentifierDOMException(s, engine);
140: }
141: return (Value) v;
142: }
143: return super .createValue(lu, engine);
144: }
145:
146: /**
147: * Implements {@link
148: * ValueManager#createStringValue(short,String,CSSEngine)}.
149: */
150: public Value createStringValue(short type, String value,
151: CSSEngine engine) throws DOMException {
152: if (type != CSSPrimitiveValue.CSS_IDENT) {
153: throw createInvalidStringTypeDOMException(type, engine);
154: }
155: Object v = values.get(value.toLowerCase().intern());
156: if (v == null) {
157: throw createInvalidIdentifierDOMException(value, engine);
158: }
159: return (Value) v;
160: }
161:
162: /**
163: * Implements {@link
164: * ValueManager#computeValue(CSSStylableElement,String,CSSEngine,int,StyleMap,Value)}.
165: */
166: public Value computeValue(CSSStylableElement elt, String pseudo,
167: CSSEngine engine, int idx, StyleMap sm, Value value) {
168: switch (value.getPrimitiveType()) {
169: case CSSPrimitiveValue.CSS_NUMBER:
170: case CSSPrimitiveValue.CSS_PX:
171: return value;
172:
173: case CSSPrimitiveValue.CSS_MM:
174: CSSContext ctx = engine.getCSSContext();
175: float v = value.getFloatValue();
176: return new FloatValue(CSSPrimitiveValue.CSS_NUMBER, v
177: / ctx.getPixelUnitToMillimeter());
178:
179: case CSSPrimitiveValue.CSS_CM:
180: ctx = engine.getCSSContext();
181: v = value.getFloatValue();
182: return new FloatValue(CSSPrimitiveValue.CSS_NUMBER, v * 10f
183: / ctx.getPixelUnitToMillimeter());
184:
185: case CSSPrimitiveValue.CSS_IN:
186: ctx = engine.getCSSContext();
187: v = value.getFloatValue();
188: return new FloatValue(CSSPrimitiveValue.CSS_NUMBER, v
189: * 25.4f / ctx.getPixelUnitToMillimeter());
190:
191: case CSSPrimitiveValue.CSS_PT:
192: ctx = engine.getCSSContext();
193: v = value.getFloatValue();
194: return new FloatValue(CSSPrimitiveValue.CSS_NUMBER, v
195: * 25.4f / (72f * ctx.getPixelUnitToMillimeter()));
196:
197: case CSSPrimitiveValue.CSS_PC:
198: ctx = engine.getCSSContext();
199: v = value.getFloatValue();
200: return new FloatValue(CSSPrimitiveValue.CSS_NUMBER,
201: (v * 25.4f / (6f * ctx.getPixelUnitToMillimeter())));
202:
203: case CSSPrimitiveValue.CSS_EMS:
204: sm.putParentRelative(idx, true);
205:
206: v = value.getFloatValue();
207: CSSStylableElement p;
208: p = CSSEngine.getParentCSSStylableElement(elt);
209: float fs;
210: if (p == null) {
211: ctx = engine.getCSSContext();
212: fs = ctx.getMediumFontSize();
213: } else {
214: fs = engine.getComputedStyle(p, null, idx)
215: .getFloatValue();
216: }
217: return new FloatValue(CSSPrimitiveValue.CSS_NUMBER, v * fs);
218:
219: case CSSPrimitiveValue.CSS_EXS:
220: sm.putParentRelative(idx, true);
221:
222: v = value.getFloatValue();
223: p = CSSEngine.getParentCSSStylableElement(elt);
224: if (p == null) {
225: ctx = engine.getCSSContext();
226: fs = ctx.getMediumFontSize();
227: } else {
228: fs = engine.getComputedStyle(p, null, idx)
229: .getFloatValue();
230: }
231: return new FloatValue(CSSPrimitiveValue.CSS_NUMBER, v * fs
232: * 0.5f); // !!! x-height
233:
234: case CSSPrimitiveValue.CSS_PERCENTAGE:
235: sm.putParentRelative(idx, true);
236:
237: v = value.getFloatValue();
238: p = CSSEngine.getParentCSSStylableElement(elt);
239: if (p == null) {
240: ctx = engine.getCSSContext();
241: fs = ctx.getMediumFontSize();
242: } else {
243: fs = engine.getComputedStyle(p, null, idx)
244: .getFloatValue();
245: }
246: return new FloatValue(CSSPrimitiveValue.CSS_NUMBER, v * fs
247: / 100f);
248: }
249:
250: if (value == ValueConstants.LARGER_VALUE) {
251: sm.putParentRelative(idx, true);
252:
253: CSSStylableElement p;
254: p = CSSEngine.getParentCSSStylableElement(elt);
255: float fs;
256: if (p == null) {
257: CSSContext ctx = engine.getCSSContext();
258: fs = ctx.getMediumFontSize();
259: } else {
260: fs = engine.getComputedStyle(p, null, idx)
261: .getFloatValue();
262: }
263: return new FloatValue(CSSPrimitiveValue.CSS_NUMBER,
264: fs * 1.2f);
265: } else if (value == ValueConstants.SMALLER_VALUE) {
266: sm.putParentRelative(idx, true);
267:
268: CSSStylableElement p;
269: p = CSSEngine.getParentCSSStylableElement(elt);
270: float fs;
271: if (p == null) {
272: CSSContext ctx = engine.getCSSContext();
273: fs = ctx.getMediumFontSize();
274: } else {
275: fs = engine.getComputedStyle(p, null, idx)
276: .getFloatValue();
277: }
278: return new FloatValue(CSSPrimitiveValue.CSS_NUMBER,
279: fs / 1.2f);
280: }
281:
282: // absolute identifiers
283: CSSContext ctx = engine.getCSSContext();
284: float fs = ctx.getMediumFontSize();
285: String s = value.getStringValue();
286: switch (s.charAt(0)) {
287: case 'm':
288: break;
289:
290: case 's':
291: fs = (float) (fs / 1.2);
292: break;
293:
294: case 'l':
295: fs = (float) (fs * 1.2);
296: break;
297:
298: default: // 'x'
299: switch (s.charAt(1)) {
300: case 'x':
301: switch (s.charAt(3)) {
302: case 's':
303: fs = (float) (((fs / 1.2) / 1.2) / 1.2);
304: break;
305:
306: default: // 'l'
307: fs = (float) (fs * 1.2 * 1.2 * 1.2);
308: }
309: break;
310:
311: default: // '-'
312: switch (s.charAt(2)) {
313: case 's':
314: fs = (float) ((fs / 1.2) / 1.2);
315: break;
316:
317: default: // 'l'
318: fs = (float) (fs * 1.2 * 1.2);
319: }
320: }
321: }
322: return new FloatValue(CSSPrimitiveValue.CSS_NUMBER, fs);
323: }
324:
325: /**
326: * Indicates the orientation of the property associated with
327: * this manager.
328: */
329: protected int getOrientation() {
330: return VERTICAL_ORIENTATION; // Not used
331: }
332: }
|