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

19 package org.apache.batik.css.engine.value.svg;
20
21 import org.apache.batik.css.engine.CSSEngine;
22 import org.apache.batik.css.engine.CSSStylableElement;
23 import org.apache.batik.css.engine.StyleMap;
24 import org.apache.batik.css.engine.value.FloatValue;
25 import org.apache.batik.css.engine.value.LengthManager;
26 import org.apache.batik.css.engine.value.StringMap;
27 import org.apache.batik.css.engine.value.Value;
28 import org.apache.batik.css.engine.value.ValueManager;
29 import org.apache.batik.util.CSSConstants;
30 import org.apache.batik.util.SVGTypes;
31
32 import org.w3c.css.sac.LexicalUnit;
33 import org.w3c.dom.DOMException;
34 import org.w3c.dom.css.CSSPrimitiveValue;
35
36 /**
37  * This class provides a manager for the 'baseline-shift' property values.
38  *
39  * @author <a href="mailto:stephane@hillion.org">Stephane Hillion</a>
40  * @version $Id: BaselineShiftManager.java 1733416 2016-03-03 07:07:13Z gadams $
41  */

42 public class BaselineShiftManager extends LengthManager {
43
44     /**
45      * The identifier values.
46      */

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

60     public boolean isInheritedProperty() {
61         return false;
62     }
63
64     /**
65      * Implements {@link ValueManager#isAnimatableProperty()}.
66      */

67     public boolean isAnimatableProperty() {
68         return true;
69     }
70
71     /**
72      * Implements {@link ValueManager#isAdditiveProperty()}.
73      */

74     public boolean isAdditiveProperty() {
75         return false;
76     }
77
78     /**
79      * Implements {@link ValueManager#getPropertyType()}.
80      */

81     public int getPropertyType() {
82         return SVGTypes.TYPE_BASELINE_SHIFT_VALUE;
83     }
84
85     /**
86      * Implements {@link ValueManager#getPropertyName()}.
87      */

88     public String getPropertyName() {
89         return CSSConstants.CSS_BASELINE_SHIFT_PROPERTY;
90     }
91
92     /**
93      * Implements {@link ValueManager#getDefaultValue()}.
94      */

95     public Value getDefaultValue() {
96         return SVGValueConstants.BASELINE_VALUE;
97     }
98
99     /**
100      * Implements {@link ValueManager#createValue(LexicalUnit,CSSEngine)}.
101      */

102     public Value createValue(LexicalUnit lu, CSSEngine engine)
103         throws DOMException {
104         switch (lu.getLexicalUnitType()) {
105         case LexicalUnit.SAC_INHERIT:
106             return SVGValueConstants.INHERIT_VALUE;
107
108         case LexicalUnit.SAC_IDENT:
109             Object v = values.get(lu.getStringValue().toLowerCase().intern());
110             if (v == null) {
111                 throw createInvalidIdentifierDOMException(lu.getStringValue());
112             }
113             return (Value)v;
114         }
115         return super.createValue(lu, engine);
116     }
117
118     /**
119      * Implements {@link ValueManager#createStringValue(short,String,CSSEngine)}.
120      */

121     public Value createStringValue(short type, String value, CSSEngine engine)
122         throws DOMException {
123         if (type != CSSPrimitiveValue.CSS_IDENT) {
124             throw createInvalidIdentifierDOMException(value);
125         }
126         Object v = values.get(value.toLowerCase().intern());
127         if (v == null) {
128             throw createInvalidIdentifierDOMException(value);
129         }
130         return (Value)v;
131     }
132
133     /**
134      * Implements {@link
135      * ValueManager#computeValue(CSSStylableElement,String,CSSEngine,int,StyleMap,Value)}.
136      */

137     public Value computeValue(CSSStylableElement elt,
138                               String pseudo,
139                               CSSEngine engine,
140                               int idx,
141                               StyleMap sm,
142                               Value value) {
143         if (value.getPrimitiveType() == CSSPrimitiveValue.CSS_PERCENTAGE) {
144             sm.putLineHeightRelative(idx, true);
145
146             int fsi = engine.getLineHeightIndex();
147             CSSStylableElement parent;
148             parent = (CSSStylableElement)elt.getParentNode();
149             if (parent == null) {
150                 // Hmmm somthing pretty odd - can't happen accordint to spec,
151                 // should always have text parent.
152                 // http://www.w3.org/TR/SVG11/text.html#BaselineShiftProperty
153                 parent = elt;
154             }
155             Value fs = engine.getComputedStyle(parent, pseudo, fsi);
156             float fsv = fs.getFloatValue();
157             float v = value.getFloatValue();
158             return new FloatValue(CSSPrimitiveValue.CSS_NUMBER,
159                                   (fsv * v) / 100f);
160         }
161         return super.computeValue(elt, pseudo, engine, idx, sm, value);
162     }
163
164
165     /**
166      * Indicates the orientation of the property associated with
167      * this manager.
168      */

169     protected int getOrientation() {
170         return BOTH_ORIENTATION; // Not used
171     }
172
173 }
174