From 47d455dd55be855e4cc691c32f687f723d9247ee Mon Sep 17 00:00:00 2001 From: toma Date: Wed, 25 Nov 2009 17:56:58 +0000 Subject: Copy the KDE 3.5 branch to branches/trinity for new KDE 3.5 features. BUG:215923 git-svn-id: svn://anonsvn.kde.org/home/kde/branches/trinity/kdegraphics@1054174 283d02a7-25f6-0310-bc7c-ecb5cbfe19da --- kviewshell/units.cpp | 83 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 83 insertions(+) create mode 100644 kviewshell/units.cpp (limited to 'kviewshell/units.cpp') diff --git a/kviewshell/units.cpp b/kviewshell/units.cpp new file mode 100644 index 00000000..e675f916 --- /dev/null +++ b/kviewshell/units.cpp @@ -0,0 +1,83 @@ +// units.cpp +// +// Part of KVIEWSHELL - A framework for multipage text/gfx viewers +// +// (C) 2003 Stefan Kebekus +// Distributed under the GPL + +// Add header files alphabetically + +#include + +#include +#include +#include +#include +#include + +#include "units.h" + +class unitOfDistance +{ + public: + float mmPerUnit; + const char* name; +}; + +unitOfDistance distanceUnitTable[] = { + // Metric units + {1.0, "mm"}, + {1.0, "millimeter"}, + {10.0, "cm"}, + {10.0, "centimeter"}, + {100.0*10.0, "m"}, + {100.0*10.0, "meter"}, + + // Imperial units + {25.4, "in"}, + {25.4, "inch"}, + + // Typographical units + {2540.0/7227.0, "pt"}, // TeX points. 7227points = 254cm + {25.4/72.0, "bp"}, // big points, 1/72 inch as used in Postscript + {25.4/6.0, "pc"}, // pica = 1/6 inch + {25.4/6.0, "pica"}, + {25.4*0.0148, "dd"}, // didot points = 0.0148 inches + {25.4*0.0148, "didot"}, + {25.4*0.178, "cc"}, // cicero points = 0.178 inches + {25.4*0.178, "cicero"}, + + {0.0, 0}, +}; + + + + +float distance::convertToMM(const QString &distance, bool *ok) +{ + // kdDebug() << "convertToMM( " << distance << " )" << endl; + + float MMperUnit = 0.0; + int unitPos = 0; // position of the unit in the string + + // Check for various known units, and store the beginning position + // of the unit in 'unitPos', so that distance[0..unitPos] will hold + // the value. Store the number of mm per unit in 'MMperUnit'. + for(int i=0; MMperUnit==0.0 && distanceUnitTable[i].name != 0; i++) { + unitPos = distance.findRev(distanceUnitTable[i].name); + if (unitPos != -1) + MMperUnit = distanceUnitTable[i].mmPerUnit; + } + + // If no unit has been found -> error message, set *ok to false and + // return 0.0. + if (MMperUnit == 0.0) { + kdError() << "distance::convertToMM: no known unit found in the string '" << distance << "'." << endl; + if (ok) + *ok = false; + return 0.0; + } + + QString val = distance.left(unitPos).simplifyWhiteSpace(); + return MMperUnit*val.toFloat(ok); +} -- cgit v1.2.1