KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2
3    Copyright 2001-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
20 import java.util.List JavaDoc;
21 import java.util.LinkedList JavaDoc;
22
23 import org.w3c.dom.Element JavaDoc;
24 import org.w3c.dom.Node JavaDoc;
25
26 import org.apache.batik.dom.svg.XMLBaseSupport;
27 import org.apache.batik.dom.util.XLinkSupport;
28 import org.apache.batik.util.ParsedURL;
29
30 /**
31  * Bridge class for the <font-face> element.
32  *
33  * @author <a HREF="mailto:bella.robinson@cmis.csiro.au">Bella Robinson</a>
34  * @version $Id: SVGFontFaceElementBridge.java,v 1.10 2005/02/22 09:12:57 cam Exp $
35  */

36 public class SVGFontFaceElementBridge extends AbstractSVGBridge
37                                       implements ErrorConstants {
38
39     /**
40      * Constructs a new bridge for the &lt;font-face> element.
41      */

42     public SVGFontFaceElementBridge() {
43     }
44
45     /**
46      * Returns 'font-face'.
47      */

48     public String JavaDoc getLocalName() {
49         return SVG_FONT_FACE_TAG;
50     }
51
52     /**
53      * Creates an SVGFontFace that repesents the specified
54      * &lt;font-face> element.
55      *
56      * @param ctx The current bridge context.
57      * @param fontFaceElement The &lt;font-face> element.
58      *
59      * @return A new SVGFontFace.
60      */

61     public SVGFontFace createFontFace(BridgeContext ctx,
62                                       Element JavaDoc fontFaceElement) {
63
64         // get all the font-face attributes
65

66         String JavaDoc familyNames = fontFaceElement.getAttributeNS
67             (null, SVG_FONT_FAMILY_ATTRIBUTE);
68
69         // units per em
70
String JavaDoc unitsPerEmStr = fontFaceElement.getAttributeNS
71             (null, SVG_UNITS_PER_EM_ATTRIBUTE);
72         if (unitsPerEmStr.length() == 0) {
73             unitsPerEmStr = SVG_FONT_FACE_UNITS_PER_EM_DEFAULT_VALUE;
74         }
75         float unitsPerEm;
76         try {
77             unitsPerEm = SVGUtilities.convertSVGNumber(unitsPerEmStr);
78         } catch (NumberFormatException JavaDoc ex) {
79             throw new BridgeException
80                 (fontFaceElement, ERR_ATTRIBUTE_VALUE_MALFORMED,
81                 new Object JavaDoc [] {SVG_UNITS_PER_EM_ATTRIBUTE, unitsPerEmStr});
82         }
83
84         // font-weight
85
String JavaDoc fontWeight = fontFaceElement.getAttributeNS
86             (null, SVG_FONT_WEIGHT_ATTRIBUTE);
87         if (fontWeight.length() == 0) {
88             fontWeight = SVG_FONT_FACE_FONT_WEIGHT_DEFAULT_VALUE;
89         }
90
91         // font-style
92
String JavaDoc fontStyle = fontFaceElement.getAttributeNS
93             (null, SVG_FONT_STYLE_ATTRIBUTE);
94         if (fontStyle.length() == 0) {
95             fontStyle = SVG_FONT_FACE_FONT_STYLE_DEFAULT_VALUE;
96         }
97
98         // font-variant
99
String JavaDoc fontVariant = fontFaceElement.getAttributeNS
100             (null, SVG_FONT_VARIANT_ATTRIBUTE);
101          if (fontVariant.length() == 0) {
102             fontVariant = SVG_FONT_FACE_FONT_VARIANT_DEFAULT_VALUE;
103         }
104
105         // font-stretch
106
String JavaDoc fontStretch = fontFaceElement.getAttributeNS
107             (null, SVG_FONT_STRETCH_ATTRIBUTE);
108          if (fontStretch.length() == 0) {
109             fontStretch = SVG_FONT_FACE_FONT_STRETCH_DEFAULT_VALUE;
110         }
111
112         // slopeStr
113
String JavaDoc slopeStr = fontFaceElement.getAttributeNS
114             (null, SVG_SLOPE_ATTRIBUTE);
115         if (slopeStr.length() == 0) {
116             slopeStr = SVG_FONT_FACE_SLOPE_DEFAULT_VALUE;
117         }
118         float slope;
119         try {
120             slope = SVGUtilities.convertSVGNumber(slopeStr);
121         } catch (NumberFormatException JavaDoc ex) {
122             throw new BridgeException
123                 (fontFaceElement, ERR_ATTRIBUTE_VALUE_MALFORMED,
124                 new Object JavaDoc [] {SVG_FONT_FACE_SLOPE_DEFAULT_VALUE, slopeStr});
125         }
126
127         // panose-1
128
String JavaDoc panose1 = fontFaceElement.getAttributeNS
129             (null, SVG_PANOSE_1_ATTRIBUTE);
130          if (panose1.length() == 0) {
131             panose1 = SVG_FONT_FACE_PANOSE_1_DEFAULT_VALUE;
132         }
133
134         // ascent
135
String JavaDoc ascentStr = fontFaceElement.getAttributeNS
136             (null, SVG_ASCENT_ATTRIBUTE);
137         if (ascentStr.length() == 0) {
138             // set it to be unitsPerEm * .8
139
ascentStr = String.valueOf(unitsPerEm*.8);
140         }
141         float ascent;
142         try {
143            ascent = SVGUtilities.convertSVGNumber(ascentStr);
144         } catch (NumberFormatException JavaDoc ex) {
145             throw new BridgeException
146                 (fontFaceElement, ERR_ATTRIBUTE_VALUE_MALFORMED,
147                 new Object JavaDoc [] {SVG_FONT_FACE_SLOPE_DEFAULT_VALUE, ascentStr});
148         }
149
150         // descent
151
String JavaDoc descentStr = fontFaceElement.getAttributeNS
152             (null, SVG_DESCENT_ATTRIBUTE);
153         if (descentStr.length() == 0) {
154             // set it to be unitsPerEm *.2.
155
descentStr = String.valueOf(unitsPerEm*.2);
156         }
157         float descent;
158         try {
159             descent = SVGUtilities.convertSVGNumber(descentStr);
160         } catch (NumberFormatException JavaDoc ex) {
161             throw new BridgeException
162                 (fontFaceElement, ERR_ATTRIBUTE_VALUE_MALFORMED,
163                 new Object JavaDoc [] {SVG_FONT_FACE_SLOPE_DEFAULT_VALUE, descentStr});
164         }
165
166         // underline-position
167
String JavaDoc underlinePosStr = fontFaceElement.getAttributeNS
168             (null, SVG_UNDERLINE_POSITION_ATTRIBUTE);
169         if (underlinePosStr.length() == 0) {
170             underlinePosStr = String.valueOf(-3*unitsPerEm/40);
171         }
172         float underlinePos;
173         try {
174             underlinePos = SVGUtilities.convertSVGNumber(underlinePosStr);
175         } catch (NumberFormatException JavaDoc ex) {
176             throw new BridgeException
177                 (fontFaceElement, ERR_ATTRIBUTE_VALUE_MALFORMED,
178                 new Object JavaDoc [] {SVG_FONT_FACE_SLOPE_DEFAULT_VALUE,
179                                underlinePosStr});
180         }
181
182
183         // underline-thickness
184
String JavaDoc underlineThicknessStr = fontFaceElement.getAttributeNS
185             (null, SVG_UNDERLINE_THICKNESS_ATTRIBUTE);
186         if (underlineThicknessStr.length() == 0) {
187             underlineThicknessStr = String.valueOf(unitsPerEm/20);
188         }
189         float underlineThickness;
190         try {
191             underlineThickness =
192                 SVGUtilities.convertSVGNumber(underlineThicknessStr);
193         } catch (NumberFormatException JavaDoc ex) {
194             throw new BridgeException
195                 (fontFaceElement, ERR_ATTRIBUTE_VALUE_MALFORMED,
196                 new Object JavaDoc [] {SVG_FONT_FACE_SLOPE_DEFAULT_VALUE,
197                                underlineThicknessStr});
198         }
199
200
201         // strikethrough-position
202
String JavaDoc strikethroughPosStr = fontFaceElement.getAttributeNS
203             (null, SVG_STRIKETHROUGH_POSITION_ATTRIBUTE);
204         if (strikethroughPosStr.length() == 0) {
205             strikethroughPosStr = String.valueOf(3*ascent/8);
206         }
207         float strikethroughPos;
208         try {
209             strikethroughPos =
210                 SVGUtilities.convertSVGNumber(strikethroughPosStr);
211         } catch (NumberFormatException JavaDoc ex) {
212             throw new BridgeException
213                 (fontFaceElement, ERR_ATTRIBUTE_VALUE_MALFORMED,
214                 new Object JavaDoc [] {SVG_FONT_FACE_SLOPE_DEFAULT_VALUE,
215                                strikethroughPosStr});
216         }
217
218
219         // strikethrough-thickness
220
String JavaDoc strikethroughThicknessStr = fontFaceElement.getAttributeNS
221             (null, SVG_STRIKETHROUGH_THICKNESS_ATTRIBUTE);
222         if (strikethroughThicknessStr.length() == 0) {
223             strikethroughThicknessStr = String.valueOf(unitsPerEm/20);
224         }
225         float strikethroughThickness;
226         try {
227             strikethroughThickness =
228                 SVGUtilities.convertSVGNumber(strikethroughThicknessStr);
229         } catch (NumberFormatException JavaDoc ex) {
230             throw new BridgeException
231                 (fontFaceElement, ERR_ATTRIBUTE_VALUE_MALFORMED,
232                 new Object JavaDoc [] {SVG_FONT_FACE_SLOPE_DEFAULT_VALUE,
233                                strikethroughThicknessStr});
234         }
235
236         // overline-position
237
String JavaDoc overlinePosStr = fontFaceElement.getAttributeNS
238             (null, SVG_OVERLINE_POSITION_ATTRIBUTE);
239          if (overlinePosStr.length() == 0) {
240             overlinePosStr = String.valueOf(ascent);
241         }
242         float overlinePos;
243         try {
244             overlinePos = SVGUtilities.convertSVGNumber(overlinePosStr);
245         } catch (NumberFormatException JavaDoc ex) {
246             throw new BridgeException
247                 (fontFaceElement, ERR_ATTRIBUTE_VALUE_MALFORMED,
248                 new Object JavaDoc [] {SVG_FONT_FACE_SLOPE_DEFAULT_VALUE,
249                                overlinePosStr});
250         }
251
252
253         // overline-thickness
254
String JavaDoc overlineThicknessStr = fontFaceElement.getAttributeNS
255             (null, SVG_OVERLINE_THICKNESS_ATTRIBUTE);
256         if (overlineThicknessStr.length() == 0) {
257             overlineThicknessStr = String.valueOf(unitsPerEm/20);
258         }
259         float overlineThickness;
260         try {
261             overlineThickness =
262                 SVGUtilities.convertSVGNumber(overlineThicknessStr);
263         } catch (NumberFormatException JavaDoc ex) {
264             throw new BridgeException
265                 (fontFaceElement, ERR_ATTRIBUTE_VALUE_MALFORMED,
266                 new Object JavaDoc [] {SVG_FONT_FACE_SLOPE_DEFAULT_VALUE,
267                                overlineThicknessStr});
268         }
269
270         List JavaDoc srcs = null;
271         Element JavaDoc fontElt = SVGUtilities.getParentElement(fontFaceElement);
272         if (!fontElt.getNamespaceURI().equals(SVG_NAMESPACE_URI) ||
273             !fontElt.getLocalName().equals(SVG_FONT_TAG)) {
274             srcs = getFontFaceSrcs(fontFaceElement);
275         }
276
277         // TODO: get the rest of the attributes
278
return new SVGFontFace(fontFaceElement, srcs,
279                                familyNames, unitsPerEm, fontWeight, fontStyle,
280                                fontVariant, fontStretch, slope, panose1,
281                                ascent, descent, strikethroughPos,
282                                strikethroughThickness, underlinePos,
283                                underlineThickness, overlinePos,
284                                overlineThickness);
285     }
286
287
288     public List JavaDoc getFontFaceSrcs(Element JavaDoc fontFaceElement) {
289         // Search for a font-face-src element
290
Element JavaDoc ffsrc = null;
291         for (Node JavaDoc n = fontFaceElement.getFirstChild();
292              n != null;
293              n = n.getNextSibling()) {
294             if ((n.getNodeType() == Node.ELEMENT_NODE) &&
295                 n.getNamespaceURI().equals(SVG_NAMESPACE_URI) &&
296                 n.getLocalName().equals(SVG_FONT_FACE_SRC_TAG)) {
297                     ffsrc = (Element JavaDoc)n;
298                     break;
299             }
300         }
301         if (ffsrc == null)
302             return null;
303
304         List JavaDoc ret = new LinkedList JavaDoc();
305
306         // Search for a font-face-uri, or font-face-name elements
307
for (Node JavaDoc n = ffsrc.getFirstChild();
308              n != null;
309              n = n.getNextSibling()) {
310             if ((n.getNodeType() != Node.ELEMENT_NODE) ||
311                 !n.getNamespaceURI().equals(SVG_NAMESPACE_URI))
312                 continue;
313
314             if (n.getLocalName().equals(SVG_FONT_FACE_URI_TAG)) {
315                 Element JavaDoc ffuri = (Element JavaDoc)n;
316                 String JavaDoc uri = XLinkSupport.getXLinkHref(ffuri);
317                 String JavaDoc base = XMLBaseSupport.getCascadedXMLBase(ffuri);
318                 ParsedURL purl;
319                 if (base != null) purl = new ParsedURL(base, uri);
320                 else purl = new ParsedURL(uri);
321                 ret.add(purl);
322                 continue;
323             }
324             if (n.getLocalName().equals(SVG_FONT_FACE_NAME_TAG)) {
325                 Element JavaDoc ffname = (Element JavaDoc)n;
326                 String JavaDoc s = ffname.getAttribute("name");
327                 if (s.length() != 0)
328                     ret.add(s);
329             }
330         }
331         return ret;
332     }
333 }
334
Popular Tags