Add more standard test font functions to QgsFontUtils

This commit is contained in:
Larry Shaffer 2014-02-19 13:24:49 -07:00
parent 2d4ecbac00
commit 523fd44d86
3 changed files with 71 additions and 6 deletions

View File

@ -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
@ -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 );
};

View File

@ -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 )
@ -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" );
@ -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 ) );
@ -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;
}

View File

@ -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
@ -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
@ -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