22 #include <com/sun/star/awt/DeviceInfo.hpp>
23 #include <com/sun/star/awt/XDevice.hpp>
24 #include <com/sun/star/awt/XFont.hpp>
25 #include <com/sun/star/beans/XPropertySet.hpp>
26 #include <com/sun/star/sheet/XSpreadsheetDocument.hpp>
27 #include <com/sun/star/util/Date.hpp>
28 #include <com/sun/star/util/DateTime.hpp>
30 #include <osl/diagnose.h>
33 #include <oox/token/properties.hxx>
48 bool lclIsLeapYear( sal_Int32 nYear )
50 return ((nYear % 4) == 0) && (((nYear % 100) != 0) || ((nYear % 400) == 0));
53 void lclSkipYearBlock( sal_Int32& ornDays, sal_Int16& ornYear, sal_Int32 nDaysInBlock, sal_Int32 nYearsPerBlock, sal_Int32 nMaxBlocks )
55 sal_Int32 nBlocks = ::std::min< sal_Int32 >( ornDays / nDaysInBlock, nMaxBlocks );
56 ornYear =
static_cast< sal_Int16
>( ornYear + nYearsPerBlock * nBlocks );
57 ornDays -= nBlocks * nDaysInBlock;
62 sal_Int32 lclGetDays(
const util::Date& rDate )
65 sal_Int32 nDays = rDate.Year * 365 + ((rDate.Year + 3) / 4) - ((rDate.Year + 99) / 100) + ((rDate.Year + 399) / 400);
66 OSL_ENSURE( (1 <= rDate.Month) && (rDate.Month <= 12),
"lclGetDays - invalid month" );
67 OSL_ENSURE( (1 <= rDate.Day) && (rDate.Day <= 31),
"lclGetDays - invalid day" );
68 if( (1 <= rDate.Month) && (rDate.Month <= 12) )
71 static const sal_Int32 spnCumDays[] = { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334 };
73 nDays += spnCumDays[ rDate.Month - 1 ];
80 if( (rDate.Month < 3) || !lclIsLeapYear( rDate.Year ) )
90 mnNullDate( lclGetDays(
util::
Date( 30, 12, 1899 ) ) )
114 Reference< XDevice > xDevice( aDocProps.
getAnyProperty( PROP_ReferenceDevice ), UNO_QUERY );
125 Reference< XFont > xFont = xDevice->getFont( aDesc );
130 sal_Int64 nDigitWidth = 0;
131 for(
sal_Unicode cChar =
'0'; cChar <=
'9'; ++cChar )
132 nDigitWidth = ::std::max(nDigitWidth,
o3tl::convert(xFont->getCharWidth(cChar),
134 if( nDigitWidth > 0 )
137 sal_Int64 nSpaceWidth
139 if( nSpaceWidth > 0 )
158 sal_Int32 nDays = lclGetDays( util::Date( rDateTime.Day, rDateTime.Month, rDateTime.Year ) ) -
mnNullDate;
159 OSL_ENSURE( nDays >= 0,
"UnitConverter::calcDateTimeSerial - invalid date" );
160 OSL_ENSURE( (rDateTime.Hours <= 23) && (rDateTime.Minutes <= 59) && (rDateTime.Seconds <= 59),
"UnitConverter::calcDateTimeSerial - invalid time" );
161 return nDays + rDateTime.Hours / 24.0 + rDateTime.Minutes / 1440.0 + rDateTime.Seconds / 86400.0;
166 util::DateTime aDateTime( 0, 0, 0, 0, 1, 1, 0,
false );
168 double fTime = modf( fSerial, &fDays );
171 sal_Int32 nDays = getLimitedValue< sal_Int32, double >( fDays +
mnNullDate, 0, 3652424 );
173 if( nDays >= 366 ) { ++aDateTime.Year; nDays -= 366; }
175 lclSkipYearBlock( nDays, aDateTime.Year, 400 * 365 + 97, 400, 24 );
176 lclSkipYearBlock( nDays, aDateTime.Year, 100 * 365 + 24, 100, 3 );
177 lclSkipYearBlock( nDays, aDateTime.Year, 4 * 365 + 1, 4, 24 );
178 lclSkipYearBlock( nDays, aDateTime.Year, 365, 1, 3 );
180 static const sal_Int32 spnDaysInMonth[] = { 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
181 if( (nDays >= 59) && !lclIsLeapYear( aDateTime.Year ) ) ++nDays;
182 const sal_Int32* pnDaysInMonth = spnDaysInMonth;
183 while( *pnDaysInMonth >= nDays ) { ++aDateTime.Month; nDays -= *pnDaysInMonth; ++pnDaysInMonth; }
184 aDateTime.Day =
static_cast< sal_uInt16
>( nDays + 1 );
187 sal_Int32 nTime = getLimitedValue< sal_Int32, double >( fTime * 86400, 0, 86399 );
188 aDateTime.Seconds =
static_cast< sal_uInt16
>( nTime % 60 );
190 aDateTime.Minutes =
static_cast< sal_uInt16
>( nTime % 60 );
191 aDateTime.Hours =
static_cast< sal_uInt16
>( nTime / 60 );
207 if (aIt->second == nErrorCode)
213 return iFail !=
maOoxErrCodes.end() ? iFail->first : OUString();
exports com.sun.star.lib. util
Helper class to provide access to global workbook data.
double scaleValue(double fValue, Unit eFromUnit, Unit eToUnit) const
Converts the passed value between the passed units.
const sal_uInt8 BIFF_ERR_VALUE
const sal_uInt8 BIFF_ERR_NA
const css::awt::DeviceInfo & getDeviceInfo() const
const sal_uInt8 BIFF_ERR_REF
constexpr Point convert(const Point &rPoint, o3tl::Length eFrom, o3tl::Length eTo)
css::uno::Any getAnyProperty(sal_Int32 nPropId) const
const sal_uInt8 BIFF_ERR_NAME
StylesBuffer & getStyles() const
Returns all cell formatting objects read from the styles substream.
GraphicHelper & getGraphicHelper() const
double calcSerialFromDateTime(const css::util::DateTime &rDateTime) const
Returns the serial value of the passed datetime, based on current nulldate.
css::util::DateTime calcDateTimeFromSerial(double fSerial) const
Returns the datetime of the passed serial value, based on current nulldate.
::oox::core::FilterBase & getBaseFilter() const
Returns the base filter object (base class of all filters).
std::map< OUString, sal_uInt8 > maOoxErrCodes
Coefficients for unit conversion.
OUString calcErrorString(sal_uInt8 nErrorCode) const
Returns an error string from the passed BIFF error code.
void finalizeNullDate(const css::util::Date &rNullDate)
Updates internal nulldate for date/serial conversion.
Unit
Units supported by the UnitConverter class.
const css::uno::Reference< css::sheet::XSpreadsheetDocument > & getDocument() const
Returns a reference to the source/target spreadsheet document model.
void addErrorCode(sal_uInt8 nErrorCode, const OUString &rErrorCode)
Adds an error code to the internal maps.
const sal_uInt8 BIFF_ERR_DIV0
const sal_uInt8 BIFF_ERR_NUM
Digit width of document default font.
const css::awt::FontDescriptor & getFontDescriptor() const
Returns an API font descriptor with own font information.
void finalizeImport()
Final processing after import of all style settings.
Horizontal screen pixels.
English Metric Unit (1/360,000 cm).
UnitConverter(const WorkbookHelper &rHelper)
const sal_uInt8 BIFF_ERR_NULL
Common object settings.
sal_uInt8 calcBiffErrorCode(const OUString &rErrorCode) const
Returns a BIFF error code from the passed error string.
o3tl::enumarray< Unit, double > maCoeffs
sal_Int32 mnNullDate
Maps error code strings to BIFF error constants.
FontRef getDefaultFont() const
Returns the default application font (used in the "Normal" cell style).
double getCoefficient(Unit eUnit) const
Returns the conversion coefficient for the passed unit.