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