1 18 package org.apache.batik.bridge; 19 20 import java.awt.Font ; 21 import java.awt.GraphicsEnvironment ; 22 import java.util.HashSet ; 23 import java.util.Iterator ; 24 import java.util.LinkedList ; 25 import java.util.List ; 26 import java.util.Set ; 27 28 import org.apache.batik.util.ParsedURL; 29 30 import org.w3c.dom.Element ; 31 import org.w3c.dom.Node ; 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 46 public abstract class FontFace extends GVTFontFace 47 implements ErrorConstants { 48 49 53 List srcs; 54 55 58 public FontFace 59 (List srcs, 60 String familyName, float unitsPerEm, String fontWeight, 61 String fontStyle, String fontVariant, String fontStretch, 62 float slope, String 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 78 protected FontFace(String familyName) { 79 super(familyName); 80 } 81 82 public static CSSFontFace createFontFace(String familyName, 83 FontFace src) { 84 return new CSSFontFace 85 (new LinkedList (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 97 public GVTFontFamily getFontFamily(BridgeContext ctx) { 98 String name = FontFamilyResolver.lookup(familyName); 99 if (name != null) { 100 GVTFontFace ff = createFontFace(name, this); 101 return new AWTFontFamily(ff); 102 } 103 104 Iterator iter = srcs.iterator(); 105 while (iter.hasNext()) { 106 Object o = iter.next(); 107 if (o instanceof String ) { 108 String str = (String )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 ex) { 120 ctx.getUserAgent().displayError(ex); 122 } catch (BridgeException ex) { 123 if (ERR_URI_UNSECURE.equals(ex.getCode())) 126 ctx.getUserAgent().displayError(ex); 127 } catch (Exception ex) { 128 } 130 } 131 } 132 133 return new AWTFontFamily(this); 134 } 135 136 139 protected GVTFontFamily getFontFamily(BridgeContext ctx, 140 ParsedURL purl) { 141 String purlStr = purl.toString(); 142 143 Element e = getBaseElement(ctx); 144 SVGDocument svgDoc = (SVGDocument)e.getOwnerDocument(); 145 String docURL = svgDoc.getURL(); 146 ParsedURL pDocURL = null; 147 if (docURL != null) 148 pDocURL = new ParsedURL(docURL); 149 150 String 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 ex) { 158 userAgent.displayError(ex); 164 return null; 165 } 166 167 if (purl.getRef() != null) { 168 Element 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 fontElt = ref; 179 if (doc != rdoc) { 180 fontElt = (Element )doc.importNode(ref, true); 181 String base = XMLBaseSupport.getCascadedXMLBase(ref); 182 Element 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 Element fontFaceElt = null; 191 for (Node 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 )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 try { 213 Font font = Font.createFont(Font.TRUETYPE_FONT, 214 purl.openStream()); 215 return new AWTFontFamily(this, font); 216 } catch (Exception ex) { 217 } 218 return null; 219 } 220 221 225 protected Element getBaseElement(BridgeContext ctx) { 226 SVGDocument d = (SVGDocument)ctx.getDocument(); 227 return d.getRootElement(); 228 } 229 230 } 231 | Popular Tags |