KickJava   Java API By Example, From Geeks To Geeks.

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


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.font.TextAttribute JavaDoc;
21 import java.util.ArrayList JavaDoc;
22 import java.util.StringTokenizer JavaDoc;
23
24 import org.apache.batik.css.engine.SVGCSSEngine;
25 import org.apache.batik.css.engine.value.Value;
26 import org.apache.batik.gvt.TextNode;
27 import org.apache.batik.util.CSSConstants;
28 import org.w3c.dom.Element JavaDoc;
29 import org.w3c.dom.Node JavaDoc;
30 import org.w3c.dom.css.CSSPrimitiveValue;
31
32 /**
33  * A collection of utility method for text.
34  *
35  * @author <a HREF="mailto:stephane@hillion.org">Stephane Hillion</a>
36  * @author <a HREF="mailto:bill.haneman@ireland.sun.com">Bill Haneman</a>
37  * @version $Id: TextUtilities.java,v 1.10 2005/03/27 08:58:30 cam Exp $
38  */

39 public abstract class TextUtilities implements CSSConstants, ErrorConstants {
40
41     /**
42      * Returns the content of the given element.
43      */

44     public static String JavaDoc getElementContent(Element JavaDoc e) {
45         StringBuffer JavaDoc result = new StringBuffer JavaDoc();
46         for (Node n = e.getFirstChild();
47              n != null;
48              n = n.getNextSibling()) {
49             switch (n.getNodeType()) {
50             case Node.ELEMENT_NODE:
51                 result.append(getElementContent((Element JavaDoc)n));
52                 break;
53             case Node.CDATA_SECTION_NODE:
54             case Node.TEXT_NODE:
55                 result.append(n.getNodeValue());
56             }
57         }
58         return result.toString();
59     }
60
61     /**
62      * Returns the float list that represents a set of horizontal
63      * values or percentage.
64      *
65      * @param element the element that defines the specified coordinates
66      * @param attrName the name of the attribute (used by error handling)
67      * @param valueStr the delimited string containing values of the coordinate
68      * @param ctx the bridge context
69      */

70     public static
71         ArrayList JavaDoc svgHorizontalCoordinateArrayToUserSpace(Element JavaDoc element,
72                                                           String JavaDoc attrName,
73                                                           String JavaDoc valueStr,
74                                                           BridgeContext ctx) {
75
76         UnitProcessor.Context uctx = UnitProcessor.createContext(ctx, element);
77         ArrayList JavaDoc values = new ArrayList JavaDoc();
78         StringTokenizer JavaDoc st = new StringTokenizer JavaDoc(valueStr, ", ", false);
79         while (st.hasMoreTokens()) {
80             values.add
81                 (new Float JavaDoc(UnitProcessor.svgHorizontalCoordinateToUserSpace
82                            (st.nextToken(), attrName, uctx)));
83         }
84         return values;
85     }
86
87     /**
88      * Returns the float list that represents a set of values or percentage.
89      *
90      *
91      * @param element the element that defines the specified coordinates
92      * @param attrName the name of the attribute (used by error handling)
93      * @param valueStr the delimited string containing values of the coordinate
94      * @param ctx the bridge context
95      */

96     public static
97         ArrayList JavaDoc svgVerticalCoordinateArrayToUserSpace(Element JavaDoc element,
98                                                         String JavaDoc attrName,
99                                                         String JavaDoc valueStr,
100                                                         BridgeContext ctx) {
101
102         UnitProcessor.Context uctx = UnitProcessor.createContext(ctx, element);
103         ArrayList JavaDoc values = new ArrayList JavaDoc();
104         StringTokenizer JavaDoc st = new StringTokenizer JavaDoc(valueStr, ", ", false);
105         while (st.hasMoreTokens()) {
106             values.add
107                 (new Float JavaDoc(UnitProcessor.svgVerticalCoordinateToUserSpace
108                            (st.nextToken(), attrName, uctx)));
109         }
110         return values;
111     }
112
113
114     public static ArrayList JavaDoc svgRotateArrayToFloats(Element JavaDoc element,
115                                                    String JavaDoc attrName,
116                                                    String JavaDoc valueStr,
117                                                    BridgeContext ctx) {
118
119         StringTokenizer JavaDoc st = new StringTokenizer JavaDoc(valueStr, ", ", false);
120         ArrayList JavaDoc values = new ArrayList JavaDoc();
121         String JavaDoc s;
122         while (st.hasMoreTokens()) {
123             try {
124                 s = st.nextToken();
125                 values.add
126                     (new Float JavaDoc(Math.toRadians
127                                (SVGUtilities.convertSVGNumber(s))));
128             } catch (NumberFormatException JavaDoc ex) {
129                 throw new BridgeException
130                     (element, ERR_ATTRIBUTE_VALUE_MALFORMED,
131                      new Object JavaDoc [] {attrName, valueStr});
132             }
133         }
134         return values;
135     }
136
137     /**
138      * Converts the font-size CSS value to a float value.
139      * @param e the element
140      */

141     public static Float JavaDoc convertFontSize(Element JavaDoc e) {
142         Value v = CSSUtilities.getComputedStyle
143             (e, SVGCSSEngine.FONT_SIZE_INDEX);
144         return new Float JavaDoc(v.getFloatValue());
145     }
146
147     /**
148      * Converts the font-style CSS value to a float value.
149      * @param e the element
150      */

151     public static Float JavaDoc convertFontStyle(Element JavaDoc e) {
152         Value v = CSSUtilities.getComputedStyle
153             (e, SVGCSSEngine.FONT_STYLE_INDEX);
154         switch (v.getStringValue().charAt(0)) {
155         case 'n':
156             return TextAttribute.POSTURE_REGULAR;
157         default:
158             return TextAttribute.POSTURE_OBLIQUE;
159         }
160     }
161
162     /**
163      * Converts the font-stretch CSS value to a float value.
164      * @param e the element
165      */

166     public static Float JavaDoc convertFontStretch(Element JavaDoc e) {
167         Value v = CSSUtilities.getComputedStyle
168             (e, SVGCSSEngine.FONT_STRETCH_INDEX);
169         String JavaDoc s = v.getStringValue();
170         switch (s.charAt(0)) {
171         case 'u':
172             if (s.charAt(6) == 'c') {
173                 return TextAttribute.WIDTH_CONDENSED;
174             } else {
175                 return TextAttribute.WIDTH_EXTENDED;
176             }
177
178         case 'e':
179             if (s.charAt(6) == 'c') {
180                 return TextAttribute.WIDTH_CONDENSED;
181             } else {
182                 if (s.length() == 8) {
183                     return TextAttribute.WIDTH_SEMI_EXTENDED;
184                 } else {
185                     return TextAttribute.WIDTH_EXTENDED;
186                 }
187             }
188
189         case 's':
190             if (s.charAt(6) == 'c') {
191                 return TextAttribute.WIDTH_SEMI_CONDENSED;
192             } else {
193                 return TextAttribute.WIDTH_SEMI_EXTENDED;
194             }
195
196         default:
197             return TextAttribute.WIDTH_REGULAR;
198         }
199     }
200
201     /**
202      * Converts the font-weight CSS value to a float value.
203      * @param e the element
204      */

205     public static Float JavaDoc convertFontWeight(Element JavaDoc e) {
206         Value v = CSSUtilities.getComputedStyle
207             (e, SVGCSSEngine.FONT_WEIGHT_INDEX);
208         float f = v.getFloatValue();
209         switch ((int)f) {
210         case 100:
211             return TextAttribute.WEIGHT_EXTRA_LIGHT;
212         case 200:
213             return TextAttribute.WEIGHT_LIGHT;
214         case 300:
215             return TextAttribute.WEIGHT_DEMILIGHT;
216         case 400:
217             return TextAttribute.WEIGHT_REGULAR;
218         case 500:
219             return TextAttribute.WEIGHT_SEMIBOLD;
220         default:
221             return TextAttribute.WEIGHT_BOLD;
222             /* Would like to do this but the JDK 1.3 & 1.4
223                seems to drop back to 'REGULAR' instead of 'BOLD'
224                if there is not a match.
225         case 700:
226             return TextAttribute.WEIGHT_HEAVY;
227         case 800:
228             return TextAttribute.WEIGHT_EXTRABOLD;
229         case 900:
230             return TextAttribute.WEIGHT_ULTRABOLD;
231             */

232         }
233     }
234
235     /**
236      * Converts the text-anchor CSS value to a TextNode.Anchor.
237      * @param e the element
238      */

239     public static TextNode.Anchor convertTextAnchor(Element JavaDoc e) {
240         Value v = CSSUtilities.getComputedStyle
241             (e, SVGCSSEngine.TEXT_ANCHOR_INDEX);
242         switch (v.getStringValue().charAt(0)) {
243         case 's':
244             return TextNode.Anchor.START;
245         case 'm':
246             return TextNode.Anchor.MIDDLE;
247         default:
248             return TextNode.Anchor.END;
249         }
250     }
251
252     /**
253      * Converts a baseline-shift CSS value to a value usable as a text
254      * attribute, or null.
255      * @param e the element
256      */

257     public static Object JavaDoc convertBaselineShift(Element JavaDoc e) {
258         Value v = CSSUtilities.getComputedStyle
259             (e, SVGCSSEngine.BASELINE_SHIFT_INDEX);
260         if (v.getPrimitiveType() == CSSPrimitiveValue.CSS_IDENT) {
261             String JavaDoc s = v.getStringValue();
262             switch (s.charAt(2)) {
263             case 'p': //suPerscript
264
return TextAttribute.SUPERSCRIPT_SUPER;
265
266             case 'b': //suBscript
267
return TextAttribute.SUPERSCRIPT_SUB;
268
269             default:
270                 return null;
271             }
272         } else {
273             return new Float JavaDoc(v.getFloatValue());
274         }
275     }
276
277     /**
278      * Converts a kerning CSS value to a value usable as a text
279      * attribute, or null.
280      * @param e the element
281      */

282     public static Float JavaDoc convertKerning(Element JavaDoc e) {
283         Value v = CSSUtilities.getComputedStyle
284             (e, SVGCSSEngine.KERNING_INDEX);
285         if (v.getPrimitiveType() == CSSPrimitiveValue.CSS_IDENT) {
286             return null;
287         }
288         return new Float JavaDoc(v.getFloatValue());
289     }
290
291     /**
292      * Converts a letter-spacing CSS value to a value usable as a text
293      * attribute, or null.
294      * @param e the element
295      */

296     public static Float JavaDoc convertLetterSpacing(Element JavaDoc e) {
297         Value v = CSSUtilities.getComputedStyle
298             (e, SVGCSSEngine.LETTER_SPACING_INDEX);
299         if (v.getPrimitiveType() == CSSPrimitiveValue.CSS_IDENT) {
300             return null;
301         }
302         return new Float JavaDoc(v.getFloatValue());
303     }
304
305     /**
306      * Converts a word-spacing CSS value to a value usable as a text
307      * attribute, or null.
308      * @param e the element
309      */

310     public static Float JavaDoc convertWordSpacing(Element JavaDoc e) {
311         Value v = CSSUtilities.getComputedStyle
312             (e, SVGCSSEngine.WORD_SPACING_INDEX);
313         if (v.getPrimitiveType() == CSSPrimitiveValue.CSS_IDENT) {
314             return null;
315         }
316         return new Float JavaDoc(v.getFloatValue());
317     }
318 }
319
Popular Tags