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