25#include <QRegularExpressionMatch>
29#include <finders_interface.h>
33class QgsCharTextureRect
36 QgsCharTextureRect(
const QString &grapheme,
const QSize &boundingRectSize,
const QPoint &characterOffsetFromOrigin,
int texturePaddingPixels )
37 : grapheme( grapheme )
38 , boundingRectSize( boundingRectSize )
39 , characterOffsetFromOrigin( characterOffsetFromOrigin )
41 paddedRect = rectpack2D::rect_xywh( 0, 0, boundingRectSize.width() + 2 * texturePaddingPixels, boundingRectSize.height() + 2 * texturePaddingPixels );
49 const auto &get_rect()
const
56 return QRect( paddedRect.x, paddedRect.y, paddedRect.w, paddedRect.h );
61 QSize boundingRectSize;
63 QPoint characterOffsetFromOrigin;
65 rectpack2D::rect_xywh paddedRect;
78 return static_cast< int >( mRects.size() );
83 auto it = mGraphemeIndices.constFind( grapheme );
84 if ( it == mGraphemeIndices.constEnd() )
87 return mRects[it.value()].asQRect();
92 auto it = mStringMetrics.constFind(
string );
93 if ( it == mStringMetrics.constEnd() )
96 return it->graphemeMetrics.count();
101 auto it = mStringMetrics.constFind(
string );
102 if ( it == mStringMetrics.constEnd() )
105 return it->totalWidth;
110 auto it = mStringMetrics.constFind(
string );
111 if ( it == mStringMetrics.constEnd() )
114 const GraphemeMetric &graphemeMetrics = it.value().graphemeMetrics[graphemeIndex];
115 auto charIt = mGraphemeIndices.constFind( graphemeMetrics.grapheme );
116 if ( charIt == mGraphemeIndices.constEnd() )
119 return QPoint( graphemeMetrics.horizontalAdvance, -( mRects[charIt.value()].boundingRectSize.height() + mRects[charIt.value()].characterOffsetFromOrigin.y() ) );
124 auto it = mStringMetrics.constFind(
string );
125 if ( it == mStringMetrics.constEnd() )
128 const GraphemeMetric &graphemeMetrics = it.value().graphemeMetrics.value( graphemeIndex );
129 auto charIt = mGraphemeIndices.constFind( graphemeMetrics.grapheme );
130 if ( charIt == mGraphemeIndices.constEnd() )
133 return mRects[charIt.value()].asQRect();
138 if ( mAtlasSize.isEmpty() )
141 QImage res( mAtlasSize, QImage::Format_ARGB32_Premultiplied );
142 res.fill( Qt::transparent );
144 QPainter painter( &res );
146 for (
const QgsCharTextureRect &
rect : mRects )
157 if ( mAtlasSize.isEmpty() )
160 QImage res( mAtlasSize, QImage::Format_ARGB32_Premultiplied );
161 res.fill( Qt::transparent );
163 QPainter painter( &res );
164 painter.setPen( Qt::NoPen );
168 for (
const QgsCharTextureRect &
rect : mRects )
170 const QColor color = ramp.
color( index / (
static_cast< int >( mRects.size() ) - 1 ) );
172 painter.setBrush( QBrush( color ) );
173 painter.drawRect(
rect.asQRect() );
191 int texturePaddingPixels = 2;
198 QSet<QString> uniqueGraphemes;
199 QMap< QString, QgsFontTextureAtlas::StringMetrics > stringMetrics;
200 for (
const QString &
string : strings )
204 QgsFontTextureAtlas::StringMetrics thisStringMetrics;
205 thisStringMetrics.totalWidth = fontMetrics.boundingRect(
string ).width();
206 thisStringMetrics.graphemeMetrics.reserve( graphemes.size() );
207 QString currentString;
208 for (
const QString &grapheme : graphemes )
210 uniqueGraphemes.insert( grapheme );
211 thisStringMetrics.graphemeMetrics << QgsFontTextureAtlas::GraphemeMetric(
static_cast< int >( std::round( fontMetrics.horizontalAdvance( currentString ) ) ), grapheme );
212 currentString += grapheme;
214 stringMetrics.insert(
string, thisStringMetrics );
217 if ( uniqueGraphemes.isEmpty() )
223 std::vector<QgsCharTextureRect> charRects;
224 charRects.reserve( uniqueGraphemes.size() );
225 for (
const QString &
c : uniqueGraphemes )
227 const thread_local QRegularExpression sWhitespaceRx( QStringLiteral(
"^\\s+$" ) );
228 if ( sWhitespaceRx.match(
c ).hasMatch() )
231 const QRect boundingRect =
c.size() == 1 ? fontMetrics.boundingRect(
c.at( 0 ) ).toRect() : fontMetrics.boundingRect(
c ).toRect();
232 charRects.emplace_back( QgsCharTextureRect(
c, boundingRect.size(), boundingRect.topLeft(), texturePaddingPixels ) );
236 using spacesType = rectpack2D::empty_spaces<false, rectpack2D::default_empty_spaces>;
239 auto reportSuccessful = []( rectpack2D::rect_xywh & ) {
240 return rectpack2D::callback_result::CONTINUE_PACKING;
243 auto reportUnsuccessful = [&result]( rectpack2D::rect_xywh & ) {
245 return rectpack2D::callback_result::ABORT_PACKING;
248 const auto discardStep = -4;
250 auto byWidth = [](
const rectpack2D::rect_xywh *a,
const rectpack2D::rect_xywh *b ) {
254 const rectpack2D::rect_wh resultSize = rectpack2D::find_best_packing<spacesType>(
256 rectpack2D::make_finder_input(
261 rectpack2D::flipping_option::DISABLED
270 res.mFormat = format;
271 res.mRects = std::move( charRects );
272 res.mAtlasSize = QSize( resultSize.w, resultSize.h );
273 res.mStringMetrics = std::move( stringMetrics );
274 res.mTexturePaddingPixels = texturePaddingPixels;
277 for (
const QgsCharTextureRect &r : res.mRects )
279 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