KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > fop > render > rtf > FoUnitsConverter


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. 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 /* $Id: FoUnitsConverter.java 426576 2006-07-28 15:44:37Z jeremias $ */
19
20 package org.apache.fop.render.rtf;
21
22 import java.util.Map JavaDoc;
23 import java.util.HashMap JavaDoc;
24
25 //FOP
26
import org.apache.fop.apps.FOPException;
27
28
29 /** Converts XSL-FO units to RTF units
30  *
31  * @author Bertrand Delacretaz <bdelacretaz@codeconsult.ch>
32  * @author putzi
33  * @author Peter Herweg <pherweg@web.de>
34  *
35  * This class was originally developed by Bertrand Delacretaz bdelacretaz@codeconsult.ch
36  * for the JFOR project and is now integrated into FOP.
37  */

38
39 final class FoUnitsConverter {
40     private static final FoUnitsConverter INSTANCE = new FoUnitsConverter();
41
42     /** points to twips: 1 twip is 1/20 of a point */
43     public static final float POINT_TO_TWIPS = 20f;
44
45     /** millimeters and centimeters to twips: , one point is 1/72 of an inch, one inch is 25.4 mm */
46     public static final float IN_TO_TWIPS = 72f * POINT_TO_TWIPS;
47     public static final float MM_TO_TWIPS = IN_TO_TWIPS / 25.4f;
48     public static final float CM_TO_TWIPS = 10 * MM_TO_TWIPS;
49
50
51     /** conversion factors keyed by xsl:fo units names */
52     private static final Map JavaDoc TWIP_FACTORS = new HashMap JavaDoc();
53     static {
54         TWIP_FACTORS.put("mm", new Float JavaDoc(MM_TO_TWIPS));
55         TWIP_FACTORS.put("cm", new Float JavaDoc(CM_TO_TWIPS));
56         TWIP_FACTORS.put("pt", new Float JavaDoc(POINT_TO_TWIPS));
57         TWIP_FACTORS.put("in", new Float JavaDoc(IN_TO_TWIPS));
58     }
59
60     /** singleton pattern */
61     private FoUnitsConverter() {
62     }
63
64     /** singleton pattern */
65     static FoUnitsConverter getInstance() {
66         return INSTANCE;
67     }
68
69     /** convert given value to RTF units
70      * @param foValue a value like "12mm"
71      * TODO: tested with "mm" units only, needs work to comply with FO spec
72      * Why does it search for period instead of simply breaking last two
73      * Characters into another units string? - Chris
74      */

75     float convertToTwips(String JavaDoc foValue)
76             throws FOPException {
77         foValue = foValue.trim();
78
79         // break value into number and units
80
final StringBuffer JavaDoc number = new StringBuffer JavaDoc();
81         final StringBuffer JavaDoc units = new StringBuffer JavaDoc();
82
83         for (int i = 0; i < foValue.length(); i++) {
84             final char c = foValue.charAt(i);
85             if (Character.isDigit(c) || c == '.') {
86                 number.append(c);
87             } else {
88                 // found the end of the digits
89
units.append(foValue.substring(i).trim());
90                 break;
91             }
92         }
93
94         return numberToTwips(number.toString(), units.toString());
95     }
96
97
98     /** convert given value to twips according to given units */
99     private float numberToTwips(String JavaDoc number, String JavaDoc units)
100             throws FOPException {
101         float result = 0;
102
103         // convert number to integer
104
try {
105             if (number != null && number.trim().length() > 0) {
106                 result = Float.valueOf(number).floatValue();
107             }
108         } catch (Exception JavaDoc e) {
109             throw new FOPException("number format error: cannot convert '"
110                                    + number + "' to float value");
111         }
112
113         // find conversion factor
114
if (units != null && units.trim().length() > 0) {
115             final Float JavaDoc factor = (Float JavaDoc)TWIP_FACTORS.get(units.toLowerCase());
116             if (factor == null) {
117                 throw new FOPException("conversion factor not found for '" + units + "' units");
118             }
119             result *= factor.floatValue();
120         }
121
122         return result;
123     }
124
125     /** convert a font size given in points like "12pt" */
126     int convertFontSize(String JavaDoc size) throws FOPException {
127         size = size.trim();
128         final String JavaDoc sFONTSUFFIX = "pt";
129         if (!size.endsWith(sFONTSUFFIX)) {
130             throw new FOPException("Invalid font size '" + size + "', must end with '"
131                                    + sFONTSUFFIX + "'");
132         }
133
134         float result = 0;
135         size = size.substring(0, size.length() - sFONTSUFFIX.length());
136         try {
137             result = (Float.valueOf(size).floatValue());
138         } catch (Exception JavaDoc e) {
139             throw new FOPException("Invalid font size value '" + size + "'");
140         }
141
142         // RTF font size units are in half-points
143
return (int)(result * 2.0);
144     }
145 }
146
Popular Tags