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