Skip to content

Commit

Permalink
Merge pull request #6334 from mhugent/dxf_mtext_escape_spaces
Browse files Browse the repository at this point in the history
Escape blanks in MTEXT and add unit test
  • Loading branch information
mhugent committed Feb 14, 2018
2 parents b664f12 + 256e441 commit a817d0e
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/core/dxf/qgsdxfexport.cpp
Expand Up @@ -3666,7 +3666,7 @@ void QgsDxfExport::writeMText( const QString &layer, const QString &text, const
writeGroup( 3, t.left( 250 ) );
t = t.mid( 250 );
}
writeGroup( 1, text );
writeGroup( 1, t );

writeGroup( 50, angle ); // Rotation angle in radians
writeGroup( 41, width * 1.1 ); // Reference rectangle width
Expand Down Expand Up @@ -4414,6 +4414,7 @@ void QgsDxfExport::drawLabel( const QString &layerId, QgsRenderContext &context,
else
{
txt = txt.replace( wrapchr, QLatin1String( "\\P" ) );
txt.replace( " ", "\\~" );

if ( tmpLyr.format().font().underline() )
{
Expand Down
37 changes: 37 additions & 0 deletions tests/src/core/testqgsdxfexport.cpp
Expand Up @@ -43,6 +43,7 @@ class TestQgsDxfExport : public QObject
void testPolygons();
void testMtext();
void testMTextNoSymbology(); //tests if label export works if layer has vector renderer type 'no symbols'
void testMTextEscapeSpaces();
void testText();

private:
Expand Down Expand Up @@ -82,6 +83,7 @@ void TestQgsDxfExport::init()
mPointLayerNoSymbols = new QgsVectorLayer( filename, QStringLiteral( "points" ), QStringLiteral( "ogr" ) );
QVERIFY( mPointLayerNoSymbols->isValid() );
mPointLayerNoSymbols->setRenderer( new QgsNullSymbolRenderer() );
mPointLayerNoSymbols->addExpressionField( QStringLiteral( "'A text with spaces'" ), QgsField( QStringLiteral( "Spacestest" ), QVariant::String ) );
QgsProject::instance()->addMapLayer( mPointLayerNoSymbols );
filename = QStringLiteral( TEST_DATA_DIR ) + "/lines.shp";
mLineLayer = new QgsVectorLayer( filename, QStringLiteral( "lines" ), QStringLiteral( "ogr" ) );
Expand Down Expand Up @@ -193,6 +195,41 @@ void TestQgsDxfExport::testMTextNoSymbology()
QVERIFY( testMtext( mPointLayerNoSymbols, QStringLiteral( "text_no_symbology_dxf" ) ) );
}

void TestQgsDxfExport::testMTextEscapeSpaces()
{
QgsPalLayerSettings settings;
settings.fieldName = QStringLiteral( "Spacestest" );
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_spaces" );
QFile dxfFile( file );
QCOMPARE( d.writeToFile( &dxfFile, QStringLiteral( "CP1252" ) ), 0 );
dxfFile.close();
QVERIFY( fileContainsText( file, "\\fQGIS Vera Sans|i0|b1;\\H3.81136;A\\~text\\~with\\~spaces" ) );
}

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

0 comments on commit a817d0e

Please sign in to comment.