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
)
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
);
217 awt::Rectangle
aViewBox( 0, 0, aSize
.Width
* 360, aSize
.Height
* 360 );
218 aPropertyMap
[ PROP_ViewBox
] <<= aViewBox
;
220 Sequence
< EnhancedCustomShapeAdjustmentValue
> aAdjustmentValues( maAdjustmentGuideList
.size() );
221 for ( i
= 0; i
< maAdjustmentGuideList
.size(); i
++ )
223 EnhancedCustomShapeAdjustmentValue aAdjustmentVal
;
224 aAdjustmentVal
.Value
<<= maAdjustmentGuideList
[ i
].maFormula
.toInt32();
225 aAdjustmentVal
.State
= PropertyState_DIRECT_VALUE
;
226 aAdjustmentVal
.Name
= maAdjustmentGuideList
[ i
].maName
;
227 aAdjustmentValues
[ i
] = aAdjustmentVal
;
229 aPropertyMap
[ PROP_AdjustmentValues
] <<= aAdjustmentValues
;
233 Sequence
< EnhancedCustomShapeSegment
> aSegments( maSegments
.size() );
234 for ( i
= 0; i
< maSegments
.size(); i
++ )
235 aSegments
[ i
] = maSegments
[ i
];
236 aPath
[ PROP_Segments
] <<= aSegments
;
238 if ( maTextRect
.has() ) {
239 Sequence
< EnhancedCustomShapeTextFrame
> aTextFrames(1);
240 aTextFrames
[0].TopLeft
.First
= maTextRect
.get().l
;
241 aTextFrames
[0].TopLeft
.Second
= maTextRect
.get().t
;
242 aTextFrames
[0].BottomRight
.First
= maTextRect
.get().r
;
243 aTextFrames
[0].BottomRight
.Second
= maTextRect
.get().b
;
244 aPath
[ PROP_TextFrames
] <<= aTextFrames
;
247 sal_uInt32 j
, k
, nParameterPairs
= 0;
248 for ( i
= 0; i
< maPath2DList
.size(); i
++ )
249 nParameterPairs
+= maPath2DList
[ i
].parameter
.size();
251 Sequence
< EnhancedCustomShapeParameterPair
> aParameterPairs( nParameterPairs
);
252 for ( i
= 0, k
= 0; i
< maPath2DList
.size(); i
++ )
253 for ( j
= 0; j
< maPath2DList
[ i
].parameter
.size(); j
++ )
254 aParameterPairs
[ k
++ ] = maPath2DList
[ i
].parameter
[ j
];
255 aPath
[ PROP_Coordinates
] <<= aParameterPairs
;
257 if ( maPath2DList
.size() )
259 sal_Bool bAllZero
= sal_True
;
260 for ( i
=0; i
< maPath2DList
.size(); i
++ )
262 if ( maPath2DList
[i
].w
|| maPath2DList
[i
].h
) {
263 bAllZero
= sal_False
;
269 Sequence
< awt::Size
> aSubViewSize( maPath2DList
.size() );
270 for ( i
=0; i
< maPath2DList
.size(); i
++ )
272 aSubViewSize
[i
].Width
= static_cast< sal_Int32
>( maPath2DList
[i
].w
);
273 aSubViewSize
[i
].Height
= static_cast< sal_Int32
>( maPath2DList
[i
].h
);
274 OSL_TRACE("set subpath %d size: %d x %d", i
, maPath2DList
[i
].w
, maPath2DList
[i
].h
);
276 aPath
[ PROP_SubViewSize
] <<= aSubViewSize
;
280 Sequence
< PropertyValue
> aPathSequence
= aPath
.makePropertyValueSequence();
281 aPropertyMap
[ PROP_Path
] <<= aPathSequence
;
283 Sequence
< OUString
> aEquations( maGuideList
.size() );
284 for ( i
= 0; i
< maGuideList
.size(); i
++ )
285 aEquations
[ i
] = maGuideList
[ i
].maFormula
;
286 aPropertyMap
[ PROP_Equations
] <<= aEquations
;
288 Sequence
< PropertyValues
> aHandles( maAdjustHandleList
.size() );
289 for ( i
= 0; i
< maAdjustHandleList
.size(); i
++ )
292 // maAdjustmentHandle[ i ].gdRef1 ... maAdjustmentHandle[ i ].gdRef2 ... :(
293 // gdRef1 && gdRef2 -> we do not offer such reference, so it is difficult
294 // to determine the correct adjustment handle that should be updated with the adjustment
295 // position. here is the solution: the adjustment value that is used within the position
296 // has to be updated, in case the position is a formula the first usage of a
297 // adjustment value is decisive
298 if ( maAdjustHandleList
[ i
].polar
)
300 aHandle
[ PROP_Position
] <<= maAdjustHandleList
[ i
].pos
;
301 if ( maAdjustHandleList
[ i
].min1
.has() )
302 aHandle
[ PROP_RadiusRangeMinimum
] <<= maAdjustHandleList
[ i
].min1
.get();
303 if ( maAdjustHandleList
[ i
].max1
.has() )
304 aHandle
[ PROP_RadiusRangeMaximum
] <<= maAdjustHandleList
[ i
].max1
.get();
306 /* TODO: AngleMin & AngleMax
307 if ( maAdjustHandleList[ i ].min2.has() )
308 aHandle[ PROP_ ] = maAdjustHandleList[ i ].min2.get();
309 if ( maAdjustHandleList[ i ].max2.has() )
310 aHandle[ PROP_ ] = maAdjustHandleList[ i ].max2.get();
315 aHandle
[ PROP_Position
] <<= maAdjustHandleList
[ i
].pos
;
316 if ( maAdjustHandleList
[ i
].gdRef1
.has() )
318 // TODO: PROP_RefX and PROP_RefY are not yet part of our file format,
319 // so the handles will not work after save/reload
320 sal_Int32 nIndex
= GetCustomShapeGuideValue( maAdjustmentGuideList
, maAdjustHandleList
[ i
].gdRef1
.get() );
322 aHandle
[ PROP_RefX
] <<= nIndex
;
324 if ( maAdjustHandleList
[ i
].gdRef2
.has() )
326 sal_Int32 nIndex
= GetCustomShapeGuideValue( maAdjustmentGuideList
, maAdjustHandleList
[ i
].gdRef2
.get() );
328 aHandle
[ PROP_RefY
] <<= nIndex
;
330 if ( maAdjustHandleList
[ i
].min1
.has() )
331 aHandle
[ PROP_RangeXMinimum
] <<= maAdjustHandleList
[ i
].min1
.get();
332 if ( maAdjustHandleList
[ i
].max1
.has() )
333 aHandle
[ PROP_RangeXMaximum
] <<= maAdjustHandleList
[ i
].max1
.get();
334 if ( maAdjustHandleList
[ i
].min2
.has() )
335 aHandle
[ PROP_RangeYMinimum
] <<= maAdjustHandleList
[ i
].min2
.get();
336 if ( maAdjustHandleList
[ i
].max2
.has() )
337 aHandle
[ PROP_RangeYMaximum
] <<= maAdjustHandleList
[ i
].max2
.get();
339 aHandles
[ i
] = aHandle
.makePropertyValueSequence();
341 aPropertyMap
[ PROP_Handles
] <<= aHandles
;
344 OSL_TRACE("==cscode== begin");
345 aPropertyMap
.dumpCode();
346 OSL_TRACE("==cscode== end");
348 // converting the vector to a sequence
349 Sequence
< PropertyValue
> aSeq
= aPropertyMap
.makePropertyValueSequence();
350 PropertySet
aPropSet( xPropSet
);
351 aPropSet
.setProperty( PROP_CustomShapeGeometry
, aSeq
);
355 Any
CustomShapeProvider::createStringSequence( size_t nStrings
, const char **pStrings
)
357 Sequence
< OUString
> aStringSequence( nStrings
);
358 for (size_t i
= 0; i
< nStrings
; i
++)
359 aStringSequence
[i
] = OUString::intern(
360 pStrings
[i
], strlen( pStrings
[i
] ),
361 RTL_TEXTENCODING_ASCII_US
);
362 return makeAny( aStringSequence
);
365 com::sun::star::uno::Sequence
< com::sun::star::drawing::EnhancedCustomShapeSegment
>
366 CustomShapeProvider::createSegmentSequence( size_t nElems
, const sal_uInt16
*pValues
)
368 Sequence
< EnhancedCustomShapeSegment
> aSequence( (nElems
+ 1) / 2 );
369 for (size_t i
= 0, j
= 0; i
< nElems
/ 2; i
++)
371 aSequence
[i
].Command
= pValues
[j
++];
372 aSequence
[i
].Count
= pValues
[j
++];
377 com::sun::star::drawing::EnhancedCustomShapeParameterPair
378 CustomShapeProvider::createParameterPair( const ParameterPairData
*pData
)
380 EnhancedCustomShapeParameterPair aParameterPair
;
381 aParameterPair
.First
.Type
= pData
->nFirstType
;
382 aParameterPair
.First
.Value
= makeAny(pData
->nFirstValue
);
383 aParameterPair
.Second
.Type
= pData
->nSecondType
;
384 aParameterPair
.Second
.Value
= makeAny(pData
->nSecondValue
);
385 return aParameterPair
;
388 com::sun::star::uno::Sequence
< com::sun::star::drawing::EnhancedCustomShapeParameterPair
>
389 CustomShapeProvider::createParameterPairSequence( size_t nElems
, const ParameterPairData
*pData
)
391 Sequence
< EnhancedCustomShapeParameterPair
> aSequence( nElems
);
392 for (size_t i
= 0; i
< nElems
; i
++)
393 aSequence
[i
] = createParameterPair( pData
+ i
);
399 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */