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 ):
78  break;
79  case XDR_TOKEN( oneCellAnchor ):
81  break;
82  case XDR_TOKEN( twoCellAnchor ):
83  {
85  OUString sEditAs = rAttribs.getXString( XML_editAs, OUString() );
86  if ( !sEditAs.isEmpty() )
87  {
88  if ( sEditAs.equalsIgnoreAsciiCase("absolute" ) )
90  else if ( sEditAs.equalsIgnoreAsciiCase("oneCell") )
92  else if (sEditAs.equalsIgnoreAsciiCase("twoCell") )
94  }
95  }
96  break;
97  default:
98  OSL_ENSURE( false, "ShapeAnchor::importAnchor - unexpected element" );
99  }
101 }
102 
103 void ShapeAnchor::importPos( const AttributeList& rAttribs )
104 {
105  OSL_ENSURE( meAnchorType == ANCHOR_ABSOLUTE, "ShapeAnchor::importPos - unexpected 'xdr:pos' element" );
106  maPos.X = rAttribs.getHyper( XML_x, 0 );
107  maPos.Y = rAttribs.getHyper( XML_y, 0 );
108 }
109 
110 void ShapeAnchor::importExt( const AttributeList& rAttribs )
111 {
112  OSL_ENSURE( (meAnchorType == ANCHOR_ABSOLUTE) || (meAnchorType == ANCHOR_ONECELL), "ShapeAnchor::importExt - unexpected 'xdr:ext' element" );
113  maSize.Width = rAttribs.getHyper( XML_cx, 0 );
114  maSize.Height = rAttribs.getHyper( XML_cy, 0 );
115 }
116 
118 {
119  maClientData.mbLocksWithSheet = rAttribs.getBool( XML_fLocksWithSheet, true );
120  maClientData.mbPrintsWithSheet = rAttribs.getBool( XML_fPrintsWithSheet, true );
121 }
122 
123 void ShapeAnchor::setCellPos( sal_Int32 nElement, sal_Int32 nParentContext, const OUString& rValue )
124 {
125  CellAnchorModel* pCellAnchor = nullptr;
126  switch( nParentContext )
127  {
128  case XDR_TOKEN( from ):
129  OSL_ENSURE( (meAnchorType == ANCHOR_ONECELL) || (meAnchorType == ANCHOR_TWOCELL), "ShapeAnchor::setCellPos - unexpected 'xdr:from' element" );
130  pCellAnchor = &maFrom;
131  break;
132  case XDR_TOKEN( to ):
133  OSL_ENSURE( meAnchorType == ANCHOR_TWOCELL, "ShapeAnchor::setCellPos - unexpected 'xdr:to' element" );
134  pCellAnchor = &maTo;
135  break;
136  default:
137  OSL_ENSURE( false, "ShapeAnchor::setCellPos - unexpected parent element" );
138  }
139  if( pCellAnchor ) switch( nElement )
140  {
141  case XDR_TOKEN( col ): pCellAnchor->mnCol = rValue.toInt32(); break;
142  case XDR_TOKEN( row ): pCellAnchor->mnRow = rValue.toInt32(); break;
143  case XDR_TOKEN( colOff ): pCellAnchor->mnColOffset = rValue.toInt64(); break;
144  case XDR_TOKEN( rowOff ): pCellAnchor->mnRowOffset = rValue.toInt64(); break;
145  default: OSL_ENSURE( false, "ShapeAnchor::setCellPos - unexpected element" );
146  }
147 }
148 
149 void ShapeAnchor::importVmlAnchor( const OUString& rAnchor )
150 {
153 
154  sal_Int32 nValues[8];
155  sal_Int32 nI{ 0 };
156 
157  for(sal_Int32 nIndex{ 0 }; nIndex>=0;)
158  {
159  nValues[nI] = rAnchor.getToken( 0, ',', nIndex ).toInt32();
160  if (++nI==8)
161  {
162  maFrom.mnCol = nValues[0];
163  maFrom.mnColOffset = nValues[1];
164  maFrom.mnRow = nValues[2];
165  maFrom.mnRowOffset = nValues[3];
166  maTo.mnCol = nValues[4];
167  maTo.mnColOffset = nValues[5];
168  maTo.mnRow = nValues[6];
169  maTo.mnRowOffset = nValues[7];
170  return;
171  }
172  }
173 
174  OSL_FAIL("ShapeAnchor::importVmlAnchor - missing anchor tokens" );
175 }
176 
178 {
179  // Checks whether the shape is visible based on the anchor
180  // if From and To anchor has the same attribute values, the shape will not have width and height
181  // and thus we can assume that such kind of shape will be not be visible
182  return !(meAnchorType == ANCHOR_TWOCELL &&
183  (maTo.mnRow == maFrom.mnRow && maTo.mnCol == maFrom.mnCol) &&
185 }
186 
187 EmuRectangle ShapeAnchor::calcAnchorRectEmu( const css::awt::Size& rPageSizeHmm ) const
188 {
189  AddressConverter& rAddrConv = getAddressConverter();
190  EmuSize aPageSize( lclHmmToEmu( rPageSizeHmm.Width ), lclHmmToEmu( rPageSizeHmm.Height ) );
191  EmuRectangle aAnchorRect( -1, -1, -1, -1 );
192 
193  // calculate shape position
194  switch( meAnchorType )
195  {
196  case ANCHOR_ABSOLUTE:
197  OSL_ENSURE( maPos.isValid(), "ShapeAnchor::calcAnchorRectEmu - invalid position" );
198  if( maPos.isValid() && (maPos.X < aPageSize.Width) && (maPos.Y < aPageSize.Height) )
199  aAnchorRect.setPos( maPos );
200  break;
201  case ANCHOR_ONECELL:
202  case ANCHOR_TWOCELL:
203  case ANCHOR_VML:
204  OSL_ENSURE( maFrom.isValid(), "ShapeAnchor::calcAnchorRectEmu - invalid position" );
205  if( maFrom.isValid() && rAddrConv.checkCol( maFrom.mnCol, true ) && rAddrConv.checkRow( maFrom.mnRow, true ) )
206  {
207  EmuPoint aPoint = calcCellAnchorEmu( maFrom );
208  if( (aPoint.X < aPageSize.Width) && (aPoint.Y < aPageSize.Height) )
209  aAnchorRect.setPos( aPoint );
210  }
211  break;
212  case ANCHOR_INVALID:
213  OSL_ENSURE( false, "ShapeAnchor::calcAnchorRectEmu - invalid anchor" );
214  break;
215  }
216 
217  // calculate shape size
218  if( (aAnchorRect.X >= 0) && (aAnchorRect.Y >= 0) ) switch( meAnchorType )
219  {
220  case ANCHOR_ABSOLUTE:
221  case ANCHOR_ONECELL:
222  OSL_ENSURE( maSize.isValid(), "ShapeAnchor::calcAnchorRectEmu - invalid size" );
223  if( maSize.isValid() )
224  {
225  aAnchorRect.Width = ::std::min< sal_Int64 >( maSize.Width, aPageSize.Width - aAnchorRect.X );
226  aAnchorRect.Height = ::std::min< sal_Int64 >( maSize.Height, aPageSize.Height - aAnchorRect.Y );
227  }
228  break;
229  case ANCHOR_TWOCELL:
230  case ANCHOR_VML:
231  OSL_ENSURE( maTo.isValid(), "ShapeAnchor::calcAnchorRectEmu - invalid position" );
232  if( maTo.isValid() )
233  {
234  /* Pass a valid cell address to calcCellAnchorEmu(), otherwise
235  nothing useful is returned, even if either row or column is valid. */
236  ScAddress aToCell = rAddrConv.createValidCellAddress( BinAddress( maTo.mnCol, maTo.mnRow ), getSheetIndex(), true );
237  CellAnchorModel aValidTo = maTo;
238  aValidTo.mnCol = aToCell.Col();
239  aValidTo.mnRow = aToCell.Row();
240  EmuPoint aPoint = calcCellAnchorEmu( aValidTo );
241  // width (if column index is valid, use the calculated offset, otherwise stretch to maximum available X position)
242  aAnchorRect.Width = aPageSize.Width - aAnchorRect.X;
243  if( aToCell.Col() == maTo.mnCol )
244  aAnchorRect.Width = ::std::min< sal_Int64 >( aPoint.X - aAnchorRect.X + 1, aAnchorRect.Width );
245  // height (if row index is valid, use the calculated offset, otherwise stretch to maximum available Y position)
246  aAnchorRect.Height = aPageSize.Height - aAnchorRect.Y;
247  if( aToCell.Row() == maTo.mnRow )
248  aAnchorRect.Height = ::std::min< sal_Int64 >( aPoint.Y - aAnchorRect.Y + 1, aAnchorRect.Height );
249  }
250  break;
251  case ANCHOR_INVALID:
252  break;
253  }
254 
255  return aAnchorRect;
256 }
257 
258 css::awt::Rectangle ShapeAnchor::calcAnchorRectHmm( const css::awt::Size& rPageSizeHmm ) const
259 {
260  EmuRectangle aAnchorRect = calcAnchorRectEmu( rPageSizeHmm );
261  return css::awt::Rectangle( lclEmuToHmm( aAnchorRect.X ), lclEmuToHmm( aAnchorRect.Y ), lclEmuToHmm( aAnchorRect.Width ), lclEmuToHmm( aAnchorRect.Height ) );
262 }
263 
264 EmuPoint ShapeAnchor::calcCellAnchorEmu( const CellAnchorModel& rModel ) const
265 {
266  // calculate position of top-left edge of the cell
267  css::awt::Point aPoint = getCellPosition( rModel.mnCol, rModel.mnRow );
268  EmuPoint aEmuPoint( lclHmmToEmu( aPoint.X ), lclHmmToEmu( aPoint.Y ) );
269 
270  // add the offset inside the cell
271  switch( meCellAnchorType )
272  {
273  case CellAnchorType::Emu:
274  aEmuPoint.X += rModel.mnColOffset;
275  aEmuPoint.Y += rModel.mnRowOffset;
276  break;
277 
279  {
280  const UnitConverter& rUnitConv = getUnitConverter();
281  aEmuPoint.X += static_cast< sal_Int64 >( rUnitConv.scaleValue( static_cast< double >( rModel.mnColOffset ), Unit::ScreenX, Unit::Emu ) );
282  aEmuPoint.Y += static_cast< sal_Int64 >( rUnitConv.scaleValue( static_cast< double >( rModel.mnRowOffset ), Unit::ScreenY, Unit::Emu ) );
283  }
284  break;
285  }
286 
287  return aEmuPoint;
288 }
289 
290 } // namespace oox::xls
291 
292 /* 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:262
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:50
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:49
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:45
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:79
sal_Int32 mnRow
Column index.
Definition: drawingbase.hxx:48
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:267
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:77
sal_Int32 mnRow
Absolute anchor (top-left corner and size in absolute units).
Definition: drawingbase.hxx:75
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.