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" )
245 .arg( familyStyle ).arg( loaded ?
"loaded" :
"FAILED to load" ) );
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" ) );