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