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 "oox/drawingml/customshapeproperties.hxx"
21 : #include "oox/helper/helper.hxx"
22 : #include "oox/helper/propertymap.hxx"
23 : #include "oox/helper/propertyset.hxx"
24 : #include "oox/token/tokenmap.hxx"
25 : #include <com/sun/star/awt/Rectangle.hpp>
26 : #include <com/sun/star/awt/Size.hpp>
27 : #include <com/sun/star/beans/XMultiPropertySet.hpp>
28 : #include <com/sun/star/lang/XMultiServiceFactory.hpp>
29 : #include <com/sun/star/graphic/XGraphicTransformer.hpp>
30 : #include <com/sun/star/drawing/XShape.hpp>
31 : #include <com/sun/star/drawing/XEnhancedCustomShapeDefaulter.hpp>
32 : #include <com/sun/star/drawing/EnhancedCustomShapeTextFrame.hpp>
33 :
34 : using namespace ::oox::core;
35 : using namespace ::com::sun::star;
36 : using namespace ::com::sun::star::uno;
37 : using namespace ::com::sun::star::beans;
38 : using namespace ::com::sun::star::graphic;
39 : using namespace ::com::sun::star::drawing;
40 :
41 : # define USS(x) OUStringToOString( x, RTL_TEXTENCODING_UTF8 ).getStr()
42 :
43 : namespace oox { namespace drawingml {
44 :
45 0 : CustomShapeProperties::CustomShapeProperties()
46 : : mnShapePresetType ( -1 )
47 : , mbShapeTypeOverride(false)
48 : , mbMirroredX ( false )
49 : , mbMirroredY ( false )
50 : , mnTextRotateAngle ( 0 )
51 0 : , mnArcNum ( 0 )
52 : {
53 0 : }
54 0 : CustomShapeProperties::~CustomShapeProperties()
55 : {
56 0 : }
57 :
58 0 : OUString CustomShapeProperties::getShapePresetTypeName() const
59 : {
60 0 : return StaticTokenMap::get().getUnicodeTokenName( mnShapePresetType );
61 : }
62 :
63 0 : sal_Int32 CustomShapeProperties::SetCustomShapeGuideValue( std::vector< CustomShapeGuide >& rGuideList, const CustomShapeGuide& rGuide )
64 : {
65 0 : sal_uInt32 nIndex = 0;
66 0 : for( ; nIndex < rGuideList.size(); nIndex++ )
67 : {
68 0 : if ( rGuideList[ nIndex ].maName == rGuide.maName )
69 0 : break;
70 : }
71 0 : if ( nIndex == rGuideList.size() )
72 0 : rGuideList.push_back( rGuide );
73 0 : return static_cast< sal_Int32 >( nIndex );
74 : }
75 :
76 : // returns the index into the guidelist for a given formula name,
77 : // if the return value is < 0 then the guide value could not be found
78 0 : sal_Int32 CustomShapeProperties::GetCustomShapeGuideValue( const std::vector< CustomShapeGuide >& rGuideList, const OUString& rFormulaName )
79 : {
80 : // traverse the list from the end, because guide names can be reused
81 : // and current is the last one
82 : // see a1 guide in gear6 custom shape preset as example
83 0 : sal_Int32 nIndex = static_cast< sal_Int32 >( rGuideList.size() ) - 1;
84 0 : for( ; nIndex >= 0; nIndex-- )
85 : {
86 0 : if ( rGuideList[ nIndex ].maName == rFormulaName )
87 0 : break;
88 : }
89 :
90 0 : return nIndex;
91 : }
92 :
93 0 : CustomShapeProperties::PresetsMap CustomShapeProperties::maPresetsMap;
94 :
95 0 : static OUString GetConnectorShapeType( sal_Int32 nType )
96 : {
97 : OSL_TRACE("GetConnectorShapeType preset: %d %d", nType, XML_straightConnector1);
98 :
99 0 : OUString sType;
100 0 : switch( nType )
101 : {
102 : case XML_straightConnector1:
103 0 : sType = "mso-spt32";
104 0 : break;
105 : default:
106 0 : break;
107 : }
108 0 : return sType;
109 : }
110 :
111 :
112 0 : void CustomShapeProperties::pushToPropSet( const ::oox::core::FilterBase& /* rFilterBase */,
113 : const Reference < XPropertySet >& xPropSet, const Reference < XShape > & xShape, const awt::Size &aSize )
114 : {
115 0 : if ( mnShapePresetType >= 0 )
116 : {
117 : OSL_TRACE("preset: %d", mnShapePresetType);
118 :
119 0 : if (maPresetsMap.empty())
120 0 : initializePresetsMap();
121 :
122 0 : PropertyMap aPropertyMap;
123 0 : PropertySet aPropSet( xPropSet );
124 :
125 0 : OUString sConnectorShapeType = GetConnectorShapeType( mnShapePresetType );
126 :
127 0 : if (sConnectorShapeType.getLength() > 0)
128 : {
129 : OSL_TRACE("connector shape: %s (%d)", USS(sConnectorShapeType), mnShapePresetType);
130 : //const uno::Reference < drawing::XShape > xShape( xPropSet, UNO_QUERY );
131 0 : Reference< drawing::XEnhancedCustomShapeDefaulter > xDefaulter( xShape, UNO_QUERY );
132 0 : if( xDefaulter.is() ) {
133 0 : xDefaulter->createCustomShapeDefaults( sConnectorShapeType );
134 0 : aPropertyMap.setProperty( PROP_Type, sConnectorShapeType );
135 0 : }
136 : }
137 0 : else if (maPresetsMap.find(mnShapePresetType) != maPresetsMap.end())
138 : {
139 : OSL_TRACE("found property map for preset: %s (%d)", USS(getShapePresetTypeName()), mnShapePresetType);
140 :
141 0 : CustomShapeProvider *pProvider = maPresetsMap[ mnShapePresetType ];
142 0 : if (pProvider)
143 0 : aPropertyMap = pProvider->getProperties();
144 : #ifdef DEBUG
145 : aPropertyMap.dumpCode();
146 : #endif
147 : }
148 :
149 0 : aPropertyMap.setProperty( PROP_MirroredX, (sal_Bool) mbMirroredX );
150 0 : aPropertyMap.setProperty( PROP_MirroredY, (sal_Bool) mbMirroredY );
151 0 : aPropertyMap.setProperty( PROP_TextPreRotateAngle, mnTextRotateAngle );
152 0 : aPropertyMap.setProperty( PROP_IsPostRotateAngle, (sal_Bool)true); // For OpenXML Imports
153 0 : Sequence< PropertyValue > aSeq = aPropertyMap.makePropertyValueSequence();
154 0 : aPropSet.setProperty( PROP_CustomShapeGeometry, aSeq );
155 :
156 0 : if ( maAdjustmentGuideList.size() )
157 : {
158 0 : const OUString sType = "Type";
159 0 : const OUString sCustomShapeGeometry("CustomShapeGeometry");
160 0 : uno::Any aGeoPropSet = xPropSet->getPropertyValue( sCustomShapeGeometry );
161 0 : uno::Sequence< beans::PropertyValue > aGeoPropSeq;
162 0 : if ( aGeoPropSet >>= aGeoPropSeq )
163 : {
164 0 : sal_Int32 i, nCount = aGeoPropSeq.getLength();
165 0 : for ( i = 0; i < nCount; i++ )
166 : {
167 0 : const OUString sAdjustmentValues("AdjustmentValues");
168 0 : if ( aGeoPropSeq[ i ].Name.equals( sAdjustmentValues ) )
169 : {
170 0 : uno::Sequence< com::sun::star::drawing::EnhancedCustomShapeAdjustmentValue > aAdjustmentSeq;
171 0 : if ( aGeoPropSeq[ i ].Value >>= aAdjustmentSeq )
172 : {
173 0 : int nIndex=0;
174 0 : for (std::vector< CustomShapeGuide >::const_iterator aIter( maAdjustmentGuideList.begin() ), aEnd(maAdjustmentGuideList.end());
175 : aIter != aEnd; ++aIter)
176 : {
177 0 : if ( (*aIter).maName.getLength() > 3 )
178 : {
179 0 : sal_Int32 nAdjustmentIndex = (*aIter).maName.copy( 3 ).toInt32() - 1;
180 0 : if ( ( nAdjustmentIndex >= 0 ) && ( nAdjustmentIndex < aAdjustmentSeq.getLength() ) )
181 : {
182 0 : EnhancedCustomShapeAdjustmentValue aAdjustmentVal;
183 0 : aAdjustmentVal.Value <<= (*aIter).maFormula.toInt32();
184 0 : aAdjustmentVal.State = PropertyState_DIRECT_VALUE;
185 0 : aAdjustmentVal.Name = (*aIter).maName;
186 0 : aAdjustmentSeq[ nAdjustmentIndex ] = aAdjustmentVal;
187 : }
188 0 : } else if ( aAdjustmentSeq.getLength() > 0 ) {
189 0 : EnhancedCustomShapeAdjustmentValue aAdjustmentVal;
190 0 : aAdjustmentVal.Value <<= (*aIter).maFormula.toInt32();
191 0 : aAdjustmentVal.State = PropertyState_DIRECT_VALUE;
192 0 : aAdjustmentVal.Name = (*aIter).maName;
193 0 : aAdjustmentSeq[ nIndex++ ] = aAdjustmentVal;
194 : }
195 : }
196 0 : aGeoPropSeq[ i ].Value <<= aAdjustmentSeq;
197 0 : xPropSet->setPropertyValue( sCustomShapeGeometry, Any( aGeoPropSeq ) );
198 0 : }
199 : }
200 0 : else if ( aGeoPropSeq[ i ].Name.equals( sType ) )
201 : {
202 0 : if ( sConnectorShapeType.getLength() > 0 )
203 0 : aGeoPropSeq[ i ].Value <<= sConnectorShapeType;
204 : else
205 0 : aGeoPropSeq[ i ].Value <<= OUString( "ooxml-CustomShape" );
206 : }
207 0 : }
208 0 : }
209 0 : }
210 : }
211 : else
212 : {
213 : sal_uInt32 i;
214 0 : PropertyMap aPropertyMap;
215 0 : aPropertyMap.setProperty( PROP_Type, OUString( "ooxml-non-primitive" ));
216 0 : aPropertyMap.setProperty( PROP_MirroredX, (sal_Bool) mbMirroredX );
217 0 : aPropertyMap.setProperty( PROP_MirroredY, (sal_Bool) mbMirroredY );
218 : // Note 1: If Equations are defined - they are processed using internal div by 360 coordinates
219 : // while if they are not, standard ooxml coordinates are used.
220 : // This size specifically affects scaling.
221 : // Note 2: Width and Height are set to 0 to force scaling to 1.
222 0 : awt::Rectangle aViewBox( 0, 0, aSize.Width, aSize.Height );
223 0 : if( maGuideList.size() )
224 0 : aViewBox = awt::Rectangle( 0, 0, 0, 0 );
225 0 : aPropertyMap.setProperty( PROP_ViewBox, aViewBox);
226 :
227 0 : Sequence< EnhancedCustomShapeAdjustmentValue > aAdjustmentValues( maAdjustmentGuideList.size() );
228 0 : for ( i = 0; i < maAdjustmentGuideList.size(); i++ )
229 : {
230 0 : EnhancedCustomShapeAdjustmentValue aAdjustmentVal;
231 0 : aAdjustmentVal.Value <<= maAdjustmentGuideList[ i ].maFormula.toInt32();
232 0 : aAdjustmentVal.State = PropertyState_DIRECT_VALUE;
233 0 : aAdjustmentVal.Name = maAdjustmentGuideList[ i ].maName;
234 0 : aAdjustmentValues[ i ] = aAdjustmentVal;
235 0 : }
236 0 : aPropertyMap.setProperty( PROP_AdjustmentValues, aAdjustmentValues);
237 :
238 0 : PropertyMap aPath;
239 :
240 0 : Sequence< EnhancedCustomShapeSegment > aSegments( maSegments.size() );
241 0 : for ( i = 0; i < maSegments.size(); i++ )
242 0 : aSegments[ i ] = maSegments[ i ];
243 0 : aPath.setProperty( PROP_Segments, aSegments);
244 :
245 0 : if ( maTextRect.has() ) {
246 0 : Sequence< EnhancedCustomShapeTextFrame > aTextFrames(1);
247 0 : aTextFrames[0].TopLeft.First = maTextRect.get().l;
248 0 : aTextFrames[0].TopLeft.Second = maTextRect.get().t;
249 0 : aTextFrames[0].BottomRight.First = maTextRect.get().r;
250 0 : aTextFrames[0].BottomRight.Second = maTextRect.get().b;
251 0 : aPath.setProperty( PROP_TextFrames, aTextFrames);
252 : }
253 :
254 0 : sal_uInt32 j, k, nParameterPairs = 0;
255 0 : for ( i = 0; i < maPath2DList.size(); i++ )
256 0 : nParameterPairs += maPath2DList[ i ].parameter.size();
257 :
258 0 : Sequence< EnhancedCustomShapeParameterPair > aParameterPairs( nParameterPairs );
259 0 : for ( i = 0, k = 0; i < maPath2DList.size(); i++ )
260 0 : for ( j = 0; j < maPath2DList[ i ].parameter.size(); j++ )
261 0 : aParameterPairs[ k++ ] = maPath2DList[ i ].parameter[ j ];
262 0 : aPath.setProperty( PROP_Coordinates, aParameterPairs);
263 :
264 0 : if ( maPath2DList.size() )
265 : {
266 0 : sal_Bool bAllZero = sal_True;
267 0 : for ( i=0; i < maPath2DList.size(); i++ )
268 : {
269 0 : if ( maPath2DList[i].w || maPath2DList[i].h ) {
270 0 : bAllZero = sal_False;
271 0 : break;
272 : }
273 : }
274 :
275 0 : if ( !bAllZero ) {
276 0 : Sequence< awt::Size > aSubViewSize( maPath2DList.size() );
277 0 : for ( i=0; i < maPath2DList.size(); i++ )
278 : {
279 0 : aSubViewSize[i].Width = static_cast< sal_Int32 >( maPath2DList[i].w );
280 0 : aSubViewSize[i].Height = static_cast< sal_Int32 >( maPath2DList[i].h );
281 : OSL_TRACE("set subpath %d size: %d x %d", i, maPath2DList[i].w, maPath2DList[i].h);
282 : }
283 0 : aPath.setProperty( PROP_SubViewSize, aSubViewSize);
284 : }
285 : }
286 :
287 0 : Sequence< PropertyValue > aPathSequence = aPath.makePropertyValueSequence();
288 0 : aPropertyMap.setProperty( PROP_Path, aPathSequence);
289 :
290 0 : Sequence< OUString > aEquations( maGuideList.size() );
291 0 : for ( i = 0; i < maGuideList.size(); i++ )
292 0 : aEquations[ i ] = maGuideList[ i ].maFormula;
293 0 : aPropertyMap.setProperty( PROP_Equations, aEquations);
294 :
295 0 : Sequence< PropertyValues > aHandles( maAdjustHandleList.size() );
296 0 : for ( i = 0; i < maAdjustHandleList.size(); i++ )
297 : {
298 0 : PropertyMap aHandle;
299 : // maAdjustmentHandle[ i ].gdRef1 ... maAdjustmentHandle[ i ].gdRef2 ... :(
300 : // gdRef1 && gdRef2 -> we do not offer such reference, so it is difficult
301 : // to determine the correct adjustment handle that should be updated with the adjustment
302 : // position. here is the solution: the adjustment value that is used within the position
303 : // has to be updated, in case the position is a formula the first usage of a
304 : // adjustment value is decisive
305 0 : if ( maAdjustHandleList[ i ].polar )
306 : {
307 0 : aHandle.setProperty( PROP_Position, maAdjustHandleList[ i ].pos);
308 0 : if ( maAdjustHandleList[ i ].min1.has() )
309 0 : aHandle.setProperty( PROP_RadiusRangeMinimum, maAdjustHandleList[ i ].min1.get());
310 0 : if ( maAdjustHandleList[ i ].max1.has() )
311 0 : aHandle.setProperty( PROP_RadiusRangeMaximum, maAdjustHandleList[ i ].max1.get());
312 :
313 : /* TODO: AngleMin & AngleMax
314 : if ( maAdjustHandleList[ i ].min2.has() )
315 : aHandle.setProperty( PROP_ ] = maAdjustHandleList[ i ].min2.get());
316 : if ( maAdjustHandleList[ i ].max2.has() )
317 : aHandle.setProperty( PROP_ ] = maAdjustHandleList[ i ].max2.get());
318 : */
319 : }
320 : else
321 : {
322 0 : aHandle.setProperty( PROP_Position, maAdjustHandleList[ i ].pos);
323 0 : if ( maAdjustHandleList[ i ].gdRef1.has() )
324 : {
325 : // TODO: PROP_RefX and PROP_RefY are not yet part of our file format,
326 : // so the handles will not work after save/reload
327 0 : sal_Int32 nIndex = GetCustomShapeGuideValue( maAdjustmentGuideList, maAdjustHandleList[ i ].gdRef1.get() );
328 0 : if ( nIndex >= 0 )
329 0 : aHandle.setProperty( PROP_RefX, nIndex);
330 : }
331 0 : if ( maAdjustHandleList[ i ].gdRef2.has() )
332 : {
333 0 : sal_Int32 nIndex = GetCustomShapeGuideValue( maAdjustmentGuideList, maAdjustHandleList[ i ].gdRef2.get() );
334 0 : if ( nIndex >= 0 )
335 0 : aHandle.setProperty( PROP_RefY, nIndex);
336 : }
337 0 : if ( maAdjustHandleList[ i ].min1.has() )
338 0 : aHandle.setProperty( PROP_RangeXMinimum, maAdjustHandleList[ i ].min1.get());
339 0 : if ( maAdjustHandleList[ i ].max1.has() )
340 0 : aHandle.setProperty( PROP_RangeXMaximum, maAdjustHandleList[ i ].max1.get());
341 0 : if ( maAdjustHandleList[ i ].min2.has() )
342 0 : aHandle.setProperty( PROP_RangeYMinimum, maAdjustHandleList[ i ].min2.get());
343 0 : if ( maAdjustHandleList[ i ].max2.has() )
344 0 : aHandle.setProperty( PROP_RangeYMaximum, maAdjustHandleList[ i ].max2.get());
345 : }
346 0 : aHandles[ i ] = aHandle.makePropertyValueSequence();
347 0 : }
348 0 : aPropertyMap.setProperty( PROP_Handles, aHandles);
349 :
350 : #ifdef DEBUG
351 : SAL_INFO("oox.cscode", "==cscode== begin");
352 : aPropertyMap.dumpCode();
353 : SAL_INFO("oox.cscode", "==cscode== end");
354 : #endif
355 : // converting the vector to a sequence
356 0 : Sequence< PropertyValue > aSeq = aPropertyMap.makePropertyValueSequence();
357 0 : PropertySet aPropSet( xPropSet );
358 0 : aPropSet.setProperty( PROP_CustomShapeGeometry, aSeq );
359 : }
360 0 : }
361 :
362 0 : Any CustomShapeProvider::createStringSequence( size_t nStrings, const char **pStrings )
363 : {
364 0 : Sequence< OUString > aStringSequence( nStrings );
365 0 : for (size_t i = 0; i < nStrings; i++)
366 0 : aStringSequence[i] = OUString::intern(
367 0 : pStrings[i], strlen( pStrings[i] ),
368 0 : RTL_TEXTENCODING_ASCII_US );
369 0 : return makeAny( aStringSequence );
370 : }
371 :
372 : com::sun::star::uno::Sequence< com::sun::star::drawing::EnhancedCustomShapeSegment >
373 0 : CustomShapeProvider::createSegmentSequence( size_t nElems, const sal_uInt16 *pValues )
374 : {
375 0 : Sequence< EnhancedCustomShapeSegment > aSequence( (nElems + 1) / 2 );
376 0 : for (size_t i = 0, j = 0; i < nElems / 2; i++)
377 : {
378 0 : aSequence[i].Command = pValues[j++];
379 0 : aSequence[i].Count = pValues[j++];
380 : }
381 0 : return aSequence;
382 : }
383 :
384 : com::sun::star::drawing::EnhancedCustomShapeParameterPair
385 0 : CustomShapeProvider::createParameterPair( const ParameterPairData *pData )
386 : {
387 0 : EnhancedCustomShapeParameterPair aParameterPair;
388 0 : aParameterPair.First.Type = pData->nFirstType;
389 0 : aParameterPair.First.Value = makeAny(pData->nFirstValue);
390 0 : aParameterPair.Second.Type = pData->nSecondType;
391 0 : aParameterPair.Second.Value = makeAny(pData->nSecondValue);
392 0 : return aParameterPair;
393 : }
394 :
395 : com::sun::star::uno::Sequence< com::sun::star::drawing::EnhancedCustomShapeParameterPair >
396 0 : CustomShapeProvider::createParameterPairSequence( size_t nElems, const ParameterPairData *pData )
397 : {
398 0 : Sequence< EnhancedCustomShapeParameterPair > aSequence( nElems );
399 0 : for (size_t i = 0; i < nElems; i++)
400 0 : aSequence[i] = createParameterPair( pData + i );
401 0 : return aSequence;
402 : }
403 :
404 0 : } }
405 :
406 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|