LibreOffice Module oox (master) 1
customshapeproperties.cxx
Go to the documentation of this file.
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
23#include <oox/token/properties.hxx>
25#include <com/sun/star/awt/Rectangle.hpp>
26#include <com/sun/star/awt/Size.hpp>
27#include <com/sun/star/beans/PropertyValues.hpp>
28#include <com/sun/star/beans/XPropertySet.hpp>
29#include <com/sun/star/drawing/EnhancedCustomShapeAdjustmentValue.hpp>
30#include <com/sun/star/drawing/EnhancedCustomShapeTextFrame.hpp>
31#include <com/sun/star/drawing/XEnhancedCustomShapeDefaulter.hpp>
32#include <com/sun/star/drawing/XShape.hpp>
34#include <o3tl/string_view.hxx>
35#include <sal/log.hxx>
36
37#include <algorithm>
38
39using namespace ::com::sun::star;
40using namespace ::com::sun::star::uno;
41using namespace ::com::sun::star::beans;
42using namespace ::com::sun::star::drawing;
43
44namespace oox::drawingml {
45
47: mnShapePresetType ( -1 )
48, mbShapeTypeOverride(false)
49, mbMirroredX ( false )
50, mbMirroredY ( false )
51, mnTextPreRotateAngle ( 0 )
52, mnTextCameraZRotateAngle ( 0 )
53, mnArcNum ( 0 )
54{
55}
56
57uno::Sequence< sal_Int8 > const & CustomShapeProperties::getShapePresetTypeName() const
58{
60}
61
62sal_Int32 CustomShapeProperties::SetCustomShapeGuideValue( std::vector< CustomShapeGuide >& rGuideList, const CustomShapeGuide& rGuide )
63{
64 std::vector<CustomShapeGuide>::size_type nIndex = 0;
65 for( ; nIndex < rGuideList.size(); nIndex++ )
66 {
67 if ( rGuideList[ nIndex ].maName == rGuide.maName )
68 break;
69 }
70 if ( nIndex == rGuideList.size() )
71 rGuideList.push_back( rGuide );
72 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
77sal_Int32 CustomShapeProperties::GetCustomShapeGuideValue( const std::vector< CustomShapeGuide >& rGuideList, std::u16string_view 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 sal_Int32 nIndex = static_cast< sal_Int32 >( rGuideList.size() ) - 1;
83 for( ; nIndex >= 0; nIndex-- )
84 {
85 if ( rGuideList[ nIndex ].maName == rFormulaName )
86 break;
87 }
88
89 return nIndex;
90}
91
93{
94 return !((getShapePresetType() >= 0 || maPath2DList.size() > 0) &&
95 getShapePresetType() != XML_Rect &&
96 getShapePresetType() != XML_rect);
97}
98
100
102 const Reference < XPropertySet >& xPropSet, const awt::Size &aSize )
103{
104 if ( mnShapePresetType >= 0 )
105 {
106 SAL_INFO("oox.drawingml", "preset: " << mnShapePresetType);
107
108 if (maPresetDataMap.empty())
110
111 PropertyMap aPropertyMap;
112 PropertySet aPropSet( xPropSet );
113
115 {
116 SAL_INFO(
117 "oox.drawingml",
118 "found property map for preset: " << mnShapePresetType);
119
120 aPropertyMap = maPresetDataMap[mnShapePresetType];
121#ifdef DEBUG
122 aPropertyMap.dumpCode( aPropertyMap.makePropertySet() );
123#endif
124 }
125
126 aPropertyMap.setProperty( PROP_MirroredX, mbMirroredX );
127 aPropertyMap.setProperty( PROP_MirroredY, mbMirroredY );
128 aPropertyMap.setProperty( PROP_TextPreRotateAngle, mnTextPreRotateAngle );
129 aPropertyMap.setProperty( PROP_TextCameraZRotateAngle, mnTextCameraZRotateAngle );
130 if (moTextAreaRotateAngle.has_value())
131 aPropertyMap.setProperty(PROP_TextRotateAngle, moTextAreaRotateAngle.value());
133 aPropSet.setProperty( PROP_CustomShapeGeometry, aSeq );
134
135 if ( !maAdjustmentGuideList.empty() )
136 {
137 static constexpr OUStringLiteral sCustomShapeGeometry(u"CustomShapeGeometry");
138 static constexpr OUStringLiteral sAdjustmentValues(u"AdjustmentValues");
139 uno::Any aGeoPropSet = xPropSet->getPropertyValue( sCustomShapeGeometry );
140 uno::Sequence< beans::PropertyValue > aGeoPropSeq;
141 if ( aGeoPropSet >>= aGeoPropSeq )
142 {
143 for ( auto& rGeoProp : asNonConstRange(aGeoPropSeq) )
144 {
145 if ( rGeoProp.Name == sAdjustmentValues )
146 {
147 uno::Sequence< css::drawing::EnhancedCustomShapeAdjustmentValue > aAdjustmentSeq;
148 if ( rGeoProp.Value >>= aAdjustmentSeq )
149 {
150 auto aAdjustmentSeqRange = asNonConstRange(aAdjustmentSeq);
151 int nIndex=0;
152 for (auto const& adjustmentGuide : maAdjustmentGuideList)
153 {
154 if ( adjustmentGuide.maName.getLength() > 3 )
155 {
156 sal_Int32 nAdjustmentIndex = o3tl::toInt32(adjustmentGuide.maName.subView( 3 )) - 1;
157 if ( ( nAdjustmentIndex >= 0 ) && ( nAdjustmentIndex < aAdjustmentSeq.getLength() ) )
158 {
159 EnhancedCustomShapeAdjustmentValue aAdjustmentVal;
160 aAdjustmentVal.Value <<= adjustmentGuide.maFormula.toInt32();
161 aAdjustmentVal.State = PropertyState_DIRECT_VALUE;
162 aAdjustmentVal.Name = adjustmentGuide.maName;
163 aAdjustmentSeqRange[ nAdjustmentIndex ] = aAdjustmentVal;
164 }
165 } else if ( aAdjustmentSeq.hasElements() ) {
166 EnhancedCustomShapeAdjustmentValue aAdjustmentVal;
167 aAdjustmentVal.Value <<= adjustmentGuide.maFormula.toInt32();
168 aAdjustmentVal.State = PropertyState_DIRECT_VALUE;
169 aAdjustmentVal.Name = adjustmentGuide.maName;
170 if (nIndex < aAdjustmentSeq.getLength())
171 {
172 aAdjustmentSeqRange[nIndex] = aAdjustmentVal;
173 ++nIndex;
174 }
175 }
176 }
177 rGeoProp.Value <<= aAdjustmentSeq;
178 xPropSet->setPropertyValue( sCustomShapeGeometry, Any( aGeoPropSeq ) );
179 break;
180 }
181 }
182 }
183 }
184 }
185 }
186 else
187 {
188 PropertyMap aPropertyMap;
189 aPropertyMap.setProperty( PROP_Type, OUString( "ooxml-non-primitive" ));
190 aPropertyMap.setProperty( PROP_MirroredX, mbMirroredX );
191 aPropertyMap.setProperty( PROP_MirroredY, mbMirroredY );
193 aPropertyMap.setProperty( PROP_TextPreRotateAngle, mnTextPreRotateAngle );
194 if (moTextAreaRotateAngle.has_value())
195 aPropertyMap.setProperty(PROP_TextRotateAngle, moTextAreaRotateAngle.value());
196 // Note 1: If Equations are defined - they are processed using internal div by 360 coordinates
197 // while if they are not, standard ooxml coordinates are used.
198 // This size specifically affects scaling.
199 // Note 2: Width and Height are set to 0 to force scaling to 1.
200 awt::Rectangle aViewBox( 0, 0, aSize.Width, aSize.Height );
201 // tdf#113187 Each ArcTo introduces two additional equations for converting start and swing
202 // angles. They do not count for test for use of standard ooxml coordinates
203 if (maGuideList.size() - 2 * countArcTo() > 0)
204 aViewBox = awt::Rectangle( 0, 0, 0, 0 );
205 aPropertyMap.setProperty( PROP_ViewBox, aViewBox);
206
208 auto aAdjustmentValuesRange = asNonConstRange(aAdjustmentValues);
209 for ( std::vector<CustomShapeGuide>::size_type i = 0; i < maAdjustmentGuideList.size(); i++ )
210 {
211 EnhancedCustomShapeAdjustmentValue aAdjustmentVal;
212 aAdjustmentVal.Value <<= maAdjustmentGuideList[ i ].maFormula.toInt32();
213 aAdjustmentVal.State = PropertyState_DIRECT_VALUE;
214 aAdjustmentVal.Name = maAdjustmentGuideList[ i ].maName;
215 aAdjustmentValuesRange[ i ] = aAdjustmentVal;
216 }
217 aPropertyMap.setProperty( PROP_AdjustmentValues, aAdjustmentValues);
218
219 PropertyMap aPath;
220
222
223 if ( maTextRect.has_value() ) {
225 { /* tl */ { maTextRect.value().l, maTextRect.value().t },
226 /* br */ { maTextRect.value().r, maTextRect.value().b } }
227 };
228 aPath.setProperty( PROP_TextFrames, aTextFrames);
229 }
230
231 sal_uInt32 nParameterPairs = 0;
232 for ( auto const & i: maPath2DList )
233 nParameterPairs += i.parameter.size();
234
235 Sequence< EnhancedCustomShapeParameterPair > aParameterPairs( nParameterPairs );
236 auto aParameterPairsRange = asNonConstRange(aParameterPairs);
237 sal_uInt32 k = 0;
238 for ( auto const & i: maPath2DList )
239 for ( auto const & j: i.parameter )
240 aParameterPairsRange[ k++ ] = j;
241 aPath.setProperty( PROP_Coordinates, aParameterPairs);
242
243 if ( !maPath2DList.empty() )
244 {
245 bool bAllZero = true;
246 for ( auto const & i: maPath2DList )
247 {
248 if ( i.w || i.h ) {
249 bAllZero = false;
250 break;
251 }
252 }
253
254 if ( !bAllZero ) {
255 Sequence< awt::Size > aSubViewSize( maPath2DList.size() );
256 std::transform(maPath2DList.begin(), maPath2DList.end(), aSubViewSize.getArray(),
257 [](const auto& p2d)
258 {
259 SAL_INFO("oox.cscode",
260 "set subpath; size: " << p2d.w << " x " << p2d.h);
261 return awt::Size(p2d.w, p2d.h);
262 });
263 aPath.setProperty( PROP_SubViewSize, aSubViewSize);
264 }
265 }
266
268 aPropertyMap.setProperty( PROP_Path, aPathSequence);
269
270 Sequence< OUString > aEquations( maGuideList.size() );
271 std::transform(maGuideList.begin(), maGuideList.end(), aEquations.getArray(),
272 [](const auto& g) { return g.maFormula; });
273 aPropertyMap.setProperty( PROP_Equations, aEquations);
274
276 auto aHandlesRange = asNonConstRange(aHandles);
277 for ( std::vector<AdjustHandle>::size_type i = 0; i < maAdjustHandleList.size(); i++ )
278 {
279 PropertyMap aHandle;
280 // maAdjustmentHandle[ i ].gdRef1 ... maAdjustmentHandle[ i ].gdRef2 ... :(
281 // gdRef1 && gdRef2 -> we do not offer such reference, so it is difficult
282 // to determine the correct adjustment handle that should be updated with the adjustment
283 // position. here is the solution: the adjustment value that is used within the position
284 // has to be updated, in case the position is a formula the first usage of a
285 // adjustment value is decisive
286 if ( maAdjustHandleList[ i ].polar )
287 {
288 // Polar handles in DrawingML
289 // 1. don't have reference center, so PROP_Polar isn't needed.
290 // 2. position always use planar coordinates.
291 // 3. use RefAngle and RefR to specify adjustment value to be updated.
292 // 4. The unit of angular adjustment values are 6000th degree.
293
294 aHandle.setProperty( PROP_Position, maAdjustHandleList[ i ].pos);
295 if ( maAdjustHandleList[ i ].gdRef1.has_value() )
296 {
298 if ( nIndex >= 0 )
299 aHandle.setProperty( PROP_RefR, nIndex);
300 }
301 if ( maAdjustHandleList[ i ].gdRef2.has_value() )
302 {
304 if ( nIndex >= 0 )
305 aHandle.setProperty( PROP_RefAngle, nIndex);
306 }
307 if ( maAdjustHandleList[ i ].min1.has_value() )
308 aHandle.setProperty( PROP_RadiusRangeMinimum, maAdjustHandleList[ i ].min1.value());
309 if ( maAdjustHandleList[ i ].max1.has_value() )
310 aHandle.setProperty( PROP_RadiusRangeMaximum, maAdjustHandleList[ i ].max1.value());
311
312 /* TODO: AngleMin & AngleMax
313 if ( maAdjustHandleList[ i ].min2.has() )
314 aHandle.setProperty( PROP_ ] = maAdjustHandleList[ i ].min2.get());
315 if ( maAdjustHandleList[ i ].max2.has() )
316 aHandle.setProperty( PROP_ ] = maAdjustHandleList[ i ].max2.get());
317 */
318 }
319 else
320 {
321 aHandle.setProperty( PROP_Position, maAdjustHandleList[ i ].pos);
322 if ( maAdjustHandleList[ i ].gdRef1.has_value() )
323 {
324 // TODO: PROP_RefX and PROP_RefY are not yet part of our file format,
325 // so the handles will not work after save/reload
327 if ( nIndex >= 0 )
328 aHandle.setProperty( PROP_RefX, nIndex);
329 }
330 if ( maAdjustHandleList[ i ].gdRef2.has_value() )
331 {
333 if ( nIndex >= 0 )
334 aHandle.setProperty( PROP_RefY, nIndex);
335 }
336 if ( maAdjustHandleList[ i ].min1.has_value() )
337 aHandle.setProperty( PROP_RangeXMinimum, maAdjustHandleList[ i ].min1.value());
338 if ( maAdjustHandleList[ i ].max1.has_value() )
339 aHandle.setProperty( PROP_RangeXMaximum, maAdjustHandleList[ i ].max1.value());
340 if ( maAdjustHandleList[ i ].min2.has_value() )
341 aHandle.setProperty( PROP_RangeYMinimum, maAdjustHandleList[ i ].min2.value());
342 if ( maAdjustHandleList[ i ].max2.has_value() )
343 aHandle.setProperty( PROP_RangeYMaximum, maAdjustHandleList[ i ].max2.value());
344 }
345 aHandlesRange[ i ] = aHandle.makePropertyValueSequence();
346 }
347 aPropertyMap.setProperty( PROP_Handles, aHandles);
348
349#ifdef DEBUG
350 // Note that the script oox/source/drawingml/customshapes/generatePresetsData.pl looks
351 // for these ==cscode== and ==csdata== markers, so don't "clean up" these SAL_INFOs.
352 SAL_INFO("oox.cscode", "==cscode== begin");
353 aPropertyMap.dumpCode( aPropertyMap.makePropertySet() );
354 SAL_INFO("oox.cscode", "==cscode== end");
355 SAL_INFO("oox.csdata", "==csdata== begin");
356 aPropertyMap.dumpData( aPropertyMap.makePropertySet() );
357 SAL_INFO("oox.csdata", "==csdata== end");
358#endif
359 // converting the vector to a sequence
361 PropertySet aPropSet( xPropSet );
362 aPropSet.setProperty( PROP_CustomShapeGeometry, aSeq );
363 }
364}
365
366}
367
368/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
A helper that maps property identifiers to property values.
Definition: propertymap.hxx:52
static void dumpData(const css::uno::Reference< css::beans::XPropertySet > &rXPropSet)
css::uno::Sequence< css::beans::PropertyValue > makePropertyValueSequence() const
Returns a sequence of property values, filled with all contained properties.
bool setProperty(sal_Int32 nPropId, Type &&rValue)
Sets the specified property to the passed value.
Definition: propertymap.hxx:72
static void dumpCode(const css::uno::Reference< css::beans::XPropertySet > &rXPropSet)
css::uno::Reference< css::beans::XPropertySet > makePropertySet() const
Creates a property set supporting the XPropertySet interface and inserts all properties.
A wrapper for a UNO property set.
Definition: propertyset.hxx:58
bool setProperty(sal_Int32 nPropId, const Type &rValue)
Puts the passed value into the property set.
css::uno::Sequence< sal_Int8 > const & getUtf8TokenName(sal_Int32 nToken) const
Returns the UTF8 name of the passed token identifier as byte sequence.
Definition: tokenmap.hxx:49
std::optional< sal_Int32 > moTextAreaRotateAngle
void pushToPropSet(const css::uno::Reference< css::beans::XPropertySet > &xPropSet, const css::awt::Size &aSize)
bool representsDefaultShape() const
Returns whether or not the current CustomShapeProperties represent a default shape preset that is rec...
std::unordered_map< sal_Int32, PropertyMap > PresetDataMap
std::vector< CustomShapeGuide > maGuideList
static sal_Int32 GetCustomShapeGuideValue(const std::vector< CustomShapeGuide > &rGuideList, std::u16string_view rFormulaName)
std::vector< CustomShapeGuide > maAdjustmentGuideList
css::uno::Sequence< sal_Int8 > const & getShapePresetTypeName() const
static sal_Int32 SetCustomShapeGuideValue(std::vector< CustomShapeGuide > &rGuideList, const CustomShapeGuide &rGuide)
std::vector< css::drawing::EnhancedCustomShapeSegment > maSegments
std::vector< AdjustHandle > maAdjustHandleList
OUString maName
Definition: dffdumper.cxx:161
float u
sal_Int32 nIndex
Sequence< sal_Int8 > aSeq
#define SAL_INFO(area, stream)
css::uno::Sequence< DstElementType > containerToSequence(const SrcType &i_Container)
sal_Int32 toInt32(std::u16string_view str, sal_Int16 radix=10)
TokenMap & StaticTokenMap()
Definition: tokenmap.cxx:90
size_t pos