Skip to content

Commit e3d0305

Browse files
committed
Use tightBoundingRect to calculate size of dxf text. Otherwise, the text will be too big in the dxf
1 parent 6d3199f commit e3d0305

File tree

1 file changed

+18
-2
lines changed

1 file changed

+18
-2
lines changed

src/core/dxf/qgsdxfexport.cpp

+18-2
Original file line numberDiff line numberDiff line change
@@ -4598,13 +4598,29 @@ void QgsDxfExport::drawLabel( QString layerId, QgsRenderContext& context, pal::L
45984598
txt.prepend( "\\K" ).append( "\\k" );
45994599
}
46004600

4601+
QFontMetricsF* fm = lf->labelFontMetrics();
4602+
if ( !fm )
4603+
{
4604+
return;
4605+
}
4606+
4607+
QRectF textBoundingRect = fm->tightBoundingRect( txt );
4608+
4609+
double maxAscent = -textBoundingRect.y();
4610+
double maxDescent = textBoundingRect.height() - maxAscent;
4611+
46014612
txt.prepend( QString( "\\f%1|i%2|b%3;\\H%4;" )
46024613
.arg( tmpLyr.textFont.family() )
46034614
.arg( tmpLyr.textFont.italic() ? 1 : 0 )
46044615
.arg( tmpLyr.textFont.bold() ? 1 : 0 )
4605-
.arg( label->getHeight() / ( 1 + txt.count( "\\P" ) ) * 0.75 ) );
4616+
.arg(( maxAscent ) * context.mapToPixel().mapUnitsPerPixel() ) );
4617+
4618+
double labelY = label->getY();
4619+
4620+
int nLines = txt.count( "\\P" ) + 1;
4621+
labelY += ( label->getHeight() - ( maxAscent + maxDescent + fm->lineSpacing() * ( nLines - 1 ) ) * context.mapToPixel().mapUnitsPerPixel() ) / 2.0;
46064622

4607-
writeMText( dxfLayer, txt, QgsPointV2( label->getX(), label->getY() ), label->getWidth(), label->getAlpha() * 180.0 / M_PI, tmpLyr.textColor );
4623+
writeMText( dxfLayer, txt, QgsPointV2( label->getX(), labelY ), label->getWidth(), label->getAlpha() * 180.0 / M_PI, tmpLyr.textColor );
46084624
}
46094625

46104626

0 commit comments

Comments
 (0)