KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > batik > dom > svg > SVGTextContentSupport


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.dom.svg;
19
20 import java.awt.geom.Point2D JavaDoc;
21 import java.awt.geom.Rectangle2D JavaDoc;
22
23 import org.w3c.dom.DOMException JavaDoc;
24 import org.w3c.dom.Element JavaDoc;
25 import org.w3c.dom.svg.SVGMatrix;
26 import org.w3c.dom.svg.SVGPoint;
27 import org.w3c.dom.svg.SVGRect;
28
29 /**
30  * This class provides support for the SVGTextContentElement interface.
31  *
32  * @author nicolas.socheleau@bitflash.com
33  * @version $Id: SVGTextContentSupport.java,v 1.6 2004/08/18 07:13:19 vhardy Exp $
34  */

35 public class SVGTextContentSupport
36 {
37
38     /**
39      * To implement {@link
40      * org.w3c.dom.svg.SVGTextContentElement#getNumberOfChars()}.
41      */

42     public static int getNumberOfChars(Element JavaDoc elt)
43     {
44         final SVGOMElement svgelt = (SVGOMElement)elt;
45
46         return (((SVGTextContent)svgelt.getSVGContext()).getNumberOfChars());
47     }
48
49
50     /**
51      * To implement {@link
52      * org.w3c.dom.svg.SVGTextContentElement#getExtentOfChar(int charnum)}.
53      */

54     public static SVGRect getExtentOfChar(Element JavaDoc elt, final int charnum ) {
55         final SVGOMElement svgelt = (SVGOMElement)elt;
56
57         if ( (charnum < 0) ||
58              (charnum >= getNumberOfChars(elt)) ){
59             throw svgelt.createDOMException
60                 (DOMException.INDEX_SIZE_ERR,
61                  "",null);
62         }
63         
64         final SVGTextContent context = (SVGTextContent)svgelt.getSVGContext();
65         Rectangle2D JavaDoc r2d = getExtent(svgelt, context, charnum);
66             
67         return new SVGRect() {
68                 public float getX() {
69                     return (float)SVGTextContentSupport.getExtent
70                         (svgelt, context, charnum).getX();
71                 }
72                 public void setX(float x) throws DOMException JavaDoc {
73                     throw svgelt.createDOMException
74                         (DOMException.NO_MODIFICATION_ALLOWED_ERR,
75                          "readonly.rect", null);
76                 }
77
78                 public float getY() {
79                     return (float)SVGTextContentSupport.getExtent
80                         (svgelt, context, charnum).getY();
81                 }
82                 public void setY(float y) throws DOMException JavaDoc {
83                     throw svgelt.createDOMException
84                         (DOMException.NO_MODIFICATION_ALLOWED_ERR,
85                          "readonly.rect", null);
86                 }
87
88                 public float getWidth() {
89                     return (float)SVGTextContentSupport.getExtent
90                         (svgelt, context, charnum).getWidth();
91                 }
92                 public void setWidth(float width) throws DOMException JavaDoc {
93                     throw svgelt.createDOMException
94                         (DOMException.NO_MODIFICATION_ALLOWED_ERR,
95                          "readonly.rect", null);
96                 }
97
98                 public float getHeight() {
99                     return (float)SVGTextContentSupport.getExtent
100                         (svgelt, context, charnum).getHeight();
101                 }
102                 public void setHeight(float height) throws DOMException JavaDoc {
103                     throw svgelt.createDOMException
104                         (DOMException.NO_MODIFICATION_ALLOWED_ERR,
105                          "readonly.rect", null);
106                 }
107             };
108     }
109
110     protected static Rectangle2D JavaDoc getExtent
111         (SVGOMElement svgelt, SVGTextContent context, int charnum) {
112         Rectangle2D JavaDoc r2d = context.getExtentOfChar(charnum);
113         if (r2d == null) throw svgelt.createDOMException
114                              (DOMException.INDEX_SIZE_ERR, "",null);
115         return r2d;
116     }
117     
118     /**
119      * To implement {@link
120      * org.w3c.dom.svg.SVGTextContentElement#getStartPositionOfChar(int charnum)}.
121      */

122     public static SVGPoint getStartPositionOfChar
123         (Element JavaDoc elt, final int charnum) throws DOMException JavaDoc {
124
125         final SVGOMElement svgelt = (SVGOMElement)elt;
126
127         if ( (charnum < 0) ||
128              (charnum >= getNumberOfChars(elt)) ){
129             throw svgelt.createDOMException
130                 (DOMException.INDEX_SIZE_ERR,
131                  "",null);
132         }
133         
134         final SVGTextContent context = (SVGTextContent)svgelt.getSVGContext();
135         Point2D JavaDoc p2d = getStartPos(svgelt, context, charnum);
136
137         return new SVGTextPoint(svgelt){
138                 public float getX(){
139                     return (float)SVGTextContentSupport.getStartPos
140                         (this.svgelt, context, charnum).getX();
141                 }
142                 public float getY(){
143                     return (float)SVGTextContentSupport.getStartPos
144                         (this.svgelt, context, charnum).getY();
145                 }
146             };
147     }
148
149     protected static Point2D JavaDoc getStartPos
150         (SVGOMElement svgelt, SVGTextContent context, int charnum) {
151         Point2D JavaDoc p2d = context.getStartPositionOfChar(charnum);
152         if (p2d == null) throw svgelt.createDOMException
153                              (DOMException.INDEX_SIZE_ERR, "",null);
154         return p2d;
155     }
156     
157     /**
158      * To implement {@link
159      * org.w3c.dom.svg.SVGTextContentElement#getEndPositionOfChar(int charnum)}.
160      */

161     public static SVGPoint getEndPositionOfChar
162         (Element JavaDoc elt,final int charnum) throws DOMException JavaDoc {
163
164         final SVGOMElement svgelt = (SVGOMElement)elt;
165
166         if ( (charnum < 0) ||
167              (charnum >= getNumberOfChars(elt)) ){
168             throw svgelt.createDOMException
169                 (DOMException.INDEX_SIZE_ERR,
170                  "",null);
171         }
172         
173         final SVGTextContent context = (SVGTextContent)svgelt.getSVGContext();
174         Point2D JavaDoc p2d = getEndPos(svgelt, context, charnum);
175
176         return new SVGTextPoint(svgelt){
177                 public float getX(){
178                     return (float)SVGTextContentSupport.getEndPos
179                         (this.svgelt, context, charnum).getX();
180                 }
181                 public float getY(){
182                     return (float)SVGTextContentSupport.getEndPos
183                         (this.svgelt, context, charnum).getY();
184                 }
185             };
186     }
187
188     protected static Point2D JavaDoc getEndPos
189         (SVGOMElement svgelt, SVGTextContent context, int charnum) {
190         Point2D JavaDoc p2d = context.getEndPositionOfChar(charnum);
191         if (p2d == null) throw svgelt.createDOMException
192                              (DOMException.INDEX_SIZE_ERR, "",null);
193         return p2d;
194     }
195
196     /**
197      * To implement {@link
198      * org.w3c.dom.svg.SVGTextContentElement#selectSubString(int charnum, int nchars)}.
199      */

200     public static void selectSubString(Element JavaDoc elt, int charnum, int nchars){
201
202         final SVGOMElement svgelt = (SVGOMElement)elt;
203
204         if ( (charnum < 0) ||
205              (charnum >= getNumberOfChars(elt)) ){
206             throw svgelt.createDOMException
207                 (DOMException.INDEX_SIZE_ERR,
208                  "",null);
209         }
210         
211         final SVGTextContent context = (SVGTextContent)svgelt.getSVGContext();
212
213         context.selectSubString(charnum, nchars);
214     }
215
216     /**
217      * To implement {@link
218      * org.w3c.dom.svg.SVGTextContentElement#getRotationOfChar(int charnum)}.
219      */

220     public static float getRotationOfChar(Element JavaDoc elt, final int charnum ) {
221         final SVGOMElement svgelt = (SVGOMElement)elt;
222
223         if ( (charnum < 0) ||
224              (charnum >= getNumberOfChars(elt)) ){
225             throw svgelt.createDOMException
226                 (DOMException.INDEX_SIZE_ERR,
227                  "",null);
228         }
229         
230         final SVGTextContent context = (SVGTextContent)svgelt.getSVGContext();
231         
232         return context.getRotationOfChar(charnum);
233     }
234
235     /**
236      * To implement {@link
237      * org.w3c.dom.svg.SVGTextContentElement#selectSubString(int charnum, int nchars)}.
238      */

239     public static float getComputedTextLength(Element JavaDoc elt){
240
241         final SVGOMElement svgelt = (SVGOMElement)elt;
242
243         final SVGTextContent context = (SVGTextContent)svgelt.getSVGContext();
244
245         return context.getComputedTextLength();
246     }
247
248     /**
249      * To implement {@link
250      * org.w3c.dom.svg.SVGTextContentElement#selectSubString(int charnum, int nchars)}.
251      */

252     public static float getSubStringLength(Element JavaDoc elt, int charnum, int nchars){
253
254         final SVGOMElement svgelt = (SVGOMElement)elt;
255
256         if ( (charnum < 0) ||
257              (charnum >= getNumberOfChars(elt)) ){
258             throw svgelt.createDOMException
259                 (DOMException.INDEX_SIZE_ERR,
260                  "",null);
261         }
262         
263         final SVGTextContent context = (SVGTextContent)svgelt.getSVGContext();
264
265         return context.getSubStringLength(charnum,nchars);
266     }
267
268     /**
269      * To implement {@link
270      * org.w3c.dom.svg.SVGTextContentElement#getCharNumAtPosition(SVGPoint point)}.
271      */

272     public static int getCharNumAtPosition(Element JavaDoc elt, final float x, final float y) throws DOMException JavaDoc {
273
274         final SVGOMElement svgelt = (SVGOMElement)elt;
275
276         final SVGTextContent context = (SVGTextContent)svgelt.getSVGContext();
277         
278         return context.getCharNumAtPosition(x,y);
279     }
280
281     public static class SVGTextPoint extends SVGOMPoint {
282         SVGOMElement svgelt;
283         SVGTextPoint(SVGOMElement elem) {
284             svgelt = elem;
285         }
286         public void setX(float x) throws DOMException JavaDoc {
287             throw svgelt.createDOMException
288                 (DOMException.NO_MODIFICATION_ALLOWED_ERR,
289                  "readonly.point", null);
290         }
291         public void setY(float y) throws DOMException JavaDoc {
292             throw svgelt.createDOMException
293                 (DOMException.NO_MODIFICATION_ALLOWED_ERR,
294                  "readonly.point", null);
295         }
296     }
297
298 }
299
Popular Tags