1 18 package org.apache.batik.bridge; 19 20 import java.util.List ; 21 import java.util.LinkedList ; 22 23 import org.w3c.dom.Element ; 24 import org.w3c.dom.Node ; 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 36 public class SVGFontFaceElementBridge extends AbstractSVGBridge 37 implements ErrorConstants { 38 39 42 public SVGFontFaceElementBridge() { 43 } 44 45 48 public String getLocalName() { 49 return SVG_FONT_FACE_TAG; 50 } 51 52 61 public SVGFontFace createFontFace(BridgeContext ctx, 62 Element fontFaceElement) { 63 64 66 String familyNames = fontFaceElement.getAttributeNS 67 (null, SVG_FONT_FAMILY_ATTRIBUTE); 68 69 String 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 ex) { 79 throw new BridgeException 80 (fontFaceElement, ERR_ATTRIBUTE_VALUE_MALFORMED, 81 new Object [] {SVG_UNITS_PER_EM_ATTRIBUTE, unitsPerEmStr}); 82 } 83 84 String 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 String 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 String 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 String 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 String 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 ex) { 122 throw new BridgeException 123 (fontFaceElement, ERR_ATTRIBUTE_VALUE_MALFORMED, 124 new Object [] {SVG_FONT_FACE_SLOPE_DEFAULT_VALUE, slopeStr}); 125 } 126 127 String 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 String ascentStr = fontFaceElement.getAttributeNS 136 (null, SVG_ASCENT_ATTRIBUTE); 137 if (ascentStr.length() == 0) { 138 ascentStr = String.valueOf(unitsPerEm*.8); 140 } 141 float ascent; 142 try { 143 ascent = SVGUtilities.convertSVGNumber(ascentStr); 144 } catch (NumberFormatException ex) { 145 throw new BridgeException 146 (fontFaceElement, ERR_ATTRIBUTE_VALUE_MALFORMED, 147 new Object [] {SVG_FONT_FACE_SLOPE_DEFAULT_VALUE, ascentStr}); 148 } 149 150 String descentStr = fontFaceElement.getAttributeNS 152 (null, SVG_DESCENT_ATTRIBUTE); 153 if (descentStr.length() == 0) { 154 descentStr = String.valueOf(unitsPerEm*.2); 156 } 157 float descent; 158 try { 159 descent = SVGUtilities.convertSVGNumber(descentStr); 160 } catch (NumberFormatException ex) { 161 throw new BridgeException 162 (fontFaceElement, ERR_ATTRIBUTE_VALUE_MALFORMED, 163 new Object [] {SVG_FONT_FACE_SLOPE_DEFAULT_VALUE, descentStr}); 164 } 165 166 String 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 ex) { 176 throw new BridgeException 177 (fontFaceElement, ERR_ATTRIBUTE_VALUE_MALFORMED, 178 new Object [] {SVG_FONT_FACE_SLOPE_DEFAULT_VALUE, 179 underlinePosStr}); 180 } 181 182 183 String 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 ex) { 194 throw new BridgeException 195 (fontFaceElement, ERR_ATTRIBUTE_VALUE_MALFORMED, 196 new Object [] {SVG_FONT_FACE_SLOPE_DEFAULT_VALUE, 197 underlineThicknessStr}); 198 } 199 200 201 String 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 ex) { 212 throw new BridgeException 213 (fontFaceElement, ERR_ATTRIBUTE_VALUE_MALFORMED, 214 new Object [] {SVG_FONT_FACE_SLOPE_DEFAULT_VALUE, 215 strikethroughPosStr}); 216 } 217 218 219 String 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 ex) { 230 throw new BridgeException 231 (fontFaceElement, ERR_ATTRIBUTE_VALUE_MALFORMED, 232 new Object [] {SVG_FONT_FACE_SLOPE_DEFAULT_VALUE, 233 strikethroughThicknessStr}); 234 } 235 236 String 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 ex) { 246 throw new BridgeException 247 (fontFaceElement, ERR_ATTRIBUTE_VALUE_MALFORMED, 248 new Object [] {SVG_FONT_FACE_SLOPE_DEFAULT_VALUE, 249 overlinePosStr}); 250 } 251 252 253 String 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 ex) { 264 throw new BridgeException 265 (fontFaceElement, ERR_ATTRIBUTE_VALUE_MALFORMED, 266 new Object [] {SVG_FONT_FACE_SLOPE_DEFAULT_VALUE, 267 overlineThicknessStr}); 268 } 269 270 List srcs = null; 271 Element 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 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 getFontFaceSrcs(Element fontFaceElement) { 289 Element ffsrc = null; 291 for (Node 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 )n; 298 break; 299 } 300 } 301 if (ffsrc == null) 302 return null; 303 304 List ret = new LinkedList (); 305 306 for (Node 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 ffuri = (Element )n; 316 String uri = XLinkSupport.getXLinkHref(ffuri); 317 String 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 ffname = (Element )n; 326 String s = ffname.getAttribute("name"); 327 if (s.length() != 0) 328 ret.add(s); 329 } 330 } 331 return ret; 332 } 333 } 334 | Popular Tags |