nss: upgrade to release 3.73
[LibreOffice.git] / sc / source / filter / oox / drawingbase.cxx
blob938f3fa67fcc416831818d2b5e1d38dbe52fd251
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>
21 #include <addressconverter.hxx>
23 #include <com/sun/star/awt/Rectangle.hpp>
24 #include <osl/diagnose.h>
25 #include <oox/helper/attributelist.hxx>
26 #include <unitconverter.hxx>
27 #include <oox/token/namespaces.hxx>
28 #include <oox/token/tokens.hxx>
30 namespace oox::xls {
32 using namespace ::oox::drawingml;
34 namespace {
36 /** Converts the passed 32-bit integer value from 1/100 mm to EMUs. */
37 sal_Int64 lclHmmToEmu( sal_Int32 nValue )
39 return (nValue < 0) ? -1 : convertHmmToEmu( nValue );
42 /** Converts the passed 64-bit integer value from EMUs to 1/100 mm. */
43 sal_Int32 lclEmuToHmm( sal_Int64 nValue )
45 return (nValue < 0) ? -1 : convertEmuToHmm( nValue );
48 } // namespace
50 CellAnchorModel::CellAnchorModel() :
51 mnCol( -1 ),
52 mnRow( -1 ),
53 mnColOffset( 0 ),
54 mnRowOffset( 0 )
58 AnchorClientDataModel::AnchorClientDataModel() :
59 mbLocksWithSheet( true ),
60 mbPrintsWithSheet( true )
64 ShapeAnchor::ShapeAnchor( const WorksheetHelper& rHelper ) :
65 WorksheetHelper( rHelper ),
66 meAnchorType( ANCHOR_INVALID ),
67 meCellAnchorType( CellAnchorType::Emu ),
68 meEditAs( ANCHOR_TWOCELL )
72 void ShapeAnchor::importAnchor( sal_Int32 nElement, const AttributeList& rAttribs )
74 switch( nElement )
76 case XDR_TOKEN( absoluteAnchor ):
77 meAnchorType = ANCHOR_ABSOLUTE;
78 meEditAs = ANCHOR_ABSOLUTE;
79 break;
80 case XDR_TOKEN( oneCellAnchor ):
81 meAnchorType = ANCHOR_ONECELL;
82 meEditAs = ANCHOR_ONECELL;
83 break;
84 case XDR_TOKEN( twoCellAnchor ):
86 meAnchorType = ANCHOR_TWOCELL;
87 OUString sEditAs = rAttribs.getXString( XML_editAs, OUString() );
88 if ( !sEditAs.isEmpty() )
90 if ( sEditAs.equalsIgnoreAsciiCase("absolute" ) )
91 meEditAs = ANCHOR_ABSOLUTE;
92 else if ( sEditAs.equalsIgnoreAsciiCase("oneCell") )
93 meEditAs = ANCHOR_ONECELL;
94 else if (sEditAs.equalsIgnoreAsciiCase("twoCell") )
95 meEditAs = ANCHOR_TWOCELL;
98 break;
99 default:
100 OSL_ENSURE( false, "ShapeAnchor::importAnchor - unexpected element" );
102 meCellAnchorType = CellAnchorType::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 = CellAnchorType::Pixel;
156 sal_Int32 nValues[8];
157 sal_Int32 nI{ 0 };
159 for(sal_Int32 nIndex{ 0 }; nIndex>=0;)
161 nValues[nI] = rAnchor.getToken( 0, ',', nIndex ).toInt32();
162 if (++nI==8)
164 maFrom.mnCol = nValues[0];
165 maFrom.mnColOffset = nValues[1];
166 maFrom.mnRow = nValues[2];
167 maFrom.mnRowOffset = nValues[3];
168 maTo.mnCol = nValues[4];
169 maTo.mnColOffset = nValues[5];
170 maTo.mnRow = nValues[6];
171 maTo.mnRowOffset = nValues[7];
172 return;
176 OSL_FAIL("ShapeAnchor::importVmlAnchor - missing anchor tokens" );
179 bool ShapeAnchor::isAnchorValid() const
181 // Checks whether the shape is visible based on the anchor
182 // if From and To anchor has the same attribute values, the shape will not have width and height
183 // and thus we can assume that such kind of shape will be not be visible
184 return !(meAnchorType == ANCHOR_TWOCELL &&
185 (maTo.mnRow == maFrom.mnRow && maTo.mnCol == maFrom.mnCol) &&
186 (maTo.mnColOffset == maFrom.mnColOffset && maTo.mnRowOffset == maFrom.mnRowOffset));
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 CellAnchorType::Emu:
276 aEmuPoint.X += rModel.mnColOffset;
277 aEmuPoint.Y += rModel.mnRowOffset;
278 break;
280 case CellAnchorType::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;
289 return aEmuPoint;
292 } // namespace oox::xls
294 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */