1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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"
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
)
52 bool bIsEmfPlusInteger15
= u8
& nTopBitInt7
;
53 bool bNegative
= u8
& nSignBitInt7
;
54 unsigned char val1
= u8
& nValueMaskInt7
;
57 if (!bIsEmfPlusInteger15
)
59 return static_cast<signed char>(val1
);
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));
77 pPointTypes
.reset( new sal_uInt8
[_nPoints
] );
80 EMFPPath::~EMFPPath ()
84 void EMFPPath::Read (SvStream
& s
, sal_uInt32 pathFlags
)
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
);
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
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
);
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
);
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
);
132 ::basegfx::B2DPolyPolygon
& EMFPPath::GetPolygon (EmfPlusHelperData
const & rR
, bool bMapIt
, bool bAddLineToCloseShape
)
134 ::basegfx::B2DPolygon polygon
;
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
);
151 mapped
= rR
.Map(xPoints
[i
], yPoints
[i
]);
153 mapped
= ::basegfx::B2DPoint(xPoints
[i
], yPoints
[i
]);
157 if ((pPointTypes
[i
] & 0x07) == 3)
159 if (((i
- last_normal
)% 3) == 1)
162 polygon
.setNextControlPoint (p
- 1, mapped
);
163 SAL_INFO ("drawinglayer.emf", "EMF+\t\tPolygon append next: " << p
- 1 << " mapped: " << mapped
.getX () << "," << mapped
.getY ());
166 else if (((i
- last_normal
) % 3) == 2)
179 polygon
.append (mapped
);
180 SAL_INFO ("drawinglayer.emf", "EMF+\t\tPoint: " << xPoints
[i
] << "," << yPoints
[i
] << " mapped: " << mapped
.getX () << ":" << mapped
.getY ());
184 polygon
.setPrevControlPoint (p
, prev
);
185 SAL_INFO ("drawinglayer.emf", "EMF+\t\tPolygon append prev: " << p
<< " mapped: " << prev
.getX () << "," << prev
.getY ());
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");
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());
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
;
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
;
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
;
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],
279 y
= calculateSplineCoefficients(yPoints
[i
- 3], yPoints
[i
- 2], yPoints
[i
- 1],
281 polygon
.append(rR
.Map(x
, y
));
285 aPolygon
.append(polygon
);
289 ::basegfx::B2DPolyPolygon
& EMFPPath::GetClosedCardinalSpline(EmfPlusHelperData
const& rR
, float fTension
)
291 ::basegfx::B2DPolygon polygon
;
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],
309 y
= calculateSplineCoefficients(yPoints
[i
- 3], yPoints
[i
- 2], yPoints
[i
- 1],
311 polygon
.append(rR
.Map(x
, y
));
314 polygon
.setClosed(true);
316 aPolygon
.append(polygon
);
321 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */