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