summaryrefslogtreecommitdiffstats
path: root/src/widgets/platforms/mac/qfontdatabase_mac.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/widgets/platforms/mac/qfontdatabase_mac.cpp')
-rw-r--r--src/widgets/platforms/mac/qfontdatabase_mac.cpp142
1 files changed, 84 insertions, 58 deletions
diff --git a/src/widgets/platforms/mac/qfontdatabase_mac.cpp b/src/widgets/platforms/mac/qfontdatabase_mac.cpp
index 6fdaf06c8b..724dbf6c4a 100644
--- a/src/widgets/platforms/mac/qfontdatabase_mac.cpp
+++ b/src/widgets/platforms/mac/qfontdatabase_mac.cpp
@@ -249,6 +249,63 @@ static inline float weightToFloat(unsigned int weight)
return (weight - 50) / 100.0;
}
+static QFontEngine *loadFromDatabase(const QFontDef &req, const QFontPrivate *d)
+{
+#if defined(QT_MAC_USE_COCOA)
+ QCFString fontName = NULL;
+#else
+ ATSFontFamilyRef familyRef = 0;
+ ATSFontRef fontRef = 0;
+#endif
+
+ QStringList family_list = familyList(req);
+
+ const char *stylehint = styleHint(req);
+ if (stylehint)
+ family_list << QLatin1String(stylehint);
+
+ // add QFont::defaultFamily() to the list, for compatibility with previous versions
+ family_list << QApplication::font().defaultFamily();
+
+ QMutexLocker locker(fontDatabaseMutex());
+ QFontDatabasePrivate *db = privateDb();
+ if (!db->count)
+ initializeDb();
+ for (int i = 0; i < family_list.size(); ++i) {
+ for (int k = 0; k < db->count; ++k) {
+ if (db->families[k]->name.compare(family_list.at(i), Qt::CaseInsensitive) == 0) {
+ QByteArray family_name = db->families[k]->name.toUtf8();
+#if defined(QT_MAC_USE_COCOA)
+ QCFType<CTFontRef> ctFont = CTFontCreateWithName(QCFString(db->families[k]->name), 12, NULL);
+ if (ctFont) {
+ fontName = CTFontCopyFullName(ctFont);
+ goto found;
+ }
+#else
+ familyRef = ATSFontFamilyFindFromName(QCFString(db->families[k]->name), kATSOptionFlagsDefault);
+ if (familyRef) {
+ fontRef = ATSFontFindFromName(QCFString(db->families[k]->name), kATSOptionFlagsDefault);
+ goto found;
+ }
+#endif
+ }
+ }
+ }
+found:
+#ifdef QT_MAC_USE_COCOA
+ if (fontName)
+ return new QCoreTextFontEngineMulti(fontName, req, d->kerning);
+#else
+ if (familyRef) {
+ QCFString actualName;
+ if (ATSFontFamilyGetName(familyRef, kATSOptionFlagsDefault, &actualName) == noErr)
+ req.family = actualName;
+ return new QFontEngineMacMulti(familyRef, req, fontDef, d->kerning);
+ }
+#endif
+ return NULL;
+}
+
void QFontDatabase::load(const QFontPrivate *d, int script)
{
// sanity checks
@@ -289,69 +346,38 @@ void QFontDatabase::load(const QFontPrivate *d, int script)
return; // the font info and fontdef should already be filled
}
- //find the font
- QStringList family_list = familyList(req);
-
- const char *stylehint = styleHint(req);
- if (stylehint)
- family_list << QLatin1String(stylehint);
-
- // add QFont::defaultFamily() to the list, for compatibility with
- // previous versions
- family_list << QApplication::font().defaultFamily();
-
+ QFontEngine *engine = NULL;
#if defined(QT_MAC_USE_COCOA)
- QCFString fontName = NULL, familyName = NULL;
-#else
- ATSFontFamilyRef familyRef = 0;
- ATSFontRef fontRef = 0;
-#endif
-
- QMutexLocker locker(fontDatabaseMutex());
- QFontDatabasePrivate *db = privateDb();
- if (!db->count)
- initializeDb();
- for(int i = 0; i < family_list.size(); ++i) {
- for (int k = 0; k < db->count; ++k) {
- if (db->families[k]->name.compare(family_list.at(i), Qt::CaseInsensitive) == 0) {
- QByteArray family_name = db->families[k]->name.toUtf8();
-#if defined(QT_MAC_USE_COCOA)
- QCFType<CTFontRef> ctFont = CTFontCreateWithName(QCFString(db->families[k]->name), 12, NULL);
- if (ctFont) {
- fontName = CTFontCopyFullName(ctFont);
- familyName = CTFontCopyFamilyName(ctFont);
- goto FamilyFound;
- }
-#else
- familyRef = ATSFontFamilyFindFromName(QCFString(db->families[k]->name), kATSOptionFlagsDefault);
- if (familyRef) {
- fontRef = ATSFontFindFromName(QCFString(db->families[k]->name), kATSOptionFlagsDefault);
- goto FamilyFound;
- }
-#endif
+ // Shortcut to get the font directly without going through the font database
+ if (!req.family.isEmpty() && !req.styleName.isEmpty()) {
+ QCFString expectedFamily = QCFString(req.family);
+ QCFString expectedStyle = QCFString(req.styleName);
+
+ QCFType<CFMutableDictionaryRef> attributes = CFDictionaryCreateMutable(NULL, 0,
+ &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks);
+ CFDictionaryAddValue(attributes, kCTFontFamilyNameAttribute, expectedFamily);
+ CFDictionaryAddValue(attributes, kCTFontStyleNameAttribute, expectedStyle);
+
+ QCFType<CTFontDescriptorRef> descriptor = CTFontDescriptorCreateWithAttributes(attributes);
+ CGAffineTransform transform = qt_transform_from_fontdef(req);
+ QCFType<CTFontRef> ctFont = CTFontCreateWithFontDescriptor(descriptor, req.pixelSize, &transform);
+ if (ctFont) {
+ QCFString familyName = CTFontCopyFamilyName(ctFont);
+ // Only accept the font if the family name is exactly the same as we specified
+ if (CFEqual(expectedFamily, familyName)) {
+ engine = new QCoreTextFontEngineMulti(ctFont, req, d->kerning);
}
}
}
-FamilyFound:
- //fill in the engine's font definition
- QFontDef fontDef = d->request; //copy..
- if(fontDef.pointSize < 0)
- fontDef.pointSize = qt_mac_pointsize(fontDef, d->dpi);
- else
- fontDef.pixelSize = qt_mac_pixelsize(fontDef, d->dpi);
-
-#ifdef QT_MAC_USE_COCOA
- fontDef.family = familyName;
- QFontEngine *engine = new QCoreTextFontEngineMulti(fontName, fontDef, d->kerning);
-#else
- QCFString actualName;
- if (ATSFontFamilyGetName(familyRef, kATSOptionFlagsDefault, &actualName) == noErr)
- fontDef.family = actualName;
- QFontEngine *engine = new QFontEngineMacMulti(familyRef, fontRef, fontDef, d->kerning);
#endif
- d->engineData->engine = engine;
- engine->ref.ref(); //a ref for the engineData->engine
- QFontCache::instance()->insertEngine(key, engine);
+ if (!engine)
+ engine = loadFromDatabase(req, d);
+
+ if (engine) {
+ d->engineData->engine = engine;
+ engine->ref.ref();
+ QFontCache::instance()->insertEngine(key, engine);
+ }
}
static void registerFont(QFontDatabasePrivate::ApplicationFont *fnt)