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