Skip to content

Commit 256e441

Browse files
committed
Escape blanks in MTEXT and add unit test
1 parent bf4ba08 commit 256e441

File tree

2 files changed

+39
-1
lines changed

2 files changed

+39
-1
lines changed

src/core/dxf/qgsdxfexport.cpp

+2-1
Original file line numberDiff line numberDiff line change
@@ -3666,7 +3666,7 @@ void QgsDxfExport::writeMText( const QString &layer, const QString &text, const
36663666
writeGroup( 3, t.left( 250 ) );
36673667
t = t.mid( 250 );
36683668
}
3669-
writeGroup( 1, text );
3669+
writeGroup( 1, t );
36703670

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

44184419
if ( tmpLyr.format().font().underline() )
44194420
{

tests/src/core/testqgsdxfexport.cpp

+37
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ class TestQgsDxfExport : public QObject
4343
void testPolygons();
4444
void testMtext();
4545
void testMTextNoSymbology(); //tests if label export works if layer has vector renderer type 'no symbols'
46+
void testMTextEscapeSpaces();
4647
void testText();
4748

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

198+
void TestQgsDxfExport::testMTextEscapeSpaces()
199+
{
200+
QgsPalLayerSettings settings;
201+
settings.fieldName = QStringLiteral( "Spacestest" );
202+
QgsTextFormat format;
203+
format.setFont( QgsFontUtils::getStandardTestFont( QStringLiteral( "Bold" ) ).family() );
204+
format.setSize( 12 );
205+
format.setNamedStyle( QStringLiteral( "Bold" ) );
206+
format.setColor( QColor( 200, 0, 200 ) );
207+
settings.setFormat( format );
208+
mPointLayerNoSymbols->setLabeling( new QgsVectorLayerSimpleLabeling( settings ) );
209+
mPointLayerNoSymbols->setLabelsEnabled( true );
210+
211+
QgsDxfExport d;
212+
d.addLayers( QList< QgsDxfExport::DxfLayer >() << QgsDxfExport::DxfLayer( mPointLayerNoSymbols ) );
213+
214+
QgsMapSettings mapSettings;
215+
QSize size( 640, 480 );
216+
mapSettings.setOutputSize( size );
217+
mapSettings.setExtent( mPointLayerNoSymbols->extent() );
218+
mapSettings.setLayers( QList<QgsMapLayer *>() << mPointLayerNoSymbols );
219+
mapSettings.setOutputDpi( 96 );
220+
mapSettings.setDestinationCrs( mPointLayerNoSymbols->crs() );
221+
222+
d.setMapSettings( mapSettings );
223+
d.setSymbologyScale( 1000 );
224+
d.setSymbologyExport( QgsDxfExport::FeatureSymbology );
225+
226+
QString file = getTempFileName( "mtext_escape_spaces" );
227+
QFile dxfFile( file );
228+
QCOMPARE( d.writeToFile( &dxfFile, QStringLiteral( "CP1252" ) ), 0 );
229+
dxfFile.close();
230+
QVERIFY( fileContainsText( file, "\\fQGIS Vera Sans|i0|b1;\\H3.81136;A\\~text\\~with\\~spaces" ) );
231+
}
232+
196233
void TestQgsDxfExport::testText()
197234
{
198235
QgsPalLayerSettings settings;

0 commit comments

Comments
 (0)