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 :
32 : using namespace ::com::sun::star;
33 :
34 : namespace basegfx
35 : {
36 : namespace unotools
37 : {
38 :
39 46 : B2DPolyPolygon polyPolygonBezierToB2DPolyPolygon(const drawing::PolyPolygonBezierCoords& rSourcePolyPolygon)
40 : throw( lang::IllegalArgumentException )
41 : {
42 46 : const sal_Int32 nOuterSequenceCount(rSourcePolyPolygon.Coordinates.getLength());
43 46 : B2DPolyPolygon aNewPolyPolygon;
44 :
45 46 : if(rSourcePolyPolygon.Flags.getLength() != nOuterSequenceCount)
46 0 : throw lang::IllegalArgumentException();
47 :
48 : // get pointers to inner sequence
49 46 : const drawing::PointSequence* pInnerSequence = rSourcePolyPolygon.Coordinates.getConstArray();
50 46 : const drawing::FlagSequence* pInnerSequenceFlags = rSourcePolyPolygon.Flags.getConstArray();
51 :
52 96 : for(sal_Int32 a(0); a < nOuterSequenceCount; a++)
53 : {
54 50 : const sal_Int32 nInnerSequenceCount(pInnerSequence->getLength());
55 :
56 50 : if(pInnerSequenceFlags->getLength() != nInnerSequenceCount)
57 0 : throw lang::IllegalArgumentException();
58 :
59 : // prepare new polygon
60 50 : basegfx::B2DPolygon aNewPolygon;
61 50 : const awt::Point* pArray = pInnerSequence->getConstArray();
62 50 : const drawing::PolygonFlags* pArrayFlags = pInnerSequenceFlags->getConstArray();
63 :
64 : // get first point and flag
65 50 : basegfx::B2DPoint aNewCoordinatePair(pArray->X, pArray->Y); pArray++;
66 50 : drawing::PolygonFlags ePolyFlag(*pArrayFlags); pArrayFlags++;
67 50 : basegfx::B2DPoint aControlA;
68 50 : basegfx::B2DPoint aControlB;
69 :
70 : // first point is not allowed to be a control point
71 50 : if(drawing::PolygonFlags_CONTROL == ePolyFlag)
72 0 : throw lang::IllegalArgumentException();
73 :
74 : // add first point as start point
75 50 : aNewPolygon.append(aNewCoordinatePair);
76 438 : for(sal_Int32 b(1); b < nInnerSequenceCount;)
77 : {
78 : // prepare loop
79 338 : bool bControlA(false);
80 338 : bool bControlB(false);
81 :
82 : // get next point and flag
83 338 : aNewCoordinatePair = basegfx::B2DPoint(pArray->X, pArray->Y);
84 338 : ePolyFlag = *pArrayFlags;
85 338 : pArray++; pArrayFlags++; b++;
86 :
87 338 : if(b < nInnerSequenceCount && drawing::PolygonFlags_CONTROL == ePolyFlag)
88 : {
89 188 : aControlA = aNewCoordinatePair;
90 188 : bControlA = true;
91 :
92 : // get next point and flag
93 188 : aNewCoordinatePair = basegfx::B2DPoint(pArray->X, pArray->Y);
94 188 : ePolyFlag = *pArrayFlags;
95 188 : pArray++; pArrayFlags++; b++;
96 : }
97 :
98 338 : if(b < nInnerSequenceCount && drawing::PolygonFlags_CONTROL == ePolyFlag)
99 : {
100 188 : aControlB = aNewCoordinatePair;
101 188 : bControlB = true;
102 :
103 : // get next point and flag
104 188 : aNewCoordinatePair = basegfx::B2DPoint(pArray->X, pArray->Y);
105 188 : ePolyFlag = *pArrayFlags;
106 188 : pArray++; pArrayFlags++; b++;
107 : }
108 :
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 338 : if(drawing::PolygonFlags_CONTROL == ePolyFlag || bControlA != bControlB)
112 0 : throw lang::IllegalArgumentException();
113 :
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
121 : // for the future.
122 864 : if(bControlA
123 188 : && aControlA.equal(aControlB)
124 338 : && aControlA.equal(aNewPolygon.getB2DPoint(aNewPolygon.count() - 1)))
125 : {
126 0 : bControlA = bControlB = false;
127 : }
128 :
129 338 : if(bControlA)
130 : {
131 : // add bezier edge
132 188 : aNewPolygon.appendBezierSegment(aControlA, aControlB, aNewCoordinatePair);
133 : }
134 : else
135 : {
136 : // add edge
137 150 : aNewPolygon.append(aNewCoordinatePair);
138 : }
139 : }
140 :
141 : // next sequence
142 50 : pInnerSequence++;
143 50 : pInnerSequenceFlags++;
144 :
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 50 : basegfx::tools::checkClosed(aNewPolygon);
148 :
149 : // add new subpolygon
150 50 : aNewPolyPolygon.append(aNewPolygon);
151 50 : }
152 :
153 46 : return aNewPolyPolygon;
154 : }
155 :
156 : /////////////////////////////////////////////////////////////////////////////////
157 :
158 340 : void b2DPolyPolygonToPolyPolygonBezier( const basegfx::B2DPolyPolygon& rPolyPoly,
159 : drawing::PolyPolygonBezierCoords& rRetval )
160 : {
161 340 : rRetval.Coordinates.realloc(rPolyPoly.count());
162 340 : rRetval.Flags.realloc(rPolyPoly.count());
163 :
164 340 : drawing::PointSequence* pOuterSequence = rRetval.Coordinates.getArray();
165 340 : drawing::FlagSequence* pOuterFlags = rRetval.Flags.getArray();
166 :
167 382 : for(sal_uInt32 a=0;a<rPolyPoly.count();a++)
168 : {
169 42 : const B2DPolygon& rPoly = rPolyPoly.getB2DPolygon(a);
170 42 : sal_uInt32 nCount(rPoly.count());
171 42 : const bool bClosed(rPoly.isClosed());
172 :
173 : // calculate input vertex count
174 42 : const sal_uInt32 nLoopCount(bClosed ? nCount : (nCount ? nCount - 1L : 0L ));
175 :
176 42 : std::vector<awt::Point> aPoints; aPoints.reserve(nLoopCount);
177 42 : std::vector<drawing::PolygonFlags> aFlags; aFlags.reserve(nLoopCount);
178 :
179 42 : if( nCount )
180 : {
181 : // prepare insert index and current point
182 42 : basegfx::B2DCubicBezier aBezier;
183 42 : aBezier.setStartPoint(rPoly.getB2DPoint(0));
184 :
185 540 : for(sal_uInt32 b(0L); b<nLoopCount; b++)
186 : {
187 : // add current point (always) and remember StartPointIndex for evtl. later corrections
188 996 : const awt::Point aStartPoint(fround(aBezier.getStartPoint().getX()),
189 1494 : fround(aBezier.getStartPoint().getY()));
190 498 : const sal_uInt32 nStartPointIndex(aPoints.size());
191 498 : aPoints.push_back(aStartPoint);
192 498 : aFlags.push_back(drawing::PolygonFlags_NORMAL);
193 :
194 : // prepare next segment
195 498 : const sal_uInt32 nNextIndex((b + 1) % nCount);
196 498 : aBezier.setEndPoint(rPoly.getB2DPoint(nNextIndex));
197 498 : aBezier.setControlPointA(rPoly.getNextControlPoint(b));
198 498 : aBezier.setControlPointB(rPoly.getPrevControlPoint(nNextIndex));
199 :
200 498 : if(aBezier.isBezier())
201 : {
202 : // if one is used, add always two control points due to the old schema
203 756 : aPoints.push_back( awt::Point(fround(aBezier.getControlPointA().getX()),
204 1134 : fround(aBezier.getControlPointA().getY())) );
205 378 : aFlags.push_back(drawing::PolygonFlags_CONTROL);
206 :
207 756 : aPoints.push_back( awt::Point(fround(aBezier.getControlPointB().getX()),
208 1134 : fround(aBezier.getControlPointB().getY())) );
209 378 : aFlags.push_back(drawing::PolygonFlags_CONTROL);
210 : }
211 :
212 : // test continuity with previous control point to set flag value
213 498 : if(aBezier.getControlPointA() != aBezier.getStartPoint() && (bClosed || b))
214 : {
215 354 : const basegfx::B2VectorContinuity eCont(rPoly.getContinuityInPoint(b));
216 :
217 354 : if(basegfx::CONTINUITY_C1 == eCont)
218 : {
219 6 : aFlags[nStartPointIndex] = drawing::PolygonFlags_SMOOTH;
220 : }
221 348 : else if(basegfx::CONTINUITY_C2 == eCont)
222 : {
223 48 : aFlags[nStartPointIndex] = drawing::PolygonFlags_SYMMETRIC;
224 : }
225 : }
226 :
227 : // prepare next polygon step
228 498 : aBezier.setStartPoint(aBezier.getEndPoint());
229 : }
230 :
231 42 : if(bClosed)
232 : {
233 : // add first point again as closing point due to old definition
234 28 : aPoints.push_back( aPoints[0] );
235 28 : aFlags.push_back(drawing::PolygonFlags_NORMAL);
236 : }
237 : else
238 : {
239 : // add last point as closing point
240 14 : const basegfx::B2DPoint aClosingPoint(rPoly.getB2DPoint(nCount - 1L));
241 14 : const awt::Point aEnd(fround(aClosingPoint.getX()),
242 28 : fround(aClosingPoint.getY()));
243 14 : aPoints.push_back(aEnd);
244 14 : aFlags.push_back(drawing::PolygonFlags_NORMAL);
245 42 : }
246 : }
247 :
248 42 : *pOuterSequence++ = comphelper::containerToSequence(aPoints);
249 42 : *pOuterFlags++ = comphelper::containerToSequence(aFlags);
250 42 : }
251 340 : }
252 :
253 : }
254 : }
255 :
256 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|