File indexing completed on 2024-05-12 16:29:11

0001 /*
0002  * This file is part of Office 2007 Filters for Calligra
0003  * Copyright (C) 2002 Laurent Montel <lmontel@mandrakesoft.com>
0004  * Copyright (c) 2003 Lukas Tinkl <lukas@kde.org>
0005  * Copyright (C) 2003 David Faure <faure@kde.org>
0006  * Copyright (C) 2009-2010 Nokia Corporation and/or its subsidiary(-ies).
0007  *
0008  * Contact: Suresh Chande suresh.chande@nokia.com
0009  *
0010  * This library is free software; you can redistribute it and/or
0011  * modify it under the terms of the GNU Lesser General Public License
0012  * version 2.1 as published by the Free Software Foundation.
0013  *
0014  * This library is distributed in the hope that it will be useful, but
0015  * WITHOUT ANY WARRANTY; without even the implied warranty of
0016  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
0017  * Lesser General Public License for more details.
0018  *
0019  * You should have received a copy of the GNU Lesser General Public
0020  * License along with this library; if not, write to the Free Software
0021  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
0022  * 02110-1301 USA
0023  *
0024  */
0025 
0026 #ifndef MSOOXML_UNITS_H
0027 #define MSOOXML_UNITS_H
0028 
0029 #include <QString>
0030 
0031 // conversion from twips (http://en.wikipedia.org/wiki/Twip)
0032 #define TWIP_TO_DM(tw) ((tw)*0.000176389)
0033 #define TWIP_TO_CM(tw) ((tw)*0.001763889)
0034 #define TWIP_TO_MM(tw) ((tw)*0.017638889)
0035 #define TWIP_TO_POINT(tw) ((tw)*0.05)
0036 #define TWIP_TO_INCH(tw) ((tw)*0.000694444)
0037 #define TWIP_TO_PI(tw) ((tw)*0.004166667)
0038 #define TWIP_TO_CC(tw) ((tw)*0.00389404975957)
0039 #define TWIP_TO_PX(tw) ((tw)*0.066798611)
0040 
0041 // conversion to twips
0042 #define DM_TO_TWIP(dm) ((dm)*5669.28776738)
0043 #define CM_TO_TWIP(cm) ((cm)*566.929098146)
0044 #define MM_TO_TWIP(mm) ((mm)*56.6929130287)
0045 #define POINT_TO_TWIP(pt) ((pt)*20.0)
0046 #define INCH_TO_TWIP(in) ((in)*1440.0)
0047 #define PI_TO_TWIP(pi) ((pi)*240.0)
0048 #define CC_TO_TWIP(cc) ((cc)*256.80206)
0049 #define PX_TO_TWIP(px) ((px)*14.970371)
0050 
0051 // EMU conversion (ECMA-376, 20.1.2.1: EMU Unit of Measurement)
0052 //! Converts emu value (integer or double) to cm
0053 #define EMU_TO_CM(emu) ((emu)/360000.0)
0054 
0055 //! Converts emu value (integer or double) to inches
0056 #define EMU_TO_INCH(emu) ((emu)/914400.0)
0057 
0058 //! Converts emu value (integer or double) to points
0059 #define EMU_TO_POINT(emu) ((emu)/12700.0)
0060 
0061 namespace MSOOXML
0062 {
0063 
0064 namespace Utils {
0065 
0066 //! Performs EMU conversion and returns string.
0067 inline QString cmString(qreal cm)
0068 {
0069     QString res;
0070     return res.sprintf("%3.3fcm", cm);
0071 }
0072 
0073 //! Converts EMU Unit of Measurement to cm.
0074 /*! Converts value expressed in EMU (ECMA-376, 20.1.2.1: EMU Unit of Measurement)
0075     to ODF-compliant "0.000cm" unit.
0076     "0" and "" is converted to "0cm".
0077     @return empty string on error. */
0078 //! CASE \#P505
0079 KOMSOOXML_EXPORT QString EMU_to_ODF(const QString& emuValue);
0080 
0081 //! Converts TWIP Unit of Measurement to cm.
0082 /*! Converts value expressed in TWIPs  to ODF-compliant "0.000cm" unit.
0083     "0" and "" is converted to "0cm".
0084     @return empty string on error. */
0085 KOMSOOXML_EXPORT QString TWIP_to_ODF(const QString& twipValue);
0086 
0087 //! ECMA-376, 17.18.23 ST_EighthPointMeasure (Measurement in Eighths of a Point), p. 1540
0088 /*! Converts eighths of a point (equivalent to 1/576th of an inch) to point
0089     to ODF-compliant "0.000pt" unit.
0090     @return empty string on failure. */
0091 KOMSOOXML_EXPORT QString ST_EighthPointMeasure_to_ODF(const QString& value);
0092 
0093 //! ECMA-376, 22.9.2.14 ST_TwipsMeasure (Measurement in Twentieths of a Point), p. 4339
0094 /*! Converts:
0095     * Case 1: a positive number in twips (twentieths of a point, equivalent to 1/1440th of an inch), or
0096     * Case 2: a positive decimal number immediately followed by a unit identifier.
0097     The conversion's target is ODF-compliant "0.000xx" unit, where xx is "mm", "cm", "pt", etc.
0098     For case 1 it is always "pt".
0099     @return empty string on error. */
0100 KOMSOOXML_EXPORT QString ST_TwipsMeasure_to_pt(const QString& value);
0101 
0102 //! Like ST_TwipsMeasure_to_pt() but for case 1 always converts to "cm".
0103 KOMSOOXML_EXPORT QString ST_TwipsMeasure_to_cm(const QString& value);
0104 
0105 //! ECMA-376, 22.9.2.12 ST_PositiveUniversalMeasure (Positive Universal Measurement), p. 4340
0106 /*! Converts number+unit of measurement into ODF-compliant number+unit.
0107     @a value should match the following regular expression pattern: [0-9]+(\.[0-9]+)?(mm|cm|in|pt|pc|pi).
0108     Values with units mm, cm, in, pt, pi are just copied.
0109     Values with "pc" (another name for Pica) are replaced with "pi".
0110     @return empty string on error. */
0111 KOMSOOXML_EXPORT QString ST_PositiveUniversalMeasure_to_ODF(const QString& value);
0112 
0113 //! Like ST_PositiveUniversalMeasure_to_ODF(const QString&) but always converts to cm.
0114 KOMSOOXML_EXPORT QString ST_PositiveUniversalMeasure_to_cm(const QString& value);
0115 
0116 } // Utils
0117 } // MSOOXML
0118 
0119 //! Performs EMU conversion and returns string.
0120 inline QString EMU_TO_CM_STRING(int emu)
0121 {
0122     return MSOOXML::Utils::cmString(EMU_TO_CM(qreal(emu)));
0123 }
0124 
0125 //! Performs EMU conversion and returns string.
0126 inline QString EMU_TO_INCH_STRING(int emu)
0127 {
0128     return MSOOXML::Utils::cmString(EMU_TO_INCH(qreal(emu)));
0129 }
0130 
0131 // px conversion
0132 #define PT_TO_PX(pt) ((pt)*1.33597222222)
0133 #define PX_TO_CM(px) ((px)*0.0264)
0134 
0135 #endif /* MSOOXML_UNITS_H */