1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #include <fumorph.hxx>
21 #include <svx/xlnclit.hxx>
22 #include <svx/xlnwtit.hxx>
23 #include <svx/xflclit.hxx>
24 #include <svx/svdopath.hxx>
25 #include <svx/svdogrp.hxx>
26 #include <editeng/eeitem.hxx>
27 #include <editeng/outlobj.hxx>
31 #include <basegfx/polygon/b2dpolygontools.hxx>
32 #include <basegfx/polygon/b2dpolypolygontools.hxx>
33 #include <basegfx/matrix/b2dhommatrix.hxx>
34 #include <basegfx/matrix/b2dhommatrixtools.hxx>
36 #include <strings.hrc>
37 #include <sdresid.hxx>
39 #include <sdabstdlg.hxx>
41 #include <svx/svditer.hxx>
43 #include <basegfx/color/bcolor.hxx>
44 #include <com/sun/star/drawing/LineStyle.hpp>
46 using namespace com::sun::star
;
56 : FuPoor(pViewSh
, pWin
, pView
, pDoc
, rReq
)
60 rtl::Reference
<FuPoor
> FuMorph::Create(
68 rtl::Reference
<FuPoor
> xFunc( new FuMorph( pViewSh
, pWin
, pView
, pDoc
, rReq
) );
69 xFunc
->DoExecute(rReq
);
73 void FuMorph::DoExecute( SfxRequest
& )
75 const SdrMarkList
& rMarkList
= mpView
->GetMarkedObjectList();
77 if(rMarkList
.GetMarkCount() != 2)
81 SdrObject
* pObj1
= rMarkList
.GetMark(0)->GetMarkedSdrObj();
82 SdrObject
* pObj2
= rMarkList
.GetMark(1)->GetMarkedSdrObj();
83 SdrObject
* pCloneObj1(pObj1
->CloneSdrObject(pObj1
->getSdrModelFromSdrObject()));
84 SdrObject
* pCloneObj2(pObj2
->CloneSdrObject(pObj2
->getSdrModelFromSdrObject()));
86 // delete text at clone, otherwise we do net get a correct PathObj
87 pCloneObj1
->SetOutlinerParaObject(nullptr);
88 pCloneObj2
->SetOutlinerParaObject(nullptr);
90 // create path objects
91 SdrObject
* pPolyObj1
= pCloneObj1
->ConvertToPolyObj(false, false);
92 SdrObject
* pPolyObj2
= pCloneObj2
->ConvertToPolyObj(false, false);
93 SdAbstractDialogFactory
* pFact
= SdAbstractDialogFactory::Create();
94 ScopedVclPtr
<AbstractMorphDlg
> pDlg( pFact
->CreateMorphDlg(mpWindow
? mpWindow
->GetFrameWeld() : nullptr, pObj1
, pObj2
) );
95 if(pPolyObj1
&& pPolyObj2
&& (pDlg
->Execute() == RET_OK
))
97 B2DPolyPolygonList_impl aPolyPolyList
;
98 ::basegfx::B2DPolyPolygon aPolyPoly1
;
99 ::basegfx::B2DPolyPolygon aPolyPoly2
;
101 pDlg
->SaveSettings();
103 // #i48168# Not always is the pPolyObj1/pPolyObj2 a SdrPathObj, it may also be a group object
104 // containing SdrPathObjs. To get the polygons, I add two iters here
105 SdrObjListIter
aIter1(*pPolyObj1
);
106 SdrObjListIter
aIter2(*pPolyObj2
);
108 while(aIter1
.IsMore())
110 SdrObject
* pObj
= aIter1
.Next();
111 if(auto pPathObj
= dynamic_cast< SdrPathObj
*>( pObj
))
112 aPolyPoly1
.append(pPathObj
->GetPathPoly());
115 while(aIter2
.IsMore())
117 SdrObject
* pObj
= aIter2
.Next();
118 if(auto pPathObj
= dynamic_cast< SdrPathObj
*>( pObj
))
119 aPolyPoly2
.append(pPathObj
->GetPathPoly());
123 if(aPolyPoly1
.count() && aPolyPoly2
.count())
125 aPolyPoly1
= ::basegfx::utils::correctOrientations(aPolyPoly1
);
126 aPolyPoly1
.removeDoublePoints();
127 ::basegfx::B2VectorOrientation
eIsClockwise1(::basegfx::utils::getOrientation(aPolyPoly1
.getB2DPolygon(0)));
129 aPolyPoly2
= ::basegfx::utils::correctOrientations(aPolyPoly2
);
130 aPolyPoly2
.removeDoublePoints();
131 ::basegfx::B2VectorOrientation
eIsClockwise2(::basegfx::utils::getOrientation(aPolyPoly2
.getB2DPolygon(0)));
133 // set same orientation
134 if(eIsClockwise1
!= eIsClockwise2
)
137 // force same poly count
138 if(aPolyPoly1
.count() < aPolyPoly2
.count())
139 ImpAddPolys(aPolyPoly1
, aPolyPoly2
);
140 else if(aPolyPoly2
.count() < aPolyPoly1
.count())
141 ImpAddPolys(aPolyPoly2
, aPolyPoly1
);
143 // use orientation flag from dialog
144 if(!pDlg
->IsOrientationFade())
147 // force same point counts
148 for( sal_uInt32
a(0); a
< aPolyPoly1
.count(); a
++ )
150 ::basegfx::B2DPolygon
aSub1(aPolyPoly1
.getB2DPolygon(a
));
151 ::basegfx::B2DPolygon
aSub2(aPolyPoly2
.getB2DPolygon(a
));
153 if(aSub1
.count() < aSub2
.count())
154 ImpEqualizePolyPointCount(aSub1
, aSub2
);
155 else if(aSub2
.count() < aSub1
.count())
156 ImpEqualizePolyPointCount(aSub2
, aSub1
);
158 aPolyPoly1
.setB2DPolygon(a
, aSub1
);
159 aPolyPoly2
.setB2DPolygon(a
, aSub2
);
162 ImpMorphPolygons(aPolyPoly1
, aPolyPoly2
, pDlg
->GetFadeSteps(), aPolyPolyList
);
164 OUString
aString(mpView
->GetDescriptionOfMarkedObjects());
165 aString
+= " " + SdResId(STR_UNDO_MORPHING
);
167 mpView
->BegUndo(aString
);
168 ImpInsertPolygons(aPolyPolyList
, pDlg
->IsAttributeFade(), pObj1
, pObj2
);
172 SdrObject::Free( pCloneObj1
);
173 SdrObject::Free( pCloneObj2
);
175 SdrObject::Free( pPolyObj1
);
176 SdrObject::Free( pPolyObj2
);
179 static ::basegfx::B2DPolygon
ImpGetExpandedPolygon(
180 const ::basegfx::B2DPolygon
& rCandidate
,
184 if(rCandidate
.count() && nNum
&& rCandidate
.count() != nNum
)
186 // length of step in dest poly
187 ::basegfx::B2DPolygon aRetval
;
188 const double fStep(::basegfx::utils::getLength(rCandidate
) / static_cast<double>(rCandidate
.isClosed() ? nNum
: nNum
- 1));
189 double fDestPos(0.0);
191 sal_uInt32
nSrcPos(0);
192 sal_uInt32
nSrcPosNext((nSrcPos
+ 1 == rCandidate
.count()) ? 0L : nSrcPos
+ 1);
193 double fNextSrcLen(::basegfx::B2DVector(rCandidate
.getB2DPoint(nSrcPos
) - rCandidate
.getB2DPoint(nSrcPosNext
)).getLength());
195 for(sal_uInt32
b(0); b
< nNum
; b
++)
197 // calc fDestPos in source
198 while(fSrcPos
+ fNextSrcLen
< fDestPos
)
200 fSrcPos
+= fNextSrcLen
;
202 nSrcPosNext
= (nSrcPos
+ 1 == rCandidate
.count()) ? 0L : nSrcPos
+ 1;
203 fNextSrcLen
= ::basegfx::B2DVector(rCandidate
.getB2DPoint(nSrcPos
) - rCandidate
.getB2DPoint(nSrcPosNext
)).getLength();
206 // fDestPos is between fSrcPos and (fSrcPos + fNextSrcLen)
207 const double fLenA((fDestPos
- fSrcPos
) / fNextSrcLen
);
208 const ::basegfx::B2DPoint
aOld1(rCandidate
.getB2DPoint(nSrcPos
));
209 const ::basegfx::B2DPoint
aOld2(rCandidate
.getB2DPoint(nSrcPosNext
));
210 ::basegfx::B2DPoint
aNewPoint(basegfx::interpolate(aOld1
, aOld2
, fLenA
));
211 aRetval
.append(aNewPoint
);
217 if(aRetval
.count() >= 3)
219 aRetval
.setClosed(rCandidate
.isClosed());
231 * make the point count of the polygons equal in adding points
233 void FuMorph::ImpEqualizePolyPointCount(
234 ::basegfx::B2DPolygon
& rSmall
,
235 const ::basegfx::B2DPolygon
& rBig
238 // create poly with equal point count
239 const sal_uInt32
nCnt(rBig
.count());
240 ::basegfx::B2DPolygon
aPoly1(ImpGetExpandedPolygon(rSmall
, nCnt
));
242 // create transformation for rBig to do the compare
243 const ::basegfx::B2DRange
aSrcSize(::basegfx::utils::getRange(rBig
));
244 const ::basegfx::B2DPoint
aSrcPos(aSrcSize
.getCenter());
245 const ::basegfx::B2DRange
aDstSize(::basegfx::utils::getRange(rSmall
));
246 const ::basegfx::B2DPoint
aDstPos(aDstSize
.getCenter());
248 basegfx::B2DHomMatrix
aTrans(basegfx::utils::createTranslateB2DHomMatrix(-aSrcPos
.getX(), -aSrcPos
.getY()));
249 aTrans
.scale(aDstSize
.getWidth() / aSrcSize
.getWidth(), aDstSize
.getHeight() / aSrcSize
.getHeight());
250 aTrans
.translate(aDstPos
.getX(), aDstPos
.getY());
252 // transpose points to have smooth linear blending
253 ::basegfx::B2DPolygon aPoly2
;
254 aPoly2
.append(::basegfx::B2DPoint(), nCnt
);
255 sal_uInt32
nInd(ImpGetNearestIndex(aPoly1
, aTrans
* rBig
.getB2DPoint(0)));
257 for(sal_uInt32
a(0); a
< nCnt
; a
++)
259 aPoly2
.setB2DPoint((a
+ nCnt
- nInd
) % nCnt
, aPoly1
.getB2DPoint(a
));
262 aPoly2
.setClosed(rBig
.isClosed());
266 sal_uInt32
FuMorph::ImpGetNearestIndex(
267 const ::basegfx::B2DPolygon
& rPoly
,
268 const ::basegfx::B2DPoint
& rPos
271 double fMinDist
= 0.0;
272 sal_uInt32 nActInd
= 0;
274 for(sal_uInt32
a(0); a
< rPoly
.count(); a
++)
276 double fNewDist(::basegfx::B2DVector(rPoly
.getB2DPoint(a
) - rPos
).getLength());
278 if(!a
|| fNewDist
< fMinDist
)
289 * add to a point reduced polys until count is same
291 void FuMorph::ImpAddPolys(
292 ::basegfx::B2DPolyPolygon
& rSmaller
,
293 const ::basegfx::B2DPolyPolygon
& rBigger
296 while(rSmaller
.count() < rBigger
.count())
298 const ::basegfx::B2DPolygon
& aToBeCopied(rBigger
.getB2DPolygon(rSmaller
.count()));
299 const ::basegfx::B2DRange
aToBeCopiedPolySize(::basegfx::utils::getRange(aToBeCopied
));
300 ::basegfx::B2DPoint
aNewPoint(aToBeCopiedPolySize
.getCenter());
301 ::basegfx::B2DPolygon aNewPoly
;
303 const ::basegfx::B2DRange
aSrcSize(::basegfx::utils::getRange(rBigger
.getB2DPolygon(0)));
304 const ::basegfx::B2DPoint
aSrcPos(aSrcSize
.getCenter());
305 const ::basegfx::B2DRange
aDstSize(::basegfx::utils::getRange(rSmaller
.getB2DPolygon(0)));
306 const ::basegfx::B2DPoint
aDstPos(aDstSize
.getCenter());
307 aNewPoint
= aNewPoint
- aSrcPos
+ aDstPos
;
309 for(sal_uInt32
a(0); a
< aToBeCopied
.count(); a
++)
311 aNewPoly
.append(aNewPoint
);
314 rSmaller
.append(aNewPoly
);
319 * create group object with morphed polygons
321 void FuMorph::ImpInsertPolygons(
322 B2DPolyPolygonList_impl
& rPolyPolyList3D
,
324 const SdrObject
* pObj1
,
325 const SdrObject
* pObj2
332 long nStartLineWidth
= 0;
333 long nEndLineWidth
= 0;
334 SdrPageView
* pPageView
= mpView
->GetSdrPageView();
335 SfxItemPool
& rPool
= pObj1
->GetObjectItemPool();
336 SfxItemSet
aSet1( rPool
,svl::Items
<SDRATTR_START
,SDRATTR_NOTPERSIST_FIRST
-1,EE_ITEMS_START
,EE_ITEMS_END
>{} );
337 SfxItemSet
aSet2( aSet1
);
338 bool bLineColor
= false;
339 bool bFillColor
= false;
340 bool bLineWidth
= false;
341 bool bIgnoreLine
= false;
342 bool bIgnoreFill
= false;
344 aSet1
.Put(pObj1
->GetMergedItemSet());
345 aSet2
.Put(pObj2
->GetMergedItemSet());
347 const drawing::LineStyle eLineStyle1
= aSet1
.Get(XATTR_LINESTYLE
).GetValue();
348 const drawing::LineStyle eLineStyle2
= aSet2
.Get(XATTR_LINESTYLE
).GetValue();
349 const drawing::FillStyle eFillStyle1
= aSet1
.Get(XATTR_FILLSTYLE
).GetValue();
350 const drawing::FillStyle eFillStyle2
= aSet2
.Get(XATTR_FILLSTYLE
).GetValue();
352 if ( bAttributeFade
)
354 if ( ( eLineStyle1
!= drawing::LineStyle_NONE
) && ( eLineStyle2
!= drawing::LineStyle_NONE
) )
356 bLineWidth
= bLineColor
= true;
358 aStartLineCol
= aSet1
.Get(XATTR_LINECOLOR
).GetColorValue();
359 aEndLineCol
= aSet2
.Get(XATTR_LINECOLOR
).GetColorValue();
361 nStartLineWidth
= aSet1
.Get(XATTR_LINEWIDTH
).GetValue();
362 nEndLineWidth
= aSet2
.Get(XATTR_LINEWIDTH
).GetValue();
364 else if ( ( eLineStyle1
== drawing::LineStyle_NONE
) && ( eLineStyle2
== drawing::LineStyle_NONE
) )
367 if ( ( eFillStyle1
== drawing::FillStyle_SOLID
) && ( eFillStyle2
== drawing::FillStyle_SOLID
) )
370 aStartFillCol
= aSet1
.Get(XATTR_FILLCOLOR
).GetColorValue();
371 aEndFillCol
= aSet2
.Get(XATTR_FILLCOLOR
).GetColorValue();
373 else if ( ( eFillStyle1
== drawing::FillStyle_NONE
) && ( eFillStyle2
== drawing::FillStyle_NONE
) )
380 SfxItemSet
aSet( aSet1
);
381 SdrObjGroup
* pObjGroup
= new SdrObjGroup(mpView
->getSdrModelFromSdrView());
382 SdrObjList
* pObjList
= pObjGroup
->GetSubList();
383 const size_t nCount
= rPolyPolyList3D
.size();
384 const double fStep
= 1. / ( nCount
+ 1 );
385 const double fDelta
= nEndLineWidth
- nStartLineWidth
;
386 double fFactor
= fStep
;
388 aSet
.Put( XLineStyleItem( drawing::LineStyle_SOLID
) );
389 aSet
.Put( XFillStyleItem( drawing::FillStyle_SOLID
) );
391 for ( size_t i
= 0; i
< nCount
; i
++, fFactor
+= fStep
)
393 const ::basegfx::B2DPolyPolygon
& rPolyPoly3D
= rPolyPolyList3D
[ i
];
394 SdrPathObj
* pNewObj
= new SdrPathObj(
395 mpView
->getSdrModelFromSdrView(),
402 const basegfx::BColor
aLineColor(basegfx::interpolate(aStartLineCol
.getBColor(), aEndLineCol
.getBColor(), fFactor
));
403 aSet
.Put( XLineColorItem( "", Color(aLineColor
)));
405 else if ( bIgnoreLine
)
406 aSet
.Put( XLineStyleItem( drawing::LineStyle_NONE
) );
411 const basegfx::BColor
aFillColor(basegfx::interpolate(aStartFillCol
.getBColor(), aEndFillCol
.getBColor(), fFactor
));
412 aSet
.Put( XFillColorItem( "", Color(aFillColor
)));
414 else if ( bIgnoreFill
)
415 aSet
.Put( XFillStyleItem( drawing::FillStyle_NONE
) );
419 aSet
.Put( XLineWidthItem( nStartLineWidth
+ static_cast<long>( fFactor
* fDelta
+ 0.5 ) ) );
421 pNewObj
->SetMergedItemSetAndBroadcast(aSet
);
423 pObjList
->InsertObject( pNewObj
);
428 pObjList
->InsertObject(
429 pObj1
->CloneSdrObject(pObj1
->getSdrModelFromSdrObject()),
431 pObjList
->InsertObject(
432 pObj2
->CloneSdrObject(pObj2
->getSdrModelFromSdrObject()) );
434 mpView
->DeleteMarked();
435 mpView
->InsertObjectAtView( pObjGroup
, *pPageView
, SdrInsertFlags:: SETDEFLAYER
);
440 * create single morphed PolyPolygon
442 ::basegfx::B2DPolyPolygon
FuMorph::ImpCreateMorphedPolygon(
443 const ::basegfx::B2DPolyPolygon
& rPolyPolyStart
,
444 const ::basegfx::B2DPolyPolygon
& rPolyPolyEnd
,
445 double fMorphingFactor
448 ::basegfx::B2DPolyPolygon aNewPolyPolygon
;
449 const double fFactor
= 1.0 - fMorphingFactor
;
451 for(sal_uInt32
a(0); a
< rPolyPolyStart
.count(); a
++)
453 const ::basegfx::B2DPolygon
& aPolyStart(rPolyPolyStart
.getB2DPolygon(a
));
454 const ::basegfx::B2DPolygon
& aPolyEnd(rPolyPolyEnd
.getB2DPolygon(a
));
455 const sal_uInt32
nCount(aPolyStart
.count());
456 ::basegfx::B2DPolygon aNewPolygon
;
458 for(sal_uInt32
b(0); b
< nCount
; b
++)
460 const ::basegfx::B2DPoint
& aPtStart(aPolyStart
.getB2DPoint(b
));
461 const ::basegfx::B2DPoint
& aPtEnd(aPolyEnd
.getB2DPoint(b
));
462 aNewPolygon
.append(aPtEnd
+ ((aPtStart
- aPtEnd
) * fFactor
));
465 aNewPolygon
.setClosed(aPolyStart
.isClosed() && aPolyEnd
.isClosed());
466 aNewPolyPolygon
.append(aNewPolygon
);
469 return aNewPolyPolygon
;
473 * create morphed PolyPolygons
475 void FuMorph::ImpMorphPolygons(
476 const ::basegfx::B2DPolyPolygon
& rPolyPoly1
,
477 const ::basegfx::B2DPolyPolygon
& rPolyPoly2
,
478 const sal_uInt16 nSteps
,
479 B2DPolyPolygonList_impl
& rPolyPolyList3D
485 const ::basegfx::B2DRange
aStartPolySize(::basegfx::utils::getRange(rPolyPoly1
));
486 const ::basegfx::B2DPoint
aStartCenter(aStartPolySize
.getCenter());
487 const ::basegfx::B2DRange
aEndPolySize(::basegfx::utils::getRange(rPolyPoly2
));
488 const ::basegfx::B2DPoint
aEndCenter(aEndPolySize
.getCenter());
489 const ::basegfx::B2DPoint
aDelta(aEndCenter
- aStartCenter
);
490 const double fFactor(1.0 / (nSteps
+ 1));
493 for(sal_uInt16
i(0); i
< nSteps
; i
++)
496 ::basegfx::B2DPolyPolygon aNewPolyPoly2D
= ImpCreateMorphedPolygon(rPolyPoly1
, rPolyPoly2
, fValue
);
498 const ::basegfx::B2DRange
aNewPolySize(::basegfx::utils::getRange(aNewPolyPoly2D
));
499 const ::basegfx::B2DPoint
aNewS(aNewPolySize
.getCenter());
500 const ::basegfx::B2DPoint
aRealS(aStartCenter
+ (aDelta
* fValue
));
501 const ::basegfx::B2DPoint
aDiff(aRealS
- aNewS
);
503 aNewPolyPoly2D
.transform(basegfx::utils::createTranslateB2DHomMatrix(aDiff
));
504 rPolyPolyList3D
.push_back( std::move(aNewPolyPoly2D
) );
508 } // end of namespace sd
510 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */