Add support for more font weights internally

We should have more font weights in QFont::Weight to allow for
finer grained control.
For the time being, we can simply use intermediate weights to better
support the different font weights falling in between the ones
defined in QFont::Weight.
Also amend the documentation to clarify the fact that QFont supports
and can return weights falling outside the predefined values, which
is already the case (e.g. when using fontconfig).

Done-with: Gabriel de Dietrich
Change-Id: I693cdd48b8b77e7ed550cdf991227bcb819d8e7b
Reviewed-by: Allan Sandfeld Jensen <allan.jensen@theqtcompany.com>
Reviewed-by: Eskil Abrahamsen Blomfeldt <eskil.abrahamsen-blomfeldt@theqtcompany.com>
This commit is contained in:
Pierre Rossi 2014-10-21 17:11:58 +02:00 committed by Pierre Rossi
parent 0f3323ce01
commit 0c482869fb
5 changed files with 36 additions and 9 deletions

View File

@ -1014,8 +1014,8 @@ void QFont::setStyle(Style style)
}
/*!
Returns the weight of the font which is one of the enumerated
values from \l{QFont::Weight}.
Returns the weight of the font, using the same scale as the
\l{QFont::Weight} enumeration.
\sa setWeight(), Weight, QFontInfo
*/
@ -1028,8 +1028,8 @@ int QFont::weight() const
\enum QFont::Weight
Qt uses a weighting scale from 0 to 99 similar to, but not the
same as, the scales used in Windows or CSS. A weight of 0 is
ultralight, whilst 99 will be extremely black.
same as, the scales used in Windows or CSS. A weight of 0 will be
thin, whilst 99 will be extremely black.
This enum contains the predefined font weights:
@ -1041,8 +1041,8 @@ int QFont::weight() const
*/
/*!
Sets the weight the font to \a weight, which should be a value
from the \l QFont::Weight enumeration.
Sets the weight of the font to \a weight, using the scale defined by
\l QFont::Weight enumeration.
\sa weight(), QFontInfo
*/

View File

@ -85,10 +85,11 @@ static int getFontWeight(const QString &weightString)
QString s = weightString.toLower();
// Test in decreasing order of commonness
if (s == QLatin1String("medium") ||
s == QLatin1String("normal")
if (s == QLatin1String("normal")
|| s.compare(QCoreApplication::translate("QFontDatabase", "Normal"), Qt::CaseInsensitive) == 0)
return QFont::Normal;
if (s == QLatin1String("medium"))
return qt_mediumFontWeight;
if (s == QLatin1String("bold")
|| s.compare(QCoreApplication::translate("QFontDatabase", "Bold"), Qt::CaseInsensitive) == 0)
return QFont::Bold;
@ -100,6 +101,10 @@ static int getFontWeight(const QString &weightString)
return QFont::Black;
if (s == QLatin1String("light"))
return QFont::Light;
if (s == QLatin1String("thin"))
return qt_thinFontWeight;
if (s == QLatin1String("extralight"))
return qt_extralightFontWeight;
if (s.contains(QLatin1String("bold"))
|| s.contains(QCoreApplication::translate("QFontDatabase", "Bold"), Qt::CaseInsensitive)) {

View File

@ -78,6 +78,10 @@ enum HB_Compat_Error {
typedef void (*qt_destroy_func_t) (void *user_data);
typedef bool (*qt_get_font_table_func_t) (void *user_data, uint tag, uchar *buffer, uint *length);
const QFont::Weight qt_mediumFontWeight = static_cast<QFont::Weight>(57);
const QFont::Weight qt_extralightFontWeight = static_cast<QFont::Weight>(12);
const QFont::Weight qt_thinFontWeight = static_cast<QFont::Weight>(0);
class Q_GUI_EXPORT QFontEngine
{
public:

View File

@ -277,10 +277,16 @@ QStringList QBasicFontDatabase::addTTFile(const QByteArray &fontData, const QByt
if (os2->usWeightClass == 0)
;
else if (os2->usWeightClass < 150)
weight = qt_thinFontWeight;
else if (os2->usWeightClass < 250)
weight = qt_extralightFontWeight;
else if (os2->usWeightClass < 350)
weight = QFont::Light;
else if (os2->usWeightClass < 450)
weight = QFont::Normal;
else if (os2->usWeightClass < 550)
weight = qt_mediumFontWeight;
else if (os2->usWeightClass < 650)
weight = QFont::DemiBold;
else if (os2->usWeightClass < 750)
@ -290,10 +296,16 @@ QStringList QBasicFontDatabase::addTTFile(const QByteArray &fontData, const QByt
if (os2->panose[2] >= 2) {
int w = os2->panose[2];
if (w <= 3)
if (w <= 1)
weight = qt_thinFontWeight;
else if (w <= 2)
weight = qt_extralightFontWeight;
else if (w <= 3)
weight = QFont::Light;
else if (w <= 5)
weight = QFont::Normal;
else if (w <= 6)
weight = qt_mediumFontWeight;
else if (w <= 7)
weight = QFont::DemiBold;
else if (w <= 8)

View File

@ -294,10 +294,16 @@ static void getFontDescription(CTFontDescriptorRef font, FontDescription *fd)
fd->weight = QFont::Bold;
else if (normalizedWeight >= 0.3)
fd->weight = QFont::DemiBold;
else if (normalizedWeight >= 0.2)
fd->weight = qt_mediumFontWeight;
else if (normalizedWeight == 0.0)
fd->weight = QFont::Normal;
else if (normalizedWeight <= -0.4)
fd->weight = QFont::Light;
else if (normalizedWeight <= -0.6)
fd->weight = qt_extralightFontWeight;
else if (normalizedWeight <= -0.8)
fd->weight = qt_thinFontWeight;
}
}
if (CFNumberRef italic = (CFNumberRef) CFDictionaryGetValue(styles, kCTFontSlantTrait)) {