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 : #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>
27 :
28 : #include <basegfx/tools/unotools.hxx>
29 : #include <comphelper/sequence.hxx>
30 :
31 : using namespace ::com::sun::star;
32 :
33 : namespace basegfx
34 : {
35 : namespace unotools
36 : {
37 :
38 1286 : B2DPolyPolygon polyPolygonBezierToB2DPolyPolygon(const drawing::PolyPolygonBezierCoords& rSourcePolyPolygon)
39 : throw( lang::IllegalArgumentException )
40 : {
41 1286 : const sal_Int32 nOuterSequenceCount(rSourcePolyPolygon.Coordinates.getLength());
42 1286 : B2DPolyPolygon aNewPolyPolygon;
43 :
44 1286 : if(rSourcePolyPolygon.Flags.getLength() != nOuterSequenceCount)
45 0 : throw lang::IllegalArgumentException();
46 :
47 : // get pointers to inner sequence
48 1286 : const drawing::PointSequence* pInnerSequence = rSourcePolyPolygon.Coordinates.getConstArray();
49 1286 : const drawing::FlagSequence* pInnerSequenceFlags = rSourcePolyPolygon.Flags.getConstArray();
50 :
51 2894 : for(sal_Int32 a(0); a < nOuterSequenceCount; a++)
52 : {
53 1608 : const sal_Int32 nInnerSequenceCount(pInnerSequence->getLength());
54 :
55 1608 : if (!nInnerSequenceCount)
56 0 : throw lang::IllegalArgumentException();
57 :
58 1608 : if (pInnerSequenceFlags->getLength() != nInnerSequenceCount)
59 0 : throw lang::IllegalArgumentException();
60 :
61 : // prepare new polygon
62 1608 : basegfx::B2DPolygon aNewPolygon;
63 1608 : const awt::Point* pArray = pInnerSequence->getConstArray();
64 1608 : const drawing::PolygonFlags* pArrayFlags = pInnerSequenceFlags->getConstArray();
65 :
66 : // get first point and flag
67 3216 : basegfx::B2DPoint aNewCoordinatePair(pArray->X, pArray->Y); pArray++;
68 1608 : drawing::PolygonFlags ePolyFlag(*pArrayFlags); pArrayFlags++;
69 3216 : basegfx::B2DPoint aControlA;
70 3216 : basegfx::B2DPoint aControlB;
71 :
72 : // first point is not allowed to be a control point
73 1608 : if(drawing::PolygonFlags_CONTROL == ePolyFlag)
74 0 : throw lang::IllegalArgumentException();
75 :
76 : // add first point as start point
77 1608 : aNewPolygon.append(aNewCoordinatePair);
78 26298 : for(sal_Int32 b(1); b < nInnerSequenceCount;)
79 : {
80 : // prepare loop
81 23082 : bool bControlA(false);
82 23082 : bool bControlB(false);
83 :
84 : // get next point and flag
85 23082 : aNewCoordinatePair = basegfx::B2DPoint(pArray->X, pArray->Y);
86 23082 : ePolyFlag = *pArrayFlags;
87 23082 : pArray++; pArrayFlags++; b++;
88 :
89 23082 : if(b < nInnerSequenceCount && drawing::PolygonFlags_CONTROL == ePolyFlag)
90 : {
91 6112 : aControlA = aNewCoordinatePair;
92 6112 : bControlA = true;
93 :
94 : // get next point and flag
95 6112 : aNewCoordinatePair = basegfx::B2DPoint(pArray->X, pArray->Y);
96 6112 : ePolyFlag = *pArrayFlags;
97 6112 : pArray++; pArrayFlags++; b++;
98 : }
99 :
100 23082 : if(b < nInnerSequenceCount && drawing::PolygonFlags_CONTROL == ePolyFlag)
101 : {
102 6112 : aControlB = aNewCoordinatePair;
103 6112 : bControlB = true;
104 :
105 : // get next point and flag
106 6112 : aNewCoordinatePair = basegfx::B2DPoint(pArray->X, pArray->Y);
107 6112 : ePolyFlag = *pArrayFlags;
108 6112 : pArray++; pArrayFlags++; b++;
109 : }
110 :
111 : // two or no control points are consumed, another one would be an error.
112 : // It's also an error if only one control point was read
113 23082 : if(drawing::PolygonFlags_CONTROL == ePolyFlag || bControlA != bControlB)
114 0 : throw lang::IllegalArgumentException();
115 :
116 : // the previous writes used the B2DPolyPoygon -> tools::PolyPolygon converter
117 : // which did not create minimal PolyPolygons, but created all control points
118 : // as null vectors (identical points). Because of the former P(CA)(CB)-norm of
119 : // B2DPolygon and it's unused sign of being the zero-vector and CA and CB being
120 : // relative to P, an empty edge was exported as P == CA == CB. Luckily, the new
121 : // export format can be read without errors by the old OOo-versions, so we need only
122 : // to correct here at read and do not need to export a wrong but compatible version
123 : // for the future.
124 67910 : if(bControlA
125 6112 : && aControlA.equal(aControlB)
126 48836 : && aControlA.equal(aNewPolygon.getB2DPoint(aNewPolygon.count() - 1)))
127 : {
128 1328 : bControlA = bControlB = false;
129 : }
130 :
131 23082 : if(bControlA)
132 : {
133 : // add bezier edge
134 4784 : aNewPolygon.appendBezierSegment(aControlA, aControlB, aNewCoordinatePair);
135 : }
136 : else
137 : {
138 : // add edge
139 18298 : aNewPolygon.append(aNewCoordinatePair);
140 : }
141 : }
142 :
143 : // next sequence
144 1608 : pInnerSequence++;
145 1608 : pInnerSequenceFlags++;
146 :
147 : // #i72807# API import uses old line start/end-equal definition for closed,
148 : // so we need to correct this to closed state here
149 1608 : basegfx::tools::checkClosed(aNewPolygon);
150 :
151 : // add new subpolygon
152 1608 : aNewPolyPolygon.append(aNewPolygon);
153 1608 : }
154 :
155 1286 : return aNewPolyPolygon;
156 : }
157 :
158 3481 : void b2DPolyPolygonToPolyPolygonBezier( const basegfx::B2DPolyPolygon& rPolyPoly,
159 : drawing::PolyPolygonBezierCoords& rRetval )
160 : {
161 3481 : rRetval.Coordinates.realloc(rPolyPoly.count());
162 3481 : rRetval.Flags.realloc(rPolyPoly.count());
163 :
164 3481 : drawing::PointSequence* pOuterSequence = rRetval.Coordinates.getArray();
165 3481 : drawing::FlagSequence* pOuterFlags = rRetval.Flags.getArray();
166 :
167 3830 : for(sal_uInt32 a=0;a<rPolyPoly.count();a++)
168 : {
169 349 : const B2DPolygon& rPoly = rPolyPoly.getB2DPolygon(a);
170 349 : sal_uInt32 nCount(rPoly.count());
171 349 : const bool bClosed(rPoly.isClosed());
172 :
173 : // calculate input vertex count
174 349 : const sal_uInt32 nLoopCount(bClosed ? nCount : (nCount ? nCount - 1L : 0L ));
175 :
176 698 : std::vector<awt::Point> aPoints; aPoints.reserve(nLoopCount);
177 698 : std::vector<drawing::PolygonFlags> aFlags; aFlags.reserve(nLoopCount);
178 :
179 349 : if( nCount )
180 : {
181 : // prepare insert index and current point
182 349 : basegfx::B2DCubicBezier aBezier;
183 349 : aBezier.setStartPoint(rPoly.getB2DPoint(0));
184 :
185 3037 : for(sal_uInt32 b(0L); b<nLoopCount; b++)
186 : {
187 : // add current point (always) and remember StartPointIndex for evtl. later corrections
188 5376 : const awt::Point aStartPoint(fround(aBezier.getStartPoint().getX()),
189 8064 : fround(aBezier.getStartPoint().getY()));
190 2688 : const sal_uInt32 nStartPointIndex(aPoints.size());
191 2688 : aPoints.push_back(aStartPoint);
192 2688 : aFlags.push_back(drawing::PolygonFlags_NORMAL);
193 :
194 : // prepare next segment
195 2688 : const sal_uInt32 nNextIndex((b + 1) % nCount);
196 2688 : aBezier.setEndPoint(rPoly.getB2DPoint(nNextIndex));
197 2688 : aBezier.setControlPointA(rPoly.getNextControlPoint(b));
198 2688 : aBezier.setControlPointB(rPoly.getPrevControlPoint(nNextIndex));
199 :
200 2688 : if(aBezier.isBezier())
201 : {
202 : // if one is used, add always two control points due to the old schema
203 2720 : aPoints.push_back( awt::Point(fround(aBezier.getControlPointA().getX()),
204 4080 : fround(aBezier.getControlPointA().getY())) );
205 1360 : aFlags.push_back(drawing::PolygonFlags_CONTROL);
206 :
207 2720 : aPoints.push_back( awt::Point(fround(aBezier.getControlPointB().getX()),
208 4080 : fround(aBezier.getControlPointB().getY())) );
209 1360 : aFlags.push_back(drawing::PolygonFlags_CONTROL);
210 : }
211 :
212 : // test continuity with previous control point to set flag value
213 2688 : if(aBezier.getControlPointA() != aBezier.getStartPoint() && (bClosed || b))
214 : {
215 1345 : const basegfx::B2VectorContinuity eCont(rPoly.getContinuityInPoint(b));
216 :
217 1345 : if(basegfx::B2VectorContinuity::C1 == eCont)
218 : {
219 216 : aFlags[nStartPointIndex] = drawing::PolygonFlags_SMOOTH;
220 : }
221 1129 : else if(basegfx::B2VectorContinuity::C2 == eCont)
222 : {
223 267 : aFlags[nStartPointIndex] = drawing::PolygonFlags_SYMMETRIC;
224 : }
225 : }
226 :
227 : // prepare next polygon step
228 2688 : aBezier.setStartPoint(aBezier.getEndPoint());
229 : }
230 :
231 349 : if(bClosed)
232 : {
233 : // add first point again as closing point due to old definition
234 341 : aPoints.push_back( aPoints[0] );
235 341 : aFlags.push_back(drawing::PolygonFlags_NORMAL);
236 : }
237 : else
238 : {
239 : // add last point as closing point
240 8 : const basegfx::B2DPoint aClosingPoint(rPoly.getB2DPoint(nCount - 1L));
241 8 : const awt::Point aEnd(fround(aClosingPoint.getX()),
242 16 : fround(aClosingPoint.getY()));
243 8 : aPoints.push_back(aEnd);
244 8 : aFlags.push_back(drawing::PolygonFlags_NORMAL);
245 349 : }
246 : }
247 :
248 349 : *pOuterSequence++ = comphelper::containerToSequence(aPoints);
249 349 : *pOuterFlags++ = comphelper::containerToSequence(aFlags);
250 349 : }
251 3481 : }
252 :
253 : }
254 : }
255 :
256 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|