21 #include <QApplication>
24 #include <QFontDatabase>
26 #include <QStringList>
31 QFontInfo fi = QFontInfo( f );
32 return fi.exactMatch();
37 QFont tmpFont = QFont( family );
39 return tmpFont.family().startsWith( family, Qt::CaseInsensitive );
51 QStringList fontFamilies = fontDB.families();
54 QList<QString>::const_iterator it = fontFamilies.constBegin();
55 for ( ; it != fontFamilies.constEnd(); ++it )
58 if ( it->startsWith( family, Qt::CaseInsensitive ) )
65 *match = ( *it == family );
81 QFont f = QFont( family );
103 if ( fontstyle.isEmpty() )
108 QFontDatabase fontDB;
121 if ( fontstyle == fontDB.styleString( f ) )
126 QFont appfont = QApplication::font();
127 int defaultSize = appfont.pointSize();
130 bool foundmatch =
false;
133 styledfont = fontDB.font( f.family(), fontstyle, defaultSize );
134 if ( appfont != styledfont || fontstyle != fontDB.styleString( f ) )
141 if ( fallback && !foundmatch )
143 QFont testFont = QFont( f );
144 testFont.setPointSize( defaultSize );
147 foreach (
const QString &style, fontDB.styles( f.family() ) )
149 styledfont = fontDB.font( f.family(), style, defaultSize );
150 styledfont = styledfont.resolve( f );
151 if ( testFont.toString() == styledfont.toString() )
161 foreach (
const QString &style, fontDB.styles( f.family() ) )
163 styledfont = fontDB.font( f.family(), style, defaultSize );
164 if ( QApplication::font() != styledfont )
177 if ( f.pointSizeF() != -1 )
179 styledfont.setPointSizeF( f.pointSizeF() );
181 else if ( f.pixelSize() != -1 )
183 styledfont.setPixelSize( f.pixelSize() );
185 styledfont.setCapitalization( f.capitalization() );
186 styledfont.setUnderline( f.underline() );
187 styledfont.setStrikeOut( f.strikeOut() );
188 styledfont.setWordSpacing( f.wordSpacing() );
189 styledfont.setLetterSpacing( QFont::AbsoluteSpacing, f.letterSpacing() );
200 return "QGIS Vera Sans";
206 bool fontsLoaded =
false;
209 QMap<QString, QString> fontStyles;
210 fontStyles.insert(
"Roman",
"QGIS-Vera/QGIS-Vera.ttf" );
211 fontStyles.insert(
"Oblique",
"QGIS-Vera/QGIS-VeraIt.ttf" );
212 fontStyles.insert(
"Bold",
"QGIS-Vera/QGIS-VeraBd.ttf" );
213 fontStyles.insert(
"Bold Oblique",
"QGIS-Vera/QGIS-VeraBI.ttf" );
215 QMap<QString, QString>::const_iterator f = fontStyles.constBegin();
216 for ( ; f != fontStyles.constEnd(); ++f )
218 QString fontstyle( f.key() );
219 QString fontpath( f.value() );
220 if ( !( loadstyles.contains( fontstyle ) || loadstyles.contains(
"All" ) ) )
224 QString familyStyle = QString(
"%1 %2" ).arg( fontFamily ).arg( fontstyle );
228 fontsLoaded = ( fontsLoaded || false );
229 QgsDebugMsg( QString(
"Test font '%1' already available" ).arg( familyStyle ) );
241 int fontID = QFontDatabase::addApplicationFont( fontPath );
242 loaded = ( fontID != -1 );
243 fontsLoaded = ( fontsLoaded || loaded );
244 QgsDebugMsg( QString(
"Test font '%1' %2 from filesystem [%3]" )
245 .arg( familyStyle ).arg( loaded ?
"loaded" :
"FAILED to load" ).arg( fontPath ) );
249 QFile fontResource(
":/testdata/font/" + fontpath );
250 if ( fontResource.open( QIODevice::ReadOnly ) )
252 int fontID = QFontDatabase::addApplicationFontFromData( fontResource.readAll() );
253 loaded = ( fontID != -1 );
254 fontsLoaded = ( fontsLoaded || loaded );
256 QgsDebugMsg( QString(
"Test font '%1' %2 from testdata.qrc" )
257 .arg( familyStyle ).arg( loaded ?
"loaded" :
"FAILED to load" ) );
267 QFontDatabase fontDB;
275 f.setBold( style.contains(
"Bold" ) );
276 f.setItalic( style.contains(
"Oblique" ) );
static bool fontFamilyHasStyle(const QString &family, const QString &style)
Check whether font family on system has specific style.
static bool isRunningFromBuildDir()
Indicates whether running from build directory (not installed)
static bool fontFamilyOnSystem(const QString &family)
Check whether font family is on system in a quick manner, which does not compare [foundry].
static QFont getStandardTestFont(const QString &style="Roman", int pointsize=12)
Get standard test font with specific style.
static QString standardTestFontFamily()
Get standard test font family.
static QString buildSourcePath()
Returns path to the source directory.
static bool loadStandardTestFonts(QStringList loadstyles)
Loads standard test fonts from filesystem or qrc resource.
static bool updateFontViaStyle(QFont &f, const QString &fontstyle, bool fallback=false)
Updates font with named style and retain all font properties.
static bool fontMatchOnSystem(const QFont &f)
Check whether exact font is on system.
static bool fontFamilyMatchOnSystem(const QString &family, QString *chosen=0, bool *match=0)
Check whether font family is on system.