1 // -*- Mode: C++; c-basic-offset: 2; indent-tabs-mode: nil; c-brace-offset: 0; -*-
7 // (C) 2005 Stefan Kebekus
8 // (C) 2006 Wilfried Huss
10 // Distributed under the GPL.
20 #define mm_per_cm 10.0
21 #define mm_per_m 1000.0
22 #define mm_per_inch 25.4
23 #define mm_per_TeXPoint (2540.0/7227.0)
24 #define mm_per_bigPoint (25.4/72.0)
25 #define mm_per_pica (25.4/6.0)
26 #define mm_per_didot (25.4*0.0148)
27 #define mm_per_cicero (25.4*0.178)
28 #define mm_per_scaledPoint (25.4/(72.27 * 65536.0))
31 /** @short Represents a phyical length
33 This class is used to represent a physical length. Its main purpose
34 it to help in the conversion of units, and to avoid confusion
35 about units. To avoid misunderstandings, there is no default
36 constructor so that this class needs to be explicitly initialized
37 with one of the functions below.
39 @warning Lengths are stored internally in mm. If you convert to
40 or from any other unit, expect floating point round-off errors.
42 @author Stefan Kebekus <kebekus@kde.org>
49 /** constructs a 'length = 0mm' object */
50 Length() {length_in_mm
= 0;}
52 /** sets the length in millimeters */
53 void setLength_in_mm(double l
) {length_in_mm
= l
;}
55 /** sets the length in centimeters */
56 void setLength_in_cm(double l
) {length_in_mm
= l
*mm_per_cm
;}
58 /** sets the length in meters */
59 void setLength_in_m(double l
) {length_in_mm
= l
*mm_per_m
;}
61 /** sets the length in inches */
62 void setLength_in_inch(double l
) {length_in_mm
= l
*mm_per_inch
;}
64 /** sets the length in TeX points */
65 void setLength_in_TeXPoints(double l
) {length_in_mm
= l
*mm_per_TeXPoint
;}
67 /** sets the length in big points (1/72 of an inch) */
68 void setLength_in_bigPoints(double l
) {length_in_mm
= l
*mm_per_bigPoint
;}
70 /** sets the length in picas (1/6 of an inch) */
71 void setLength_in_pica(double l
) {length_in_mm
= l
*mm_per_pica
;}
73 /** sets the length in didots (0.0148 inches) */
74 void setLength_in_didot(double l
) {length_in_mm
= l
*mm_per_didot
;}
76 /** sets the length in ciceros (0.178 inches) */
77 void setLength_in_cicero(double l
) {length_in_mm
= l
*mm_per_cicero
;}
79 /** sets the length in scaled points (1 scaled point = 65536 TeX points) */
80 void setLength_in_scaledPoints(double l
) {length_in_mm
= l
*mm_per_scaledPoint
;}
82 /** sets the length in pixel. The parameter @param res is the resolution of the
83 used device in DPI. */
84 void setLength_in_pixel(int l
, double res
) { setLength_in_inch(l
/ res
); }
86 /** @returns the length in millimeters */
87 double getLength_in_mm() const {return length_in_mm
;}
89 /** @returns the length in centimeters */
90 double getLength_in_cm() const {return length_in_mm
/mm_per_cm
;}
92 /** @returns the length in meters */
93 double getLength_in_m() const {return length_in_mm
/mm_per_m
;}
95 /** @returns the length in inches */
96 double getLength_in_inch() const {return length_in_mm
/mm_per_inch
;}
98 /** @returns the length in TeX points */
99 double getLength_in_TeXPoints() const {return length_in_mm
/mm_per_TeXPoint
;}
101 /** @returns the length in big points (1/72 of an inch) */
102 double getLength_in_bigPoints() const {return length_in_mm
/mm_per_bigPoint
;}
104 /** @returns the length in picas (1/6 of an inch) */
105 double getLength_in_pica() const {return length_in_mm
/mm_per_pica
;}
107 /** @returns the length in didots (0.0148 inches) */
108 double getLength_in_didot() const {return length_in_mm
/mm_per_didot
;}
110 /** @returns the length in ciceros (0.178 inches) */
111 double getLength_in_cicero() const {return length_in_mm
/mm_per_cicero
;}
113 /** @returns the length in scaled points (1 scaled point = 65536 TeX points) */
114 double getLength_in_scaledPoints() const {return length_in_mm
/mm_per_scaledPoint
;}
116 /** @returns the length in pixel. The parameter @param res is the resolution of the
117 used device in DPI. */
118 int getLength_in_pixel(double res
) const { return int(getLength_in_inch() * res
); }
120 /** @returns true is lengths differ by no more than 2mm */
121 bool isNearlyEqual(const Length
&o
) const {return fabs(length_in_mm
-o
.getLength_in_mm()) <= 2.0;}
123 /** Comparison of two lengthes */
124 bool operator > (const Length
&o
) const {return (length_in_mm
> o
.getLength_in_mm());}
125 bool operator < (const Length
&o
) const {return (length_in_mm
< o
.getLength_in_mm());}
127 /** Comparison of two lengthes */
128 bool operator >= (const Length
&o
) const {return (length_in_mm
>= o
.getLength_in_mm());}
129 bool operator <= (const Length
&o
) const {return (length_in_mm
<= o
.getLength_in_mm());}
131 /** Ratio of two lengthes
133 @warning There is no safeguared to prevent you from division by
134 zero. If the length in the denominator is near 0.0, a floating point
137 @returns the ratio of the two lengthes as a double
139 double operator / (const Length
&o
) const {return (length_in_mm
/o
.getLength_in_mm());}
141 /** Sum of two lengthes
143 @returns the sum of the lengthes as a Length
145 Length
operator + (const Length
&o
) const {Length r
; r
.length_in_mm
= length_in_mm
+ o
.length_in_mm
; return r
; }
147 /** Difference of two lengthes
149 @returns the difference of the lengthes as a Length
151 Length
operator - (const Length
&o
) const {Length r
; r
.length_in_mm
= length_in_mm
- o
.length_in_mm
; return r
; }
153 /** Division of a length
155 @warning There is no safeguared to prevent you from division by
156 zero. If the number in the denominator is near 0.0, a floating point
159 @returns a fraction of the original length as a Length
161 Length
operator / (const double l
) const {Length r
; r
.length_in_mm
= length_in_mm
/l
; return r
; }
163 /** Multiplication of a length
165 @returns a multiplied length as a Length
167 Length
operator * (const double l
) const {Length r
; r
.length_in_mm
= length_in_mm
*l
; return r
; }
169 /** This method converts a string that gives a distance in one of the
170 commonly used units, such as "12.3mm", "12 inch" or "15 didot" to
171 millimeters. For a complete list of supported units, see the
172 static lists that are hardcoded in "units.cpp".
174 If the conversion is not possible *ok is set to "false" and an
175 undefined value is returned. If the unit could not be recognized,
176 an error message is printed via kdError(). Otherwise, *ok is set
179 It is possible in rare circumstances that ok is set to true
180 although the string is malformed.
182 It is fine to set ok to 0. */
183 static float convertToMM(const QString
&distance
, bool *ok
=0);
186 /** Length in millimeters */
193 #undef mm_per_TeXPoint
194 #undef mm_per_bigPoint
198 #undef mm_per_scaledPoint