Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
5 : * This Source Code Form is subject to the terms of the Mozilla Public
6 : * License, v. 2.0. If a copy of the MPL was not distributed with this
7 : * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
11 : * Licensed to the Apache Software Foundation (ASF) under one or more
12 : * contributor license agreements. See the NOTICE file distributed
13 : * with this work for additional information regarding copyright
14 : * ownership. The ASF licenses this file to you under the Apache
15 : * License, Version 2.0 (the "License"); you may not use this file
16 : * except in compliance with the License. You may obtain a copy of
17 : * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18 : */
19 :
20 : #include "unitconverter.hxx"
21 :
22 : #include <com/sun/star/awt/DeviceInfo.hpp>
23 : #include <com/sun/star/awt/FontDescriptor.hpp>
24 : #include <com/sun/star/awt/XDevice.hpp>
25 : #include <com/sun/star/awt/XFont.hpp>
26 : #include <com/sun/star/util/Date.hpp>
27 : #include <com/sun/star/util/DateTime.hpp>
28 : #include <rtl/math.hxx>
29 : #include "oox/core/filterbase.hxx"
30 : #include "oox/helper/propertyset.hxx"
31 : #include "oox/token/properties.hxx"
32 : #include "stylesbuffer.hxx"
33 :
34 : namespace oox {
35 : namespace xls {
36 :
37 : // ============================================================================
38 :
39 : using namespace ::com::sun::star::awt;
40 : using namespace ::com::sun::star::uno;
41 : using namespace ::com::sun::star::util;
42 :
43 : using ::rtl::OUString;
44 :
45 : // ============================================================================
46 :
47 : namespace {
48 :
49 : const double MM100_PER_INCH = 2540.0;
50 : const double MM100_PER_POINT = MM100_PER_INCH / 72.0;
51 : const double MM100_PER_TWIP = MM100_PER_POINT / 20.0;
52 : const double MM100_PER_EMU = 1.0 / 360.0;
53 :
54 : // ----------------------------------------------------------------------------
55 :
56 : /** Returns true, if the passed year is a leap year. */
57 44 : inline sal_Int32 lclIsLeapYear( sal_Int32 nYear )
58 : {
59 44 : return ((nYear % 4) == 0) && (((nYear % 100) != 0) || ((nYear % 400) == 0));
60 : }
61 :
62 0 : void lclSkipYearBlock( sal_Int32& ornDays, sal_uInt16& ornYear, sal_Int32 nDaysInBlock, sal_Int32 nYearsPerBlock, sal_Int32 nMaxBlocks )
63 : {
64 0 : sal_Int32 nBlocks = ::std::min< sal_Int32 >( ornDays / nDaysInBlock, nMaxBlocks );
65 0 : ornYear = static_cast< sal_uInt16 >( ornYear + nYearsPerBlock * nBlocks );
66 0 : ornDays -= nBlocks * nDaysInBlock;
67 0 : }
68 :
69 : /** Returns the number of days before the passed date, starting from the null
70 : date 0000-Jan-01, using standard leap year conventions. */
71 44 : sal_Int32 lclGetDays( const Date& rDate )
72 : {
73 : // number of days in all full years before passed date including all leap days
74 44 : sal_Int32 nDays = rDate.Year * 365 + ((rDate.Year + 3) / 4) - ((rDate.Year + 99) / 100) + ((rDate.Year + 399) / 400);
75 : OSL_ENSURE( (1 <= rDate.Month) && (rDate.Month <= 12), "lclGetDays - invalid month" );
76 : OSL_ENSURE( (1 <= rDate.Day) && (rDate.Day <= 31), "lclGetDays - invalid day" ); // yes, this is weak...
77 44 : if( (1 <= rDate.Month) && (rDate.Month <= 12) )
78 : {
79 : // number of days at start of month jan feb mar apr may jun jul aug sep oct nov dec
80 : static const sal_Int32 spnCumDays[] = { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334 };
81 : // add number of days in full months before passed date
82 44 : nDays += spnCumDays[ rDate.Month - 1 ];
83 : // add number of days from passed date (this adds one day too much)
84 44 : nDays += rDate.Day;
85 : /* Remove the one day added too much if there is no leap day before
86 : the passed day in the passed year. This means: remove the day, if
87 : we are in january or february (leap day not reached if existing),
88 : or if the passed year is not a leap year. */
89 44 : if( (rDate.Month < 3) || !lclIsLeapYear( rDate.Year ) )
90 44 : --nDays;
91 : }
92 44 : return nDays;
93 : }
94 :
95 : } // namespace
96 :
97 : // ----------------------------------------------------------------------------
98 :
99 22 : UnitConverter::UnitConverter( const WorkbookHelper& rHelper ) :
100 : WorkbookHelper( rHelper ),
101 : maCoeffs( UNIT_ENUM_SIZE, 1.0 ),
102 22 : mnNullDate( lclGetDays( Date( 30, 12, 1899 ) ) )
103 : {
104 : // initialize constant and default coefficients
105 22 : const DeviceInfo& rDeviceInfo = getBaseFilter().getGraphicHelper().getDeviceInfo();
106 22 : maCoeffs[ UNIT_INCH ] = MM100_PER_INCH;
107 22 : maCoeffs[ UNIT_POINT ] = MM100_PER_POINT;
108 22 : maCoeffs[ UNIT_TWIP ] = MM100_PER_TWIP;
109 22 : maCoeffs[ UNIT_EMU ] = MM100_PER_EMU;
110 22 : maCoeffs[ UNIT_SCREENX ] = (rDeviceInfo.PixelPerMeterX > 0) ? (100000.0 / rDeviceInfo.PixelPerMeterX) : 50.0;
111 22 : maCoeffs[ UNIT_SCREENY ] = (rDeviceInfo.PixelPerMeterY > 0) ? (100000.0 / rDeviceInfo.PixelPerMeterY) : 50.0;
112 22 : maCoeffs[ UNIT_REFDEVX ] = 12.5; // default: 1 px = 0.125 mm
113 22 : maCoeffs[ UNIT_REFDEVY ] = 12.5; // default: 1 px = 0.125 mm
114 22 : maCoeffs[ UNIT_DIGIT ] = 200.0; // default: 1 digit = 2 mm
115 22 : maCoeffs[ UNIT_SPACE ] = 100.0; // default 1 space = 1 mm
116 :
117 : // error code maps
118 22 : addErrorCode( BIFF_ERR_NULL, "#NULL!" );
119 22 : addErrorCode( BIFF_ERR_DIV0, "#DIV/0!" );
120 22 : addErrorCode( BIFF_ERR_VALUE, "#VALUE!" );
121 22 : addErrorCode( BIFF_ERR_REF, "#REF!" );
122 22 : addErrorCode( BIFF_ERR_NAME, "#NAME?" );
123 22 : addErrorCode( BIFF_ERR_NUM, "#NUM!" );
124 22 : addErrorCode( BIFF_ERR_NA, "#NA" );
125 22 : }
126 :
127 22 : void UnitConverter::finalizeImport()
128 : {
129 22 : PropertySet aDocProps( getDocument() );
130 22 : Reference< XDevice > xDevice( aDocProps.getAnyProperty( PROP_ReferenceDevice ), UNO_QUERY );
131 22 : if( xDevice.is() )
132 : {
133 : // get reference device metric first, needed to get character widths below
134 22 : DeviceInfo aInfo = xDevice->getInfo();
135 22 : maCoeffs[ UNIT_REFDEVX ] = 100000.0 / aInfo.PixelPerMeterX;
136 22 : maCoeffs[ UNIT_REFDEVY ] = 100000.0 / aInfo.PixelPerMeterY;
137 :
138 : // get character widths from default font
139 22 : if( const Font* pDefFont = getStyles().getDefaultFont().get() )
140 : {
141 : // XDevice expects pixels in font descriptor, but font contains twips
142 22 : FontDescriptor aDesc = pDefFont->getFontDescriptor();
143 22 : Reference< XFont > xFont = xDevice->getFont( aDesc );
144 22 : if( xFont.is() )
145 : {
146 : // get maximum width of all digits
147 22 : sal_Int32 nDigitWidth = 0;
148 242 : for( sal_Unicode cChar = '0'; cChar <= '9'; ++cChar )
149 220 : nDigitWidth = ::std::max( nDigitWidth, scaleToMm100( xFont->getCharWidth( cChar ), UNIT_TWIP ) );
150 22 : if( nDigitWidth > 0 )
151 22 : maCoeffs[ UNIT_DIGIT ] = nDigitWidth;
152 : // get width of space character
153 22 : sal_Int32 nSpaceWidth = scaleToMm100( xFont->getCharWidth( ' ' ), UNIT_TWIP );
154 22 : if( nSpaceWidth > 0 )
155 22 : maCoeffs[ UNIT_SPACE ] = nSpaceWidth;
156 22 : }
157 : }
158 22 : }
159 22 : }
160 :
161 22 : void UnitConverter::finalizeNullDate( const Date& rNullDate )
162 : {
163 : // convert the nulldate to number of days since 0000-Jan-01
164 22 : mnNullDate = lclGetDays( rNullDate );
165 22 : }
166 :
167 : // conversion -----------------------------------------------------------------
168 :
169 16 : double UnitConverter::scaleValue( double fValue, Unit eFromUnit, Unit eToUnit ) const
170 : {
171 16 : return (eFromUnit == eToUnit) ? fValue : (fValue * getCoefficient( eFromUnit ) / getCoefficient( eToUnit ));
172 : }
173 :
174 1296 : sal_Int32 UnitConverter::scaleToMm100( double fValue, Unit eUnit ) const
175 : {
176 1296 : return static_cast< sal_Int32 >( fValue * getCoefficient( eUnit ) + 0.5 );
177 : }
178 :
179 18 : double UnitConverter::scaleFromMm100( sal_Int32 nMm100, Unit eUnit ) const
180 : {
181 18 : return static_cast< double >( nMm100 ) / getCoefficient( eUnit );
182 : }
183 :
184 0 : double UnitConverter::calcSerialFromDateTime( const DateTime& rDateTime ) const
185 : {
186 0 : sal_Int32 nDays = lclGetDays( Date( rDateTime.Day, rDateTime.Month, rDateTime.Year ) ) - mnNullDate;
187 : OSL_ENSURE( nDays >= 0, "UnitConverter::calcDateTimeSerial - invalid date" );
188 : OSL_ENSURE( (rDateTime.Hours <= 23) && (rDateTime.Minutes <= 59) && (rDateTime.Seconds <= 59), "UnitConverter::calcDateTimeSerial - invalid time" );
189 0 : return nDays + rDateTime.Hours / 24.0 + rDateTime.Minutes / 1440.0 + rDateTime.Seconds / 86400.0;
190 : }
191 :
192 0 : DateTime UnitConverter::calcDateTimeFromSerial( double fSerial ) const
193 : {
194 0 : DateTime aDateTime( 0, 0, 0, 0, 1, 1, 0 );
195 0 : double fDays = 0.0;
196 0 : double fTime = modf( fSerial, &fDays );
197 :
198 : // calculate date from number of days with O(1) complexity
199 0 : sal_Int32 nDays = getLimitedValue< sal_Int32, double >( fDays + mnNullDate, 0, 3652424 );
200 : // skip year 0, assumed to be a leap year. By starting at year 1, leap years can be handled easily
201 0 : if( nDays >= 366 ) { ++aDateTime.Year; nDays -= 366; }
202 : // skip full blocks of 400, 100, 4 years, and remaining full years
203 0 : lclSkipYearBlock( nDays, aDateTime.Year, 400 * 365 + 97, 400, 24 );
204 0 : lclSkipYearBlock( nDays, aDateTime.Year, 100 * 365 + 24, 100, 3 );
205 0 : lclSkipYearBlock( nDays, aDateTime.Year, 4 * 365 + 1, 4, 24 );
206 0 : lclSkipYearBlock( nDays, aDateTime.Year, 365, 1, 3 );
207 : // skip full months of current year
208 : static const sal_Int32 spnDaysInMonth[] = { 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
209 0 : if( (nDays >= 59) && !lclIsLeapYear( aDateTime.Year ) ) ++nDays;
210 0 : const sal_Int32* pnDaysInMonth = spnDaysInMonth;
211 0 : while( *pnDaysInMonth >= nDays ) { ++aDateTime.Month; nDays -= *pnDaysInMonth; ++pnDaysInMonth; }
212 0 : aDateTime.Day = static_cast< sal_uInt16 >( nDays + 1 );
213 :
214 : // calculate time from fractional part of serial
215 0 : sal_Int32 nTime = getLimitedValue< sal_Int32, double >( fTime * 86400, 0, 86399 );
216 0 : aDateTime.Seconds = static_cast< sal_uInt16 >( nTime % 60 );
217 0 : nTime /= 60;
218 0 : aDateTime.Minutes = static_cast< sal_uInt16 >( nTime % 60 );
219 0 : aDateTime.Hours = static_cast< sal_uInt16 >( nTime / 60 );
220 :
221 0 : return aDateTime;
222 : }
223 :
224 0 : sal_uInt8 UnitConverter::calcBiffErrorCode( const OUString& rErrorCode ) const
225 : {
226 0 : OoxErrorCodeMap::const_iterator aIt = maOoxErrCodes.find( rErrorCode );
227 0 : return (aIt == maOoxErrCodes.end()) ? BIFF_ERR_NA : aIt->second;
228 : }
229 :
230 154 : void UnitConverter::addErrorCode( sal_uInt8 nErrorCode, const OUString& rErrorCode )
231 : {
232 154 : maOoxErrCodes[ rErrorCode ] = nErrorCode;
233 154 : }
234 :
235 1346 : double UnitConverter::getCoefficient( Unit eUnit ) const
236 : {
237 : OSL_ENSURE( static_cast< size_t >( eUnit ) < UNIT_ENUM_SIZE, "UnitConverter::getCoefficient - invalid unit" );
238 1346 : return maCoeffs[ static_cast< size_t >( eUnit ) ];
239 : }
240 :
241 : // ============================================================================
242 :
243 : } // namespace xls
244 24 : } // namespace oox
245 :
246 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|