Coverage Report

Created: 2026-03-31 07:41

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/qtbase/src/gui/painting/qtextureglyphcache_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 QTEXTUREGLYPHCACHE_P_H
6
#define QTEXTUREGLYPHCACHE_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 for the convenience
13
// of other Qt classes.  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 <qhash.h>
21
#include <qimage.h>
22
#include <qobject.h>
23
#include <qtransform.h>
24
25
#include <private/qfontengineglyphcache_p.h>
26
27
#ifndef QT_DEFAULT_TEXTURE_GLYPH_CACHE_WIDTH
28
0
#define QT_DEFAULT_TEXTURE_GLYPH_CACHE_WIDTH 256
29
#endif
30
31
struct glyph_metrics_t;
32
typedef unsigned int glyph_t;
33
34
35
QT_BEGIN_NAMESPACE
36
37
class QTextItemInt;
38
39
class Q_GUI_EXPORT QTextureGlyphCache : public QFontEngineGlyphCache
40
{
41
public:
42
    QTextureGlyphCache(QFontEngine::GlyphFormat format, const QTransform &matrix, const QColor &color = QColor())
43
0
        : QFontEngineGlyphCache(format, matrix, color), m_current_fontengine(nullptr),
44
0
                                               m_w(0), m_h(0), m_cx(0), m_cy(0), m_currentRowHeight(0)
45
0
        { }
46
47
    ~QTextureGlyphCache();
48
49
    struct GlyphAndSubPixelPosition
50
    {
51
        GlyphAndSubPixelPosition(glyph_t g, const QFixedPoint &spp)
52
0
            : glyph(g), subPixelPosition(spp) {}
53
54
        bool operator==(const GlyphAndSubPixelPosition &other) const
55
0
        {
56
0
            return glyph == other.glyph && subPixelPosition == other.subPixelPosition;
57
0
        }
58
59
        glyph_t glyph;
60
        QFixedPoint subPixelPosition;
61
    };
62
63
    struct Coord {
64
        int x;
65
        int y;
66
        int w;
67
        int h;
68
69
        int baseLineX;
70
        int baseLineY;
71
72
        bool isNull() const
73
0
        {
74
0
            return w == 0 || h == 0;
75
0
        }
76
    };
77
78
    bool populate(QFontEngine *fontEngine,
79
                  qsizetype numGlyphs,
80
                  const glyph_t *glyphs,
81
                  const QFixedPoint *positions,
82
                  QPainter::RenderHints renderHints = QPainter::RenderHints(),
83
                  bool includeGlyphCacheScale = false);
84
0
    bool hasPendingGlyphs() const { return !m_pendingGlyphs.isEmpty(); }
85
    void fillInPendingGlyphs();
86
87
    virtual void createTextureData(int width, int height) = 0;
88
    virtual void resizeTextureData(int width, int height) = 0;
89
0
    virtual int glyphPadding() const { return 0; }
90
91
0
    virtual void beginFillTexture() { }
92
    virtual void fillTexture(const Coord &coord,
93
                             glyph_t glyph,
94
                             const QFixedPoint &subPixelPosition) = 0;
95
0
    virtual void endFillTexture() { }
96
97
0
    inline void createCache(int width, int height) {
98
0
        m_w = width;
99
0
        m_h = height;
100
0
        createTextureData(width, height);
101
0
    }
102
103
    inline void resizeCache(int width, int height)
104
0
    {
105
0
        resizeTextureData(width, height);
106
0
        m_w = width;
107
0
        m_h = height;
108
0
    }
109
110
0
    inline bool isNull() const { return m_h == 0; }
111
112
    QHash<GlyphAndSubPixelPosition, Coord> coords;
113
0
    virtual int maxTextureWidth() const { return QT_DEFAULT_TEXTURE_GLYPH_CACHE_WIDTH; }
114
0
    virtual int maxTextureHeight() const { return -1; }
115
116
    QImage textureMapForGlyph(glyph_t g, const QFixedPoint &subPixelPosition) const;
117
118
protected:
119
    int calculateSubPixelPositionCount(glyph_t) const;
120
121
    QFontEngine *m_current_fontengine;
122
    QHash<GlyphAndSubPixelPosition, Coord> m_pendingGlyphs;
123
124
    int m_w; // image width
125
    int m_h; // image height
126
    int m_cx; // current x
127
    int m_cy; // current y
128
    int m_currentRowHeight; // Height of last row
129
};
130
131
inline size_t qHash(const QTextureGlyphCache::GlyphAndSubPixelPosition &g, size_t seed = 0)
132
0
{
133
0
    return qHashMulti(seed,
134
0
                      g.glyph,
135
0
                      g.subPixelPosition.x.value(),
136
0
                      g.subPixelPosition.y.value());
137
0
}
138
139
140
class Q_GUI_EXPORT QImageTextureGlyphCache : public QTextureGlyphCache
141
{
142
public:
143
    QImageTextureGlyphCache(QFontEngine::GlyphFormat format, const QTransform &matrix, const QColor &color = QColor())
144
0
        : QTextureGlyphCache(format, matrix, color) { }
145
    ~QImageTextureGlyphCache();
146
147
    virtual void createTextureData(int width, int height) override;
148
    virtual void resizeTextureData(int width, int height) override;
149
    virtual void fillTexture(const Coord &c,
150
                             glyph_t glyph,
151
                             const QFixedPoint &subPixelPosition) override;
152
153
0
    inline const QImage &image() const { return m_image; }
154
155
private:
156
    QImage m_image;
157
};
158
159
QT_END_NAMESPACE
160
161
#endif