|
15 | 15 |
|
16 | 16 | #include "qgsfontutils.h" |
17 | 17 |
|
| 18 | +#include "qgsapplication.h" |
| 19 | +#include "qgslogger.h" |
| 20 | + |
18 | 21 | #include <QApplication> |
| 22 | +#include <QFile> |
19 | 23 | #include <QFont> |
20 | 24 | #include <QFontDatabase> |
21 | 25 | #include <QFontInfo> |
@@ -191,3 +195,67 @@ bool QgsFontUtils::updateFontViaStyle( QFont& f, const QString& fontstyle, bool |
191 | 195 |
|
192 | 196 | return false; |
193 | 197 | } |
| 198 | + |
| 199 | +bool QgsFontUtils::loadStandardTestFonts( QStringList loadstyles ) |
| 200 | +{ |
| 201 | + // load standard test font from filesystem or testdata.qrc (for unit tests and general testing) |
| 202 | + QFontDatabase fontDB; |
| 203 | + bool fontsLoaded = false; |
| 204 | + |
| 205 | + QString fontFamily( "QGIS Vera Sans" ); |
| 206 | + QMap<QString, QString> fontStyles; |
| 207 | + fontStyles.insert( "Roman", "QGIS-Vera/QGIS-Vera.ttf" ); |
| 208 | + fontStyles.insert( "Oblique", "QGIS-Vera/QGIS-VeraIt.ttf" ); |
| 209 | + fontStyles.insert( "Bold", "QGIS-Vera/QGIS-VeraBd.ttf" ); |
| 210 | + fontStyles.insert( "Bold Oblique", "QGIS-Vera/QGIS-VeraBI.ttf" ); |
| 211 | + |
| 212 | + QMap<QString, QString>::const_iterator f = fontStyles.constBegin(); |
| 213 | + for ( ; f != fontStyles.constEnd(); ++f ) |
| 214 | + { |
| 215 | + QString fontstyle( f.key() ); |
| 216 | + QString fontpath( f.value() ); |
| 217 | + if ( ! ( loadstyles.contains( fontstyle ) || loadstyles.contains( "All" ) ) ) |
| 218 | + { |
| 219 | + continue; |
| 220 | + } |
| 221 | + QString familyStyle = QString( "%1 %2" ).arg( fontFamily ).arg( fontstyle ); |
| 222 | + |
| 223 | + if ( fontFamilyOnSystem( fontFamily ) |
| 224 | + && fontDB.styles( fontFamily ).contains( fontstyle ) ) |
| 225 | + { |
| 226 | + fontsLoaded = ( fontsLoaded || false ); |
| 227 | + QgsDebugMsg( QString( "Test font '%1' already available" ).arg( familyStyle ) ); |
| 228 | + } |
| 229 | + else |
| 230 | + { |
| 231 | + bool loaded = false; |
| 232 | + if ( QgsApplication::isRunningFromBuildDir() ) |
| 233 | + { |
| 234 | + // workaround for bugs with Qt 4.8.5 (other versions?) on Mac 10.9, where fonts |
| 235 | + // from qrc resources load but fail to work and default font is substituted [LS]: |
| 236 | + // https://bugreports.qt-project.org/browse/QTBUG-30917 |
| 237 | + // https://bugreports.qt-project.org/browse/QTBUG-32789 |
| 238 | + QString fontPath( QgsApplication::buildSourcePath() + "/tests/testdata/font/" + fontpath ); |
| 239 | + int fontID = QFontDatabase::addApplicationFont( fontPath ); |
| 240 | + loaded = ( fontID != -1 ); |
| 241 | + fontsLoaded = ( fontsLoaded || loaded ); |
| 242 | + QgsDebugMsg( QString( "Test font '%1' %2 from filesystem") |
| 243 | + .arg( familyStyle ).arg( loaded ? "loaded" : "FAILED to load" ) ); |
| 244 | + } |
| 245 | + else |
| 246 | + { |
| 247 | + QFile fontResource( ":/testdata/font/" + fontpath ); |
| 248 | + if ( fontResource.open( QIODevice::ReadOnly ) ) |
| 249 | + { |
| 250 | + int fontID = QFontDatabase::addApplicationFontFromData( fontResource.readAll() ); |
| 251 | + loaded = ( fontID != -1 ); |
| 252 | + fontsLoaded = ( fontsLoaded || loaded ); |
| 253 | + } |
| 254 | + QgsDebugMsg( QString( "Test font '%1' %2 from testdata.qrc") |
| 255 | + .arg( familyStyle ).arg( loaded ? "loaded" : "FAILED to load" ) ); |
| 256 | + } |
| 257 | + } |
| 258 | + } |
| 259 | + |
| 260 | + return fontsLoaded; |
| 261 | +} |
0 commit comments