22 #include <QApplication> 25 #include <QFontDatabase> 27 #include <QStringList> 33 QFontInfo fi = QFontInfo( f );
34 return fi.exactMatch();
39 QFont tmpFont = QFont( family );
41 return tmpFont.family().startsWith( family, Qt::CaseInsensitive );
50 if ( fontDB.styles( family ).contains( style ) )
54 QString modified( style );
55 if ( style ==
"Roman" )
57 if ( style ==
"Oblique" )
59 if ( style ==
"Bold Oblique" )
60 modified =
"Bold Italic";
61 if ( fontDB.styles( family ).contains( modified ) )
71 QStringList fontFamilies = fontDB.families();
74 QList<QString>::const_iterator it = fontFamilies.constBegin();
75 for ( ; it != fontFamilies.constEnd(); ++it )
78 if ( it->startsWith( family, Qt::CaseInsensitive ) )
85 *match = ( *it == family );
101 QFont
f = QFont( family );
102 *chosen = f.family();
123 if ( fontstyle.isEmpty() )
128 QFontDatabase fontDB;
141 if ( fontstyle == fontDB.styleString( f ) )
146 QFont appfont = QApplication::font();
147 int defaultSize = appfont.pointSize();
150 bool foundmatch =
false;
153 styledfont = fontDB.font( f.family(), fontstyle, defaultSize );
154 if ( appfont != styledfont || fontstyle != fontDB.styleString( f ) )
161 if ( fallback && !foundmatch )
163 QFont testFont = QFont( f );
164 testFont.setPointSize( defaultSize );
167 Q_FOREACH (
const QString &style, fontDB.styles( f.family() ) )
169 styledfont = fontDB.font( f.family(), style, defaultSize );
170 styledfont = styledfont.resolve( f );
171 if ( testFont.toString() == styledfont.toString() )
181 Q_FOREACH (
const QString &style, fontDB.styles( f.family() ) )
183 styledfont = fontDB.font( f.family(), style, defaultSize );
184 if ( QApplication::font() != styledfont )
199 styledfont.setPointSizeF( f.pointSizeF() );
201 else if ( f.pixelSize() != -1 )
203 styledfont.setPixelSize( f.pixelSize() );
205 styledfont.setCapitalization( f.capitalization() );
206 styledfont.setUnderline( f.underline() );
207 styledfont.setStrikeOut( f.strikeOut() );
208 styledfont.setWordSpacing( f.wordSpacing() );
209 styledfont.setLetterSpacing( QFont::AbsoluteSpacing, f.letterSpacing() );
220 return QStringLiteral(
"QGIS Vera Sans" );
226 bool fontsLoaded =
false;
229 QMap<QString, QString> fontStyles;
230 fontStyles.insert( QStringLiteral(
"Roman" ), QStringLiteral(
"QGIS-Vera/QGIS-Vera.ttf" ) );
231 fontStyles.insert( QStringLiteral(
"Oblique" ), QStringLiteral(
"QGIS-Vera/QGIS-VeraIt.ttf" ) );
232 fontStyles.insert( QStringLiteral(
"Bold" ), QStringLiteral(
"QGIS-Vera/QGIS-VeraBd.ttf" ) );
233 fontStyles.insert( QStringLiteral(
"Bold Oblique" ), QStringLiteral(
"QGIS-Vera/QGIS-VeraBI.ttf" ) );
235 QMap<QString, QString>::const_iterator
f = fontStyles.constBegin();
236 for ( ; f != fontStyles.constEnd(); ++
f )
238 QString fontstyle( f.key() );
239 QString fontpath( f.value() );
240 if ( !( loadstyles.contains( fontstyle ) || loadstyles.contains( QStringLiteral(
"All" ) ) ) )
247 QgsDebugMsg( QString(
"Test font '%1 %2' already available" ).arg( fontFamily, fontstyle ) );
259 int fontID = QFontDatabase::addApplicationFont( fontPath );
260 loaded = ( fontID != -1 );
261 fontsLoaded = ( fontsLoaded || loaded );
262 QgsDebugMsg( QString(
"Test font '%1 %2' %3 from filesystem [%4]" )
263 .arg( fontFamily, fontstyle, loaded ?
"loaded" :
"FAILED to load", fontPath ) );
265 QgsDebugMsg( QString(
"font families in %1: %2" ).arg( fontID ).arg( db.applicationFontFamilies( fontID ).join(
"," ) ) );
269 QFile fontResource(
":/testdata/font/" + fontpath );
270 if ( fontResource.open( QIODevice::ReadOnly ) )
272 int fontID = QFontDatabase::addApplicationFontFromData( fontResource.readAll() );
273 loaded = ( fontID != -1 );
274 fontsLoaded = ( fontsLoaded || loaded );
276 QgsDebugMsg( QString(
"Test font '%1' (%2) %3 from testdata.qrc" )
277 .arg( fontFamily, fontstyle, loaded ?
"loaded" :
"FAILED to load" ) );
292 QFontDatabase fontDB;
295 if ( !f.exactMatch() )
298 if ( style ==
"Roman" )
300 else if ( style ==
"Oblique" )
302 else if ( style ==
"Bold Oblique" )
303 modified =
"Bold Italic";
304 if ( !modified.isEmpty() )
307 if ( !f.exactMatch() )
310 QgsDebugMsg( QString(
"Requested: %1" ).arg( f.toString() ) );
312 QgsDebugMsg( QString(
"Replaced: %1,%2,%3,%4,%5,%6,%7,%8,%9,%10" ).arg( fi.family() ).arg( fi.pointSizeF() ).arg( fi.pixelSize() ).arg( fi.styleHint() ).arg( fi.weight() ).arg( fi.style() ).arg( fi.underline() ).arg( fi.strikeOut() ).arg( fi.fixedPitch() ).arg( fi.rawMode() ) );
316 f.setBold( style.contains( QLatin1String(
"Bold" ) ) );
317 f.setItalic( style.contains( QLatin1String(
"Oblique" ) ) || style.contains( QLatin1String(
"Italic" ) ) );
324 QDomElement fontElem = document.createElement( elementName );
325 fontElem.setAttribute( QStringLiteral(
"description" ), font.toString() );
332 if ( element.isNull() )
337 font.fromString( element.attribute( QStringLiteral(
"description" ) ) );
338 if ( element.hasAttribute( QStringLiteral(
"style" ) ) )
348 if ( element.isNull() )
353 QDomNodeList nodeList = element.elementsByTagName( childNode );
354 if ( !nodeList.isEmpty() )
356 QDomElement fontElem = nodeList.at( 0 ).toElement();
367 std::unique_ptr< QMimeData >mimeData(
new QMimeData );
369 QDomDocument fontDoc;
370 QDomElement fontElem =
toXmlElement( font, fontDoc, QStringLiteral(
"font" ) );
371 fontDoc.appendChild( fontElem );
372 mimeData->setText( fontDoc.toString() );
374 return mimeData.release();
386 QString text = data->text();
387 if ( !text.isEmpty() )
392 if ( doc.setContent( text ) )
394 elem = doc.documentElement();
396 if ( elem.nodeName() != QStringLiteral(
"font" ) )
397 elem = elem.firstChildElement( QStringLiteral(
"font" ) );
410 static QMap<QString, QString> createTranslatedStyleMap()
412 QMap<QString, QString> translatedStyleMap;
413 QStringList words = QStringList() << QStringLiteral(
"Normal" ) << QStringLiteral(
"Light" ) << QStringLiteral(
"Bold" ) << QStringLiteral(
"Black" ) << QStringLiteral(
"Demi" ) << QStringLiteral(
"Italic" ) << QStringLiteral(
"Oblique" );
414 Q_FOREACH (
const QString &word, words )
416 translatedStyleMap.insert( QCoreApplication::translate(
"QFontDatabase", qPrintable( word ) ), word );
418 return translatedStyleMap;
423 QStringList words = namedStyle.split(
' ', QString::SkipEmptyParts );
424 for (
int i = 0, n = words.length(); i < n; ++i )
426 words[i] = QCoreApplication::translate(
"QFontDatabase", words[i].toUtf8(),
nullptr, QCoreApplication::UnicodeUTF8 );
428 return words.join( QStringLiteral(
" " ) );
433 static QMap<QString, QString> translatedStyleMap = createTranslatedStyleMap();
434 QStringList words = namedStyle.split(
' ', QString::SkipEmptyParts );
435 for (
int i = 0, n = words.length(); i < n; ++i )
437 if ( translatedStyleMap.contains( words[i] ) )
439 words[i] = translatedStyleMap.value( words[i] );
443 QgsDebugMsg( QString(
"Warning: style map does not contain %1" ).arg( words[i] ) );
446 return words.join( QStringLiteral(
" " ) );
451 QString css = QStringLiteral(
"font-family: " ) + font.family() +
';';
454 css += QLatin1String(
"font-style: " );
455 switch ( font.style() )
457 case QFont::StyleNormal:
458 css += QLatin1String(
"normal" );
460 case QFont::StyleItalic:
461 css += QLatin1String(
"italic" );
463 case QFont::StyleOblique:
464 css += QLatin1String(
"oblique" );
471 switch ( font.weight() )
479 case QFont::DemiBold:
488 #if QT_VERSION >= 0x050500 492 case QFont::ExtraLight:
498 case QFont::ExtraBold:
503 css += QStringLiteral(
"font-weight: %1;" ).arg( cssWeight );
506 css += QStringLiteral(
"font-size: %1px;" ).arg( font.pointSizeF() >= 0 ? font.pointSizeF() * pointToPixelScale : font.pixelSize() );
513 if ( family.isEmpty() )
519 QStringList recentFamilies = settings.
value( QStringLiteral(
"fonts/recent" ) ).toStringList();
522 recentFamilies.removeAll( family );
525 recentFamilies.prepend( family );
528 recentFamilies = recentFamilies.mid( 0, 10 );
530 settings.
setValue( QStringLiteral(
"fonts/recent" ), recentFamilies );
536 return settings.
value( QStringLiteral(
"fonts/recent" ) ).toStringList();
static QFont fromMimeData(const QMimeData *data, bool *ok=nullptr)
Attempts to parse the provided mime data as a QFont.
This class is a composition of two QSettings instances:
static bool setFromXmlElement(QFont &font, const QDomElement &element)
Sets the properties of a font to match the properties stored in an XML element.
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 QString translateNamedStyle(const QString &namedStyle)
Returns the localized named style of a font, if such a translation is available.
bool qgsDoubleNear(double a, double b, double epsilon=4 *DBL_EPSILON)
Compare two doubles (but allow some difference)
static QString asCSS(const QFont &font, double pointToPixelMultiplier=1.0)
Returns a CSS string representing the specified font as closely as possible.
void setValue(const QString &key, const QVariant &value, const QgsSettings::Section section=QgsSettings::NoSection)
Sets the value of setting key to value.
static QStringList recentFontFamilies()
Returns a list of recently used font families.
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 bool setFromXmlChildNode(QFont &font, const QDomElement &element, const QString &childNode)
Sets the properties of a font to match the properties stored in an XML child node.
static bool loadStandardTestFonts(const QStringList &loadstyles)
Loads standard test fonts from filesystem or qrc resource.
static QString standardTestFontFamily()
Get standard test font family.
static void addRecentFontFamily(const QString &family)
Adds a font family to the list of recently used font families.
static QString untranslateNamedStyle(const QString &namedStyle)
Returns the english named style of a font, if possible.
static QString buildSourcePath()
Returns path to the source directory. Valid only when running from build directory.
static bool fontFamilyMatchOnSystem(const QString &family, QString *chosen=nullptr, bool *match=nullptr)
Check whether font family is on system.
QVariant value(const QString &key, const QVariant &defaultValue=QVariant(), const Section section=NoSection) const
Returns the value for setting key.
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 QDomElement toXmlElement(const QFont &font, QDomDocument &document, const QString &elementName)
Returns a DOM element containing the properties of the font.
static QMimeData * toMimeData(const QFont &font)
Returns new mime data representing the specified font settings.