KickJava   Java API By Example, From Geeks To Geeks.

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


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 org.apache.batik.css.engine.SVGCSSEngine;
21 import org.apache.batik.parser.ParseException;
22 import org.w3c.dom.Element JavaDoc;
23
24 /**
25  * This class provides methods to convert SVG length and coordinate to
26  * float in user units.
27  *
28  * @author <a HREF="mailto:stephane@hillion.org">Stephane Hillion</a>
29  * @author <a HREF="mailto:tkormann@apache.org">Thierry Kormann</a>
30  * @version $Id: UnitProcessor.java,v 1.14 2004/08/18 07:12:36 vhardy Exp $
31  */

32 public abstract class UnitProcessor
33     extends org.apache.batik.parser.UnitProcessor {
34
35     /**
36      * No instance of this class is required.
37      */

38     protected UnitProcessor() { }
39
40     /**
41      * Creates a context for the specified element.
42      *
43      * @param ctx the bridge context that contains the user agent and
44      * viewport definition
45      * @param e the element interested in its context
46      */

47     public static Context createContext(BridgeContext ctx, Element JavaDoc e) {
48         return new DefaultContext(ctx, e);
49     }
50
51     /////////////////////////////////////////////////////////////////////////
52
// SVG methods - objectBoundingBox
53
/////////////////////////////////////////////////////////////////////////
54

55     /**
56      * Returns the specified horizontal coordinate in object bounding box
57      * coordinate system.
58      *
59      * @param s the horizontal coordinate
60      * @param attr the attribute name that represents the coordinate
61      * @param ctx the context used to resolve relative value
62      */

63     public static
64         float svgHorizontalCoordinateToObjectBoundingBox(String JavaDoc s,
65                                                          String JavaDoc attr,
66                                                          Context ctx) {
67         return svgToObjectBoundingBox(s, attr, HORIZONTAL_LENGTH, ctx);
68     }
69
70     /**
71      * Returns the specified vertical coordinate in object bounding box
72      * coordinate system.
73      *
74      * @param s the vertical coordinate
75      * @param attr the attribute name that represents the coordinate
76      * @param ctx the context used to resolve relative value
77      */

78     public static
79         float svgVerticalCoordinateToObjectBoundingBox(String JavaDoc s,
80                                                        String JavaDoc attr,
81                                                        Context ctx) {
82         return svgToObjectBoundingBox(s, attr, VERTICAL_LENGTH, ctx);
83     }
84
85     /**
86      * Returns the specified 'other' coordinate in object bounding box
87      * coordinate system.
88      *
89      * @param s the 'other' coordinate
90      * @param attr the attribute name that represents the coordinate
91      * @param ctx the context used to resolve relative value
92      */

93     public static
94         float svgOtherCoordinateToObjectBoundingBox(String JavaDoc s,
95                                                     String JavaDoc attr,
96                                                     Context ctx) {
97         return svgToObjectBoundingBox(s, attr, OTHER_LENGTH, ctx);
98     }
99
100     /**
101      * Returns the specified horizontal length in object bounding box
102      * coordinate system. A length must be greater than 0.
103      *
104      * @param s the 'other' length
105      * @param attr the attribute name that represents the length
106      * @param ctx the context used to resolve relative value
107      */

108     public static
109         float svgHorizontalLengthToObjectBoundingBox(String JavaDoc s,
110                                                      String JavaDoc attr,
111                                                      Context ctx) {
112         return svgLengthToObjectBoundingBox(s, attr, HORIZONTAL_LENGTH, ctx);
113     }
114
115     /**
116      * Returns the specified vertical length in object bounding box
117      * coordinate system. A length must be greater than 0.
118      *
119      * @param s the vertical length
120      * @param attr the attribute name that represents the length
121      * @param ctx the context used to resolve relative value
122      */

123     public static
124         float svgVerticalLengthToObjectBoundingBox(String JavaDoc s,
125                                                    String JavaDoc attr,
126                                                    Context ctx) {
127         return svgLengthToObjectBoundingBox(s, attr, VERTICAL_LENGTH, ctx);
128     }
129
130     /**
131      * Returns the specified 'other' length in object bounding box
132      * coordinate system. A length must be greater than 0.
133      *
134      * @param s the 'other' length
135      * @param attr the attribute name that represents the length
136      * @param ctx the context used to resolve relative value
137      */

138     public static
139         float svgOtherLengthToObjectBoundingBox(String JavaDoc s,
140                                                 String JavaDoc attr,
141                                                 Context ctx) {
142         return svgLengthToObjectBoundingBox(s, attr, OTHER_LENGTH, ctx);
143     }
144
145     /**
146      * Returns the specified length with the specified direction in
147      * user units. A length must be greater than 0.
148      *
149      * @param s the length
150      * @param attr the attribute name that represents the length
151      * @param d the direction of the length
152      * @param ctx the context used to resolve relative value
153      */

154     public static float svgLengthToObjectBoundingBox(String JavaDoc s,
155                                                      String JavaDoc attr,
156                                                      short d,
157                                                      Context ctx) {
158         float v = svgToObjectBoundingBox(s, attr, d, ctx);
159         if (v < 0) {
160             throw new BridgeException(ctx.getElement(),
161                                       ErrorConstants.ERR_LENGTH_NEGATIVE,
162                                       new Object JavaDoc[] {attr, s});
163         }
164         return v;
165     }
166
167     /**
168      * Returns the specified value with the specified direction in
169      * objectBoundingBox units.
170      *
171      * @param s the value
172      * @param attr the attribute name that represents the value
173      * @param d the direction of the value
174      * @param ctx the context used to resolve relative value
175      */

176     public static float svgToObjectBoundingBox(String JavaDoc s,
177                                                String JavaDoc attr,
178                                                short d,
179                                                Context ctx) {
180         try {
181             return org.apache.batik.parser.UnitProcessor.
182                 svgToObjectBoundingBox(s, attr, d, ctx);
183         } catch (ParseException ex) {
184             throw new BridgeException(ctx.getElement(),
185                                   ErrorConstants.ERR_ATTRIBUTE_VALUE_MALFORMED,
186                                       new Object JavaDoc[] {attr, s, ex});
187         }
188     }
189
190     /////////////////////////////////////////////////////////////////////////
191
// SVG methods - userSpace
192
/////////////////////////////////////////////////////////////////////////
193

194
195     /**
196      * Returns the specified horizontal length in user units. A length
197      * must be greater than 0.
198      *
199      * @param s the horizontal length
200      * @param attr the attribute name that represents the length
201      * @param ctx the context used to resolve relative value
202      */

203     public static float svgHorizontalLengthToUserSpace(String JavaDoc s,
204                                                        String JavaDoc attr,
205                                                        Context ctx) {
206         return svgLengthToUserSpace(s, attr, HORIZONTAL_LENGTH, ctx);
207     }
208
209     /**
210      * Returns the specified vertical length in user units. A length
211      * must be greater than 0.
212      *
213      * @param s the vertical length
214      * @param attr the attribute name that represents the length
215      * @param ctx the context used to resolve relative value
216      */

217     public static float svgVerticalLengthToUserSpace(String JavaDoc s,
218                                                      String JavaDoc attr,
219                                                      Context ctx) {
220         return svgLengthToUserSpace(s, attr, VERTICAL_LENGTH, ctx);
221     }
222
223     /**
224      * Returns the specified 'other' length in user units. A length
225      * must be greater than 0.
226      *
227      * @param s the 'other' length
228      * @param attr the attribute name that represents the length
229      * @param ctx the context used to resolve relative value
230      */

231     public static float svgOtherLengthToUserSpace(String JavaDoc s,
232                                                   String JavaDoc attr,
233                                                   Context ctx) {
234         return svgLengthToUserSpace(s, attr, OTHER_LENGTH, ctx);
235     }
236
237     /**
238      * Returns the specified horizontal coordinate in user units.
239      *
240      * @param s the horizontal coordinate
241      * @param attr the attribute name that represents the length
242      * @param ctx the context used to resolve relative value
243      */

244     public static float svgHorizontalCoordinateToUserSpace(String JavaDoc s,
245                                                            String JavaDoc attr,
246                                                            Context ctx) {
247         return svgToUserSpace(s, attr, HORIZONTAL_LENGTH, ctx);
248     }
249
250     /**
251      * Returns the specified vertical coordinate in user units.
252      *
253      * @param s the vertical coordinate
254      * @param attr the attribute name that represents the length
255      * @param ctx the context used to resolve relative value
256      */

257     public static float svgVerticalCoordinateToUserSpace(String JavaDoc s,
258                                                          String JavaDoc attr,
259                                                          Context ctx) {
260         return svgToUserSpace(s, attr, VERTICAL_LENGTH, ctx);
261     }
262
263     /**
264      * Returns the specified 'other' coordinate in user units.
265      *
266      * @param s the 'other' coordinate
267      * @param attr the attribute name that represents the length
268      * @param ctx the context used to resolve relative value
269      */

270     public static float svgOtherCoordinateToUserSpace(String JavaDoc s,
271                                                       String JavaDoc attr,
272                                                       Context ctx) {
273         return svgToUserSpace(s, attr, OTHER_LENGTH, ctx);
274     }
275
276     /**
277      * Returns the specified length with the specified direction in
278      * user units. A length must be greater than 0.
279      *
280      * @param s the 'other' coordinate
281      * @param attr the attribute name that represents the length
282      * @param d the direction of the length
283      * @param ctx the context used to resolve relative value
284      */

285     public static float svgLengthToUserSpace(String JavaDoc s,
286                                              String JavaDoc attr,
287                                              short d,
288                                              Context ctx) {
289         float v = svgToUserSpace(s, attr, d, ctx);
290         if (v < 0) {
291             throw new BridgeException(ctx.getElement(),
292                                       ErrorConstants.ERR_LENGTH_NEGATIVE,
293                                       new Object JavaDoc[] {attr, s});
294         } else {
295             return v;
296         }
297     }
298
299     /**
300      * Returns the specified coordinate with the specified direction
301      * in user units.
302      *
303      * @param s the 'other' coordinate
304      * @param attr the attribute name that represents the length
305      * @param d the direction of the coordinate
306      * @param ctx the context used to resolve relative value
307      */

308     public static float svgToUserSpace(String JavaDoc s,
309                                        String JavaDoc attr,
310                                        short d,
311                                        Context ctx) {
312         try {
313             return org.apache.batik.parser.UnitProcessor.
314                 svgToUserSpace(s, attr, d, ctx);
315         } catch (ParseException ex) {
316             throw new BridgeException(ctx.getElement(),
317                                  ErrorConstants.ERR_ATTRIBUTE_VALUE_MALFORMED,
318                                       new Object JavaDoc[] {attr, s, ex});
319         }
320     }
321
322     /**
323      * This class is the default context for a particular
324      * element. Informations not available on the element are get from
325      * the bridge context (such as the viewport or the pixel to
326      * millimeter factor.
327      */

328     public static class DefaultContext implements Context {
329
330         /** The element. */
331         protected Element JavaDoc e;
332         protected BridgeContext ctx;
333
334         public DefaultContext(BridgeContext ctx, Element JavaDoc e) {
335             this.ctx = ctx;
336             this.e = e;
337         }
338
339         /**
340          * Returns the element.
341          */

342         public Element JavaDoc getElement() {
343             return e;
344         }
345
346         /**
347          * Returns the size of a px CSS unit in millimeters.
348          */

349         public float getPixelUnitToMillimeter() {
350             return ctx.getUserAgent().getPixelUnitToMillimeter();
351         }
352
353         /**
354          * Returns the size of a px CSS unit in millimeters.
355          * This will be removed after next release.
356          * @see #getPixelUnitToMillimeter()
357          */

358         public float getPixelToMM() {
359             return getPixelUnitToMillimeter();
360             
361         }
362
363         /**
364          * Returns the font-size value.
365          */

366         public float getFontSize() {
367             return CSSUtilities.getComputedStyle
368                 (e, SVGCSSEngine.FONT_SIZE_INDEX).getFloatValue();
369         }
370
371         /**
372          * Returns the x-height value.
373          */

374         public float getXHeight() {
375             return 0.5f;
376         }
377
378         /**
379          * Returns the viewport width used to compute units.
380          */

381         public float getViewportWidth() {
382             return ctx.getViewport(e).getWidth();
383         }
384
385         /**
386          * Returns the viewport height used to compute units.
387          */

388         public float getViewportHeight() {
389             return ctx.getViewport(e).getHeight();
390         }
391     }
392 }
393
Popular Tags