22#include <QTextDocument>
32 mBlocks.append( block );
43 document.
reserve( lines.size() );
44 for (
const QString &line : lines )
54#define TAB_REPLACEMENT_MARKER " ignore_me_i_am_a_tab "
60 document.
reserve( lines.size() );
62 for (
const QString &l : std::as_const( lines ) )
69 QTextDocument sourceDoc;
80 const thread_local QRegularExpression sRxWordSpacingFix( QStringLiteral(
"word-spacing:\\s*(-?\\d+(?:\\.\\d+)?)([a-zA-Z]*)" ) );
81 line.replace( sRxWordSpacingFix, QStringLiteral(
"word-spacing: \\1px" ) );
83 sourceDoc.setHtml( line );
85 QTextBlock sourceBlock = sourceDoc.firstBlock();
89 const int headingLevel = sourceBlock.blockFormat().headingLevel();
91 if ( headingLevel > 0 )
93 switch ( headingLevel )
118 auto it = sourceBlock.begin();
121 while ( !it.atEnd() )
123 const QTextFragment fragment = it.fragment();
124 if ( fragment.isValid() )
127 const QString fragmentText = fragment.text();
128 if ( fragmentText.contains( QStringLiteral(
"\u2028" ) ) )
131 const QStringList
splitLines = fragmentText.split( QStringLiteral(
"\u2028" ), Qt::SplitBehaviorFlags::SkipEmptyParts );
133 for (
const QString &splitLine : std::as_const(
splitLines ) )
140 previousFormat = &document.
at( document.
size() - 1 ).
at( 0 ).characterFormat();
150 if ( previousFormat )
153 newFormat.overrideWith( *previousFormat );
160 for (
const QString &part : tabSplit )
162 if ( !part.isEmpty() )
165 block.
append( splitFragment );
167 if ( index != tabSplit.size() - 1 )
177 splitFragment.
setText( splitLine );
181 if ( previousFormat )
184 newFormat.overrideWith( *previousFormat );
188 block.
append( splitFragment );
208 for (
const QString &part : tabSplit )
210 if ( !part.isEmpty() )
213 block.
append( tmpFragment );
215 if ( index != tabSplit.size() - 1 )
229 block.
append( tmpFragment );
235 if ( !block.
empty() )
238 sourceBlock = sourceBlock.next();
239 if ( !sourceBlock.isValid() )
249 mBlocks.append( block );
254 mBlocks.push_back( block );
259 mBlocks.reserve( count );
264 return mBlocks.at( i );
274 return mBlocks.size();
279 QStringList textLines;
280 textLines.reserve( mBlocks.size() );
286 line.append( fragment.text() );
295 const QVector< QgsTextBlock > prevBlocks = mBlocks;
297 mBlocks.reserve( prevBlocks.size() );
304 QStringList thisParts;
305 if ( !wrapCharacter.isEmpty() && wrapCharacter != QLatin1String(
"\n" ) )
308 const QStringList lines = fragment.text().split( wrapCharacter );
309 for (
const QString &line : lines )
311 thisParts.append( line.split(
'\n' ) );
316 thisParts = fragment.text().split(
'\n' );
320 if ( autoWrapLength != 0 )
322 QStringList autoWrappedLines;
323 autoWrappedLines.reserve( thisParts.count() );
324 for (
const QString &line : std::as_const( thisParts ) )
326 autoWrappedLines.append(
QgsStringUtils::wordWrap( line, autoWrapLength, useMaxLineLengthWhenAutoWrapping ).split(
'\n' ) );
328 thisParts = autoWrappedLines;
331 if ( thisParts.empty() )
333 else if ( thisParts.size() == 1 )
334 destinationBlock.
append( fragment );
337 if ( !thisParts.at( 0 ).isEmpty() )
340 append( destinationBlock );
341 destinationBlock.
clear();
342 for (
int i = 1 ; i < thisParts.size() - 1; ++i )
348 destinationBlock.
append(
QgsTextFragment( thisParts.at( thisParts.size() - 1 ), fragment.characterFormat() ) );
351 append( destinationBlock );
359 block.applyCapitalization( capitalization );
364QVector< QgsTextBlock >::const_iterator QgsTextDocument::begin()
const
366 return mBlocks.begin();
369QVector< QgsTextBlock >::const_iterator QgsTextDocument::end()
const
371 return mBlocks.end();
Capitalization
String capitalization options.
static QString wordWrap(const QString &string, int length, bool useMaxLineLength=true, const QString &customDelimiter=QString())
Automatically wraps a string by inserting new line characters at appropriate locations in the string.
Stores information relating to individual block formatting.
Represents a block of text consisting of one or more QgsTextFragment objects.
void clear()
Clears the block, removing all its contents.
static QgsTextBlock fromPlainText(const QString &text, const QgsTextCharacterFormat &format=QgsTextCharacterFormat())
Constructor for QgsTextBlock consisting of a plain text, and optional character format.
void setBlockFormat(const QgsTextBlockFormat &format)
Sets the block format for the fragment.
void append(const QgsTextFragment &fragment)
Appends a fragment to the block.
bool empty() const
Returns true if the block is empty.
Stores information relating to individual character formatting.
void overrideWith(const QgsTextCharacterFormat &other)
Override all the default/unset properties of the current character format with the settings from anot...
void setFontPercentageSize(double size)
Sets the font percentage size (as fraction of inherited font size).
Represents a document consisting of one or more QgsTextBlock objects.
void splitLines(const QString &wrapCharacter, int autoWrapLength=0, bool useMaxLineLengthWhenAutoWrapping=true)
Splits lines of text in the document to separate lines, using a specified wrap character (wrapCharact...
QgsTextBlock & operator[](int index)
Returns the block at the specified index.
const QgsTextBlock & at(int index) const
Returns the block at the specified index.
void reserve(int count)
Reserves the specified count of blocks for optimised block appending.
QStringList toPlainText() const
Returns a list of plain text lines of text representing the document.
int size() const
Returns the number of blocks in the document.
static QgsTextDocument fromHtml(const QStringList &lines)
Constructor for QgsTextDocument consisting of a set of HTML formatted lines.
static QgsTextDocument fromPlainText(const QStringList &lines)
Constructor for QgsTextDocument consisting of a set of plain text lines.
void append(const QgsTextBlock &block)
Appends a block to the document.
void applyCapitalization(Qgis::Capitalization capitalization)
Applies a capitalization style to the document's text.
Stores a fragment of document along with formatting overrides to be used when rendering the fragment.
void setText(const QString &text)
Sets the text content of the fragment.
void setCharacterFormat(const QgsTextCharacterFormat &format)
Sets the character format for the fragment.
const QgsTextCharacterFormat & characterFormat() const
Returns the character formatting for the fragment.
#define TAB_REPLACEMENT_MARKER