Skip to content

Commit

Permalink
DXF export: handle line breaks in label text. Line breaks in TEXT or …
Browse files Browse the repository at this point in the history
…MTEXT makes dxf viewers hang or crash
  • Loading branch information
mhugent committed Feb 20, 2020
1 parent ced0ed4 commit 697bad9
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/core/dxf/qgsdxfexport.cpp
Expand Up @@ -2314,10 +2314,14 @@ void QgsDxfExport::drawLabel( const QString &layerId, QgsRenderContext &context,

if ( mFlags & FlagNoMText )
{
txt.replace( QChar( QChar::LineFeed ), ' ' );
txt.replace( QChar( QChar::CarriageReturn ), ' ' );
writeText( dxfLayer, txt, label, tmpLyr, context.expressionContext() );
}
else
{
txt.replace( QString( QChar( QChar::CarriageReturn ) ) + QString( QChar( QChar::LineFeed ) ), QStringLiteral( "\\P" ) );
txt.replace( QChar( QChar::CarriageReturn ), QStringLiteral( "\\P" ) );
txt = txt.replace( wrapchr, QLatin1String( "\\P" ) );
txt.replace( " ", "\\~" );

Expand Down
43 changes: 43 additions & 0 deletions tests/src/core/testqgsdxfexport.cpp
Expand Up @@ -52,6 +52,7 @@ class TestQgsDxfExport : public QObject
void testMtext();
void testMtext_data();
void testMTextEscapeSpaces();
void testMTextEscapeLineBreaks();
void testText();
void testTextAngle();
void testTextAlign();
Expand Down Expand Up @@ -398,6 +399,48 @@ void TestQgsDxfExport::testMTextEscapeSpaces()
QVERIFY2( fileContainsText( file, "\\fQGIS Vera Sans|i0|b1;\\H3.81136;A\\~text\\~with\\~spaces", &debugInfo ), debugInfo.toUtf8().constData() );
}

void TestQgsDxfExport::testMTextEscapeLineBreaks()
{
int field = mPointLayerNoSymbols->addExpressionField( QStringLiteral("'A text with ' || char(13) || char(10) || 'line break'"), QgsField( QStringLiteral( "linebreaktest" ), QVariant::String ) );

QgsPalLayerSettings settings;
settings.fieldName = QStringLiteral( "linebreaktest" );
QgsTextFormat format;
format.setFont( QgsFontUtils::getStandardTestFont( QStringLiteral( "Bold" ) ).family() );
format.setSize( 12 );
format.setNamedStyle( QStringLiteral( "Bold" ) );
format.setColor( QColor( 200, 0, 200 ) );
settings.setFormat( format );
mPointLayerNoSymbols->setLabeling( new QgsVectorLayerSimpleLabeling( settings ) );
mPointLayerNoSymbols->setLabelsEnabled( true );

QgsDxfExport d;
d.addLayers( QList< QgsDxfExport::DxfLayer >() << QgsDxfExport::DxfLayer( mPointLayerNoSymbols ) );

QgsMapSettings mapSettings;
QSize size( 640, 480 );
mapSettings.setOutputSize( size );
mapSettings.setExtent( mPointLayerNoSymbols->extent() );
mapSettings.setLayers( QList<QgsMapLayer *>() << mPointLayerNoSymbols );
mapSettings.setOutputDpi( 96 );
mapSettings.setDestinationCrs( mPointLayerNoSymbols->crs() );

d.setMapSettings( mapSettings );
d.setSymbologyScale( 1000 );
d.setSymbologyExport( QgsDxfExport::FeatureSymbology );

QString file = getTempFileName( "mtext_escape_linebreaks" );
QFile dxfFile( file );
QCOMPARE( d.writeToFile( &dxfFile, QStringLiteral( "CP1252" ) ), QgsDxfExport::ExportResult::Success );
dxfFile.close();

dxfFile.open( QIODevice::ReadOnly );
QString fileContent = QTextStream( &dxfFile ).readAll();
dxfFile.close();
QVERIFY( fileContent.contains( "A\\~text\\~with\\~\\Pline\\~break" ) );
mPointLayerNoSymbols->removeExpressionField( field );
}

void TestQgsDxfExport::testText()
{
QgsPalLayerSettings settings;
Expand Down

0 comments on commit 697bad9

Please sign in to comment.