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