1 /*
2 * This file is part of Office 2007 Filters for Calligra
3 * Copyright (C) 2002 Laurent Montel <lmontel@mandrakesoft.com>
4 * Copyright (c) 2003 Lukas Tinkl <lukas@kde.org>
5 * Copyright (C) 2003 David Faure <faure@kde.org>
6 * Copyright (C) 2009-2010 Nokia Corporation and/or its subsidiary(-ies).
7 *
8 * Contact: Suresh Chande suresh.chande@nokia.com
9 *
10 * This library is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Lesser General Public License
12 * version 2.1 as published by the Free Software Foundation.
13 *
14 * This library is distributed in the hope that it will be useful, but
15 * WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
18 *
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this library; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
22 * 02110-1301 USA
23 *
24 */
25
26 #ifndef MSOOXML_UNITS_H
27 #define MSOOXML_UNITS_H
28
29 #include <QString>
30
31 // conversion from twips (http://en.wikipedia.org/wiki/Twip)
32 #define TWIP_TO_DM(tw) ((tw)*0.000176389)
33 #define TWIP_TO_CM(tw) ((tw)*0.001763889)
34 #define TWIP_TO_MM(tw) ((tw)*0.017638889)
35 #define TWIP_TO_POINT(tw) ((tw)*0.05)
36 #define TWIP_TO_INCH(tw) ((tw)*0.000694444)
37 #define TWIP_TO_PI(tw) ((tw)*0.004166667)
38 #define TWIP_TO_CC(tw) ((tw)*0.00389404975957)
39 #define TWIP_TO_PX(tw) ((tw)*0.066798611)
40
41 // conversion to twips
42 #define DM_TO_TWIP(dm) ((dm)*5669.28776738)
43 #define CM_TO_TWIP(cm) ((cm)*566.929098146)
44 #define MM_TO_TWIP(mm) ((mm)*56.6929130287)
45 #define POINT_TO_TWIP(pt) ((pt)*20.0)
46 #define INCH_TO_TWIP(in) ((in)*1440.0)
47 #define PI_TO_TWIP(pi) ((pi)*240.0)
48 #define CC_TO_TWIP(cc) ((cc)*256.80206)
49 #define PX_TO_TWIP(px) ((px)*14.970371)
50
51 // EMU conversion (ECMA-376, 20.1.2.1: EMU Unit of Measurement)
52 //! Converts emu value (integer or double) to cm
53 #define EMU_TO_CM(emu) ((emu)/360000.0)
54
55 //! Converts emu value (integer or double) to inches
56 #define EMU_TO_INCH(emu) ((emu)/914400.0)
57
58 //! Converts emu value (integer or double) to points
59 #define EMU_TO_POINT(emu) ((emu)/12700.0)
60
61 namespace MSOOXML
62 {
63
64 namespace Utils {
65
66 //! Performs EMU conversion and returns string.
cmString(qreal cm)67 inline QString cmString(qreal cm)
68 {
69 QString res;
70 return res.sprintf("%3.3fcm", cm);
71 }
72
73 //! Converts EMU Unit of Measurement to cm.
74 /*! Converts value expressed in EMU (ECMA-376, 20.1.2.1: EMU Unit of Measurement)
75 to ODF-compliant "0.000cm" unit.
76 "0" and "" is converted to "0cm".
77 @return empty string on error. */
78 //! CASE \#P505
79 KOMSOOXML_EXPORT QString EMU_to_ODF(const QString& emuValue);
80
81 //! Converts TWIP Unit of Measurement to cm.
82 /*! Converts value expressed in TWIPs to ODF-compliant "0.000cm" unit.
83 "0" and "" is converted to "0cm".
84 @return empty string on error. */
85 KOMSOOXML_EXPORT QString TWIP_to_ODF(const QString& twipValue);
86
87 //! ECMA-376, 17.18.23 ST_EighthPointMeasure (Measurement in Eighths of a Point), p. 1540
88 /*! Converts eighths of a point (equivalent to 1/576th of an inch) to point
89 to ODF-compliant "0.000pt" unit.
90 @return empty string on failure. */
91 KOMSOOXML_EXPORT QString ST_EighthPointMeasure_to_ODF(const QString& value);
92
93 //! ECMA-376, 22.9.2.14 ST_TwipsMeasure (Measurement in Twentieths of a Point), p. 4339
94 /*! Converts:
95 * Case 1: a positive number in twips (twentieths of a point, equivalent to 1/1440th of an inch), or
96 * Case 2: a positive decimal number immediately followed by a unit identifier.
97 The conversion's target is ODF-compliant "0.000xx" unit, where xx is "mm", "cm", "pt", etc.
98 For case 1 it is always "pt".
99 @return empty string on error. */
100 KOMSOOXML_EXPORT QString ST_TwipsMeasure_to_pt(const QString& value);
101
102 //! Like ST_TwipsMeasure_to_pt() but for case 1 always converts to "cm".
103 KOMSOOXML_EXPORT QString ST_TwipsMeasure_to_cm(const QString& value);
104
105 //! ECMA-376, 22.9.2.12 ST_PositiveUniversalMeasure (Positive Universal Measurement), p. 4340
106 /*! Converts number+unit of measurement into ODF-compliant number+unit.
107 @a value should match the following regular expression pattern: [0-9]+(\.[0-9]+)?(mm|cm|in|pt|pc|pi).
108 Values with units mm, cm, in, pt, pi are just copied.
109 Values with "pc" (another name for Pica) are replaced with "pi".
110 @return empty string on error. */
111 KOMSOOXML_EXPORT QString ST_PositiveUniversalMeasure_to_ODF(const QString& value);
112
113 //! Like ST_PositiveUniversalMeasure_to_ODF(const QString&) but always converts to cm.
114 KOMSOOXML_EXPORT QString ST_PositiveUniversalMeasure_to_cm(const QString& value);
115
116 } // Utils
117 } // MSOOXML
118
119 //! Performs EMU conversion and returns string.
EMU_TO_CM_STRING(int emu)120 inline QString EMU_TO_CM_STRING(int emu)
121 {
122 return MSOOXML::Utils::cmString(EMU_TO_CM(qreal(emu)));
123 }
124
125 //! Performs EMU conversion and returns string.
EMU_TO_INCH_STRING(int emu)126 inline QString EMU_TO_INCH_STRING(int emu)
127 {
128 return MSOOXML::Utils::cmString(EMU_TO_INCH(qreal(emu)));
129 }
130
131 // px conversion
132 #define PT_TO_PX(pt) ((pt)*1.33597222222)
133 #define PX_TO_CM(px) ((px)*0.0264)
134
135 #endif /* MSOOXML_UNITS_H */
136