1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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>
33 using namespace ::oox::drawingml
;
37 /** Converts the passed 32-bit integer value from 1/100 mm to EMUs. */
38 sal_Int64
lclHmmToEmu( sal_Int32 nValue
)
40 return (nValue
< 0) ? -1 : convertHmmToEmu( nValue
);
43 /** Converts the passed 64-bit integer value from EMUs to 1/100 mm. */
44 sal_Int32
lclEmuToHmm( sal_Int64 nValue
)
46 return (nValue
< 0) ? -1 : convertEmuToHmm( nValue
);
51 CellAnchorModel::CellAnchorModel() :
59 AnchorClientDataModel::AnchorClientDataModel() :
60 mbLocksWithSheet( true ),
61 mbPrintsWithSheet( true )
65 ShapeAnchor::ShapeAnchor( const WorksheetHelper
& rHelper
) :
66 WorksheetHelper( rHelper
),
67 meAnchorType( ANCHOR_INVALID
),
68 meCellAnchorType( CellAnchorType::Emu
),
69 meEditAs( ANCHOR_TWOCELL
)
73 void ShapeAnchor::importAnchor( sal_Int32 nElement
, const AttributeList
& rAttribs
)
77 case XDR_TOKEN( absoluteAnchor
):
78 meAnchorType
= ANCHOR_ABSOLUTE
;
80 case XDR_TOKEN( oneCellAnchor
):
81 meAnchorType
= ANCHOR_ONECELL
;
83 case XDR_TOKEN( twoCellAnchor
):
85 meAnchorType
= ANCHOR_TWOCELL
;
86 OUString sEditAs
= rAttribs
.getXString( XML_editAs
, OUString() );
87 if ( !sEditAs
.isEmpty() )
89 if ( sEditAs
.equalsIgnoreAsciiCase("absolute" ) )
90 meEditAs
= ANCHOR_ABSOLUTE
;
91 else if ( sEditAs
.equalsIgnoreAsciiCase("oneCell") )
92 meEditAs
= ANCHOR_ONECELL
;
93 else if (sEditAs
.equalsIgnoreAsciiCase("twoCell") )
94 meEditAs
= ANCHOR_TWOCELL
;
99 OSL_ENSURE( false, "ShapeAnchor::importAnchor - unexpected element" );
101 meCellAnchorType
= CellAnchorType::Emu
;
104 void ShapeAnchor::importPos( const AttributeList
& rAttribs
)
106 OSL_ENSURE( meAnchorType
== ANCHOR_ABSOLUTE
, "ShapeAnchor::importPos - unexpected 'xdr:pos' element" );
107 maPos
.X
= rAttribs
.getHyper( XML_x
, 0 );
108 maPos
.Y
= rAttribs
.getHyper( XML_y
, 0 );
111 void ShapeAnchor::importExt( const AttributeList
& rAttribs
)
113 OSL_ENSURE( (meAnchorType
== ANCHOR_ABSOLUTE
) || (meAnchorType
== ANCHOR_ONECELL
), "ShapeAnchor::importExt - unexpected 'xdr:ext' element" );
114 maSize
.Width
= rAttribs
.getHyper( XML_cx
, 0 );
115 maSize
.Height
= rAttribs
.getHyper( XML_cy
, 0 );
118 void ShapeAnchor::importClientData( const AttributeList
& rAttribs
)
120 maClientData
.mbLocksWithSheet
= rAttribs
.getBool( XML_fLocksWithSheet
, true );
121 maClientData
.mbPrintsWithSheet
= rAttribs
.getBool( XML_fPrintsWithSheet
, true );
124 void ShapeAnchor::setCellPos( sal_Int32 nElement
, sal_Int32 nParentContext
, const OUString
& rValue
)
126 CellAnchorModel
* pCellAnchor
= nullptr;
127 switch( nParentContext
)
129 case XDR_TOKEN( from
):
130 OSL_ENSURE( (meAnchorType
== ANCHOR_ONECELL
) || (meAnchorType
== ANCHOR_TWOCELL
), "ShapeAnchor::setCellPos - unexpected 'xdr:from' element" );
131 pCellAnchor
= &maFrom
;
133 case XDR_TOKEN( to
):
134 OSL_ENSURE( meAnchorType
== ANCHOR_TWOCELL
, "ShapeAnchor::setCellPos - unexpected 'xdr:to' element" );
138 OSL_ENSURE( false, "ShapeAnchor::setCellPos - unexpected parent element" );
140 if( pCellAnchor
) switch( nElement
)
142 case XDR_TOKEN( col
): pCellAnchor
->mnCol
= rValue
.toInt32(); break;
143 case XDR_TOKEN( row
): pCellAnchor
->mnRow
= rValue
.toInt32(); break;
144 case XDR_TOKEN( colOff
): pCellAnchor
->mnColOffset
= rValue
.toInt64(); break;
145 case XDR_TOKEN( rowOff
): pCellAnchor
->mnRowOffset
= rValue
.toInt64(); break;
146 default: OSL_ENSURE( false, "ShapeAnchor::setCellPos - unexpected element" );
150 void ShapeAnchor::importVmlAnchor( const OUString
& rAnchor
)
152 meAnchorType
= ANCHOR_VML
;
153 meCellAnchorType
= CellAnchorType::Pixel
;
155 sal_Int32 nValues
[8];
158 for(sal_Int32 nIndex
{ 0 }; nIndex
>=0;)
160 nValues
[nI
] = rAnchor
.getToken( 0, ',', nIndex
).toInt32();
163 maFrom
.mnCol
= nValues
[0];
164 maFrom
.mnColOffset
= nValues
[1];
165 maFrom
.mnRow
= nValues
[2];
166 maFrom
.mnRowOffset
= nValues
[3];
167 maTo
.mnCol
= nValues
[4];
168 maTo
.mnColOffset
= nValues
[5];
169 maTo
.mnRow
= nValues
[6];
170 maTo
.mnRowOffset
= nValues
[7];
175 OSL_FAIL("ShapeAnchor::importVmlAnchor - missing anchor tokens" );
178 bool ShapeAnchor::isAnchorValid() const
180 // Checks whether the shape is visible based on the anchor
181 // if From and To anchor has the same attribute values, the shape will not have width and height
182 // and thus we can assume that such kind of shape will be not be visible
183 return !(meAnchorType
== ANCHOR_TWOCELL
&&
184 (maTo
.mnRow
== maFrom
.mnRow
&& maTo
.mnCol
== maFrom
.mnCol
) &&
185 (maTo
.mnColOffset
== maFrom
.mnColOffset
&& maTo
.mnRowOffset
== maFrom
.mnRowOffset
));
188 EmuRectangle
ShapeAnchor::calcAnchorRectEmu( const css::awt::Size
& rPageSizeHmm
) const
190 AddressConverter
& rAddrConv
= getAddressConverter();
191 EmuSize
aPageSize( lclHmmToEmu( rPageSizeHmm
.Width
), lclHmmToEmu( rPageSizeHmm
.Height
) );
192 EmuRectangle
aAnchorRect( -1, -1, -1, -1 );
194 // calculate shape position
195 switch( meAnchorType
)
197 case ANCHOR_ABSOLUTE
:
198 OSL_ENSURE( maPos
.isValid(), "ShapeAnchor::calcAnchorRectEmu - invalid position" );
199 if( maPos
.isValid() && (maPos
.X
< aPageSize
.Width
) && (maPos
.Y
< aPageSize
.Height
) )
200 aAnchorRect
.setPos( maPos
);
205 OSL_ENSURE( maFrom
.isValid(), "ShapeAnchor::calcAnchorRectEmu - invalid position" );
206 if( maFrom
.isValid() && rAddrConv
.checkCol( maFrom
.mnCol
, true ) && rAddrConv
.checkRow( maFrom
.mnRow
, true ) )
208 EmuPoint aPoint
= calcCellAnchorEmu( maFrom
);
209 if( (aPoint
.X
< aPageSize
.Width
) && (aPoint
.Y
< aPageSize
.Height
) )
210 aAnchorRect
.setPos( aPoint
);
214 OSL_ENSURE( false, "ShapeAnchor::calcAnchorRectEmu - invalid anchor" );
218 // calculate shape size
219 if( (aAnchorRect
.X
>= 0) && (aAnchorRect
.Y
>= 0) ) switch( meAnchorType
)
221 case ANCHOR_ABSOLUTE
:
223 OSL_ENSURE( maSize
.isValid(), "ShapeAnchor::calcAnchorRectEmu - invalid size" );
224 if( maSize
.isValid() )
226 aAnchorRect
.Width
= ::std::min
< sal_Int64
>( maSize
.Width
, aPageSize
.Width
- aAnchorRect
.X
);
227 aAnchorRect
.Height
= ::std::min
< sal_Int64
>( maSize
.Height
, aPageSize
.Height
- aAnchorRect
.Y
);
232 OSL_ENSURE( maTo
.isValid(), "ShapeAnchor::calcAnchorRectEmu - invalid position" );
235 /* Pass a valid cell address to calcCellAnchorEmu(), otherwise
236 nothing useful is returned, even if either row or column is valid. */
237 ScAddress aToCell
= rAddrConv
.createValidCellAddress( BinAddress( maTo
.mnCol
, maTo
.mnRow
), getSheetIndex(), true );
238 CellAnchorModel aValidTo
= maTo
;
239 aValidTo
.mnCol
= aToCell
.Col();
240 aValidTo
.mnRow
= aToCell
.Row();
241 EmuPoint aPoint
= calcCellAnchorEmu( aValidTo
);
242 // width (if column index is valid, use the calculated offset, otherwise stretch to maximum available X position)
243 aAnchorRect
.Width
= aPageSize
.Width
- aAnchorRect
.X
;
244 if( aToCell
.Col() == maTo
.mnCol
)
245 aAnchorRect
.Width
= ::std::min
< sal_Int64
>( aPoint
.X
- aAnchorRect
.X
+ 1, aAnchorRect
.Width
);
246 // height (if row index is valid, use the calculated offset, otherwise stretch to maximum available Y position)
247 aAnchorRect
.Height
= aPageSize
.Height
- aAnchorRect
.Y
;
248 if( aToCell
.Row() == maTo
.mnRow
)
249 aAnchorRect
.Height
= ::std::min
< sal_Int64
>( aPoint
.Y
- aAnchorRect
.Y
+ 1, aAnchorRect
.Height
);
259 css::awt::Rectangle
ShapeAnchor::calcAnchorRectHmm( const css::awt::Size
& rPageSizeHmm
) const
261 EmuRectangle aAnchorRect
= calcAnchorRectEmu( rPageSizeHmm
);
262 return css::awt::Rectangle( lclEmuToHmm( aAnchorRect
.X
), lclEmuToHmm( aAnchorRect
.Y
), lclEmuToHmm( aAnchorRect
.Width
), lclEmuToHmm( aAnchorRect
.Height
) );
265 EmuPoint
ShapeAnchor::calcCellAnchorEmu( const CellAnchorModel
& rModel
) const
267 // calculate position of top-left edge of the cell
268 css::awt::Point aPoint
= getCellPosition( rModel
.mnCol
, rModel
.mnRow
);
269 EmuPoint
aEmuPoint( lclHmmToEmu( aPoint
.X
), lclHmmToEmu( aPoint
.Y
) );
271 // add the offset inside the cell
272 switch( meCellAnchorType
)
274 case CellAnchorType::Emu
:
275 aEmuPoint
.X
+= rModel
.mnColOffset
;
276 aEmuPoint
.Y
+= rModel
.mnRowOffset
;
279 case CellAnchorType::Pixel
:
281 const UnitConverter
& rUnitConv
= getUnitConverter();
282 aEmuPoint
.X
+= static_cast< sal_Int64
>( rUnitConv
.scaleValue( static_cast< double >( rModel
.mnColOffset
), Unit::ScreenX
, Unit::Emu
) );
283 aEmuPoint
.Y
+= static_cast< sal_Int64
>( rUnitConv
.scaleValue( static_cast< double >( rModel
.mnRowOffset
), Unit::ScreenY
, Unit::Emu
) );
294 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */