KickJava   Java API By Example, From Geeks To Geeks.

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


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
20 import java.awt.Font JavaDoc;
21 import java.awt.GraphicsEnvironment JavaDoc;
22 import java.util.HashSet JavaDoc;
23 import java.util.Iterator JavaDoc;
24 import java.util.LinkedList JavaDoc;
25 import java.util.List JavaDoc;
26 import java.util.Set JavaDoc;
27
28 import org.apache.batik.util.ParsedURL;
29
30 import org.w3c.dom.Element JavaDoc;
31 import org.w3c.dom.Node JavaDoc;
32 import org.w3c.dom.svg.SVGDocument;
33
34 import org.apache.batik.dom.svg.XMLBaseSupport;
35 import org.apache.batik.gvt.font.GVTFontFamily;
36 import org.apache.batik.gvt.font.GVTFontFace;
37 import org.apache.batik.gvt.font.AWTFontFamily;
38 import org.apache.batik.gvt.font.FontFamilyResolver;
39
40 /**
41  * This class represents a <font-face> element or @font-face rule
42  *
43  * @author <a HREF="mailto:bella.robinson@cmis.csiro.au">Bella Robinson</a>
44  * @version $Id: FontFace.java,v 1.9 2005/02/22 09:12:57 cam Exp $
45  */

46 public abstract class FontFace extends GVTFontFace
47     implements ErrorConstants {
48
49     /**
50      * List of ParsedURL's referencing SVGFonts or TrueType fonts,
51      * or Strings naming locally installed fonts.
52      */

53     List JavaDoc srcs;
54
55     /**
56      * Constructes an SVGFontFace with the specfied font-face attributes.
57      */

58     public FontFace
59         (List JavaDoc srcs,
60          String JavaDoc familyName, float unitsPerEm, String JavaDoc fontWeight,
61          String JavaDoc fontStyle, String JavaDoc fontVariant, String JavaDoc fontStretch,
62          float slope, String JavaDoc panose1, float ascent, float descent,
63          float strikethroughPosition, float strikethroughThickness,
64          float underlinePosition, float underlineThickness,
65          float overlinePosition, float overlineThickness) {
66         super(familyName, unitsPerEm, fontWeight,
67               fontStyle, fontVariant, fontStretch,
68               slope, panose1, ascent, descent,
69               strikethroughPosition, strikethroughThickness,
70               underlinePosition, underlineThickness,
71               overlinePosition, overlineThickness);
72         this.srcs = srcs;
73     }
74
75     /**
76      * Constructes an SVGFontFace with the specfied fontName.
77      */

78     protected FontFace(String JavaDoc familyName) {
79         super(familyName);
80     }
81
82     public static CSSFontFace createFontFace(String JavaDoc familyName,
83                                              FontFace src) {
84         return new CSSFontFace
85             (new LinkedList JavaDoc(src.srcs),
86              familyName, src.unitsPerEm, src.fontWeight,
87              src.fontStyle, src.fontVariant, src.fontStretch,
88              src.slope, src.panose1, src.ascent, src.descent,
89              src.strikethroughPosition, src.strikethroughThickness,
90              src.underlinePosition, src.underlineThickness,
91              src.overlinePosition, src.overlineThickness);
92     }
93     
94     /**
95      * Returns the font associated with this rule or element.
96      */

97     public GVTFontFamily getFontFamily(BridgeContext ctx) {
98         String JavaDoc name = FontFamilyResolver.lookup(familyName);
99         if (name != null) {
100             GVTFontFace ff = createFontFace(name, this);
101             return new AWTFontFamily(ff);
102         }
103
104         Iterator JavaDoc iter = srcs.iterator();
105         while (iter.hasNext()) {
106             Object JavaDoc o = iter.next();
107             if (o instanceof String JavaDoc) {
108                 String JavaDoc str = (String JavaDoc)o;
109                 name = FontFamilyResolver.lookup(str);
110                 if (name != null) {
111                     GVTFontFace ff = createFontFace(str, this);
112                     return new AWTFontFamily(ff);
113                 }
114             } else if (o instanceof ParsedURL) {
115                 try {
116                     GVTFontFamily ff = getFontFamily(ctx, (ParsedURL)o);
117                     if (ff != null)
118                         return ff;
119                 } catch (SecurityException JavaDoc ex) {
120                     // Security violation notify the user but keep going.
121
ctx.getUserAgent().displayError(ex);
122                 } catch (BridgeException ex) {
123                     // If Security violation notify
124
// the user but keep going.
125
if (ERR_URI_UNSECURE.equals(ex.getCode()))
126                         ctx.getUserAgent().displayError(ex);
127                 } catch (Exception JavaDoc ex) {
128                     // Do nothing couldn't get Referenced URL.
129
}
130             }
131         }
132
133         return new AWTFontFamily(this);
134     }
135
136     /**
137      * Tries to build a GVTFontFamily from a URL reference
138      */

139     protected GVTFontFamily getFontFamily(BridgeContext ctx,
140                                           ParsedURL purl) {
141         String JavaDoc purlStr = purl.toString();
142
143         Element JavaDoc e = getBaseElement(ctx);
144         SVGDocument svgDoc = (SVGDocument)e.getOwnerDocument();
145         String JavaDoc docURL = svgDoc.getURL();
146         ParsedURL pDocURL = null;
147         if (docURL != null)
148             pDocURL = new ParsedURL(docURL);
149
150         // try to load an SVG document
151
String JavaDoc baseURI = XMLBaseSupport.getCascadedXMLBase(e);
152         purl = new ParsedURL(baseURI, purlStr);
153         UserAgent userAgent = ctx.getUserAgent();
154
155         try {
156             userAgent.checkLoadExternalResource(purl, pDocURL);
157         } catch (SecurityException JavaDoc ex) {
158             // Can't load font - Security violation.
159
// We should not throw the error that is for certain, just
160
// move down the font list, but do we display the error or not???
161
// I'll vote yes just because it is a security exception (other
162
// exceptions like font not available etc I would skip).
163
userAgent.displayError(ex);
164             return null;
165         }
166
167         if (purl.getRef() != null) {
168             // Reference must be to a SVGFont.
169
Element JavaDoc ref = ctx.getReferencedElement(e, purlStr);
170             if (!ref.getNamespaceURI().equals(SVG_NAMESPACE_URI) ||
171                 !ref.getLocalName().equals(SVG_FONT_TAG)) {
172                 return null;
173             }
174
175             SVGDocument doc = (SVGDocument)e.getOwnerDocument();
176             SVGDocument rdoc = (SVGDocument)ref.getOwnerDocument();
177
178             Element JavaDoc fontElt = ref;
179             if (doc != rdoc) {
180                 fontElt = (Element JavaDoc)doc.importNode(ref, true);
181                 String JavaDoc base = XMLBaseSupport.getCascadedXMLBase(ref);
182                 Element JavaDoc g = doc.createElementNS(SVG_NAMESPACE_URI, SVG_G_TAG);
183                 g.appendChild(fontElt);
184                 g.setAttributeNS(XMLBaseSupport.XML_NAMESPACE_URI,
185                                  "xml:base", base);
186                 CSSUtilities.computeStyleAndURIs(ref, fontElt, purlStr);
187             }
188             
189             // Search for a font-face element
190
Element JavaDoc fontFaceElt = null;
191             for (Node JavaDoc n = fontElt.getFirstChild();
192                  n != null;
193                  n = n.getNextSibling()) {
194                 if ((n.getNodeType() == Node.ELEMENT_NODE) &&
195                     n.getNamespaceURI().equals(SVG_NAMESPACE_URI) &&
196                     n.getLocalName().equals(SVG_FONT_FACE_TAG)) {
197                     fontFaceElt = (Element JavaDoc)n;
198                     break;
199                 }
200             }
201             
202             SVGFontFaceElementBridge fontFaceBridge;
203             fontFaceBridge = (SVGFontFaceElementBridge)ctx.getBridge
204                 (SVG_NAMESPACE_URI, SVG_FONT_FACE_TAG);
205             GVTFontFace gff = fontFaceBridge.createFontFace(ctx, fontFaceElt);
206             
207
208             return new SVGFontFamily(gff, fontElt, ctx);
209         }
210         // Must be a reference to a 'Web Font'.
211
// Let's see if JDK can parse it.
212
try {
213             Font JavaDoc font = Font.createFont(Font.TRUETYPE_FONT,
214                                         purl.openStream());
215             return new AWTFontFamily(this, font);
216         } catch (Exception JavaDoc ex) {
217         }
218         return null;
219     }
220
221     /**
222      * Default implementation uses the root element of the document
223      * associated with BridgeContext. This is useful for CSS case.
224      */

225     protected Element JavaDoc getBaseElement(BridgeContext ctx) {
226         SVGDocument d = (SVGDocument)ctx.getDocument();
227         return d.getRootElement();
228     }
229
230 }
231
Popular Tags