Version 5.2.6.1, tag libreoffice-5.2.6.1
[LibreOffice.git] / sc / source / filter / oox / drawingbase.cxx
blobeae05aac6241dfff98c0a9212b9cff4fa6a2c248
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #include "drawingbase.hxx"
22 #include <com/sun/star/awt/Rectangle.hpp>
23 #include <osl/diagnose.h>
24 #include <oox/helper/attributelist.hxx>
25 #include <oox/helper/binaryinputstream.hxx>
26 #include "unitconverter.hxx"
27 #include <oox/helper/propertyset.hxx>
28 #include <oox/token/namespaces.hxx>
29 #include <oox/token/properties.hxx>
30 #include <oox/token/tokens.hxx>
32 namespace oox {
33 namespace xls {
35 using namespace ::com::sun::star::table;
36 using namespace ::oox::drawingml;
38 namespace {
40 /** Converts the passed 32-bit integer value from 1/100 mm to EMUs. */
41 inline sal_Int64 lclHmmToEmu( sal_Int32 nValue )
43 return (nValue < 0) ? -1 : convertHmmToEmu( nValue );
46 /** Converts the passed 64-bit integer value from EMUs to 1/100 mm. */
47 inline sal_Int32 lclEmuToHmm( sal_Int64 nValue )
49 return (nValue < 0) ? -1 : convertEmuToHmm( nValue );
52 } // namespace
54 CellAnchorModel::CellAnchorModel() :
55 mnCol( -1 ),
56 mnRow( -1 ),
57 mnColOffset( 0 ),
58 mnRowOffset( 0 )
62 AnchorClientDataModel::AnchorClientDataModel() :
63 mbLocksWithSheet( true ),
64 mbPrintsWithSheet( true )
68 ShapeAnchor::ShapeAnchor( const WorksheetHelper& rHelper ) :
69 WorksheetHelper( rHelper ),
70 meAnchorType( ANCHOR_INVALID ),
71 meCellAnchorType( CELLANCHOR_EMU ),
72 meEditAs( ANCHOR_TWOCELL )
76 void ShapeAnchor::importAnchor( sal_Int32 nElement, const AttributeList& rAttribs )
78 switch( nElement )
80 case XDR_TOKEN( absoluteAnchor ):
81 meAnchorType = ANCHOR_ABSOLUTE;
82 break;
83 case XDR_TOKEN( oneCellAnchor ):
84 meAnchorType = ANCHOR_ONECELL;
85 break;
86 case XDR_TOKEN( twoCellAnchor ):
88 meAnchorType = ANCHOR_TWOCELL;
89 OUString sEditAs = rAttribs.getXString( XML_editAs, OUString() );
90 if ( !sEditAs.isEmpty() )
92 if ( sEditAs.equalsIgnoreAsciiCase("absolute" ) )
93 meEditAs = ANCHOR_ABSOLUTE;
94 else if ( sEditAs.equalsIgnoreAsciiCase("oneCell") )
95 meEditAs = ANCHOR_ONECELL;
98 break;
99 default:
100 OSL_ENSURE( false, "ShapeAnchor::importAnchor - unexpected element" );
102 meCellAnchorType = CELLANCHOR_EMU;
105 void ShapeAnchor::importPos( const AttributeList& rAttribs )
107 OSL_ENSURE( meAnchorType == ANCHOR_ABSOLUTE, "ShapeAnchor::importPos - unexpected 'xdr:pos' element" );
108 maPos.X = rAttribs.getHyper( XML_x, 0 );
109 maPos.Y = rAttribs.getHyper( XML_y, 0 );
112 void ShapeAnchor::importExt( const AttributeList& rAttribs )
114 OSL_ENSURE( (meAnchorType == ANCHOR_ABSOLUTE) || (meAnchorType == ANCHOR_ONECELL), "ShapeAnchor::importExt - unexpected 'xdr:ext' element" );
115 maSize.Width = rAttribs.getHyper( XML_cx, 0 );
116 maSize.Height = rAttribs.getHyper( XML_cy, 0 );
119 void ShapeAnchor::importClientData( const AttributeList& rAttribs )
121 maClientData.mbLocksWithSheet = rAttribs.getBool( XML_fLocksWithSheet, true );
122 maClientData.mbPrintsWithSheet = rAttribs.getBool( XML_fPrintsWithSheet, true );
125 void ShapeAnchor::setCellPos( sal_Int32 nElement, sal_Int32 nParentContext, const OUString& rValue )
127 CellAnchorModel* pCellAnchor = nullptr;
128 switch( nParentContext )
130 case XDR_TOKEN( from ):
131 OSL_ENSURE( (meAnchorType == ANCHOR_ONECELL) || (meAnchorType == ANCHOR_TWOCELL), "ShapeAnchor::setCellPos - unexpected 'xdr:from' element" );
132 pCellAnchor = &maFrom;
133 break;
134 case XDR_TOKEN( to ):
135 OSL_ENSURE( meAnchorType == ANCHOR_TWOCELL, "ShapeAnchor::setCellPos - unexpected 'xdr:to' element" );
136 pCellAnchor = &maTo;
137 break;
138 default:
139 OSL_ENSURE( false, "ShapeAnchor::setCellPos - unexpected parent element" );
141 if( pCellAnchor ) switch( nElement )
143 case XDR_TOKEN( col ): pCellAnchor->mnCol = rValue.toInt32(); break;
144 case XDR_TOKEN( row ): pCellAnchor->mnRow = rValue.toInt32(); break;
145 case XDR_TOKEN( colOff ): pCellAnchor->mnColOffset = rValue.toInt64(); break;
146 case XDR_TOKEN( rowOff ): pCellAnchor->mnRowOffset = rValue.toInt64(); break;
147 default: OSL_ENSURE( false, "ShapeAnchor::setCellPos - unexpected element" );
151 void ShapeAnchor::importVmlAnchor( const OUString& rAnchor )
153 meAnchorType = ANCHOR_VML;
154 meCellAnchorType = CELLANCHOR_PIXEL;
156 ::std::vector< OUString > aTokens;
157 sal_Int32 nIndex = 0;
158 while( nIndex >= 0 )
159 aTokens.push_back( rAnchor.getToken( 0, ',', nIndex ).trim() );
161 OSL_ENSURE( aTokens.size() >= 8, "ShapeAnchor::importVmlAnchor - missing anchor tokens" );
162 if( aTokens.size() >= 8 )
164 maFrom.mnCol = aTokens[ 0 ].toInt32();
165 maFrom.mnColOffset = aTokens[ 1 ].toInt32();
166 maFrom.mnRow = aTokens[ 2 ].toInt32();
167 maFrom.mnRowOffset = aTokens[ 3 ].toInt32();
168 maTo.mnCol = aTokens[ 4 ].toInt32();
169 maTo.mnColOffset = aTokens[ 5 ].toInt32();
170 maTo.mnRow = aTokens[ 6 ].toInt32();
171 maTo.mnRowOffset = aTokens[ 7 ].toInt32();
175 bool ShapeAnchor::isAnchorValid() const
177 // Checks whether the shape is visible based on the anchor
178 // if From and To anchor has the same attribute values, the shape will not have width and height
179 // and thus we can assume that such kind of shape will be not be visible
180 if (meAnchorType == ANCHOR_TWOCELL &&
181 (maTo.mnRow == maFrom.mnRow && maTo.mnCol == maFrom.mnCol) &&
182 (maTo.mnColOffset == maFrom.mnColOffset && maTo.mnRowOffset == maFrom.mnRowOffset))
184 return false;
186 return true;
189 EmuRectangle ShapeAnchor::calcAnchorRectEmu( const css::awt::Size& rPageSizeHmm ) const
191 AddressConverter& rAddrConv = getAddressConverter();
192 EmuSize aPageSize( lclHmmToEmu( rPageSizeHmm.Width ), lclHmmToEmu( rPageSizeHmm.Height ) );
193 EmuRectangle aAnchorRect( -1, -1, -1, -1 );
195 // calculate shape position
196 switch( meAnchorType )
198 case ANCHOR_ABSOLUTE:
199 OSL_ENSURE( maPos.isValid(), "ShapeAnchor::calcAnchorRectEmu - invalid position" );
200 if( maPos.isValid() && (maPos.X < aPageSize.Width) && (maPos.Y < aPageSize.Height) )
201 aAnchorRect.setPos( maPos );
202 break;
203 case ANCHOR_ONECELL:
204 case ANCHOR_TWOCELL:
205 case ANCHOR_VML:
206 OSL_ENSURE( maFrom.isValid(), "ShapeAnchor::calcAnchorRectEmu - invalid position" );
207 if( maFrom.isValid() && rAddrConv.checkCol( maFrom.mnCol, true ) && rAddrConv.checkRow( maFrom.mnRow, true ) )
209 EmuPoint aPoint = calcCellAnchorEmu( maFrom );
210 if( (aPoint.X < aPageSize.Width) && (aPoint.Y < aPageSize.Height) )
211 aAnchorRect.setPos( aPoint );
213 break;
214 case ANCHOR_INVALID:
215 OSL_ENSURE( false, "ShapeAnchor::calcAnchorRectEmu - invalid anchor" );
216 break;
219 // calculate shape size
220 if( (aAnchorRect.X >= 0) && (aAnchorRect.Y >= 0) ) switch( meAnchorType )
222 case ANCHOR_ABSOLUTE:
223 case ANCHOR_ONECELL:
224 OSL_ENSURE( maSize.isValid(), "ShapeAnchor::calcAnchorRectEmu - invalid size" );
225 if( maSize.isValid() )
227 aAnchorRect.Width = ::std::min< sal_Int64 >( maSize.Width, aPageSize.Width - aAnchorRect.X );
228 aAnchorRect.Height = ::std::min< sal_Int64 >( maSize.Height, aPageSize.Height - aAnchorRect.Y );
230 break;
231 case ANCHOR_TWOCELL:
232 case ANCHOR_VML:
233 OSL_ENSURE( maTo.isValid(), "ShapeAnchor::calcAnchorRectEmu - invalid position" );
234 if( maTo.isValid() )
236 /* Pass a valid cell address to calcCellAnchorEmu(), otherwise
237 nothing useful is returned, even if either row or column is valid. */
238 ScAddress aToCell = rAddrConv.createValidCellAddress( BinAddress( maTo.mnCol, maTo.mnRow ), getSheetIndex(), true );
239 CellAnchorModel aValidTo = maTo;
240 aValidTo.mnCol = aToCell.Col();
241 aValidTo.mnRow = aToCell.Row();
242 EmuPoint aPoint = calcCellAnchorEmu( aValidTo );
243 // width (if column index is valid, use the calculated offset, otherwise stretch to maximum available X position)
244 aAnchorRect.Width = aPageSize.Width - aAnchorRect.X;
245 if( aToCell.Col() == maTo.mnCol )
246 aAnchorRect.Width = ::std::min< sal_Int64 >( aPoint.X - aAnchorRect.X + 1, aAnchorRect.Width );
247 // height (if row index is valid, use the calculated offset, otherwise stretch to maximum available Y position)
248 aAnchorRect.Height = aPageSize.Height - aAnchorRect.Y;
249 if( aToCell.Row() == maTo.mnRow )
250 aAnchorRect.Height = ::std::min< sal_Int64 >( aPoint.Y - aAnchorRect.Y + 1, aAnchorRect.Height );
252 break;
253 case ANCHOR_INVALID:
254 break;
257 return aAnchorRect;
260 css::awt::Rectangle ShapeAnchor::calcAnchorRectHmm( const css::awt::Size& rPageSizeHmm ) const
262 EmuRectangle aAnchorRect = calcAnchorRectEmu( rPageSizeHmm );
263 return css::awt::Rectangle( lclEmuToHmm( aAnchorRect.X ), lclEmuToHmm( aAnchorRect.Y ), lclEmuToHmm( aAnchorRect.Width ), lclEmuToHmm( aAnchorRect.Height ) );
266 EmuPoint ShapeAnchor::calcCellAnchorEmu( const CellAnchorModel& rModel ) const
268 // calculate position of top-left edge of the cell
269 css::awt::Point aPoint = getCellPosition( rModel.mnCol, rModel.mnRow );
270 EmuPoint aEmuPoint( lclHmmToEmu( aPoint.X ), lclHmmToEmu( aPoint.Y ) );
272 // add the offset inside the cell
273 switch( meCellAnchorType )
275 case CELLANCHOR_EMU:
276 aEmuPoint.X += rModel.mnColOffset;
277 aEmuPoint.Y += rModel.mnRowOffset;
278 break;
280 case CELLANCHOR_PIXEL:
282 const UnitConverter& rUnitConv = getUnitConverter();
283 aEmuPoint.X += static_cast< sal_Int64 >( rUnitConv.scaleValue( static_cast< double >( rModel.mnColOffset ), UNIT_SCREENX, UNIT_EMU ) );
284 aEmuPoint.Y += static_cast< sal_Int64 >( rUnitConv.scaleValue( static_cast< double >( rModel.mnRowOffset ), UNIT_SCREENY, UNIT_EMU ) );
286 break;
288 case CELLANCHOR_COLROW:
290 css::awt::Size aCellSize = getCellSize( rModel.mnCol, rModel.mnRow );
291 EmuSize aEmuSize( lclHmmToEmu( aCellSize.Width ), lclHmmToEmu( aCellSize.Height ) );
292 // X offset is given in 1/1024 of column width
293 aEmuPoint.X += static_cast< sal_Int64 >( aEmuSize.Width * getLimitedValue< double >( static_cast< double >( rModel.mnColOffset ) / 1024.0, 0.0, 1.0 ) + 0.5 );
294 // Y offset is given in 1/256 of row height
295 aEmuPoint.Y += static_cast< sal_Int64 >( aEmuSize.Height * getLimitedValue< double >( static_cast< double >( rModel.mnRowOffset ) / 256.0, 0.0, 1.0 ) + 0.5 );
297 break;
300 return aEmuPoint;
303 } // namespace xls
304 } // namespace oox
306 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */