| 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 | #ifndef QTEXTUREGLYPHCACHE_P_H |
| 41 | #define QTEXTUREGLYPHCACHE_P_H |
| 42 | |
| 43 | // |
| 44 | // W A R N I N G |
| 45 | // ------------- |
| 46 | // |
| 47 | // This file is not part of the Qt API. It exists for the convenience |
| 48 | // of other Qt classes. This header file may change from version to |
| 49 | // version without notice, or even be removed. |
| 50 | // |
| 51 | // We mean it. |
| 52 | // |
| 53 | |
| 54 | #include <QtGui/private/qtguiglobal_p.h> |
| 55 | #include <qhash.h> |
| 56 | #include <qimage.h> |
| 57 | #include <qobject.h> |
| 58 | #include <qtransform.h> |
| 59 | |
| 60 | #include <private/qfontengineglyphcache_p.h> |
| 61 | |
| 62 | #ifndef QT_DEFAULT_TEXTURE_GLYPH_CACHE_WIDTH |
| 63 | #define QT_DEFAULT_TEXTURE_GLYPH_CACHE_WIDTH 256 |
| 64 | #endif |
| 65 | |
| 66 | struct glyph_metrics_t; |
| 67 | typedef unsigned int glyph_t; |
| 68 | |
| 69 | |
| 70 | QT_BEGIN_NAMESPACE |
| 71 | |
| 72 | class QTextItemInt; |
| 73 | |
| 74 | class Q_GUI_EXPORT QTextureGlyphCache : public QFontEngineGlyphCache |
| 75 | { |
| 76 | public: |
| 77 | QTextureGlyphCache(QFontEngine::GlyphFormat format, const QTransform &matrix, const QColor &color = QColor()) |
| 78 | : QFontEngineGlyphCache(format, matrix, color), m_current_fontengine(nullptr), |
| 79 | m_w(0), m_h(0), m_cx(0), m_cy(0), m_currentRowHeight(0) |
| 80 | { } |
| 81 | |
| 82 | ~QTextureGlyphCache(); |
| 83 | |
| 84 | struct GlyphAndSubPixelPosition |
| 85 | { |
| 86 | GlyphAndSubPixelPosition(glyph_t g, QFixed spp) : glyph(g), subPixelPosition(spp) {} |
| 87 | |
| 88 | bool operator==(const GlyphAndSubPixelPosition &other) const |
| 89 | { |
| 90 | return glyph == other.glyph && subPixelPosition == other.subPixelPosition; |
| 91 | } |
| 92 | |
| 93 | glyph_t glyph; |
| 94 | QFixed subPixelPosition; |
| 95 | }; |
| 96 | |
| 97 | struct Coord { |
| 98 | int x; |
| 99 | int y; |
| 100 | int w; |
| 101 | int h; |
| 102 | |
| 103 | int baseLineX; |
| 104 | int baseLineY; |
| 105 | |
| 106 | bool isNull() const |
| 107 | { |
| 108 | return w == 0 || h == 0; |
| 109 | } |
| 110 | }; |
| 111 | |
| 112 | bool populate(QFontEngine *fontEngine, int numGlyphs, const glyph_t *glyphs, |
| 113 | const QFixedPoint *positions); |
| 114 | bool hasPendingGlyphs() const { return !m_pendingGlyphs.isEmpty(); }; |
| 115 | void fillInPendingGlyphs(); |
| 116 | |
| 117 | virtual void createTextureData(int width, int height) = 0; |
| 118 | virtual void resizeTextureData(int width, int height) = 0; |
| 119 | virtual int glyphPadding() const { return 0; } |
| 120 | |
| 121 | virtual void beginFillTexture() { } |
| 122 | virtual void fillTexture(const Coord &coord, glyph_t glyph, QFixed subPixelPosition) = 0; |
| 123 | virtual void endFillTexture() { } |
| 124 | |
| 125 | inline void createCache(int width, int height) { |
| 126 | m_w = width; |
| 127 | m_h = height; |
| 128 | createTextureData(width, height); |
| 129 | } |
| 130 | |
| 131 | inline void resizeCache(int width, int height) |
| 132 | { |
| 133 | resizeTextureData(width, height); |
| 134 | m_w = width; |
| 135 | m_h = height; |
| 136 | } |
| 137 | |
| 138 | inline bool isNull() const { return m_h == 0; } |
| 139 | |
| 140 | QHash<GlyphAndSubPixelPosition, Coord> coords; |
| 141 | virtual int maxTextureWidth() const { return QT_DEFAULT_TEXTURE_GLYPH_CACHE_WIDTH; } |
| 142 | virtual int maxTextureHeight() const { return -1; } |
| 143 | |
| 144 | QImage textureMapForGlyph(glyph_t g, QFixed subPixelPosition) const; |
| 145 | |
| 146 | protected: |
| 147 | int calculateSubPixelPositionCount(glyph_t) const; |
| 148 | |
| 149 | QFontEngine *m_current_fontengine; |
| 150 | QHash<GlyphAndSubPixelPosition, Coord> m_pendingGlyphs; |
| 151 | |
| 152 | int m_w; // image width |
| 153 | int m_h; // image height |
| 154 | int m_cx; // current x |
| 155 | int m_cy; // current y |
| 156 | int m_currentRowHeight; // Height of last row |
| 157 | }; |
| 158 | |
| 159 | inline uint qHash(const QTextureGlyphCache::GlyphAndSubPixelPosition &g) |
| 160 | { |
| 161 | return (g.glyph << 8) | (g.subPixelPosition * 10).round().toInt(); |
| 162 | } |
| 163 | |
| 164 | |
| 165 | class Q_GUI_EXPORT QImageTextureGlyphCache : public QTextureGlyphCache |
| 166 | { |
| 167 | public: |
| 168 | QImageTextureGlyphCache(QFontEngine::GlyphFormat format, const QTransform &matrix, const QColor &color = QColor()) |
| 169 | : QTextureGlyphCache(format, matrix, color) { } |
| 170 | ~QImageTextureGlyphCache(); |
| 171 | |
| 172 | virtual void createTextureData(int width, int height) override; |
| 173 | virtual void resizeTextureData(int width, int height) override; |
| 174 | virtual void fillTexture(const Coord &c, glyph_t glyph, QFixed subPixelPosition) override; |
| 175 | |
| 176 | inline const QImage &image() const { return m_image; } |
| 177 | |
| 178 | private: |
| 179 | QImage m_image; |
| 180 | }; |
| 181 | |
| 182 | QT_END_NAMESPACE |
| 183 | |
| 184 | #endif |
| 185 | |