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 <com/sun/star/drawing/PolyPolygonBezierCoords.hpp>
21 #include <com/sun/star/drawing/PointSequence.hpp>
22 #include <com/sun/star/drawing/FlagSequence.hpp>
23 #include <basegfx/polygon/b2dpolypolygontools.hxx>
24 #include <basegfx/polygon/b2dpolygontools.hxx>
25 #include <basegfx/polygon/b2dpolygon.hxx>
26 #include <basegfx/curve/b2dcubicbezier.hxx>
28 #include <basegfx/tools/unotools.hxx>
29 #include <comphelper/sequence.hxx>
32 using namespace ::com::sun::star
;
39 B2DPolyPolygon
polyPolygonBezierToB2DPolyPolygon(const drawing::PolyPolygonBezierCoords
& rSourcePolyPolygon
)
40 throw( lang::IllegalArgumentException
)
42 const sal_Int32
nOuterSequenceCount(rSourcePolyPolygon
.Coordinates
.getLength());
43 B2DPolyPolygon aNewPolyPolygon
;
45 if(rSourcePolyPolygon
.Flags
.getLength() != nOuterSequenceCount
)
46 throw lang::IllegalArgumentException();
48 // get pointers to inner sequence
49 const drawing::PointSequence
* pInnerSequence
= rSourcePolyPolygon
.Coordinates
.getConstArray();
50 const drawing::FlagSequence
* pInnerSequenceFlags
= rSourcePolyPolygon
.Flags
.getConstArray();
52 for(sal_Int32
a(0); a
< nOuterSequenceCount
; a
++)
54 const sal_Int32
nInnerSequenceCount(pInnerSequence
->getLength());
56 if(pInnerSequenceFlags
->getLength() != nInnerSequenceCount
)
57 throw lang::IllegalArgumentException();
59 // prepare new polygon
60 basegfx::B2DPolygon aNewPolygon
;
61 const awt::Point
* pArray
= pInnerSequence
->getConstArray();
62 const drawing::PolygonFlags
* pArrayFlags
= pInnerSequenceFlags
->getConstArray();
64 // get first point and flag
65 basegfx::B2DPoint
aNewCoordinatePair(pArray
->X
, pArray
->Y
); pArray
++;
66 drawing::PolygonFlags
ePolyFlag(*pArrayFlags
); pArrayFlags
++;
67 basegfx::B2DPoint aControlA
;
68 basegfx::B2DPoint aControlB
;
70 // first point is not allowed to be a control point
71 if(drawing::PolygonFlags_CONTROL
== ePolyFlag
)
72 throw lang::IllegalArgumentException();
74 // add first point as start point
75 aNewPolygon
.append(aNewCoordinatePair
);
76 for(sal_Int32
b(1); b
< nInnerSequenceCount
;)
79 bool bControlA(false);
80 bool bControlB(false);
82 // get next point and flag
83 aNewCoordinatePair
= basegfx::B2DPoint(pArray
->X
, pArray
->Y
);
84 ePolyFlag
= *pArrayFlags
;
85 pArray
++; pArrayFlags
++; b
++;
87 if(b
< nInnerSequenceCount
&& drawing::PolygonFlags_CONTROL
== ePolyFlag
)
89 aControlA
= aNewCoordinatePair
;
92 // get next point and flag
93 aNewCoordinatePair
= basegfx::B2DPoint(pArray
->X
, pArray
->Y
);
94 ePolyFlag
= *pArrayFlags
;
95 pArray
++; pArrayFlags
++; b
++;
98 if(b
< nInnerSequenceCount
&& drawing::PolygonFlags_CONTROL
== ePolyFlag
)
100 aControlB
= aNewCoordinatePair
;
103 // get next point and flag
104 aNewCoordinatePair
= basegfx::B2DPoint(pArray
->X
, pArray
->Y
);
105 ePolyFlag
= *pArrayFlags
;
106 pArray
++; pArrayFlags
++; b
++;
109 // two or no control points are consumed, another one would be an error.
110 // It's also an error if only one control point was read
111 if(drawing::PolygonFlags_CONTROL
== ePolyFlag
|| bControlA
!= bControlB
)
112 throw lang::IllegalArgumentException();
114 // the previous writes used the B2DPolyPoygon -> PolyPolygon converter
115 // which did not create minimal PolyPolygons, but created all control points
116 // as null vectors (identical points). Because of the former P(CA)(CB)-norm of
117 // B2DPolygon and it's unused sign of being the zero-vector and CA and CB being
118 // relative to P, an empty edge was exported as P == CA == CB. Luckily, the new
119 // export format can be read without errors by the old OOo-versions, so we need only
120 // to correct here at read and do not need to export a wrong but compatible version
123 && aControlA
.equal(aControlB
)
124 && aControlA
.equal(aNewPolygon
.getB2DPoint(aNewPolygon
.count() - 1)))
126 bControlA
= bControlB
= false;
132 aNewPolygon
.appendBezierSegment(aControlA
, aControlB
, aNewCoordinatePair
);
137 aNewPolygon
.append(aNewCoordinatePair
);
143 pInnerSequenceFlags
++;
145 // #i72807# API import uses old line start/end-equal definition for closed,
146 // so we need to correct this to closed state here
147 basegfx::tools::checkClosed(aNewPolygon
);
149 // add new subpolygon
150 aNewPolyPolygon
.append(aNewPolygon
);
153 return aNewPolyPolygon
;
156 /////////////////////////////////////////////////////////////////////////////////
158 void b2DPolyPolygonToPolyPolygonBezier( const basegfx::B2DPolyPolygon
& rPolyPoly
,
159 drawing::PolyPolygonBezierCoords
& rRetval
)
161 rRetval
.Coordinates
.realloc(rPolyPoly
.count());
162 rRetval
.Flags
.realloc(rPolyPoly
.count());
164 drawing::PointSequence
* pOuterSequence
= rRetval
.Coordinates
.getArray();
165 drawing::FlagSequence
* pOuterFlags
= rRetval
.Flags
.getArray();
167 for(sal_uInt32 a
=0;a
<rPolyPoly
.count();a
++)
169 const B2DPolygon
& rPoly
= rPolyPoly
.getB2DPolygon(a
);
170 sal_uInt32
nCount(rPoly
.count());
171 const bool bClosed(rPoly
.isClosed());
173 // calculate input vertex count
174 const sal_uInt32
nLoopCount(bClosed
? nCount
: (nCount
? nCount
- 1L : 0L ));
176 std::vector
<awt::Point
> aPoints
; aPoints
.reserve(nLoopCount
);
177 std::vector
<drawing::PolygonFlags
> aFlags
; aFlags
.reserve(nLoopCount
);
181 // prepare insert index and current point
182 basegfx::B2DCubicBezier aBezier
;
183 aBezier
.setStartPoint(rPoly
.getB2DPoint(0));
185 for(sal_uInt32
b(0L); b
<nLoopCount
; b
++)
187 // add current point (always) and remember StartPointIndex for evtl. later corrections
188 const awt::Point
aStartPoint(fround(aBezier
.getStartPoint().getX()),
189 fround(aBezier
.getStartPoint().getY()));
190 const sal_uInt32
nStartPointIndex(aPoints
.size());
191 aPoints
.push_back(aStartPoint
);
192 aFlags
.push_back(drawing::PolygonFlags_NORMAL
);
194 // prepare next segment
195 const sal_uInt32
nNextIndex((b
+ 1) % nCount
);
196 aBezier
.setEndPoint(rPoly
.getB2DPoint(nNextIndex
));
197 aBezier
.setControlPointA(rPoly
.getNextControlPoint(b
));
198 aBezier
.setControlPointB(rPoly
.getPrevControlPoint(nNextIndex
));
200 if(aBezier
.isBezier())
202 // if one is used, add always two control points due to the old schema
203 aPoints
.push_back( awt::Point(fround(aBezier
.getControlPointA().getX()),
204 fround(aBezier
.getControlPointA().getY())) );
205 aFlags
.push_back(drawing::PolygonFlags_CONTROL
);
207 aPoints
.push_back( awt::Point(fround(aBezier
.getControlPointB().getX()),
208 fround(aBezier
.getControlPointB().getY())) );
209 aFlags
.push_back(drawing::PolygonFlags_CONTROL
);
212 // test continuity with previous control point to set flag value
213 if(aBezier
.getControlPointA() != aBezier
.getStartPoint() && (bClosed
|| b
))
215 const basegfx::B2VectorContinuity
eCont(rPoly
.getContinuityInPoint(b
));
217 if(basegfx::CONTINUITY_C1
== eCont
)
219 aFlags
[nStartPointIndex
] = drawing::PolygonFlags_SMOOTH
;
221 else if(basegfx::CONTINUITY_C2
== eCont
)
223 aFlags
[nStartPointIndex
] = drawing::PolygonFlags_SYMMETRIC
;
227 // prepare next polygon step
228 aBezier
.setStartPoint(aBezier
.getEndPoint());
233 // add first point again as closing point due to old definition
234 aPoints
.push_back( aPoints
[0] );
235 aFlags
.push_back(drawing::PolygonFlags_NORMAL
);
239 // add last point as closing point
240 const basegfx::B2DPoint
aClosingPoint(rPoly
.getB2DPoint(nCount
- 1L));
241 const awt::Point
aEnd(fround(aClosingPoint
.getX()),
242 fround(aClosingPoint
.getY()));
243 aPoints
.push_back(aEnd
);
244 aFlags
.push_back(drawing::PolygonFlags_NORMAL
);
248 *pOuterSequence
++ = comphelper::containerToSequence(aPoints
);
249 *pOuterFlags
++ = comphelper::containerToSequence(aFlags
);
256 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */