tdf#162786, tdf#161947: Add support for setuptools and pip
[LibreOffice.git] / drawinglayer / source / tools / emfppath.cxx
blobcf841fd999e0a6827156eddfa5742e6841d61a7f
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 <basegfx/point/b2dpoint.hxx>
21 #include <basegfx/polygon/b2dpolygon.hxx>
22 #include <basegfx/polygon/b2dpolypolygon.hxx>
23 #include <sal/log.hxx>
24 #include "emfppath.hxx"
26 namespace
28 const unsigned char nTopBitInt7 = 0x80;
29 const unsigned char nSignBitInt7 = 0x40;
30 // include the sign bit so if it's negative we get
31 // that "missing" bit pre-set to 1
32 const unsigned char nValueMaskInt7 = 0x7F;
35 namespace emfplushelper
37 typedef double matrix [4][4];
39 constexpr sal_uInt32 nDetails = 8;
40 constexpr double alpha[nDetails]
41 = { 1. / nDetails, 2. / nDetails, 3. / nDetails, 4. / nDetails,
42 5. / nDetails, 6. / nDetails, 7. / nDetails, 8. / nDetails };
44 // see 2.2.2.21 EmfPlusInteger7
45 // 2.2.2.22 EmfPlusInteger15
46 // and 2.2.2.37 EmfPlusPointR Object
47 static sal_Int16 GetEmfPlusInteger(SvStream& s)
49 unsigned char u8(0);
50 s.ReadUChar(u8);
52 bool bIsEmfPlusInteger15 = u8 & nTopBitInt7;
53 bool bNegative = u8 & nSignBitInt7;
54 unsigned char val1 = u8 & nValueMaskInt7;
55 if (bNegative)
56 val1 |= nTopBitInt7;
57 if (!bIsEmfPlusInteger15)
59 return static_cast<signed char>(val1);
62 s.ReadUChar(u8);
63 sal_uInt16 nRet = (val1 << 8) | u8;
64 return static_cast<sal_Int16>(nRet);
67 EMFPPath::EMFPPath (sal_uInt32 _nPoints, bool bLines)
69 if (_nPoints > SAL_MAX_UINT32 / (2 * sizeof(float)))
71 _nPoints = SAL_MAX_UINT32 / (2 * sizeof(float));
74 nPoints = _nPoints;
76 if (!bLines)
77 pPointTypes.reset( new sal_uInt8 [_nPoints] );
80 EMFPPath::~EMFPPath ()
84 void EMFPPath::Read (SvStream& s, sal_uInt32 pathFlags)
86 float fx, fy;
87 for (sal_uInt32 i = 0; i < nPoints; i++)
89 if (pathFlags & 0x800)
91 // EMFPlusPointR: points are stored in EMFPlusInteger7 or
92 // EMFPlusInteger15 objects, see section 2.2.2.21/22
93 // If 0x800 bit is set, the 0x4000 bit is undefined and must be ignored
94 sal_Int32 x = GetEmfPlusInteger(s);
95 sal_Int32 y = GetEmfPlusInteger(s);
96 xPoints.push_back(x);
97 yPoints.push_back(y);
98 SAL_INFO("drawinglayer.emf", "EMF+\t\t\t" << i << ". EmfPlusPointR [x,y]: " << x << ", " << y);
100 else if (pathFlags & 0x4000)
102 // EMFPlusPoint: stored in signed short 16bit integer format
103 sal_Int16 x, y;
105 s.ReadInt16(x).ReadInt16(y);
106 SAL_INFO("drawinglayer.emf", "EMF+\t\t\t" << i << ". EmfPlusPoint [x,y]: " << x << ", " << y);
107 xPoints.push_back(x);
108 yPoints.push_back(y);
110 else
112 // EMFPlusPointF: stored in Single (float) format
113 s.ReadFloat(fx).ReadFloat(fy);
114 SAL_INFO("drawinglayer.emf", "EMF+\t" << i << ". EMFPlusPointF [x,y]: " << fx << ", " << fy);
115 xPoints.push_back(fx);
116 yPoints.push_back(fy);
120 if (pPointTypes)
122 for (sal_uInt32 i = 0; i < nPoints; i++)
124 s.ReadUChar(pPointTypes[i]);
125 SAL_INFO("drawinglayer.emf", "EMF+\tpoint type: 0x" << std::hex << static_cast<int>(pPointTypes[i]) << std::dec);
129 aPolygon.clear();
132 ::basegfx::B2DPolyPolygon& EMFPPath::GetPolygon (EmfPlusHelperData const & rR, bool bMapIt, bool bAddLineToCloseShape)
134 ::basegfx::B2DPolygon polygon;
135 aPolygon.clear ();
136 sal_uInt32 last_normal = 0, p = 0;
137 ::basegfx::B2DPoint prev, mapped;
138 bool hasPrev = false;
140 for (sal_uInt32 i = 0; i < nPoints; i++)
142 if (p && pPointTypes && (pPointTypes [i] == 0))
144 aPolygon.append (polygon);
145 last_normal = i;
146 p = 0;
147 polygon.clear ();
150 if (bMapIt)
151 mapped = rR.Map(xPoints[i], yPoints [i]);
152 else
153 mapped = ::basegfx::B2DPoint(xPoints[i], yPoints[i]);
155 if (pPointTypes)
157 if ((pPointTypes [i] & 0x07) == 3)
159 if (((i - last_normal )% 3) == 1)
161 assert(p != 0);
162 polygon.setNextControlPoint (p - 1, mapped);
163 SAL_INFO ("drawinglayer.emf", "EMF+\t\tPolygon append next: " << p - 1 << " mapped: " << mapped.getX () << "," << mapped.getY ());
164 continue;
166 else if (((i - last_normal) % 3) == 2)
168 prev = mapped;
169 hasPrev = true;
170 continue;
173 else
175 last_normal = i;
179 polygon.append (mapped);
180 SAL_INFO ("drawinglayer.emf", "EMF+\t\tPoint: " << xPoints[i] << "," << yPoints[i] << " mapped: " << mapped.getX () << ":" << mapped.getY ());
182 if (hasPrev)
184 polygon.setPrevControlPoint (p, prev);
185 SAL_INFO ("drawinglayer.emf", "EMF+\t\tPolygon append prev: " << p << " mapped: " << prev.getX () << "," << prev.getY ());
186 hasPrev = false;
189 p++;
191 if (pPointTypes && (pPointTypes [i] & 0x80)) // closed polygon
193 polygon.setClosed (true);
194 aPolygon.append (polygon);
195 SAL_INFO ("drawinglayer.emf", "EMF+\t\tClose polygon");
196 last_normal = i + 1;
197 p = 0;
198 polygon.clear ();
202 // Draw an extra line between the last point and the first point, to close the shape.
203 if (bAddLineToCloseShape)
205 polygon.setClosed (true);
208 if (polygon.count ())
210 aPolygon.append (polygon);
212 #if OSL_DEBUG_LEVEL > 1
213 for (unsigned int i=0; i<aPolygon.count(); i++) {
214 polygon = aPolygon.getB2DPolygon(i);
215 SAL_INFO ("drawinglayer.emf", "EMF+\t\tPolygon: " << i);
216 for (unsigned int j=0; j<polygon.count(); j++) {
217 ::basegfx::B2DPoint point = polygon.getB2DPoint(j);
218 SAL_INFO ("drawinglayer.emf", "EMF+\t\t\tPoint: " << point.getX() << "," << point.getY());
219 if (polygon.isPrevControlPointUsed(j)) {
220 point = polygon.getPrevControlPoint(j);
221 SAL_INFO ("drawinglayer.emf", "EMF+\t\t\tPrev: " << point.getX() << "," << point.getY());
223 if (polygon.isNextControlPointUsed(j)) {
224 point = polygon.getNextControlPoint(j);
225 SAL_INFO ("drawinglayer.emf", "EMF+\t\t\tNext: " << point.getX() << "," << point.getY());
229 #endif
232 return aPolygon;
235 static void GetCardinalMatrix(float tension, matrix& m)
237 m[0][1] = 2. - tension;
238 m[0][2] = tension - 2.;
239 m[1][0] = 2. * tension;
240 m[1][1] = tension - 3.;
241 m[1][2] = 3. - 2. * tension;
242 m[3][1] = 1.;
243 m[0][3] = m[2][2] = tension;
244 m[0][0] = m[1][3] = m[2][0] = -tension;
245 m[2][1] = m[2][3] = m[3][0] = m[3][2] = m[3][3] = 0.;
248 static double calculateSplineCoefficients(float p0, float p1, float p2, float p3, sal_uInt32 step, matrix m)
250 double a = m[0][0] * p0 + m[0][1] * p1 + m[0][2] * p2 + m[0][3] * p3;
251 double b = m[1][0] * p0 + m[1][1] * p1 + m[1][2] * p2 + m[1][3] * p3;
252 double c = m[2][0] * p0 + m[2][2] * p2;
253 double d = p1;
254 return (d + alpha[step] * (c + alpha[step] * (b + alpha[step] * a)));
257 ::basegfx::B2DPolyPolygon& EMFPPath::GetCardinalSpline(EmfPlusHelperData const& rR, float fTension,
258 sal_uInt32 aOffset, sal_uInt32 aNumSegments)
260 ::basegfx::B2DPolygon polygon;
261 matrix mat;
262 double x, y;
263 if (aNumSegments >= nPoints)
264 aNumSegments = nPoints - 1;
265 GetCardinalMatrix(fTension, mat);
266 // duplicate first point
267 xPoints.push_front(xPoints.front());
268 yPoints.push_front(yPoints.front());
269 // duplicate last point
270 xPoints.push_back(xPoints.back());
271 yPoints.push_back(yPoints.back());
273 for (sal_uInt32 i = 3 + aOffset; i < aNumSegments + 3; i++)
275 for (sal_uInt32 s = 0; s < nDetails; s++)
277 x = calculateSplineCoefficients(xPoints[i - 3], xPoints[i - 2], xPoints[i - 1],
278 xPoints[i], s, mat);
279 y = calculateSplineCoefficients(yPoints[i - 3], yPoints[i - 2], yPoints[i - 1],
280 yPoints[i], s, mat);
281 polygon.append(rR.Map(x, y));
284 if (polygon.count())
285 aPolygon.append(polygon);
286 return aPolygon;
289 ::basegfx::B2DPolyPolygon& EMFPPath::GetClosedCardinalSpline(EmfPlusHelperData const& rR, float fTension)
291 ::basegfx::B2DPolygon polygon;
292 matrix mat;
293 double x, y;
294 GetCardinalMatrix(fTension, mat);
295 // add three first points at the end
296 xPoints.push_back(xPoints[0]);
297 yPoints.push_back(yPoints[0]);
298 xPoints.push_back(xPoints[1]);
299 yPoints.push_back(yPoints[1]);
300 xPoints.push_back(xPoints[2]);
301 yPoints.push_back(yPoints[2]);
303 for (sal_uInt32 i = 3; i < nPoints + 3; i++)
305 for (sal_uInt32 s = 0; s < nDetails; s++)
307 x = calculateSplineCoefficients(xPoints[i - 3], xPoints[i - 2], xPoints[i - 1],
308 xPoints[i], s, mat);
309 y = calculateSplineCoefficients(yPoints[i - 3], yPoints[i - 2], yPoints[i - 1],
310 yPoints[i], s, mat);
311 polygon.append(rR.Map(x, y));
314 polygon.setClosed(true);
315 if (polygon.count())
316 aPolygon.append(polygon);
317 return aPolygon;
321 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */