1*d0626817SAndrew Rist /************************************************************** 2cdf0e10cSrcweir * 3*d0626817SAndrew Rist * Licensed to the Apache Software Foundation (ASF) under one 4*d0626817SAndrew Rist * or more contributor license agreements. See the NOTICE file 5*d0626817SAndrew Rist * distributed with this work for additional information 6*d0626817SAndrew Rist * regarding copyright ownership. The ASF licenses this file 7*d0626817SAndrew Rist * to you under the Apache License, Version 2.0 (the 8*d0626817SAndrew Rist * "License"); you may not use this file except in compliance 9*d0626817SAndrew Rist * with the License. You may obtain a copy of the License at 10*d0626817SAndrew Rist * 11*d0626817SAndrew Rist * http://www.apache.org/licenses/LICENSE-2.0 12*d0626817SAndrew Rist * 13*d0626817SAndrew Rist * Unless required by applicable law or agreed to in writing, 14*d0626817SAndrew Rist * software distributed under the License is distributed on an 15*d0626817SAndrew Rist * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 16*d0626817SAndrew Rist * KIND, either express or implied. See the License for the 17*d0626817SAndrew Rist * specific language governing permissions and limitations 18*d0626817SAndrew Rist * under the License. 19*d0626817SAndrew Rist * 20*d0626817SAndrew Rist *************************************************************/ 21*d0626817SAndrew Rist 22*d0626817SAndrew Rist 23cdf0e10cSrcweir 24cdf0e10cSrcweir #include "oox/xls/drawingbase.hxx" 25cdf0e10cSrcweir 26cdf0e10cSrcweir #include <com/sun/star/awt/Rectangle.hpp> 27cdf0e10cSrcweir #include "oox/helper/attributelist.hxx" 28cdf0e10cSrcweir #include "oox/helper/binaryinputstream.hxx" 29cdf0e10cSrcweir #include "oox/xls/unitconverter.hxx" 30cdf0e10cSrcweir 31cdf0e10cSrcweir namespace oox { 32cdf0e10cSrcweir namespace xls { 33cdf0e10cSrcweir 34cdf0e10cSrcweir // ============================================================================ 35cdf0e10cSrcweir 36cdf0e10cSrcweir using namespace ::com::sun::star::awt; 37cdf0e10cSrcweir using namespace ::com::sun::star::table; 38cdf0e10cSrcweir using namespace ::oox::drawingml; 39cdf0e10cSrcweir 40cdf0e10cSrcweir using ::rtl::OUString; 41cdf0e10cSrcweir 42cdf0e10cSrcweir // ============================================================================ 43cdf0e10cSrcweir 44cdf0e10cSrcweir namespace { 45cdf0e10cSrcweir 46cdf0e10cSrcweir /** Converts the passed 32-bit integer value from 1/100 mm to EMUs. */ 47cdf0e10cSrcweir inline sal_Int64 lclHmmToEmu( sal_Int32 nValue ) 48cdf0e10cSrcweir { 49cdf0e10cSrcweir return (nValue < 0) ? -1 : convertHmmToEmu( nValue ); 50cdf0e10cSrcweir } 51cdf0e10cSrcweir 52cdf0e10cSrcweir /** Converts the passed 64-bit integer value from EMUs to 1/100 mm. */ 53cdf0e10cSrcweir inline sal_Int32 lclEmuToHmm( sal_Int64 nValue ) 54cdf0e10cSrcweir { 55cdf0e10cSrcweir return (nValue < 0) ? -1 : convertEmuToHmm( nValue ); 56cdf0e10cSrcweir } 57cdf0e10cSrcweir 58cdf0e10cSrcweir /** Reads the cell anchor model from a BIFF or DFF stream. */ 59cdf0e10cSrcweir BinaryInputStream& operator>>( BinaryInputStream& rStrm, CellAnchorModel& rModel ) 60cdf0e10cSrcweir { 61cdf0e10cSrcweir // all members are given as 16-bit unsigned values 62cdf0e10cSrcweir rModel.mnCol = rStrm.readuInt16(); 63cdf0e10cSrcweir rModel.mnColOffset = rStrm.readuInt16(); 64cdf0e10cSrcweir rModel.mnRow = rStrm.readuInt16(); 65cdf0e10cSrcweir rModel.mnRowOffset = rStrm.readuInt16(); 66cdf0e10cSrcweir return rStrm; 67cdf0e10cSrcweir } 68cdf0e10cSrcweir 69cdf0e10cSrcweir } // namespace 70cdf0e10cSrcweir 71cdf0e10cSrcweir // ============================================================================ 72cdf0e10cSrcweir 73cdf0e10cSrcweir CellAnchorModel::CellAnchorModel() : 74cdf0e10cSrcweir mnCol( -1 ), 75cdf0e10cSrcweir mnRow( -1 ), 76cdf0e10cSrcweir mnColOffset( 0 ), 77cdf0e10cSrcweir mnRowOffset( 0 ) 78cdf0e10cSrcweir { 79cdf0e10cSrcweir } 80cdf0e10cSrcweir 81cdf0e10cSrcweir // ---------------------------------------------------------------------------- 82cdf0e10cSrcweir 83cdf0e10cSrcweir AnchorClientDataModel::AnchorClientDataModel() : 84cdf0e10cSrcweir mbLocksWithSheet( true ), 85cdf0e10cSrcweir mbPrintsWithSheet( true ) 86cdf0e10cSrcweir { 87cdf0e10cSrcweir } 88cdf0e10cSrcweir 89cdf0e10cSrcweir // ============================================================================ 90cdf0e10cSrcweir 91cdf0e10cSrcweir ShapeAnchor::ShapeAnchor( const WorksheetHelper& rHelper ) : 92cdf0e10cSrcweir WorksheetHelper( rHelper ), 93cdf0e10cSrcweir meAnchorType( ANCHOR_INVALID ), 94cdf0e10cSrcweir meCellAnchorType( CELLANCHOR_EMU ), 95cdf0e10cSrcweir mnEditAs( XML_twoCell ) 96cdf0e10cSrcweir { 97cdf0e10cSrcweir } 98cdf0e10cSrcweir 99cdf0e10cSrcweir void ShapeAnchor::importAnchor( sal_Int32 nElement, const AttributeList& rAttribs ) 100cdf0e10cSrcweir { 101cdf0e10cSrcweir switch( nElement ) 102cdf0e10cSrcweir { 103cdf0e10cSrcweir case XDR_TOKEN( absoluteAnchor ): 104cdf0e10cSrcweir meAnchorType = ANCHOR_ABSOLUTE; 105cdf0e10cSrcweir break; 106cdf0e10cSrcweir case XDR_TOKEN( oneCellAnchor ): 107cdf0e10cSrcweir meAnchorType = ANCHOR_ONECELL; 108cdf0e10cSrcweir break; 109cdf0e10cSrcweir case XDR_TOKEN( twoCellAnchor ): 110cdf0e10cSrcweir meAnchorType = ANCHOR_TWOCELL; 111cdf0e10cSrcweir mnEditAs = rAttribs.getToken( XML_editAs, XML_twoCell ); 112cdf0e10cSrcweir break; 113cdf0e10cSrcweir default: 114cdf0e10cSrcweir OSL_ENSURE( false, "ShapeAnchor::importAnchor - unexpected element" ); 115cdf0e10cSrcweir } 116cdf0e10cSrcweir meCellAnchorType = CELLANCHOR_EMU; 117cdf0e10cSrcweir } 118cdf0e10cSrcweir 119cdf0e10cSrcweir void ShapeAnchor::importPos( const AttributeList& rAttribs ) 120cdf0e10cSrcweir { 121cdf0e10cSrcweir OSL_ENSURE( meAnchorType == ANCHOR_ABSOLUTE, "ShapeAnchor::importPos - unexpected 'xdr:pos' element" ); 122cdf0e10cSrcweir maPos.X = rAttribs.getHyper( XML_x, 0 ); 123cdf0e10cSrcweir maPos.Y = rAttribs.getHyper( XML_y, 0 ); 124cdf0e10cSrcweir } 125cdf0e10cSrcweir 126cdf0e10cSrcweir void ShapeAnchor::importExt( const AttributeList& rAttribs ) 127cdf0e10cSrcweir { 128cdf0e10cSrcweir OSL_ENSURE( (meAnchorType == ANCHOR_ABSOLUTE) || (meAnchorType == ANCHOR_ONECELL), "ShapeAnchor::importExt - unexpected 'xdr:ext' element" ); 129cdf0e10cSrcweir maSize.Width = rAttribs.getHyper( XML_cx, 0 ); 130cdf0e10cSrcweir maSize.Height = rAttribs.getHyper( XML_cy, 0 ); 131cdf0e10cSrcweir } 132cdf0e10cSrcweir 133cdf0e10cSrcweir void ShapeAnchor::importClientData( const AttributeList& rAttribs ) 134cdf0e10cSrcweir { 135cdf0e10cSrcweir maClientData.mbLocksWithSheet = rAttribs.getBool( XML_fLocksWithSheet, true ); 136cdf0e10cSrcweir maClientData.mbPrintsWithSheet = rAttribs.getBool( XML_fPrintsWithSheet, true ); 137cdf0e10cSrcweir } 138cdf0e10cSrcweir 139cdf0e10cSrcweir void ShapeAnchor::setCellPos( sal_Int32 nElement, sal_Int32 nParentContext, const OUString& rValue ) 140cdf0e10cSrcweir { 141cdf0e10cSrcweir CellAnchorModel* pCellAnchor = 0; 142cdf0e10cSrcweir switch( nParentContext ) 143cdf0e10cSrcweir { 144cdf0e10cSrcweir case XDR_TOKEN( from ): 145cdf0e10cSrcweir OSL_ENSURE( (meAnchorType == ANCHOR_ONECELL) || (meAnchorType == ANCHOR_TWOCELL), "ShapeAnchor::setCellPos - unexpected 'xdr:from' element" ); 146cdf0e10cSrcweir pCellAnchor = &maFrom; 147cdf0e10cSrcweir break; 148cdf0e10cSrcweir case XDR_TOKEN( to ): 149cdf0e10cSrcweir OSL_ENSURE( meAnchorType == ANCHOR_TWOCELL, "ShapeAnchor::setCellPos - unexpected 'xdr:to' element" ); 150cdf0e10cSrcweir pCellAnchor = &maTo; 151cdf0e10cSrcweir break; 152cdf0e10cSrcweir default: 153cdf0e10cSrcweir OSL_ENSURE( false, "ShapeAnchor::setCellPos - unexpected parent element" ); 154cdf0e10cSrcweir } 155cdf0e10cSrcweir if( pCellAnchor ) switch( nElement ) 156cdf0e10cSrcweir { 157cdf0e10cSrcweir case XDR_TOKEN( col ): pCellAnchor->mnCol = rValue.toInt32(); break; 158cdf0e10cSrcweir case XDR_TOKEN( row ): pCellAnchor->mnRow = rValue.toInt32(); break; 159cdf0e10cSrcweir case XDR_TOKEN( colOff ): pCellAnchor->mnColOffset = rValue.toInt64(); break; 160cdf0e10cSrcweir case XDR_TOKEN( rowOff ): pCellAnchor->mnRowOffset = rValue.toInt64(); break; 161cdf0e10cSrcweir default: OSL_ENSURE( false, "ShapeAnchor::setCellPos - unexpected element" ); 162cdf0e10cSrcweir } 163cdf0e10cSrcweir } 164cdf0e10cSrcweir 165cdf0e10cSrcweir void ShapeAnchor::importVmlAnchor( const OUString& rAnchor ) 166cdf0e10cSrcweir { 167cdf0e10cSrcweir meAnchorType = ANCHOR_TWOCELL; /// VML uses two-cell anchors only 168cdf0e10cSrcweir meCellAnchorType = CELLANCHOR_PIXEL; /// VML uses screen pixels for offset values 169cdf0e10cSrcweir 170cdf0e10cSrcweir ::std::vector< OUString > aTokens; 171cdf0e10cSrcweir sal_Int32 nIndex = 0; 172cdf0e10cSrcweir while( nIndex >= 0 ) 173cdf0e10cSrcweir aTokens.push_back( rAnchor.getToken( 0, ',', nIndex ).trim() ); 174cdf0e10cSrcweir 175cdf0e10cSrcweir OSL_ENSURE( aTokens.size() >= 8, "ShapeAnchor::importVmlAnchor - missing anchor tokens" ); 176cdf0e10cSrcweir if( aTokens.size() >= 8 ) 177cdf0e10cSrcweir { 178cdf0e10cSrcweir maFrom.mnCol = aTokens[ 0 ].toInt32(); 179cdf0e10cSrcweir maFrom.mnColOffset = aTokens[ 1 ].toInt32(); 180cdf0e10cSrcweir maFrom.mnRow = aTokens[ 2 ].toInt32(); 181cdf0e10cSrcweir maFrom.mnRowOffset = aTokens[ 3 ].toInt32(); 182cdf0e10cSrcweir maTo.mnCol = aTokens[ 4 ].toInt32(); 183cdf0e10cSrcweir maTo.mnColOffset = aTokens[ 5 ].toInt32(); 184cdf0e10cSrcweir maTo.mnRow = aTokens[ 6 ].toInt32(); 185cdf0e10cSrcweir maTo.mnRowOffset = aTokens[ 7 ].toInt32(); 186cdf0e10cSrcweir } 187cdf0e10cSrcweir } 188cdf0e10cSrcweir 189cdf0e10cSrcweir void ShapeAnchor::importBiffAnchor( BinaryInputStream& rStrm ) 190cdf0e10cSrcweir { 191cdf0e10cSrcweir meAnchorType = ANCHOR_TWOCELL; /// BIFF/DFF use two-cell anchors only 192cdf0e10cSrcweir meCellAnchorType = CELLANCHOR_COLROW; /// BIFF/DFF use fraction of column/row for offset values 193cdf0e10cSrcweir rStrm >> maFrom >> maTo; 194cdf0e10cSrcweir } 195cdf0e10cSrcweir 196cdf0e10cSrcweir EmuRectangle ShapeAnchor::calcAnchorRectEmu( const Size& rPageSizeHmm ) const 197cdf0e10cSrcweir { 198cdf0e10cSrcweir AddressConverter& rAddrConv = getAddressConverter(); 199cdf0e10cSrcweir EmuSize aPageSize( lclHmmToEmu( rPageSizeHmm.Width ), lclHmmToEmu( rPageSizeHmm.Height ) ); 200cdf0e10cSrcweir EmuRectangle aAnchorRect( -1, -1, -1, -1 ); 201cdf0e10cSrcweir 202cdf0e10cSrcweir // calculate shape position 203cdf0e10cSrcweir switch( meAnchorType ) 204cdf0e10cSrcweir { 205cdf0e10cSrcweir case ANCHOR_ABSOLUTE: 206cdf0e10cSrcweir OSL_ENSURE( maPos.isValid(), "ShapeAnchor::calcAnchorRectEmu - invalid position" ); 207cdf0e10cSrcweir if( maPos.isValid() && (maPos.X < aPageSize.Width) && (maPos.Y < aPageSize.Height) ) 208cdf0e10cSrcweir aAnchorRect.setPos( maPos ); 209cdf0e10cSrcweir break; 210cdf0e10cSrcweir case ANCHOR_ONECELL: 211cdf0e10cSrcweir case ANCHOR_TWOCELL: 212cdf0e10cSrcweir OSL_ENSURE( maFrom.isValid(), "ShapeAnchor::calcAnchorRectEmu - invalid position" ); 213cdf0e10cSrcweir if( maFrom.isValid() && rAddrConv.checkCol( maFrom.mnCol, true ) && rAddrConv.checkRow( maFrom.mnRow, true ) ) 214cdf0e10cSrcweir { 215cdf0e10cSrcweir EmuPoint aPoint = calcCellAnchorEmu( maFrom ); 216cdf0e10cSrcweir if( (aPoint.X < aPageSize.Width) && (aPoint.Y < aPageSize.Height) ) 217cdf0e10cSrcweir aAnchorRect.setPos( aPoint ); 218cdf0e10cSrcweir } 219cdf0e10cSrcweir break; 220cdf0e10cSrcweir case ANCHOR_INVALID: 221cdf0e10cSrcweir OSL_ENSURE( false, "ShapeAnchor::calcAnchorRectEmu - invalid anchor" ); 222cdf0e10cSrcweir break; 223cdf0e10cSrcweir } 224cdf0e10cSrcweir 225cdf0e10cSrcweir // calculate shape size 226cdf0e10cSrcweir if( (aAnchorRect.X >= 0) && (aAnchorRect.Y >= 0) ) switch( meAnchorType ) 227cdf0e10cSrcweir { 228cdf0e10cSrcweir case ANCHOR_ABSOLUTE: 229cdf0e10cSrcweir case ANCHOR_ONECELL: 230cdf0e10cSrcweir OSL_ENSURE( maSize.isValid(), "ShapeAnchor::calcAnchorRectEmu - invalid size" ); 231cdf0e10cSrcweir if( maSize.isValid() ) 232cdf0e10cSrcweir { 233cdf0e10cSrcweir aAnchorRect.Width = ::std::min< sal_Int64 >( maSize.Width, aPageSize.Width - aAnchorRect.X ); 234cdf0e10cSrcweir aAnchorRect.Height = ::std::min< sal_Int64 >( maSize.Height, aPageSize.Height - aAnchorRect.Y ); 235cdf0e10cSrcweir } 236cdf0e10cSrcweir break; 237cdf0e10cSrcweir case ANCHOR_TWOCELL: 238cdf0e10cSrcweir OSL_ENSURE( maTo.isValid(), "ShapeAnchor::calcAnchorRectEmu - invalid position" ); 239cdf0e10cSrcweir if( maTo.isValid() ) 240cdf0e10cSrcweir { 241cdf0e10cSrcweir /* Pass a valid cell address to calcCellAnchorEmu(), otherwise 242cdf0e10cSrcweir nothing useful is returned, even if either row or column is valid. */ 243cdf0e10cSrcweir CellAddress aToCell = rAddrConv.createValidCellAddress( BinAddress( maTo.mnCol, maTo.mnRow ), getSheetIndex(), true ); 244cdf0e10cSrcweir CellAnchorModel aValidTo = maTo; 245cdf0e10cSrcweir aValidTo.mnCol = aToCell.Column; 246cdf0e10cSrcweir aValidTo.mnRow = aToCell.Row; 247cdf0e10cSrcweir EmuPoint aPoint = calcCellAnchorEmu( aValidTo ); 248cdf0e10cSrcweir // width (if column index is valid, use the calculated offset, otherwise stretch to maximum available X position) 249cdf0e10cSrcweir aAnchorRect.Width = aPageSize.Width - aAnchorRect.X; 250cdf0e10cSrcweir if( aToCell.Column == maTo.mnCol ) 251cdf0e10cSrcweir aAnchorRect.Width = ::std::min< sal_Int64 >( aPoint.X - aAnchorRect.X + 1, aAnchorRect.Width ); 252cdf0e10cSrcweir // height (if row index is valid, use the calculated offset, otherwise stretch to maximum available Y position) 253cdf0e10cSrcweir aAnchorRect.Height = aPageSize.Height - aAnchorRect.Y; 254cdf0e10cSrcweir if( aToCell.Row == maTo.mnRow ) 255cdf0e10cSrcweir aAnchorRect.Height = ::std::min< sal_Int64 >( aPoint.Y - aAnchorRect.Y + 1, aAnchorRect.Height ); 256cdf0e10cSrcweir } 257cdf0e10cSrcweir break; 258cdf0e10cSrcweir case ANCHOR_INVALID: 259cdf0e10cSrcweir break; 260cdf0e10cSrcweir } 261cdf0e10cSrcweir 262cdf0e10cSrcweir // add 0.75 mm (27,000 EMUs) in X direction to correct display error 263cdf0e10cSrcweir if( aAnchorRect.X >= 0 ) 264cdf0e10cSrcweir aAnchorRect.X += 27000; 265cdf0e10cSrcweir // remove 0.25 mm (9,000 EMUs) in Y direction to correct display error 266cdf0e10cSrcweir if( aAnchorRect.Y >= 9000 ) 267cdf0e10cSrcweir aAnchorRect.Y -= 9000; 268cdf0e10cSrcweir 269cdf0e10cSrcweir return aAnchorRect; 270cdf0e10cSrcweir } 271cdf0e10cSrcweir 272cdf0e10cSrcweir Rectangle ShapeAnchor::calcAnchorRectHmm( const Size& rPageSizeHmm ) const 273cdf0e10cSrcweir { 274cdf0e10cSrcweir EmuRectangle aAnchorRect = calcAnchorRectEmu( rPageSizeHmm ); 275cdf0e10cSrcweir return Rectangle( lclEmuToHmm( aAnchorRect.X ), lclEmuToHmm( aAnchorRect.Y ), lclEmuToHmm( aAnchorRect.Width ), lclEmuToHmm( aAnchorRect.Height ) ); 276cdf0e10cSrcweir } 277cdf0e10cSrcweir 278cdf0e10cSrcweir // private -------------------------------------------------------------------- 279cdf0e10cSrcweir 280cdf0e10cSrcweir EmuPoint ShapeAnchor::calcCellAnchorEmu( const CellAnchorModel& rModel ) const 281cdf0e10cSrcweir { 282cdf0e10cSrcweir // calculate position of top-left edge of the cell 283cdf0e10cSrcweir Point aPoint = getCellPosition( rModel.mnCol, rModel.mnRow ); 284cdf0e10cSrcweir EmuPoint aEmuPoint( lclHmmToEmu( aPoint.X ), lclHmmToEmu( aPoint.Y ) ); 285cdf0e10cSrcweir 286cdf0e10cSrcweir // add the offset inside the cell 287cdf0e10cSrcweir switch( meCellAnchorType ) 288cdf0e10cSrcweir { 289cdf0e10cSrcweir case CELLANCHOR_EMU: 290cdf0e10cSrcweir aEmuPoint.X += rModel.mnColOffset; 291cdf0e10cSrcweir aEmuPoint.Y += rModel.mnRowOffset; 292cdf0e10cSrcweir break; 293cdf0e10cSrcweir 294cdf0e10cSrcweir case CELLANCHOR_PIXEL: 295cdf0e10cSrcweir { 296cdf0e10cSrcweir const UnitConverter& rUnitConv = getUnitConverter(); 297cdf0e10cSrcweir aEmuPoint.X += static_cast< sal_Int64 >( rUnitConv.scaleValue( static_cast< double >( rModel.mnColOffset ), UNIT_SCREENX, UNIT_EMU ) ); 298cdf0e10cSrcweir aEmuPoint.Y += static_cast< sal_Int64 >( rUnitConv.scaleValue( static_cast< double >( rModel.mnRowOffset ), UNIT_SCREENY, UNIT_EMU ) ); 299cdf0e10cSrcweir } 300cdf0e10cSrcweir break; 301cdf0e10cSrcweir 302cdf0e10cSrcweir case CELLANCHOR_COLROW: 303cdf0e10cSrcweir { 304cdf0e10cSrcweir Size aCellSize = getCellSize( rModel.mnCol, rModel.mnRow ); 305cdf0e10cSrcweir EmuSize aEmuSize( lclHmmToEmu( aCellSize.Width ), lclHmmToEmu( aCellSize.Height ) ); 306cdf0e10cSrcweir // X offset is given in 1/1024 of column width 307cdf0e10cSrcweir aEmuPoint.X += static_cast< sal_Int64 >( aEmuSize.Width * getLimitedValue< double >( static_cast< double >( rModel.mnColOffset ) / 1024.0, 0.0, 1.0 ) + 0.5 ); 308cdf0e10cSrcweir // Y offset is given in 1/256 of row height 309cdf0e10cSrcweir aEmuPoint.Y += static_cast< sal_Int64 >( aEmuSize.Height * getLimitedValue< double >( static_cast< double >( rModel.mnRowOffset ) / 256.0, 0.0, 1.0 ) + 0.5 ); 310cdf0e10cSrcweir } 311cdf0e10cSrcweir break; 312cdf0e10cSrcweir } 313cdf0e10cSrcweir 314cdf0e10cSrcweir return aEmuPoint; 315cdf0e10cSrcweir } 316cdf0e10cSrcweir 317cdf0e10cSrcweir // ============================================================================ 318cdf0e10cSrcweir 319cdf0e10cSrcweir } // namespace xls 320cdf0e10cSrcweir } // namespace oox 321