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