Back to index

scribus-ng  1.3.4.dfsg+svn20071115
units.cpp
Go to the documentation of this file.
00001 /*
00002 For general Scribus (>=1.3.2) copyright and licensing information please refer
00003 to the COPYING file provided with the program. Following this notice may exist
00004 a copyright and/or license notice that predates the release of Scribus 1.3.2
00005 for which a new license (GPL+exception) is in place.
00006 */
00007 /***************************************************************************
00008     begin                : Jan 2005
00009     copyright            : (C) 2005 by Craig Bradney
00010     email                : cbradney@zip.com.au
00011  ***************************************************************************/
00012 
00013 /***************************************************************************
00014  *                                                                         *
00015  *   This program is free software; you can redistribute it and/or modify  *
00016  *   it under the terms of the GNU General Public License as published by  *
00017  *   the Free Software Foundation; either version 2 of the License, or     *
00018  *   (at your option) any later version.                                   *
00019  *                                                                         *
00020  ***************************************************************************/
00021 
00022 #include <qstring.h>
00023 #include <qobject.h>
00024 #include "units.h"
00025 
00029 const double unitGetRatioFromIndex(const int index)
00030 {
00031        //PT, MM, IN, P, CM, C (Cicero)
00032        //NOTE: Calling functions that divide by this value will crash on divide by 0. They shouldnt be getting
00033        // a zero value if they are accessing here with a correct index.
00034        if (index<UNITMIN || index>UNITMAX)
00035               return 0;
00036        double ratio[] = { 1.0, 25.4/72.0, 1.0/72.0, 1.0/12.0, 2.54/72.0, 25.4/72.0/4.512 };
00037        return ratio[index];
00038 }
00039 
00043 const double unitValueFromString(const QString& value)
00044 {
00045        QString lowerValue = value.lower();
00046        QString dbl = "0.0";
00047        if (lowerValue.find("pt") != -1)
00048        {
00049               dbl = lowerValue.remove("pt");
00050        }
00051        else if (lowerValue.find("mm") != -1)
00052        {
00053               dbl = lowerValue.remove("mm");
00054        }
00055        else if (lowerValue.find("in") != -1)
00056        {
00057               dbl = lowerValue.remove("in");
00058        }
00059        else if (lowerValue.find("p") != -1)
00060        {
00061               dbl = lowerValue.remove("p");
00062        }
00063        else if (lowerValue.find("cm") != -1)
00064        {
00065               dbl = lowerValue.remove("cm");
00066        }
00067        else if (lowerValue.find("c") != -1)
00068        {
00069               dbl = lowerValue.remove("c");
00070        }
00071        else
00072               dbl = "0.0";
00073 
00074        dbl = dbl.stripWhiteSpace();
00075        return dbl.toDouble();
00076 }
00077 
00081 const scUnit unitIndexFromString(const QString& value)
00082 {
00083        QString lowerValue = value.lower();
00084        scUnit retVal;
00085        if (lowerValue.find("pt") != -1)
00086        {
00087               retVal=SC_PT;
00088        }
00089        else if (lowerValue.find("mm") != -1)
00090        {
00091               retVal=SC_MM;
00092        }
00093        else if (lowerValue.find("in") != -1)
00094        {
00095               retVal=SC_IN;
00096        }
00097        else if (lowerValue.find("p") != -1)
00098        {
00099               retVal=SC_P;
00100        }
00101        else if (lowerValue.find("cm") != -1)
00102        {
00103               retVal=SC_CM;
00104        }
00105        else if (lowerValue.find("c") != -1)
00106        {
00107               retVal=SC_C;
00108        }      
00109        else
00110               retVal=SC_PT;
00111        return retVal;
00112 }
00113 
00117 const QString unitGetSuffixFromIndex(const int index)
00118 {
00119        //Could also return " "+unitGetStrFromIndex(indeX);
00120        if (index<UNITMIN || index>UNITMAX) 
00121               return "";
00122        QString suffix[] = { QObject::tr(" pt"), QObject::tr(" mm"), QObject::tr(" in"), QObject::tr(" p"), QObject::tr(" cm"), QObject::tr(" c") };
00123        return suffix[index];
00124 }
00125 
00129 const QString unitGetStrFromIndex(const int index)
00130 {
00131        if (index<UNITMIN || index>UNITMAX) 
00132               return "";
00133        QString suffix[] = { QObject::tr("pt"), QObject::tr("mm"), QObject::tr("in"), QObject::tr("p"), QObject::tr("cm"), QObject::tr("c") };
00134        return suffix[index];
00135 }
00136 
00140 const QString unitGetUntranslatedStrFromIndex(const int index)
00141 {
00142        if (index<UNITMIN || index>UNITMAX) 
00143               return "";
00144        QString suffix[] = { "pt", "mm", "in", "p", "cm", "c" };
00145        return suffix[index];
00146 }
00150 const int unitGetDecimalsFromIndex(const int index)
00151 {
00152        if (index<UNITMIN || index>UNITMAX) 
00153               return 0;
00154        //                      PT,   MM,    IN,   P,    CM,   C
00155        int decimalPoints[] = {100, 1000, 10000, 100, 10000, 10000};
00156        return decimalPoints[index];
00157 }
00158 
00162 const int unitGetPrecisionFromIndex(const int index)
00163 {
00164        if (index<UNITMIN || index>UNITMAX) 
00165               return 0;
00166        //                PT,MM,IN, P,CM, C
00167        int precision[] = {2, 3, 4, 2, 4, 4};
00168        return precision[index];
00169 }
00170 
00174 const QStringList unitGetTextUnitList()
00175 {
00176        QStringList suffixList;
00177        suffixList.append( QObject::tr( "Points (pt)" ) );
00178        suffixList.append( QObject::tr( "Millimeters (mm)" ) );
00179        suffixList.append( QObject::tr( "Inches (in)" ) );
00180        suffixList.append( QObject::tr( "Picas (p)" ) );
00181        suffixList.append( QObject::tr( "Centimeters (cm)" ) );
00182        suffixList.append( QObject::tr( "Cicero (c)" ) );
00183        return QStringList(suffixList);
00184 }
00185 
00189 const int unitGetMaxIndex()
00190 {
00191        return UNITMAX;
00192 }
00193 
00197 const double mm2pts(double mm)
00198 {
00199        return mm / unitGetRatioFromIndex(SC_MM);
00200 }
00201 
00205 const double in2pts(double in)
00206 {
00207        return in / unitGetRatioFromIndex(SC_IN);
00208 }
00209 
00213 const double p2pts(double p)
00214 {
00215        return p / unitGetRatioFromIndex(SC_P);
00216 }
00217 
00221 const double cm2pts(double cm)
00222 {
00223        return cm / unitGetRatioFromIndex(SC_CM);
00224 }
00225 
00229 const double c2pts(double c)
00230 {
00231        return c / unitGetRatioFromIndex(SC_C);
00232 }
00233 
00237 const double pts2mm(double pts)
00238 {
00239        return pts * unitGetRatioFromIndex(SC_MM);
00240 }
00241 
00245 const double pts2in(double pts)
00246 {
00247        return pts * unitGetRatioFromIndex(SC_IN);
00248 }
00249 
00253 const double pts2p(double pts)
00254 {
00255        return pts * unitGetRatioFromIndex(SC_P);
00256 }
00257 
00261 const double pts2cm(double pts)
00262 {
00263        return pts * unitGetRatioFromIndex(SC_CM);
00264 }
00265 
00269 const double pts2c(double pts)
00270 {
00271        return pts * unitGetRatioFromIndex(SC_C);
00272 }
00273 
00277 double pts2value(double unitValue, int unit)
00278 {
00279        double ret = 0.0;
00280        switch (unit)
00281        {
00282               case 0:
00283                      ret = unitValue; //dont multiply by 1
00284                      break;
00285               default:
00286                      ret = unitValue * unitGetRatioFromIndex(unit);
00287                      break;
00288        }
00289        return ret;
00290 }
00291 
00295 double value2pts(double unitValue, int unit)
00296 {
00297        double ret = 0.0;
00298        switch (unit)
00299        {
00300               case 0:
00301                      ret = unitValue; // dont divide by 1
00302                      break;
00303               default:
00304                      ret = unitValue / unitGetRatioFromIndex(unit);
00305                      break;
00306        }
00307        return ret;
00308 }
00309 
00313 double value2value(double unitValue, int primaryUnit, int secondaryUnit)
00314 {
00315        if (primaryUnit==secondaryUnit)
00316               return unitValue;
00317               
00318        double pts = 0.0, ret = 0.0;
00319        //Can make this not convert to points at a later stage, for now, the function exists and works.
00320        pts= primaryUnit==0 ? unitValue : unitValue / unitGetRatioFromIndex(primaryUnit);
00321        ret= secondaryUnit==0 ? pts : pts * unitGetRatioFromIndex(secondaryUnit);
00322        return ret;
00323 }
00324 
00328 double unitRulerGetIter1FromIndex(const int index)
00329 {
00330        if (index<UNITMIN || index>UNITMAX) 
00331               return 0;
00332        //                 PT,         MM,   IN,    P,        CM,               C
00333        double iter[] = {10.0, 720.0/25.4, 18.0, 12.0, 72.0/25.4, 72.0/25.4*4.512};
00334        return iter[index];
00335 }
00336 
00340 double unitRulerGetIter2FromIndex(const int index)
00341 {
00342        if (index<UNITMIN || index>UNITMAX) 
00343               return 0;
00344        //                  PT,          MM,   IN,     P,         CM,                C
00345        double iter[] = {100.0, 7200.0/25.4, 72.0, 120.0, 720.0/25.4, 720.0/25.4*4.512};
00346        return iter[index];
00347 }