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