Coverage Report

Created: 2026-08-25 06:40

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/qtbase/src/gui/painting/qoutlinemapper_p.h
Line
Count
Source
1
// Copyright (C) 2016 The Qt Company Ltd.
2
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
// Qt-Security score:significant reason:default
4
5
#ifndef QOUTLINEMAPPER_P_H
6
#define QOUTLINEMAPPER_P_H
7
8
//
9
//  W A R N I N G
10
//  -------------
11
//
12
// This file is not part of the Qt API.  It exists purely as an
13
// implementation detail.  This header file may change from version to
14
// version without notice, or even be removed.
15
//
16
// We mean it.
17
//
18
19
#include <QtGui/private/qtguiglobal_p.h>
20
#include <QtCore/qrect.h>
21
22
#include <QtGui/qtransform.h>
23
#include <QtGui/qpainterpath.h>
24
25
#define QT_FT_BEGIN_HEADER
26
#define QT_FT_END_HEADER
27
28
#include <private/qrasterdefs_p.h>
29
#include <private/qdatabuffer_p.h>
30
#include "qpaintengineex_p.h"
31
32
QT_BEGIN_NAMESPACE
33
34
// These limitations comes from qrasterizer.cpp, qcosmeticstroker.cpp, and qgrayraster.c.
35
// Any higher and rasterization of shapes will produce incorrect results.
36
#if Q_PROCESSOR_WORDSIZE == 8
37
constexpr int QT_RASTER_COORD_LIMIT = ((1<<23) - 1); // F24dot8 in qgrayraster.c
38
#else
39
constexpr int QT_RASTER_COORD_LIMIT = ((1<<15) - 1); // F16dot16 in qrasterizer.cpp and qcosmeticstroker.cpp
40
#endif
41
//#define QT_DEBUG_CONVERT
42
43
Q_GUI_EXPORT bool qt_scaleForTransform(const QTransform &transform, qreal *scale);
44
45
/********************************************************************************
46
 * class QOutlineMapper
47
 *
48
 * Used to map between QPainterPath and the QT_FT_Outline structure used by the
49
 * freetype scanconverter.
50
 *
51
 * The outline mapper uses a path iterator to get points from the path,
52
 * so that it is possible to transform the points as they are converted. The
53
 * callback can be a noop, translate or full-fledged xform. (Tests indicated
54
 * that using a C callback was low cost).
55
 */
56
class QOutlineMapper
57
{
58
public:
59
    QOutlineMapper() :
60
0
        m_element_types(0),
61
0
        m_elements(0),
62
0
        m_points(0),
63
0
        m_tags(0),
64
0
        m_contours(0),
65
0
        m_in_clip_elements(false)
66
0
    {
67
0
    }
68
69
    /*!
70
      Sets up the matrix to be used for conversion. This also
71
      sets up the qt_path_iterator function that is used as a callback
72
      to get points.
73
    */
74
    void setMatrix(const QTransform &m)
75
0
    {
76
0
        m_transform = m;
77
78
0
        qreal scale;
79
0
        qt_scaleForTransform(m, &scale);
80
0
        m_curve_threshold = scale == 0 ? qreal(0.25) : (qreal(0.25) / scale);
81
0
    }
82
83
    void setClipRect(QRect clipRect);
84
85
    void beginOutline(Qt::FillRule fillRule)
86
0
    {
87
#ifdef QT_DEBUG_CONVERT
88
        printf("QOutlineMapper::beginOutline rule=%d\n", fillRule);
89
#endif
90
0
        m_valid = true;
91
0
        m_elements.reset();
92
0
        m_element_types.reset();
93
0
        m_points.reset();
94
0
        m_tags.reset();
95
0
        m_contours.reset();
96
0
        m_outline.flags = fillRule == Qt::WindingFill
97
0
                          ? QT_FT_OUTLINE_NONE
98
0
                          : QT_FT_OUTLINE_EVEN_ODD_FILL;
99
0
        m_subpath_start = 0;
100
0
    }
101
102
    void endOutline();
103
104
    void clipElements(const QPointF *points, const QPainterPath::ElementType *types, int count);
105
106
    void convertElements(const QPointF *points, const QPainterPath::ElementType *types, int count);
107
108
0
    inline void moveTo(const QPointF &pt) {
109
#ifdef QT_DEBUG_CONVERT
110
        printf("QOutlineMapper::moveTo() (%f, %f)\n", pt.x(), pt.y());
111
#endif
112
0
        closeSubpath();
113
0
        m_subpath_start = m_elements.size();
114
0
        m_elements << pt;
115
0
        m_element_types << QPainterPath::MoveToElement;
116
0
    }
117
118
0
    inline void lineTo(const QPointF &pt) {
119
#ifdef QT_DEBUG_CONVERT
120
        printf("QOutlineMapper::lineTo() (%f, %f)\n", pt.x(), pt.y());
121
#endif
122
0
        m_elements.add(pt);
123
0
        m_element_types << QPainterPath::LineToElement;
124
0
    }
125
126
    void curveTo(const QPointF &cp1, const QPointF &cp2, const QPointF &ep);
127
128
0
    inline void closeSubpath() {
129
0
        int element_count = m_elements.size();
130
0
        if (element_count > 0) {
131
0
            if (m_elements.at(element_count-1) != m_elements.at(m_subpath_start)) {
132
#ifdef QT_DEBUG_CONVERT
133
                printf(" - implicitly closing\n");
134
#endif
135
                // Put the object on the stack to avoid the odd case where
136
                // lineTo reallocs the databuffer and the QPointF & will
137
                // be invalidated.
138
0
                QPointF pt = m_elements.at(m_subpath_start);
139
140
                // only do lineTo if we have element_type array...
141
0
                if (m_element_types.size())
142
0
                    lineTo(pt);
143
0
                else
144
0
                    m_elements << pt;
145
146
0
            }
147
0
        }
148
0
    }
149
150
0
    QT_FT_Outline *outline() {
151
0
        if (m_valid)
152
0
            return &m_outline;
153
0
        return nullptr;
154
0
    }
155
156
    QT_FT_Outline *convertPath(const QPainterPath &path);
157
    QT_FT_Outline *convertPath(const QVectorPath &path);
158
159
0
    inline QPainterPath::ElementType *elementTypes() const { return m_element_types.size() == 0 ? nullptr : m_element_types.data(); }
160
161
public:
162
    QDataBuffer<QPainterPath::ElementType> m_element_types;
163
    QDataBuffer<QPointF> m_elements;
164
    QDataBuffer<QT_FT_Vector> m_points;
165
    QDataBuffer<char> m_tags;
166
    QDataBuffer<int> m_contours;
167
168
    QRect m_clip_rect;
169
    QRectF m_clip_trigger_rect;
170
    QRectF controlPointRect; // only valid after endOutline()
171
172
    QT_FT_Outline m_outline;
173
174
    int m_subpath_start;
175
176
    QTransform m_transform;
177
178
    qreal m_curve_threshold;
179
180
    bool m_valid;
181
    bool m_in_clip_elements;
182
};
183
184
QT_END_NAMESPACE
185
186
#endif // QOUTLINEMAPPER_P_H