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