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