Coverage Report

Created: 2025-07-12 07:23

/src/qtbase/src/gui/painting/qoutlinemapper.cpp
Line
Count
Source (jump to first uncovered line)
1
/****************************************************************************
2
**
3
** Copyright (C) 2016 The Qt Company Ltd.
4
** Contact: https://www.qt.io/licensing/
5
**
6
** This file is part of the QtGui module of the Qt Toolkit.
7
**
8
** $QT_BEGIN_LICENSE:LGPL$
9
** Commercial License Usage
10
** Licensees holding valid commercial Qt licenses may use this file in
11
** accordance with the commercial license agreement provided with the
12
** Software or, alternatively, in accordance with the terms contained in
13
** a written agreement between you and The Qt Company. For licensing terms
14
** and conditions see https://www.qt.io/terms-conditions. For further
15
** information use the contact form at https://www.qt.io/contact-us.
16
**
17
** GNU Lesser General Public License Usage
18
** Alternatively, this file may be used under the terms of the GNU Lesser
19
** General Public License version 3 as published by the Free Software
20
** Foundation and appearing in the file LICENSE.LGPL3 included in the
21
** packaging of this file. Please review the following information to
22
** ensure the GNU Lesser General Public License version 3 requirements
23
** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
24
**
25
** GNU General Public License Usage
26
** Alternatively, this file may be used under the terms of the GNU
27
** General Public License version 2.0 or (at your option) the GNU General
28
** Public license version 3 or any later version approved by the KDE Free
29
** Qt Foundation. The licenses are as published by the Free Software
30
** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
31
** included in the packaging of this file. Please review the following
32
** information to ensure the GNU General Public License requirements will
33
** be met: https://www.gnu.org/licenses/gpl-2.0.html and
34
** https://www.gnu.org/licenses/gpl-3.0.html.
35
**
36
** $QT_END_LICENSE$
37
**
38
****************************************************************************/
39
40
#include "qoutlinemapper_p.h"
41
42
#include "qbezier_p.h"
43
#include "qmath.h"
44
#include "qpainterpath_p.h"
45
#include "qscopedvaluerollback.h"
46
47
#include <stdlib.h>
48
49
QT_BEGIN_NAMESPACE
50
51
0
#define qreal_to_fixed_26_6(f) (qRound(f * 64))
52
53
54
55
56
static const QRectF boundingRect(const QPointF *points, int pointCount)
57
0
{
58
0
    const QPointF *e = points;
59
0
    const QPointF *last = points + pointCount;
60
0
    qreal minx, maxx, miny, maxy;
61
0
    minx = maxx = e->x();
62
0
    miny = maxy = e->y();
63
0
    while (++e < last) {
64
0
        if (e->x() < minx)
65
0
            minx = e->x();
66
0
        else if (e->x() > maxx)
67
0
            maxx = e->x();
68
0
        if (e->y() < miny)
69
0
            miny = e->y();
70
0
        else if (e->y() > maxy)
71
0
            maxy = e->y();
72
0
    }
73
0
    return QRectF(QPointF(minx, miny), QPointF(maxx, maxy));
74
0
}
75
76
0
void QOutlineMapper::curveTo(const QPointF &cp1, const QPointF &cp2, const QPointF &ep) {
77
#ifdef QT_DEBUG_CONVERT
78
    printf("QOutlineMapper::curveTo() (%f, %f)\n", ep.x(), ep.y());
79
#endif
80
81
0
    if (!m_elements.size())
82
0
        return;
83
0
    QBezier bezier = QBezier::fromPoints(m_elements.last(), cp1, cp2, ep);
84
85
0
    bool outsideClip = false;
86
    // Test one point first before doing a full intersection test.
87
0
    if (!QRectF(m_clip_rect).contains(m_transform.map(ep))) {
88
0
        QRectF potentialCurveArea = m_transform.mapRect(bezier.bounds());
89
0
        outsideClip = !potentialCurveArea.intersects(m_clip_rect);
90
0
    }
91
0
    if (outsideClip) {
92
        // The curve is entirely outside the clip rect, so just
93
        // approximate it with a line that closes the path.
94
0
        lineTo(ep);
95
0
    } else {
96
0
        bezier.addToPolygon(m_elements, m_curve_threshold);
97
0
        m_element_types.reserve(m_elements.size());
98
0
        for (int i = m_elements.size() - m_element_types.size(); i; --i)
99
0
            m_element_types << QPainterPath::LineToElement;
100
0
    }
101
0
    Q_ASSERT(m_elements.size() == m_element_types.size());
102
0
}
103
104
105
QT_FT_Outline *QOutlineMapper::convertPath(const QPainterPath &path)
106
0
{
107
0
    Q_ASSERT(!path.isEmpty());
108
0
    int elmCount = path.elementCount();
109
#ifdef QT_DEBUG_CONVERT
110
    printf("QOutlineMapper::convertPath(), size=%d\n", elmCount);
111
#endif
112
0
    beginOutline(path.fillRule());
113
114
0
    for (int index=0; index<elmCount; ++index) {
115
0
        const QPainterPath::Element &elm = path.elementAt(index);
116
117
0
        switch (elm.type) {
118
119
0
        case QPainterPath::MoveToElement:
120
0
            if (index == elmCount - 1)
121
0
                continue;
122
0
            moveTo(elm);
123
0
            break;
124
125
0
        case QPainterPath::LineToElement:
126
0
            lineTo(elm);
127
0
            break;
128
129
0
        case QPainterPath::CurveToElement:
130
0
            curveTo(elm, path.elementAt(index + 1), path.elementAt(index + 2));
131
0
            index += 2;
132
0
            break;
133
134
0
        default:
135
0
            break; // This will never hit..
136
0
        }
137
0
    }
138
139
0
    endOutline();
140
0
    return outline();
141
0
}
142
143
QT_FT_Outline *QOutlineMapper::convertPath(const QVectorPath &path)
144
0
{
145
0
    int count = path.elementCount();
146
147
#ifdef QT_DEBUG_CONVERT
148
    printf("QOutlineMapper::convertPath(VP), size=%d\n", count);
149
#endif
150
0
    beginOutline(path.hasWindingFill() ? Qt::WindingFill : Qt::OddEvenFill);
151
152
0
    if (path.elements()) {
153
        // TODO: if we do closing of subpaths in convertElements instead we
154
        // could avoid this loop
155
0
        const QPainterPath::ElementType *elements = path.elements();
156
0
        const QPointF *points = reinterpret_cast<const QPointF *>(path.points());
157
158
0
        for (int index = 0; index < count; ++index) {
159
0
            switch (elements[index]) {
160
0
                case QPainterPath::MoveToElement:
161
0
                    if (index == count - 1)
162
0
                        continue;
163
0
                    moveTo(points[index]);
164
0
                    break;
165
166
0
                case QPainterPath::LineToElement:
167
0
                    lineTo(points[index]);
168
0
                    break;
169
170
0
                case QPainterPath::CurveToElement:
171
0
                    curveTo(points[index], points[index+1], points[index+2]);
172
0
                    index += 2;
173
0
                    break;
174
175
0
                default:
176
0
                    break; // This will never hit..
177
0
            }
178
0
        }
179
180
0
    } else {
181
        // ### We can kill this copying and just use the buffer straight...
182
183
0
        m_elements.resize(count);
184
0
        if (count)
185
0
            memcpy(static_cast<void *>(m_elements.data()), static_cast<const void *>(path.points()), count* sizeof(QPointF));
186
187
0
        m_element_types.resize(0);
188
0
    }
189
190
0
    endOutline();
191
0
    return outline();
192
0
}
193
194
195
void QOutlineMapper::endOutline()
196
0
{
197
0
    closeSubpath();
198
199
0
    if (m_elements.isEmpty()) {
200
0
        memset(&m_outline, 0, sizeof(m_outline));
201
0
        return;
202
0
    }
203
204
0
    QPointF *elements = m_elements.data();
205
206
    // Transform the outline
207
0
    if (m_transform.isIdentity()) {
208
        // Nothing to do
209
0
    } else if (m_transform.type() < QTransform::TxProject) {
210
0
        for (int i = 0; i < m_elements.size(); ++i)
211
0
            elements[i] = m_transform.map(elements[i]);
212
0
    } else {
213
0
        const QVectorPath vp((qreal *)elements, m_elements.size(),
214
0
                             m_element_types.size() ? m_element_types.data() : nullptr);
215
0
        QPainterPath path = vp.convertToPainterPath();
216
0
        path = m_transform.map(path);
217
0
        if (!(m_outline.flags & QT_FT_OUTLINE_EVEN_ODD_FILL))
218
0
            path.setFillRule(Qt::WindingFill);
219
0
        if (path.isEmpty()) {
220
0
            m_valid = false;
221
0
        } else {
222
0
            QTransform oldTransform = m_transform;
223
0
            m_transform.reset();
224
0
            convertPath(path);
225
0
            m_transform = oldTransform;
226
0
        }
227
0
        return;
228
0
    }
229
230
0
    controlPointRect = boundingRect(elements, m_elements.size());
231
232
#ifdef QT_DEBUG_CONVERT
233
    printf(" - control point rect (%.2f, %.2f) %.2f x %.2f, clip=(%d,%d, %dx%d)\n",
234
           controlPointRect.x(), controlPointRect.y(),
235
           controlPointRect.width(), controlPointRect.height(),
236
           m_clip_rect.x(), m_clip_rect.y(), m_clip_rect.width(), m_clip_rect.height());
237
#endif
238
239
240
    // Check for out of dev bounds...
241
0
    const bool do_clip = !m_in_clip_elements && ((controlPointRect.left() < -QT_RASTER_COORD_LIMIT
242
0
                          || controlPointRect.right() > QT_RASTER_COORD_LIMIT
243
0
                          || controlPointRect.top() < -QT_RASTER_COORD_LIMIT
244
0
                          || controlPointRect.bottom() > QT_RASTER_COORD_LIMIT
245
0
                          || controlPointRect.width() > QT_RASTER_COORD_LIMIT
246
0
                          || controlPointRect.height() > QT_RASTER_COORD_LIMIT));
247
248
0
    if (do_clip) {
249
0
        clipElements(elements, elementTypes(), m_elements.size());
250
0
    } else {
251
0
        convertElements(elements, elementTypes(), m_elements.size());
252
0
    }
253
0
}
254
255
void QOutlineMapper::convertElements(const QPointF *elements,
256
                                       const QPainterPath::ElementType *types,
257
                                       int element_count)
258
0
{
259
260
0
    if (types) {
261
        // Translate into FT coords
262
0
        const QPointF *e = elements;
263
0
        for (int i=0; i<element_count; ++i) {
264
0
            switch (*types) {
265
0
            case QPainterPath::MoveToElement:
266
0
                {
267
0
                    QT_FT_Vector pt_fixed = { qreal_to_fixed_26_6(e->x()),
268
0
                                              qreal_to_fixed_26_6(e->y()) };
269
0
                    if (i != 0)
270
0
                        m_contours << m_points.size() - 1;
271
0
                    m_points << pt_fixed;
272
0
                    m_tags <<  QT_FT_CURVE_TAG_ON;
273
0
                }
274
0
                break;
275
276
0
            case QPainterPath::LineToElement:
277
0
                {
278
0
                    QT_FT_Vector pt_fixed = { qreal_to_fixed_26_6(e->x()),
279
0
                                              qreal_to_fixed_26_6(e->y()) };
280
0
                    m_points << pt_fixed;
281
0
                    m_tags << QT_FT_CURVE_TAG_ON;
282
0
                }
283
0
                break;
284
285
0
            case QPainterPath::CurveToElement:
286
0
                {
287
0
                    QT_FT_Vector cp1_fixed = { qreal_to_fixed_26_6(e->x()),
288
0
                                               qreal_to_fixed_26_6(e->y()) };
289
0
                    ++e;
290
0
                    QT_FT_Vector cp2_fixed = { qreal_to_fixed_26_6((e)->x()),
291
0
                                               qreal_to_fixed_26_6((e)->y()) };
292
0
                    ++e;
293
0
                    QT_FT_Vector ep_fixed = { qreal_to_fixed_26_6((e)->x()),
294
0
                                              qreal_to_fixed_26_6((e)->y()) };
295
296
0
                    m_points << cp1_fixed << cp2_fixed << ep_fixed;
297
0
                    m_tags << QT_FT_CURVE_TAG_CUBIC
298
0
                           << QT_FT_CURVE_TAG_CUBIC
299
0
                           << QT_FT_CURVE_TAG_ON;
300
301
0
                    types += 2;
302
0
                    i += 2;
303
0
                }
304
0
                break;
305
0
            default:
306
0
                break;
307
0
            }
308
0
            ++types;
309
0
            ++e;
310
0
        }
311
0
    } else {
312
        // Plain polygon...
313
0
        const QPointF *last = elements + element_count;
314
0
        const QPointF *e = elements;
315
0
        while (e < last) {
316
0
            QT_FT_Vector pt_fixed = { qreal_to_fixed_26_6(e->x()),
317
0
                                      qreal_to_fixed_26_6(e->y()) };
318
0
            m_points << pt_fixed;
319
0
            m_tags << QT_FT_CURVE_TAG_ON;
320
0
            ++e;
321
0
        }
322
0
    }
323
324
    // close the very last subpath
325
0
    m_contours << m_points.size() - 1;
326
327
0
    m_outline.n_contours = m_contours.size();
328
0
    m_outline.n_points = m_points.size();
329
330
0
    m_outline.points = m_points.data();
331
0
    m_outline.tags = m_tags.data();
332
0
    m_outline.contours = m_contours.data();
333
334
#ifdef QT_DEBUG_CONVERT
335
    printf("QOutlineMapper::endOutline\n");
336
337
    printf(" - contours: %d\n", m_outline.n_contours);
338
    for (int i=0; i<m_outline.n_contours; ++i) {
339
        printf("   - %d\n", m_outline.contours[i]);
340
    }
341
342
    printf(" - points: %d\n", m_outline.n_points);
343
    for (int i=0; i<m_outline.n_points; ++i) {
344
        printf("   - %d -- %.2f, %.2f, (%d, %d)\n", i,
345
               (double) (m_outline.points[i].x / 64.0),
346
               (double) (m_outline.points[i].y / 64.0),
347
               (int) m_outline.points[i].x, (int) m_outline.points[i].y);
348
    }
349
#endif
350
0
}
351
352
void QOutlineMapper::clipElements(const QPointF *elements,
353
                                    const QPainterPath::ElementType *types,
354
                                    int element_count)
355
0
{
356
    // We could save a bit of time by actually implementing them fully
357
    // instead of going through convenience functionallity, but since
358
    // this part of code hardly every used, it shouldn't matter.
359
360
0
    QScopedValueRollback<bool> in_clip_elements(m_in_clip_elements, true);
361
362
0
    QPainterPath path;
363
364
0
    if (!(m_outline.flags & QT_FT_OUTLINE_EVEN_ODD_FILL))
365
0
        path.setFillRule(Qt::WindingFill);
366
367
0
    if (types) {
368
0
        for (int i=0; i<element_count; ++i) {
369
0
            switch (types[i]) {
370
0
            case QPainterPath::MoveToElement:
371
0
                path.moveTo(elements[i]);
372
0
                break;
373
374
0
            case QPainterPath::LineToElement:
375
0
                path.lineTo(elements[i]);
376
0
                break;
377
378
0
            case QPainterPath::CurveToElement:
379
0
                path.cubicTo(elements[i], elements[i+1], elements[i+2]);
380
0
                i += 2;
381
0
                break;
382
0
            default:
383
0
                break;
384
0
            }
385
0
        }
386
0
    } else {
387
0
        path.moveTo(elements[0]);
388
0
        for (int i=1; i<element_count; ++i)
389
0
            path.lineTo(elements[i]);
390
0
    }
391
392
0
    QPainterPath clipPath;
393
0
    clipPath.addRect(m_clip_rect);
394
0
    QPainterPath clippedPath = path.intersected(clipPath);
395
0
    if (clippedPath.isEmpty()) {
396
0
        m_valid = false;
397
0
    } else {
398
0
        QTransform oldTransform = m_transform;
399
0
        m_transform.reset();
400
0
        convertPath(clippedPath);
401
0
        m_transform = oldTransform;
402
0
    }
403
0
}
404
405
QT_END_NAMESPACE