QGIS API Documentation  2.99.0-Master (b8fd1fd)
qgsfontutils.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgsfontutils.h
3  ---------------------
4  begin : June 5, 2013
5  copyright : (C) 2013 by Larry Shaffer
6  email : larrys at dakotacarto dot com
7  ***************************************************************************
8  * *
9  * This program is free software; you can redistribute it and/or modify *
10  * it under the terms of the GNU General Public License as published by *
11  * the Free Software Foundation; either version 2 of the License, or *
12  * (at your option) any later version. *
13  * *
14  ***************************************************************************/
15 
16 #include "qgsfontutils.h"
17 
18 #include "qgsapplication.h"
19 #include "qgslogger.h"
20 
21 #include <QApplication>
22 #include <QFile>
23 #include <QFont>
24 #include <QFontDatabase>
25 #include <QFontInfo>
26 #include <QStringList>
27 
28 
29 bool QgsFontUtils::fontMatchOnSystem( const QFont &f )
30 {
31  QFontInfo fi = QFontInfo( f );
32  return fi.exactMatch();
33 }
34 
35 bool QgsFontUtils::fontFamilyOnSystem( const QString &family )
36 {
37  QFont tmpFont = QFont( family );
38  // compare just beginning of family string in case 'family [foundry]' differs
39  return tmpFont.family().startsWith( family, Qt::CaseInsensitive );
40 }
41 
42 bool QgsFontUtils::fontFamilyHasStyle( const QString &family, const QString &style )
43 {
44  QFontDatabase fontDB;
45  if ( !fontFamilyOnSystem( family ) )
46  return false;
47 
48  if ( fontDB.styles( family ).contains( style ) )
49  return true;
50 
51 #ifdef Q_OS_WIN
52  QString modified( style );
53  if ( style == "Roman" )
54  modified = "Normal";
55  if ( style == "Oblique" )
56  modified = "Italic";
57  if ( style == "Bold Oblique" )
58  modified = "Bold Italic";
59  if ( fontDB.styles( family ).contains( modified ) )
60  return true;
61 #endif
62 
63  return false;
64 }
65 
66 bool QgsFontUtils::fontFamilyMatchOnSystem( const QString &family, QString *chosen, bool *match )
67 {
68  QFontDatabase fontDB;
69  QStringList fontFamilies = fontDB.families();
70  bool found = false;
71 
72  QList<QString>::const_iterator it = fontFamilies.constBegin();
73  for ( ; it != fontFamilies.constEnd(); ++it )
74  {
75  // first compare just beginning of 'family [foundry]' string
76  if ( it->startsWith( family, Qt::CaseInsensitive ) )
77  {
78  found = true;
79  // keep looking if match info is requested
80  if ( match )
81  {
82  // full 'family [foundry]' strings have to match
83  *match = ( *it == family );
84  if ( *match )
85  break;
86  }
87  else
88  {
89  break;
90  }
91  }
92  }
93 
94  if ( found )
95  {
96  if ( chosen )
97  {
98  // retrieve the family actually assigned by matching algorithm
99  QFont f = QFont( family );
100  *chosen = f.family();
101  }
102  }
103  else
104  {
105  if ( chosen )
106  {
107  *chosen = QString();
108  }
109 
110  if ( match )
111  {
112  *match = false;
113  }
114  }
115 
116  return found;
117 }
118 
119 bool QgsFontUtils::updateFontViaStyle( QFont &f, const QString &fontstyle, bool fallback )
120 {
121  if ( fontstyle.isEmpty() )
122  {
123  return false;
124  }
125 
126  QFontDatabase fontDB;
127 
128  if ( !fallback )
129  {
130  // does the font even have the requested style?
131  bool hasstyle = fontFamilyHasStyle( f.family(), fontstyle );
132  if ( !hasstyle )
133  {
134  return false;
135  }
136  }
137 
138  // is the font's style already the same as requested?
139  if ( fontstyle == fontDB.styleString( f ) )
140  {
141  return false;
142  }
143 
144  QFont appfont = QApplication::font();
145  int defaultSize = appfont.pointSize(); // QFontDatabase::font() needs an integer for size
146 
147  QFont styledfont;
148  bool foundmatch = false;
149 
150  // if fontDB.font() fails, it returns the default app font; but, that may be the target style
151  styledfont = fontDB.font( f.family(), fontstyle, defaultSize );
152  if ( appfont != styledfont || fontstyle != fontDB.styleString( f ) )
153  {
154  foundmatch = true;
155  }
156 
157  // default to first found style if requested style is unavailable
158  // this helps in the situations where the passed-in font has to have a named style applied
159  if ( fallback && !foundmatch )
160  {
161  QFont testFont = QFont( f );
162  testFont.setPointSize( defaultSize );
163 
164  // prefer a style that mostly matches the passed-in font
165  Q_FOREACH ( const QString &style, fontDB.styles( f.family() ) )
166  {
167  styledfont = fontDB.font( f.family(), style, defaultSize );
168  styledfont = styledfont.resolve( f );
169  if ( testFont.toString() == styledfont.toString() )
170  {
171  foundmatch = true;
172  break;
173  }
174  }
175 
176  // fallback to first style found that works
177  if ( !foundmatch )
178  {
179  Q_FOREACH ( const QString &style, fontDB.styles( f.family() ) )
180  {
181  styledfont = fontDB.font( f.family(), style, defaultSize );
182  if ( QApplication::font() != styledfont )
183  {
184  foundmatch = true;
185  break;
186  }
187  }
188  }
189  }
190 
191  // similar to QFont::resolve, but font may already have pixel size set
192  // and we want to make sure that's preserved
193  if ( foundmatch )
194  {
195  if ( !qgsDoubleNear( f.pointSizeF(), -1 ) )
196  {
197  styledfont.setPointSizeF( f.pointSizeF() );
198  }
199  else if ( f.pixelSize() != -1 )
200  {
201  styledfont.setPixelSize( f.pixelSize() );
202  }
203  styledfont.setCapitalization( f.capitalization() );
204  styledfont.setUnderline( f.underline() );
205  styledfont.setStrikeOut( f.strikeOut() );
206  styledfont.setWordSpacing( f.wordSpacing() );
207  styledfont.setLetterSpacing( QFont::AbsoluteSpacing, f.letterSpacing() );
208  f = styledfont;
209 
210  return true;
211  }
212 
213  return false;
214 }
215 
217 {
218  return QStringLiteral( "QGIS Vera Sans" );
219 }
220 
221 bool QgsFontUtils::loadStandardTestFonts( const QStringList &loadstyles )
222 {
223  // load standard test font from filesystem or testdata.qrc (for unit tests and general testing)
224  bool fontsLoaded = false;
225 
226  QString fontFamily = standardTestFontFamily();
227  QMap<QString, QString> fontStyles;
228  fontStyles.insert( QStringLiteral( "Roman" ), QStringLiteral( "QGIS-Vera/QGIS-Vera.ttf" ) );
229  fontStyles.insert( QStringLiteral( "Oblique" ), QStringLiteral( "QGIS-Vera/QGIS-VeraIt.ttf" ) );
230  fontStyles.insert( QStringLiteral( "Bold" ), QStringLiteral( "QGIS-Vera/QGIS-VeraBd.ttf" ) );
231  fontStyles.insert( QStringLiteral( "Bold Oblique" ), QStringLiteral( "QGIS-Vera/QGIS-VeraBI.ttf" ) );
232 
233  QMap<QString, QString>::const_iterator f = fontStyles.constBegin();
234  for ( ; f != fontStyles.constEnd(); ++f )
235  {
236  QString fontstyle( f.key() );
237  QString fontpath( f.value() );
238  if ( !( loadstyles.contains( fontstyle ) || loadstyles.contains( QStringLiteral( "All" ) ) ) )
239  {
240  continue;
241  }
242 
243  if ( fontFamilyHasStyle( fontFamily, fontstyle ) )
244  {
245  QgsDebugMsg( QString( "Test font '%1 %2' already available" ).arg( fontFamily, fontstyle ) );
246  }
247  else
248  {
249  bool loaded = false;
251  {
252  // workaround for bugs with Qt 4.8.5 (other versions?) on Mac 10.9, where fonts
253  // from qrc resources load but fail to work and default font is substituted [LS]:
254  // https://bugreports.qt-project.org/browse/QTBUG-30917
255  // https://bugreports.qt-project.org/browse/QTBUG-32789
256  QString fontPath( QgsApplication::buildSourcePath() + "/tests/testdata/font/" + fontpath );
257  int fontID = QFontDatabase::addApplicationFont( fontPath );
258  loaded = ( fontID != -1 );
259  fontsLoaded = ( fontsLoaded || loaded );
260  QgsDebugMsg( QString( "Test font '%1 %2' %3 from filesystem [%4]" )
261  .arg( fontFamily, fontstyle, loaded ? "loaded" : "FAILED to load", fontPath ) );
262  QFontDatabase db;
263  QgsDebugMsg( QString( "font families in %1: %2" ).arg( fontID ).arg( db.applicationFontFamilies( fontID ).join( "," ) ) );
264  }
265  else
266  {
267  QFile fontResource( ":/testdata/font/" + fontpath );
268  if ( fontResource.open( QIODevice::ReadOnly ) )
269  {
270  int fontID = QFontDatabase::addApplicationFontFromData( fontResource.readAll() );
271  loaded = ( fontID != -1 );
272  fontsLoaded = ( fontsLoaded || loaded );
273  }
274  QgsDebugMsg( QString( "Test font '%1' %3 from testdata.qrc" )
275  .arg( fontFamily, fontstyle, loaded ? "loaded" : "FAILED to load" ) );
276  }
277  }
278  }
279 
280  return fontsLoaded;
281 }
282 
283 QFont QgsFontUtils::getStandardTestFont( const QString &style, int pointsize )
284 {
285  if ( ! fontFamilyHasStyle( standardTestFontFamily(), style ) )
286  {
287  loadStandardTestFonts( QStringList() << style );
288  }
289 
290  QFontDatabase fontDB;
291  QFont f = fontDB.font( standardTestFontFamily(), style, pointsize );
292 #ifdef Q_OS_WIN
293  if ( !f.exactMatch() )
294  {
295  QString modified;
296  if ( style == "Roman" )
297  modified = "Normal";
298  else if ( style == "Oblique" )
299  modified = "Italic";
300  else if ( style == "Bold Oblique" )
301  modified = "Bold Italic";
302  if ( !modified.isEmpty() )
303  f = fontDB.font( standardTestFontFamily(), modified, pointsize );
304  }
305  if ( !f.exactMatch() )
306  {
307  QgsDebugMsg( QString( "Inexact font match - consider installing the %1 font." ).arg( standardTestFontFamily() ) );
308  QgsDebugMsg( QString( "Requested: %1" ).arg( f.toString() ) );
309  QFontInfo fi( f );
310  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() ) );
311  }
312 #endif
313  // in case above statement fails to set style
314  f.setBold( style.contains( QLatin1String( "Bold" ) ) );
315  f.setItalic( style.contains( QLatin1String( "Oblique" ) ) || style.contains( QLatin1String( "Italic" ) ) );
316 
317  return f;
318 }
319 
320 QDomElement QgsFontUtils::toXmlElement( const QFont &font, QDomDocument &document, const QString &elementName )
321 {
322  QDomElement fontElem = document.createElement( elementName );
323  fontElem.setAttribute( QStringLiteral( "description" ), font.toString() );
324  fontElem.setAttribute( QStringLiteral( "style" ), untranslateNamedStyle( font.styleName() ) );
325  return fontElem;
326 }
327 
328 bool QgsFontUtils::setFromXmlElement( QFont &font, const QDomElement &element )
329 {
330  if ( element.isNull() )
331  {
332  return false;
333  }
334 
335  font.fromString( element.attribute( QStringLiteral( "description" ) ) );
336  if ( element.hasAttribute( QStringLiteral( "style" ) ) )
337  {
338  ( void )updateFontViaStyle( font, translateNamedStyle( element.attribute( QStringLiteral( "style" ) ) ) );
339  }
340 
341  return true;
342 }
343 
344 bool QgsFontUtils::setFromXmlChildNode( QFont &font, const QDomElement &element, const QString &childNode )
345 {
346  if ( element.isNull() )
347  {
348  return false;
349  }
350 
351  QDomNodeList nodeList = element.elementsByTagName( childNode );
352  if ( !nodeList.isEmpty() )
353  {
354  QDomElement fontElem = nodeList.at( 0 ).toElement();
355  return setFromXmlElement( font, fontElem );
356  }
357  else
358  {
359  return false;
360  }
361 }
362 
363 static QMap<QString, QString> createTranslatedStyleMap()
364 {
365  QMap<QString, QString> translatedStyleMap;
366  QStringList words = QStringList() << QStringLiteral( "Normal" ) << QStringLiteral( "Light" ) << QStringLiteral( "Bold" ) << QStringLiteral( "Black" ) << QStringLiteral( "Demi" ) << QStringLiteral( "Italic" ) << QStringLiteral( "Oblique" );
367  Q_FOREACH ( const QString &word, words )
368  {
369  translatedStyleMap.insert( QCoreApplication::translate( "QFontDatabase", qPrintable( word ) ), word );
370  }
371  return translatedStyleMap;
372 }
373 
374 QString QgsFontUtils::translateNamedStyle( const QString &namedStyle )
375 {
376  QStringList words = namedStyle.split( ' ', QString::SkipEmptyParts );
377  for ( int i = 0, n = words.length(); i < n; ++i )
378  {
379  words[i] = QCoreApplication::translate( "QFontDatabase", words[i].toUtf8(), nullptr, QCoreApplication::UnicodeUTF8 );
380  }
381  return words.join( QStringLiteral( " " ) );
382 }
383 
384 QString QgsFontUtils::untranslateNamedStyle( const QString &namedStyle )
385 {
386  static QMap<QString, QString> translatedStyleMap = createTranslatedStyleMap();
387  QStringList words = namedStyle.split( ' ', QString::SkipEmptyParts );
388  for ( int i = 0, n = words.length(); i < n; ++i )
389  {
390  if ( translatedStyleMap.contains( words[i] ) )
391  {
392  words[i] = translatedStyleMap.value( words[i] );
393  }
394  else
395  {
396  QgsDebugMsg( QString( "Warning: style map does not contain %1" ).arg( words[i] ) );
397  }
398  }
399  return words.join( QStringLiteral( " " ) );
400 }
401 
402 QString QgsFontUtils::asCSS( const QFont &font, double pointToPixelScale )
403 {
404  QString css = QStringLiteral( "font-family: " ) + font.family() + ';';
405 
406  //style
407  css += QLatin1String( "font-style: " );
408  switch ( font.style() )
409  {
410  case QFont::StyleNormal:
411  css += QLatin1String( "normal" );
412  break;
413  case QFont::StyleItalic:
414  css += QLatin1String( "italic" );
415  break;
416  case QFont::StyleOblique:
417  css += QLatin1String( "oblique" );
418  break;
419  }
420  css += ';';
421 
422  //weight
423  int cssWeight = 400;
424  switch ( font.weight() )
425  {
426  case QFont::Light:
427  cssWeight = 300;
428  break;
429  case QFont::Normal:
430  cssWeight = 400;
431  break;
432  case QFont::DemiBold:
433  cssWeight = 600;
434  break;
435  case QFont::Bold:
436  cssWeight = 700;
437  break;
438  case QFont::Black:
439  cssWeight = 900;
440  break;
441 #if QT_VERSION >= 0x050500
442  case QFont::Thin:
443  cssWeight = 100;
444  break;
445  case QFont::ExtraLight:
446  cssWeight = 200;
447  break;
448  case QFont::Medium:
449  cssWeight = 500;
450  break;
451  case QFont::ExtraBold:
452  cssWeight = 800;
453  break;
454 #endif
455  }
456  css += QStringLiteral( "font-weight: %1;" ).arg( cssWeight );
457 
458  //size
459  css += QStringLiteral( "font-size: %1px;" ).arg( font.pointSizeF() >= 0 ? font.pointSizeF() * pointToPixelScale : font.pixelSize() );
460 
461  return css;
462 }
#define QgsDebugMsg(str)
Definition: qgslogger.h:36
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)
Definition: qgis.h:198
static QString asCSS(const QFont &font, double pointToPixelMultiplier=1.0)
Returns a CSS string representing the specified font as closely as possible.
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 QMap< QString, QString > createTranslatedStyleMap()
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 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.
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.