Skip to content

Commit

Permalink
Merge pull request qgis#60299 from nyalldawson/fix_html_multiple_tabs
Browse files Browse the repository at this point in the history
Fix handling of multiple consecutive tabs with HTML text rendering
  • Loading branch information
troopa81 authored Jan 29, 2025
2 parents 5c1f7bb + c2782ff commit d1c2651
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 2 deletions.
9 changes: 7 additions & 2 deletions src/core/textrenderer/qgstextdocument.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,10 @@ QgsTextDocument QgsTextDocument::fromPlainText( const QStringList &lines )
// a html or css tag doesn't mess things up. Instead, Qt will just silently
// ignore html attributes it doesn't know about, like this replacement string
#define TAB_REPLACEMENT_MARKER " ignore_me_i_am_a_tab "
// when splitting by the tab replacement marker we need to be tolerant to the
// spaces surrounding REPLACEMENT_MARKER being swallowed when multiple consecutive
// tab characters exist
#define TAB_REPLACEMENT_MARKER_RX " ?ignore_me_i_am_a_tab ?"

QgsTextDocument QgsTextDocument::fromHtml( const QStringList &lines )
{
Expand All @@ -73,6 +77,7 @@ QgsTextDocument QgsTextDocument::fromHtml( const QStringList &lines )
// by first replacing it with a string which QTextDocument won't mess with, and then
// handle these markers as tab characters in the parsed HTML document.
line.replace( QString( '\t' ), QStringLiteral( TAB_REPLACEMENT_MARKER ) );
const thread_local QRegularExpression sTabReplacementMarkerRx( QStringLiteral( TAB_REPLACEMENT_MARKER_RX ) );

// cheat a little. Qt css requires some properties to have the "px" suffix. But we don't treat these properties
// as pixels, because that doesn't scale well with different dpi render targets! So let's instead use just instead treat the suffix as
Expand Down Expand Up @@ -162,7 +167,7 @@ QgsTextDocument QgsTextDocument::fromHtml( const QStringList &lines )
}
splitFragment.setCharacterFormat( newFormat );

const QStringList tabSplit = splitLine.split( QStringLiteral( TAB_REPLACEMENT_MARKER ) );
const QStringList tabSplit = splitLine.split( sTabReplacementMarkerRx );
int index = 0;
for ( const QString &part : tabSplit )
{
Expand Down Expand Up @@ -210,7 +215,7 @@ QgsTextDocument QgsTextDocument::fromHtml( const QStringList &lines )
newFormat.overrideWith( blockFormat );
tmpFragment.setCharacterFormat( newFormat );

const QStringList tabSplit = fragmentText.split( QStringLiteral( TAB_REPLACEMENT_MARKER ) );
const QStringList tabSplit = fragmentText.split( sTabReplacementMarkerRx );
int index = 0;
for ( const QString &part : tabSplit )
{
Expand Down
14 changes: 14 additions & 0 deletions tests/src/python/test_qgstextrenderer.py
Original file line number Diff line number Diff line change
Expand Up @@ -3656,6 +3656,20 @@ def testDrawTabFixedSize(self):
self.checkRender(format, "text_tab_fixed_size", text=["with\ttabs", "a\tb"])
)

def testDrawTabsMultipleHtmlFixedSize(self):
format = QgsTextFormat()
format.setFont(getTestFont("bold"))
format.setSize(20)
format.setAllowHtmlFormatting(True)
format.setSizeUnit(QgsUnitTypes.RenderUnit.RenderPoints)
format.setTabStopDistance(20)
format.setTabStopDistanceUnit(Qgis.RenderUnit.Millimeters)
self.assertTrue(
self.checkRender(
format, "text_tab_multiple_html", text=["with\t\ttabs", "a\t\tb"]
)
)

def testDrawTabPositionsFixedSize(self):
format = QgsTextFormat()
format.setFont(getTestFont("bold"))
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit d1c2651

Please sign in to comment.