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

source code of qtbase/src/gui/painting/qtextureglyphcache_p.h