Skip to content

Instantly share code, notes, and snippets.

@elpuri
Created December 27, 2010 18:51
Show Gist options
  • Save elpuri/756413 to your computer and use it in GitHub Desktop.
Save elpuri/756413 to your computer and use it in GitHub Desktop.
Small utility class for loading custom fonts on Symbian
/*
Juha Turunen ([email protected]), 2010
Use freely in any commercial or non-commercial way. All rights waived.
More Qt/QtQuick/Symbian stuff at: http://www.juhaturunen.com/blog/
Instructions:
- You need to link to a few Symbian DLLs. Add this line to your applications .pro file:
symbian { LIBS += -lcone -lws32 -lbafl -legul }
- Deploy your font files to a place where the Symbian font and bitmap server can read them.
\resources\<your application UID>\ would be a good place. Do NOT deploy to \resources\fonts
(causes trouble during uninstall of your app) or your application's private directory (Symbian
FBS can't access the file).
- If you expect your app to be frequently used and want to shave a few milliseconds from the startup,
use the constructor parameter retainFonts. If a true value is provided, the fonts will not be released
when your application terminates.
- Use Symbian style paths with backslashes e.g. c:\directory\file.txt
- Example usage:
#ifdef Q_OS_SYMBIAN
#include "SymbianFontLoader.h"
#endif Q_OS_SYMBIAN
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
#ifdef Q_OS_SYMBIAN
SymbianFontLoader fontLoader(true);
fontLoader.load("c:\\resources\\DEADBEEF\\perkelica.ttf");
// In case you're interested which fonts are installed
qDebug() << fontLoader.loadedFonts();
#endif
// Rest of your stuff here
return a.exec();
}
*/
#ifndef SYMBIANFONTLOADER_H
#define SYMBIANFONTLOADER_H
#include <QString>
#include <coemain.h>
#include <w32std.h>
#include <badesca.h>
#include <gulutil.h>
class SymbianFontLoader
{
public:
SymbianFontLoader(bool retainFonts = false) {
m_retainFonts = retainFonts;
m_screenDevice = CCoeEnv::Static()->ScreenDevice();
}
~SymbianFontLoader() {
if (!m_retainFonts)
foreach(int id, m_fontIds)
m_screenDevice->RemoveFile(id);
}
QStringList loadedFonts() {
QStringList fonts;
TRAPD(err, listFontsL(fonts));
qt_symbian_throwIfError(err);
return fonts;
}
void load(QString path) {
TPtrC fontFile(path.utf16(), path.length());
int fontId;
int err = m_screenDevice->AddFile(fontFile, fontId);
qt_symbian_throwIfError(err);
if (!m_retainFonts)
m_fontIds << fontId;
}
private:
void listFontsL(QStringList& fonts) {
CDesCArrayFlat *fontNames = new (ELeave) CDesCArrayFlat(5);
CleanupStack::PushL(fontNames);
FontUtils::GetAvailableFontsL(*m_screenDevice, *fontNames, EGulAllFonts);
int fontCount = fontNames->Length();
for (int i=0; i < fontCount; i++) {
TPtrC fontName = fontNames->MdcaPoint(i);
QString font = QString::fromUtf16(fontName.Ptr(), fontName.Length());
fonts << font;
}
CleanupStack::PopAndDestroy();
}
private:
CWsScreenDevice* m_screenDevice;
bool m_retainFonts;
QList<int> m_fontIds;
};
#endif // SYMBIANFONTLOADER_H
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment