Ticket #47466: patch-improve-fontweight-support.diff

File patch-improve-fontweight-support.diff, 17.2 KB (added by RJVB (René Bertin), 9 years ago)
  • src/gui/dialogs/qfontdialog.cpp

    diff --git src/gui/dialogs/qfontdialog.cpp src/gui/dialogs/qfontdialog.cpp
    index d791462..4de4565 100644
    void QFontDialogPrivate::updateStyles() 
    563563    Q_Q(QFontDialog);
    564564    QStringList styles = fdb.styles(familyList->currentText());
    565565    styleList->model()->setStringList(styles);
     566    // RJVB
     567//    qDebug() << "QFontDialogPrivate::updateStyles() familyList="
     568//            << familyList << "style=" << style
     569//            << "styles=" << styles << "styleList=" << styleList;
    566570
    567571    if (styles.isEmpty()) {
    568572        styleEdit->clear();
    void QFontDialogPrivate::updateStyles() 
    572576            bool found = false;
    573577            bool first = true;
    574578            QString cstyle = style;
     579            // RJVB 20150415
     580            QStringList lightStyles, normalStyles, demiBoldStyles, blackStyles;
     581            lightStyles << "Thin" << "Light" << "Book";
     582            normalStyles << "Normal" << "Regular";
     583            demiBoldStyles << "DemiBold" << "Demi Bold" << "SemiBold" << "Semi Bold" << "Medium";
     584            blackStyles << "Black" << "Ultra" << "Heavy" << "UltraBold";
    575585
    576586        redo:
    577587            for (int i = 0; i < (int)styleList->count(); i++) {
    578                 if (cstyle == styleList->text(i)) {
     588                QString istyle = styleList->text(i);
     589                if (cstyle == istyle) {
    579590                     styleList->setCurrentItem(i);
    580591                     found = true;
    581592                     break;
    582593                 }
     594                // RJVB 20150415: handle style synonyms
     595                if ((lightStyles.contains(cstyle, Qt::CaseInsensitive) && lightStyles.contains(istyle, Qt::CaseInsensitive))
     596                        || (normalStyles.contains(cstyle, Qt::CaseInsensitive) && normalStyles.contains(istyle, Qt::CaseInsensitive))
     597                        || (demiBoldStyles.contains(cstyle, Qt::CaseInsensitive) && demiBoldStyles.contains(istyle, Qt::CaseInsensitive))
     598                        || (blackStyles.contains(cstyle, Qt::CaseInsensitive) && blackStyles.contains(istyle, Qt::CaseInsensitive))) {
     599                    styleList->setCurrentItem(i);
     600                    found = true;
     601                    break;
     602                }
    583603            }
    584604            if (!found && first) {
    585605                if (cstyle.contains(QLatin1String("Italic"))) {
    void QFontDialog::setCurrentFont(const QFont &font) 
    815835        QFontInfo fi(font);
    816836        d->size = fi.pointSize();
    817837    }
     838    // RJVB
     839//    qDebug() << "QFontDialog::setCurrentFont" << font << "style=" << d->style << "size=" << d->size;
    818840    d->strikeout->setChecked(font.strikeOut());
    819841    d->underline->setChecked(font.underline());
    820842    d->updateFamilies();
  • src/gui/dialogs/qfontdialog_mac.mm

    diff --git src/gui/dialogs/qfontdialog_mac.mm src/gui/dialogs/qfontdialog_mac.mm
    index d557a7a..8351391 100644
    void QFontDialogPrivate::setFont(void *delegate, const QFont &font) 
    512512#if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_5
    513513    if (qstrcmp(fe->name(), "CoreText") == 0) {
    514514        nsFont = reinterpret_cast<const NSFont *>(static_cast<QCoreTextFontEngineMulti *>(fe)->ctfont);
     515        // RJVB
     516//        qDebug() << "setFont" << fe << "gives:";
     517//        NSLog(@"setFont(%@)", nsFont);
    515518    } else
    516519#endif
    517520    {
    void QFontDialogPrivate::setFont(void *delegate, const QFont &font) 
    520523        if (font.style() == QFont::StyleItalic) {
    521524            mask |= NSItalicFontMask;
    522525        }
    523         if (font.weight() == QFont::Bold) {
    524             weight = 9;
    525             mask |= NSBoldFontMask;
     526        // RJVB
     527        // Thin,Light -> 3, Book -> 4
     528        // Normal/Regular -> 5
     529        // Medium/SemiBold/Demibold -> 6,7,8
     530        // Bold -> 9
     531        // Ultra/Black/Heavy -> 10,11
     532        QByteArray *weights = NULL;
     533        switch (font.weight()) {
     534            case QFont::Light:
     535                weights = new QByteArray("3,4");
     536                break;
     537            case QFont::Normal:
     538                weights = new QByteArray("5");
     539                break;
     540            case QFont::DemiBold:
     541                weights = new QByteArray("6,7,8");
     542                break;
     543            case QFont::Bold:
     544                weights = new QByteArray("9");
     545                break;
     546            case QFont::Black:
     547                weights = new QByteArray("10,11");
     548                break;
    526549        }
    527550
    528551        NSFontManager *mgr = [NSFontManager sharedFontManager];
    529552        QFontInfo fontInfo(font);
    530         nsFont = [mgr fontWithFamily:qt_mac_QStringToNSString(fontInfo.family())
    531             traits:mask
    532             weight:weight
    533             size:fontInfo.pointSize()];
     553        if (weights) {
     554            nsFont = NULL;
     555            for (int i = 0 ; i < weights->size() && !nsFont ; ++i) {
     556                weight = (*weights)[i] - '0';
     557                nsFont = [mgr fontWithFamily:qt_mac_QStringToNSString(fontInfo.family())
     558                         traits:mask
     559                         weight:weight
     560                         size:fontInfo.pointSize()];
     561            }
     562            delete weights;
     563        }
     564        else {
     565            if (font.weight() == QFont::Bold) {
     566                weight = 9;
     567                mask |= NSBoldFontMask;
     568            }
     569            nsFont = [mgr fontWithFamily:qt_mac_QStringToNSString(fontInfo.family())
     570                     traits:mask
     571                     weight:weight
     572                     size:fontInfo.pointSize()];
     573        }
    534574    }
    535575
    536576    [mgr setSelectedFont:const_cast<NSFont *>(nsFont) isMultiple:NO];
  • src/gui/kernel/qt_mac.cpp

    diff --git src/gui/kernel/qt_mac.cpp src/gui/kernel/qt_mac.cpp
    index fb241ce..5c54299 100644
    QFont qfontForThemeFont(ThemeFontID themeID) 
    7171    CFNumberRef num = static_cast<CFNumberRef>(CFDictionaryGetValue(dict, kCTFontWeightTrait));
    7272    float fW;
    7373    CFNumberGetValue(num, kCFNumberFloat32Type, &fW);
    74     QFont::Weight wght = fW > 0. ? QFont::Bold : QFont::Normal;
     74    // RJVB 20150415
     75    extern QFont::Weight weightFromInteger(int weight); // in qfontdatabase.cpp
     76    //QFont::Weight wght = fW > 0. ? QFont::Bold : QFont::Normal;
     77    QFont::Weight wght = weightFromInteger((int)fW*500+500);
    7578    num = static_cast<CFNumberRef>(CFDictionaryGetValue(dict, kCTFontSlantTrait));
    7679    CFNumberGetValue(num, kCFNumberFloatType, &fW);
    7780    bool italic = (fW != 0.0);
  • src/gui/text/qfontdatabase.cpp

    diff --git src/gui/text/qfontdatabase.cpp src/gui/text/qfontdatabase.cpp
    index 4c2ace4..391f414 100644
    static int getFontWeight(const QString &weightString) 
    103103    QString s = weightString.toLower();
    104104
    105105    // Test in decreasing order of commonness
    106     if (s == QLatin1String("medium") ||
     106    // RJVB 20150415: "Medium" is *not* normal weight, but equivalent to Semi/DemiBold;
     107    // however "regular" == "normal"
     108    if (s == QLatin1String("regular") ||
    107109        s == QLatin1String("normal")
    108         || s.compare(QApplication::translate("QFontDatabase", "Normal"), Qt::CaseInsensitive) == 0)
     110        || s.compare(QApplication::translate("QFontDatabase", "Normal"), Qt::CaseInsensitive) == 0
     111        || s.compare(QApplication::translate("QFontDatabase", "Regular"), Qt::CaseInsensitive) == 0)
    109112        return QFont::Normal;
    110113    if (s == QLatin1String("bold")
    111114        || s.compare(QApplication::translate("QFontDatabase", "Bold"), Qt::CaseInsensitive) == 0)
    112115        return QFont::Bold;
     116    // RJVB 20150415: add SemiBold and Medium
    113117    if (s == QLatin1String("demibold") || s == QLatin1String("demi bold")
    114         || s.compare(QApplication::translate("QFontDatabase", "Demi Bold"), Qt::CaseInsensitive) == 0)
     118        || s == QLatin1String("semibold") || s == QLatin1String("semi bold")
     119        || s == QLatin1String("medium")
     120        || s.compare(QApplication::translate("QFontDatabase", "Demi Bold"), Qt::CaseInsensitive) == 0
     121        || s.compare(QApplication::translate("QFontDatabase", "Semi Bold"), Qt::CaseInsensitive) == 0
     122        || s.compare(QApplication::translate("QFontDatabase", "Medium"), Qt::CaseInsensitive) == 0)
    115123        return QFont::DemiBold;
    116     if (s == QLatin1String("black")
    117         || s.compare(QApplication::translate("QFontDatabase", "Black"), Qt::CaseInsensitive) == 0)
     124    // RJVB 20150415: add Heavy and Ultra
     125    if (s == QLatin1String("black") || s == QLatin1String("Heavy") || s == QLatin1String("Ultra") || s == QLatin1String("UltraBold")
     126        || s.compare(QApplication::translate("QFontDatabase", "Black"), Qt::CaseInsensitive) == 0
     127        || s.compare(QApplication::translate("QFontDatabase", "Heavy"), Qt::CaseInsensitive) == 0
     128        || s.compare(QApplication::translate("QFontDatabase", "UltraBold"), Qt::CaseInsensitive) == 0
     129        || s.compare(QApplication::translate("QFontDatabase", "Ultra"), Qt::CaseInsensitive) == 0)
    118130        return QFont::Black;
    119     if (s == QLatin1String("light"))
     131    // RJVB 20150415: add Book and Thin
     132    if (s == QLatin1String("light") || s == QLatin1String("book") || s == QLatin1String("Thin")
     133        || s.compare(QApplication::translate("QFontDatabase", "Book"), Qt::CaseInsensitive) == 0)
    120134        return QFont::Light;
    121135
    122136    if (s.contains(QLatin1String("bold"))
    123137        || s.contains(QApplication::translate("QFontDatabase", "Bold"), Qt::CaseInsensitive)) {
    124         if (s.contains(QLatin1String("demi"))
    125             || s.compare(QApplication::translate("QFontDatabase", "Demi"), Qt::CaseInsensitive) == 0)
     138        if (s.contains(QLatin1String("demi")) || s.contains(QLatin1String("semi"))
     139            || s.compare(QApplication::translate("QFontDatabase", "Demi"), Qt::CaseInsensitive) == 0
     140            || s.compare(QApplication::translate("QFontDatabase", "Semi"), Qt::CaseInsensitive) == 0)
    126141            return (int) QFont::DemiBold;
    127142        return (int) QFont::Bold;
    128143    }
    QFont QFontDatabase::font(const QString &family, const QString &style, 
    20242039    if (!s) // no styles found?
    20252040        return QApplication::font();
    20262041
     2042    // RJVB
     2043//    qDebug() << "QFontDatabase::font(" << family << "," << style << "," << pointSize << ")"
     2044//             << "will load font with weight" << s->key.weight << "and style" << s->styleName << s->key.style;
    20272045    QFont fnt(family, pointSize, s->key.weight);
    20282046    fnt.setStyle((QFont::Style)s->key.style);
    20292047    if (!s->styleName.isEmpty())
  • src/gui/text/qfontdatabase_mac.cpp

    diff --git src/gui/text/qfontdatabase_mac.cpp src/gui/text/qfontdatabase_mac.cpp
    index 816a7bd..be0ae55 100644
    if (QSysInfo::MacintoshVersion >= QSysInfo::MV_10_5) { 
    168168                Q_ASSERT(CFNumberIsFloatType(weight));
    169169                double d;
    170170                if(CFNumberGetValue(weight, kCFNumberDoubleType, &d)) {
    171                     //qDebug() << "BOLD" << (QString)family_name << d;
    172                     styleKey.weight = (d > 0.0) ? QFont::Bold : QFont::Normal;
     171                    if (!qgetenv("QT_DUMP_FONT_STYLE_WEIGHTS").isEmpty()) {
     172                        extern int getCTFontWeightFromDescriptor(CTFontDescriptorRef fontRef, int size);
     173                        qDebug() << "Font" << (QString)family_name << "style" << styleName << "weight=" << d
     174                                 << "NSFont weight{12pt,18pt}=" << getCTFontWeightFromDescriptor(font,12) << getCTFontWeightFromDescriptor(font,18);
     175                    }
     176                    //styleKey.weight = (d > 0.0) ? QFont::Bold : QFont::Normal;
     177                    // RJVB 20150415: do a true breakdown of the various known font weights rather than a binary
     178                    // classification as in the line above. We could also use weightFromInteger(d*500+500) ...
     179                    if (d == 0.0) {
     180                        // Normal or Regular : (0)
     181                        styleKey.weight = QFont::Normal;
     182                    }
     183                    else if (d < 0.0) {
     184                        // Thin (-0.6), Light (-0.4) or Book (-0.23 !)
     185                        styleKey.weight = QFont::Light;
     186                    }
     187                    else if (d < 0.4) {
     188                        // Demi Bold, Semi Bold or Medium (0.23 or 0.3)
     189                        styleKey.weight = QFont::DemiBold;
     190                    }
     191                    else if (d < 0.5) {
     192                        // Bold (0.4)
     193                        styleKey.weight = QFont::Bold;
     194                    }
     195                    else{
     196                        // ExtraBold or Ultra (0.5), Heavy or Black (0.62)
     197                        styleKey.weight = QFont::Black;
     198                    }
    173199                }
    174200            }
    175201            if(CFNumberRef italic = (CFNumberRef)CFDictionaryGetValue(styles, kCTFontSlantTrait)) {
    if (QSysInfo::MacintoshVersion >= QSysInfo::MV_10_5) { 
    202228            //qDebug() << "WTF?";
    203229        }
    204230    }
    205 } else 
     231} else
    206232#endif
    207233    {
    208234#ifndef QT_MAC_USE_COCOA
    static void registerFont(QFontDatabasePrivate::ApplicationFont *fnt) 
    450476                    return;
    451477
    452478                ATSFontActivateFromFileReference(&ref, kATSFontContextLocal, kATSFontFormatUnspecified, 0, kATSOptionFlagsDefault, &handle);
    453         } else 
     479        } else
    454480#endif
    455481        {
    456482#ifndef Q_WS_MAC64
  • src/gui/text/qfontengine_coretext.mm

    diff --git src/gui/text/qfontengine_coretext.mm src/gui/text/qfontengine_coretext.mm
    index 204d685..66e560e 100644
    int getTraitValue(CFDictionaryRef allTraits, CFStringRef trait) 
    451451        CFNumberRef traitNum = (CFNumberRef) CFDictionaryGetValue(allTraits, trait);
    452452        float v = 0;
    453453        CFNumberGetValue(traitNum, kCFNumberFloatType, &v);
    454         // the value we get from CFNumberRef is from -1.0 to 1.0
     454        // the value we get from CFNumberRef is from -1.0 to 1.0 :
     455        // Thin (-0.6), Light (-0.4) or Book (-0.23 !)
     456        // Normal or Regular : (0)
     457        // Demi Bold, Semi Bold or Medium (0.23 or 0.3)
     458        // Bold (0.4)
     459        // ExtraBold or Ultra (0.5), Heavy or Black (0.62)
    455460        int value = v * 500 + 500;
     461        // value:
     462        // Thin (200), Light (300) or Book (385 !)
     463        // Normal or Regular : (500)
     464        // Demi Bold, Semi Bold or Medium (615 or 650)
     465        // Bold (700)
     466        // ExtraBold or Ultra (750; will be considered "Bold" because < 800), Heavy or Black (810)
    456467        return value;
    457468    }
    458469
    459470    return 0;
    460471}
    461472
     473// RJVB
     474int getCTFontWeightFromDescriptor(CTFontDescriptorRef fontRef, int size)
     475{
     476    NSFont *font = [NSFont fontWithDescriptor:(NSFontDescriptor*)fontRef size:(CGFloat)size];
     477    return [[NSFontManager sharedFontManager] weightOfFont:font];
     478}
     479
    462480void QCoreTextFontEngine::init()
    463481{
    464482    Q_ASSERT(ctfont != NULL);
  • src/plugins/platforms/fontdatabases/coretext/qcoretextfontdatabase.mm

    diff --git src/plugins/platforms/fontdatabases/coretext/qcoretextfontdatabase.mm src/plugins/platforms/fontdatabases/coretext/qcoretextfontdatabase.mm
    index 312015f..1380b02 100644
    void QCoreTextFontDatabase::populateFontDatabase() 
    137137            if (CFNumberRef weight = (CFNumberRef)CFDictionaryGetValue(styles, kCTFontWeightTrait)) {
    138138                Q_ASSERT(CFNumberIsFloatType(weight));
    139139                double d;
     140                         // RJVB 20150415
     141                         extern QFont::Weight weightFromInteger(int weight);
    140142                if (CFNumberGetValue(weight, kCFNumberDoubleType, &d)) {
    141                     if (d > 0.0)
    142                         fontWeight = QFont::Bold;
     143                    //if (d > 0.0)
     144                    //    fontWeight = QFont::Bold;
     145                                fontWeight = weightFromInteger((int)d * 500 + 500);
    143146                }
    144147            }
    145148            if (CFNumberRef italic = (CFNumberRef)CFDictionaryGetValue(styles, kCTFontSlantTrait)) {
  • tools/qtconfig/mainwindow.cpp

    diff --git tools/qtconfig/mainwindow.cpp tools/qtconfig/mainwindow.cpp
    index 1bb6e4e..8c81099 100644
    MainWindow::MainWindow() 
    347347    ui->fontStyleCombo->addItems(styles);
    348348
    349349    QString stylestring = db.styleString(QApplication::font());
     350
     351    // RJVB 20150415
     352    QStringList lightStyles, normalStyles, demiBoldStyles, blackStyles;
     353    lightStyles << "Thin" << "Light" << "Book";
     354    normalStyles << "Normal" << "Regular";
     355    demiBoldStyles << "DemiBold" << "Demi Bold" << "SemiBold" << "Semi Bold" << "Medium";
     356    blackStyles << "Black" << "Ultra" << "Heavy" << "UltraBold";
     357
    350358    sit = styles.begin();
    351359    i = 0;
    352360    possible = -1;
    MainWindow::MainWindow() 
    355363            break;
    356364        if ((*sit).contains(stylestring))
    357365            possible = i;
     366        // RJVB 20150415: handle style synonyms
     367        if ((lightStyles.contains(stylestring, Qt::CaseInsensitive) && lightStyles.contains(*sit, Qt::CaseInsensitive))
     368                || (normalStyles.contains(stylestring, Qt::CaseInsensitive) && normalStyles.contains(*sit, Qt::CaseInsensitive))
     369                || (demiBoldStyles.contains(stylestring, Qt::CaseInsensitive) && demiBoldStyles.contains(*sit, Qt::CaseInsensitive))
     370                || (blackStyles.contains(stylestring, Qt::CaseInsensitive) && blackStyles.contains(*sit, Qt::CaseInsensitive))) {
     371            break;
     372        }
    358373
    359374        i++;
    360375        sit++;