1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #include "unitconverter.hxx"
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/beans/XPropertySet.hpp>
27 #include <com/sun/star/util/Date.hpp>
28 #include <com/sun/star/util/DateTime.hpp>
29 #include <osl/diagnose.h>
30 #include <rtl/math.hxx>
31 #include <oox/core/filterbase.hxx>
32 #include <oox/helper/propertyset.hxx>
33 #include <oox/token/properties.hxx>
34 #include "stylesbuffer.hxx"
39 using namespace ::com::sun::star
;
40 using namespace ::com::sun::star::awt
;
41 using namespace ::com::sun::star::uno
;
45 const double MM100_PER_INCH
= 2540.0;
46 const double MM100_PER_POINT
= MM100_PER_INCH
/ 72.0;
47 const double MM100_PER_TWIP
= MM100_PER_POINT
/ 20.0;
48 const double MM100_PER_EMU
= 1.0 / 360.0;
50 /** Returns true, if the passed year is a leap year. */
51 inline bool lclIsLeapYear( sal_Int32 nYear
)
53 return ((nYear
% 4) == 0) && (((nYear
% 100) != 0) || ((nYear
% 400) == 0));
56 void lclSkipYearBlock( sal_Int32
& ornDays
, sal_Int16
& ornYear
, sal_Int32 nDaysInBlock
, sal_Int32 nYearsPerBlock
, sal_Int32 nMaxBlocks
)
58 sal_Int32 nBlocks
= ::std::min
< sal_Int32
>( ornDays
/ nDaysInBlock
, nMaxBlocks
);
59 ornYear
= static_cast< sal_Int16
>( ornYear
+ nYearsPerBlock
* nBlocks
);
60 ornDays
-= nBlocks
* nDaysInBlock
;
63 /** Returns the number of days before the passed date, starting from the null
64 date 0000-Jan-01, using standard leap year conventions. */
65 sal_Int32
lclGetDays( const util::Date
& rDate
)
67 // number of days in all full years before passed date including all leap days
68 sal_Int32 nDays
= rDate
.Year
* 365 + ((rDate
.Year
+ 3) / 4) - ((rDate
.Year
+ 99) / 100) + ((rDate
.Year
+ 399) / 400);
69 OSL_ENSURE( (1 <= rDate
.Month
) && (rDate
.Month
<= 12), "lclGetDays - invalid month" );
70 OSL_ENSURE( (1 <= rDate
.Day
) && (rDate
.Day
<= 31), "lclGetDays - invalid day" ); // yes, this is weak...
71 if( (1 <= rDate
.Month
) && (rDate
.Month
<= 12) )
73 // number of days at start of month jan feb mar apr may jun jul aug sep oct nov dec
74 static const sal_Int32 spnCumDays
[] = { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334 };
75 // add number of days in full months before passed date
76 nDays
+= spnCumDays
[ rDate
.Month
- 1 ];
77 // add number of days from passed date (this adds one day too much)
79 /* Remove the one day added too much if there is no leap day before
80 the passed day in the passed year. This means: remove the day, if
81 we are in january or february (leap day not reached if existing),
82 or if the passed year is not a leap year. */
83 if( (rDate
.Month
< 3) || !lclIsLeapYear( rDate
.Year
) )
91 UnitConverter::UnitConverter( const WorkbookHelper
& rHelper
) :
92 WorkbookHelper( rHelper
),
93 maCoeffs( UNIT_ENUM_SIZE
, 1.0 ),
94 mnNullDate( lclGetDays( util::Date( 30, 12, 1899 ) ) )
96 // initialize constant and default coefficients
97 const DeviceInfo
& rDeviceInfo
= getBaseFilter().getGraphicHelper().getDeviceInfo();
98 maCoeffs
[ UNIT_INCH
] = MM100_PER_INCH
;
99 maCoeffs
[ UNIT_POINT
] = MM100_PER_POINT
;
100 maCoeffs
[ UNIT_TWIP
] = MM100_PER_TWIP
;
101 maCoeffs
[ UNIT_EMU
] = MM100_PER_EMU
;
102 maCoeffs
[ UNIT_SCREENX
] = (rDeviceInfo
.PixelPerMeterX
> 0) ? (100000.0 / rDeviceInfo
.PixelPerMeterX
) : 50.0;
103 maCoeffs
[ UNIT_SCREENY
] = (rDeviceInfo
.PixelPerMeterY
> 0) ? (100000.0 / rDeviceInfo
.PixelPerMeterY
) : 50.0;
104 maCoeffs
[ UNIT_REFDEVX
] = 12.5; // default: 1 px = 0.125 mm
105 maCoeffs
[ UNIT_REFDEVY
] = 12.5; // default: 1 px = 0.125 mm
106 maCoeffs
[ UNIT_DIGIT
] = 200.0; // default: 1 digit = 2 mm
107 maCoeffs
[ UNIT_SPACE
] = 100.0; // default 1 space = 1 mm
110 addErrorCode( BIFF_ERR_NULL
, "#NULL!" );
111 addErrorCode( BIFF_ERR_DIV0
, "#DIV/0!" );
112 addErrorCode( BIFF_ERR_VALUE
, "#VALUE!" );
113 addErrorCode( BIFF_ERR_REF
, "#REF!" );
114 addErrorCode( BIFF_ERR_NAME
, "#NAME?" );
115 addErrorCode( BIFF_ERR_NUM
, "#NUM!" );
116 addErrorCode( BIFF_ERR_NA
, "#N/A" );
119 void UnitConverter::finalizeImport()
121 PropertySet
aDocProps( getDocument() );
122 Reference
< XDevice
> xDevice( aDocProps
.getAnyProperty( PROP_ReferenceDevice
), UNO_QUERY
);
125 // get reference device metric first, needed to get character widths below
126 DeviceInfo aInfo
= xDevice
->getInfo();
127 maCoeffs
[ UNIT_REFDEVX
] = 100000.0 / aInfo
.PixelPerMeterX
;
128 maCoeffs
[ UNIT_REFDEVY
] = 100000.0 / aInfo
.PixelPerMeterY
;
130 // get character widths from default font
131 if( const oox::xls::Font
* pDefFont
= getStyles().getDefaultFont().get() )
133 // XDevice expects pixels in font descriptor, but font contains twips
134 FontDescriptor aDesc
= pDefFont
->getFontDescriptor();
135 Reference
< XFont
> xFont
= xDevice
->getFont( aDesc
);
138 // get maximum width of all digits
139 sal_Int32 nDigitWidth
= 0;
140 for( sal_Unicode cChar
= '0'; cChar
<= '9'; ++cChar
)
141 nDigitWidth
= ::std::max( nDigitWidth
, scaleToMm100( xFont
->getCharWidth( cChar
), UNIT_TWIP
) );
142 if( nDigitWidth
> 0 )
143 maCoeffs
[ UNIT_DIGIT
] = nDigitWidth
;
144 // get width of space character
145 sal_Int32 nSpaceWidth
= scaleToMm100( xFont
->getCharWidth( ' ' ), UNIT_TWIP
);
146 if( nSpaceWidth
> 0 )
147 maCoeffs
[ UNIT_SPACE
] = nSpaceWidth
;
153 void UnitConverter::finalizeNullDate( const util::Date
& rNullDate
)
155 // convert the nulldate to number of days since 0000-Jan-01
156 mnNullDate
= lclGetDays( rNullDate
);
159 // conversion -----------------------------------------------------------------
161 double UnitConverter::scaleValue( double fValue
, Unit eFromUnit
, Unit eToUnit
) const
163 return (eFromUnit
== eToUnit
) ? fValue
: (fValue
* getCoefficient( eFromUnit
) / getCoefficient( eToUnit
));
166 sal_Int32
UnitConverter::scaleToMm100( double fValue
, Unit eUnit
) const
168 return static_cast< sal_Int32
>( fValue
* getCoefficient( eUnit
) + 0.5 );
171 double UnitConverter::scaleFromMm100( sal_Int32 nMm100
, Unit eUnit
) const
173 return static_cast< double >( nMm100
) / getCoefficient( eUnit
);
176 double UnitConverter::calcSerialFromDateTime( const util::DateTime
& rDateTime
) const
178 sal_Int32 nDays
= lclGetDays( util::Date( rDateTime
.Day
, rDateTime
.Month
, rDateTime
.Year
) ) - mnNullDate
;
179 OSL_ENSURE( nDays
>= 0, "UnitConverter::calcDateTimeSerial - invalid date" );
180 OSL_ENSURE( (rDateTime
.Hours
<= 23) && (rDateTime
.Minutes
<= 59) && (rDateTime
.Seconds
<= 59), "UnitConverter::calcDateTimeSerial - invalid time" );
181 return nDays
+ rDateTime
.Hours
/ 24.0 + rDateTime
.Minutes
/ 1440.0 + rDateTime
.Seconds
/ 86400.0;
184 util::DateTime
UnitConverter::calcDateTimeFromSerial( double fSerial
) const
186 util::DateTime
aDateTime( 0, 0, 0, 0, 1, 1, 0, false );
188 double fTime
= modf( fSerial
, &fDays
);
190 // calculate date from number of days with O(1) complexity
191 sal_Int32 nDays
= getLimitedValue
< sal_Int32
, double >( fDays
+ mnNullDate
, 0, 3652424 );
192 // skip year 0, assumed to be a leap year. By starting at year 1, leap years can be handled easily
193 if( nDays
>= 366 ) { ++aDateTime
.Year
; nDays
-= 366; }
194 // skip full blocks of 400, 100, 4 years, and remaining full years
195 lclSkipYearBlock( nDays
, aDateTime
.Year
, 400 * 365 + 97, 400, 24 );
196 lclSkipYearBlock( nDays
, aDateTime
.Year
, 100 * 365 + 24, 100, 3 );
197 lclSkipYearBlock( nDays
, aDateTime
.Year
, 4 * 365 + 1, 4, 24 );
198 lclSkipYearBlock( nDays
, aDateTime
.Year
, 365, 1, 3 );
199 // skip full months of current year
200 static const sal_Int32 spnDaysInMonth
[] = { 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
201 if( (nDays
>= 59) && !lclIsLeapYear( aDateTime
.Year
) ) ++nDays
;
202 const sal_Int32
* pnDaysInMonth
= spnDaysInMonth
;
203 while( *pnDaysInMonth
>= nDays
) { ++aDateTime
.Month
; nDays
-= *pnDaysInMonth
; ++pnDaysInMonth
; }
204 aDateTime
.Day
= static_cast< sal_uInt16
>( nDays
+ 1 );
206 // calculate time from fractional part of serial
207 sal_Int32 nTime
= getLimitedValue
< sal_Int32
, double >( fTime
* 86400, 0, 86399 );
208 aDateTime
.Seconds
= static_cast< sal_uInt16
>( nTime
% 60 );
210 aDateTime
.Minutes
= static_cast< sal_uInt16
>( nTime
% 60 );
211 aDateTime
.Hours
= static_cast< sal_uInt16
>( nTime
/ 60 );
216 sal_uInt8
UnitConverter::calcBiffErrorCode( const OUString
& rErrorCode
) const
218 OoxErrorCodeMap::const_iterator aIt
= maOoxErrCodes
.find( rErrorCode
);
219 return (aIt
== maOoxErrCodes
.end()) ? BIFF_ERR_NA
: aIt
->second
;
222 OUString
UnitConverter::calcErrorString( sal_uInt8 nErrorCode
) const
224 OoxErrorCodeMap::const_iterator
iFail( maOoxErrCodes
.end());
225 for (OoxErrorCodeMap::const_iterator
aIt( maOoxErrCodes
.begin()); aIt
!= maOoxErrCodes
.end(); ++aIt
)
227 if (aIt
->second
== nErrorCode
)
229 if (aIt
->second
== BIFF_ERR_NA
)
232 assert(iFail
!= maOoxErrCodes
.end()); // BIFF_ERR_NA really should be in the map..
233 return iFail
!= maOoxErrCodes
.end() ? iFail
->first
: OUString();
236 void UnitConverter::addErrorCode( sal_uInt8 nErrorCode
, const OUString
& rErrorCode
)
238 maOoxErrCodes
[ rErrorCode
] = nErrorCode
;
241 double UnitConverter::getCoefficient( Unit eUnit
) const
243 OSL_ENSURE( static_cast< size_t >( eUnit
) < UNIT_ENUM_SIZE
, "UnitConverter::getCoefficient - invalid unit" );
244 return maCoeffs
[ static_cast< size_t >( eUnit
) ];
250 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */