File indexing completed on 2024-05-12 04:33:58
0001 // -*- Mode: C++; c-basic-offset: 2; indent-tabs-mode: nil; c-brace-offset: 0; -*- 0002 // 0003 // Class: length 0004 // 0005 // Part of KVIEWSHELL 0006 // 0007 // SPDX-FileCopyrightText: 2005 Stefan Kebekus 0008 // SPDX-FileCopyrightText: 2006 Wilfried Huss 0009 // 0010 // SPDX-License-Identifier: GPL-2.0-or-later 0011 0012 #ifndef _length_h_ 0013 #define _length_h_ 0014 0015 #include <cmath> 0016 #include <math.h> 0017 0018 class QString; 0019 0020 #define mm_per_cm 10.0 0021 #define mm_per_m 1000.0 0022 #define mm_per_inch 25.4 0023 #define mm_per_TeXPoint (2540.0 / 7227.0) 0024 #define mm_per_bigPoint (25.4 / 72.0) 0025 #define mm_per_pica (25.4 / 6.0) 0026 #define mm_per_didot (25.4 * 0.0148) 0027 #define mm_per_cicero (25.4 * 0.178) 0028 #define mm_per_scaledPoint (25.4 / (72.27 * 65536.0)) 0029 0030 /** @short Represents a phyical length 0031 0032 This class is used to represent a physical length. Its main purpose 0033 it to help in the conversion of units, and to avoid confusion 0034 about units. To avoid misunderstandings, there is no default 0035 constructor so that this class needs to be explicitly initialized 0036 with one of the functions below. 0037 0038 @warning Lengths are stored internally in mm. If you convert to 0039 or from any other unit, expect floating point round-off errors. 0040 0041 @author Stefan Kebekus <kebekus@kde.org> 0042 @version 1.0.0 0043 */ 0044 0045 class Length 0046 { 0047 public: 0048 /** constructs a 'length = 0mm' object */ 0049 Length() 0050 { 0051 length_in_mm = 0; 0052 } 0053 0054 /** sets the length in millimeters */ 0055 void setLength_in_mm(double l) 0056 { 0057 length_in_mm = l; 0058 } 0059 0060 /** sets the length in centimeters */ 0061 void setLength_in_cm(double l) 0062 { 0063 length_in_mm = l * mm_per_cm; 0064 } 0065 0066 /** sets the length in meters */ 0067 void setLength_in_m(double l) 0068 { 0069 length_in_mm = l * mm_per_m; 0070 } 0071 0072 /** sets the length in inches */ 0073 void setLength_in_inch(double l) 0074 { 0075 length_in_mm = l * mm_per_inch; 0076 } 0077 0078 /** sets the length in TeX points */ 0079 void setLength_in_TeXPoints(double l) 0080 { 0081 length_in_mm = l * mm_per_TeXPoint; 0082 } 0083 0084 /** sets the length in big points (1/72 of an inch) */ 0085 void setLength_in_bigPoints(double l) 0086 { 0087 length_in_mm = l * mm_per_bigPoint; 0088 } 0089 0090 /** sets the length in picas (1/6 of an inch) */ 0091 void setLength_in_pica(double l) 0092 { 0093 length_in_mm = l * mm_per_pica; 0094 } 0095 0096 /** sets the length in didots (0.0148 inches) */ 0097 void setLength_in_didot(double l) 0098 { 0099 length_in_mm = l * mm_per_didot; 0100 } 0101 0102 /** sets the length in ciceros (0.178 inches) */ 0103 void setLength_in_cicero(double l) 0104 { 0105 length_in_mm = l * mm_per_cicero; 0106 } 0107 0108 /** sets the length in scaled points (1 scaled point = 65536 TeX points) */ 0109 void setLength_in_scaledPoints(double l) 0110 { 0111 length_in_mm = l * mm_per_scaledPoint; 0112 } 0113 0114 /** sets the length (@param l ) in pixels. The parameter @param res is the resolution of the 0115 used device in DPI. */ 0116 void setLength_in_pixel(int l, double res) 0117 { 0118 setLength_in_inch(l / res); 0119 } 0120 0121 /** @returns the length in millimeters */ 0122 double getLength_in_mm() const 0123 { 0124 return length_in_mm; 0125 } 0126 0127 /** @returns the length in centimeters */ 0128 double getLength_in_cm() const 0129 { 0130 return length_in_mm / mm_per_cm; 0131 } 0132 0133 /** @returns the length in meters */ 0134 double getLength_in_m() const 0135 { 0136 return length_in_mm / mm_per_m; 0137 } 0138 0139 /** @returns the length in inches */ 0140 double getLength_in_inch() const 0141 { 0142 return length_in_mm / mm_per_inch; 0143 } 0144 0145 /** @returns the length in TeX points */ 0146 double getLength_in_TeXPoints() const 0147 { 0148 return length_in_mm / mm_per_TeXPoint; 0149 } 0150 0151 /** @returns the length in big points (1/72 of an inch) */ 0152 double getLength_in_bigPoints() const 0153 { 0154 return length_in_mm / mm_per_bigPoint; 0155 } 0156 0157 /** @returns the length in picas (1/6 of an inch) */ 0158 double getLength_in_pica() const 0159 { 0160 return length_in_mm / mm_per_pica; 0161 } 0162 0163 /** @returns the length in didots (0.0148 inches) */ 0164 double getLength_in_didot() const 0165 { 0166 return length_in_mm / mm_per_didot; 0167 } 0168 0169 /** @returns the length in ciceros (0.178 inches) */ 0170 double getLength_in_cicero() const 0171 { 0172 return length_in_mm / mm_per_cicero; 0173 } 0174 0175 /** @returns the length in scaled points (1 scaled point = 65536 TeX points) */ 0176 double getLength_in_scaledPoints() const 0177 { 0178 return length_in_mm / mm_per_scaledPoint; 0179 } 0180 0181 /** @returns the length in pixel. The parameter @param res is the resolution of the 0182 used device in DPI. */ 0183 int getLength_in_pixel(double res) const 0184 { 0185 return int(getLength_in_inch() * res); 0186 } 0187 0188 /** @returns true is lengths differ by no more than 2mm */ 0189 bool isNearlyEqual(const Length o) const 0190 { 0191 return fabs(length_in_mm - o.getLength_in_mm()) <= 2.0; 0192 } 0193 0194 /** Comparison of two lengthes */ 0195 bool operator>(const Length o) const 0196 { 0197 return (length_in_mm > o.getLength_in_mm()); 0198 } 0199 bool operator<(const Length o) const 0200 { 0201 return (length_in_mm < o.getLength_in_mm()); 0202 } 0203 0204 /** Comparison of two lengthes */ 0205 bool operator>=(const Length o) const 0206 { 0207 return (length_in_mm >= o.getLength_in_mm()); 0208 } 0209 bool operator<=(const Length o) const 0210 { 0211 return (length_in_mm <= o.getLength_in_mm()); 0212 } 0213 0214 /** Ratio of two lengthes 0215 0216 @warning There is no safeguared to prevent you from division by 0217 zero. If the length in the denominator is near 0.0, a floating point 0218 exception may occur. 0219 0220 @returns the ratio of the two lengthes as a double 0221 */ 0222 double operator/(const Length o) const 0223 { 0224 return (length_in_mm / o.getLength_in_mm()); 0225 } 0226 0227 /** Sum of two lengthes 0228 0229 @returns the sum of the lengthes as a Length 0230 */ 0231 Length operator+(const Length o) const 0232 { 0233 Length r; 0234 r.length_in_mm = length_in_mm + o.length_in_mm; 0235 return r; 0236 } 0237 0238 /** Difference of two lengthes 0239 0240 @returns the difference of the lengthes as a Length 0241 */ 0242 Length operator-(const Length o) const 0243 { 0244 Length r; 0245 r.length_in_mm = length_in_mm - o.length_in_mm; 0246 return r; 0247 } 0248 0249 /** Division of a length 0250 0251 @warning There is no safeguared to prevent you from division by 0252 zero. If the number in the denominator is near 0.0, a floating point 0253 exception may occur. 0254 0255 @returns a fraction of the original length as a Length 0256 */ 0257 Length operator/(const double l) const 0258 { 0259 Length r; 0260 r.length_in_mm = length_in_mm / l; 0261 return r; 0262 } 0263 0264 /** Multiplication of a length 0265 0266 @returns a multiplied length as a Length 0267 */ 0268 Length operator*(const double l) const 0269 { 0270 Length r; 0271 r.length_in_mm = length_in_mm * l; 0272 return r; 0273 } 0274 0275 /** This method converts a string that gives a distance in one of the 0276 commonly used units, such as "12.3mm", "12 inch" or "15 didot" to 0277 millimeters. For a complete list of supported units, see the 0278 static lists that are hardcoded in "units.cpp". 0279 0280 If the conversion is not possible *ok is set to "false" and an 0281 undefined value is returned. If the unit could not be recognized, 0282 an error message is printed via kdError(). Otherwise, *ok is set 0283 to true. 0284 0285 It is possible in rare circumstances that ok is set to true 0286 although the string is malformed. 0287 0288 It is fine to set ok to 0. */ 0289 static float convertToMM(const QString &distance, bool *ok = nullptr); 0290 0291 private: 0292 /** Length in millimeters */ 0293 double length_in_mm; 0294 }; 0295 0296 #undef mm_per_cm 0297 #undef mm_per_m 0298 #undef mm_per_inch 0299 #undef mm_per_TeXPoint 0300 #undef mm_per_bigPoint 0301 #undef mm_per_pica 0302 #undef mm_per_didot 0303 #undef mm_per_cicero 0304 #undef mm_per_scaledPoint 0305 0306 #endif