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.bridge;
020:
021: import java.util.List;
022: import java.util.LinkedList;
023:
024: import org.apache.batik.css.engine.CSSEngine;
025: import org.apache.batik.css.engine.FontFaceRule;
026: import org.apache.batik.css.engine.StyleMap;
027: import org.apache.batik.css.engine.SVGCSSEngine;
028: import org.apache.batik.css.engine.value.Value;
029: import org.apache.batik.css.engine.value.ValueConstants;
030: import org.apache.batik.css.engine.value.ValueManager;
031: import org.apache.batik.gvt.font.GVTFontFamily;
032: import org.apache.batik.util.ParsedURL;
033: import org.apache.batik.util.SVGConstants;
034: import org.w3c.dom.css.CSSValue;
035: import org.w3c.dom.css.CSSPrimitiveValue;
036:
037: /**
038: * This class represents a <font-face> element or @font-face rule
039: *
040: * @author <a href="mailto:bella.robinson@cmis.csiro.au">Bella Robinson</a>
041: * @version $Id: CSSFontFace.java 475477 2006-11-15 22:44:28Z cam $
042: */
043: public class CSSFontFace extends FontFace implements SVGConstants {
044:
045: GVTFontFamily fontFamily = null;
046:
047: /**
048: * Constructes an CSSFontFace with the specfied font-face attributes.
049: */
050: public CSSFontFace(List srcs, String familyName, float unitsPerEm,
051: String fontWeight, String fontStyle, String fontVariant,
052: String fontStretch, float slope, String panose1,
053: float ascent, float descent, float strikethroughPosition,
054: float strikethroughThickness, float underlinePosition,
055: float underlineThickness, float overlinePosition,
056: float overlineThickness) {
057: super (srcs, familyName, unitsPerEm, fontWeight, fontStyle,
058: fontVariant, fontStretch, slope, panose1, ascent,
059: descent, strikethroughPosition, strikethroughThickness,
060: underlinePosition, underlineThickness,
061: overlinePosition, overlineThickness);
062: }
063:
064: protected CSSFontFace(String familyName) {
065: super (familyName);
066: }
067:
068: public static CSSFontFace createCSSFontFace(CSSEngine eng,
069: FontFaceRule ffr) {
070: StyleMap sm = ffr.getStyleMap();
071: String familyName = getStringProp(sm, eng,
072: SVGCSSEngine.FONT_FAMILY_INDEX);
073:
074: CSSFontFace ret = new CSSFontFace(familyName);
075:
076: Value v;
077: v = sm.getValue(SVGCSSEngine.FONT_WEIGHT_INDEX);
078: if (v != null)
079: ret.fontWeight = v.getCssText();
080: v = sm.getValue(SVGCSSEngine.FONT_STYLE_INDEX);
081: if (v != null)
082: ret.fontStyle = v.getCssText();
083: v = sm.getValue(SVGCSSEngine.FONT_VARIANT_INDEX);
084: if (v != null)
085: ret.fontVariant = v.getCssText();
086: v = sm.getValue(SVGCSSEngine.FONT_STRETCH_INDEX);
087: if (v != null)
088: ret.fontStretch = v.getCssText();
089: v = sm.getValue(SVGCSSEngine.SRC_INDEX);
090:
091: ParsedURL base = ffr.getURL();
092: if ((v != null) && (v != ValueConstants.NONE_VALUE)) {
093: if (v.getCssValueType() == CSSValue.CSS_PRIMITIVE_VALUE) {
094: ret.srcs = new LinkedList();
095: ret.srcs.add(getSrcValue(v, base));
096: } else if (v.getCssValueType() == CSSValue.CSS_VALUE_LIST) {
097: ret.srcs = new LinkedList();
098: for (int i = 0; i < v.getLength(); i++) {
099: ret.srcs.add(getSrcValue(v.item(i), base));
100: }
101: }
102: }
103: /*
104: float unitsPerEm = getFloatProp
105: (sm, eng, SVGCSSEngine.UNITS_PER_EM_INDEX);
106: String slope = getFloatProp
107: (sm, eng, SVGCSSEngine.SLOPE_INDEX);
108: String panose1 = getStringProp
109: (sm, eng, SVGCSSEngine.PANOSE1_INDEX);
110: String ascent = getFloatProp
111: (sm, eng, SVGCSSEngine.ASCENT_INDEX);
112: String descent = getFloatProp
113: (sm, eng, SVGCSSEngine.DESCENT_INDEX);
114: String strikethroughPosition = getFloatProp
115: (sm, eng, SVGCSSEngine.STRIKETHROUGH_POSITION_INDEX);
116: String strikethroughThickness = getFloatProp
117: (sm, eng, SVGCSSEngine.STRIKETHROUGH_THICKNESS_INDEX);
118: String underlinePosition = getFloatProp
119: (sm, eng, SVGCSSEngine.UNDERLINE_POSITION_INDEX);
120: String underlineThickness = getFloatProp
121: (sm, eng, SVGCSSEngine.UNDERLINE_THICKNESS_INDEX);
122: String overlinePosition = getFloatProp
123: (sm, eng, SVGCSSEngine.OVERLINE_POSITION_INDEX);
124: String overlineThickness = getFloatProp
125: (sm, eng, SVGCSSEngine.OVERLINE_THICKNESS_INDEX);
126: */
127: return ret;
128: }
129:
130: public static Object getSrcValue(Value v, ParsedURL base) {
131: if (v.getCssValueType() != CSSValue.CSS_PRIMITIVE_VALUE)
132: return null;
133: if (v.getPrimitiveType() == CSSPrimitiveValue.CSS_URI) {
134: if (base != null)
135: return new ParsedURL(base, v.getStringValue());
136: return new ParsedURL(v.getStringValue());
137: }
138: if (v.getPrimitiveType() == CSSPrimitiveValue.CSS_STRING)
139: return v.getStringValue();
140: return null;
141: }
142:
143: public static String getStringProp(StyleMap sm, CSSEngine eng,
144: int pidx) {
145: Value v = sm.getValue(pidx);
146: ValueManager[] vms = eng.getValueManagers();
147: if (v == null) {
148: ValueManager vm = vms[pidx];
149: v = vm.getDefaultValue();
150: }
151: while (v.getCssValueType() == CSSValue.CSS_VALUE_LIST) {
152: v = v.item(0);
153: }
154: return v.getStringValue();
155: }
156:
157: public static float getFloatProp(StyleMap sm, CSSEngine eng,
158: int pidx) {
159: Value v = sm.getValue(pidx);
160: ValueManager[] vms = eng.getValueManagers();
161: if (v == null) {
162: ValueManager vm = vms[pidx];
163: v = vm.getDefaultValue();
164: }
165: while (v.getCssValueType() == CSSValue.CSS_VALUE_LIST) {
166: v = v.item(0);
167: }
168: return v.getFloatValue();
169: }
170:
171: /**
172: * Returns the font associated with this rule or element.
173: */
174: public GVTFontFamily getFontFamily(BridgeContext ctx) {
175: if (fontFamily != null)
176: return fontFamily;
177:
178: fontFamily = super.getFontFamily(ctx);
179: return fontFamily;
180: }
181: }
|