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>
26 #include <unitconverter.hxx>
27 #include <oox/token/namespaces.hxx>
28 #include <oox/token/tokens.hxx>
29 
30 namespace oox::xls {
31 
32 using namespace ::oox::drawingml;
33 
34 namespace {
35 
37 sal_Int64 lclHmmToEmu( sal_Int32 nValue )
38 {
39  return (nValue < 0) ? -1 : convertHmmToEmu( nValue );
40 }
41 
43 sal_Int32 lclEmuToHmm( sal_Int64 nValue )
44 {
45  return (nValue < 0) ? -1 : convertEmuToHmm( nValue );
46 }
47 
48 } // namespace
49 
51  mnCol( -1 ),
52  mnRow( -1 ),
53  mnColOffset( 0 ),
54  mnRowOffset( 0 )
55 {
56 }
57 
59  mbLocksWithSheet( true ),
60  mbPrintsWithSheet( true )
61 {
62 }
63 
65  WorksheetHelper( rHelper ),
66  meAnchorType( ANCHOR_INVALID ),
67  meCellAnchorType( CellAnchorType::Emu ),
68  meEditAs( ANCHOR_TWOCELL )
69 {
70 }
71 
72 void ShapeAnchor::importAnchor( sal_Int32 nElement, const AttributeList& rAttribs )
73 {
74  switch( nElement )
75  {
76  case XDR_TOKEN( absoluteAnchor ):
79  break;
80  case XDR_TOKEN( oneCellAnchor ):
83  break;
84  case XDR_TOKEN( twoCellAnchor ):
85  {
87  OUString sEditAs = rAttribs.getXString( XML_editAs, OUString() );
88  if ( !sEditAs.isEmpty() )
89  {
90  if ( sEditAs.equalsIgnoreAsciiCase("absolute" ) )
92  else if ( sEditAs.equalsIgnoreAsciiCase("oneCell") )
94  else if (sEditAs.equalsIgnoreAsciiCase("twoCell") )
96  }
97  }
98  break;
99  default:
100  OSL_ENSURE( false, "ShapeAnchor::importAnchor - unexpected element" );
101  }
103 }
104 
105 void ShapeAnchor::importPos( const AttributeList& rAttribs )
106 {
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 );
110 }
111 
112 void ShapeAnchor::importExt( const AttributeList& rAttribs )
113 {
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 );
117 }
118 
120 {
121  maClientData.mbLocksWithSheet = rAttribs.getBool( XML_fLocksWithSheet, true );
122  maClientData.mbPrintsWithSheet = rAttribs.getBool( XML_fPrintsWithSheet, true );
123 }
124 
125 void ShapeAnchor::setCellPos( sal_Int32 nElement, sal_Int32 nParentContext, const OUString& rValue )
126 {
127  CellAnchorModel* pCellAnchor = nullptr;
128  switch( nParentContext )
129  {
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" );
140  }
141  if( pCellAnchor ) switch( nElement )
142  {
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" );
148  }
149 }
150 
151 void ShapeAnchor::importVmlAnchor( const OUString& rAnchor )
152 {
155 
156  sal_Int32 nValues[8];
157  sal_Int32 nI{ 0 };
158 
159  for(sal_Int32 nIndex{ 0 }; nIndex>=0;)
160  {
161  nValues[nI] = rAnchor.getToken( 0, ',', nIndex ).toInt32();
162  if (++nI==8)
163  {
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;
173  }
174  }
175 
176  OSL_FAIL("ShapeAnchor::importVmlAnchor - missing anchor tokens" );
177 }
178 
180 {
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) &&
187 }
188 
189 EmuRectangle ShapeAnchor::calcAnchorRectEmu( const css::awt::Size& rPageSizeHmm ) const
190 {
191  AddressConverter& rAddrConv = getAddressConverter();
192  EmuSize aPageSize( lclHmmToEmu( rPageSizeHmm.Width ), lclHmmToEmu( rPageSizeHmm.Height ) );
193  EmuRectangle aAnchorRect( -1, -1, -1, -1 );
194 
195  // calculate shape position
196  switch( meAnchorType )
197  {
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 ) )
208  {
209  EmuPoint aPoint = calcCellAnchorEmu( maFrom );
210  if( (aPoint.X < aPageSize.Width) && (aPoint.Y < aPageSize.Height) )
211  aAnchorRect.setPos( aPoint );
212  }
213  break;
214  case ANCHOR_INVALID:
215  OSL_ENSURE( false, "ShapeAnchor::calcAnchorRectEmu - invalid anchor" );
216  break;
217  }
218 
219  // calculate shape size
220  if( (aAnchorRect.X >= 0) && (aAnchorRect.Y >= 0) ) switch( meAnchorType )
221  {
222  case ANCHOR_ABSOLUTE:
223  case ANCHOR_ONECELL:
224  OSL_ENSURE( maSize.isValid(), "ShapeAnchor::calcAnchorRectEmu - invalid size" );
225  if( maSize.isValid() )
226  {
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 );
229  }
230  break;
231  case ANCHOR_TWOCELL:
232  case ANCHOR_VML:
233  OSL_ENSURE( maTo.isValid(), "ShapeAnchor::calcAnchorRectEmu - invalid position" );
234  if( maTo.isValid() )
235  {
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 );
251  }
252  break;
253  case ANCHOR_INVALID:
254  break;
255  }
256 
257  return aAnchorRect;
258 }
259 
260 css::awt::Rectangle ShapeAnchor::calcAnchorRectHmm( const css::awt::Size& rPageSizeHmm ) const
261 {
262  EmuRectangle aAnchorRect = calcAnchorRectEmu( rPageSizeHmm );
263  return css::awt::Rectangle( lclEmuToHmm( aAnchorRect.X ), lclEmuToHmm( aAnchorRect.Y ), lclEmuToHmm( aAnchorRect.Width ), lclEmuToHmm( aAnchorRect.Height ) );
264 }
265 
266 EmuPoint ShapeAnchor::calcCellAnchorEmu( const CellAnchorModel& rModel ) const
267 {
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 ) );
271 
272  // add the offset inside the cell
273  switch( meCellAnchorType )
274  {
275  case CellAnchorType::Emu:
276  aEmuPoint.X += rModel.mnColOffset;
277  aEmuPoint.Y += rModel.mnRowOffset;
278  break;
279 
281  {
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 ) );
285  }
286  break;
287  }
288 
289  return aEmuPoint;
290 }
291 
292 } // namespace oox::xls
293 
294 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
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)
OptValue< bool > getBool(sal_Int32 nAttrToken) const
SCROW Row() const
Definition: address.hxx:261
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
Helper class that provides functions to convert values from and to different units.
AnchorPointModel maPos
Type of the cell anchor models.
UBlockCode from
UBlockCode to
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.
void importVmlAnchor(const OUString &rAnchor)
Imports the client anchor settings from a VML element.
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.
void setCellPos(sal_Int32 nElement, sal_Int32 nParentContext, const OUString &rValue)
Sets an attribute of the cell-dependent anchor position from xdr:from and xdr:to elements.
Twips (1/20 point).
Two-cell anchor (top-left and bottom-right corner at cell).
Definition: drawingbase.hxx:78
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:266
ShapeAnchor(const WorksheetHelper &rHelper)
Definition: drawingbase.cxx:64
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:72
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:50
::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.