Mac: fix bugs for font selection in QFontDialog
authorLiang Qi <liang.qi@digia.com>
Tue, 30 Oct 2012 13:01:12 +0000 (14:01 +0100)
committerThe Qt Project <gerrit-noreply@qt-project.org>
Fri, 14 Dec 2012 11:22:15 +0000 (12:22 +0100)
Use localized family name and style name when selecting font with
non-English locale

Task-number: QTBUG-27415
Change-Id: Ie81507ed011fc096e0f5edad146e97c392e86494
Reviewed-by: Eskil Abrahamsen Blomfeldt <eskil.abrahamsen-blomfeldt@digia.com>
(cherry picked from commit 3c09f6bc9aee0c97427fe8da6efdc73b4ac473aa)

src/gui/text/qfontdatabase.cpp
src/plugins/platforms/cocoa/qcocoafontdialoghelper.mm

index b599660..a560b41 100644 (file)
@@ -595,7 +595,7 @@ QtFontFamily *QFontDatabasePrivate::family(const QString &f, bool create)
     if (res < 0)
         pos++;
 
-    // qDebug("adding family %s at %d total=%d", f.latin1(), pos, count);
+    // qDebug() << "adding family " << f.toLatin1() << " at " << pos << " total=" << count;
     if (!(count % 8)) {
         QtFontFamily **newFamilies = (QtFontFamily **)
                    realloc(families,
index 5ccd019..ff3ba63 100644 (file)
@@ -92,20 +92,16 @@ static QFont qfontForCocoaFont(NSFont *cocoaFont, const QFont &resolveFont)
     QFont newFont;
     if (cocoaFont) {
         int pSize = qRound([cocoaFont pointSize]);
-        QString family(QCFString::toQString([cocoaFont familyName]));
-        QString typeface(QCFString::toQString([cocoaFont fontName]));
-
-        int hyphenPos = typeface.indexOf(QLatin1Char('-'));
-        if (hyphenPos != -1) {
-            typeface.remove(0, hyphenPos + 1);
-        } else {
-            typeface = QLatin1String("Normal");
-        }
+        CTFontDescriptorRef font = CTFontCopyFontDescriptor((CTFontRef)cocoaFont);
+        // QCoreTextFontDatabase::populateFontDatabase() is using localized names
+        QString family = QCFString::toQString((CFStringRef) CTFontDescriptorCopyLocalizedAttribute(font, kCTFontFamilyNameAttribute, NULL));
+        QString style = QCFString::toQString((CFStringRef) CTFontDescriptorCopyLocalizedAttribute(font, kCTFontStyleNameAttribute, NULL));
 
-        newFont = QFontDatabase().font(family, typeface, pSize);
+        newFont = QFontDatabase().font(family, style, pSize);
         newFont.setUnderline(resolveFont.underline());
         newFont.setStrikeOut(resolveFont.strikeOut());
 
+        CFRelease(font);
     }
     return newFont;
 }