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