KickJava   Java API By Example, From Geeks To Geeks.

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


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.awt.Shape JavaDoc;
21 import java.awt.geom.AffineTransform JavaDoc;
22 import java.awt.geom.GeneralPath JavaDoc;
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 JavaDoc;
30
31 /**
32  * Bridge class for the <textPath> element.
33  *
34  * @author <a HREF="mailto:bella.robinson@cmis.csiro.au">Bella Robinson</a>
35  * @version $Id: SVGTextPathElementBridge.java,v 1.8 2005/03/03 01:19:53 deweese Exp $
36  */

37 public class SVGTextPathElementBridge extends AbstractSVGBridge
38                                       implements ErrorConstants {
39
40     /**
41      * Constructs a new bridge for the &lt;textPath> element.
42      */

43     public SVGTextPathElementBridge() {}
44
45     /**
46      * Returns 'textPath'.
47      */

48     public String JavaDoc getLocalName() {
49         return SVG_TEXT_PATH_TAG;
50     }
51
52     /**
53      * Creates a TextPath object that represents the path along which the text
54      * is to be rendered.
55      *
56      * @param ctx The bridge context.
57      * @param textPathElement The &lt;textPath> element.
58      *
59      * @return The new TextPath.
60      */

61     public TextPath createTextPath(BridgeContext ctx, Element JavaDoc textPathElement) {
62
63         // get the referenced element
64
String JavaDoc uri = XLinkSupport.getXLinkHref(textPathElement);
65         Element JavaDoc 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             // couldn't find the referenced element
71
// or the referenced element was not a path
72
throw new BridgeException(textPathElement, ERR_URI_BAD_TARGET,
73                                           new Object JavaDoc[] {uri});
74         }
75
76         // construct a shape for the referenced path element
77
String JavaDoc s = pathElement.getAttributeNS(null, SVG_D_ATTRIBUTE);
78         Shape JavaDoc 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 JavaDoc[] {SVG_D_ATTRIBUTE});
89             } finally {
90                 pathShape = app.getShape();
91             }
92         } else {
93             throw new BridgeException(pathElement, ERR_ATTRIBUTE_MISSING,
94                                       new Object JavaDoc[] {SVG_D_ATTRIBUTE});
95         }
96
97         // if the reference path element has a transform apply the transform
98
// to the path shape
99
s = pathElement.getAttributeNS(null, SVG_TRANSFORM_ATTRIBUTE);
100         if (s.length() != 0) {
101             AffineTransform JavaDoc tr = SVGUtilities.convertTransform(pathElement,
102                                                   SVG_TRANSFORM_ATTRIBUTE, s);
103             pathShape = tr.createTransformedShape(pathShape);
104         }
105
106         // create the TextPath object that we are going to return
107
TextPath textPath = new TextPath(new GeneralPath JavaDoc(pathShape));
108
109         // set the start offset if specified
110
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                 // its a percentage of the length of the path
116
float pathLength = textPath.lengthOfPath();
117                 String JavaDoc percentString = s.substring(0,percentIndex);
118                 float startOffsetPercent = 0;
119                 try {
120                     startOffsetPercent = SVGUtilities.convertSVGNumber(percentString);
121                 } catch (NumberFormatException JavaDoc e) {
122                     startOffsetPercent = -1;
123                 }
124                 if (startOffsetPercent < 0) {
125                     throw new BridgeException(textPathElement, ERR_ATTRIBUTE_VALUE_MALFORMED,
126                                               new Object JavaDoc[] {SVG_START_OFFSET_ATTRIBUTE, s});
127                 }
128                 startOffset = (float)(startOffsetPercent * pathLength/100.0);
129
130             } else {
131                 // its an absolute length
132
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