1 18 package org.apache.batik.bridge; 19 20 import java.awt.Shape ; 21 import java.awt.geom.AffineTransform ; 22 import java.awt.geom.GeneralPath ; 23 24 import org.apache.batik.dom.util.XLinkSupport; 25 import org.apache.batik.gvt.text.TextPath; 26 import org.apache.batik.parser.AWTPathProducer; 27 import org.apache.batik.parser.ParseException; 28 import org.apache.batik.parser.PathParser; 29 import org.w3c.dom.Element ; 30 31 37 public class SVGTextPathElementBridge extends AbstractSVGBridge 38 implements ErrorConstants { 39 40 43 public SVGTextPathElementBridge() {} 44 45 48 public String getLocalName() { 49 return SVG_TEXT_PATH_TAG; 50 } 51 52 61 public TextPath createTextPath(BridgeContext ctx, Element textPathElement) { 62 63 String uri = XLinkSupport.getXLinkHref(textPathElement); 65 Element pathElement = ctx.getReferencedElement(textPathElement, uri); 66 67 if ((pathElement == null) || 68 (!SVG_NAMESPACE_URI.equals(pathElement.getNamespaceURI())) || 69 (!pathElement.getLocalName().equals(SVG_PATH_TAG))) { 70 throw new BridgeException(textPathElement, ERR_URI_BAD_TARGET, 73 new Object [] {uri}); 74 } 75 76 String s = pathElement.getAttributeNS(null, SVG_D_ATTRIBUTE); 78 Shape pathShape = null; 79 if (s.length() != 0) { 80 AWTPathProducer app = new AWTPathProducer(); 81 app.setWindingRule(CSSUtilities.convertFillRule(pathElement)); 82 try { 83 PathParser pathParser = new PathParser(); 84 pathParser.setPathHandler(app); 85 pathParser.parse(s); 86 } catch (ParseException ex) { 87 throw new BridgeException(pathElement, ERR_ATTRIBUTE_VALUE_MALFORMED, 88 new Object [] {SVG_D_ATTRIBUTE}); 89 } finally { 90 pathShape = app.getShape(); 91 } 92 } else { 93 throw new BridgeException(pathElement, ERR_ATTRIBUTE_MISSING, 94 new Object [] {SVG_D_ATTRIBUTE}); 95 } 96 97 s = pathElement.getAttributeNS(null, SVG_TRANSFORM_ATTRIBUTE); 100 if (s.length() != 0) { 101 AffineTransform tr = SVGUtilities.convertTransform(pathElement, 102 SVG_TRANSFORM_ATTRIBUTE, s); 103 pathShape = tr.createTransformedShape(pathShape); 104 } 105 106 TextPath textPath = new TextPath(new GeneralPath (pathShape)); 108 109 s = textPathElement.getAttributeNS(null, SVG_START_OFFSET_ATTRIBUTE); 111 if (s.length() > 0) { 112 float startOffset = 0; 113 int percentIndex = s.indexOf("%"); 114 if (percentIndex != -1) { 115 float pathLength = textPath.lengthOfPath(); 117 String percentString = s.substring(0,percentIndex); 118 float startOffsetPercent = 0; 119 try { 120 startOffsetPercent = SVGUtilities.convertSVGNumber(percentString); 121 } catch (NumberFormatException e) { 122 startOffsetPercent = -1; 123 } 124 if (startOffsetPercent < 0) { 125 throw new BridgeException(textPathElement, ERR_ATTRIBUTE_VALUE_MALFORMED, 126 new Object [] {SVG_START_OFFSET_ATTRIBUTE, s}); 127 } 128 startOffset = (float)(startOffsetPercent * pathLength/100.0); 129 130 } else { 131 UnitProcessor.Context uctx = UnitProcessor.createContext(ctx, textPathElement); 133 startOffset = UnitProcessor.svgOtherLengthToUserSpace(s, SVG_START_OFFSET_ATTRIBUTE, uctx); 134 } 135 textPath.setStartOffset(startOffset); 136 } 137 138 return textPath; 139 } 140 141 142 } 143 144 | Popular Tags |