KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > batik > css > engine > value > svg > BaselineShiftManager


1 /*
2
3    Copyright 2002-2003 The Apache Software Foundation
4
5    Licensed under the Apache License, Version 2.0 (the "License");
6    you may not use this file except in compliance with the License.
7    You may obtain a copy of the License at
8
9        http://www.apache.org/licenses/LICENSE-2.0
10
11    Unless required by applicable law or agreed to in writing, software
12    distributed under the License is distributed on an "AS IS" BASIS,
13    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14    See the License for the specific language governing permissions and
15    limitations under the License.
16
17  */

18 package org.apache.batik.css.engine.value.svg;
19
20 import org.apache.batik.css.engine.CSSEngine;
21 import org.apache.batik.css.engine.CSSStylableElement;
22 import org.apache.batik.css.engine.StyleMap;
23 import org.apache.batik.css.engine.value.FloatValue;
24 import org.apache.batik.css.engine.value.LengthManager;
25 import org.apache.batik.css.engine.value.StringMap;
26 import org.apache.batik.css.engine.value.Value;
27 import org.apache.batik.css.engine.value.ValueManager;
28 import org.apache.batik.util.CSSConstants;
29 import org.w3c.css.sac.LexicalUnit;
30 import org.w3c.dom.DOMException JavaDoc;
31 import org.w3c.dom.css.CSSPrimitiveValue;
32
33 /**
34  * This class provides a manager for the 'baseline-shift' property values.
35  *
36  * @author <a HREF="mailto:stephane@hillion.org">Stephane Hillion</a>
37  * @version $Id: BaselineShiftManager.java,v 1.6 2005/03/27 08:58:31 cam Exp $
38  */

39 public class BaselineShiftManager extends LengthManager {
40     
41     /**
42      * The identifier values.
43      */

44     protected final static StringMap values = new StringMap();
45     static {
46     values.put(CSSConstants.CSS_BASELINE_VALUE,
47                    SVGValueConstants.BASELINE_VALUE);
48     values.put(CSSConstants.CSS_SUB_VALUE,
49                    SVGValueConstants.SUB_VALUE);
50     values.put(CSSConstants.CSS_SUPER_VALUE,
51                    SVGValueConstants.SUPER_VALUE);
52     }
53
54     /**
55      * Implements {@link ValueManager#isInheritedProperty()}.
56      */

57     public boolean isInheritedProperty() {
58     return false;
59     }
60
61     /**
62      * Implements {@link ValueManager#getPropertyName()}.
63      */

64     public String JavaDoc getPropertyName() {
65     return CSSConstants.CSS_BASELINE_SHIFT_PROPERTY;
66     }
67     
68     /**
69      * Implements {@link ValueManager#getDefaultValue()}.
70      */

71     public Value getDefaultValue() {
72         return SVGValueConstants.BASELINE_VALUE;
73     }
74
75     /**
76      * Implements {@link ValueManager#createValue(LexicalUnit,CSSEngine)}.
77      */

78     public Value createValue(LexicalUnit lu, CSSEngine engine)
79         throws DOMException JavaDoc {
80         switch (lu.getLexicalUnitType()) {
81         case LexicalUnit.SAC_INHERIT:
82             return SVGValueConstants.INHERIT_VALUE;
83
84         case LexicalUnit.SAC_IDENT:
85         Object JavaDoc v = values.get(lu.getStringValue().toLowerCase().intern());
86         if (v == null) {
87                 throw createInvalidIdentifierDOMException(lu.getStringValue());
88             }
89             return (Value)v;
90         }
91         return super.createValue(lu, engine);
92     }
93
94     /**
95      * Implements {@link ValueManager#createStringValue(short,String,CSSEngine)}.
96      */

97     public Value createStringValue(short type, String JavaDoc value, CSSEngine engine)
98         throws DOMException JavaDoc {
99     if (type != CSSPrimitiveValue.CSS_IDENT) {
100             throw createInvalidIdentifierDOMException(value);
101         }
102     Object JavaDoc v = values.get(value.toLowerCase().intern());
103     if (v == null) {
104             throw createInvalidIdentifierDOMException(value);
105     }
106         return (Value)v;
107     }
108
109     /**
110      * Implements {@link
111      * ValueManager#computeValue(CSSStylableElement,String,CSSEngine,int,StyleMap,Value)}.
112      */

113     public Value computeValue(CSSStylableElement elt,
114                               String JavaDoc pseudo,
115                               CSSEngine engine,
116                               int idx,
117                               StyleMap sm,
118                               Value value) {
119         if (value.getPrimitiveType() == CSSPrimitiveValue.CSS_PERCENTAGE) {
120             sm.putLineHeightRelative(idx, true);
121
122             int fsi = engine.getLineHeightIndex();
123         CSSStylableElement parent;
124         parent = (CSSStylableElement)elt.getParentNode();
125         if (parent == null) {
126         // Hmmm somthing pretty odd - can't happen accordint to spec,
127
// should always have text parent.
128
// http://www.w3.org/TR/SVG11/text.html#BaselineShiftProperty
129
parent = elt;
130         }
131             Value fs = engine.getComputedStyle(parent, pseudo, fsi);
132             float fsv = fs.getFloatValue();
133             float v = value.getFloatValue();
134             return new FloatValue(CSSPrimitiveValue.CSS_NUMBER,
135                                   (fsv * v) / 100f);
136         }
137         return super.computeValue(elt, pseudo, engine, idx, sm, value);
138     }
139
140
141     /**
142      * Indicates the orientation of the property associated with
143      * this manager.
144      */

145     protected int getOrientation() {
146         return BOTH_ORIENTATION; // Not used
147
}
148
149 }
150
Popular Tags