25#include <QRegularExpressionMatch>
29using namespace Qt::StringLiterals;
32#include <finders_interface.h>
36class QgsCharTextureRect
39 QgsCharTextureRect(
const QString &grapheme,
const QSize &boundingRectSize,
const QPoint &characterOffsetFromOrigin,
int texturePaddingPixels )
40 : grapheme( grapheme )
41 , boundingRectSize( boundingRectSize )
42 , characterOffsetFromOrigin( characterOffsetFromOrigin )
44 paddedRect = rectpack2D::rect_xywh( 0, 0, boundingRectSize.width() + 2 * texturePaddingPixels, boundingRectSize.height() + 2 * texturePaddingPixels );
52 const auto &get_rect()
const
59 return QRect( paddedRect.x, paddedRect.y, paddedRect.w, paddedRect.h );
64 QSize boundingRectSize;
66 QPoint characterOffsetFromOrigin;
68 rectpack2D::rect_xywh paddedRect;
81 return static_cast< int >( mRects.size() );
86 auto it = mGraphemeIndices.constFind( grapheme );
87 if ( it == mGraphemeIndices.constEnd() )
90 return mRects[it.value()].asQRect();
95 auto it = mStringMetrics.constFind(
string );
96 if ( it == mStringMetrics.constEnd() )
99 return it->graphemeMetrics.count();
104 auto it = mStringMetrics.constFind(
string );
105 if ( it == mStringMetrics.constEnd() )
108 return it->totalWidth;
113 auto it = mStringMetrics.constFind(
string );
114 if ( it == mStringMetrics.constEnd() )
117 const GraphemeMetric &graphemeMetrics = it.value().graphemeMetrics[graphemeIndex];
118 auto charIt = mGraphemeIndices.constFind( graphemeMetrics.grapheme );
119 if ( charIt == mGraphemeIndices.constEnd() )
122 return QPoint( graphemeMetrics.horizontalAdvance, -( mRects[charIt.value()].boundingRectSize.height() + mRects[charIt.value()].characterOffsetFromOrigin.y() ) );
127 auto it = mStringMetrics.constFind(
string );
128 if ( it == mStringMetrics.constEnd() )
131 const GraphemeMetric &graphemeMetrics = it.value().graphemeMetrics.value( graphemeIndex );
132 auto charIt = mGraphemeIndices.constFind( graphemeMetrics.grapheme );
133 if ( charIt == mGraphemeIndices.constEnd() )
136 return mRects[charIt.value()].asQRect();
141 if ( mAtlasSize.isEmpty() )
144 QImage res( mAtlasSize, QImage::Format_ARGB32_Premultiplied );
145 res.fill( Qt::transparent );
147 QPainter painter( &res );
149 for (
const QgsCharTextureRect &
rect : mRects )
160 if ( mAtlasSize.isEmpty() )
163 QImage res( mAtlasSize, QImage::Format_ARGB32_Premultiplied );
164 res.fill( Qt::transparent );
166 QPainter painter( &res );
167 painter.setPen( Qt::NoPen );
171 for (
const QgsCharTextureRect &
rect : mRects )
173 const QColor color = ramp.
color( index / (
static_cast< int >( mRects.size() ) - 1 ) );
175 painter.setBrush( QBrush( color ) );
176 painter.drawRect(
rect.asQRect() );
194 int texturePaddingPixels = 2;
201 QSet<QString> uniqueGraphemes;
202 QMap< QString, QgsFontTextureAtlas::StringMetrics > stringMetrics;
203 for (
const QString &
string : strings )
207 QgsFontTextureAtlas::StringMetrics thisStringMetrics;
208 thisStringMetrics.totalWidth = fontMetrics.boundingRect(
string ).width();
209 thisStringMetrics.graphemeMetrics.reserve( graphemes.size() );
210 QString currentString;
211 for (
const QString &grapheme : graphemes )
213 uniqueGraphemes.insert( grapheme );
214 thisStringMetrics.graphemeMetrics << QgsFontTextureAtlas::GraphemeMetric(
static_cast< int >( std::round( fontMetrics.horizontalAdvance( currentString ) ) ), grapheme );
215 currentString += grapheme;
217 stringMetrics.insert(
string, thisStringMetrics );
220 if ( uniqueGraphemes.isEmpty() )
226 std::vector<QgsCharTextureRect> charRects;
227 charRects.reserve( uniqueGraphemes.size() );
228 for (
const QString &
c : uniqueGraphemes )
230 const thread_local QRegularExpression sWhitespaceRx( u
"^\\s+$"_s );
231 if ( sWhitespaceRx.match(
c ).hasMatch() )
234 const QRect boundingRect =
c.size() == 1 ? fontMetrics.boundingRect(
c.at( 0 ) ).toRect() : fontMetrics.boundingRect(
c ).toRect();
235 charRects.emplace_back( QgsCharTextureRect(
c, boundingRect.size(), boundingRect.topLeft(), texturePaddingPixels ) );
239 using spacesType = rectpack2D::empty_spaces<false, rectpack2D::default_empty_spaces>;
242 auto reportSuccessful = []( rectpack2D::rect_xywh & ) {
243 return rectpack2D::callback_result::CONTINUE_PACKING;
246 auto reportUnsuccessful = [&result]( rectpack2D::rect_xywh & ) {
248 return rectpack2D::callback_result::ABORT_PACKING;
251 const auto discardStep = -4;
253 auto byWidth = [](
const rectpack2D::rect_xywh *a,
const rectpack2D::rect_xywh *b ) {
257 const rectpack2D::rect_wh resultSize = rectpack2D::find_best_packing<spacesType>(
259 rectpack2D::make_finder_input(
264 rectpack2D::flipping_option::DISABLED
273 res.mFormat = format;
274 res.mRects = std::move( charRects );
275 res.mAtlasSize = QSize( resultSize.w, resultSize.h );
276 res.mStringMetrics = std::move( stringMetrics );
277 res.mTexturePaddingPixels = texturePaddingPixels;
280 for (
const QgsCharTextureRect &r : res.mRects )
282 res.mGraphemeIndices.insert( r.grapheme, index++ );
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.
int graphemeCount(const QString &string) const
Returns the number of graphemes to render for a given string.
QImage renderAtlasTexture() const
Renders the combined texture atlas, containing all required characters.
int totalWidth(const QString &string) const
Returns the total width (in pixels) required for a given string.
QRect textureRectForGrapheme(const QString &string, int graphemeIndex) const
Returns the packed rectangle for the texture for the matching grapheme.
QRect rect(const QString &grapheme) const
Returns the packed rectangle for the texture for the specified grapheme.
QgsFontTextureAtlas & operator=(const QgsFontTextureAtlas &other)
QImage renderDebugTexture() const
Renders a debug texture.
QPoint pixelOffsetForGrapheme(const QString &string, int graphemeIndex) const
Returns the pixel offset at which the texture for the matching grapheme should be placed.
int count() const
Returns the number of textures in the atlas.
static QStringList splitToGraphemes(const QString &text)
Splits a text string to a list of graphemes, which are the smallest allowable character divisions in ...
A color ramp consisting of random colors, constrained within component ranges.
virtual void setTotalColorCount(int colorCount)
Sets the desired total number of unique colors for the resultant ramp.
QColor color(double value) const override
Returns the color corresponding to a specified value.
Contains information about the context of a rendering operation.
void setScaleFactor(double factor)
Sets the scaling factor for the render to convert painter units to physical sizes.
double convertToPainterUnits(double size, Qgis::RenderUnit unit, const QgsMapUnitScale &scale=QgsMapUnitScale(), Qgis::RenderSubcomponentProperty property=Qgis::RenderSubcomponentProperty::Generic) const
Converts a size from the specified units to painter units (pixels).
static QgsRenderContext fromQPainter(QPainter *painter)
Creates a default render context given a pixel based QPainter destination.
Qgis::RenderUnit sizeUnit() const
Returns the units for the buffer size.
double size() const
Returns the size of the buffer.
bool enabled() const
Returns whether the buffer is enabled.
Container for all settings relating to text rendering.
QgsTextBufferSettings & buffer()
Returns a reference to the text buffer settings.
static void drawText(const QRectF &rect, double rotation, Qgis::TextHorizontalAlignment alignment, const QStringList &textLines, QgsRenderContext &context, const QgsTextFormat &format, bool drawAsOutlines=true, Qgis::TextVerticalAlignment vAlignment=Qgis::TextVerticalAlignment::Top, Qgis::TextRendererFlags flags=Qgis::TextRendererFlags(), Qgis::TextLayoutMode mode=Qgis::TextLayoutMode::Rectangle)
Draws text within a rectangle using the specified settings.
static QFontMetricsF fontMetrics(QgsRenderContext &context, const QgsTextFormat &format, double scaleFactor=1.0)
Returns the font metrics for the given text format, when rendered in the specified render context.
As part of the API refactoring and improvements which landed in the Processing API was substantially reworked from the x version This was done in order to allow much of the underlying Processing framework to be ported into c