QGIS API Documentation 3.99.0-Master (d270888f95f)
Loading...
Searching...
No Matches
qgsfonttextureatlasgenerator.h
Go to the documentation of this file.
1/***************************************************************************
2 qgsfonttextureatlasgenerator.h
3 --------------------------------------
4 Date : September 2025
5 Copyright : (C) 2025 by Nyall Dawson
6 Email : nyall dot dawson at gmail dot com
7 ***************************************************************************
8 * *
9 * This program is free software; you can redistribute it and/or modify *
10 * it under the terms of the GNU General Public License as published by *
11 * the Free Software Foundation; either version 2 of the License, or *
12 * (at your option) any later version. *
13 * *
14 ***************************************************************************/
15
16#ifndef QGSFONTTEXTUREATLASGENERATOR_H
17#define QGSFONTTEXTUREATLASGENERATOR_H
18
19#include <vector>
20
21#include "qgis_3d.h"
22#include "qgstextformat.h"
23
24#include <QImage>
25#include <QRect>
26#include <QString>
27
28using namespace Qt::StringLiterals;
29
31class QgsCharTextureRect;
33
34
46class _3D_EXPORT QgsFontTextureAtlas
47{
48 public:
51
54
58 bool isValid() const { return mAtlasSize.isValid(); }
59
64 QSize atlasSize() const { return mAtlasSize; }
65
66#ifndef SIP_RUN
70 QRect rect( const QString &grapheme ) const;
71#else
72
78 QRect rect( const QString &grapheme ) const;
79 //%MethodCode
80 const QRect res = sipCpp->rect( *a0 );
81 if ( res.isNull() )
82 {
83 PyErr_SetString( PyExc_KeyError, u"No rectangle for character %1 exists."_s.arg( *a0 ).toUtf8().constData() );
84 sipIsErr = 1;
85 }
86 else
87 {
88 return sipConvertFromNewType( new QRect( res ), sipType_QRect, Py_None );
89 }
90 //%End
91#endif
92
99 int graphemeCount( const QString &string ) const;
100
107 int totalWidth( const QString &string ) const;
108
115 QPoint pixelOffsetForGrapheme( const QString &string, int graphemeIndex ) const;
116
123 QRect textureRectForGrapheme( const QString &string, int graphemeIndex ) const;
124
128 QImage renderAtlasTexture() const;
129
136 QImage renderDebugTexture() const;
137
141 int count() const;
142
143#ifdef SIP_RUN
144 int __len__() const;
145 % Docstring
146 Returns the number of textures in the atlas.
147 % End
148 //%MethodCode
149 sipRes
150 = sipCpp->count();
151 //% End
152#endif
153
154 private:
155 struct GraphemeMetric
156 {
157 GraphemeMetric( int horizontalAdvance = 0, const QString &grapheme = QString() )
158 : horizontalAdvance( horizontalAdvance )
159 , grapheme( grapheme )
160 {}
161
162 int horizontalAdvance = 0;
163 QString grapheme;
164 };
165
166 struct StringMetrics
167 {
168 int totalWidth = 0;
169 QVector< GraphemeMetric > graphemeMetrics;
170 };
171
172 QgsTextFormat mFormat;
173 std::vector< QgsCharTextureRect > mRects;
174 QSize mAtlasSize;
175 QHash< QString, int > mGraphemeIndices;
176 QMap< QString, StringMetrics > mStringMetrics;
177 int mTexturePaddingPixels = 0;
178
180};
181
189{
190 public:
194 static QgsFontTextureAtlas create( const QgsTextFormat &format, const QStringList &strings );
195};
196
197#endif // QGSFONTTEXTUREATLASGENERATOR_H
Generates texture atlases for a font by efficiently packing the characters required for a set of stri...
static QgsFontTextureAtlas create(const QgsTextFormat &format, const QStringList &strings)
Creates the texture atlas for a set of strings, using the specified text format.
Encapsulates a font texture atlas.
QSize atlasSize() const
Returns the total size required for the atlas, i.e.
bool isValid() const
Returns true if the atlas is valid.
QgsFontTextureAtlas(const QgsFontTextureAtlas &other)
QgsFontTextureAtlas & operator=(const QgsFontTextureAtlas &other)
Container for all settings relating to text rendering.