Skip to content

Commit

Permalink
Truncate text on line annotation text when line is too short instead
Browse files Browse the repository at this point in the history
of hiding all text
  • Loading branch information
nyalldawson committed Mar 31, 2023
1 parent c8db000 commit bb66189
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 4 deletions.
1 change: 1 addition & 0 deletions src/core/textrenderer/qgstextrenderer.cpp
Expand Up @@ -295,6 +295,7 @@ void QgsTextRenderer::drawDocumentOnLine( const QPolygonF &line, const QgsTextFo
QgsTextRendererUtils::RespectPainterOrientation,
-1, -1,
QgsTextRendererUtils::CurvedTextFlag::UseBaselinePlacement
| QgsTextRendererUtils::CurvedTextFlag::TruncateStringWhenLineIsTooShort
| QgsTextRendererUtils::CurvedTextFlag::UprightCharactersOnly );

if ( placement->graphemePlacement.empty() )
Expand Down
24 changes: 20 additions & 4 deletions src/core/textrenderer/qgstextrendererutils.cpp
Expand Up @@ -215,7 +215,7 @@ std::unique_ptr< QgsTextRendererUtils::CurvePlacementProperties > QgsTextRendere
return output;
}

const int characterCount = metrics.count();
int characterCount = metrics.count();

if ( direction == RespectPainterOrientation && !isSecondAttempt )
{
Expand All @@ -234,7 +234,15 @@ std::unique_ptr< QgsTextRendererUtils::CurvePlacementProperties > QgsTextRendere
double characterStartX, characterStartY;
if ( !nextCharPosition( characterWidth, pathDistances[endindex], x, y, numPoints, endindex, distance, characterStartX, characterStartY, endLabelX, endLabelY ) )
{
return output;
if ( flags & QgsTextRendererUtils::CurvedTextFlag::TruncateStringWhenLineIsTooShort )
{
characterCount = i + 1;
break;
}
else
{
return output;
}
}
if ( i == 0 )
{
Expand Down Expand Up @@ -289,8 +297,16 @@ std::unique_ptr< QgsTextRendererUtils::CurvePlacementProperties > QgsTextRendere
double characterEndY = 0;
if ( !nextCharPosition( characterWidth, pathDistances[index], x, y, numPoints, index, offsetAlongSegment, characterStartX, characterStartY, characterEndX, characterEndY ) )
{
output->graphemePlacement.clear();
return output;
if ( flags & QgsTextRendererUtils::CurvedTextFlag::TruncateStringWhenLineIsTooShort )
{
characterCount = i + 1;
break;
}
else
{
output->graphemePlacement.clear();
return output;
}
}

// Calculate angle from the start of the character to the end based on start/end of character
Expand Down
1 change: 1 addition & 0 deletions src/core/textrenderer/qgstextrendererutils.h
Expand Up @@ -148,6 +148,7 @@ class CORE_EXPORT QgsTextRendererUtils
*/
enum class CurvedTextFlag : int
{
TruncateStringWhenLineIsTooShort = 1 << 0, //!< When a string is too long for the line, truncate characters instead of aborting the placement
UseBaselinePlacement = 1 << 1, //!< Generate placement based on the character baselines instead of centers
UprightCharactersOnly = 1 << 2, //!< Permit upright characters only. If not present then upside down text placement is permitted.
};
Expand Down
31 changes: 31 additions & 0 deletions tests/src/python/test_qgsannotationlinetextitem.py
Expand Up @@ -205,6 +205,37 @@ def testRenderLine(self):

self.assertTrue(self.imageCheck('linetext_item', 'linetext_item', image))

def testRenderLineTruncate(self):
item = QgsAnnotationLineTextItem('my text', QgsLineString(((12, 13), (13, 13.1), (14, 12))))

format = QgsTextFormat.fromQFont(getTestFont('Bold'))
format.setColor(QColor(255, 0, 0))
format.setOpacity(150 / 255)
format.setSize(75)
item.setFormat(format)

settings = QgsMapSettings()
settings.setDestinationCrs(QgsCoordinateReferenceSystem('EPSG:4326'))
settings.setExtent(QgsRectangle(11.9, 11.9, 14.5, 14))
settings.setOutputSize(QSize(600, 300))

settings.setFlag(QgsMapSettings.Antialiasing, False)

rc = QgsRenderContext.fromMapSettings(settings)
image = QImage(600, 300, QImage.Format_ARGB32)
image.setDotsPerMeterX(int(96 / 25.4 * 1000))
image.setDotsPerMeterY(int(96 / 25.4 * 1000))
image.fill(QColor(255, 255, 255))
painter = QPainter(image)
rc.setPainter(painter)

try:
item.render(rc, None)
finally:
painter.end()

self.assertTrue(self.imageCheck('linetext_item_truncate', 'linetext_item_truncate', image))

def testRenderLineTextExpression(self):
item = QgsAnnotationLineTextItem('[% 1 + 1.5 %]', QgsLineString(((12, 13), (13, 13.1), (14, 12))))

Expand Down
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 bb66189

Please sign in to comment.