Version 6.1.0.2, tag libreoffice-6.1.0.2
[LibreOffice.git] / basegfx / source / tools / unotools.cxx
bloba80b6b9994a34f546e085fb2276168ba4b822900
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
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 <com/sun/star/lang/IllegalArgumentException.hpp>
24 #include <basegfx/polygon/b2dpolypolygontools.hxx>
25 #include <basegfx/polygon/b2dpolygontools.hxx>
26 #include <basegfx/polygon/b2dpolygon.hxx>
27 #include <basegfx/curve/b2dcubicbezier.hxx>
29 #include <basegfx/utils/unotools.hxx>
30 #include <comphelper/sequence.hxx>
32 using namespace ::com::sun::star;
34 namespace basegfx
36 namespace unotools
39 B2DPolyPolygon polyPolygonBezierToB2DPolyPolygon(const drawing::PolyPolygonBezierCoords& rSourcePolyPolygon)
41 const sal_Int32 nOuterSequenceCount(rSourcePolyPolygon.Coordinates.getLength());
42 B2DPolyPolygon aNewPolyPolygon;
44 if(rSourcePolyPolygon.Flags.getLength() != nOuterSequenceCount)
45 throw lang::IllegalArgumentException();
47 // get pointers to inner sequence
48 const drawing::PointSequence* pInnerSequence = rSourcePolyPolygon.Coordinates.getConstArray();
49 const drawing::FlagSequence* pInnerSequenceFlags = rSourcePolyPolygon.Flags.getConstArray();
51 for(sal_Int32 a(0); a < nOuterSequenceCount; a++)
53 const sal_Int32 nInnerSequenceCount(pInnerSequence->getLength());
55 if (!nInnerSequenceCount)
56 throw lang::IllegalArgumentException();
58 if (pInnerSequenceFlags->getLength() != nInnerSequenceCount)
59 throw lang::IllegalArgumentException();
61 // prepare new polygon
62 basegfx::B2DPolygon aNewPolygon;
63 const awt::Point* pArray = pInnerSequence->getConstArray();
64 const drawing::PolygonFlags* pArrayFlags = pInnerSequenceFlags->getConstArray();
66 // get first point and flag
67 basegfx::B2DPoint aNewCoordinatePair(pArray->X, pArray->Y); pArray++;
68 drawing::PolygonFlags ePolyFlag(*pArrayFlags); pArrayFlags++;
69 basegfx::B2DPoint aControlA;
70 basegfx::B2DPoint aControlB;
72 // first point is not allowed to be a control point
73 if(ePolyFlag == drawing::PolygonFlags_CONTROL)
74 throw lang::IllegalArgumentException();
76 // add first point as start point
77 aNewPolygon.append(aNewCoordinatePair);
78 for(sal_Int32 b(1); b < nInnerSequenceCount;)
80 // prepare loop
81 bool bControlA(false);
82 bool bControlB(false);
84 // get next point and flag
85 aNewCoordinatePair = basegfx::B2DPoint(pArray->X, pArray->Y);
86 ePolyFlag = *pArrayFlags;
87 pArray++; pArrayFlags++; b++;
89 if(b < nInnerSequenceCount && ePolyFlag == drawing::PolygonFlags_CONTROL)
91 aControlA = aNewCoordinatePair;
92 bControlA = true;
94 // get next point and flag
95 aNewCoordinatePair = basegfx::B2DPoint(pArray->X, pArray->Y);
96 ePolyFlag = *pArrayFlags;
97 pArray++; pArrayFlags++; b++;
100 if(b < nInnerSequenceCount && ePolyFlag == drawing::PolygonFlags_CONTROL)
102 aControlB = aNewCoordinatePair;
103 bControlB = true;
105 // get next point and flag
106 aNewCoordinatePair = basegfx::B2DPoint(pArray->X, pArray->Y);
107 ePolyFlag = *pArrayFlags;
108 pArray++; pArrayFlags++; b++;
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 if(ePolyFlag == drawing::PolygonFlags_CONTROL || bControlA != bControlB)
114 throw lang::IllegalArgumentException();
116 // the previous writes used the B2DPolyPoygon -> utils::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 if(bControlA
125 && aControlA.equal(aControlB)
126 && aControlA.equal(aNewPolygon.getB2DPoint(aNewPolygon.count() - 1)))
128 bControlA = bControlB = false;
131 if(bControlA)
133 // add bezier edge
134 aNewPolygon.appendBezierSegment(aControlA, aControlB, aNewCoordinatePair);
136 else
138 // add edge
139 aNewPolygon.append(aNewCoordinatePair);
143 // next sequence
144 pInnerSequence++;
145 pInnerSequenceFlags++;
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 basegfx::utils::checkClosed(aNewPolygon);
151 // add new subpolygon
152 aNewPolyPolygon.append(aNewPolygon);
155 return aNewPolyPolygon;
158 void b2DPolyPolygonToPolyPolygonBezier( const basegfx::B2DPolyPolygon& rPolyPoly,
159 drawing::PolyPolygonBezierCoords& rRetval )
161 rRetval.Coordinates.realloc(rPolyPoly.count());
162 rRetval.Flags.realloc(rPolyPoly.count());
164 drawing::PointSequence* pOuterSequence = rRetval.Coordinates.getArray();
165 drawing::FlagSequence* pOuterFlags = rRetval.Flags.getArray();
167 for(sal_uInt32 a=0;a<rPolyPoly.count();a++)
169 const B2DPolygon& rPoly = rPolyPoly.getB2DPolygon(a);
170 sal_uInt32 nCount(rPoly.count());
171 const bool bClosed(rPoly.isClosed());
173 // calculate input vertex count
174 const sal_uInt32 nLoopCount(bClosed ? nCount : (nCount ? nCount - 1 : 0 ));
176 std::vector<awt::Point> aPoints; aPoints.reserve(nLoopCount);
177 std::vector<drawing::PolygonFlags> aFlags; aFlags.reserve(nLoopCount);
179 if( nCount )
181 // prepare insert index and current point
182 basegfx::B2DCubicBezier aBezier;
183 aBezier.setStartPoint(rPoly.getB2DPoint(0));
185 for(sal_uInt32 b(0); b<nLoopCount; b++)
187 // add current point (always) and remember StartPointIndex for evtl. later corrections
188 const awt::Point aStartPoint(fround(aBezier.getStartPoint().getX()),
189 fround(aBezier.getStartPoint().getY()));
190 const sal_uInt32 nStartPointIndex(aPoints.size());
191 aPoints.push_back(aStartPoint);
192 aFlags.push_back(drawing::PolygonFlags_NORMAL);
194 // prepare next segment
195 const sal_uInt32 nNextIndex((b + 1) % nCount);
196 aBezier.setEndPoint(rPoly.getB2DPoint(nNextIndex));
197 aBezier.setControlPointA(rPoly.getNextControlPoint(b));
198 aBezier.setControlPointB(rPoly.getPrevControlPoint(nNextIndex));
200 if(aBezier.isBezier())
202 // if one is used, add always two control points due to the old schema
203 aPoints.emplace_back(fround(aBezier.getControlPointA().getX()),
204 fround(aBezier.getControlPointA().getY()) );
205 aFlags.push_back(drawing::PolygonFlags_CONTROL);
207 aPoints.emplace_back(fround(aBezier.getControlPointB().getX()),
208 fround(aBezier.getControlPointB().getY()) );
209 aFlags.push_back(drawing::PolygonFlags_CONTROL);
212 // test continuity with previous control point to set flag value
213 if(aBezier.getControlPointA() != aBezier.getStartPoint() && (bClosed || b))
215 const basegfx::B2VectorContinuity eCont(rPoly.getContinuityInPoint(b));
217 if(eCont == basegfx::B2VectorContinuity::C1)
219 aFlags[nStartPointIndex] = drawing::PolygonFlags_SMOOTH;
221 else if(eCont == basegfx::B2VectorContinuity::C2)
223 aFlags[nStartPointIndex] = drawing::PolygonFlags_SYMMETRIC;
227 // prepare next polygon step
228 aBezier.setStartPoint(aBezier.getEndPoint());
231 if(bClosed)
233 // add first point again as closing point due to old definition
234 aPoints.push_back( aPoints[0] );
235 aFlags.push_back(drawing::PolygonFlags_NORMAL);
237 else
239 // add last point as closing point
240 const basegfx::B2DPoint aClosingPoint(rPoly.getB2DPoint(nCount - 1));
241 const awt::Point aEnd(fround(aClosingPoint.getX()),
242 fround(aClosingPoint.getY()));
243 aPoints.push_back(aEnd);
244 aFlags.push_back(drawing::PolygonFlags_NORMAL);
248 *pOuterSequence++ = comphelper::containerToSequence(aPoints);
249 *pOuterFlags++ = comphelper::containerToSequence(aFlags);
256 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */