Coverage Report

Created: 2025-12-08 09:28

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libreoffice/drawinglayer/source/tools/emfppath.cxx
Line
Count
Source
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
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"
25
26
namespace
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
35
namespace 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
0
    {
49
0
        unsigned char u8(0);
50
0
        s.ReadUChar(u8);
51
52
0
        bool bIsEmfPlusInteger15 = u8 & nTopBitInt7;
53
0
        bool bNegative = u8 & nSignBitInt7;
54
0
        unsigned char val1 = u8 & nValueMaskInt7;
55
0
        if (bNegative)
56
0
            val1 |= nTopBitInt7;
57
0
        if (!bIsEmfPlusInteger15)
58
0
        {
59
0
            return static_cast<signed char>(val1);
60
0
        }
61
62
0
        s.ReadUChar(u8);
63
0
        sal_uInt16 nRet = (val1 << 8) | u8;
64
0
        return static_cast<sal_Int16>(nRet);
65
0
    }
66
67
    EMFPPath::EMFPPath (sal_uInt32 _nPoints, bool bLines)
68
0
    {
69
0
        if (_nPoints > SAL_MAX_UINT32 / (2 * sizeof(float)))
70
0
        {
71
0
            _nPoints = SAL_MAX_UINT32 / (2 * sizeof(float));
72
0
        }
73
74
0
        nPoints = _nPoints;
75
76
0
        if (!bLines)
77
0
            pPointTypes.reset( new sal_uInt8 [_nPoints] );
78
0
    }
79
80
    EMFPPath::~EMFPPath ()
81
0
    {
82
0
    }
83
84
    void EMFPPath::Read (SvStream& s, sal_uInt32 pathFlags)
85
0
    {
86
0
        float fx, fy;
87
0
        for (sal_uInt32 i = 0; i < nPoints; i++)
88
0
        {
89
0
            if (pathFlags & 0x800)
90
0
            {
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
0
                sal_Int32 x = GetEmfPlusInteger(s);
95
0
                sal_Int32 y = GetEmfPlusInteger(s);
96
0
                xPoints.push_back(x);
97
0
                yPoints.push_back(y);
98
0
                SAL_INFO("drawinglayer.emf", "EMF+\t\t\t" << i << ". EmfPlusPointR [x,y]: " << x << ", " << y);
99
0
            }
100
0
            else if (pathFlags & 0x4000)
101
0
            {
102
                // EMFPlusPoint: stored in signed short 16bit integer format
103
0
                sal_Int16 x, y;
104
105
0
                s.ReadInt16(x).ReadInt16(y);
106
0
                SAL_INFO("drawinglayer.emf", "EMF+\t\t\t" << i << ". EmfPlusPoint [x,y]: " << x << ", " << y);
107
0
                xPoints.push_back(x);
108
0
                yPoints.push_back(y);
109
0
            }
110
0
            else
111
0
            {
112
                // EMFPlusPointF: stored in Single (float) format
113
0
                s.ReadFloat(fx).ReadFloat(fy);
114
0
                SAL_INFO("drawinglayer.emf", "EMF+\t" << i << ". EMFPlusPointF [x,y]: " << fx << ", " << fy);
115
0
                xPoints.push_back(fx);
116
0
                yPoints.push_back(fy);
117
0
            }
118
0
        }
119
120
0
        if (pPointTypes)
121
0
        {
122
0
            for (sal_uInt32 i = 0; i < nPoints; i++)
123
0
            {
124
0
                s.ReadUChar(pPointTypes[i]);
125
0
                SAL_INFO("drawinglayer.emf", "EMF+\tpoint type: 0x" << std::hex << static_cast<int>(pPointTypes[i]) << std::dec);
126
0
            }
127
0
        }
128
129
0
        aPolygon.clear();
130
0
    }
131
132
    ::basegfx::B2DPolyPolygon& EMFPPath::GetPolygon (EmfPlusHelperData const & rR, bool bMapIt, bool bAddLineToCloseShape)
133
0
    {
134
0
        ::basegfx::B2DPolygon polygon;
135
0
        aPolygon.clear ();
136
0
        sal_uInt32 last_normal = 0, p = 0;
137
0
        ::basegfx::B2DPoint prev, mapped;
138
0
        bool hasPrev = false;
139
140
0
        for (sal_uInt32 i = 0; i < nPoints; i++)
141
0
        {
142
0
            if (p && pPointTypes && (pPointTypes [i] == 0))
143
0
            {
144
0
                aPolygon.append (polygon);
145
0
                last_normal = i;
146
0
                p = 0;
147
0
                polygon.clear ();
148
0
            }
149
150
0
            if (bMapIt)
151
0
                mapped = rR.Map(xPoints[i], yPoints [i]);
152
0
            else
153
0
                mapped = ::basegfx::B2DPoint(xPoints[i], yPoints[i]);
154
155
0
            if (pPointTypes)
156
0
            {
157
0
                if ((pPointTypes [i] & 0x07) == 3)
158
0
                {
159
0
                    if (((i - last_normal )% 3) == 1)
160
0
                    {
161
0
                        assert(p != 0);
162
0
                        polygon.setNextControlPoint (p - 1, mapped);
163
0
                        SAL_INFO ("drawinglayer.emf", "EMF+\t\tPolygon append next: " << p - 1 << " mapped: " << mapped.getX () << "," << mapped.getY ());
164
0
                        continue;
165
0
                    }
166
0
                    else if (((i - last_normal) % 3) == 2)
167
0
                    {
168
0
                        prev = mapped;
169
0
                        hasPrev = true;
170
0
                        continue;
171
0
                    }
172
0
                }
173
0
                else
174
0
                {
175
0
                    last_normal = i;
176
0
                }
177
0
            }
178
179
0
            polygon.append (mapped);
180
0
            SAL_INFO ("drawinglayer.emf", "EMF+\t\tPoint: " << xPoints[i] << "," << yPoints[i] << " mapped: " << mapped.getX () << ":" << mapped.getY ());
181
182
0
            if (hasPrev)
183
0
            {
184
0
                polygon.setPrevControlPoint (p, prev);
185
0
                SAL_INFO ("drawinglayer.emf", "EMF+\t\tPolygon append prev: " << p << " mapped: " << prev.getX () << "," << prev.getY ());
186
0
                hasPrev = false;
187
0
            }
188
189
0
            p++;
190
191
0
            if (pPointTypes && (pPointTypes [i] & 0x80)) // closed polygon
192
0
            {
193
0
                polygon.setClosed (true);
194
0
                aPolygon.append (polygon);
195
0
                SAL_INFO ("drawinglayer.emf", "EMF+\t\tClose polygon");
196
0
                last_normal = i + 1;
197
0
                p = 0;
198
0
                polygon.clear ();
199
0
            }
200
0
        }
201
202
        // Draw an extra line between the last point and the first point, to close the shape.
203
0
        if (bAddLineToCloseShape)
204
0
        {
205
0
            polygon.setClosed (true);
206
0
        }
207
208
0
        if (polygon.count ())
209
0
        {
210
0
            aPolygon.append (polygon);
211
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());
222
                    }
223
                    if (polygon.isNextControlPointUsed(j)) {
224
                        point = polygon.getNextControlPoint(j);
225
                        SAL_INFO ("drawinglayer.emf", "EMF+\t\t\tNext: " << point.getX() << "," << point.getY());
226
                    }
227
                }
228
            }
229
#endif
230
0
        }
231
232
0
        return aPolygon;
233
0
    }
234
235
    static void GetCardinalMatrix(float tension, matrix& m)
236
0
    {
237
0
        m[0][1] = 2. - tension;
238
0
        m[0][2] = tension - 2.;
239
0
        m[1][0] = 2. * tension;
240
0
        m[1][1] = tension - 3.;
241
0
        m[1][2] = 3. - 2. * tension;
242
0
        m[3][1] = 1.;
243
0
        m[0][3] = m[2][2] = tension;
244
0
        m[0][0] = m[1][3] = m[2][0] = -tension;
245
0
        m[2][1] = m[2][3] = m[3][0] = m[3][2] = m[3][3] = 0.;
246
0
    }
247
248
    static double calculateSplineCoefficients(float p0, float p1, float p2, float p3, sal_uInt32 step, matrix m)
249
0
    {
250
0
        double a = m[0][0] * p0 + m[0][1] * p1 + m[0][2] * p2 + m[0][3] * p3;
251
0
        double b = m[1][0] * p0 + m[1][1] * p1 + m[1][2] * p2 + m[1][3] * p3;
252
0
        double c = m[2][0] * p0 + m[2][2] * p2;
253
0
        double d = p1;
254
0
        return (d + alpha[step] * (c + alpha[step] * (b + alpha[step] * a)));
255
0
    }
256
257
    ::basegfx::B2DPolyPolygon& EMFPPath::GetCardinalSpline(EmfPlusHelperData const& rR, float fTension,
258
                                                           sal_uInt32 aOffset, sal_uInt32 aNumSegments)
259
0
    {
260
0
        ::basegfx::B2DPolygon polygon;
261
0
        matrix mat;
262
0
        double x, y;
263
0
        if (aNumSegments >= nPoints)
264
0
            aNumSegments = nPoints - 1;
265
0
        GetCardinalMatrix(fTension, mat);
266
        // duplicate first point
267
0
        xPoints.push_front(xPoints.front());
268
0
        yPoints.push_front(yPoints.front());
269
        // duplicate last point
270
0
        xPoints.push_back(xPoints.back());
271
0
        yPoints.push_back(yPoints.back());
272
273
0
        for (sal_uInt32 i = 3 + aOffset; i < aNumSegments + 3; i++)
274
0
        {
275
0
            for (sal_uInt32 s = 0; s < nDetails; s++)
276
0
            {
277
0
                x = calculateSplineCoefficients(xPoints[i - 3], xPoints[i - 2], xPoints[i - 1],
278
0
                                                xPoints[i], s, mat);
279
0
                y = calculateSplineCoefficients(yPoints[i - 3], yPoints[i - 2], yPoints[i - 1],
280
0
                                                yPoints[i], s, mat);
281
0
                polygon.append(rR.Map(x, y));
282
0
            }
283
0
        }
284
0
        if (polygon.count())
285
0
            aPolygon.append(polygon);
286
0
        return aPolygon;
287
0
    }
288
289
    ::basegfx::B2DPolyPolygon& EMFPPath::GetClosedCardinalSpline(EmfPlusHelperData const& rR, float fTension)
290
0
    {
291
0
        ::basegfx::B2DPolygon polygon;
292
0
        matrix mat;
293
0
        double x, y;
294
0
        GetCardinalMatrix(fTension, mat);
295
        // add three first points at the end
296
0
        xPoints.push_back(xPoints[0]);
297
0
        yPoints.push_back(yPoints[0]);
298
0
        xPoints.push_back(xPoints[1]);
299
0
        yPoints.push_back(yPoints[1]);
300
0
        xPoints.push_back(xPoints[2]);
301
0
        yPoints.push_back(yPoints[2]);
302
303
0
        for (sal_uInt32 i = 3; i < nPoints + 3; i++)
304
0
        {
305
0
            for (sal_uInt32 s = 0; s < nDetails; s++)
306
0
            {
307
0
                x = calculateSplineCoefficients(xPoints[i - 3], xPoints[i - 2], xPoints[i - 1],
308
0
                                                xPoints[i], s, mat);
309
0
                y = calculateSplineCoefficients(yPoints[i - 3], yPoints[i - 2], yPoints[i - 1],
310
0
                                                yPoints[i], s, mat);
311
0
                polygon.append(rR.Map(x, y));
312
0
            }
313
0
        }
314
0
        polygon.setClosed(true);
315
0
        if (polygon.count())
316
0
            aPolygon.append(polygon);
317
0
        return aPolygon;
318
0
    }
319
}
320
321
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */