LibreOffice Module svx (master)  1
EnhancedCustomShape3d.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 
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/NormalsKind.hpp>
45 #include <com/sun/star/drawing/ShadeMode.hpp>
47 #include <com/sun/star/drawing/EnhancedCustomShapeParameterPair.hpp>
48 #include <com/sun/star/drawing/EnhancedCustomShapeMetalType.hpp>
49 #include <com/sun/star/drawing/ProjectionMode.hpp>
56 #include <svx/xlnwtit.hxx>
57 #include <svx/xlntrit.hxx>
58 #include <svx/xfltrit.hxx>
59 #include <basegfx/color/bcolor.hxx>
60 
61 using namespace com::sun::star;
62 using namespace com::sun::star::uno;
63 
64 namespace {
65 
66 void GetOrigin( const SdrCustomShapeGeometryItem& rItem, double& rOriginX, double& rOriginY )
67 {
68  css::drawing::EnhancedCustomShapeParameterPair aOriginParaPair;
69  const Any* pAny = rItem.GetPropertyValueByName( "Extrusion", "Origin" );
70  if ( ! ( pAny && ( *pAny >>= aOriginParaPair ) && ( aOriginParaPair.First.Value >>= rOriginX ) && ( aOriginParaPair.Second.Value >>= rOriginY ) ) )
71  {
72  rOriginX = 0.50;
73  rOriginY =-0.50;
74  }
75 }
76 
77 void GetRotateAngle( const SdrCustomShapeGeometryItem& rItem, double& rAngleX, double& rAngleY )
78 {
79  css::drawing::EnhancedCustomShapeParameterPair aRotateAngleParaPair;
80  const Any* pAny = rItem.GetPropertyValueByName( "Extrusion", "RotateAngle" );
81  if ( ! ( pAny && ( *pAny >>= aRotateAngleParaPair ) && ( aRotateAngleParaPair.First.Value >>= rAngleX ) && ( aRotateAngleParaPair.Second.Value >>= rAngleY ) ) )
82  {
83  rAngleX = 0.0;
84  rAngleY = 0.0;
85  }
86  rAngleX = basegfx::deg2rad(rAngleX);
87  rAngleY = basegfx::deg2rad(rAngleY);
88 }
89 
90 void GetSkew( const SdrCustomShapeGeometryItem& rItem, double& rSkewAmount, double& rSkewAngle )
91 {
92  css::drawing::EnhancedCustomShapeParameterPair aSkewParaPair;
93  const Any* pAny = rItem.GetPropertyValueByName( "Extrusion", "Skew" );
94  if ( ! ( pAny && ( *pAny >>= aSkewParaPair ) && ( aSkewParaPair.First.Value >>= rSkewAmount ) && ( aSkewParaPair.Second.Value >>= rSkewAngle ) ) )
95  {
96  rSkewAmount = 50;
97  // ODF default is 45, but older ODF documents expect -135 as default. For intermediate
98  // solution see tdf#141301 and tdf#141127.
99  // MS Office default -135 is set in msdffimp.cxx to make import independent from setting here.
100  rSkewAngle = -135;
101  }
102  rSkewAngle = basegfx::deg2rad(rSkewAngle);
103 }
104 
105 void GetExtrusionDepth( const SdrCustomShapeGeometryItem& rItem, const double* pMap, double& rBackwardDepth, double& rForwardDepth )
106 {
107  css::drawing::EnhancedCustomShapeParameterPair aDepthParaPair;
108  double fDepth = 0, fFraction = 0;
109  const Any* pAny = rItem.GetPropertyValueByName( "Extrusion", "Depth" );
110  if ( pAny && ( *pAny >>= aDepthParaPair ) && ( aDepthParaPair.First.Value >>= fDepth ) && ( aDepthParaPair.Second.Value >>= fFraction ) )
111  {
112  rForwardDepth = fDepth * fFraction;
113  rBackwardDepth = fDepth - rForwardDepth;
114  }
115  else
116  {
117  rBackwardDepth = 1270;
118  rForwardDepth = 0;
119  }
120  if ( pMap )
121  {
122  double fMap = *pMap;
123  rBackwardDepth *= fMap;
124  rForwardDepth *= fMap;
125  }
126 }
127 
128 double GetDouble( const SdrCustomShapeGeometryItem& rItem, const OUString& rPropertyName, double fDefault )
129 {
130  double fRetValue = fDefault;
131  const Any* pAny = rItem.GetPropertyValueByName( "Extrusion", rPropertyName );
132  if ( pAny )
133  *pAny >>= fRetValue;
134  return fRetValue;
135 }
136 
137 drawing::ShadeMode GetShadeMode( const SdrCustomShapeGeometryItem& rItem, const drawing::ShadeMode eDefault )
138 {
139  drawing::ShadeMode eRet( eDefault );
140  const Any* pAny = rItem.GetPropertyValueByName( "Extrusion", "ShadeMode" );
141  if ( pAny )
142  *pAny >>= eRet;
143  return eRet;
144 }
145 
146 bool GetBool( const SdrCustomShapeGeometryItem& rItem, const OUString& rPropertyName, const bool bDefault )
147 {
148  bool bRetValue = bDefault;
149  const Any* pAny = rItem.GetPropertyValueByName( "Extrusion", rPropertyName );
150  if ( pAny )
151  *pAny >>= bRetValue;
152  return bRetValue;
153 }
154 
155 drawing::Position3D GetPosition3D( const SdrCustomShapeGeometryItem& rItem, const OUString& rPropertyName,
156  const drawing::Position3D& rDefault, const double* pMap )
157 {
158  drawing::Position3D aRetValue( rDefault );
159  const Any* pAny = rItem.GetPropertyValueByName( "Extrusion", rPropertyName );
160  if ( pAny )
161  *pAny >>= aRetValue;
162  if ( pMap )
163  {
164  aRetValue.PositionX *= *pMap;
165  aRetValue.PositionY *= *pMap;
166  aRetValue.PositionZ *= *pMap;
167  }
168  return aRetValue;
169 }
170 
171 drawing::Direction3D GetDirection3D( const SdrCustomShapeGeometryItem& rItem, const OUString& rPropertyName, const drawing::Direction3D& rDefault )
172 {
173  drawing::Direction3D aRetValue( rDefault );
174  const Any* pAny = rItem.GetPropertyValueByName( "Extrusion", rPropertyName );
175  if ( pAny )
176  *pAny >>= aRetValue;
177  return aRetValue;
178 }
179 
180 sal_Int16 GetMetalType(const SdrCustomShapeGeometryItem& rItem, const sal_Int16 eDefault)
181 {
182  sal_Int16 aRetValue(eDefault);
183  const Any* pAny = rItem.GetPropertyValueByName("Extrusion", "MetalType");
184  if (pAny)
185  *pAny >>= aRetValue;
186  return aRetValue;
187 }
188 
189 // Calculates the light directions for the additional lights, which are used to emulate soft
190 // lights of MS Office. Method needs to be documented in the Wiki
191 // https://wiki.documentfoundation.org/Development/ODF_Implementer_Notes in part
192 // List_of_LibreOffice_ODF_implementation-defined_items
193 // The method expects vector rLight to be normalized and results normalized vectors.
194 void lcl_SoftLightsDirection(const basegfx::B3DVector& rLight, basegfx::B3DVector& rSoftUp,
195  basegfx::B3DVector& rSoftDown, basegfx::B3DVector& rSoftRight,
196  basegfx::B3DVector& rSoftLeft)
197 {
198  constexpr double fAngle = basegfx::deg2rad(60); // angle between regular light and soft light
199 
200  // We first create directions around (0|0|1) and then rotate them to the light position.
201  rSoftUp = basegfx::B3DVector(0.0, sin(fAngle), cos(fAngle));
202  rSoftDown = basegfx::B3DVector(0.0, -sin(fAngle), cos(fAngle));
203  rSoftRight = basegfx::B3DVector(sin(fAngle), 0.0, cos(fAngle));
204  rSoftLeft = basegfx::B3DVector(-sin(fAngle), 0.0, cos(fAngle));
205 
206  basegfx::B3DHomMatrix aRotateMat;
207  aRotateMat.rotate(0.0, 0.0, M_PI_4);
208  if (rLight.getX() == 0.0 && rLight.getZ() == 0.0)
209  {
210  // Special case with light from top or bottom
211  if (rLight.getY() >= 0.0)
212  aRotateMat.rotate(-M_PI_2, 0.0, 0.0);
213  else
214  aRotateMat.rotate(M_PI_2, 0.0, 0.0);
215  }
216  else
217  {
218  // Azimuth from z-axis to x-axis. (0|0|1) to (1|0|0) is 90deg.
219  double fAzimuth = atan2(rLight.getX(), rLight.getZ());
220  // Elevation from xz-plane to y-axis. (0|0|1) to (0|1|0) is 90deg.
221  double fElevation = atan2(rLight.getY(), std::hypot(rLight.getX(), rLight.getZ()));
222  aRotateMat.rotate(-fElevation, fAzimuth, 0.0);
223  }
224 
225  rSoftUp = aRotateMat * rSoftUp;
226  rSoftDown = aRotateMat * rSoftDown;
227  rSoftRight = aRotateMat * rSoftRight;
228  rSoftLeft = aRotateMat * rSoftLeft;
229 }
230 }
231 
233  const SdrObject* pShape2d,
234  const SdrObjCustomShape& rSdrObjCustomShape)
235 {
236  SdrObject* pRet(nullptr);
237  const SdrCustomShapeGeometryItem& rGeometryItem(rSdrObjCustomShape.GetMergedItem(SDRATTR_CUSTOMSHAPE_GEOMETRY));
238  double fMap(1.0), *pMap = nullptr;
239  Fraction aFraction( rSdrObjCustomShape.getSdrModelFromSdrObject().GetScaleFraction() );
240 
241  if ( aFraction.GetNumerator() != 1 || aFraction.GetDenominator() != 1 )
242  {
243  fMap *= double(aFraction);
244  pMap = &fMap;
245  }
246 
247  if ( rSdrObjCustomShape.getSdrModelFromSdrObject().GetScaleUnit() != MapUnit::Map100thMM )
248  {
249  DBG_ASSERT( rSdrObjCustomShape.getSdrModelFromSdrObject().GetScaleUnit() == MapUnit::MapTwip, "EnhancedCustomShape3d::Current MapMode is Unsupported" );
250  // But we could use MapToO3tlUnit from <tools/UnitConversion> ... ?
252  pMap = &fMap;
253  }
254 
255  if ( GetBool( rGeometryItem, "Extrusion", false ) )
256  {
257  bool bIsMirroredX(rSdrObjCustomShape.IsMirroredX());
258  bool bIsMirroredY(rSdrObjCustomShape.IsMirroredY());
259  tools::Rectangle aSnapRect(rSdrObjCustomShape.GetLogicRect());
260  Degree100 nObjectRotation(rSdrObjCustomShape.GetRotateAngle());
261  if ( nObjectRotation )
262  {
263  double a = toRadians(36000_deg100 - nObjectRotation);
264  tools::Long dx = aSnapRect.Right() - aSnapRect.Left();
265  tools::Long dy = aSnapRect.Bottom()- aSnapRect.Top();
266  Point aP( aSnapRect.TopLeft() );
267  RotatePoint( aP, rSdrObjCustomShape.GetSnapRect().Center(), sin( a ), cos( a ) );
268  aSnapRect.SetLeft( aP.X() );
269  aSnapRect.SetTop( aP.Y() );
270  aSnapRect.SetRight( aSnapRect.Left() + dx );
271  aSnapRect.SetBottom( aSnapRect.Top() + dy );
272  }
273  Point aCenter( aSnapRect.Center() );
274 
275  SfxItemSet aSet( rSdrObjCustomShape.GetMergedItemSet() );
276 
277  //SJ: vertical writing is not required, by removing this item no outliner is created
279 
280  // #i105323# For 3D AutoShapes, the shadow attribute has to be applied to each
281  // created visualisation helper model shape individually. The shadow itself
282  // will then be rendered from the 3D renderer correctly for the whole 3D scene
283  // (and thus behind all objects of which the visualisation may be built). So,
284  // do NOT remove it from the ItemSet here.
285  // aSet.ClearItem(SDRATTR_SHADOW);
286 
287  std::vector< E3dCompoundObject* > aPlaceholderObjectList;
288 
289  double fExtrusionBackward, fExtrusionForward;
290  GetExtrusionDepth( rGeometryItem, pMap, fExtrusionBackward, fExtrusionForward );
291  double fDepth = fExtrusionBackward + fExtrusionForward;
292  if ( fDepth < 1.0 )
293  fDepth = 1.0;
294 
295  drawing::ProjectionMode eProjectionMode( drawing::ProjectionMode_PARALLEL );
296  const Any* pAny = rGeometryItem.GetPropertyValueByName( "Extrusion", "ProjectionMode" );
297  if ( pAny )
298  *pAny >>= eProjectionMode;
299  // pShape2d Convert in scenes which include 3D Objects
300  E3dDefaultAttributes a3DDefaultAttr;
301  a3DDefaultAttr.SetDefaultLatheCharacterMode( true );
302  a3DDefaultAttr.SetDefaultExtrudeCharacterMode( true );
303 
304  E3dScene* pScene = new E3dScene(rSdrObjCustomShape.getSdrModelFromSdrObject());
305 
306  bool bSceneHasObjects ( false );
307  bool bUseTwoFillStyles( false );
308 
309  drawing::ShadeMode eShadeMode( GetShadeMode( rGeometryItem, drawing::ShadeMode_FLAT ) );
310  bool bUseExtrusionColor = GetBool( rGeometryItem, "Color", false );
311 
312  drawing::FillStyle eFillStyle( aSet.Get(XATTR_FILLSTYLE).GetValue() );
313  pScene->GetProperties().SetObjectItem( Svx3DShadeModeItem(static_cast<sal_uInt16>(eShadeMode)));
314  aSet.Put( makeSvx3DPercentDiagonalItem( 0 ) );
315  aSet.Put( Svx3DTextureModeItem( 1 ) );
316  // SPECIFIC needed for ShadeMode_SMOOTH and ShadeMode_PHONG, otherwise FLAT is faster.
317  if (eShadeMode == drawing::ShadeMode_SMOOTH || eShadeMode == drawing::ShadeMode_PHONG)
318  aSet.Put( Svx3DNormalsKindItem(static_cast<sal_uInt16>(drawing::NormalsKind_SPECIFIC)));
319  else
320  aSet.Put( Svx3DNormalsKindItem(static_cast<sal_uInt16>(drawing::NormalsKind_FLAT)));
321 
322  if ( eShadeMode == drawing::ShadeMode_DRAFT )
323  {
324  aSet.Put( XLineStyleItem( drawing::LineStyle_SOLID ) );
325  aSet.Put( XFillStyleItem ( drawing::FillStyle_NONE ) );
326  aSet.Put( makeSvx3DDoubleSidedItem( true ) );
327  }
328  else
329  {
330  aSet.Put( XLineStyleItem( drawing::LineStyle_NONE ) );
331  if ( eFillStyle == drawing::FillStyle_NONE )
332  aSet.Put( XFillStyleItem( drawing::FillStyle_SOLID ) );
333  else if ( ( eFillStyle == drawing::FillStyle_BITMAP ) || ( eFillStyle == drawing::FillStyle_GRADIENT ) || bUseExtrusionColor )
334  bUseTwoFillStyles = true;
335 
336  // If shapes are mirrored once (mirroring two times correct geometry again)
337  // double-sided at the object and two-sided-lighting at the scene need to be set.
338 
339  // #i122777# Also use double sided for two fill styles since there several 3d objects get
340  // created with a depth of 0; one of them is the backside which needs double-sided to
341  // get visible
342  if(bUseTwoFillStyles || (bIsMirroredX && !bIsMirroredY) || (!bIsMirroredX && bIsMirroredY))
343  {
344  aSet.Put( makeSvx3DDoubleSidedItem( true ) );
346  }
347  }
348 
349  tools::Rectangle aBoundRect2d;
350  basegfx::B2DPolyPolygon aTotalPolyPoly;
351  SdrObjListIter aIter( *pShape2d, SdrIterMode::DeepNoGroups );
352  const bool bMultipleSubObjects(aIter.Count() > 1);
353 
354  while( aIter.IsMore() )
355  {
356  const SdrObject* pNext = aIter.Next();
357  bool bIsPlaceholderObject = (pNext->GetMergedItem( XATTR_FILLSTYLE ).GetValue() == drawing::FillStyle_NONE )
358  && (pNext->GetMergedItem( XATTR_LINESTYLE ).GetValue() == drawing::LineStyle_NONE );
359  basegfx::B2DPolyPolygon aPolyPoly;
360  SfxItemSet aLocalSet(aSet);
361  drawing::FillStyle aLocalFillStyle(eFillStyle);
362 
363  if ( auto pPathObj = dynamic_cast<const SdrPathObj*>(pNext) )
364  {
365  const SfxItemSet& rSet = pNext->GetMergedItemSet();
366  bool bNeedToConvertToContour(false);
367 
368  // do conversion only for single line objects; for all others a fill and a
369  // line object get created. When we have fill, we want no line. That line has
370  // always been there, but since it was never converted to contour, it kept
371  // invisible (all this 'hidden' logic should be migrated to primitives).
372  if(!bMultipleSubObjects)
373  {
374  const drawing::FillStyle eStyle(rSet.Get(XATTR_FILLSTYLE).GetValue());
375 
376  if(drawing::FillStyle_NONE == eStyle)
377  {
380 
381  bNeedToConvertToContour = (0.0 < aLine.getWidth() || 0.0 != aLine.getFullDotDashLen());
382 
383  if(!bNeedToConvertToContour && !aLine.isDefault())
384  {
387 
388  if((aLineStartEnd.getStartWidth() && aLineStartEnd.isStartActive())
389  || (aLineStartEnd.getEndWidth() && aLineStartEnd.isEndActive()))
390  {
391  bNeedToConvertToContour = true;
392  }
393  }
394  }
395  }
396 
397  if(bNeedToConvertToContour)
398  {
399  SdrObject* pNewObj = pNext->ConvertToContourObj(const_cast< SdrObject* >(pNext));
400  SdrPathObj* pNewPathObj = dynamic_cast< SdrPathObj* >(pNewObj);
401 
402  if(pNewPathObj)
403  {
404  aPolyPoly = pNewPathObj->GetPathPoly();
405 
406  if(aPolyPoly.isClosed())
407  {
408  // correct item properties from line to fill style
409  if(eShadeMode == drawing::ShadeMode_DRAFT)
410  {
411  // for draft, create wireframe with fixed line width
412  aLocalSet.Put(XLineStyleItem(drawing::LineStyle_SOLID));
413  aLocalSet.Put(XLineWidthItem(40));
414  aLocalFillStyle = drawing::FillStyle_NONE;
415  }
416  else
417  {
418  // switch from line to fill, copy line attr to fill attr (color, transparence)
419  aLocalSet.Put(XLineWidthItem(0));
420  aLocalSet.Put(XLineStyleItem(drawing::LineStyle_NONE));
421  aLocalSet.Put(XFillColorItem(OUString(), aLocalSet.Get(XATTR_LINECOLOR).GetColorValue()));
422  aLocalSet.Put(XFillStyleItem(drawing::FillStyle_SOLID));
423  aLocalSet.Put(XFillTransparenceItem(aLocalSet.Get(XATTR_LINETRANSPARENCE).GetValue()));
424  aLocalFillStyle = drawing::FillStyle_SOLID;
425  }
426  }
427  else
428  {
429  // correct item properties to hairlines
430  aLocalSet.Put(XLineWidthItem(0));
431  aLocalSet.Put(XLineStyleItem(drawing::LineStyle_SOLID));
432  }
433  }
434 
435  SdrObject::Free(pNewObj);
436  }
437  else
438  {
439  aPolyPoly = pPathObj->GetPathPoly();
440  }
441  }
442  else
443  {
444  SdrObjectUniquePtr pNewObj = pNext->ConvertToPolyObj( false, false );
445  SdrPathObj* pPath = dynamic_cast<SdrPathObj*>( pNewObj.get() );
446  if ( pPath )
447  aPolyPoly = pPath->GetPathPoly();
448  }
449 
450  if( aPolyPoly.count() )
451  {
452  if(aPolyPoly.areControlPointsUsed())
453  {
454  aPolyPoly = basegfx::utils::adaptiveSubdivideByAngle(aPolyPoly);
455  }
456 
457  const basegfx::B2DRange aTempRange(basegfx::utils::getRange(aPolyPoly));
458  const tools::Rectangle aBoundRect(basegfx::fround(aTempRange.getMinX()), basegfx::fround(aTempRange.getMinY()), basegfx::fround(aTempRange.getMaxX()), basegfx::fround(aTempRange.getMaxY()));
459  aTotalPolyPoly.append(aPolyPoly);
460  aBoundRect2d.Union( aBoundRect );
461 
462  // #i122777# depth 0 is okay for planes when using double-sided
463  E3dCompoundObject* p3DObj = new E3dExtrudeObj(
464  rSdrObjCustomShape.getSdrModelFromSdrObject(),
465  a3DDefaultAttr,
466  aPolyPoly,
467  bUseTwoFillStyles ? 0 : fDepth );
468 
469  p3DObj->NbcSetLayer( pShape2d->GetLayer() );
470  p3DObj->SetMergedItemSet( aLocalSet );
471 
472  if ( bIsPlaceholderObject )
473  aPlaceholderObjectList.push_back( p3DObj );
474  else if ( bUseTwoFillStyles )
475  {
476  BitmapEx aFillBmp;
477  bool bFillBmpTile = p3DObj->GetMergedItem( XATTR_FILLBMP_TILE ).GetValue();
478  if ( bFillBmpTile )
479  {
480  const XFillBitmapItem& rBmpItm = p3DObj->GetMergedItem(XATTR_FILLBITMAP);
481  aFillBmp = rBmpItm.GetGraphicObject().GetGraphic().GetBitmapEx();
482 
483  // #i122777# old adaptation of FillStyle bitmap size to 5-times the original size; this is not needed
484  // anymore and was used in old times to male the fill look better when converting to 3D. Removed
485  // from regular 3D objects for some time, also needs to be removed from CustomShapes
486 
487  //Size aLogicalSize = aFillBmp.GetPrefSize();
488  //if ( aFillBmp.GetPrefMapMode() == MapUnit::MapPixel )
489  // aLogicalSize = Application::GetDefaultDevice()->PixelToLogic( aLogicalSize, MapUnit::Map100thMM );
490  //else
491  // aLogicalSize = OutputDevice::LogicToLogic( aLogicalSize, aFillBmp.GetPrefMapMode(), MapUnit::Map100thMM );
492  //aLogicalSize.Width() *= 5; ;// :-( nice scaling, look at engine3d/obj3d.cxx
493  //aLogicalSize.Height() *= 5;
494  //aFillBmp.SetPrefSize( aLogicalSize );
495  //aFillBmp.SetPrefMapMode( MapUnit::Map100thMM );
496  //p3DObj->SetMergedItem(XFillBitmapItem(String(), Graphic(aFillBmp)));
497  }
498  else
499  {
500  if ( aSnapRect != aBoundRect && aSnapRect.GetWidth() > 0 && aSnapRect.GetHeight() > 0)
501  {
502  const XFillBitmapItem& rBmpItm = p3DObj->GetMergedItem(XATTR_FILLBITMAP);
503  aFillBmp = rBmpItm.GetGraphicObject().GetGraphic().GetBitmapEx();
504  Size aBmpSize( aFillBmp.GetSizePixel() );
505  double fXScale = static_cast<double>(aBoundRect.GetWidth()) / static_cast<double>(aSnapRect.GetWidth());
506  double fYScale = static_cast<double>(aBoundRect.GetHeight()) / static_cast<double>(aSnapRect.GetHeight());
507 
508  Point aPt( static_cast<sal_Int32>( static_cast<double>( aBoundRect.Left() - aSnapRect.Left() )* static_cast<double>(aBmpSize.Width()) / static_cast<double>(aSnapRect.GetWidth()) ),
509  static_cast<sal_Int32>( static_cast<double>( aBoundRect.Top() - aSnapRect.Top() ) * static_cast<double>(aBmpSize.Height()) / static_cast<double>(aSnapRect.GetHeight()) ) );
510  Size aSize( static_cast<sal_Int32>( aBmpSize.Width() * fXScale ),
511  static_cast<sal_Int32>( aBmpSize.Height() * fYScale ) );
512  tools::Rectangle aCropRect( aPt, aSize );
513  aFillBmp.Crop( aCropRect );
514  p3DObj->SetMergedItem(XFillBitmapItem(OUString(), Graphic(aFillBmp)));
515  }
516  }
517  pScene->InsertObject( p3DObj );
518  p3DObj = new E3dExtrudeObj(
519  rSdrObjCustomShape.getSdrModelFromSdrObject(),
520  a3DDefaultAttr,
521  aPolyPoly,
522  fDepth);
523  p3DObj->NbcSetLayer( pShape2d->GetLayer() );
524  p3DObj->SetMergedItemSet( aLocalSet );
525  if ( bUseExtrusionColor )
526  p3DObj->SetMergedItem( XFillColorItem( "", rSdrObjCustomShape.GetMergedItem( XATTR_SECONDARYFILLCOLOR ).GetColorValue() ) );
527  p3DObj->SetMergedItem( XFillStyleItem( drawing::FillStyle_SOLID ) );
528  p3DObj->SetMergedItem( Svx3DCloseFrontItem( false ) );
529  p3DObj->SetMergedItem( Svx3DCloseBackItem( false ) );
530  pScene->InsertObject( p3DObj );
531 
532  // #i122777# depth 0 is okay for planes when using double-sided
533  p3DObj = new E3dExtrudeObj(
534  rSdrObjCustomShape.getSdrModelFromSdrObject(),
535  a3DDefaultAttr,
536  aPolyPoly,
537  0);
538 
539  p3DObj->NbcSetLayer( pShape2d->GetLayer() );
540  p3DObj->SetMergedItemSet( aLocalSet );
541 
542  basegfx::B3DHomMatrix aFrontTransform( p3DObj->GetTransform() );
543  aFrontTransform.translate( 0.0, 0.0, fDepth );
544  p3DObj->NbcSetTransform( aFrontTransform );
545 
546  if ( ( aLocalFillStyle == drawing::FillStyle_BITMAP ) && !aFillBmp.IsEmpty() )
547  {
548  p3DObj->SetMergedItem(XFillBitmapItem(OUString(), Graphic(aFillBmp)));
549  }
550  }
551  else if ( aLocalFillStyle == drawing::FillStyle_NONE )
552  {
553  const XLineColorItem& rLineColor = p3DObj->GetMergedItem( XATTR_LINECOLOR );
554  p3DObj->SetMergedItem( XFillColorItem( "", rLineColor.GetColorValue() ) );
555  p3DObj->SetMergedItem( makeSvx3DDoubleSidedItem( true ) );
556  p3DObj->SetMergedItem( Svx3DCloseFrontItem( false ) );
557  p3DObj->SetMergedItem( Svx3DCloseBackItem( false ) );
558  }
559  pScene->InsertObject( p3DObj );
560  bSceneHasObjects = true;
561  }
562  }
563 
564  if ( bSceneHasObjects ) // is the SdrObject properly converted
565  {
566  // then we can change the return value
567  pRet = pScene;
568 
569  // Camera settings, Perspective ...
570  Camera3D rCamera = pScene->GetCamera();
571  pScene->NbcSetSnapRect( aSnapRect );
572 
573  // InitScene replacement
574  double fW = aBoundRect2d.getWidth();
575  double fH = aBoundRect2d.getHeight();
576  rCamera.SetAutoAdjustProjection( false );
577  rCamera.SetViewWindow( -fW / 2, - fH / 2, fW, fH);
578  basegfx::B3DPoint aLookAt( 0.0, 0.0, 0.0 );
579  basegfx::B3DPoint aCamPos( 0.0, 0.0, 100.0 );
580  rCamera.SetPosAndLookAt( aCamPos, aLookAt );
581  rCamera.SetFocalLength( 1.0 );
582  ProjectionType eProjectionType( eProjectionMode == drawing::ProjectionMode_PARALLEL ? ProjectionType::Parallel : ProjectionType::Perspective );
583  rCamera.SetProjection( eProjectionType );
584  pScene->SetCamera( rCamera );
585  pScene->SetBoundAndSnapRectsDirty();
586 
587  basegfx::B3DHomMatrix aNewTransform( pScene->GetTransform() );
588  basegfx::B2DHomMatrix aPolyPolyTransform;
589  // Apply flip and z-rotation to scene transformation (y up). At same time transform
590  // aTotalPolyPoly (y down) which will be used for 2D boundRect of shape having 2D
591  // transformations applied.
592 
593  // API values use shape center as origin. Move scene so, that shape center is origin.
594  aNewTransform.translate( -aCenter.X(), aCenter.Y(), -fExtrusionBackward);
595  aPolyPolyTransform.translate(-aCenter.X(), -aCenter.Y());
596 
597  double fZRotate(basegfx::deg2rad(rSdrObjCustomShape.GetObjectRotation()));
598  if ( fZRotate != 0.0 )
599  {
600  aNewTransform.rotate( 0.0, 0.0, fZRotate );
601  aPolyPolyTransform.rotate(-fZRotate);
602  }
603  if ( bIsMirroredX )
604  {
605  aNewTransform.scale( -1.0, 1, 1 );
606  aPolyPolyTransform.scale(-1.0, 1);
607  }
608  if ( bIsMirroredY )
609  {
610  aNewTransform.scale( 1, -1.0, 1 );
611  aPolyPolyTransform.scale(1, -1.0);
612  }
613  aPolyPolyTransform.translate(aCenter.X(), aCenter.Y());
614  aTotalPolyPoly.transform(aPolyPolyTransform);
615 
616  // x- and y-rotation have an own rotation center. x- and y-value of rotation center are
617  // fractions of shape size, z-value is in Hmm in property. Shape center is (0 0 0).
618  // Values in property are in custom shape extrusion space with y-axis down.
619  double fXRotate, fYRotate;
620  GetRotateAngle( rGeometryItem, fXRotate, fYRotate );
621  drawing::Direction3D aRotationCenterDefault( 0, 0, 0 );
622  drawing::Direction3D aRotationCenter( GetDirection3D( rGeometryItem, "RotationCenter", aRotationCenterDefault ) );
623  aRotationCenter.DirectionX *= aSnapRect.getWidth();
624  aRotationCenter.DirectionY *= aSnapRect.getHeight();
625  if (pMap)
626  {
627  aRotationCenter.DirectionZ *= *pMap;
628  }
629  aNewTransform.translate( -aRotationCenter.DirectionX, aRotationCenter.DirectionY, -aRotationCenter.DirectionZ );
630  if( fYRotate != 0.0 )
631  aNewTransform.rotate( 0.0, -fYRotate, 0.0 );
632  if( fXRotate != 0.0 )
633  aNewTransform.rotate( -fXRotate, 0.0, 0.0 );
634  aNewTransform.translate(aRotationCenter.DirectionX, -aRotationCenter.DirectionY, aRotationCenter.DirectionZ);
635 
636  // oblique parallel projection is done by shearing the object, not by moving the camera
637  if (eProjectionMode == drawing::ProjectionMode_PARALLEL)
638  {
639  double fSkew, fAlpha;
640  GetSkew( rGeometryItem, fSkew, fAlpha );
641  if ( fSkew != 0.0 )
642  {
643  double fInvTanBeta( fSkew / 100.0 );
644  if(fInvTanBeta)
645  {
646  aNewTransform.shearXY(
647  fInvTanBeta * cos(fAlpha),
648  fInvTanBeta * sin(fAlpha));
649  }
650  }
651  }
652 
653  pScene->NbcSetTransform( aNewTransform );
654 
655  // These values are used later again, so declare them outside the if-statement. They will
656  // contain the absolute values of ViewPoint in 3D scene coordinate system, y-axis up.
657  double fViewPointX = 0; // dummy values
658  double fViewPointY = 0;
659  double fViewPointZ = 25000;
660  if (eProjectionMode == drawing::ProjectionMode_PERSPECTIVE)
661  {
662  double fOriginX, fOriginY;
663  // Calculate BoundRect of shape, including flip and z-rotation, from aTotalPolyPoly.
664  tools::Rectangle aBoundAfter2DTransform; // aBoundAfter2DTransform has y-axis down.
665  basegfx::B2DRange aTotalPolyPolyRange(aTotalPolyPoly.getB2DRange());
666  aBoundAfter2DTransform.SetLeft(aTotalPolyPolyRange.getMinX());
667  aBoundAfter2DTransform.SetTop(aTotalPolyPolyRange.getMinY());
668  aBoundAfter2DTransform.SetRight(aTotalPolyPolyRange.getMaxX());
669  aBoundAfter2DTransform.SetBottom(aTotalPolyPolyRange.getMaxY());
670 
671  // Property "Origin" in API is relative to bounding box of shape after 2D
672  // transformations. Range is [-0.5;0.5] with center of bounding box as 0.
673  // Resolve "Origin" fractions to length
674  GetOrigin( rGeometryItem, fOriginX, fOriginY );
675  fOriginX *= aBoundAfter2DTransform.GetWidth();
676  fOriginY *= aBoundAfter2DTransform.GetHeight();
677  // Resolve length to absolute value for 3D
678  fOriginX += aBoundAfter2DTransform.Center().X();
679  fOriginY += aBoundAfter2DTransform.Center().Y();
680  fOriginY = - fOriginY;
681  // Scene is translated so that shape center is origin of coordinate system.
682  // Translate point "Origin" too.
683  fOriginX -= aCenter.X();
684  fOriginY -= -aCenter.Y();
685  // API ViewPoint values are relative to point "Origin" and have y-axis down.
686  // ToDo: These default ViewPoint values are used as default by MS Office. But ODF
687  // default is (3500, -3500, 25000), details in tdf#146192.
688  drawing::Position3D aViewPointDefault( 3472, -3472, 25000 );
689  drawing::Position3D aViewPoint( GetPosition3D( rGeometryItem, "ViewPoint", aViewPointDefault, pMap ) );
690  fViewPointX = aViewPoint.PositionX + fOriginX;
691  fViewPointY = - aViewPoint.PositionY + fOriginY;
692  fViewPointZ = aViewPoint.PositionZ;
693  }
694 
695  // now set correct camera position
696  if (eProjectionMode == drawing::ProjectionMode_PARALLEL)
697  {
698  basegfx::B3DPoint _aLookAt( 0.0, 0.0, 0.0 );
699  basegfx::B3DPoint _aNewCamPos( 0.0, 0.0, 25000.0 );
700  rCamera.SetPosAndLookAt( _aNewCamPos, _aLookAt );
701  pScene->SetCamera( rCamera );
702  }
703  else
704  {
705  basegfx::B3DPoint _aLookAt(fViewPointX, fViewPointY, 0.0);
706  basegfx::B3DPoint aNewCamPos(fViewPointX, fViewPointY, fViewPointZ);
707  rCamera.SetPosAndLookAt( aNewCamPos, _aLookAt );
708  pScene->SetCamera( rCamera );
709  }
710 
711  // NbcSetTransform has not updated the scene 2D rectangles.
712  // Idea: Get a bound volume as polygon from bound rectangle of shape without 2D
713  // transformations. Calculate its projection to the XY-plane. Then calculate the bounding
714  // rectangle of the projection and convert this rectangle back to absolute 2D coordinates.
715  // Set that as 2D rectangle of the scene.
716  const tools::Polygon aPolygon(aBoundRect2d); // y-up
717  basegfx::B3DPolygon aPolygonBoundVolume; // y-down, scene coordinates
718  for (sal_uInt16 i = 0; i < 4; i++ )
719  {
720  aPolygonBoundVolume.append(basegfx::B3DPoint(aPolygon[i].X(), -aPolygon[i].Y(), 0));
721  }
722  for (sal_uInt16 i = 0; i < 4; i++ )
723  {
724  aPolygonBoundVolume.append(basegfx::B3DPoint(aPolygon[i].X(), -aPolygon[i].Y(), fDepth));
725  }
726  aPolygonBoundVolume.transform(aNewTransform);
727 
728  // projection
729  tools::Polygon a2DProjectionResult(8); // in fact 3D points with z=0
730  for (sal_uInt16 i = 0; i < 8; i++ )
731  {
732  const basegfx::B3DPoint aPoint3D(aPolygonBoundVolume.getB3DPoint(i));
733 
734  if (eProjectionMode == drawing::ProjectionMode_PARALLEL)
735  {
736  a2DProjectionResult[i].setX(aPoint3D.getX());
737  a2DProjectionResult[i].setY(aPoint3D.getY());
738  }
739  else
740  {
741  // skip point if line from viewpoint to point is parallel to xy-plane
742  if (double fDiv = aPoint3D.getZ() - fViewPointZ; fDiv != 0.0)
743  {
744  double f = (- fViewPointZ) / fDiv;
745  double fX = (aPoint3D.getX() - fViewPointX) * f + fViewPointX;
746  double fY = (aPoint3D.getY() - fViewPointY) * f + fViewPointY;;
747  a2DProjectionResult[i].setX(static_cast<sal_Int32>(fX));
748  a2DProjectionResult[i].setY(static_cast<sal_Int32>(fY));
749  }
750  }
751  }
752  // Convert to y-axis down
753  for (sal_uInt16 i = 0; i < 8; i++ )
754  {
755  a2DProjectionResult[i].setY(- a2DProjectionResult[i].Y());
756  }
757  // Shift back to shape center
758  a2DProjectionResult.Translate(aCenter);
759 
760  pScene->SetLogicRect(a2DProjectionResult.GetBoundRect());
761 
762 
763  // light and material
764 
765  // "LightFace" has nothing corresponding in 3D rendering engine.
766  /* bool bLightFace = */ GetBool(rGeometryItem, "LightFace", true); // default in ODF
767 
768  // Light directions
769 
770  drawing::Direction3D aFirstLightDirectionDefault(50000.0, 0.0, 10000.0);
771  drawing::Direction3D aFirstLightDirection(GetDirection3D( rGeometryItem, "FirstLightDirection", aFirstLightDirectionDefault));
772  if (aFirstLightDirection.DirectionX == 0.0 && aFirstLightDirection.DirectionY == 0.0
773  && aFirstLightDirection.DirectionZ == 0.0)
774  aFirstLightDirection.DirectionZ = 1.0;
775  basegfx::B3DVector aLight1Vector(aFirstLightDirection.DirectionX, -aFirstLightDirection.DirectionY, aFirstLightDirection.DirectionZ);
776  aLight1Vector.normalize();
777 
778  drawing::Direction3D aSecondLightDirectionDefault(-50000.0, 0.0, 10000.0);
779  drawing::Direction3D aSecondLightDirection(GetDirection3D( rGeometryItem, "SecondLightDirection", aSecondLightDirectionDefault));
780  if (aSecondLightDirection.DirectionX == 0.0 && aSecondLightDirection.DirectionY == 0.0
781  && aSecondLightDirection.DirectionZ == 0.0)
782  aSecondLightDirection.DirectionZ = 1.0;
783  basegfx::B3DVector aLight2Vector(aSecondLightDirection.DirectionX, -aSecondLightDirection.DirectionY, aSecondLightDirection.DirectionZ);
784  aLight2Vector.normalize();
785 
786  // Light Intensity
787 
788  // For "FirstLight" the 3D-Scene light "1" is regularly used. In case of surface "Matte"
789  // the light 4 is used instead. For "SecondLight" the 3D-Scene light "2" is regularly used.
790  // In case first or second light is not harsh, the lights 5 to 8 are used in addition
791  // to get a soft light appearance.
792  // The 3D-Scene light "3" is currently not used.
793 
794  // ODF default 66%. MS Office default 38000/65536=0.579 is set in import filter.
795  double fLight1Intensity = GetDouble(rGeometryItem, "FirstLightLevel", 66) / 100.0;
796  // ODF and MS Office have both default 'true'.
797  bool bFirstLightHarsh = GetBool(rGeometryItem, "FirstLightHarsh", true);
798  // ODF default 66%. MS Office default 38000/65536=0.579 is set in import filter
799  double fLight2Intensity = GetDouble(rGeometryItem, "SecondLightLevel", 66) / 100.0;
800  // ODF has default 'true'. MS Office default 'false' is set in import.
801  bool bSecondLightHarsh = GetBool(rGeometryItem, "SecondLightHarsh", true);
802 
803  // ODF default 33%. MS Office default 20000/65536=0.305 is set in import filter.
804  double fAmbientIntensity = GetDouble(rGeometryItem, "Brightness", 33) / 100.0;
805 
806  double fLight1IntensityForSpecular(fLight1Intensity); // remember original value
807  if (!bFirstLightHarsh || !bSecondLightHarsh) // might need softing lights
808  {
809  bool bNeedSoftLights(false); // catch case of lights with zero intensity.
810  basegfx::B3DVector aLight5Vector;
811  basegfx::B3DVector aLight6Vector;
812  basegfx::B3DVector aLight7Vector;
813  basegfx::B3DVector aLight8Vector;
814  // The needed light intensities depend on the angle between regular light and
815  // additional lights, currently for 60deg.
816  Color aHoriSoftLightColor;
817  Color aVertSoftLightColor;
818 
819  if (!bSecondLightHarsh && fLight2Intensity > 0.0
820  && (bFirstLightHarsh || fLight1Intensity == 0.0)) // only second light soft
821  {
822  // That is default for shapes generated in the UI, for LO and MS Office as well.
823  bNeedSoftLights = true;
824  double fLight2SoftIntensity = fLight2Intensity * 0.40;
825  aHoriSoftLightColor = Color(basegfx::BColor(fLight2SoftIntensity).clamp());
826  aVertSoftLightColor = aHoriSoftLightColor;
827  fLight2Intensity *= 0.2;
828 
829  lcl_SoftLightsDirection(aLight2Vector, aLight5Vector, aLight6Vector,
830  aLight7Vector, aLight8Vector);
831  }
832  else if (!bFirstLightHarsh && fLight1Intensity > 0.0
833  && (bSecondLightHarsh || fLight2Intensity == 0.0)) // only first light soft
834  {
835  bNeedSoftLights = true;
836  double fLight1SoftIntensity = fLight1Intensity * 0.40;
837  aHoriSoftLightColor = Color(basegfx::BColor(fLight1SoftIntensity).clamp());
838  aVertSoftLightColor = aHoriSoftLightColor;
839  fLight1Intensity *= 0.2;
840 
841  lcl_SoftLightsDirection(aLight1Vector, aLight5Vector, aLight6Vector,
842  aLight7Vector, aLight8Vector);
843  }
844  else if (!bFirstLightHarsh && fLight1Intensity > 0.0 && !bSecondLightHarsh
845  && fLight2Intensity > 0.0) // both lights soft
846  {
847  bNeedSoftLights = true;
848  // We do not hat enough lights. We use two soft lights for FirstLight and two for
849  // SecondLight and double intensity.
850  double fLight1SoftIntensity = fLight1Intensity * 0.8;
851  fLight1Intensity *= 0.4;
852  aHoriSoftLightColor = Color(basegfx::BColor(fLight1SoftIntensity).clamp());
853  basegfx::B3DVector aDummy1, aDummy2;
854  lcl_SoftLightsDirection(aLight1Vector, aDummy1, aDummy2, aLight7Vector,
855  aLight8Vector);
856 
857  double fLight2SoftIntensity = fLight2Intensity * 0.8;
858  aVertSoftLightColor = Color(basegfx::BColor(fLight2SoftIntensity).clamp());
859  fLight2Intensity *= 0.4;
860  lcl_SoftLightsDirection(aLight2Vector, aLight5Vector, aLight6Vector, aDummy1,
861  aDummy2);
862  }
863 
864  if (bNeedSoftLights)
865  {
866  pScene->GetProperties().SetObjectItem(
867  makeSvx3DLightDirection5Item(aLight5Vector));
868  pScene->GetProperties().SetObjectItem(
869  makeSvx3DLightcolor5Item(aVertSoftLightColor));
871  pScene->GetProperties().SetObjectItem(
872  makeSvx3DLightDirection6Item(aLight6Vector));
873  pScene->GetProperties().SetObjectItem(
874  makeSvx3DLightcolor6Item(aVertSoftLightColor));
876  pScene->GetProperties().SetObjectItem(
877  makeSvx3DLightDirection7Item(aLight7Vector));
878  pScene->GetProperties().SetObjectItem(
879  makeSvx3DLightcolor7Item(aHoriSoftLightColor));
881  pScene->GetProperties().SetObjectItem(
882  makeSvx3DLightDirection8Item(aLight8Vector));
883  pScene->GetProperties().SetObjectItem(
884  makeSvx3DLightcolor8Item(aHoriSoftLightColor));
886  }
887  }
888 
889  // ToDo: MSO seems to add half of the surplus to ambient color. ODF restricts value to <1.
890  if (fLight1Intensity > 1.0)
891  {
892  fAmbientIntensity += (fLight1Intensity - 1.0) / 2.0;
893  }
894 
895  // ToDo: How to handle fAmbientIntensity larger 1.0 ? Perhaps lighten object color?
896 
897  // Now set the regularly 3D-scene light attributes.
898  Color aAmbientColor(basegfx::BColor(fAmbientIntensity).clamp());
899  pScene->GetProperties().SetObjectItem(makeSvx3DAmbientcolorItem(aAmbientColor));
900 
901  pScene->GetProperties().SetObjectItem(makeSvx3DLightDirection1Item(aLight1Vector));
902  pScene->GetProperties().SetObjectItem(makeSvx3DLightOnOff1Item(fLight1Intensity > 0.0));
903  Color aLight1Color(basegfx::BColor(fLight1Intensity).clamp());
904  pScene->GetProperties().SetObjectItem(makeSvx3DLightcolor1Item(aLight1Color));
905 
906  pScene->GetProperties().SetObjectItem(makeSvx3DLightDirection2Item(aLight2Vector));
907  pScene->GetProperties().SetObjectItem(makeSvx3DLightOnOff2Item(fLight2Intensity > 0.0));
908  Color aLight2Color(basegfx::BColor(fLight2Intensity).clamp());
909  pScene->GetProperties().SetObjectItem(makeSvx3DLightcolor2Item(aLight2Color));
910 
911  // Object reactions on light
912  // Diffusion, Specular-Color and -Intensity are object properties, not scene properties.
913  // Surface flag "Metal" is an object property too.
914 
915  // Property "Diffusion" would correspond to style attribute "drd3:diffuse-color".
916  // But that is not implemented. We cannot ignore the attribute because MS Office sets
917  // attribute c3DDiffuseAmt to 43712 (Type Fixed 16.16, approx 66,9%) instead of MSO
918  // default 65536 (100%), if the user sets surface 'Metal' in the UI of MS Office.
919  // We will change the material color of the 3D object as ersatz.
920  // ODF data type is percent with default 0%. MSO default is set in import filter.
921  double fDiffusion = GetDouble(rGeometryItem, "Diffusion", 0.0) / 100.0;
922 
923  // ODF standard specifies for value true: "the specular color for the shading of an
924  // extruded shape is gray (red, green and blue values of 200) instead of white and 15% is
925  // added to the specularity."
926  // Neither 'specularity' nor 'specular color' is clearly defined in the standard. ODF term
927  // 'specularity' seems to correspond to UI field 'Specular Intensity' for 3D scenes.
928  // MS Office uses current material color in case 'Metal' is set. To detect, whether
929  // rendering similar to MS Office has to be used the property 'MetalType' is used. It is
930  // set on import and in the extrusion bar.
931  bool bMetal = GetBool(rGeometryItem, "Metal", false);
932  sal_Int16 eMetalType(
933  GetMetalType(rGeometryItem, drawing::EnhancedCustomShapeMetalType::MetalODF));
934  bool bMetalMSCompatible
935  = eMetalType == drawing::EnhancedCustomShapeMetalType::MetalMSCompatible;
936 
937  // Property "Specularity" corresponds to 3D object style attribute dr3d:specular-color.
938  double fSpecularity = GetDouble(rGeometryItem, "Specularity", 0) / 100.0;
939 
940  if (bMetal && !bMetalMSCompatible)
941  {
942  fSpecularity *= 200.0 / 255.0;
943  }
944 
945  // MS Office seems to render as if 'Specular Color' = Specularity * Light1Intensity.
946  double fShadingFactor = fLight1IntensityForSpecular * fSpecularity;
947  Color aSpecularCol(basegfx::BColor(fShadingFactor).clamp());
948  // In case of bMetalMSCompatible the color will be recalculated in the below loop.
949 
950  // Shininess ODF default 50 (unit %). MS Office default 5, import filter makes *10.
951  // Shininess corresponds to "Specular Intensity" with the nonlinear relationship
952  // "Specular Intensity" = 2^c3DShininess = 2^("Shininess" / 10)
953  double fShininess = GetDouble(rGeometryItem, "Shininess", 50) / 10.0;
954  fShininess = std::clamp<double>(pow(2, fShininess), 0.0, 100.0);
955  sal_uInt16 nIntensity = static_cast<sal_uInt16>(basegfx::fround(fShininess));
956  if (bMetal && !bMetalMSCompatible)
957  {
958  nIntensity += 15; // as specified in ODF
959  nIntensity = std::clamp<sal_uInt16>(nIntensity, 0, 100);
960  }
961 
962  SdrObjListIter aSceneIter(*pScene, SdrIterMode::DeepNoGroups);
963  while (aSceneIter.IsMore())
964  {
965  const SdrObject* pNext = aSceneIter.Next();
966 
967  // Change material color as ersatz for missing style attribute "drd3:diffuse-color".
968  // For this ersatz we exclude case fDiffusion == 0.0, because for older documents this
969  // attribute is not written out to draw:extrusion-diffusion and ODF default 0 would
970  // produce black objects.
971  const Color& rMatColor
972  = pNext->GetProperties().GetItem(XATTR_FILLCOLOR).GetColorValue();
973  Color aOldMatColor(rMatColor);
974  if (basegfx::fTools::more(fDiffusion, 0.0)
975  && !basegfx::fTools::equal(fDiffusion, 1.0))
976  {
977  // Occurs e.g. with MS surface preset 'Metal'.
978  sal_uInt16 nHue;
979  sal_uInt16 nSaturation;
980  sal_uInt16 nBrightness;
981  rMatColor.RGBtoHSB(nHue, nSaturation, nBrightness);
982  nBrightness
983  = static_cast<sal_uInt16>(static_cast<double>(nBrightness) * fDiffusion);
984  nBrightness = std::clamp<sal_uInt16>(nBrightness, 0, 100);
985  Color aNewMatColor = Color::HSBtoRGB(nHue, nSaturation, nBrightness);
986  pNext->GetProperties().SetObjectItem(XFillColorItem("", aNewMatColor));
987  }
988 
989  // Using material color instead of gray in case of MS Office compatible rendering.
990  if (bMetal && bMetalMSCompatible)
991  {
992  sal_uInt16 nHue;
993  sal_uInt16 nSaturation;
994  sal_uInt16 nBrightness;
995  aOldMatColor.RGBtoHSB(nHue, nSaturation, nBrightness);
996  nBrightness = static_cast<sal_uInt16>(static_cast<double>(nBrightness)
997  * fShadingFactor);
998  nBrightness = std::clamp<sal_uInt16>(nBrightness, 0, 100);
999  aSpecularCol = Color::HSBtoRGB(nHue, nSaturation, nBrightness);
1000  }
1001 
1003  pNext->GetProperties().SetObjectItem(
1005  }
1006 
1007  // fSpecularity = 0 is used to indicate surface preset "Matte".
1008  if (basegfx::fTools::equalZero(fSpecularity))
1009  {
1010  // First light in LO 3D engine is always specular, all other lights are never specular.
1011  // We copy light1 values to light4 and use it instead of light1 in the 3D scene.
1014  pScene->GetProperties().SetObjectItem(makeSvx3DLightcolor4Item(aLight1Color));
1015  pScene->GetProperties().SetObjectItem(makeSvx3DLightDirection4Item(aLight1Vector));
1016  }
1017 
1018  // removing placeholder objects
1019  for (E3dCompoundObject* pTemp : aPlaceholderObjectList)
1020  {
1021  pScene->RemoveObject( pTemp->GetOrdNum() );
1022  // always use SdrObject::Free(...) for SdrObjects (!)
1023  SdrObject* pTemp2(pTemp);
1024  SdrObject::Free(pTemp2);
1025  }
1026  }
1027  else
1028  {
1029  // always use SdrObject::Free(...) for SdrObjects (!)
1030  SdrObject* pTemp(pScene);
1031  SdrObject::Free(pTemp);
1032  }
1033  }
1034  return pRet;
1035 }
1036 
1037 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
void SetAutoAdjustProjection(bool bAdjust)
Definition: camera3d.hxx:62
virtual SdrObject * RemoveObject(size_t nObjNum) override
Definition: scene3d.cxx:791
SvxColorItem makeSvx3DLightcolor7Item(const Color &rCol)
Definition: svx3ditems.hxx:191
SfxBoolItem makeSvx3DLightOnOff2Item(bool bVal)
Definition: svx3ditems.hxx:207
SfxBoolItem makeSvx3DDoubleSidedItem(bool bVal)
Definition: svx3ditems.hxx:58
const SfxPoolItem & GetItem(const sal_uInt16 nWhich) const
Definition: properties.cxx:103
void SetViewWindow(double fX, double fY, double fW, double fH)
Definition: camera3d.cxx:41
tools::Long getWidth() const
enum SAL_DLLPUBLIC_RTTI FillStyle
virtual void SetBoundAndSnapRectsDirty(bool bNotMyself=false, bool bRecursive=true) override
Definition: scene3d.cxx:802
bool equalZero(const T &rfVal)
ProjectionType
Definition: viewpt3d.hxx:38
constexpr double deg2rad(double v)
constexpr tools::Long Left() const
const Fraction & GetScaleFraction() const
Definition: svdmodel.hxx:372
B3DVector & normalize()
long Long
constexpr TypedWhichId< XFillStyleItem > XATTR_FILLSTYLE(XATTR_FILL_FIRST)
constexpr Point convert(const Point &rPoint, o3tl::Length eFrom, o3tl::Length eTo)
constexpr TypedWhichId< XLineStyleItem > XATTR_LINESTYLE(XATTR_LINE_FIRST)
void Translate(const Point &rTrans)
SvxColorItem makeSvx3DLightcolor2Item(const Color &rCol)
Definition: svx3ditems.hxx:171
const basegfx::B3DHomMatrix & GetTransform() const
Definition: obj3d.hxx:112
static void Free(SdrObject *&_rpObject)
Definition: svdobj.cxx:471
virtual sdr::properties::BaseProperties & GetProperties() const
Definition: svdobj.cxx:210
virtual const tools::Rectangle & GetSnapRect() const override
Definition: svdoattr.cxx:49
SfxUInt16Item makeSvx3DMaterialSpecularIntensityItem(sal_uInt16 nVal)
Definition: svx3ditems.hxx:116
B2DRange getB2DRange() const
SfxBoolItem makeSvx3DLightOnOff7Item(bool bVal)
Definition: svx3ditems.hxx:227
tools::Long getHeight() const
bool more(const T &rfValA, const T &rfValB)
constexpr TypedWhichId< XLineTransparenceItem > XATTR_LINETRANSPARENCE(XATTR_LINE_FIRST+10)
SfxUInt16Item makeSvx3DPercentDiagonalItem(sal_uInt16 nVal)
Definition: svx3ditems.hxx:34
MapUnit GetScaleUnit() const
Definition: svdmodel.hxx:370
bool IsMore() const
Definition: svditer.hxx:62
virtual void NbcSetTransform(const basegfx::B3DHomMatrix &rMatrix) override
Definition: scene3d.cxx:590
bool IsMirroredX() const
Definition: svdoashp.cxx:471
SfxBoolItem makeSvx3DLightOnOff1Item(bool bVal)
Definition: svx3ditems.hxx:203
void SetCamera(const Camera3D &rNewCamera)
Definition: scene3d.cxx:343
attribute::SdrLineAttribute createNewSdrLineAttribute(const SfxItemSet &rSet)
SvxB3DVectorItem makeSvx3DLightDirection4Item(const basegfx::B3DVector &rVec)
Definition: svx3ditems.hxx:247
constexpr tools::Long GetWidth() const
void SetMergedItemSet(const SfxItemSet &rSet, bool bClearAllItems=false)
Definition: svdobj.cxx:1997
void append(const B3DPoint &rPoint, sal_uInt32 nCount=1)
static Color HSBtoRGB(sal_uInt16 nHue, sal_uInt16 nSaturation, sal_uInt16 nBrightness)
B2IRange fround(const B2DRange &rRange)
const Color & GetColorValue() const
Definition: xattr.cxx:300
SvxColorItem makeSvx3DMaterialSpecularItem(const Color &rCol)
Definition: svx3ditems.hxx:112
virtual void NbcSetTransform(const basegfx::B3DHomMatrix &rMatrix)
Definition: obj3d.cxx:353
const GraphicObject & GetGraphicObject() const
Definition: xbtmpit.hxx:56
constexpr void SetLeft(tools::Long v)
SvxB3DVectorItem makeSvx3DLightDirection6Item(const basegfx::B3DVector &rVec)
Definition: svx3ditems.hxx:255
bool IsEmpty() const
double getY() const
constexpr TypedWhichId< SvxWritingModeItem > SDRATTR_TEXTDIRECTION(SDRATTR_NOTPERSIST_FIRST+34)
B3DPoint const & getB3DPoint(sal_uInt32 nIndex) const
sal_uInt16 ClearItem(sal_uInt16 nWhich=0)
double getZ() const
virtual void SetObjectItem(const SfxPoolItem &rItem)=0
bool isClosed() const
#define DBG_ASSERT(sCon, aError)
constexpr TypedWhichId< SdrCustomShapeGeometryItem > SDRATTR_CUSTOMSHAPE_GEOMETRY(SDRATTR_CUSTOMSHAPE_FIRST+2)
int i
uno_Any a
double getX() const
constexpr TypedWhichId< XSecondaryFillColorItem > XATTR_SECONDARYFILLCOLOR(XATTR_FILL_FIRST+12)
bool equal(T const &rfValA, T const &rfValB)
constexpr TypedWhichId< XFillBitmapItem > XATTR_FILLBITMAP(XATTR_FILL_FIRST+4)
void SetFocalLength(double fLen)
Definition: camera3d.cxx:172
SdrModel & getSdrModelFromSdrObject() const
Definition: svdobj.cxx:279
void transform(const basegfx::B2DHomMatrix &rMatrix)
constexpr tools::Long Top() const
virtual void SetLogicRect(const tools::Rectangle &rRect)
Definition: svdobj.cxx:1694
size_t Count() const
Definition: svditer.hxx:69
double toRadians(D x)
constexpr void SetRight(tools::Long v)
void SetMergedItem(const SfxPoolItem &rItem)
Definition: svdobj.cxx:1982
constexpr void SetBottom(tools::Long v)
BitmapEx GetBitmapEx(const GraphicConversionParameters &rParameters=GraphicConversionParameters()) const
const SfxPoolItem & GetMergedItem(const sal_uInt16 nWhich) const
Definition: svdobj.cxx:2007
Abstract DrawObject.
Definition: svdobj.hxx:259
SvxB3DVectorItem makeSvx3DLightDirection5Item(const basegfx::B3DVector &rVec)
Definition: svx3ditems.hxx:251
constexpr void SetTop(tools::Long v)
attribute::SdrLineStartEndAttribute createNewSdrLineStartEndAttribute(const SfxItemSet &rSet, double fWidth)
#define Y
B2DRange getRange(const B2DPolygon &rCandidate)
SvxColorItem makeSvx3DLightcolor1Item(const Color &rCol)
Definition: svx3ditems.hxx:167
constexpr Point Center() const
virtual SdrLayerID GetLayer() const
Definition: svdobj.cxx:668
void SetPosAndLookAt(const basegfx::B3DPoint &rNewPos, const basegfx::B3DPoint &rNewLookAt)
Definition: camera3d.cxx:69
void translate(double fX, double fY, double fZ)
virtual Degree100 GetRotateAngle() const override
Definition: svdotxtr.cxx:84
SvxColorItem makeSvx3DLightcolor4Item(const Color &rCol)
Definition: svx3ditems.hxx:179
SvxColorItem makeSvx3DLightcolor8Item(const Color &rCol)
Definition: svx3ditems.hxx:195
void append(const B2DPolygon &rPolygon, sal_uInt32 nCount=1)
SvxColorItem makeSvx3DAmbientcolorItem(const Color &rCol)
Definition: svx3ditems.hxx:199
SdrObject * Next()
Definition: svditer.hxx:63
constexpr TypedWhichId< XFillColorItem > XATTR_FILLCOLOR(XATTR_FILL_FIRST+1)
const SfxPoolItem * Put(const SfxPoolItem &rItem, sal_uInt16 nWhich)
void RotatePoint(Point &rPnt, const Point &rRef, double sn, double cs)
Definition: svdtrans.hxx:101
SdrObjectUniquePtr ConvertToPolyObj(bool bBezier, bool bLineToArea) const
Definition: svdobj.cxx:2618
sal_uInt32 count() const
SfxBoolItem makeSvx3DLightOnOff4Item(bool bVal)
Definition: svx3ditems.hxx:215
static SdrObject * Create3DObject(const SdrObject *pShape2d, const SdrObjCustomShape &rSdrObjCustomShape)
B2DPolygon adaptiveSubdivideByAngle(const B2DPolygon &rCandidate, double fAngleBound)
const SfxPoolItem & Get(sal_uInt16 nWhich, bool bSrchInParent=true) const
const Graphic & GetGraphic() const
virtual void InsertObject(SdrObject *pObj, size_t nPos=SAL_MAX_SIZE) override
Definition: scene3d.cxx:761
tools::Rectangle & Union(const tools::Rectangle &rRect)
SvxB3DVectorItem makeSvx3DLightDirection2Item(const basegfx::B3DVector &rVec)
Definition: svx3ditems.hxx:239
double GetObjectRotation() const
Definition: svdoashp.hxx:144
SdrObject * ConvertToContourObj(SdrObject *pRet, bool bForceLineDash=false) const
Definition: svdobj.cxx:2580
eFillStyle
Definition: fillctrl.cxx:53
SvxB3DVectorItem makeSvx3DLightDirection1Item(const basegfx::B3DVector &rVec)
Definition: svx3ditems.hxx:235
void SetProjection(ProjectionType ePrj)
Definition: viewpt3d.hxx:80
const SfxItemSet & GetMergedItemSet() const
Definition: svdobj.cxx:1972
SfxBoolItem makeSvx3DLightOnOff8Item(bool bVal)
Definition: svx3ditems.hxx:231
constexpr TypedWhichId< XFillBmpTileItem > XATTR_FILLBMP_TILE(XATTR_FILL_FIRST+7)
std::unique_ptr< SdrObject, SdrObjectFreeOp > SdrObjectUniquePtr
Definition: svdobj.hxx:97
void translate(double fX, double fY)
const basegfx::B2DPolyPolygon & GetPathPoly() const
Definition: svdopath.hxx:141
css::uno::Any * GetPropertyValueByName(const OUString &rPropName)
SfxBoolItem makeSvx3DTwoSidedLightingItem(bool bVal)
Definition: svx3ditems.hxx:163
SvxB3DVectorItem makeSvx3DLightDirection8Item(const basegfx::B3DVector &rVec)
Definition: svx3ditems.hxx:263
void rotate(double fAngleX, double fAngleY, double fAngleZ)
SfxBoolItem makeSvx3DLightOnOff5Item(bool bVal)
Definition: svx3ditems.hxx:219
void(* f)(TrueTypeTable *)
void RGBtoHSB(sal_uInt16 &nHue, sal_uInt16 &nSaturation, sal_uInt16 &nBrightness) const
tools::Rectangle GetBoundRect() const
bool areControlPointsUsed() const
const Size & GetSizePixel() const
SvxColorItem makeSvx3DLightcolor6Item(const Color &rCol)
Definition: svx3ditems.hxx:187
void transform(const B3DHomMatrix &rMatrix)
constexpr TypedWhichId< XLineColorItem > XATTR_LINECOLOR(XATTR_LINE_FIRST+3)
bool IsMirroredY() const
Definition: svdoashp.cxx:480
bool Crop(const tools::Rectangle &rRectPixel)
const Camera3D & GetCamera() const
Definition: scene3d.hxx:127
SvxColorItem makeSvx3DLightcolor5Item(const Color &rCol)
Definition: svx3ditems.hxx:183
SfxBoolItem makeSvx3DLightOnOff6Item(bool bVal)
Definition: svx3ditems.hxx:223
virtual void NbcSetSnapRect(const tools::Rectangle &rRect) override
Definition: scene3d.cxx:315
constexpr tools::Long GetHeight() const
virtual const tools::Rectangle & GetLogicRect() const override
Definition: svdotxtr.cxx:69
SvxB3DVectorItem makeSvx3DLightDirection7Item(const basegfx::B3DVector &rVec)
Definition: svx3ditems.hxx:259
virtual void NbcSetLayer(SdrLayerID nLayer)
Definition: svdobj.cxx:685