2
For general Scribus (>=1.3.2) copyright and licensing information please refer
3
to the COPYING file provided with the program. Following this notice may exist
4
a copyright and/or license notice that predates the release of Scribus 1.3.2
5
for which a new license (GPL+exception) is in place.
7
/***************************************************************************
9
copyright : (C) 2005 by Craig Bradney
10
email : cbradney@zip.com.au
11
***************************************************************************/
13
/***************************************************************************
15
* This program is free software; you can redistribute it and/or modify *
16
* it under the terms of the GNU General Public License as published by *
17
* the Free Software Foundation; either version 2 of the License, or *
18
* (at your option) any later version. *
20
***************************************************************************/
27
* @brief Returns the ratio to points for the selected unit of measure. Ratios are for: PT, MM, IN, P, CM, C
29
const double unitGetRatioFromIndex(const int index)
31
//PT, MM, IN, P, CM, C (Cicero)
32
//NOTE: Calling functions that divide by this value will crash on divide by 0. They shouldnt be getting
33
// a zero value if they are accessing here with a correct index.
34
if (index<UNITMIN || index>UNITMAX)
36
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 };
41
* @brief Strip the text from a value and return the double value for the unit
43
const double unitValueFromString(const QString& value)
45
QString lowerValue = value.lower();
47
if (lowerValue.find("pt") != -1)
49
dbl = lowerValue.remove("pt");
51
else if (lowerValue.find("mm") != -1)
53
dbl = lowerValue.remove("mm");
55
else if (lowerValue.find("in") != -1)
57
dbl = lowerValue.remove("in");
59
else if (lowerValue.find("p") != -1)
61
dbl = lowerValue.remove("p");
63
else if (lowerValue.find("cm") != -1)
65
dbl = lowerValue.remove("cm");
67
else if (lowerValue.find("c") != -1)
69
dbl = lowerValue.remove("c");
74
dbl = dbl.stripWhiteSpace();
75
return dbl.toDouble();
79
* @brief Strip the text from a value and return the Unit index for the value
81
const Unit unitIndexFromString(const QString& value)
83
QString lowerValue = value.lower();
85
if (lowerValue.find("pt") != -1)
89
else if (lowerValue.find("mm") != -1)
93
else if (lowerValue.find("in") != -1)
97
else if (lowerValue.find("p") != -1)
101
else if (lowerValue.find("cm") != -1)
105
else if (lowerValue.find("c") != -1)
115
* @brief Returns the suffix used in GUI widgets
117
const QString unitGetSuffixFromIndex(const int index)
119
//Could also return " "+unitGetStrFromIndex(indeX);
120
if (index<UNITMIN || index>UNITMAX)
122
QString suffix[] = { QObject::tr(" pt"), QObject::tr(" mm"), QObject::tr(" in"), QObject::tr(" p"), QObject::tr(" cm"), QObject::tr(" c") };
123
return suffix[index];
127
* @brief Returns a general suffix for each of the units
129
const QString unitGetStrFromIndex(const int index)
131
if (index<UNITMIN || index>UNITMAX)
133
QString suffix[] = { QObject::tr("pt"), QObject::tr("mm"), QObject::tr("in"), QObject::tr("p"), QObject::tr("cm"), QObject::tr("c") };
134
return suffix[index];
138
* @brief Returns a general untranslated suffix for each of the units
140
const QString unitGetUntranslatedStrFromIndex(const int index)
142
if (index<UNITMIN || index>UNITMAX)
144
QString suffix[] = { "pt", "mm", "in", "p", "cm", "c" };
145
return suffix[index];
148
* @brief Returns the decimals for the units
150
const int unitGetDecimalsFromIndex(const int index)
152
if (index<UNITMIN || index>UNITMAX)
154
// PT, MM, IN, P, CM, C
155
int decimalPoints[] = {100, 1000, 10000, 100, 10000, 10000};
156
return decimalPoints[index];
160
* @brief Returns the precision for the units
162
const int unitGetPrecisionFromIndex(const int index)
164
if (index<UNITMIN || index>UNITMAX)
167
int precision[] = {2, 3, 4, 2, 4, 4};
168
return precision[index];
172
* @brief Returns a QStringList of the units for use in QComboBoxes etc
174
const QStringList unitGetTextUnitList()
176
QStringList suffixList;
177
suffixList.append( QObject::tr( "Points (pt)" ) );
178
suffixList.append( QObject::tr( "Millimeters (mm)" ) );
179
suffixList.append( QObject::tr( "Inches (in)" ) );
180
suffixList.append( QObject::tr( "Picas (p)" ) );
181
suffixList.append( QObject::tr( "Centimeters (cm)" ) );
182
suffixList.append( QObject::tr( "Cicero (c)" ) );
183
return QStringList(suffixList);
187
* @brief Returns the maximum index of the units we have now
189
const int unitGetMaxIndex()
195
* @brief Returns the pts value from the mm value supplied
197
const double mm2pts(double mm)
199
return mm / unitGetRatioFromIndex(SC_MM);
203
* @brief Returns the pts value from the in value supplied
205
const double in2pts(double in)
207
return in / unitGetRatioFromIndex(SC_IN);
211
* @brief Returns the pts value from the pica value supplied
213
const double p2pts(double p)
215
return p / unitGetRatioFromIndex(SC_P);
219
* @brief Returns the pts value from the cm value supplied
221
const double cm2pts(double cm)
223
return cm / unitGetRatioFromIndex(SC_CM);
227
* @brief Returns the pts value from the cm value supplied
229
const double c2pts(double c)
231
return c / unitGetRatioFromIndex(SC_C);
235
* @brief Returns the mm value from the pt value supplied
237
const double pts2mm(double pts)
239
return pts * unitGetRatioFromIndex(SC_MM);
243
* @brief Returns the in value from the pt value supplied
245
const double pts2in(double pts)
247
return pts * unitGetRatioFromIndex(SC_IN);
251
* @brief Returns the pica value from the pt value supplied
253
const double pts2p(double pts)
255
return pts * unitGetRatioFromIndex(SC_P);
259
* @brief Returns the cm value from the pt value supplied
261
const double pts2cm(double pts)
263
return pts * unitGetRatioFromIndex(SC_CM);
267
* @brief Returns the c value from the pt value supplied
269
const double pts2c(double pts)
271
return pts * unitGetRatioFromIndex(SC_C);
275
* @brief Returns the value from the pt value supplied based on unit index
277
double pts2value(double unitValue, int unit)
283
ret = unitValue; //dont multiply by 1
286
ret = unitValue * unitGetRatioFromIndex(unit);
293
* @brief Returns the pt value from the value supplied based on unit index
295
double value2pts(double unitValue, int unit)
301
ret = unitValue; // dont divide by 1
304
ret = unitValue / unitGetRatioFromIndex(unit);
311
* @brief Returns the secondary unit value from the value supplied based on primary unit
313
double value2value(double unitValue, int primaryUnit, int secondaryUnit)
315
if (primaryUnit==secondaryUnit)
318
double pts = 0.0, ret = 0.0;
319
//Can make this not convert to points at a later stage, for now, the function exists and works.
320
pts= primaryUnit==0 ? unitValue : unitValue / unitGetRatioFromIndex(primaryUnit);
321
ret= secondaryUnit==0 ? pts : pts * unitGetRatioFromIndex(secondaryUnit);
326
* @brief Sets up iteration value 1 for vruler, hruler and tabruler
328
double unitRulerGetIter1FromIndex(const int index)
330
if (index<UNITMIN || index>UNITMAX)
332
// PT, MM, IN, P, CM, C
333
double iter[] = {10.0, 720.0/25.4, 18.0, 12.0, 72.0/25.4, 72.0/25.4*4.512};
338
* @brief Sets up iteration value 2 for vruler, hruler and tabruler
340
double unitRulerGetIter2FromIndex(const int index)
342
if (index<UNITMIN || index>UNITMAX)
344
// PT, MM, IN, P, CM, C
345
double iter[] = {100.0, 7200.0/25.4, 72.0, 120.0, 720.0/25.4, 720.0/25.4*4.512};