KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > batik > bridge > CSSFontFace


1 /*
2
3    Copyright 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.bridge;
19 import java.util.List JavaDoc;
20 import java.util.LinkedList JavaDoc;
21
22 import org.apache.batik.css.engine.CSSEngine;
23 import org.apache.batik.css.engine.FontFaceRule;
24 import org.apache.batik.css.engine.StyleMap;
25 import org.apache.batik.css.engine.SVGCSSEngine;
26 import org.apache.batik.css.engine.value.Value;
27 import org.apache.batik.css.engine.value.ValueConstants;
28 import org.apache.batik.css.engine.value.ValueManager;
29 import org.apache.batik.gvt.font.GVTFontFamily;
30 import org.apache.batik.util.ParsedURL;
31 import org.apache.batik.util.SVGConstants;
32 import org.w3c.dom.css.CSSValue;
33 import org.w3c.dom.css.CSSPrimitiveValue;
34
35 /**
36  * This class represents a <font-face> element or @font-face rule
37  *
38  * @author <a HREF="mailto:bella.robinson@cmis.csiro.au">Bella Robinson</a>
39  * @version $Id: CSSFontFace.java,v 1.4 2004/08/18 07:12:31 vhardy Exp $
40  */

41 public class CSSFontFace extends FontFace implements SVGConstants {
42
43     GVTFontFamily fontFamily = null;
44
45     /**
46      * Constructes an CSSFontFace with the specfied font-face attributes.
47      */

48     public CSSFontFace
49         (List JavaDoc srcs,
50          String JavaDoc familyName, float unitsPerEm, String JavaDoc fontWeight,
51          String JavaDoc fontStyle, String JavaDoc fontVariant, String JavaDoc fontStretch,
52          float slope, String JavaDoc panose1, float ascent, float descent,
53          float strikethroughPosition, float strikethroughThickness,
54          float underlinePosition, float underlineThickness,
55          float overlinePosition, float overlineThickness) {
56         super(srcs,
57               familyName, unitsPerEm, fontWeight, fontStyle,
58               fontVariant, fontStretch, slope, panose1, ascent, descent,
59               strikethroughPosition, strikethroughThickness,
60               underlinePosition, underlineThickness,
61               overlinePosition, overlineThickness);
62     }
63
64     protected CSSFontFace(String JavaDoc familyName) {
65         super(familyName);
66     }
67
68     public static CSSFontFace createCSSFontFace(CSSEngine eng,
69                                                 FontFaceRule ffr) {
70         StyleMap sm = ffr.getStyleMap();
71         String JavaDoc familyName = getStringProp
72             (sm, eng, SVGCSSEngine.FONT_FAMILY_INDEX);
73
74         CSSFontFace ret = new CSSFontFace(familyName);
75
76         Value v;
77         v = sm.getValue(SVGCSSEngine.FONT_WEIGHT_INDEX);
78         if (v != null)
79             ret.fontWeight = v.getCssText();
80         v = sm.getValue(SVGCSSEngine.FONT_STYLE_INDEX);
81         if (v != null)
82             ret.fontStyle = v.getCssText();
83         v = sm.getValue(SVGCSSEngine.FONT_VARIANT_INDEX);
84         if (v != null)
85             ret.fontVariant = v.getCssText();
86         v = sm.getValue(SVGCSSEngine.FONT_STRETCH_INDEX);
87         if (v != null)
88             ret.fontStretch = v.getCssText();
89         v = sm.getValue(SVGCSSEngine.SRC_INDEX);
90         
91         ParsedURL base = ffr.getURL();
92         if ((v != null) && (v != ValueConstants.NONE_VALUE)) {
93             if (v.getCssValueType() == CSSValue.CSS_PRIMITIVE_VALUE) {
94                 ret.srcs = new LinkedList JavaDoc();
95                 ret.srcs.add(getSrcValue(v, base));
96             } else if (v.getCssValueType() == CSSValue.CSS_VALUE_LIST) {
97                 ret.srcs = new LinkedList JavaDoc();
98                 for (int i=0; i<v.getLength(); i++) {
99                     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 JavaDoc 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     public static String JavaDoc getStringProp(StyleMap sm, CSSEngine eng, int pidx) {
143         Value v = sm.getValue(pidx);
144         ValueManager [] vms = eng.getValueManagers();
145         if (v == null) {
146             ValueManager vm = vms[pidx];
147             v = vm.getDefaultValue();
148         }
149         while (v.getCssValueType() == CSSValue.CSS_VALUE_LIST) {
150             v = v.item(0);
151         }
152         return v.getStringValue();
153     }
154
155     public static float getFloatProp(StyleMap sm, CSSEngine eng, int pidx) {
156         Value v = sm.getValue(pidx);
157         ValueManager [] vms = eng.getValueManagers();
158         if (v == null) {
159             ValueManager vm = vms[pidx];
160             v = vm.getDefaultValue();
161         }
162         while (v.getCssValueType() == CSSValue.CSS_VALUE_LIST) {
163             v = v.item(0);
164         }
165         return v.getFloatValue();
166     }
167
168     /**
169      * Returns the font associated with this rule or element.
170      */

171     public GVTFontFamily getFontFamily(BridgeContext ctx) {
172         if (fontFamily != null)
173             return fontFamily ;
174
175         fontFamily = super.getFontFamily(ctx);
176         return fontFamily;
177     }
178 }
179
Popular Tags