Skip to content

Commit

Permalink
Add more standard test font functions to QgsFontUtils
Browse files Browse the repository at this point in the history
  • Loading branch information
dakcarto committed Feb 19, 2014
1 parent 2d4ecba commit 523fd44
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 6 deletions.
22 changes: 21 additions & 1 deletion python/core/qgsfontutils.sip
Expand Up @@ -16,6 +16,14 @@ class QgsFontUtils
*/
static bool fontFamilyOnSystem( const QString& family );

/** Check whether font family on system has specific style
* @param family The family to test
* @param style The style to test for
* @returns Whether family has style
* @note Added in QGIS 2.1
*/
static bool fontFamilyHasStyle( const QString& family, const QString& style );

/** Check whether font family is on system
* @param family The family to test
* @param chosen The actual family (possibly from different foundry) returned by system
Expand All @@ -33,12 +41,24 @@ class QgsFontUtils
*/
static bool updateFontViaStyle( QFont& f, const QString& fontstyle, bool fallback = false );

/** Get standard test font family
* @note Added in QGIS 2.1
*/
static QString standardTestFontFamily();

/** Loads standard test fonts from filesystem or qrc resource
* @param loadstyles List of styles to load, e.g. Roman, Oblique, Bold, Bold Oblique
* @param loadstyles List of styles to load, e.g. All, Roman, Oblique, Bold, Bold Oblique
* @returns Whether any font was loaded
* @note Done by default on debug app/server startup to ensure fonts available for unit tests (Roman and Bold)
* @note Added in QGIS 2.1
*/
static bool loadStandardTestFonts( QStringList loadstyles );

/** Get standard test font with specific style
* @param style Style to load, e.g. Roman, Oblique, Bold, Bold Oblique
* @param pointsize Font point size to set
* @returns QFont
* @note Added in QGIS 2.1
*/
static QFont getStandardTestFont( const QString& style = "Roman", int pointsize = 12 );
};
35 changes: 30 additions & 5 deletions src/core/qgsfontutils.cpp
Expand Up @@ -36,7 +36,13 @@ bool QgsFontUtils::fontFamilyOnSystem( const QString& family )
{
QFont tmpFont = QFont( family );
// compare just beginning of family string in case 'family [foundry]' differs
return family.startsWith( tmpFont.family(), Qt::CaseInsensitive );
return tmpFont.family().startsWith( family, Qt::CaseInsensitive );
}

bool QgsFontUtils::fontFamilyHasStyle( const QString& family, const QString& style )
{
QFontDatabase fontDB;
return ( fontFamilyOnSystem( family ) && fontDB.styles( family ).contains( style ) );
}

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

QString QgsFontUtils::standardTestFontFamily()
{
return "QGIS Vera Sans";
}

bool QgsFontUtils::loadStandardTestFonts( QStringList loadstyles )
{
// load standard test font from filesystem or testdata.qrc (for unit tests and general testing)
QFontDatabase fontDB;
bool fontsLoaded = false;

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

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

return fontsLoaded;
}

QFont QgsFontUtils::getStandardTestFont( const QString& style, int pointsize )
{
QFontDatabase fontDB;
if ( ! fontFamilyHasStyle( standardTestFontFamily(), style ) )
{
loadStandardTestFonts( QStringList() << style );
}

QFont f = fontDB.font( standardTestFontFamily(), style, pointsize );
// in case above statement fails to set style
f.setBold( style.contains( "Bold" ) );
f.setItalic( style.contains( "Oblique" ) );

return f;
}
20 changes: 20 additions & 0 deletions src/core/qgsfontutils.h
Expand Up @@ -34,6 +34,14 @@ class CORE_EXPORT QgsFontUtils
*/
static bool fontFamilyOnSystem( const QString& family );

/** Check whether font family on system has specific style
* @param family The family to test
* @param style The style to test for
* @returns Whether family has style
* @note Added in QGIS 2.1
*/
static bool fontFamilyHasStyle( const QString& family, const QString& style );

/** Check whether font family is on system
* @param family The family to test
* @param chosen The actual family (possibly from different foundry) returned by system
Expand All @@ -51,6 +59,11 @@ class CORE_EXPORT QgsFontUtils
*/
static bool updateFontViaStyle( QFont& f, const QString& fontstyle, bool fallback = false );

/** Get standard test font family
* @note Added in QGIS 2.1
*/
static QString standardTestFontFamily();

/** Loads standard test fonts from filesystem or qrc resource
* @param loadstyles List of styles to load, e.g. All, Roman, Oblique, Bold, Bold Oblique
* @returns Whether any font was loaded
Expand All @@ -59,6 +72,13 @@ class CORE_EXPORT QgsFontUtils
*/
static bool loadStandardTestFonts( QStringList loadstyles );

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

#endif // QGSFONTUTILS_H

0 comments on commit 523fd44

Please sign in to comment.