001: /*
002:
003: Licensed to the Apache Software Foundation (ASF) under one or more
004: contributor license agreements. See the NOTICE file distributed with
005: this work for additional information regarding copyright ownership.
006: The ASF licenses this file to You under the Apache License, Version 2.0
007: (the "License"); you may not use this file except in compliance with
008: the License. You may obtain a copy of the License at
009:
010: http://www.apache.org/licenses/LICENSE-2.0
011:
012: Unless required by applicable law or agreed to in writing, software
013: distributed under the License is distributed on an "AS IS" BASIS,
014: WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015: See the License for the specific language governing permissions and
016: limitations under the License.
017:
018: */
019: package org.apache.batik.css.engine.value.css2;
020:
021: import org.apache.batik.css.engine.CSSContext;
022: import org.apache.batik.css.engine.CSSEngine;
023: import org.apache.batik.css.engine.CSSStylableElement;
024: import org.apache.batik.css.engine.StyleMap;
025: import org.apache.batik.css.engine.value.AbstractValueManager;
026: import org.apache.batik.css.engine.value.ListValue;
027: import org.apache.batik.css.engine.value.StringMap;
028: import org.apache.batik.css.engine.value.StringValue;
029: import org.apache.batik.css.engine.value.Value;
030: import org.apache.batik.css.engine.value.ValueConstants;
031: import org.apache.batik.css.engine.value.ValueManager;
032: import org.apache.batik.util.CSSConstants;
033: import org.apache.batik.util.SVGTypes;
034:
035: import org.w3c.css.sac.LexicalUnit;
036: import org.w3c.dom.DOMException;
037: import org.w3c.dom.css.CSSPrimitiveValue;
038:
039: /**
040: * This class provides a factory for the 'font-family' property values.
041: *
042: * @author <a href="mailto:stephane@hillion.org">Stephane Hillion</a>
043: * @version $Id: FontFamilyManager.java 478160 2006-11-22 13:35:06Z dvholten $
044: */
045: public class FontFamilyManager extends AbstractValueManager {
046:
047: /**
048: * The default value.
049: */
050: protected static final ListValue DEFAULT_VALUE = new ListValue();
051: static {
052: DEFAULT_VALUE.append(new StringValue(
053: CSSPrimitiveValue.CSS_STRING, "Arial"));
054: DEFAULT_VALUE.append(new StringValue(
055: CSSPrimitiveValue.CSS_STRING, "Helvetica"));
056: DEFAULT_VALUE.append(new StringValue(
057: CSSPrimitiveValue.CSS_IDENT,
058: CSSConstants.CSS_SANS_SERIF_VALUE));
059: }
060:
061: /**
062: * The identifier values.
063: */
064: protected static final StringMap values = new StringMap();
065: static {
066: values.put(CSSConstants.CSS_CURSIVE_VALUE,
067: ValueConstants.CURSIVE_VALUE);
068: values.put(CSSConstants.CSS_FANTASY_VALUE,
069: ValueConstants.FANTASY_VALUE);
070: values.put(CSSConstants.CSS_MONOSPACE_VALUE,
071: ValueConstants.MONOSPACE_VALUE);
072: values.put(CSSConstants.CSS_SERIF_VALUE,
073: ValueConstants.SERIF_VALUE);
074: values.put(CSSConstants.CSS_SANS_SERIF_VALUE,
075: ValueConstants.SANS_SERIF_VALUE);
076: }
077:
078: /**
079: * Implements {@link ValueManager#isInheritedProperty()}.
080: */
081: public boolean isInheritedProperty() {
082: return true;
083: }
084:
085: /**
086: * Implements {@link ValueManager#isAnimatableProperty()}.
087: */
088: public boolean isAnimatableProperty() {
089: return true;
090: }
091:
092: /**
093: * Implements {@link ValueManager#isAdditiveProperty()}.
094: */
095: public boolean isAdditiveProperty() {
096: return false;
097: }
098:
099: /**
100: * Implements {@link ValueManager#getPropertyType()}.
101: */
102: public int getPropertyType() {
103: return SVGTypes.TYPE_FONT_FAMILY_VALUE;
104: }
105:
106: /**
107: * Implements {@link ValueManager#getPropertyName()}.
108: */
109: public String getPropertyName() {
110: return CSSConstants.CSS_FONT_FAMILY_PROPERTY;
111: }
112:
113: /**
114: * Implements {@link ValueManager#getDefaultValue()}.
115: */
116: public Value getDefaultValue() {
117: return DEFAULT_VALUE;
118: }
119:
120: /**
121: * Implements {@link ValueManager#createValue(LexicalUnit,CSSEngine)}.
122: */
123: public Value createValue(LexicalUnit lu, CSSEngine engine)
124: throws DOMException {
125: switch (lu.getLexicalUnitType()) {
126: case LexicalUnit.SAC_INHERIT:
127: return ValueConstants.INHERIT_VALUE;
128:
129: default:
130: throw createInvalidLexicalUnitDOMException(lu
131: .getLexicalUnitType());
132:
133: case LexicalUnit.SAC_IDENT:
134: case LexicalUnit.SAC_STRING_VALUE:
135: }
136: ListValue result = new ListValue();
137: for (;;) {
138: switch (lu.getLexicalUnitType()) {
139: case LexicalUnit.SAC_STRING_VALUE:
140: result.append(new StringValue(
141: CSSPrimitiveValue.CSS_STRING, lu
142: .getStringValue()));
143: lu = lu.getNextLexicalUnit();
144: break;
145:
146: case LexicalUnit.SAC_IDENT:
147: StringBuffer sb = new StringBuffer(lu.getStringValue());
148: lu = lu.getNextLexicalUnit();
149: if (lu != null
150: && lu.getLexicalUnitType() == LexicalUnit.SAC_IDENT) {
151: do {
152: sb.append(' ');
153: sb.append(lu.getStringValue());
154: lu = lu.getNextLexicalUnit();
155: } while (lu != null
156: && lu.getLexicalUnitType() == LexicalUnit.SAC_IDENT);
157: result
158: .append(new StringValue(
159: CSSPrimitiveValue.CSS_STRING, sb
160: .toString()));
161: } else {
162: String id = sb.toString();
163: String s = id.toLowerCase().intern();
164: Value v = (Value) values.get(s);
165: result.append((v != null) ? v : new StringValue(
166: CSSPrimitiveValue.CSS_STRING, id));
167: }
168: }
169: if (lu == null) {
170: return result;
171: }
172: if (lu.getLexicalUnitType() != LexicalUnit.SAC_OPERATOR_COMMA) {
173: throw createInvalidLexicalUnitDOMException(lu
174: .getLexicalUnitType());
175: }
176: lu = lu.getNextLexicalUnit();
177: if (lu == null) {
178: throw createMalformedLexicalUnitDOMException();
179: }
180: }
181: }
182:
183: /**
184: * Implements {@link
185: * ValueManager#computeValue(CSSStylableElement,String,CSSEngine,int,StyleMap,Value)}.
186: */
187: public Value computeValue(CSSStylableElement elt, String pseudo,
188: CSSEngine engine, int idx, StyleMap sm, Value value) {
189: if (value == DEFAULT_VALUE) {
190: CSSContext ctx = engine.getCSSContext();
191: value = ctx.getDefaultFontFamily();
192: }
193: return value;
194: }
195: }
|