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 "EnhancedCustomShape3d.hxx"
21 #include <o3tl/unit_conversion.hxx>
22 #include <svx/deflt3d.hxx>
23 #include <svx/svdmodel.hxx>
24 #include <tools/poly.hxx>
25 #include <svx/svditer.hxx>
26 #include <svx/svdobj.hxx>
27 #include <svx/svdoashp.hxx>
28 #include <svl/itemset.hxx>
29 #include <svx/xfillit0.hxx>
30 #include <svx/xlineit0.hxx>
31 #include <svx/xsflclit.hxx>
32 #include <svx/xbtmpit.hxx>
33 #include <svx/xflclit.hxx>
34 #include <svx/svdopath.hxx>
35 #include <svx/svddef.hxx>
36 #include <svx/svx3ditems.hxx>
37 #include <extrud3d.hxx>
38 #include <svx/xflbmtit.hxx>
39 #include <svx/xlnclit.hxx>
40 #include <svx/sdasitm.hxx>
41 #include <svx/scene3d.hxx>
42 #include <com/sun/star/drawing/Position3D.hpp>
43 #include <com/sun/star/drawing/Direction3D.hpp>
44 #include <com/sun/star/drawing/ShadeMode.hpp>
45 #include <svx/sdr/properties/properties.hxx>
46 #include <com/sun/star/drawing/EnhancedCustomShapeParameterPair.hpp>
47 #include <basegfx/polygon/b2dpolypolygontools.hxx>
48 #include <basegfx/range/b2drange.hxx>
49 #include <sdr/primitive2d/sdrattributecreator.hxx>
50 #include <drawinglayer/attribute/sdrlineattribute.hxx>
51 #include <drawinglayer/attribute/sdrlinestartendattribute.hxx>
52 #include <svx/xlnwtit.hxx>
53 #include <svx/xlntrit.hxx>
54 #include <svx/xfltrit.hxx>
55 
56 using namespace com::sun::star;
57 using namespace com::sun::star::uno;
58 
59 namespace {
60 
61 void GetOrigin( const SdrCustomShapeGeometryItem& rItem, double& rOriginX, double& rOriginY )
62 {
63     css::drawing::EnhancedCustomShapeParameterPair aOriginParaPair;
64     const Any* pAny = rItem.GetPropertyValueByName( "Extrusion", "Origin" );
65     if ( ! ( pAny && ( *pAny >>= aOriginParaPair ) && ( aOriginParaPair.First.Value >>= rOriginX ) && ( aOriginParaPair.Second.Value >>= rOriginY ) ) )
66     {
67         rOriginX = 0.50;
68         rOriginY =-0.50;
69     }
70 }
71 
72 void GetRotateAngle( const SdrCustomShapeGeometryItem& rItem, double& rAngleX, double& rAngleY )
73 {
74     css::drawing::EnhancedCustomShapeParameterPair aRotateAngleParaPair;
75     const Any* pAny = rItem.GetPropertyValueByName( "Extrusion", "RotateAngle" );
76     if ( ! ( pAny && ( *pAny >>= aRotateAngleParaPair ) && ( aRotateAngleParaPair.First.Value >>= rAngleX ) && ( aRotateAngleParaPair.Second.Value >>= rAngleY ) ) )
77     {
78         rAngleX = 0.0;
79         rAngleY = 0.0;
80     }
81     rAngleX = basegfx::deg2rad(rAngleX);
82     rAngleY = basegfx::deg2rad(rAngleY);
83 }
84 
85 void GetSkew( const SdrCustomShapeGeometryItem& rItem, double& rSkewAmount, double& rSkewAngle )
86 {
87     css::drawing::EnhancedCustomShapeParameterPair aSkewParaPair;
88     const Any* pAny = rItem.GetPropertyValueByName( "Extrusion", "Skew" );
89     if ( ! ( pAny && ( *pAny >>= aSkewParaPair ) && ( aSkewParaPair.First.Value >>= rSkewAmount ) && ( aSkewParaPair.Second.Value >>= rSkewAngle ) ) )
90     {
91         rSkewAmount = 50;
92         rSkewAngle = -135;
93     }
94     rSkewAngle = basegfx::deg2rad(rSkewAngle);
95 }
96 
97 void GetExtrusionDepth( const SdrCustomShapeGeometryItem& rItem, const double* pMap, double& rBackwardDepth, double& rForwardDepth )
98 {
99     css::drawing::EnhancedCustomShapeParameterPair aDepthParaPair;
100     double fDepth = 0, fFraction = 0;
101     const Any* pAny = rItem.GetPropertyValueByName( "Extrusion", "Depth" );
102     if ( pAny && ( *pAny >>= aDepthParaPair ) && ( aDepthParaPair.First.Value >>= fDepth ) && ( aDepthParaPair.Second.Value >>= fFraction ) )
103     {
104         rForwardDepth = fDepth * fFraction;
105         rBackwardDepth = fDepth - rForwardDepth;
106     }
107     else
108     {
109         rBackwardDepth = 1270;
110         rForwardDepth = 0;
111     }
112     if ( pMap )
113     {
114         double fMap = *pMap;
115         rBackwardDepth *= fMap;
116         rForwardDepth *= fMap;
117     }
118 }
119 
120 double GetDouble( const SdrCustomShapeGeometryItem& rItem, const OUString& rPropertyName, double fDefault )
121 {
122     double fRetValue = fDefault;
123     const Any* pAny = rItem.GetPropertyValueByName( "Extrusion", rPropertyName );
124     if ( pAny )
125         *pAny >>= fRetValue;
126     return fRetValue;
127 }
128 
129 drawing::ShadeMode GetShadeMode( const SdrCustomShapeGeometryItem& rItem, const drawing::ShadeMode eDefault )
130 {
131     drawing::ShadeMode eRet( eDefault );
132     const Any* pAny = rItem.GetPropertyValueByName( "Extrusion", "ShadeMode" );
133     if ( pAny )
134         *pAny >>= eRet;
135     return eRet;
136 }
137 
138 bool GetBool( const SdrCustomShapeGeometryItem& rItem, const OUString& rPropertyName, const bool bDefault )
139 {
140     bool bRetValue = bDefault;
141     const Any* pAny = rItem.GetPropertyValueByName( "Extrusion", rPropertyName );
142     if ( pAny )
143         *pAny >>= bRetValue;
144     return bRetValue;
145 }
146 
147 drawing::Position3D GetPosition3D( const SdrCustomShapeGeometryItem& rItem, const OUString& rPropertyName,
148                                     const drawing::Position3D& rDefault, const double* pMap )
149 {
150     drawing::Position3D aRetValue( rDefault );
151     const Any* pAny = rItem.GetPropertyValueByName( "Extrusion", rPropertyName );
152     if ( pAny )
153         *pAny >>= aRetValue;
154     if ( pMap )
155     {
156         aRetValue.PositionX *= *pMap;
157         aRetValue.PositionY *= *pMap;
158         aRetValue.PositionZ *= *pMap;
159     }
160     return aRetValue;
161 }
162 
163 drawing::Direction3D GetDirection3D( const SdrCustomShapeGeometryItem& rItem, const OUString& rPropertyName, const drawing::Direction3D& rDefault )
164 {
165     drawing::Direction3D aRetValue( rDefault );
166     const Any* pAny = rItem.GetPropertyValueByName( "Extrusion", rPropertyName );
167     if ( pAny )
168         *pAny >>= aRetValue;
169     return aRetValue;
170 }
171 
172 }
173 
174 EnhancedCustomShape3d::Transformation2D::Transformation2D(
175     const SdrObjCustomShape& rSdrObjCustomShape,
176     const double *pMap)
177 :   aCenter(rSdrObjCustomShape.GetSnapRect().Center())
178     , eProjectionMode( drawing::ProjectionMode_PARALLEL )
179     , fSkewAngle(0.0)
180     , fSkew(0.0)
181     , fOriginX(0.0)
182     , fOriginY(0.0)
183 {
184     const SdrCustomShapeGeometryItem& rGeometryItem(rSdrObjCustomShape.GetMergedItem( SDRATTR_CUSTOMSHAPE_GEOMETRY ));
185     const Any* pAny = rGeometryItem.GetPropertyValueByName( "Extrusion", "ProjectionMode" );
186     if ( pAny )
187         *pAny >>= eProjectionMode;
188 
189     if ( eProjectionMode == drawing::ProjectionMode_PARALLEL )
190         GetSkew( rGeometryItem, fSkew, fSkewAngle );
191     else
192     {
193         GetOrigin( rGeometryItem, fOriginX, fOriginY );
194         fOriginX = fOriginX * rSdrObjCustomShape.GetLogicRect().GetWidth();
195         fOriginY = fOriginY * rSdrObjCustomShape.GetLogicRect().GetHeight();
196 
197         drawing::Position3D aViewPointDefault( 3472, -3472, 25000 );
198         drawing::Position3D aViewPoint( GetPosition3D( rGeometryItem, "ViewPoint", aViewPointDefault, pMap ) );
199         fViewPoint.setX(aViewPoint.PositionX);
200         fViewPoint.setY(aViewPoint.PositionY);
201         fViewPoint.setZ(-aViewPoint.PositionZ);
202     }
203 }
204 
205 basegfx::B3DPolygon EnhancedCustomShape3d::Transformation2D::ApplySkewSettings( const basegfx::B3DPolygon& rPoly3D ) const
206 {
207     basegfx::B3DPolygon aRetval;
208 
209     sal_uInt32 j;
210     for ( j = 0; j < rPoly3D.count(); j++ )
211     {
212         const basegfx::B3DPoint aPoint(rPoly3D.getB3DPoint(j));
213         double fDepth(-( aPoint.getZ() * fSkew ) / 100.0);
214         aRetval.append(basegfx::B3DPoint(
215             aPoint.getX() + (fDepth * cos( fSkewAngle )),
216             aPoint.getY() - (fDepth * sin( fSkewAngle )),
217             aPoint.getZ()));
218     }
219 
220     return aRetval;
221 }
222 
223 Point EnhancedCustomShape3d::Transformation2D::Transform2D( const basegfx::B3DPoint& rPoint3D ) const
224 {
225     Point aPoint2D;
226     if ( eProjectionMode == drawing::ProjectionMode_PARALLEL )
227     {
228         aPoint2D.setX( static_cast<sal_Int32>(rPoint3D.getX()) );
229         aPoint2D.setY( static_cast<sal_Int32>(rPoint3D.getY()) );
230     }
231     else
232     {
233         double fX = rPoint3D.getX() - fOriginX;
234         double fY = rPoint3D.getY() - fOriginY;
235         double f = ( - fViewPoint.getZ() ) / ( rPoint3D.getZ() - fViewPoint.getZ() );
236         aPoint2D.setX( static_cast<sal_Int32>(( fX - fViewPoint.getX() ) * f + fViewPoint.getX() + fOriginX ) );
237         aPoint2D.setY( static_cast<sal_Int32>(( fY - fViewPoint.getY() ) * f + fViewPoint.getY() + fOriginY ) );
238     }
239     aPoint2D.Move( aCenter.X(), aCenter.Y() );
240     return aPoint2D;
241 }
242 
243 bool EnhancedCustomShape3d::Transformation2D::IsParallel() const
244 {
245     return eProjectionMode == css::drawing::ProjectionMode_PARALLEL;
246 }
247 
248 SdrObject* EnhancedCustomShape3d::Create3DObject(
249     const SdrObject* pShape2d,
250     const SdrObjCustomShape& rSdrObjCustomShape)
251 {
252     SdrObject* pRet(nullptr);
253     const SdrCustomShapeGeometryItem& rGeometryItem(rSdrObjCustomShape.GetMergedItem(SDRATTR_CUSTOMSHAPE_GEOMETRY));
254     double fMap(1.0), *pMap = nullptr;
255     Fraction aFraction( rSdrObjCustomShape.getSdrModelFromSdrObject().GetScaleFraction() );
256 
257     if ( aFraction.GetNumerator() != 1 || aFraction.GetDenominator() != 1 )
258     {
259         fMap *= double(aFraction);
260         pMap = &fMap;
261     }
262 
263     if ( rSdrObjCustomShape.getSdrModelFromSdrObject().GetScaleUnit() != MapUnit::Map100thMM )
264     {
265         DBG_ASSERT( rSdrObjCustomShape.getSdrModelFromSdrObject().GetScaleUnit() == MapUnit::MapTwip, "EnhancedCustomShape3d::Current MapMode is Unsupported" );
266         // But we could use MapToO3tlUnit from <tools/UnitConversion> ... ?
267         fMap *= o3tl::convert(1.0, o3tl::Length::mm100, o3tl::Length::twip);
268         pMap = &fMap;
269     }
270 
271     if ( GetBool( rGeometryItem, "Extrusion", false ) )
272     {
273         bool bIsMirroredX(rSdrObjCustomShape.IsMirroredX());
274         bool bIsMirroredY(rSdrObjCustomShape.IsMirroredY());
275         tools::Rectangle aSnapRect(rSdrObjCustomShape.GetLogicRect());
276         Degree100 nObjectRotation(rSdrObjCustomShape.GetRotateAngle());
277         if ( nObjectRotation )
278         {
279             double a = (36000 - nObjectRotation.get()) * F_PI18000;
280             tools::Long dx = aSnapRect.Right() - aSnapRect.Left();
281             tools::Long dy = aSnapRect.Bottom()- aSnapRect.Top();
282             Point aP( aSnapRect.TopLeft() );
283             RotatePoint( aP, rSdrObjCustomShape.GetSnapRect().Center(), sin( a ), cos( a ) );
284             aSnapRect.SetLeft( aP.X() );
285             aSnapRect.SetTop( aP.Y() );
286             aSnapRect.SetRight( aSnapRect.Left() + dx );
287             aSnapRect.SetBottom( aSnapRect.Top() + dy );
288         }
289         Point aCenter( aSnapRect.Center() );
290 
291         SfxItemSet aSet( rSdrObjCustomShape.GetMergedItemSet() );
292 
293         //SJ: vertical writing is not required, by removing this item no outliner is created
294         aSet.ClearItem( SDRATTR_TEXTDIRECTION );
295 
296         // #i105323# For 3D AutoShapes, the shadow attribute has to be applied to each
297         // created visualisation helper model shape individually. The shadow itself
298         // will then be rendered from the 3D renderer correctly for the whole 3D scene
299         // (and thus behind all objects of which the visualisation may be built). So,
300         // do NOT remove it from the ItemSet here.
301         // aSet.ClearItem(SDRATTR_SHADOW);
302 
303         std::vector< E3dCompoundObject* > aPlaceholderObjectList;
304 
305         double fExtrusionBackward, fExtrusionForward;
306         GetExtrusionDepth( rGeometryItem, pMap, fExtrusionBackward, fExtrusionForward );
307         double fDepth = fExtrusionBackward + fExtrusionForward;
308         if ( fDepth < 1.0 )
309             fDepth = 1.0;
310 
311         drawing::ProjectionMode eProjectionMode( drawing::ProjectionMode_PARALLEL );
312         const Any* pAny = rGeometryItem.GetPropertyValueByName( "Extrusion", "ProjectionMode" );
313         if ( pAny )
314             *pAny >>= eProjectionMode;
315         ProjectionType eProjectionType( eProjectionMode == drawing::ProjectionMode_PARALLEL ? ProjectionType::Parallel : ProjectionType::Perspective );
316         // pShape2d Convert in scenes which include 3D Objects
317         E3dDefaultAttributes a3DDefaultAttr;
318         a3DDefaultAttr.SetDefaultLatheCharacterMode( true );
319         a3DDefaultAttr.SetDefaultExtrudeCharacterMode( true );
320 
321         E3dScene* pScene = new E3dScene(rSdrObjCustomShape.getSdrModelFromSdrObject());
322 
323         bool bSceneHasObjects ( false );
324         bool bUseTwoFillStyles( false );
325 
326         drawing::ShadeMode eShadeMode( GetShadeMode( rGeometryItem, drawing::ShadeMode_FLAT ) );
327         bool bUseExtrusionColor = GetBool( rGeometryItem, "Color", false );
328 
329         drawing::FillStyle eFillStyle( aSet.Get(XATTR_FILLSTYLE).GetValue() );
330         pScene->GetProperties().SetObjectItem( Svx3DShadeModeItem( 0 ) );
331         aSet.Put( makeSvx3DPercentDiagonalItem( 0 ) );
332         aSet.Put( Svx3DTextureModeItem( 1 ) );
333         aSet.Put( Svx3DNormalsKindItem( 1 ) );
334 
335         if ( eShadeMode == drawing::ShadeMode_DRAFT )
336         {
337             aSet.Put( XLineStyleItem( drawing::LineStyle_SOLID ) );
338             aSet.Put( XFillStyleItem ( drawing::FillStyle_NONE ) );
339             aSet.Put( makeSvx3DDoubleSidedItem( true ) );
340         }
341         else
342         {
343             aSet.Put( XLineStyleItem( drawing::LineStyle_NONE ) );
344             if ( eFillStyle == drawing::FillStyle_NONE )
345                 aSet.Put( XFillStyleItem( drawing::FillStyle_SOLID ) );
346             else if ( ( eFillStyle == drawing::FillStyle_BITMAP ) || ( eFillStyle == drawing::FillStyle_GRADIENT ) || bUseExtrusionColor )
347                 bUseTwoFillStyles = true;
348 
349             // If shapes are mirrored once (mirroring two times correct geometry again)
350             // double-sided at the object and two-sided-lighting at the scene need to be set.
351 
352             // #i122777# Also use double sided for two fill styles since there several 3d objects get
353             // created with a depth of 0; one of them is the backside which needs double-sided to
354             // get visible
355             if(bUseTwoFillStyles || (bIsMirroredX && !bIsMirroredY) || (!bIsMirroredX && bIsMirroredY))
356             {
357                 aSet.Put( makeSvx3DDoubleSidedItem( true ) );
358                 pScene->GetProperties().SetObjectItem( makeSvx3DTwoSidedLightingItem( true ) );
359             }
360         }
361 
362         tools::Rectangle aBoundRect2d;
363         SdrObjListIter aIter( *pShape2d, SdrIterMode::DeepNoGroups );
364         const bool bMultipleSubObjects(aIter.Count() > 1);
365 
366         while( aIter.IsMore() )
367         {
368             const SdrObject* pNext = aIter.Next();
369             bool bIsPlaceholderObject = (pNext->GetMergedItem( XATTR_FILLSTYLE ).GetValue() == drawing::FillStyle_NONE )
370                                         && (pNext->GetMergedItem( XATTR_LINESTYLE ).GetValue() == drawing::LineStyle_NONE );
371             basegfx::B2DPolyPolygon aPolyPoly;
372             SfxItemSet aLocalSet(aSet);
373             drawing::FillStyle aLocalFillStyle(eFillStyle);
374 
375             if ( auto pPathObj = dynamic_cast<const SdrPathObj*>(pNext) )
376             {
377                 const SfxItemSet& rSet = pNext->GetMergedItemSet();
378                 bool bNeedToConvertToContour(false);
379 
380                 // do conversion only for single line objects; for all others a fill and a
381                 // line object get created. When we have fill, we want no line. That line has
382                 // always been there, but since it was never converted to contour, it kept
383                 // invisible (all this 'hidden' logic should be migrated to primitives).
384                 if(!bMultipleSubObjects)
385                 {
386                     const drawing::FillStyle eStyle(rSet.Get(XATTR_FILLSTYLE).GetValue());
387 
388                     if(drawing::FillStyle_NONE == eStyle)
389                     {
390                         const drawinglayer::attribute::SdrLineAttribute aLine(
391                             drawinglayer::primitive2d::createNewSdrLineAttribute(rSet));
392 
393                         bNeedToConvertToContour = (0.0 < aLine.getWidth() || 0.0 != aLine.getFullDotDashLen());
394 
395                         if(!bNeedToConvertToContour && !aLine.isDefault())
396                         {
397                             const drawinglayer::attribute::SdrLineStartEndAttribute aLineStartEnd(
398                                 drawinglayer::primitive2d::createNewSdrLineStartEndAttribute(rSet, aLine.getWidth()));
399 
400                             if((aLineStartEnd.getStartWidth() && aLineStartEnd.isStartActive())
401                                 || (aLineStartEnd.getEndWidth() && aLineStartEnd.isEndActive()))
402                             {
403                                 bNeedToConvertToContour = true;
404                             }
405                         }
406                     }
407                 }
408 
409                 if(bNeedToConvertToContour)
410                 {
411                     SdrObject* pNewObj = pNext->ConvertToContourObj(const_cast< SdrObject* >(pNext));
412                     SdrPathObj* pNewPathObj = dynamic_cast< SdrPathObj* >(pNewObj);
413 
414                     if(pNewPathObj)
415                     {
416                         aPolyPoly = pNewPathObj->GetPathPoly();
417 
418                         if(aPolyPoly.isClosed())
419                         {
420                             // correct item properties from line to fill style
421                             if(eShadeMode == drawing::ShadeMode_DRAFT)
422                             {
423                                 // for draft, create wireframe with fixed line width
424                                 aLocalSet.Put(XLineStyleItem(drawing::LineStyle_SOLID));
425                                 aLocalSet.Put(XLineWidthItem(40));
426                                 aLocalFillStyle = drawing::FillStyle_NONE;
427                             }
428                             else
429                             {
430                                 // switch from line to fill, copy line attr to fill attr (color, transparence)
431                                 aLocalSet.Put(XLineWidthItem(0));
432                                 aLocalSet.Put(XLineStyleItem(drawing::LineStyle_NONE));
433                                 aLocalSet.Put(XFillColorItem(OUString(), aLocalSet.Get(XATTR_LINECOLOR).GetColorValue()));
434                                 aLocalSet.Put(XFillStyleItem(drawing::FillStyle_SOLID));
435                                 aLocalSet.Put(XFillTransparenceItem(aLocalSet.Get(XATTR_LINETRANSPARENCE).GetValue()));
436                                 aLocalFillStyle = drawing::FillStyle_SOLID;
437                             }
438                         }
439                         else
440                         {
441                             // correct item properties to hairlines
442                             aLocalSet.Put(XLineWidthItem(0));
443                             aLocalSet.Put(XLineStyleItem(drawing::LineStyle_SOLID));
444                         }
445                     }
446 
447                     SdrObject::Free(pNewObj);
448                 }
449                 else
450                 {
451                     aPolyPoly = pPathObj->GetPathPoly();
452                 }
453             }
454             else
455             {
456                 SdrObjectUniquePtr pNewObj = pNext->ConvertToPolyObj( false, false );
457                 SdrPathObj* pPath = dynamic_cast<SdrPathObj*>( pNewObj.get() );
458                 if ( pPath )
459                     aPolyPoly = pPath->GetPathPoly();
460             }
461 
462             if( aPolyPoly.count() )
463             {
464                 if(aPolyPoly.areControlPointsUsed())
465                 {
466                     aPolyPoly = basegfx::utils::adaptiveSubdivideByAngle(aPolyPoly);
467                 }
468 
469                 const basegfx::B2DRange aTempRange(basegfx::utils::getRange(aPolyPoly));
470                 const tools::Rectangle aBoundRect(basegfx::fround(aTempRange.getMinX()), basegfx::fround(aTempRange.getMinY()), basegfx::fround(aTempRange.getMaxX()), basegfx::fround(aTempRange.getMaxY()));
471                 aBoundRect2d.Union( aBoundRect );
472 
473                 // #i122777# depth 0 is okay for planes when using double-sided
474                 E3dCompoundObject* p3DObj = new E3dExtrudeObj(
475                     rSdrObjCustomShape.getSdrModelFromSdrObject(),
476                     a3DDefaultAttr,
477                     aPolyPoly,
478                     bUseTwoFillStyles ? 0 : fDepth );
479 
480                 p3DObj->NbcSetLayer( pShape2d->GetLayer() );
481                 p3DObj->SetMergedItemSet( aLocalSet );
482 
483                 if ( bIsPlaceholderObject )
484                     aPlaceholderObjectList.push_back( p3DObj );
485                 else if ( bUseTwoFillStyles )
486                 {
487                     BitmapEx aFillBmp;
488                     bool bFillBmpTile = p3DObj->GetMergedItem( XATTR_FILLBMP_TILE ).GetValue();
489                     if ( bFillBmpTile )
490                     {
491                         const XFillBitmapItem& rBmpItm = p3DObj->GetMergedItem(XATTR_FILLBITMAP);
492                         aFillBmp = rBmpItm.GetGraphicObject().GetGraphic().GetBitmapEx();
493 
494                         // #i122777# old adaptation of FillStyle bitmap size to 5-times the original size; this is not needed
495                         // anymore and was used in old times to male the fill look better when converting to 3D. Removed
496                         // from regular 3D objects for some time, also needs to be removed from CustomShapes
497 
498                         //Size aLogicalSize = aFillBmp.GetPrefSize();
499                         //if ( aFillBmp.GetPrefMapMode() == MapUnit::MapPixel )
500                         //  aLogicalSize = Application::GetDefaultDevice()->PixelToLogic( aLogicalSize, MapUnit::Map100thMM );
501                         //else
502                         //  aLogicalSize = OutputDevice::LogicToLogic( aLogicalSize, aFillBmp.GetPrefMapMode(), MapUnit::Map100thMM );
503                         //aLogicalSize.Width()  *= 5;           ;//             :-(     nice scaling, look at engine3d/obj3d.cxx
504                         //aLogicalSize.Height() *= 5;
505                         //aFillBmp.SetPrefSize( aLogicalSize );
506                         //aFillBmp.SetPrefMapMode( MapUnit::Map100thMM );
507                         //p3DObj->SetMergedItem(XFillBitmapItem(String(), Graphic(aFillBmp)));
508                     }
509                     else
510                     {
511                         if ( aSnapRect != aBoundRect && aSnapRect.GetWidth() > 0 && aSnapRect.GetHeight() > 0)
512                         {
513                             const XFillBitmapItem& rBmpItm = p3DObj->GetMergedItem(XATTR_FILLBITMAP);
514                             aFillBmp = rBmpItm.GetGraphicObject().GetGraphic().GetBitmapEx();
515                             Size aBmpSize( aFillBmp.GetSizePixel() );
516                             double fXScale = static_cast<double>(aBoundRect.GetWidth()) / static_cast<double>(aSnapRect.GetWidth());
517                             double fYScale = static_cast<double>(aBoundRect.GetHeight()) / static_cast<double>(aSnapRect.GetHeight());
518 
519                             Point aPt( static_cast<sal_Int32>( static_cast<double>( aBoundRect.Left() - aSnapRect.Left() )* static_cast<double>(aBmpSize.Width()) / static_cast<double>(aSnapRect.GetWidth()) ),
520                                                 static_cast<sal_Int32>( static_cast<double>( aBoundRect.Top() - aSnapRect.Top() ) * static_cast<double>(aBmpSize.Height()) / static_cast<double>(aSnapRect.GetHeight()) ) );
521                             Size aSize( static_cast<sal_Int32>( aBmpSize.Width() * fXScale ),
522                                                     static_cast<sal_Int32>( aBmpSize.Height() * fYScale ) );
523                             tools::Rectangle aCropRect( aPt, aSize );
524                             aFillBmp.Crop( aCropRect );
525                             p3DObj->SetMergedItem(XFillBitmapItem(OUString(), Graphic(aFillBmp)));
526                         }
527                     }
528                     pScene->InsertObject( p3DObj );
529                     p3DObj = new E3dExtrudeObj(
530                         rSdrObjCustomShape.getSdrModelFromSdrObject(),
531                         a3DDefaultAttr,
532                         aPolyPoly,
533                         fDepth);
534                     p3DObj->NbcSetLayer( pShape2d->GetLayer() );
535                     p3DObj->SetMergedItemSet( aLocalSet );
536                     if ( bUseExtrusionColor )
537                         p3DObj->SetMergedItem( XFillColorItem( "", rSdrObjCustomShape.GetMergedItem( XATTR_SECONDARYFILLCOLOR ).GetColorValue() ) );
538                     p3DObj->SetMergedItem( XFillStyleItem( drawing::FillStyle_SOLID ) );
539                     p3DObj->SetMergedItem( Svx3DCloseFrontItem( false ) );
540                     p3DObj->SetMergedItem( Svx3DCloseBackItem( false ) );
541                     pScene->InsertObject( p3DObj );
542 
543                     // #i122777# depth 0 is okay for planes when using double-sided
544                     p3DObj = new E3dExtrudeObj(
545                         rSdrObjCustomShape.getSdrModelFromSdrObject(),
546                         a3DDefaultAttr,
547                         aPolyPoly,
548                         0);
549 
550                     p3DObj->NbcSetLayer( pShape2d->GetLayer() );
551                     p3DObj->SetMergedItemSet( aLocalSet );
552 
553                     basegfx::B3DHomMatrix aFrontTransform( p3DObj->GetTransform() );
554                     aFrontTransform.translate( 0.0, 0.0, fDepth );
555                     p3DObj->NbcSetTransform( aFrontTransform );
556 
557                     if ( ( aLocalFillStyle == drawing::FillStyle_BITMAP ) && !aFillBmp.IsEmpty() )
558                     {
559                         p3DObj->SetMergedItem(XFillBitmapItem(OUString(), Graphic(aFillBmp)));
560                     }
561                 }
562                 else if ( aLocalFillStyle == drawing::FillStyle_NONE )
563                 {
564                     const XLineColorItem& rLineColor = p3DObj->GetMergedItem( XATTR_LINECOLOR );
565                     p3DObj->SetMergedItem( XFillColorItem( "", rLineColor.GetColorValue() ) );
566                     p3DObj->SetMergedItem( makeSvx3DDoubleSidedItem( true ) );
567                     p3DObj->SetMergedItem( Svx3DCloseFrontItem( false ) );
568                     p3DObj->SetMergedItem( Svx3DCloseBackItem( false ) );
569                 }
570                 pScene->InsertObject( p3DObj );
571                 bSceneHasObjects = true;
572             }
573         }
574 
575         if ( bSceneHasObjects ) // is the SdrObject properly converted
576         {
577             // then we can change the return value
578             pRet = pScene;
579 
580             // Camera settings, Perspective ...
581             Camera3D rCamera = pScene->GetCamera();
582             const basegfx::B3DRange& rVolume = pScene->GetBoundVolume();
583             pScene->NbcSetSnapRect( aSnapRect );
584 
585             // InitScene replacement
586             double fW = rVolume.getWidth();
587             double fH = rVolume.getHeight();
588 
589             rCamera.SetAutoAdjustProjection( false );
590             rCamera.SetViewWindow( -fW / 2, - fH / 2, fW, fH);
591             basegfx::B3DPoint aLookAt( 0.0, 0.0, 0.0 );
592             basegfx::B3DPoint aCamPos( 0.0, 0.0, 100.0 );
593             rCamera.SetPosAndLookAt( aCamPos, aLookAt );
594             rCamera.SetFocalLength( 1.0 );
595             rCamera.SetProjection( eProjectionType );
596             pScene->SetCamera( rCamera );
597             pScene->SetBoundAndSnapRectsDirty();
598 
599             double fOriginX, fOriginY;
600             GetOrigin( rGeometryItem, fOriginX, fOriginY );
601             fOriginX = fOriginX * aSnapRect.GetWidth();
602             fOriginY = fOriginY * aSnapRect.GetHeight();
603 
604             basegfx::B3DHomMatrix aNewTransform( pScene->GetTransform() );
605             aNewTransform.translate( -aCenter.X(), aCenter.Y(), -pScene->GetBoundVolume().getDepth() );
606 
607             double fXRotate, fYRotate;
608             GetRotateAngle( rGeometryItem, fXRotate, fYRotate );
609             double fZRotate(basegfx::deg2rad(rSdrObjCustomShape.GetObjectRotation()));
610             if ( fZRotate != 0.0 )
611                 aNewTransform.rotate( 0.0, 0.0, fZRotate );
612             if ( bIsMirroredX )
613                 aNewTransform.scale( -1.0, 1, 1 );
614             if ( bIsMirroredY )
615                 aNewTransform.scale( 1, -1.0, 1 );
616             if( fYRotate != 0.0 )
617                 aNewTransform.rotate( 0.0, -fYRotate, 0.0 );
618             if( fXRotate != 0.0 )
619                 aNewTransform.rotate( -fXRotate, 0.0, 0.0 );
620             if ( eProjectionType == ProjectionType::Parallel )
621             {
622                 double fSkew, fAlpha;
623                 GetSkew( rGeometryItem, fSkew, fAlpha );
624                 if ( fSkew != 0.0 )
625                 {
626                     double fInvTanBeta( fSkew / 100.0 );
627                     if(fInvTanBeta)
628                     {
629                         aNewTransform.shearXY(
630                             fInvTanBeta * cos(fAlpha),
631                             fInvTanBeta * sin(fAlpha));
632                     }
633                 }
634                 basegfx::B3DPoint _aLookAt( 0.0, 0.0, 0.0 );
635                 basegfx::B3DPoint _aNewCamPos( 0.0, 0.0, 25000.0 );
636                 rCamera.SetPosAndLookAt( _aNewCamPos, _aLookAt );
637                 pScene->SetCamera( rCamera );
638             }
639             else
640             {
641                 aNewTransform.translate( -fOriginX, fOriginY, 0.0 );
642                 // now set correct camera position
643                 drawing::Position3D aViewPointDefault( 3472, -3472, 25000 );
644                 drawing::Position3D aViewPoint( GetPosition3D( rGeometryItem, "ViewPoint", aViewPointDefault, pMap ) );
645                 double fViewPointX = aViewPoint.PositionX;
646                 double fViewPointY = aViewPoint.PositionY;
647                 double fViewPointZ = aViewPoint.PositionZ;
648                 basegfx::B3DPoint _aLookAt( fViewPointX, -fViewPointY, 0.0 );
649                 basegfx::B3DPoint aNewCamPos( fViewPointX, -fViewPointY, fViewPointZ );
650                 rCamera.SetPosAndLookAt( aNewCamPos, _aLookAt );
651                 pScene->SetCamera( rCamera );
652             }
653 
654             pScene->NbcSetTransform( aNewTransform );
655 
656 
657             // light
658 
659             double fAmbientIntensity = GetDouble( rGeometryItem, "Brightness", 22178.0 / 655.36 ) / 100.0;
660 
661             drawing::Direction3D aFirstLightDirectionDefault( 50000, 0, 10000 );
662             drawing::Direction3D aFirstLightDirection( GetDirection3D( rGeometryItem, "FirstLightDirection", aFirstLightDirectionDefault ) );
663             if ( aFirstLightDirection.DirectionZ == 0.0 )
664                 aFirstLightDirection.DirectionZ = 1.0;
665 
666             double fLightIntensity = GetDouble( rGeometryItem, "FirstLightLevel", 43712.0 / 655.36 ) / 100.0;
667 
668             /* sal_Bool bFirstLightHarsh = */ GetBool( rGeometryItem, "FirstLightHarsh", false );
669 
670             drawing::Direction3D aSecondLightDirectionDefault( -50000, 0, 10000 );
671             drawing::Direction3D aSecondLightDirection( GetDirection3D( rGeometryItem, "SecondLightDirection", aSecondLightDirectionDefault ) );
672             if ( aSecondLightDirection.DirectionZ == 0.0 )
673                 aSecondLightDirection.DirectionZ = -1;
674 
675             double fLight2Intensity = GetDouble( rGeometryItem, "SecondLightLevel", 43712.0 / 655.36 ) / 100.0;
676 
677             /* sal_Bool bLight2Harsh = */ GetBool( rGeometryItem, "SecondLightHarsh", false );
678             /* sal_Bool bLightFace = */ GetBool( rGeometryItem, "LightFace", false );
679 
680             sal_uInt16 nAmbientColor = static_cast<sal_uInt16>( fAmbientIntensity * 255.0 );
681             if ( nAmbientColor > 255 )
682                 nAmbientColor = 255;
683             Color aGlobalAmbientColor( static_cast<sal_uInt8>(nAmbientColor), static_cast<sal_uInt8>(nAmbientColor), static_cast<sal_uInt8>(nAmbientColor) );
684             pScene->GetProperties().SetObjectItem( makeSvx3DAmbientcolorItem( aGlobalAmbientColor ) );
685 
686             sal_uInt8 nSpotLight1 = static_cast<sal_uInt8>( fLightIntensity * 255.0 );
687             basegfx::B3DVector aSpotLight1( aFirstLightDirection.DirectionX, - ( aFirstLightDirection.DirectionY ), -( aFirstLightDirection.DirectionZ ) );
688             aSpotLight1.normalize();
689             pScene->GetProperties().SetObjectItem( makeSvx3DLightOnOff1Item( true ) );
690             Color aAmbientSpot1Color( nSpotLight1, nSpotLight1, nSpotLight1 );
691             pScene->GetProperties().SetObjectItem( makeSvx3DLightcolor1Item( aAmbientSpot1Color ) );
692             pScene->GetProperties().SetObjectItem( makeSvx3DLightDirection1Item( aSpotLight1 ) );
693 
694             sal_uInt8 nSpotLight2 = static_cast<sal_uInt8>( fLight2Intensity * 255.0 );
695             basegfx::B3DVector aSpotLight2( aSecondLightDirection.DirectionX, -aSecondLightDirection.DirectionY, -aSecondLightDirection.DirectionZ );
696             aSpotLight2.normalize();
697             pScene->GetProperties().SetObjectItem( makeSvx3DLightOnOff2Item( true ) );
698             Color aAmbientSpot2Color( nSpotLight2, nSpotLight2, nSpotLight2 );
699             pScene->GetProperties().SetObjectItem( makeSvx3DLightcolor2Item( aAmbientSpot2Color ) );
700             pScene->GetProperties().SetObjectItem( makeSvx3DLightDirection2Item( aSpotLight2 ) );
701 
702             sal_uInt8 nSpotLight3 = 70;
703             basegfx::B3DVector aSpotLight3( 0.0, 0.0, 1.0 );
704             pScene->GetProperties().SetObjectItem( makeSvx3DLightOnOff3Item( true ) );
705             Color aAmbientSpot3Color( nSpotLight3, nSpotLight3, nSpotLight3 );
706             pScene->GetProperties().SetObjectItem( makeSvx3DLightcolor3Item( aAmbientSpot3Color ) );
707             pScene->GetProperties().SetObjectItem( makeSvx3DLightDirection3Item( aSpotLight3 ) );
708 
709             double fSpecular = GetDouble( rGeometryItem, "Specularity", 0 ) / 100;
710             bool bMetal = GetBool( rGeometryItem, "Metal", false );
711 
712             Color aSpecularCol( 225,225,225 );
713             if ( bMetal )
714             {
715                 aSpecularCol = Color( 200, 200, 200 );
716                 fSpecular += 0.15;
717             }
718             sal_Int32 nIntensity = static_cast<sal_Int32>(fSpecular) * 100;
719             if ( nIntensity > 100 )
720                 nIntensity = 100;
721             else if ( nIntensity < 0 )
722                 nIntensity = 0;
723             nIntensity = 100 - nIntensity;
724             pScene->GetProperties().SetObjectItem( makeSvx3DMaterialSpecularItem( aSpecularCol ) );
725             pScene->GetProperties().SetObjectItem( makeSvx3DMaterialSpecularIntensityItem( static_cast<sal_uInt16>(nIntensity) ) );
726 
727             pScene->SetLogicRect(
728                 CalculateNewSnapRect(
729                     rSdrObjCustomShape,
730                     aSnapRect,
731                     aBoundRect2d,
732                     pMap));
733 
734             // removing placeholder objects
735             for (E3dCompoundObject* pTemp : aPlaceholderObjectList)
736             {
737                 pScene->RemoveObject( pTemp->GetOrdNum() );
738                 // always use SdrObject::Free(...) for SdrObjects (!)
739                 SdrObject* pTemp2(pTemp);
740                 SdrObject::Free(pTemp2);
741             }
742         }
743         else
744         {
745             // always use SdrObject::Free(...) for SdrObjects (!)
746             SdrObject* pTemp(pScene);
747             SdrObject::Free(pTemp);
748         }
749     }
750     return pRet;
751 }
752 
753 tools::Rectangle EnhancedCustomShape3d::CalculateNewSnapRect(
754     const SdrObjCustomShape& rSdrObjCustomShape,
755     const tools::Rectangle& rSnapRect,
756     const tools::Rectangle& rBoundRect,
757     const double* pMap)
758 {
759     const SdrCustomShapeGeometryItem& rGeometryItem(rSdrObjCustomShape.GetMergedItem( SDRATTR_CUSTOMSHAPE_GEOMETRY ));
760     const Point aCenter( rSnapRect.Center() );
761     double fExtrusionBackward, fExtrusionForward;
762     GetExtrusionDepth( rGeometryItem, pMap, fExtrusionBackward, fExtrusionForward );
763     sal_uInt32 i;
764 
765     // creating initial bound volume ( without rotation. skewing.and camera )
766     basegfx::B3DPolygon aBoundVolume;
767     const tools::Polygon aPolygon( rBoundRect );
768 
769     for ( i = 0; i < 4; i++ )
770     {
771         aBoundVolume.append(basegfx::B3DPoint(aPolygon[ static_cast<sal_uInt16>(i) ].X() - aCenter.X(), aPolygon[ static_cast<sal_uInt16>(i) ].Y() - aCenter.Y(), -fExtrusionForward));
772     }
773 
774     for ( i = 0; i < 4; i++ )
775     {
776         aBoundVolume.append(basegfx::B3DPoint(aPolygon[ static_cast<sal_uInt16>(i) ].X() - aCenter.X(), aPolygon[ static_cast<sal_uInt16>(i) ].Y() - aCenter.Y(), fExtrusionBackward));
777     }
778 
779     drawing::Direction3D aRotationCenterDefault( 0, 0, 0 ); // default seems to be wrong, a fractional size of shape has to be used!!
780     drawing::Direction3D aRotationCenter( GetDirection3D( rGeometryItem, "RotationCenter", aRotationCenterDefault ) );
781 
782     double fXRotate, fYRotate;
783     GetRotateAngle( rGeometryItem, fXRotate, fYRotate );
784     double fZRotate(basegfx::deg2rad(rSdrObjCustomShape.GetObjectRotation()));
785 
786     // rotating bound volume
787     basegfx::B3DHomMatrix aMatrix;
788     aMatrix.translate(-aRotationCenter.DirectionX, -aRotationCenter.DirectionY, -aRotationCenter.DirectionZ);
789     if ( fZRotate != 0.0 )
790         aMatrix.rotate( 0.0, 0.0, fZRotate );
791     if (rSdrObjCustomShape.IsMirroredX())
792         aMatrix.scale( -1.0, 1, 1 );
793     if (rSdrObjCustomShape.IsMirroredY())
794         aMatrix.scale( 1, -1.0, 1 );
795     if( fYRotate != 0.0 )
796         aMatrix.rotate( 0.0, fYRotate, 0.0 );
797     if( fXRotate != 0.0 )
798         aMatrix.rotate( -fXRotate, 0.0, 0.0 );
799     aMatrix.translate(aRotationCenter.DirectionX, aRotationCenter.DirectionY, aRotationCenter.DirectionZ);
800     aBoundVolume.transform(aMatrix);
801 
802     Transformation2D aTransformation2D(
803         rSdrObjCustomShape,
804         pMap);
805 
806     if ( aTransformation2D.IsParallel() )
807         aBoundVolume = aTransformation2D.ApplySkewSettings( aBoundVolume );
808 
809     tools::Polygon aTransformed( 8 );
810     for ( i = 0; i < 8; i++ )
811         aTransformed[ static_cast<sal_uInt16>(i) ] = aTransformation2D.Transform2D( aBoundVolume.getB3DPoint( i ) );
812 
813     return aTransformed.GetBoundRect();
814 }
815 
816 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
817