KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > fop > util > UnitConv


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: FixedLength.java 279656 2005-09-08 22:06:48Z pietsch $ */
19
20 package org.apache.fop.util;
21
22 /**
23  * Utility class for unit conversions.
24  */

25 public final class UnitConv {
26
27     /** conversion factory from millimeters to inches. */
28     public static final float IN2MM = 25.4f;
29     
30     /** conversion factory from centimeters to inches. */
31     public static final float IN2CM = 2.54f;
32     
33     /** conversion factory from inches to points. */
34     public static final int IN2PT = 72;
35     
36     /**
37      * Converts millimeters (mm) to points (pt)
38      * @param mm the value in mm
39      * @return the value in pt
40      */

41     public static double mm2pt(double mm) {
42         return mm * IN2PT / IN2MM;
43     }
44
45     /**
46      * Converts millimeters (mm) to millipoints (mpt)
47      * @param mm the value in mm
48      * @return the value in mpt
49      */

50     public static double mm2mpt(double mm) {
51         return mm * 1000 * IN2PT / IN2MM;
52     }
53
54     /**
55      * Converts points (pt) to millimeters (mm)
56      * @param pt the value in pt
57      * @return the value in mm
58      */

59     public static double pt2mm(double pt) {
60         return pt * IN2MM / IN2PT;
61     }
62     
63     /**
64      * Converts millimeters (mm) to inches (in)
65      * @param mm the value in mm
66      * @return the value in inches
67      */

68     public static double mm2in(double mm) {
69         return mm / IN2MM;
70     }
71     
72     /**
73      * Converts inches (in) to millimeters (mm)
74      * @param in the value in inches
75      * @return the value in mm
76      */

77     public static double in2mm(double in) {
78         return in * IN2MM;
79     }
80     
81     /**
82      * Converts inches (in) to millipoints (mpt)
83      * @param in the value in inches
84      * @return the value in mpt
85      */

86     public static double in2mpt(double in) {
87         return in * IN2PT * 1000;
88     }
89     
90     /**
91      * Converts millipoints (mpt) to inches (in)
92      * @param mpt the value in mpt
93      * @return the value in inches
94      */

95     public static double mpt2in(double mpt) {
96         return mpt / IN2PT / 1000;
97     }
98     
99     /**
100      * Converts millimeters (mm) to pixels (px)
101      * @param mm the value in mm
102      * @param resolution the resolution in dpi (dots per inch)
103      * @return the value in pixels
104      */

105     public static double mm2px(double mm, int resolution) {
106         return mm2in(mm) * resolution;
107     }
108
109     /**
110      * Converts millipoints (mpt) to pixels (px)
111      * @param mpt the value in mpt
112      * @param resolution the resolution in dpi (dots per inch)
113      * @return the value in pixels
114      */

115     public static double mpt2px(double mpt, int resolution) {
116         return mpt2in(mpt) * resolution;
117     }
118
119 }
120
Popular Tags