Skip to content

Commit 44d89a4

Browse files
committed
Update label gui and rendering with QgsFontUtils functions
1 parent 04c4db1 commit 44d89a4

File tree

4 files changed

+19
-77
lines changed

4 files changed

+19
-77
lines changed

src/app/qgslabelinggui.cpp

+3-43
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
#include "qgslabelengineconfigdialog.h"
2727
#include "qgsexpressionbuilderdialog.h"
2828
#include "qgsexpression.h"
29+
#include "qgsfontutils.h"
2930
#include "qgisapp.h"
3031
#include "qgsmaprenderer.h"
3132
#include "qgsproject.h"
@@ -362,7 +363,7 @@ void QgsLabelingGui::init()
362363
mFontWordSpacingSpinBox->setValue( lyr.textFont.wordSpacing() );
363364
mFontLetterSpacingSpinBox->setValue( lyr.textFont.letterSpacing() );
364365

365-
updateFontViaStyle( lyr.textNamedStyle );
366+
QgsFontUtils::updateFontViaStyle( mRefFont, lyr.textNamedStyle );
366367
updateFont( mRefFont );
367368

368369
// shape background
@@ -1053,47 +1054,6 @@ void QgsLabelingGui::changeTextFont()
10531054
}
10541055
}
10551056

1056-
void QgsLabelingGui::updateFontViaStyle( const QString & fontstyle )
1057-
{
1058-
QFont styledfont;
1059-
bool foundmatch = false;
1060-
int fontSize = 12; // QFontDatabase::font() needs an integer for size
1061-
if ( !fontstyle.isEmpty() )
1062-
{
1063-
styledfont = mFontDB.font( mRefFont.family(), fontstyle, fontSize );
1064-
styledfont.setPointSizeF( mRefFont.pointSizeF() );
1065-
if ( QApplication::font().toString() != styledfont.toString() )
1066-
{
1067-
foundmatch = true;
1068-
}
1069-
}
1070-
if ( !foundmatch )
1071-
{
1072-
foreach ( const QString &style, mFontDB.styles( mRefFont.family() ) )
1073-
{
1074-
styledfont = mFontDB.font( mRefFont.family(), style, fontSize );
1075-
styledfont.setPointSizeF( mRefFont.pointSizeF() );
1076-
styledfont = styledfont.resolve( mRefFont );
1077-
if ( mRefFont.toString() == styledfont.toString() )
1078-
{
1079-
foundmatch = true;
1080-
break;
1081-
}
1082-
}
1083-
}
1084-
if ( foundmatch )
1085-
{
1086-
// styledfont.setPointSizeF( mRefFont.pointSizeF() );
1087-
styledfont.setCapitalization( mRefFont.capitalization() );
1088-
styledfont.setUnderline( mRefFont.underline() );
1089-
styledfont.setStrikeOut( mRefFont.strikeOut() );
1090-
styledfont.setWordSpacing( mRefFont.wordSpacing() );
1091-
styledfont.setLetterSpacing( QFont::AbsoluteSpacing, mRefFont.letterSpacing() );
1092-
mRefFont = styledfont;
1093-
}
1094-
// if no match, style combobox will be left blank, which should not affect engine labeling
1095-
}
1096-
10971057
void QgsLabelingGui::updateFont( QFont font )
10981058
{
10991059
// update background reference font
@@ -1397,7 +1357,7 @@ void QgsLabelingGui::on_mFontCapitalsComboBox_currentIndexChanged( int index )
13971357

13981358
void QgsLabelingGui::on_mFontStyleComboBox_currentIndexChanged( const QString & text )
13991359
{
1400-
updateFontViaStyle( text );
1360+
QgsFontUtils::updateFontViaStyle( mRefFont, text );
14011361
updateFont( mRefFont );
14021362
}
14031363

src/app/qgslabelinggui.h

-1
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,6 @@ class QgsLabelingGui : public QWidget, private Ui::QgsLabelingGuiBase
8585
void blockInitSignals( bool block );
8686
void blockFontChangeSignals( bool blk );
8787
void setPreviewBackground( QColor color );
88-
void updateFontViaStyle( const QString & fontstyle );
8988
void syncDefinedCheckboxFrame( QgsDataDefinedButton* ddBtn, QCheckBox* chkBx, QFrame* f );
9089
void populateFontCapitalsComboBox();
9190
void populateFontStyleComboBox();

src/core/qgspallabeling.cpp

+16-31
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040

4141
#include "diagram/qgsdiagram.h"
4242
#include "qgsdiagramrendererv2.h"
43+
#include "qgsfontutils.h"
4344
#include "qgslabelsearchtree.h"
4445
#include "qgsexpression.h"
4546
#include "qgsdatadefined.h"
@@ -783,27 +784,6 @@ void QgsPalLayerSettings::readDataDefinedProperty( QgsVectorLayer* layer,
783784
}
784785
}
785786

786-
void QgsPalLayerSettings::updateFontViaStyle( QFont& font, const QString & fontstyle )
787-
{
788-
if ( !fontstyle.isEmpty() )
789-
{
790-
QFont styledfont = mFontDB.font( font.family(), fontstyle, 12 );
791-
if ( QApplication::font().toString() != styledfont.toString() )
792-
{
793-
if ( font.pointSizeF() != -1 )
794-
{
795-
styledfont.setPointSizeF( font.pointSizeF() );
796-
}
797-
else if ( font.pixelSize() != -1 )
798-
{
799-
styledfont.setPixelSize( font.pixelSize() );
800-
}
801-
802-
font = styledfont;
803-
}
804-
}
805-
}
806-
807787
void QgsPalLayerSettings::readFromLayer( QgsVectorLayer* layer )
808788
{
809789
if ( layer->customProperty( "labeling" ).toString() != QString( "pal" ) )
@@ -824,7 +804,7 @@ void QgsPalLayerSettings::readFromLayer( QgsVectorLayer* layer )
824804
textFont = QFont( fontFamily, fontSize, fontWeight, fontItalic );
825805
textFont.setPointSizeF( fontSize ); //double precision needed because of map units
826806
textNamedStyle = layer->customProperty( "labeling/namedStyle", QVariant( "" ) ).toString();
827-
updateFontViaStyle( textFont, textNamedStyle ); // must come after textFont.setPointSizeF()
807+
QgsFontUtils::updateFontViaStyle( textFont, textNamedStyle ); // must come after textFont.setPointSizeF()
828808
textFont.setCapitalization(( QFont::Capitalization )layer->customProperty( "labeling/fontCapitals", QVariant( 0 ) ).toUInt() );
829809
textFont.setUnderline( layer->customProperty( "labeling/fontUnderline" ).toBool() );
830810
textFont.setStrikeOut( layer->customProperty( "labeling/fontStrikeout" ).toBool() );
@@ -2319,12 +2299,14 @@ void QgsPalLayerSettings::parseTextStyle( QFont& labelFont,
23192299
QString family = exprVal.toString().trimmed();
23202300
QgsDebugMsgLevel( QString( "exprVal Font family:%1" ).arg( family ), 4 );
23212301

2322-
// testing for mFontDB.families().contains( ddFontFamily ) doesn't always work,
2323-
// because the families list needs looped to test for 'family [foundry]'
2324-
// which could bring unnecessary overhead, so fall back to default font instead
23252302
if ( labelFont.family() != family )
23262303
{
2327-
ddFontFamily = family;
2304+
// testing for ddFontFamily in QFontDatabase.families() may be slow to do for every feature
2305+
// (i.e. don't use QgsFontUtils::fontFamilyMatchOnSystem( family ) here)
2306+
if ( QgsFontUtils::fontFamilyOnSystem( family ) )
2307+
{
2308+
ddFontFamily = family;
2309+
}
23282310
}
23292311
}
23302312

@@ -2355,7 +2337,10 @@ void QgsPalLayerSettings::parseTextStyle( QFont& labelFont,
23552337
ddItalic = italic;
23562338
}
23572339

2340+
// TODO: update when pref for how to resolve missing family (use matching algorithm or just default font) is implemented
2341+
// (currently defaults to what has been read in from layer settings)
23582342
QFont newFont;
2343+
QFont appFont = QApplication::font();
23592344
bool newFontBuilt = false;
23602345
if ( ddBold || ddItalic )
23612346
{
@@ -2371,24 +2356,24 @@ void QgsPalLayerSettings::parseTextStyle( QFont& labelFont,
23712356
if ( !ddFontFamily.isEmpty() )
23722357
{
23732358
// both family and style are different, build font from database
2374-
QFont styledfont = mFontDB.font( ddFontFamily, ddFontStyle, 12 );
2375-
if ( QApplication::font().toString() != styledfont.toString() )
2359+
QFont styledfont = mFontDB.font( ddFontFamily, ddFontStyle, appFont.pointSize() );
2360+
if ( appFont != styledfont )
23762361
{
23772362
newFont = styledfont;
23782363
newFontBuilt = true;
23792364
}
23802365
}
23812366

23822367
// update the font face style
2383-
updateFontViaStyle( newFontBuilt ? newFont : labelFont, ddFontStyle );
2368+
QgsFontUtils::updateFontViaStyle( newFontBuilt ? newFont : labelFont, ddFontStyle );
23842369
}
23852370
else if ( !ddFontFamily.isEmpty() )
23862371
{
23872372
if ( ddFontStyle.compare( "Ignore", Qt::CaseInsensitive ) != 0 )
23882373
{
23892374
// just family is different, build font from database
2390-
QFont styledfont = mFontDB.font( ddFontFamily, textNamedStyle, 12 );
2391-
if ( QApplication::font().toString() != styledfont.toString() )
2375+
QFont styledfont = mFontDB.font( ddFontFamily, textNamedStyle, appFont.pointSize() );
2376+
if ( appFont != styledfont )
23922377
{
23932378
newFont = styledfont;
23942379
newFontBuilt = true;

src/core/qgspallabeling.h

-2
Original file line numberDiff line numberDiff line change
@@ -546,8 +546,6 @@ class CORE_EXPORT QgsPalLayerSettings
546546
QMap<QgsPalLayerSettings::DataDefinedProperties, QPair<QString, int> > mDataDefinedNames;
547547

548548
QFontDatabase mFontDB;
549-
/**Updates font with named style */
550-
void updateFontViaStyle( QFont& font, const QString & fontstyle );
551549
};
552550

553551
class CORE_EXPORT QgsLabelCandidate

0 commit comments

Comments
 (0)