LibreOffice Module sc (master)  1
drawingbase.cxx
Go to the documentation of this file.
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>
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 
38 sal_Int64 lclHmmToEmu( sal_Int32 nValue )
39 {
40  return (nValue < 0) ? -1 : convertHmmToEmu( nValue );
41 }
42 
44 sal_Int32 lclEmuToHmm( sal_Int64 nValue )
45 {
46  return (nValue < 0) ? -1 : convertEmuToHmm( nValue );
47 }
48 
49 } // namespace
50 
52  mnCol( -1 ),
53  mnRow( -1 ),
54  mnColOffset( 0 ),
55  mnRowOffset( 0 )
56 {
57 }
58 
60  mbLocksWithSheet( true ),
61  mbPrintsWithSheet( true )
62 {
63 }
64 
66  WorksheetHelper( rHelper ),
67  meAnchorType( ANCHOR_INVALID ),
68  meCellAnchorType( CellAnchorType::Emu ),
69  meEditAs( ANCHOR_TWOCELL )
70 {
71 }
72 
73 void ShapeAnchor::importAnchor( sal_Int32 nElement, const AttributeList& rAttribs )
74 {
75  switch( nElement )
76  {
77  case XDR_TOKEN( absoluteAnchor ):
80  break;
81  case XDR_TOKEN( oneCellAnchor ):
84  break;
85  case XDR_TOKEN( twoCellAnchor ):
86  {
88  OUString sEditAs = rAttribs.getXString( XML_editAs, OUString() );
89  if ( !sEditAs.isEmpty() )
90  {
91  if ( sEditAs.equalsIgnoreAsciiCase("absolute" ) )
93  else if ( sEditAs.equalsIgnoreAsciiCase("oneCell") )
95  else if (sEditAs.equalsIgnoreAsciiCase("twoCell") )
97  }
98  }
99  break;
100  default:
101  OSL_ENSURE( false, "ShapeAnchor::importAnchor - unexpected element" );
102  }
104 }
105 
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 
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 
121 {
122  maClientData.mbLocksWithSheet = rAttribs.getBool( XML_fLocksWithSheet, true );
123  maClientData.mbPrintsWithSheet = rAttribs.getBool( XML_fPrintsWithSheet, true );
124 }
125 
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 
152 void ShapeAnchor::importVmlAnchor( std::u16string_view rAnchor )
153 {
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 
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) &&
188 }
189 
190 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 
261 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 
267 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 
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: */
static N to(double f)
Definition: xltools.cxx:275
double scaleValue(double fValue, Unit eFromUnit, Unit eToUnit) const
Converts the passed value between the passed units.
sal_Int32 nIndex
sal_Int32 convertEmuToHmm(sal_Int64 nValue)
void importVmlAnchor(std::u16string_view rAnchor)
Imports the client anchor settings from a VML element.
OptValue< bool > getBool(sal_Int32 nAttrToken) const
sal_Int64 toInt64(std::u16string_view str, sal_Int16 radix=10)
SCROW Row() const
Definition: address.hxx:274
bool checkCol(sal_Int32 nCol, bool bTrackOverflow)
Checks if the passed column index is valid.
A 2D cell address struct for binary filters.
UnitConverter & getUnitConverter() const
Returns the measurement unit converter.
OptValue< OUString > getXString(sal_Int32 nAttrToken) const
sal_Int64 mnRowOffset
X offset inside the column.
Definition: drawingbase.hxx:49
sal_Int32 mnCol
std::basic_string_view< charT, traits > getToken(std::basic_string_view< charT, traits > sv, charT delimiter, std::size_t &position)
Helper class that provides functions to convert values from and to different units.
AnchorPointModel maPos
Type of the cell anchor models.
void setCellPos(sal_Int32 nElement, sal_Int32 nParentContext, std::u16string_view rValue)
Sets an attribute of the cell-dependent anchor position from xdr:from and xdr:to elements.
UBlockCode from
AnchorClientDataModel maClientData
Bottom-right position, if anchor is of type two-cell.
AnchorSizeModel maSize
Top-left position, if anchor is of type absolute.
sal_Int64 mnColOffset
Row index.
Definition: drawingbase.hxx:48
OptValue< sal_Int64 > getHyper(sal_Int32 nAttrToken) const
::oox::drawingml::EmuRectangle calcAnchorRectEmu(const css::awt::Size &rPageSizeHmm) const
Calculates the resulting shape anchor in EMUs.
Position in spreadsheet (cell position and offset inside cell).
Definition: drawingbase.hxx:44
void importExt(const AttributeList &rAttribs)
Imports the absolute anchor size from the xdr:ext element.
sal_Int64 convertHmmToEmu(sal_Int32 nValue)
bool checkRow(sal_Int32 nRow, bool bTrackOverflow)
Checks if the passed row index is valid.
Twips (1/20 point).
Two-cell anchor (top-left and bottom-right corner at cell).
Definition: drawingbase.hxx:78
sal_Int32 toInt32(std::u16string_view str, sal_Int16 radix=10)
sal_Int32 mnRow
Column index.
Definition: drawingbase.hxx:47
AnchorType meEditAs
Shape client data.
void importClientData(const AttributeList &rAttribs)
Imports the shape client data from the xdr:clientData element.
SCCOL Col() const
Definition: address.hxx:279
ShapeAnchor(const WorksheetHelper &rHelper)
Definition: drawingbase.cxx:65
css::awt::Point getCellPosition(sal_Int32 nCol, sal_Int32 nRow) const
Returns the absolute cell position in 1/100 mm.
Converter for cell addresses and cell ranges for OOXML and BIFF filters.
ScAddress createValidCellAddress(const OUString &rString, sal_Int16 nSheet, bool bTrackOverflow)
Returns a valid cell address by moving it into allowed dimensions.
CellAnchorModel maTo
Top-left position, if anchor is not of type absolute.
Horizontal screen pixels.
English Metric Unit (1/360,000 cm).
css::awt::Rectangle calcAnchorRectHmm(const css::awt::Size &rPageSizeHmm) const
Calculates the resulting shape anchor in 1/100 mm.
bool isAnchorValid() const
Checks whether the shape is visible based on the anchor.
SCTAB getSheetIndex() const
Returns the index of the current sheet.
void importAnchor(sal_Int32 nElement, const AttributeList &rAttribs)
Imports the shape anchor (one of the elements xdr:absoluteAnchor, xdr:oneCellAnchor, xdr:twoCellAnchor).
Definition: drawingbase.cxx:73
CellAnchorType meCellAnchorType
Type of this shape anchor.
CellAnchorType
Specifies how cell positions from CellAnchorModel have to be processed.
CellAnchorModel maFrom
Anchor size, if anchor is not of type two-cell.
CellAnchorModel()
Y offset inside the row.
Definition: drawingbase.cxx:51
::oox::drawingml::EmuPoint calcCellAnchorEmu(const CellAnchorModel &rModel) const
Converts the passed anchor to an absolute position in EMUs.
One-cell anchor (top-left corner at cell, size in absolute units).
Definition: drawingbase.hxx:76
sal_Int32 mnRow
Absolute anchor (top-left corner and size in absolute units).
Definition: drawingbase.hxx:74
AddressConverter & getAddressConverter() const
Returns the converter for string to cell address/range conversion.
void importPos(const AttributeList &rAttribs)
Imports the absolute anchor position from the xdr:pos element.