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;
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.w3c.css.sac.LexicalUnit;
058: import org.w3c.dom.DOMException;
059: import org.w3c.dom.css.CSSPrimitiveValue;
060: import org.w3c.dom.css.CSSValue;
061:
062: /**
063: * This class provides a manager for the property with support for
064: * length values.
065: *
066: * @author <a href="mailto:stephane@hillion.org">Stephane Hillion</a>
067: * @version $Id$
068: */
069: public abstract class LengthManager extends AbstractValueManager {
070:
071: /**
072: * Implements {@link ValueManager#createValue(LexicalUnit,CSSEngine)}.
073: */
074: public Value createValue(LexicalUnit lu, CSSEngine engine)
075: throws DOMException {
076: switch (lu.getLexicalUnitType()) {
077: case LexicalUnit.SAC_EM:
078: return new FloatValue(CSSPrimitiveValue.CSS_EMS, lu
079: .getFloatValue());
080:
081: case LexicalUnit.SAC_EX:
082: return new FloatValue(CSSPrimitiveValue.CSS_EXS, lu
083: .getFloatValue());
084:
085: case LexicalUnit.SAC_PIXEL:
086: return new FloatValue(CSSPrimitiveValue.CSS_PX, lu
087: .getFloatValue());
088:
089: case LexicalUnit.SAC_CENTIMETER:
090: return new FloatValue(CSSPrimitiveValue.CSS_CM, lu
091: .getFloatValue());
092:
093: case LexicalUnit.SAC_MILLIMETER:
094: return new FloatValue(CSSPrimitiveValue.CSS_MM, lu
095: .getFloatValue());
096:
097: case LexicalUnit.SAC_INCH:
098: return new FloatValue(CSSPrimitiveValue.CSS_IN, lu
099: .getFloatValue());
100:
101: case LexicalUnit.SAC_POINT:
102: return new FloatValue(CSSPrimitiveValue.CSS_PT, lu
103: .getFloatValue());
104:
105: case LexicalUnit.SAC_PICA:
106: return new FloatValue(CSSPrimitiveValue.CSS_PC, lu
107: .getFloatValue());
108:
109: case LexicalUnit.SAC_INTEGER:
110: return new FloatValue(CSSPrimitiveValue.CSS_NUMBER, lu
111: .getIntegerValue());
112:
113: case LexicalUnit.SAC_REAL:
114: return new FloatValue(CSSPrimitiveValue.CSS_NUMBER, lu
115: .getFloatValue());
116:
117: case LexicalUnit.SAC_PERCENTAGE:
118: return new FloatValue(CSSPrimitiveValue.CSS_PERCENTAGE, lu
119: .getFloatValue());
120: }
121: throw createInvalidLexicalUnitDOMException(lu
122: .getLexicalUnitType(), engine);
123: }
124:
125: /**
126: * Implements {@link ValueManager#createFloatValue(short,float)}.
127: */
128: public Value createFloatValue(short type, float floatValue,
129: CSSEngine engine) throws DOMException {
130: switch (type) {
131: case CSSPrimitiveValue.CSS_PERCENTAGE:
132: case CSSPrimitiveValue.CSS_EMS:
133: case CSSPrimitiveValue.CSS_EXS:
134: case CSSPrimitiveValue.CSS_PX:
135: case CSSPrimitiveValue.CSS_CM:
136: case CSSPrimitiveValue.CSS_MM:
137: case CSSPrimitiveValue.CSS_IN:
138: case CSSPrimitiveValue.CSS_PT:
139: case CSSPrimitiveValue.CSS_PC:
140: case CSSPrimitiveValue.CSS_NUMBER:
141: return new FloatValue(type, floatValue);
142: }
143: throw createInvalidFloatTypeDOMException(type, engine);
144: }
145:
146: /**
147: * Implements {@link
148: * ValueManager#computeValue(CSSStylableElement,String,CSSEngine,int,StyleMap,Value)}.
149: */
150: public Value computeValue(CSSStylableElement elt, String pseudo,
151: CSSEngine engine, int idx, StyleMap sm, Value value) {
152: if (value.getCssValueType() != CSSValue.CSS_PRIMITIVE_VALUE) {
153: return value;
154: }
155:
156: switch (value.getPrimitiveType()) {
157: case CSSPrimitiveValue.CSS_NUMBER:
158: case CSSPrimitiveValue.CSS_PX:
159: return value;
160:
161: case CSSPrimitiveValue.CSS_MM:
162: CSSContext ctx = engine.getCSSContext();
163: float v = value.getFloatValue();
164: return new FloatValue(CSSPrimitiveValue.CSS_NUMBER, v
165: / ctx.getPixelUnitToMillimeter());
166:
167: case CSSPrimitiveValue.CSS_CM:
168: ctx = engine.getCSSContext();
169: v = value.getFloatValue();
170: return new FloatValue(CSSPrimitiveValue.CSS_NUMBER, v * 10f
171: / ctx.getPixelUnitToMillimeter());
172:
173: case CSSPrimitiveValue.CSS_IN:
174: ctx = engine.getCSSContext();
175: v = value.getFloatValue();
176: return new FloatValue(CSSPrimitiveValue.CSS_NUMBER, v
177: * 25.4f / ctx.getPixelUnitToMillimeter());
178:
179: case CSSPrimitiveValue.CSS_PT:
180: ctx = engine.getCSSContext();
181: v = value.getFloatValue();
182: return new FloatValue(CSSPrimitiveValue.CSS_NUMBER, v
183: * 25.4f / (72f * ctx.getPixelUnitToMillimeter()));
184:
185: case CSSPrimitiveValue.CSS_PC:
186: ctx = engine.getCSSContext();
187: v = value.getFloatValue();
188: return new FloatValue(CSSPrimitiveValue.CSS_NUMBER,
189: (v * 25.4f / (6f * ctx.getPixelUnitToMillimeter())));
190:
191: case CSSPrimitiveValue.CSS_EMS:
192: sm.putFontSizeRelative(idx, true);
193:
194: v = value.getFloatValue();
195: int fsidx = engine.getFontSizeIndex();
196: float fs;
197: fs = engine.getComputedStyle(elt, pseudo, fsidx)
198: .getFloatValue();
199: return new FloatValue(CSSPrimitiveValue.CSS_NUMBER, v * fs);
200:
201: case CSSPrimitiveValue.CSS_EXS:
202: sm.putFontSizeRelative(idx, true);
203:
204: v = value.getFloatValue();
205: fsidx = engine.getFontSizeIndex();
206: fs = engine.getComputedStyle(elt, pseudo, fsidx)
207: .getFloatValue();
208: return new FloatValue(CSSPrimitiveValue.CSS_NUMBER, v * fs
209: * 0.5f);
210:
211: case CSSPrimitiveValue.CSS_PERCENTAGE:
212: ctx = engine.getCSSContext();
213: switch (getOrientation()) {
214: case HORIZONTAL_ORIENTATION:
215: sm.putBlockWidthRelative(idx, true);
216: fs = value.getFloatValue() * ctx.getBlockWidth(elt)
217: / 100f;
218: break;
219: case VERTICAL_ORIENTATION:
220: sm.putBlockHeightRelative(idx, true);
221: fs = value.getFloatValue() * ctx.getBlockHeight(elt)
222: / 100f;
223: break;
224: default: // Both
225: sm.putBlockWidthRelative(idx, true);
226: sm.putBlockHeightRelative(idx, true);
227: double w = ctx.getBlockWidth(elt);
228: double h = ctx.getBlockHeight(elt);
229: fs = (float) (value.getFloatValue()
230: * (Math.sqrt(w * w + h * h) / Math.sqrt(2)) / 100.0);
231: }
232: return new FloatValue(CSSPrimitiveValue.CSS_NUMBER, fs);
233: }
234: return value;
235: }
236:
237: //
238: // Orientation enumeration
239: //
240: protected final static int HORIZONTAL_ORIENTATION = 0;
241: protected final static int VERTICAL_ORIENTATION = 1;
242: protected final static int BOTH_ORIENTATION = 2;
243:
244: /**
245: * Indicates the orientation of the property associated with
246: * this manager.
247: */
248: protected abstract int getOrientation();
249: }
|