xref: /aoo4110/main/oox/inc/oox/xls/unitconverter.hxx (revision b1cdbd2c)
1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 #ifndef OOX_XLS_UNITCONVERTER_HXX
25 #define OOX_XLS_UNITCONVERTER_HXX
26 
27 #include <map>
28 #include <vector>
29 #include "oox/xls/workbookhelper.hxx"
30 
31 namespace com { namespace sun { namespace star {
32     namespace util { struct Date; struct DateTime; }
33 } } }
34 
35 namespace oox {
36 namespace xls {
37 
38 // ============================================================================
39 
40 /** Units supported by the UnitConverter class. */
41 enum Unit
42 {
43     UNIT_INCH,          /// Inches.
44     UNIT_POINT,         /// Points.
45     UNIT_TWIP,          /// Twips (1/20 point).
46     UNIT_EMU,           /// English Metric Unit (1/360,000 cm).
47     UNIT_SCREENX,       /// Horizontal screen pixels.
48     UNIT_SCREENY,       /// Vertical screen pixels.
49     UNIT_REFDEVX,       /// Horizontal pixels in Calc reference device.
50     UNIT_REFDEVY,       /// Vertical pixels in Calc reference device.
51     UNIT_DIGIT,         /// Digit width of document default font.
52     UNIT_SPACE,         /// Space character width of document default font.
53 
54     UNIT_ENUM_SIZE
55 };
56 
57 /** Helper class that provides functions to convert values from and to
58     different units.
59 
60     Provides functions to calculate the width of certain characters of the
61     default font of the imported/exported document. The default font is always
62     the first font in the styles font list, and is always referenced by the
63     default cell style ("Normal" style in Excel) which is used by all empty
64     unformatted cells in the document. To be able to calculate the charcter
65     width correctly, the default font must be known, which is the case after
66     the finalizeImport() or finalizeExport() functions have been called. Caller
67     must make sure to not call the character width conversion functions before.
68  */
69 class UnitConverter : public WorkbookHelper
70 {
71 public:
72     explicit            UnitConverter( const WorkbookHelper& rHelper );
73 
74     /** Final processing after import of all style settings. */
75     void                finalizeImport();
76     /** Updates internal nulldate for date/serial conversion. */
77     void                finalizeNullDate( const ::com::sun::star::util::Date& rNullDate );
78 
79     /** Converts the passed value between the passed units. */
80     double              scaleValue( double fValue, Unit eFromUnit, Unit eToUnit ) const;
81 
82     /** Converts the passed value to 1/100 millimeters. */
83     sal_Int32           scaleToMm100( double fValue, Unit eUnit ) const;
84     /** Converts the passed value from 1/100 millimeters to the passed unit. */
85     double              scaleFromMm100( sal_Int32 nMm100, Unit eUnit ) const;
86 
87     /** Returns the serial value of the passed datetime, based on current nulldate. */
88     double              calcSerialFromDateTime( const ::com::sun::star::util::DateTime& rDateTime ) const;
89     /** Returns the datetime of the passed serial value, based on current nulldate. */
90     ::com::sun::star::util::DateTime calcDateTimeFromSerial( double fSerial ) const;
91 
92     /** Returns an error string from the passed BIFF error code. */
93     ::rtl::OUString     calcOoxErrorCode( sal_uInt8 nErrorCode ) const;
94     /** Returns a BIFF error code from the passed error string. */
95     sal_uInt8           calcBiffErrorCode( const ::rtl::OUString& rErrorCode ) const;
96 
97 private:
98     /** Adds an error code to the internal maps. */
99     void                addErrorCode( sal_uInt8 nErrorCode, const ::rtl::OUString& rErrorCode );
100     /** Returns the conversion coefficient for the passed unit. */
101     double              getCoefficient( Unit eUnit ) const;
102 
103 private:
104     typedef ::std::vector< double >                     DoubleVector;
105     typedef ::std::map< ::rtl::OUString, sal_uInt8 >    OoxErrorCodeMap;
106     typedef ::std::map< sal_uInt8, ::rtl::OUString >    BiffErrorCodeMap;
107 
108     DoubleVector        maCoeffs;           /// Coefficients for unit conversion.
109     OoxErrorCodeMap     maOoxErrCodes;      /// Maps error code strings to BIFF error constants.
110     BiffErrorCodeMap    maBiffErrCodes;     /// Maps BIFF error constants to error code strings.
111     sal_Int32           mnNullDate;         /// Nulldate of this workbook (number of days since 0000-01-01).
112 };
113 
114 // ============================================================================
115 
116 } // namespace xls
117 } // namespace oox
118 
119 #endif
120