1 #pragma once 2 3 #include <QFontDatabase> 4 #include <QString> 5 #include <QtDebug> 6 #include <QDir> 7 8 #include "util/cmdlineargs.h" 9 10 class FontUtils { 11 public: initializeFonts(const QString & resourcePath)12 static void initializeFonts(const QString& resourcePath) { 13 QDir fontsDir(resourcePath); 14 if (!fontsDir.cd("fonts")) { 15 #ifdef __LINUX__ 16 // If the fonts already have been installed via the package 17 // manager, this is okay. We currently have no way to verify that 18 // though. 19 qDebug() 20 #else 21 qWarning() 22 #endif 23 << "No fonts directory found in" << resourcePath; 24 return; 25 } 26 27 QList<QFileInfo> files = fontsDir.entryInfoList( 28 QDir::NoDotAndDotDot | QDir::Files | QDir::Readable); 29 foreach (const QFileInfo& file, files) { 30 const QString& path = file.filePath(); 31 32 // Skip text files (e.g. license files). For all others we let Qt tell 33 // us whether the font format is supported since there is no way to 34 // check other than adding. 35 if (path.endsWith(".txt", Qt::CaseInsensitive)) { 36 continue; 37 } 38 39 addFont(path); 40 } 41 } 42 addFont(const QString & path)43 static bool addFont(const QString& path) { 44 int result = QFontDatabase::addApplicationFont(path); 45 if (result == -1) { 46 qWarning() << "Failed to add font:" << path; 47 return false; 48 } 49 50 // In developer mode, spit out all the families / styles / sizes 51 // supported by the new font. 52 if (CmdlineArgs::Instance().getDeveloper()) { 53 QFontDatabase database; 54 QStringList families = QFontDatabase::applicationFontFamilies(result); 55 foreach (const QString& family, families) { 56 QStringList styles = database.styles(family); 57 foreach (const QString& style, styles) { 58 QList<int> pointSizes = database.pointSizes(family, style); 59 QStringList pointSizesStr; 60 foreach (int point, pointSizes) { 61 pointSizesStr.append(QString::number(point)); 62 } 63 qDebug() << "FONT LOADED family:" << family 64 << "style:" << style 65 << "point sizes:" << pointSizesStr.join(","); 66 } 67 } 68 } 69 return true; 70 } 71 72 private: FontUtils()73 FontUtils() {} 74 }; 75