Skip to content

Commit

Permalink
Add method to extract blocks from HTML text for labeling
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson committed May 12, 2020
1 parent 2cbea02 commit 87296f2
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 0 deletions.
9 changes: 9 additions & 0 deletions python/core/auto_generated/qgstextrenderer.sip.in
Expand Up @@ -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)
Expand Down
33 changes: 33 additions & 0 deletions src/core/qgstextrenderer.cpp
Expand Up @@ -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();
Expand Down Expand Up @@ -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 )
{
Expand Down
9 changes: 9 additions & 0 deletions src/core/qgstextrenderer.h
Expand Up @@ -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
Expand Down
7 changes: 7 additions & 0 deletions tests/src/python/test_qgstextrenderer.py
Expand Up @@ -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)
Expand Down

0 comments on commit 87296f2

Please sign in to comment.