MythTV  0.28pre
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Groups Pages
mythfontmanager.cpp
Go to the documentation of this file.
1 #include <QDir>
2 #include <QFileInfo>
3 #include <QFontDatabase>
4 #include <QList>
5 #include <QMutexLocker>
6 
7 #include "mythfontmanager.h"
8 #include "mythlogging.h"
9 
11 
12 #define LOC QString("MythFontManager: ")
13 #define MAX_DIRS 100
14 
28 void MythFontManager::LoadFonts(const QString &directory,
29  const QString &registeredFor)
30 {
31  int maxDirs = MAX_DIRS;
32  LoadFonts(directory, registeredFor, &maxDirs);
33 }
34 
47 void MythFontManager::LoadFonts(const QString &directory,
48  const QString &registeredFor, int *maxDirs)
49 {
50  if (directory.isEmpty() || directory == "/" || registeredFor.isEmpty())
51  return;
52  (*maxDirs)--;
53  if (*maxDirs < 1)
54  {
55  LOG(VB_GENERAL, LOG_WARNING, LOC +
56  "Reached the maximum directory depth "
57  "for a font directory structure. Terminating font scan.");
58  return;
59  }
60 
61  // Load the font files from this directory
62  LoadFontsFromDirectory(directory, registeredFor);
63  // Recurse through subdirectories
64  QDir dir(directory);
65  QFileInfoList files = dir.entryInfoList();
66  QFileInfo info;
67  for (QFileInfoList::const_iterator it = files.begin();
68  ((it != files.end()) && (*maxDirs > 0)); ++it)
69  {
70  info = *it;
71  // Skip '.' and '..' and other files starting with "." by checking
72  // baseName()
73  if (!info.baseName().isEmpty() && info.isDir())
74  LoadFonts(info.absoluteFilePath(), registeredFor, maxDirs);
75  }
76 }
77 
84 void MythFontManager::ReleaseFonts(const QString &registeredFor)
85 {
86  if (registeredFor.isEmpty())
87  return;
88 
89  QMutexLocker locker(&m_lock);
90  for (FontPathToReference::iterator it = m_fontPathToReference.begin();
91  it != m_fontPathToReference.end();)
92  {
93  MythFontReference *fontRef = it.value();
94  if (registeredFor == fontRef->GetRegisteredFor())
95  {
96  LOG(VB_FILE, LOG_DEBUG, LOC +
97  QString("Removing application font '%1'")
98  .arg(fontRef->GetFontPath()));
99 
100  it = m_fontPathToReference.erase(it);
101  if (!IsFontFileLoaded(fontRef->GetFontPath()))
102  {
103  if (QFontDatabase::removeApplicationFont(fontRef->GetFontID()))
104  {
105  LOG(VB_FILE, LOG_DEBUG, LOC +
106  QString("Successfully removed application font '%1'")
107  .arg(fontRef->GetFontPath()));
108  }
109  else
110  {
111  LOG(VB_GENERAL, LOG_WARNING, LOC +
112  QString("Unable to remove application font '%1'")
113  .arg(fontRef->GetFontPath()));
114  }
115  }
116  delete fontRef;
117  }
118  else
119  {
120  ++it;
121  }
122  }
123 }
124 
134 void MythFontManager::LoadFontsFromDirectory(const QString &directory,
135  const QString &registeredFor)
136 {
137  if (directory.isEmpty() || directory == "/" || registeredFor.isEmpty())
138  return;
139 
140  LOG(VB_FILE, LOG_DEBUG, LOC +
141  QString("Scanning directory '%1' for font files.").arg(directory));
142 
143  QDir dir(directory);
144  QStringList nameFilters = QStringList() << "*.ttf" << "*.otf" << "*.ttc";
145  QStringList fontFiles = dir.entryList(nameFilters);
146  for (QStringList::const_iterator it = fontFiles.begin();
147  it != fontFiles.end(); ++it)
148  {
149  LoadFontFile(dir.absoluteFilePath(*it), registeredFor);
150  }
151 }
152 
159 void MythFontManager::LoadFontFile(const QString &fontPath,
160  const QString &registeredFor)
161 {
162  if (fontPath.isEmpty() || fontPath == "/" || registeredFor.isEmpty())
163  return;
164 
165  QMutexLocker locker(&m_lock);
166  if (IsFontFileLoaded(fontPath))
167  {
168  LOG(VB_GUI | VB_FILE, LOG_INFO, LOC +
169  QString("Font file '%1' already loaded")
170  .arg(fontPath));
171 
172  if (!RegisterFont(fontPath, registeredFor))
173  {
174  LOG(VB_GUI | VB_FILE, LOG_INFO, LOC +
175  QString("Unable to load font(s) in file '%1'")
176  .arg(fontPath));
177  }
178  }
179  else
180  {
181  LOG(VB_GUI | VB_FILE, LOG_INFO, LOC +
182  QString("Loading font file: '%1'").arg(fontPath));
183 
184  int result = QFontDatabase::addApplicationFont(fontPath);
185  if (result > -1)
186  {
187  LOG(VB_GUI | VB_FILE, LOG_DEBUG, LOC +
188  QString("In file '%1', found font(s) '%2'")
189  .arg(fontPath)
190  .arg(QFontDatabase::applicationFontFamilies(result)
191  .join(", ")));
192 
193  if (!RegisterFont(fontPath, registeredFor, result))
194  {
195  LOG(VB_GENERAL, LOG_WARNING, LOC +
196  QString("Unable to register font(s) in file '%1'")
197  .arg(fontPath));
198  }
199  }
200  else
201  {
202  LOG(VB_GENERAL, LOG_WARNING, LOC +
203  QString("Unable to load font(s) in file '%1'")
204  .arg(fontPath));
205  }
206  }
207 }
208 
215 bool MythFontManager::RegisterFont(const QString &fontPath,
216  const QString &registeredFor,
217  const int fontID)
218 {
219  int id = fontID;
220  if (id == -1)
221  {
222  QList<MythFontReference*> values;
223  values = m_fontPathToReference.values(fontPath);
224  if (values.isEmpty())
225  return false;
226  MythFontReference *ref = values.first();
227  if (ref == NULL)
228  return false;
229  else
230  id = ref->GetFontID();
231  }
232  MythFontReference *fontReference;
233  fontReference = new MythFontReference(fontPath, registeredFor, id);
234  m_fontPathToReference.insert(fontPath, fontReference);
235  return true;
236 }
237 
243 bool MythFontManager::IsFontFileLoaded(const QString &fontPath)
244 {
245  QList<MythFontReference*> values = m_fontPathToReference.values(fontPath);
246  return !values.isEmpty();
247 }
248 
250 {
251  if (!gFontManager)
252  gFontManager = new MythFontManager();
253  return gFontManager;
254 }
255 
257 {
259 }
260 
261 MythFontReference::MythFontReference(const QString &fontPath,
262  const QString &registeredFor,
263  const int fontID) :
264  m_fontPath(fontPath), m_registeredFor(registeredFor), m_fontID(fontID)
265 {
266 }
267 
268 /* vim: set expandtab tabstop=4 shiftwidth=4: */
void LoadFonts(const QString &directory, const QString &registeredFor)
Loads the fonts in font files within the given directory structure.
void LoadFontFile(const QString &fontPath, const QString &registeredFor)
Loads fonts from the file specified in fontPath.
bool IsFontFileLoaded(const QString &fontPath)
Checks whether the specified font file has already been loaded.
void ReleaseFonts(const QString &registeredFor)
Removes the font references for registeredFor, and unloads the application font if it's no longer in ...
QString GetRegisteredFor(void) const
int GetFontID(void) const
QString GetFontPath(void) const
MythFontReference(const QString &fontPath, const QString &registeredFor, const int fontID)
MythFontManager * GetGlobalFontManager(void)
bool RegisterFont(const QString &fontPath, const QString &registeredFor, const int fontID=-1)
Registers the font as being used by registeredFor.
static MythFontManager * gFontManager
void LoadFontsFromDirectory(const QString &directory, const QString &registeredFor)
Loads fonts from font files in the specified directory.
static const QString LOC
FontPathToReference m_fontPathToReference
static MythFontManager * GetGlobalFontManager(void)