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 .
21 #include <basegfx/polygon/b2dpolygon.hxx>
22 #include <basegfx/polygon/b2dpolygontools.hxx>
24 #include <svx/polypolygoneditor.hxx>
29 PolyPolygonEditor::PolyPolygonEditor( basegfx::B2DPolyPolygon aPolyPolygon
)
30 : maPolyPolygon(std::move( aPolyPolygon
))
34 bool PolyPolygonEditor::DeletePoints( const o3tl::sorted_vector
< sal_uInt16
>& rAbsPoints
)
36 bool bPolyPolyChanged
= false;
38 auto aIter( rAbsPoints
.rbegin() );
39 for( ; aIter
!= rAbsPoints
.rend(); ++aIter
)
41 sal_uInt32 nPoly
, nPnt
;
42 if( GetRelativePolyPoint(maPolyPolygon
,(*aIter
), nPoly
, nPnt
) )
45 basegfx::B2DPolygon
aCandidate(maPolyPolygon
.getB2DPolygon(nPoly
));
47 aCandidate
.remove(nPnt
);
50 if( aCandidate
.count() < 2 )
52 maPolyPolygon
.remove(nPoly
);
56 maPolyPolygon
.setB2DPolygon(nPoly
, aCandidate
);
59 bPolyPolyChanged
= true;
63 return bPolyPolyChanged
;
66 bool PolyPolygonEditor::SetSegmentsKind(SdrPathSegmentKind eKind
, const o3tl::sorted_vector
< sal_uInt16
>& rAbsPoints
)
68 bool bPolyPolyChanged
= false;
70 auto aIter( rAbsPoints
.rbegin() );
71 for( ; aIter
!= rAbsPoints
.rend(); ++aIter
)
73 sal_uInt32 nPolyNum
, nPntNum
;
75 if(PolyPolygonEditor::GetRelativePolyPoint(maPolyPolygon
, (*aIter
), nPolyNum
, nPntNum
))
77 // do change at aNewPolyPolygon. Take a look at edge.
78 basegfx::B2DPolygon
aCandidate(maPolyPolygon
.getB2DPolygon(nPolyNum
));
79 const sal_uInt32
nCount(aCandidate
.count());
81 if(nCount
&& (nPntNum
+ 1 < nCount
|| aCandidate
.isClosed()))
83 bool bCandidateChanged(false);
85 // it's a valid edge, check control point usage
86 const sal_uInt32
nNextIndex((nPntNum
+ 1) % nCount
);
87 const bool bContolUsed(aCandidate
.areControlPointsUsed()
88 && (aCandidate
.isNextControlPointUsed(nPntNum
) || aCandidate
.isPrevControlPointUsed(nNextIndex
)));
92 if(SdrPathSegmentKind::Toggle
== eKind
|| SdrPathSegmentKind::Line
== eKind
)
95 aCandidate
.resetNextControlPoint(nPntNum
);
96 aCandidate
.resetPrevControlPoint(nNextIndex
);
97 bCandidateChanged
= true;
102 if(SdrPathSegmentKind::Toggle
== eKind
|| SdrPathSegmentKind::Curve
== eKind
)
105 const basegfx::B2DPoint
aStart(aCandidate
.getB2DPoint(nPntNum
));
106 const basegfx::B2DPoint
aEnd(aCandidate
.getB2DPoint(nNextIndex
));
108 aCandidate
.setNextControlPoint(nPntNum
, interpolate(aStart
, aEnd
, (1.0 / 3.0)));
109 aCandidate
.setPrevControlPoint(nNextIndex
, interpolate(aStart
, aEnd
, (2.0 / 3.0)));
110 bCandidateChanged
= true;
114 if(bCandidateChanged
)
116 maPolyPolygon
.setB2DPolygon(nPolyNum
, aCandidate
);
117 bPolyPolyChanged
= true;
123 return bPolyPolyChanged
;
126 bool PolyPolygonEditor::SetPointsSmooth( basegfx::B2VectorContinuity eFlags
, const o3tl::sorted_vector
< sal_uInt16
>& rAbsPoints
)
128 bool bPolyPolygonChanged(false);
130 auto aIter( rAbsPoints
.rbegin() );
131 for( ; aIter
!= rAbsPoints
.rend(); ++aIter
)
133 sal_uInt32 nPolyNum
, nPntNum
;
135 if(PolyPolygonEditor::GetRelativePolyPoint(maPolyPolygon
, (*aIter
), nPolyNum
, nPntNum
))
137 // do change at aNewPolyPolygon...
138 basegfx::B2DPolygon
aCandidate(maPolyPolygon
.getB2DPolygon(nPolyNum
));
140 // set continuity in point, make sure there is a curve
141 bool bPolygonChanged
= basegfx::utils::expandToCurveInPoint(aCandidate
, nPntNum
);
142 bPolygonChanged
|= basegfx::utils::setContinuityInPoint(aCandidate
, nPntNum
, eFlags
);
146 maPolyPolygon
.setB2DPolygon(nPolyNum
, aCandidate
);
147 bPolyPolygonChanged
= true;
152 return bPolyPolygonChanged
;
155 bool PolyPolygonEditor::GetRelativePolyPoint( const basegfx::B2DPolyPolygon
& rPoly
, sal_uInt32 nAbsPnt
, sal_uInt32
& rPolyNum
, sal_uInt32
& rPointNum
)
157 const sal_uInt32
nPolyCount(rPoly
.count());
158 sal_uInt32
nPolyNum(0);
160 while(nPolyNum
< nPolyCount
)
162 const sal_uInt32
nPointCount(rPoly
.getB2DPolygon(nPolyNum
).count());
164 if(nAbsPnt
< nPointCount
)
174 nAbsPnt
-= nPointCount
;
181 } // end of namespace sdr
183 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */