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