@@ -43,11 +43,10 @@ void QgsScaleBarStyle::drawLabels( QPainter* p ) const
4343
4444 p->save ();
4545
46- p->setFont ( mScaleBar ->fontPixelSize () );
46+ p->setFont (mScaleBar ->font () );
4747
48- QFontMetricsF fontMetrics ( mScaleBar ->fontPixelSize () );
4948 QString firstLabel = mScaleBar ->firstLabelString ();
50- double xOffset = fontMetrics. width ( firstLabel ) / 2 ;
49+ double xOffset = mScaleBar -> textWidthMM ( mScaleBar -> font (), firstLabel) / 2 ;
5150
5251 // double mCurrentXCoord = mScaleBar->pen().widthF() + mScaleBar->boxContentSpace();
5352 QList<QPair<double , double > > segmentInfo;
@@ -79,7 +78,7 @@ void QgsScaleBarStyle::drawLabels( QPainter* p ) const
7978
8079 if ( segmentCounter == 0 || segmentCounter >= nSegmentsLeft ) // don't draw label for intermediate left segments
8180 {
82- p ->drawText ( QPointF ( segmentIt->first - fontMetrics. width ( currentNumericLabel ) / 2 + xOffset, mScaleBar ->fontHeight () + mScaleBar ->boxContentSpace () ) , currentNumericLabel );
81+ mScaleBar ->drawText (p, segmentIt->first - mScaleBar -> textWidthMM ( mScaleBar -> font (), currentNumericLabel) / 2 + xOffset, mScaleBar ->fontAscentMM ( mScaleBar -> font ()) + mScaleBar ->boxContentSpace (), currentNumericLabel, mScaleBar -> font () );
8382 }
8483
8584 if ( segmentCounter >= nSegmentsLeft )
@@ -93,7 +92,7 @@ void QgsScaleBarStyle::drawLabels( QPainter* p ) const
9392 if ( !segmentInfo.isEmpty () )
9493 {
9594 currentNumericLabel = QString::number ( currentLabelNumber / mScaleBar ->numMapUnitsPerScaleBarUnit () );
96- p ->drawText ( QPointF ( segmentInfo.last ().first + mScaleBar ->segmentMM () - fontMetrics. width ( currentNumericLabel ) / 2 + xOffset, mScaleBar ->fontHeight () + mScaleBar ->boxContentSpace () ) , currentNumericLabel + " " + mScaleBar ->unitLabeling () );
95+ mScaleBar ->drawText (p, segmentInfo.last ().first + mScaleBar ->segmentMM () - mScaleBar -> textWidthMM ( mScaleBar -> font (), currentNumericLabel) / 2 + xOffset, mScaleBar ->fontAscentMM ( mScaleBar -> font ()) + mScaleBar ->boxContentSpace (), currentNumericLabel + " " + mScaleBar ->unitLabeling (), mScaleBar -> font () );
9796 }
9897
9998 p->restore ();
@@ -106,18 +105,15 @@ QRectF QgsScaleBarStyle::calculateBoxSize() const
106105 return QRectF ();
107106 }
108107
109-
110- QFontMetricsF fontMetrics ( mScaleBar ->fontPixelSize () );
111-
112108 // consider centered first label
113- double firstLabelLeft = fontMetrics. width ( mScaleBar ->firstLabelString () ) / 2 ;
109+ double firstLabelLeft = mScaleBar -> textWidthMM ( mScaleBar -> font (), mScaleBar ->firstLabelString ()) / 2 ;
114110
115111 // consider last number and label
116112
117113 double largestLabelNumber = mScaleBar ->numSegments () * mScaleBar ->numUnitsPerSegment () / mScaleBar ->numMapUnitsPerScaleBarUnit ();
118114 QString largestNumberLabel = QString::number ( largestLabelNumber );
119115 QString largestLabel = QString::number ( largestLabelNumber ) + " " + mScaleBar ->unitLabeling ();
120- double largestLabelWidth = fontMetrics. width ( largestLabel ) - fontMetrics. width ( largestNumberLabel ) / 2 ;
116+ double largestLabelWidth = mScaleBar -> textWidthMM ( mScaleBar -> font (), largestLabel) - mScaleBar -> textWidthMM ( mScaleBar -> font (), largestNumberLabel) / 2 ;
121117
122118 double totalBarLength = 0.0 ;
123119
@@ -131,7 +127,7 @@ QRectF QgsScaleBarStyle::calculateBoxSize() const
131127 }
132128
133129 double width = firstLabelLeft + totalBarLength + 2 * mScaleBar ->pen ().widthF () + largestLabelWidth + 2 * mScaleBar ->boxContentSpace ();
134- double height = mScaleBar ->height () + mScaleBar ->labelBarSpace () + 2 * mScaleBar ->boxContentSpace () + mScaleBar ->fontHeight ( );
130+ double height = mScaleBar ->height () + mScaleBar ->labelBarSpace () + 2 * mScaleBar ->boxContentSpace () + mScaleBar ->fontAscentMM ( mScaleBar -> font () );
135131
136132 return QRectF ( mScaleBar ->transform ().dx (), mScaleBar ->transform ().dy (), width, height );
137133}
0 commit comments