Skip to content

Commit 523fd44

Browse files
committed
Add more standard test font functions to QgsFontUtils
1 parent 2d4ecba commit 523fd44

File tree

3 files changed

+71
-6
lines changed

3 files changed

+71
-6
lines changed

python/core/qgsfontutils.sip

+21-1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,14 @@ class QgsFontUtils
1616
*/
1717
static bool fontFamilyOnSystem( const QString& family );
1818

19+
/** Check whether font family on system has specific style
20+
* @param family The family to test
21+
* @param style The style to test for
22+
* @returns Whether family has style
23+
* @note Added in QGIS 2.1
24+
*/
25+
static bool fontFamilyHasStyle( const QString& family, const QString& style );
26+
1927
/** Check whether font family is on system
2028
* @param family The family to test
2129
* @param chosen The actual family (possibly from different foundry) returned by system
@@ -33,12 +41,24 @@ class QgsFontUtils
3341
*/
3442
static bool updateFontViaStyle( QFont& f, const QString& fontstyle, bool fallback = false );
3543

44+
/** Get standard test font family
45+
* @note Added in QGIS 2.1
46+
*/
47+
static QString standardTestFontFamily();
48+
3649
/** Loads standard test fonts from filesystem or qrc resource
37-
* @param loadstyles List of styles to load, e.g. Roman, Oblique, Bold, Bold Oblique
50+
* @param loadstyles List of styles to load, e.g. All, Roman, Oblique, Bold, Bold Oblique
3851
* @returns Whether any font was loaded
3952
* @note Done by default on debug app/server startup to ensure fonts available for unit tests (Roman and Bold)
4053
* @note Added in QGIS 2.1
4154
*/
4255
static bool loadStandardTestFonts( QStringList loadstyles );
4356

57+
/** Get standard test font with specific style
58+
* @param style Style to load, e.g. Roman, Oblique, Bold, Bold Oblique
59+
* @param pointsize Font point size to set
60+
* @returns QFont
61+
* @note Added in QGIS 2.1
62+
*/
63+
static QFont getStandardTestFont( const QString& style = "Roman", int pointsize = 12 );
4464
};

src/core/qgsfontutils.cpp

+30-5
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,13 @@ bool QgsFontUtils::fontFamilyOnSystem( const QString& family )
3636
{
3737
QFont tmpFont = QFont( family );
3838
// compare just beginning of family string in case 'family [foundry]' differs
39-
return family.startsWith( tmpFont.family(), Qt::CaseInsensitive );
39+
return tmpFont.family().startsWith( family, Qt::CaseInsensitive );
40+
}
41+
42+
bool QgsFontUtils::fontFamilyHasStyle( const QString& family, const QString& style )
43+
{
44+
QFontDatabase fontDB;
45+
return ( fontFamilyOnSystem( family ) && fontDB.styles( family ).contains( style ) );
4046
}
4147

4248
bool QgsFontUtils::fontFamilyMatchOnSystem( const QString& family, QString* chosen, bool* match )
@@ -196,13 +202,17 @@ bool QgsFontUtils::updateFontViaStyle( QFont& f, const QString& fontstyle, bool
196202
return false;
197203
}
198204

205+
QString QgsFontUtils::standardTestFontFamily()
206+
{
207+
return "QGIS Vera Sans";
208+
}
209+
199210
bool QgsFontUtils::loadStandardTestFonts( QStringList loadstyles )
200211
{
201212
// load standard test font from filesystem or testdata.qrc (for unit tests and general testing)
202-
QFontDatabase fontDB;
203213
bool fontsLoaded = false;
204214

205-
QString fontFamily( "QGIS Vera Sans" );
215+
QString fontFamily = standardTestFontFamily();
206216
QMap<QString, QString> fontStyles;
207217
fontStyles.insert( "Roman", "QGIS-Vera/QGIS-Vera.ttf" );
208218
fontStyles.insert( "Oblique", "QGIS-Vera/QGIS-VeraIt.ttf" );
@@ -220,8 +230,7 @@ bool QgsFontUtils::loadStandardTestFonts( QStringList loadstyles )
220230
}
221231
QString familyStyle = QString( "%1 %2" ).arg( fontFamily ).arg( fontstyle );
222232

223-
if ( fontFamilyOnSystem( fontFamily )
224-
&& fontDB.styles( fontFamily ).contains( fontstyle ) )
233+
if ( fontFamilyHasStyle( fontFamily, fontstyle ) )
225234
{
226235
fontsLoaded = ( fontsLoaded || false );
227236
QgsDebugMsg( QString( "Test font '%1' already available" ).arg( familyStyle ) );
@@ -259,3 +268,19 @@ bool QgsFontUtils::loadStandardTestFonts( QStringList loadstyles )
259268

260269
return fontsLoaded;
261270
}
271+
272+
QFont QgsFontUtils::getStandardTestFont( const QString& style, int pointsize )
273+
{
274+
QFontDatabase fontDB;
275+
if ( ! fontFamilyHasStyle( standardTestFontFamily(), style ) )
276+
{
277+
loadStandardTestFonts( QStringList() << style );
278+
}
279+
280+
QFont f = fontDB.font( standardTestFontFamily(), style, pointsize );
281+
// in case above statement fails to set style
282+
f.setBold( style.contains( "Bold" ) );
283+
f.setItalic( style.contains( "Oblique" ) );
284+
285+
return f;
286+
}

src/core/qgsfontutils.h

+20
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,14 @@ class CORE_EXPORT QgsFontUtils
3434
*/
3535
static bool fontFamilyOnSystem( const QString& family );
3636

37+
/** Check whether font family on system has specific style
38+
* @param family The family to test
39+
* @param style The style to test for
40+
* @returns Whether family has style
41+
* @note Added in QGIS 2.1
42+
*/
43+
static bool fontFamilyHasStyle( const QString& family, const QString& style );
44+
3745
/** Check whether font family is on system
3846
* @param family The family to test
3947
* @param chosen The actual family (possibly from different foundry) returned by system
@@ -51,6 +59,11 @@ class CORE_EXPORT QgsFontUtils
5159
*/
5260
static bool updateFontViaStyle( QFont& f, const QString& fontstyle, bool fallback = false );
5361

62+
/** Get standard test font family
63+
* @note Added in QGIS 2.1
64+
*/
65+
static QString standardTestFontFamily();
66+
5467
/** Loads standard test fonts from filesystem or qrc resource
5568
* @param loadstyles List of styles to load, e.g. All, Roman, Oblique, Bold, Bold Oblique
5669
* @returns Whether any font was loaded
@@ -59,6 +72,13 @@ class CORE_EXPORT QgsFontUtils
5972
*/
6073
static bool loadStandardTestFonts( QStringList loadstyles );
6174

75+
/** Get standard test font with specific style
76+
* @param style Style to load, e.g. Roman, Oblique, Bold, Bold Oblique
77+
* @param pointsize Font point size to set
78+
* @returns QFont
79+
* @note Added in QGIS 2.1
80+
*/
81+
static QFont getStandardTestFont( const QString& style = "Roman", int pointsize = 12 );
6282
};
6383

6484
#endif // QGSFONTUTILS_H

0 commit comments

Comments
 (0)