From b2c4c4fc2aee411b4476c6a351688060785aed7e Mon Sep 17 00:00:00 2001 From: Eskil Abrahamsen Blomfeldt Date: Wed, 24 Aug 2011 15:51:07 +0200 Subject: [PATCH] Support QRawFont from raw data with basic unix font db Add support for making a QFontEngine in QPA with the basic unix font database. The code is copy-pasted from qrawfont_ft.cpp. Task-number: QTBUG-20780 Change-Id: Id5492efe634fe90805fbca43356ec428d4d73117 Reviewed-on: http://codereview.qt.nokia.com/3511 Reviewed-by: Qt Sanity Bot Reviewed-by: Jiang Jiang --- .../basicunix/qbasicunixfontdatabase.cpp | 66 +++++++++++++++++++ .../basicunix/qbasicunixfontdatabase_p.h | 1 + 2 files changed, 67 insertions(+) diff --git a/src/platformsupport/fontdatabases/basicunix/qbasicunixfontdatabase.cpp b/src/platformsupport/fontdatabases/basicunix/qbasicunixfontdatabase.cpp index 5a0c0492c1..ee4bf26bb0 100644 --- a/src/platformsupport/fontdatabases/basicunix/qbasicunixfontdatabase.cpp +++ b/src/platformsupport/fontdatabases/basicunix/qbasicunixfontdatabase.cpp @@ -47,6 +47,7 @@ #include #include #include +#include #undef QT_NO_FREETYPE #include @@ -237,6 +238,71 @@ QFontEngine *QBasicUnixFontDatabase::fontEngine(const QFontDef &fontDef, QUnicod return engine; } +namespace { + + class QFontEngineFTRawData: public QFontEngineFT + { + public: + QFontEngineFTRawData(const QFontDef &fontDef) : QFontEngineFT(fontDef) + { + } + + void updateFamilyNameAndStyle() + { + fontDef.family = QString::fromAscii(freetype->face->family_name); + + if (freetype->face->style_flags & FT_STYLE_FLAG_ITALIC) + fontDef.style = QFont::StyleItalic; + + if (freetype->face->style_flags & FT_STYLE_FLAG_BOLD) + fontDef.weight = QFont::Bold; + } + + bool initFromData(const QByteArray &fontData) + { + FaceId faceId; + faceId.filename = ""; + faceId.index = 0; + faceId.uuid = QUuid::createUuid().toByteArray(); + + return init(faceId, true, Format_None, fontData); + } + }; + +} + +QFontEngine *QBasicUnixFontDatabase::fontEngine(const QByteArray &fontData, qreal pixelSize, + QFont::HintingPreference hintingPreference) +{ + QFontDef fontDef; + fontDef.pixelSize = pixelSize; + + QFontEngineFTRawData *fe = new QFontEngineFTRawData(fontDef); + if (!fe->initFromData(fontData)) { + delete fe; + return 0; + } + + fe->updateFamilyNameAndStyle(); + + switch (hintingPreference) { + case QFont::PreferNoHinting: + fe->setDefaultHintStyle(QFontEngineFT::HintNone); + break; + case QFont::PreferFullHinting: + fe->setDefaultHintStyle(QFontEngineFT::HintFull); + break; + case QFont::PreferVerticalHinting: + fe->setDefaultHintStyle(QFontEngineFT::HintLight); + break; + default: + // Leave it as it is + break; + } + + return fe; +} + QStringList QBasicUnixFontDatabase::fallbacksForFamily(const QString family, const QFont::Style &style, const QFont::StyleHint &styleHint, const QUnicodeTables::Script &script) const { Q_UNUSED(family); diff --git a/src/platformsupport/fontdatabases/basicunix/qbasicunixfontdatabase_p.h b/src/platformsupport/fontdatabases/basicunix/qbasicunixfontdatabase_p.h index 9e5c3f0aa6..13c931fac7 100644 --- a/src/platformsupport/fontdatabases/basicunix/qbasicunixfontdatabase_p.h +++ b/src/platformsupport/fontdatabases/basicunix/qbasicunixfontdatabase_p.h @@ -61,6 +61,7 @@ class QBasicUnixFontDatabase : public QPlatformFontDatabase public: void populateFontDatabase(); QFontEngine *fontEngine(const QFontDef &fontDef, QUnicodeTables::Script script, void *handle); + QFontEngine *fontEngine(const QByteArray &fontData, qreal pixelSize, QFont::HintingPreference hintingPreference); QStringList fallbacksForFamily(const QString family, const QFont::Style &style, const QFont::StyleHint &styleHint, const QUnicodeTables::Script &script) const; QStringList addApplicationFont(const QByteArray &fontData, const QString &fileName); void releaseHandle(void *handle);