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
31namespace oox::xls {
32
33using namespace ::oox::drawingml;
34
35namespace {
36
38sal_Int64 lclHmmToEmu( sal_Int32 nValue )
39{
40 return (nValue < 0) ? -1 : convertHmmToEmu( nValue );
41}
42
44sal_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
73void 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
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
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
126void 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
152void 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 &&
188}
189
190EmuRectangle ShapeAnchor::calcAnchorRectEmu( const css::awt::Size& rPageSizeHmm ) const
191{
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
261css::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
267EmuPoint 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 {
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: */
UBlockCode from
SCROW Row() const
Definition: address.hxx:274
SCCOL Col() const
Definition: address.hxx:279
std::optional< OUString > getXString(sal_Int32 nAttrToken) const
std::optional< sal_Int64 > getHyper(sal_Int32 nAttrToken) const
std::optional< bool > getBool(sal_Int32 nAttrToken) const
Converter for cell addresses and cell ranges for OOXML and BIFF filters.
bool checkRow(sal_Int32 nRow, bool bTrackOverflow)
Checks if the passed row index is valid.
bool checkCol(sal_Int32 nCol, bool bTrackOverflow)
Checks if the passed column index is valid.
ScAddress createValidCellAddress(const OUString &rString, sal_Int16 nSheet, bool bTrackOverflow)
Returns a valid cell address by moving it into allowed dimensions.
::oox::drawingml::EmuPoint calcCellAnchorEmu(const CellAnchorModel &rModel) const
Converts the passed anchor to an absolute position in EMUs.
AnchorSizeModel maSize
Top-left position, if anchor is of type absolute.
AnchorPointModel maPos
Type of the cell anchor models.
::oox::drawingml::EmuRectangle calcAnchorRectEmu(const css::awt::Size &rPageSizeHmm) const
Calculates the resulting shape anchor in EMUs.
void importExt(const AttributeList &rAttribs)
Imports the absolute anchor size from the xdr:ext element.
@ ANCHOR_ONECELL
Absolute anchor (top-left corner and size in absolute units).
Definition: drawingbase.hxx:74
@ ANCHOR_TWOCELL
One-cell anchor (top-left corner at cell, size in absolute units).
Definition: drawingbase.hxx:76
@ ANCHOR_VML
Two-cell anchor (top-left and bottom-right corner at cell).
Definition: drawingbase.hxx:78
@ ANCHOR_ABSOLUTE
Anchor type is unknown.
Definition: drawingbase.hxx:72
CellAnchorType meCellAnchorType
Type of this shape anchor.
CellAnchorModel maTo
Top-left position, if anchor is not of type absolute.
void importVmlAnchor(std::u16string_view rAnchor)
Imports the client anchor settings from a VML element.
void importAnchor(sal_Int32 nElement, const AttributeList &rAttribs)
Imports the shape anchor (one of the elements xdr:absoluteAnchor, xdr:oneCellAnchor,...
Definition: drawingbase.cxx:73
CellAnchorType
Specifies how cell positions from CellAnchorModel have to be processed.
@ Pixel
Offsets are given in EMUs.
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.
css::awt::Rectangle calcAnchorRectHmm(const css::awt::Size &rPageSizeHmm) const
Calculates the resulting shape anchor in 1/100 mm.
AnchorClientDataModel maClientData
Bottom-right position, if anchor is of type two-cell.
bool isAnchorValid() const
Checks whether the shape is visible based on the anchor.
CellAnchorModel maFrom
Anchor size, if anchor is not of type two-cell.
void importPos(const AttributeList &rAttribs)
Imports the absolute anchor position from the xdr:pos element.
AnchorType meEditAs
Shape client data.
void importClientData(const AttributeList &rAttribs)
Imports the shape client data from the xdr:clientData element.
ShapeAnchor(const WorksheetHelper &rHelper)
Definition: drawingbase.cxx:65
Helper class that provides functions to convert values from and to different units.
double scaleValue(double fValue, Unit eFromUnit, Unit eToUnit) const
Converts the passed value between the passed units.
AddressConverter & getAddressConverter() const
Returns the converter for string to cell address/range conversion.
UnitConverter & getUnitConverter() const
Returns the measurement unit converter.
css::awt::Point getCellPosition(sal_Int32 nCol, sal_Int32 nRow) const
Returns the absolute cell position in 1/100 mm.
SCTAB getSheetIndex() const
Returns the index of the current sheet.
sal_Int32 mnRow
sal_Int32 mnCol
sal_Int32 nIndex
RttiCompleteObjectLocator col
sal_Int32 toInt32(std::u16string_view str, sal_Int16 radix=10)
sal_Int64 toInt64(std::u16string_view str, sal_Int16 radix=10)
std::basic_string_view< charT, traits > getToken(std::basic_string_view< charT, traits > sv, charT delimiter, std::size_t &position)
sal_Int32 convertEmuToHmm(sal_Int64 nValue)
sal_Int64 convertHmmToEmu(sal_Int32 nValue)
@ ScreenX
English Metric Unit (1/360,000 cm).
@ Emu
Twips (1/20 point).
@ ScreenY
Horizontal screen pixels.
A 2D cell address struct for binary filters.
Position in spreadsheet (cell position and offset inside cell).
Definition: drawingbase.hxx:45
sal_Int64 mnRowOffset
X offset inside the column.
Definition: drawingbase.hxx:49
sal_Int64 mnColOffset
Row index.
Definition: drawingbase.hxx:48
CellAnchorModel()
Y offset inside the row.
Definition: drawingbase.cxx:51
sal_Int32 mnRow
Column index.
Definition: drawingbase.hxx:47
static N to(double f)
Definition: xltools.cxx:275