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