Version 7.6.3.2-android, tag libreoffice-7.6.3.2-android
[LibreOffice.git] / drawinglayer / source / tools / emfppath.cxx
blobe7c4a5512c7648519efc55afa8ffe57ae2d57a26
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 polygon.setNextControlPoint (p - 1, mapped);
162 SAL_INFO ("drawinglayer.emf", "EMF+\t\tPolygon append next: " << p - 1 << " mapped: " << mapped.getX () << "," << mapped.getY ());
163 continue;
165 else if (((i - last_normal) % 3) == 2)
167 prev = mapped;
168 hasPrev = true;
169 continue;
172 else
174 last_normal = i;
178 polygon.append (mapped);
179 SAL_INFO ("drawinglayer.emf", "EMF+\t\tPoint: " << xPoints[i] << "," << yPoints[i] << " mapped: " << mapped.getX () << ":" << mapped.getY ());
181 if (hasPrev)
183 polygon.setPrevControlPoint (p, prev);
184 SAL_INFO ("drawinglayer.emf", "EMF+\t\tPolygon append prev: " << p << " mapped: " << prev.getX () << "," << prev.getY ());
185 hasPrev = false;
188 p++;
190 if (pPointTypes && (pPointTypes [i] & 0x80)) // closed polygon
192 polygon.setClosed (true);
193 aPolygon.append (polygon);
194 SAL_INFO ("drawinglayer.emf", "EMF+\t\tClose polygon");
195 last_normal = i + 1;
196 p = 0;
197 polygon.clear ();
201 // Draw an extra line between the last point and the first point, to close the shape.
202 if (bAddLineToCloseShape)
204 polygon.setClosed (true);
207 if (polygon.count ())
209 aPolygon.append (polygon);
211 #if OSL_DEBUG_LEVEL > 1
212 for (unsigned int i=0; i<aPolygon.count(); i++) {
213 polygon = aPolygon.getB2DPolygon(i);
214 SAL_INFO ("drawinglayer.emf", "EMF+\t\tPolygon: " << i);
215 for (unsigned int j=0; j<polygon.count(); j++) {
216 ::basegfx::B2DPoint point = polygon.getB2DPoint(j);
217 SAL_INFO ("drawinglayer.emf", "EMF+\t\t\tPoint: " << point.getX() << "," << point.getY());
218 if (polygon.isPrevControlPointUsed(j)) {
219 point = polygon.getPrevControlPoint(j);
220 SAL_INFO ("drawinglayer.emf", "EMF+\t\t\tPrev: " << point.getX() << "," << point.getY());
222 if (polygon.isNextControlPointUsed(j)) {
223 point = polygon.getNextControlPoint(j);
224 SAL_INFO ("drawinglayer.emf", "EMF+\t\t\tNext: " << point.getX() << "," << point.getY());
228 #endif
231 return aPolygon;
234 static void GetCardinalMatrix(float tension, matrix& m)
236 m[0][1] = 2. - tension;
237 m[0][2] = tension - 2.;
238 m[1][0] = 2. * tension;
239 m[1][1] = tension - 3.;
240 m[1][2] = 3. - 2. * tension;
241 m[3][1] = 1.;
242 m[0][3] = m[2][2] = tension;
243 m[0][0] = m[1][3] = m[2][0] = -tension;
244 m[2][1] = m[2][3] = m[3][0] = m[3][2] = m[3][3] = 0.;
247 static double calculateSplineCoefficients(float p0, float p1, float p2, float p3, sal_uInt32 step, matrix m)
249 double a = m[0][0] * p0 + m[0][1] * p1 + m[0][2] * p2 + m[0][3] * p3;
250 double b = m[1][0] * p0 + m[1][1] * p1 + m[1][2] * p2 + m[1][3] * p3;
251 double c = m[2][0] * p0 + m[2][2] * p2;
252 double d = p1;
253 return (d + alpha[step] * (c + alpha[step] * (b + alpha[step] * a)));
256 ::basegfx::B2DPolyPolygon& EMFPPath::GetCardinalSpline(EmfPlusHelperData const& rR, float fTension,
257 sal_uInt32 aOffset, sal_uInt32 aNumSegments)
259 ::basegfx::B2DPolygon polygon;
260 matrix mat;
261 double x, y;
262 if (aNumSegments >= nPoints)
263 aNumSegments = nPoints - 1;
264 GetCardinalMatrix(fTension, mat);
265 // duplicate first point
266 xPoints.push_front(xPoints.front());
267 yPoints.push_front(yPoints.front());
268 // duplicate last point
269 xPoints.push_back(xPoints.back());
270 yPoints.push_back(yPoints.back());
272 for (sal_uInt32 i = 3 + aOffset; i < aNumSegments + 3; i++)
274 for (sal_uInt32 s = 0; s < nDetails; s++)
276 x = calculateSplineCoefficients(xPoints[i - 3], xPoints[i - 2], xPoints[i - 1],
277 xPoints[i], s, mat);
278 y = calculateSplineCoefficients(yPoints[i - 3], yPoints[i - 2], yPoints[i - 1],
279 yPoints[i], s, mat);
280 polygon.append(rR.Map(x, y));
283 if (polygon.count())
284 aPolygon.append(polygon);
285 return aPolygon;
288 ::basegfx::B2DPolyPolygon& EMFPPath::GetClosedCardinalSpline(EmfPlusHelperData const& rR, float fTension)
290 ::basegfx::B2DPolygon polygon;
291 matrix mat;
292 double x, y;
293 GetCardinalMatrix(fTension, mat);
294 // add three first points at the end
295 xPoints.push_back(xPoints[0]);
296 yPoints.push_back(yPoints[0]);
297 xPoints.push_back(xPoints[1]);
298 yPoints.push_back(yPoints[1]);
299 xPoints.push_back(xPoints[2]);
300 yPoints.push_back(yPoints[2]);
302 for (sal_uInt32 i = 3; i < nPoints + 3; i++)
304 for (sal_uInt32 s = 0; s < nDetails; s++)
306 x = calculateSplineCoefficients(xPoints[i - 3], xPoints[i - 2], xPoints[i - 1],
307 xPoints[i], s, mat);
308 y = calculateSplineCoefficients(yPoints[i - 3], yPoints[i - 2], yPoints[i - 1],
309 yPoints[i], s, mat);
310 polygon.append(rR.Map(x, y));
313 polygon.setClosed(true);
314 if (polygon.count())
315 aPolygon.append(polygon);
316 return aPolygon;
320 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */