Replace deprecated ATS calls with CTFontManager calls in 10.8
authorJiang Jiang <jiang.jiang@nokia.com>
Mon, 20 Aug 2012 08:37:01 +0000 (10:37 +0200)
committerQt by Nokia <qt-info@nokia.com>
Tue, 21 Aug 2012 11:41:01 +0000 (13:41 +0200)
Change-Id: I81c0361059319575e55621123d40b7c6f3c6b699
Reviewed-by: Morten Johan Sørvig <morten.sorvig@nokia.com>
src/platformsupport/fontdatabases/mac/qcoretextfontdatabase.mm

index 50f49b1..7d778e1 100644 (file)
@@ -351,9 +351,51 @@ QStringList QCoreTextFontDatabase::fallbacksForFamily(const QString family, cons
 }
 
 #ifndef Q_OS_IOS
-OSErr qt_mac_create_fsref(const QString &file, FSRef *fsref);
 QStringList QCoreTextFontDatabase::addApplicationFont(const QByteArray &fontData, const QString &fileName)
 {
+#if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_8
+    if (QSysInfo::QSysInfo::MacintoshVersion >= QSysInfo::MV_10_8) {
+        CTFontRef font = NULL;
+
+        if (!fontData.isEmpty()) {
+            QCFType<CGDataProviderRef> dataProvider = CGDataProviderCreateWithData(NULL,
+                fontData.constData(), fontData.size(), NULL);
+            CGFontRef cgFont = CGFontCreateWithDataProvider(dataProvider);
+            if (cgFont) {
+                CFErrorRef error;
+                bool success = CTFontManagerRegisterGraphicsFont(cgFont, &error);
+                if (success) {
+                    font = CTFontCreateWithGraphicsFont(cgFont, 0.0, NULL, NULL);
+                } else {
+                    NSLog(@"Unable to register font: %@", error);
+                    CFRelease(error);
+                }
+            }
+        } else {
+            CFErrorRef error;
+            QCFType<CFURLRef> fontURL = CFURLCreateWithFileSystemPath(NULL, QCFString(fileName), 0, false);
+            bool success = CTFontManagerRegisterFontsForURL(fontURL, kCTFontManagerScopeProcess, &error);
+            if (success) {
+                const void *keys[] = { fontURL };
+                const void *values[] = { kCTFontURLAttribute };
+                QCFType<CFDictionaryRef> attributes = CFDictionaryCreate(NULL, keys, values, 1,
+                    &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks);
+                QCFType<CTFontDescriptorRef> descriptor = CTFontDescriptorCreateWithAttributes(attributes);
+                font = CTFontCreateWithFontDescriptor(descriptor, 0.0, NULL);
+            } else {
+                NSLog(@"Unable to register font: %@", error);
+                CFRelease(error);
+            }
+        }
+
+        if (font) {
+            QStringList families;
+            families.append(QCFString(CTFontCopyFamilyName(font)));
+            CFRelease(font);
+            return families;
+        }
+    } else {
+#else
     ATSFontContainerRef fontContainer;
     OSStatus e;
 
@@ -363,6 +405,7 @@ QStringList QCoreTextFontDatabase::addApplicationFont(const QByteArray &fontData
                                       kATSOptionFlagsDefault, &fontContainer);
     } else {
         FSRef ref;
+        OSErr qt_mac_create_fsref(const QString &file, FSRef *fsref);
         if (qt_mac_create_fsref(fileName, &ref) != noErr)
             return QStringList();
         e = ATSFontActivateFromFileReference(&ref, kATSFontContextLocal, kATSFontFormatUnspecified, 0,
@@ -388,6 +431,10 @@ QStringList QCoreTextFontDatabase::addApplicationFont(const QByteArray &fontData
 
         return families;
     }
+#endif
+#if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_8
+    }
+#endif
 
     return QStringList();
 }