Line data Source code
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 :
21 : #include <basegfx/polygon/b2dpolygon.hxx>
22 : #include <basegfx/polygon/b2dpolygontools.hxx>
23 :
24 : #include "svx/polypolygoneditor.hxx"
25 :
26 : namespace sdr {
27 :
28 0 : PolyPolygonEditor::PolyPolygonEditor( const basegfx::B2DPolyPolygon& rPolyPolygon, bool bClosed )
29 : : maPolyPolygon( rPolyPolygon )
30 0 : , mbIsClosed( bClosed )
31 : {
32 0 : }
33 :
34 0 : bool PolyPolygonEditor::DeletePoints( const std::set< sal_uInt16 >& rAbsPoints )
35 : {
36 0 : bool bPolyPolyChanged = false;
37 :
38 0 : std::set< sal_uInt16 >::const_reverse_iterator aIter( rAbsPoints.rbegin() );
39 0 : for( ; aIter != rAbsPoints.rend(); ++aIter )
40 : {
41 : sal_uInt32 nPoly, nPnt;
42 0 : if( GetRelativePolyPoint(maPolyPolygon,(*aIter), nPoly, nPnt) )
43 : {
44 : // remove point
45 0 : basegfx::B2DPolygon aCandidate(maPolyPolygon.getB2DPolygon(nPoly));
46 :
47 0 : aCandidate.remove(nPnt);
48 :
49 0 : if( ( mbIsClosed && aCandidate.count() < 3L) || (aCandidate.count() < 2L) )
50 : {
51 0 : maPolyPolygon.remove(nPoly);
52 : }
53 : else
54 : {
55 0 : maPolyPolygon.setB2DPolygon(nPoly, aCandidate);
56 : }
57 :
58 0 : bPolyPolyChanged = true;
59 : }
60 : }
61 :
62 0 : return bPolyPolyChanged;
63 : }
64 :
65 0 : bool PolyPolygonEditor::SetSegmentsKind(SdrPathSegmentKind eKind, const std::set< sal_uInt16 >& rAbsPoints )
66 : {
67 0 : bool bPolyPolyChanged = false;
68 :
69 0 : std::set< sal_uInt16 >::const_reverse_iterator aIter( rAbsPoints.rbegin() );
70 0 : for( ; aIter != rAbsPoints.rend(); ++aIter )
71 : {
72 : sal_uInt32 nPolyNum, nPntNum;
73 :
74 0 : if(PolyPolygonEditor::GetRelativePolyPoint(maPolyPolygon, (*aIter), nPolyNum, nPntNum))
75 : {
76 : // do change at aNewPolyPolygon. Take a look at edge.
77 0 : basegfx::B2DPolygon aCandidate(maPolyPolygon.getB2DPolygon(nPolyNum));
78 0 : const sal_uInt32 nCount(aCandidate.count());
79 :
80 0 : if(nCount && (nPntNum + 1 < nCount || aCandidate.isClosed()))
81 : {
82 0 : bool bCandidateChanged(false);
83 :
84 : // it's a valid edge, check control point usage
85 0 : const sal_uInt32 nNextIndex((nPntNum + 1) % nCount);
86 0 : const bool bContolUsed(aCandidate.areControlPointsUsed()
87 0 : && (aCandidate.isNextControlPointUsed(nPntNum) || aCandidate.isPrevControlPointUsed(nNextIndex)));
88 :
89 0 : if(bContolUsed)
90 : {
91 0 : if(SDRPATHSEGMENT_TOGGLE == eKind || SDRPATHSEGMENT_LINE == eKind)
92 : {
93 : // remove control
94 0 : aCandidate.resetNextControlPoint(nPntNum);
95 0 : aCandidate.resetPrevControlPoint(nNextIndex);
96 0 : bCandidateChanged = true;
97 : }
98 : }
99 : else
100 : {
101 0 : if(SDRPATHSEGMENT_TOGGLE == eKind || SDRPATHSEGMENT_CURVE == eKind)
102 : {
103 : // add control
104 0 : const basegfx::B2DPoint aStart(aCandidate.getB2DPoint(nPntNum));
105 0 : const basegfx::B2DPoint aEnd(aCandidate.getB2DPoint(nNextIndex));
106 :
107 0 : aCandidate.setNextControlPoint(nPntNum, interpolate(aStart, aEnd, (1.0 / 3.0)));
108 0 : aCandidate.setPrevControlPoint(nNextIndex, interpolate(aStart, aEnd, (2.0 / 3.0)));
109 0 : bCandidateChanged = true;
110 : }
111 : }
112 :
113 0 : if(bCandidateChanged)
114 : {
115 0 : maPolyPolygon.setB2DPolygon(nPolyNum, aCandidate);
116 0 : bPolyPolyChanged = true;
117 : }
118 0 : }
119 : }
120 : }
121 :
122 0 : return bPolyPolyChanged;
123 : }
124 :
125 0 : bool PolyPolygonEditor::SetPointsSmooth( basegfx::B2VectorContinuity eFlags, const std::set< sal_uInt16 >& rAbsPoints)
126 : {
127 0 : bool bPolyPolygonChanged(false);
128 :
129 0 : std::set< sal_uInt16 >::const_reverse_iterator aIter( rAbsPoints.rbegin() );
130 0 : for( ; aIter != rAbsPoints.rend(); ++aIter )
131 : {
132 : sal_uInt32 nPolyNum, nPntNum;
133 :
134 0 : if(PolyPolygonEditor::GetRelativePolyPoint(maPolyPolygon, (*aIter), nPolyNum, nPntNum))
135 : {
136 : // do change at aNewPolyPolygon...
137 0 : basegfx::B2DPolygon aCandidate(maPolyPolygon.getB2DPolygon(nPolyNum));
138 :
139 : // set continuity in point, make sure there is a curve
140 0 : bool bPolygonChanged(false);
141 0 : bPolygonChanged = basegfx::tools::expandToCurveInPoint(aCandidate, nPntNum);
142 0 : bPolygonChanged |= basegfx::tools::setContinuityInPoint(aCandidate, nPntNum, eFlags);
143 :
144 0 : if(bPolygonChanged)
145 : {
146 0 : maPolyPolygon.setB2DPolygon(nPolyNum, aCandidate);
147 0 : bPolyPolygonChanged = true;
148 0 : }
149 : }
150 : }
151 :
152 0 : return bPolyPolygonChanged;
153 : }
154 :
155 664 : bool PolyPolygonEditor::GetRelativePolyPoint( const basegfx::B2DPolyPolygon& rPoly, sal_uInt32 nAbsPnt, sal_uInt32& rPolyNum, sal_uInt32& rPointNum )
156 : {
157 664 : const sal_uInt32 nPolyCount(rPoly.count());
158 664 : sal_uInt32 nPolyNum(0L);
159 :
160 1328 : while(nPolyNum < nPolyCount)
161 : {
162 664 : const sal_uInt32 nPointCount(rPoly.getB2DPolygon(nPolyNum).count());
163 :
164 664 : if(nAbsPnt < nPointCount)
165 : {
166 664 : rPolyNum = nPolyNum;
167 664 : rPointNum = nAbsPnt;
168 :
169 664 : return true;
170 : }
171 : else
172 : {
173 0 : nPolyNum++;
174 0 : nAbsPnt -= nPointCount;
175 : }
176 : }
177 :
178 0 : return false;
179 : }
180 :
181 435 : } // end of namespace sdr
182 :
183 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|