Ticket #47466: patch-improve-fontweight-support.diff
File patch-improve-fontweight-support.diff, 17.2 KB (added by RJVB (René Bertin), 10 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() 563 563 Q_Q(QFontDialog); 564 564 QStringList styles = fdb.styles(familyList->currentText()); 565 565 styleList->model()->setStringList(styles); 566 // RJVB 567 // qDebug() << "QFontDialogPrivate::updateStyles() familyList=" 568 // << familyList << "style=" << style 569 // << "styles=" << styles << "styleList=" << styleList; 566 570 567 571 if (styles.isEmpty()) { 568 572 styleEdit->clear(); … … void QFontDialogPrivate::updateStyles() 572 576 bool found = false; 573 577 bool first = true; 574 578 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"; 575 585 576 586 redo: 577 587 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) { 579 590 styleList->setCurrentItem(i); 580 591 found = true; 581 592 break; 582 593 } 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 } 583 603 } 584 604 if (!found && first) { 585 605 if (cstyle.contains(QLatin1String("Italic"))) { … … void QFontDialog::setCurrentFont(const QFont &font) 815 835 QFontInfo fi(font); 816 836 d->size = fi.pointSize(); 817 837 } 838 // RJVB 839 // qDebug() << "QFontDialog::setCurrentFont" << font << "style=" << d->style << "size=" << d->size; 818 840 d->strikeout->setChecked(font.strikeOut()); 819 841 d->underline->setChecked(font.underline()); 820 842 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) 512 512 #if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_5 513 513 if (qstrcmp(fe->name(), "CoreText") == 0) { 514 514 nsFont = reinterpret_cast<const NSFont *>(static_cast<QCoreTextFontEngineMulti *>(fe)->ctfont); 515 // RJVB 516 // qDebug() << "setFont" << fe << "gives:"; 517 // NSLog(@"setFont(%@)", nsFont); 515 518 } else 516 519 #endif 517 520 { … … void QFontDialogPrivate::setFont(void *delegate, const QFont &font) 520 523 if (font.style() == QFont::StyleItalic) { 521 524 mask |= NSItalicFontMask; 522 525 } 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; 526 549 } 527 550 528 551 NSFontManager *mgr = [NSFontManager sharedFontManager]; 529 552 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 } 534 574 } 535 575 536 576 [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) 71 71 CFNumberRef num = static_cast<CFNumberRef>(CFDictionaryGetValue(dict, kCTFontWeightTrait)); 72 72 float fW; 73 73 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); 75 78 num = static_cast<CFNumberRef>(CFDictionaryGetValue(dict, kCTFontSlantTrait)); 76 79 CFNumberGetValue(num, kCFNumberFloatType, &fW); 77 80 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) 103 103 QString s = weightString.toLower(); 104 104 105 105 // 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") || 107 109 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) 109 112 return QFont::Normal; 110 113 if (s == QLatin1String("bold") 111 114 || s.compare(QApplication::translate("QFontDatabase", "Bold"), Qt::CaseInsensitive) == 0) 112 115 return QFont::Bold; 116 // RJVB 20150415: add SemiBold and Medium 113 117 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) 115 123 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) 118 130 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) 120 134 return QFont::Light; 121 135 122 136 if (s.contains(QLatin1String("bold")) 123 137 || 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) 126 141 return (int) QFont::DemiBold; 127 142 return (int) QFont::Bold; 128 143 } … … QFont QFontDatabase::font(const QString &family, const QString &style, 2024 2039 if (!s) // no styles found? 2025 2040 return QApplication::font(); 2026 2041 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; 2027 2045 QFont fnt(family, pointSize, s->key.weight); 2028 2046 fnt.setStyle((QFont::Style)s->key.style); 2029 2047 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) { 168 168 Q_ASSERT(CFNumberIsFloatType(weight)); 169 169 double d; 170 170 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 } 173 199 } 174 200 } 175 201 if(CFNumberRef italic = (CFNumberRef)CFDictionaryGetValue(styles, kCTFontSlantTrait)) { … … if (QSysInfo::MacintoshVersion >= QSysInfo::MV_10_5) { 202 228 //qDebug() << "WTF?"; 203 229 } 204 230 } 205 } else 231 } else 206 232 #endif 207 233 { 208 234 #ifndef QT_MAC_USE_COCOA … … static void registerFont(QFontDatabasePrivate::ApplicationFont *fnt) 450 476 return; 451 477 452 478 ATSFontActivateFromFileReference(&ref, kATSFontContextLocal, kATSFontFormatUnspecified, 0, kATSOptionFlagsDefault, &handle); 453 } else 479 } else 454 480 #endif 455 481 { 456 482 #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) 451 451 CFNumberRef traitNum = (CFNumberRef) CFDictionaryGetValue(allTraits, trait); 452 452 float v = 0; 453 453 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) 455 460 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) 456 467 return value; 457 468 } 458 469 459 470 return 0; 460 471 } 461 472 473 // RJVB 474 int getCTFontWeightFromDescriptor(CTFontDescriptorRef fontRef, int size) 475 { 476 NSFont *font = [NSFont fontWithDescriptor:(NSFontDescriptor*)fontRef size:(CGFloat)size]; 477 return [[NSFontManager sharedFontManager] weightOfFont:font]; 478 } 479 462 480 void QCoreTextFontEngine::init() 463 481 { 464 482 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() 137 137 if (CFNumberRef weight = (CFNumberRef)CFDictionaryGetValue(styles, kCTFontWeightTrait)) { 138 138 Q_ASSERT(CFNumberIsFloatType(weight)); 139 139 double d; 140 // RJVB 20150415 141 extern QFont::Weight weightFromInteger(int weight); 140 142 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); 143 146 } 144 147 } 145 148 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() 347 347 ui->fontStyleCombo->addItems(styles); 348 348 349 349 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 350 358 sit = styles.begin(); 351 359 i = 0; 352 360 possible = -1; … … MainWindow::MainWindow() 355 363 break; 356 364 if ((*sit).contains(stylestring)) 357 365 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 } 358 373 359 374 i++; 360 375 sit++;