1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
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>
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
;
41 # define USS(x) OUStringToOString( x, RTL_TEXTENCODING_UTF8 ).getStr()
43 namespace oox
{ namespace drawingml
{
45 CustomShapeProperties::CustomShapeProperties()
46 : mnShapePresetType ( -1 )
47 , mbMirroredX ( sal_False
)
48 , mbMirroredY ( sal_False
)
49 , mnTextRotateAngle ( 0 )
53 CustomShapeProperties::~CustomShapeProperties()
57 OUString
CustomShapeProperties::getShapePresetTypeName() const
59 return StaticTokenMap::get().getUnicodeTokenName( mnShapePresetType
);
62 sal_Int32
CustomShapeProperties::SetCustomShapeGuideValue( std::vector
< CustomShapeGuide
>& rGuideList
, const CustomShapeGuide
& rGuide
)
64 sal_uInt32 nIndex
= 0;
65 for( ; nIndex
< rGuideList
.size(); nIndex
++ )
67 if ( rGuideList
[ nIndex
].maName
== rGuide
.maName
)
70 if ( nIndex
== rGuideList
.size() )
71 rGuideList
.push_back( rGuide
);
72 return static_cast< sal_Int32
>( nIndex
);
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 sal_Int32
CustomShapeProperties::GetCustomShapeGuideValue( const std::vector
< CustomShapeGuide
>& rGuideList
, const OUString
& rFormulaName
)
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 sal_Int32 nIndex
= static_cast< sal_Int32
>( rGuideList
.size() ) - 1;
83 for( ; nIndex
>= 0; nIndex
-- )
85 if ( rGuideList
[ nIndex
].maName
== rFormulaName
)
92 CustomShapeProperties::PresetsMap
CustomShapeProperties::maPresetsMap
;
94 static OUString
GetConnectorShapeType( sal_Int32 nType
)
96 OSL_TRACE("GetConnectorShapeType preset: %d %d", nType
, XML_straightConnector1
);
101 case XML_straightConnector1
:
111 void CustomShapeProperties::pushToPropSet( const ::oox::core::FilterBase
& /* rFilterBase */,
112 const Reference
< XPropertySet
>& xPropSet
, const Reference
< XShape
> & xShape
, const awt::Size
&aSize
)
114 if ( mnShapePresetType
>= 0 )
116 OSL_TRACE("preset: %d", mnShapePresetType
);
118 if (maPresetsMap
.empty())
119 initializePresetsMap();
121 PropertyMap aPropertyMap
;
122 PropertySet
aPropSet( xPropSet
);
124 OUString sConnectorShapeType
= GetConnectorShapeType( mnShapePresetType
);
126 if (sConnectorShapeType
.getLength() > 0)
128 OSL_TRACE("connector shape: %s (%d)", USS(sConnectorShapeType
), mnShapePresetType
);
129 //const uno::Reference < drawing::XShape > xShape( xPropSet, UNO_QUERY );
130 Reference
< drawing::XEnhancedCustomShapeDefaulter
> xDefaulter( xShape
, UNO_QUERY
);
131 if( xDefaulter
.is() ) {
132 xDefaulter
->createCustomShapeDefaults( sConnectorShapeType
);
133 aPropertyMap
[ PROP_Type
] <<= Any( sConnectorShapeType
);
136 else if (maPresetsMap
.find(mnShapePresetType
) != maPresetsMap
.end())
138 OSL_TRACE("found property map for preset: %s (%d)", USS(getShapePresetTypeName()), mnShapePresetType
);
140 CustomShapeProvider
*pProvider
= maPresetsMap
[ mnShapePresetType
];
142 aPropertyMap
= pProvider
->getProperties();
144 aPropertyMap
.dumpCode();
148 aPropertyMap
[ PROP_MirroredX
] <<= Any( mbMirroredX
);
149 aPropertyMap
[ PROP_MirroredY
] <<= Any( mbMirroredY
);
150 aPropertyMap
[ PROP_TextPreRotateAngle
] <<= Any( mnTextRotateAngle
);
151 aPropertyMap
[ PROP_IsPostRotateAngle
] <<= true; // For OpenXML Imports
152 Sequence
< PropertyValue
> aSeq
= aPropertyMap
.makePropertyValueSequence();
153 aPropSet
.setProperty( PROP_CustomShapeGeometry
, aSeq
);
155 if ( maAdjustmentGuideList
.size() )
157 const OUString sType
= "Type";
158 const OUString
sCustomShapeGeometry("CustomShapeGeometry");
159 uno::Any aGeoPropSet
= xPropSet
->getPropertyValue( sCustomShapeGeometry
);
160 uno::Sequence
< beans::PropertyValue
> aGeoPropSeq
;
161 if ( aGeoPropSet
>>= aGeoPropSeq
)
163 sal_Int32 i
, nCount
= aGeoPropSeq
.getLength();
164 for ( i
= 0; i
< nCount
; i
++ )
166 const OUString
sAdjustmentValues("AdjustmentValues");
167 if ( aGeoPropSeq
[ i
].Name
.equals( sAdjustmentValues
) )
169 uno::Sequence
< com::sun::star::drawing::EnhancedCustomShapeAdjustmentValue
> aAdjustmentSeq
;
170 if ( aGeoPropSeq
[ i
].Value
>>= aAdjustmentSeq
)
172 for (std::vector
< CustomShapeGuide
>::const_iterator
aIter( maAdjustmentGuideList
.begin() ), aEnd(maAdjustmentGuideList
.end());
173 aIter
!= aEnd
; ++aIter
)
175 if ( (*aIter
).maName
.getLength() > 3 )
177 sal_Int32 nAdjustmentIndex
= (*aIter
).maName
.copy( 3 ).toInt32() - 1;
178 if ( ( nAdjustmentIndex
>= 0 ) && ( nAdjustmentIndex
< aAdjustmentSeq
.getLength() ) )
180 EnhancedCustomShapeAdjustmentValue aAdjustmentVal
;
181 aAdjustmentVal
.Value
<<= (*aIter
).maFormula
.toInt32();
182 aAdjustmentVal
.State
= PropertyState_DIRECT_VALUE
;
183 aAdjustmentVal
.Name
= (*aIter
).maName
;
184 aAdjustmentSeq
[ nAdjustmentIndex
] = aAdjustmentVal
;
186 } else if ( aAdjustmentSeq
.getLength() > 0 ) {
187 EnhancedCustomShapeAdjustmentValue aAdjustmentVal
;
188 aAdjustmentVal
.Value
<<= (*aIter
).maFormula
.toInt32();
189 aAdjustmentVal
.State
= PropertyState_DIRECT_VALUE
;
190 aAdjustmentVal
.Name
= (*aIter
).maName
;
191 aAdjustmentSeq
[ 0 ] = aAdjustmentVal
;
194 aGeoPropSeq
[ i
].Value
<<= aAdjustmentSeq
;
195 xPropSet
->setPropertyValue( sCustomShapeGeometry
, Any( aGeoPropSeq
) );
198 else if ( aGeoPropSeq
[ i
].Name
.equals( sType
) )
200 if ( sConnectorShapeType
.getLength() > 0 )
201 aGeoPropSeq
[ i
].Value
<<= sConnectorShapeType
;
203 aGeoPropSeq
[ i
].Value
<<= OUString( "ooxml-CustomShape" );
212 PropertyMap aPropertyMap
;
213 aPropertyMap
[ PROP_Type
] <<= OUString( "ooxml-non-primitive" );
214 aPropertyMap
[ PROP_MirroredX
] <<= Any( mbMirroredX
);
215 aPropertyMap
[ PROP_MirroredY
] <<= Any( mbMirroredY
);
216 awt::Rectangle
aViewBox( 0, 0, aSize
.Width
, aSize
.Height
);
217 aPropertyMap
[ PROP_ViewBox
] <<= aViewBox
;
219 Sequence
< EnhancedCustomShapeAdjustmentValue
> aAdjustmentValues( maAdjustmentGuideList
.size() );
220 for ( i
= 0; i
< maAdjustmentGuideList
.size(); i
++ )
222 EnhancedCustomShapeAdjustmentValue aAdjustmentVal
;
223 aAdjustmentVal
.Value
<<= maAdjustmentGuideList
[ i
].maFormula
.toInt32();
224 aAdjustmentVal
.State
= PropertyState_DIRECT_VALUE
;
225 aAdjustmentVal
.Name
= maAdjustmentGuideList
[ i
].maName
;
226 aAdjustmentValues
[ i
] = aAdjustmentVal
;
228 aPropertyMap
[ PROP_AdjustmentValues
] <<= aAdjustmentValues
;
232 Sequence
< EnhancedCustomShapeSegment
> aSegments( maSegments
.size() );
233 for ( i
= 0; i
< maSegments
.size(); i
++ )
234 aSegments
[ i
] = maSegments
[ i
];
235 aPath
[ PROP_Segments
] <<= aSegments
;
237 if ( maTextRect
.has() ) {
238 Sequence
< EnhancedCustomShapeTextFrame
> aTextFrames(1);
239 aTextFrames
[0].TopLeft
.First
= maTextRect
.get().l
;
240 aTextFrames
[0].TopLeft
.Second
= maTextRect
.get().t
;
241 aTextFrames
[0].BottomRight
.First
= maTextRect
.get().r
;
242 aTextFrames
[0].BottomRight
.Second
= maTextRect
.get().b
;
243 aPath
[ PROP_TextFrames
] <<= aTextFrames
;
246 sal_uInt32 j
, k
, nParameterPairs
= 0;
247 for ( i
= 0; i
< maPath2DList
.size(); i
++ )
248 nParameterPairs
+= maPath2DList
[ i
].parameter
.size();
250 Sequence
< EnhancedCustomShapeParameterPair
> aParameterPairs( nParameterPairs
);
251 for ( i
= 0, k
= 0; i
< maPath2DList
.size(); i
++ )
252 for ( j
= 0; j
< maPath2DList
[ i
].parameter
.size(); j
++ )
253 aParameterPairs
[ k
++ ] = maPath2DList
[ i
].parameter
[ j
];
254 aPath
[ PROP_Coordinates
] <<= aParameterPairs
;
256 if ( maPath2DList
.size() )
258 sal_Bool bAllZero
= sal_True
;
259 for ( i
=0; i
< maPath2DList
.size(); i
++ )
261 if ( maPath2DList
[i
].w
|| maPath2DList
[i
].h
) {
262 bAllZero
= sal_False
;
268 Sequence
< awt::Size
> aSubViewSize( maPath2DList
.size() );
269 for ( i
=0; i
< maPath2DList
.size(); i
++ )
271 aSubViewSize
[i
].Width
= static_cast< sal_Int32
>( maPath2DList
[i
].w
);
272 aSubViewSize
[i
].Height
= static_cast< sal_Int32
>( maPath2DList
[i
].h
);
273 OSL_TRACE("set subpath %d size: %d x %d", i
, maPath2DList
[i
].w
, maPath2DList
[i
].h
);
275 aPath
[ PROP_SubViewSize
] <<= aSubViewSize
;
279 Sequence
< PropertyValue
> aPathSequence
= aPath
.makePropertyValueSequence();
280 aPropertyMap
[ PROP_Path
] <<= aPathSequence
;
282 Sequence
< OUString
> aEquations( maGuideList
.size() );
283 for ( i
= 0; i
< maGuideList
.size(); i
++ )
284 aEquations
[ i
] = maGuideList
[ i
].maFormula
;
285 aPropertyMap
[ PROP_Equations
] <<= aEquations
;
287 Sequence
< PropertyValues
> aHandles( maAdjustHandleList
.size() );
288 for ( i
= 0; i
< maAdjustHandleList
.size(); i
++ )
291 // maAdjustmentHandle[ i ].gdRef1 ... maAdjustmentHandle[ i ].gdRef2 ... :(
292 // gdRef1 && gdRef2 -> we do not offer such reference, so it is difficult
293 // to determine the correct adjustment handle that should be updated with the adjustment
294 // position. here is the solution: the adjustment value that is used within the position
295 // has to be updated, in case the position is a formula the first usage of a
296 // adjustment value is decisive
297 if ( maAdjustHandleList
[ i
].polar
)
299 aHandle
[ PROP_Position
] <<= maAdjustHandleList
[ i
].pos
;
300 if ( maAdjustHandleList
[ i
].min1
.has() )
301 aHandle
[ PROP_RadiusRangeMinimum
] <<= maAdjustHandleList
[ i
].min1
.get();
302 if ( maAdjustHandleList
[ i
].max1
.has() )
303 aHandle
[ PROP_RadiusRangeMaximum
] <<= maAdjustHandleList
[ i
].max1
.get();
305 /* TODO: AngleMin & AngleMax
306 if ( maAdjustHandleList[ i ].min2.has() )
307 aHandle[ PROP_ ] = maAdjustHandleList[ i ].min2.get();
308 if ( maAdjustHandleList[ i ].max2.has() )
309 aHandle[ PROP_ ] = maAdjustHandleList[ i ].max2.get();
314 aHandle
[ PROP_Position
] <<= maAdjustHandleList
[ i
].pos
;
315 if ( maAdjustHandleList
[ i
].gdRef1
.has() )
317 // TODO: PROP_RefX and PROP_RefY are not yet part of our file format,
318 // so the handles will not work after save/reload
319 sal_Int32 nIndex
= GetCustomShapeGuideValue( maAdjustmentGuideList
, maAdjustHandleList
[ i
].gdRef1
.get() );
321 aHandle
[ PROP_RefX
] <<= nIndex
;
323 if ( maAdjustHandleList
[ i
].gdRef2
.has() )
325 sal_Int32 nIndex
= GetCustomShapeGuideValue( maAdjustmentGuideList
, maAdjustHandleList
[ i
].gdRef2
.get() );
327 aHandle
[ PROP_RefY
] <<= nIndex
;
329 if ( maAdjustHandleList
[ i
].min1
.has() )
330 aHandle
[ PROP_RangeXMinimum
] <<= maAdjustHandleList
[ i
].min1
.get();
331 if ( maAdjustHandleList
[ i
].max1
.has() )
332 aHandle
[ PROP_RangeXMaximum
] <<= maAdjustHandleList
[ i
].max1
.get();
333 if ( maAdjustHandleList
[ i
].min2
.has() )
334 aHandle
[ PROP_RangeYMinimum
] <<= maAdjustHandleList
[ i
].min2
.get();
335 if ( maAdjustHandleList
[ i
].max2
.has() )
336 aHandle
[ PROP_RangeYMaximum
] <<= maAdjustHandleList
[ i
].max2
.get();
338 aHandles
[ i
] = aHandle
.makePropertyValueSequence();
340 aPropertyMap
[ PROP_Handles
] <<= aHandles
;
343 SAL_INFO("oox.cscode", "==cscode== begin");
344 aPropertyMap
.dumpCode();
345 SAL_INFO("oox.cscode", "==cscode== end");
347 // converting the vector to a sequence
348 Sequence
< PropertyValue
> aSeq
= aPropertyMap
.makePropertyValueSequence();
349 PropertySet
aPropSet( xPropSet
);
350 aPropSet
.setProperty( PROP_CustomShapeGeometry
, aSeq
);
354 Any
CustomShapeProvider::createStringSequence( size_t nStrings
, const char **pStrings
)
356 Sequence
< OUString
> aStringSequence( nStrings
);
357 for (size_t i
= 0; i
< nStrings
; i
++)
358 aStringSequence
[i
] = OUString::intern(
359 pStrings
[i
], strlen( pStrings
[i
] ),
360 RTL_TEXTENCODING_ASCII_US
);
361 return makeAny( aStringSequence
);
364 com::sun::star::uno::Sequence
< com::sun::star::drawing::EnhancedCustomShapeSegment
>
365 CustomShapeProvider::createSegmentSequence( size_t nElems
, const sal_uInt16
*pValues
)
367 Sequence
< EnhancedCustomShapeSegment
> aSequence( (nElems
+ 1) / 2 );
368 for (size_t i
= 0, j
= 0; i
< nElems
/ 2; i
++)
370 aSequence
[i
].Command
= pValues
[j
++];
371 aSequence
[i
].Count
= pValues
[j
++];
376 com::sun::star::drawing::EnhancedCustomShapeParameterPair
377 CustomShapeProvider::createParameterPair( const ParameterPairData
*pData
)
379 EnhancedCustomShapeParameterPair aParameterPair
;
380 aParameterPair
.First
.Type
= pData
->nFirstType
;
381 aParameterPair
.First
.Value
= makeAny(pData
->nFirstValue
);
382 aParameterPair
.Second
.Type
= pData
->nSecondType
;
383 aParameterPair
.Second
.Value
= makeAny(pData
->nSecondValue
);
384 return aParameterPair
;
387 com::sun::star::uno::Sequence
< com::sun::star::drawing::EnhancedCustomShapeParameterPair
>
388 CustomShapeProvider::createParameterPairSequence( size_t nElems
, const ParameterPairData
*pData
)
390 Sequence
< EnhancedCustomShapeParameterPair
> aSequence( nElems
);
391 for (size_t i
= 0; i
< nElems
; i
++)
392 aSequence
[i
] = createParameterPair( pData
+ i
);
398 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */