Skip to content
Permalink
Browse files

Add method to extract blocks from HTML text for labeling

  • Loading branch information
nyalldawson committed May 8, 2020
1 parent 2cbea02 commit 87296f246c9a35e935171829fda94ceb573bdfdc
@@ -1940,6 +1940,15 @@ and background shapes.
AlignRight,
};

static QStringList extractTextBlocksFromHtml( const QString &html );
%Docstring
Extracts text blocks from a ``html`` string.

This removes formatting tags, and outputs lines of text which represent the text content of HTML blocks.

.. versionadded:: 3.14
%End

static int sizeToPixel( double size, const QgsRenderContext &c, QgsUnitTypes::RenderUnit unit, const QgsMapUnitScale &mapUnitScale = QgsMapUnitScale() );
%Docstring
Calculates pixel size (considering output size should be in pixel or map units, scale factors and optionally oversampling)
@@ -29,6 +29,8 @@
#include "qgspainterswapper.h"
#include <QFontDatabase>
#include <QDesktopWidget>
#include <QTextDocument>
#include <QTextBlock>

Q_GUI_EXPORT extern int qt_defaultDpiX();
Q_GUI_EXPORT extern int qt_defaultDpiY();
@@ -2603,6 +2605,37 @@ QPixmap QgsTextFormat::textFormatPreviewPixmap( const QgsTextFormat &format, QSi
return pixmap;
}

QStringList QgsTextRenderer::extractTextBlocksFromHtml( const QString &html )
{
QStringList res;

QTextDocument doc;
doc.setHtml( html );

QTextBlock block = doc.firstBlock();
while ( true )
{
auto it = block.begin();
QString blockText;
while ( !it.atEnd() )
{
const QTextFragment fragment = it.fragment();
if ( fragment.isValid() )
{
blockText.append( fragment.text() );
}
it++;
}
if ( !blockText.isEmpty() )
res << blockText;

block = block.next();
if ( !block.isValid() )
break;
}

return res;
}

int QgsTextRenderer::sizeToPixel( double size, const QgsRenderContext &c, QgsUnitTypes::RenderUnit unit, const QgsMapUnitScale &mapUnitScale )
{
@@ -1670,6 +1670,15 @@ class CORE_EXPORT QgsTextRenderer
AlignRight, //!< Right align
};

/**
* Extracts text blocks from a \a html string.
*
* This removes formatting tags, and outputs lines of text which represent the text content of HTML blocks.
*
* \since QGIS 3.14
*/
static QStringList extractTextBlocksFromHtml( const QString &html );

/**
* Calculates pixel size (considering output size should be in pixel or map units, scale factors and optionally oversampling)
* \param size size to convert
@@ -61,6 +61,13 @@ def tearDown(self):
with open(report_file_path, 'a') as report_file:
report_file.write(self.report)

def testExtractBlocksFromHtml(self):
self.assertEqual(QgsTextRenderer.extractTextBlocksFromHtml(''), [])
self.assertEqual(QgsTextRenderer.extractTextBlocksFromHtml('abc'), ['abc'])
self.assertEqual(QgsTextRenderer.extractTextBlocksFromHtml('abc\ndef'), ['abc def'])
self.assertEqual(QgsTextRenderer.extractTextBlocksFromHtml('abc<b>def</b>'), ['abcdef'])
self.assertEqual(QgsTextRenderer.extractTextBlocksFromHtml('abc<div><b>def</b><div>ghi</div></div>'), ['abc', 'def', 'ghi'])

def createBufferSettings(self):
s = QgsTextBufferSettings()
s.setEnabled(True)

0 comments on commit 87296f2

Please sign in to comment.
You can’t perform that action at this time.