LibreOffice Module drawinglayer (master) 1
emfppath.cxx
Go to the documentation of this file.
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
23#include <sal/log.hxx>
24#include "emfppath.hxx"
25
26namespace
27{
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;
33}
34
35namespace emfplushelper
36{
37 typedef double matrix [4][4];
38
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 };
43
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)
48 {
49 unsigned char u8(0);
50 s.ReadUChar(u8);
51
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)
58 {
59 return static_cast<signed char>(val1);
60 }
61
62 s.ReadUChar(u8);
63 sal_uInt16 nRet = (val1 << 8) | u8;
64 return static_cast<sal_Int16>(nRet);
65 }
66
67 EMFPPath::EMFPPath (sal_uInt32 _nPoints, bool bLines)
68 {
69 if (_nPoints > SAL_MAX_UINT32 / (2 * sizeof(float)))
70 {
71 _nPoints = SAL_MAX_UINT32 / (2 * sizeof(float));
72 }
73
74 nPoints = _nPoints;
75
76 if (!bLines)
77 pPointTypes.reset( new sal_uInt8 [_nPoints] );
78 }
79
81 {
82 }
83
84 void EMFPPath::Read (SvStream& s, sal_uInt32 pathFlags)
85 {
86 float fx, fy;
87 for (sal_uInt32 i = 0; i < nPoints; i++)
88 {
89 if (pathFlags & 0x800)
90 {
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);
99 }
100 else if (pathFlags & 0x4000)
101 {
102 // EMFPlusPoint: stored in signed short 16bit integer format
103 sal_Int16 x, y;
104
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);
109 }
110 else
111 {
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);
117 }
118 }
119
120 if (pPointTypes)
121 {
122 for (sal_uInt32 i = 0; i < nPoints; i++)
123 {
125 SAL_INFO("drawinglayer.emf", "EMF+\tpoint type: 0x" << std::hex << static_cast<int>(pPointTypes[i]) << std::dec);
126 }
127 }
128
129 aPolygon.clear();
130 }
131
132 ::basegfx::B2DPolyPolygon& EMFPPath::GetPolygon (EmfPlusHelperData const & rR, bool bMapIt, bool bAddLineToCloseShape)
133 {
134 ::basegfx::B2DPolygon polygon;
135 aPolygon.clear ();
136 sal_uInt32 last_normal = 0, p = 0;
137 ::basegfx::B2DPoint prev, mapped;
138 bool hasPrev = false;
139
140 for (sal_uInt32 i = 0; i < nPoints; i++)
141 {
142 if (p && pPointTypes && (pPointTypes [i] == 0))
143 {
144 aPolygon.append (polygon);
145 last_normal = i;
146 p = 0;
147 polygon.clear ();
148 }
149
150 if (bMapIt)
151 mapped = rR.Map(xPoints[i], yPoints [i]);
152 else
154
155 if (pPointTypes)
156 {
157 if ((pPointTypes [i] & 0x07) == 3)
158 {
159 if (((i - last_normal )% 3) == 1)
160 {
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;
164 }
165 else if (((i - last_normal) % 3) == 2)
166 {
167 prev = mapped;
168 hasPrev = true;
169 continue;
170 }
171 }
172 else
173 {
174 last_normal = i;
175 }
176 }
177
178 polygon.append (mapped);
179 SAL_INFO ("drawinglayer.emf", "EMF+\t\tPoint: " << xPoints[i] << "," << yPoints[i] << " mapped: " << mapped.getX () << ":" << mapped.getY ());
180
181 if (hasPrev)
182 {
183 polygon.setPrevControlPoint (p, prev);
184 SAL_INFO ("drawinglayer.emf", "EMF+\t\tPolygon append prev: " << p << " mapped: " << prev.getX () << "," << prev.getY ());
185 hasPrev = false;
186 }
187
188 p++;
189
190 if (pPointTypes && (pPointTypes [i] & 0x80)) // closed polygon
191 {
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 ();
198 }
199 }
200
201 // Draw an extra line between the last point and the first point, to close the shape.
202 if (bAddLineToCloseShape)
203 {
204 polygon.setClosed (true);
205 }
206
207 if (polygon.count ())
208 {
209 aPolygon.append (polygon);
210
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++) {
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());
221 }
222 if (polygon.isNextControlPointUsed(j)) {
223 point = polygon.getNextControlPoint(j);
224 SAL_INFO ("drawinglayer.emf", "EMF+\t\t\tNext: " << point.getX() << "," << point.getY());
225 }
226 }
227 }
228#endif
229 }
230
231 return aPolygon;
232 }
233
234 static void GetCardinalMatrix(float tension, matrix& m)
235 {
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.;
245 }
246
247 static double calculateSplineCoefficients(float p0, float p1, float p2, float p3, sal_uInt32 step, matrix m)
248 {
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)));
254 }
255
257 sal_uInt32 aOffset, sal_uInt32 aNumSegments)
258 {
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());
271
272 for (sal_uInt32 i = 3 + aOffset; i < aNumSegments + 3; i++)
273 {
274 for (sal_uInt32 s = 0; s < nDetails; s++)
275 {
277 xPoints[i], s, mat);
279 yPoints[i], s, mat);
280 polygon.append(rR.Map(x, y));
281 }
282 }
283 if (polygon.count())
284 aPolygon.append(polygon);
285 return aPolygon;
286 }
287
289 {
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]);
301
302 for (sal_uInt32 i = 3; i < nPoints + 3; i++)
303 {
304 for (sal_uInt32 s = 0; s < nDetails; s++)
305 {
307 xPoints[i], s, mat);
309 yPoints[i], s, mat);
310 polygon.append(rR.Map(x, y));
311 }
312 }
313 polygon.setClosed(true);
314 if (polygon.count())
315 aPolygon.append(polygon);
316 return aPolygon;
317 }
318}
319
320/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
double d
SvStream & ReadInt16(sal_Int16 &rInt16)
SvStream & ReadFloat(float &rFloat)
SvStream & ReadUChar(unsigned char &rChar)
B2DPolygon const & getB2DPolygon(sal_uInt32 nIndex) const
void append(const B2DPolygon &rPolygon, sal_uInt32 nCount=1)
sal_uInt32 count() const
bool isPrevControlPointUsed(sal_uInt32 nIndex) const
bool isNextControlPointUsed(sal_uInt32 nIndex) const
void setPrevControlPoint(sal_uInt32 nIndex, const basegfx::B2DPoint &rValue)
basegfx::B2DPoint const & getB2DPoint(sal_uInt32 nIndex) const
void setNextControlPoint(sal_uInt32 nIndex, const basegfx::B2DPoint &rValue)
basegfx::B2DPoint getPrevControlPoint(sal_uInt32 nIndex) const
void append(const basegfx::B2DPoint &rPoint, sal_uInt32 nCount)
sal_uInt32 count() const
void setClosed(bool bNew)
basegfx::B2DPoint getNextControlPoint(sal_uInt32 nIndex) const
TYPE getX() const
TYPE getY() const
std::unique_ptr< sal_uInt8[]> pPointTypes
Definition: emfppath.hxx:31
std::deque< float > xPoints
Definition: emfppath.hxx:30
::basegfx::B2DPolyPolygon aPolygon
Definition: emfppath.hxx:28
virtual ~EMFPPath() override
Definition: emfppath.cxx:80
::basegfx::B2DPolyPolygon & GetClosedCardinalSpline(EmfPlusHelperData const &rR, float fTension)
Definition: emfppath.cxx:288
::basegfx::B2DPolyPolygon & GetPolygon(EmfPlusHelperData const &rR, bool bMapIt=true, bool bAddLineToCloseShape=false)
Definition: emfppath.cxx:132
EMFPPath(sal_uInt32 _nPoints, bool bLines=false)
Definition: emfppath.cxx:67
::basegfx::B2DPolyPolygon & GetCardinalSpline(EmfPlusHelperData const &rR, float fTension, sal_uInt32 aOffset, sal_uInt32 aNumSegments)
Definition: emfppath.cxx:256
std::deque< float > yPoints
Definition: emfppath.hxx:30
void Read(SvStream &s, sal_uInt32 pathFlags)
Definition: emfppath.cxx:84
float y
float x
void * p
uno_Any a
#define SAL_INFO(area, stream)
def point()
static void GetCardinalMatrix(float tension, matrix &m)
Definition: emfppath.cxx:234
double matrix[4][4]
Definition: emfppath.cxx:37
constexpr double alpha[nDetails]
Definition: emfppath.cxx:41
static sal_Int16 GetEmfPlusInteger(SvStream &s)
Definition: emfppath.cxx:47
constexpr sal_uInt32 nDetails
Definition: emfppath.cxx:39
static double calculateSplineCoefficients(float p0, float p1, float p2, float p3, sal_uInt32 step, matrix m)
Definition: emfppath.cxx:247
int i
m
::basegfx::B2DPoint Map(double ix, double iy) const
unsigned char sal_uInt8
#define SAL_MAX_UINT32