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