Store styleName in font database

So that queries like QFontDatabase::styles() can return exactly
the same styles as the system does. Then application can use
QFont::setStyleName() to select those styles later. With a lot
of fonts not providing correct numeric weight/width values and
even if they did, values are usually not directly mapped to
QFont enums, styleName is probably the only reliable way to
select any possible font in the system.

Reviewed-by: QTBUG-13518
Change-Id: Id8a9469b804f1b5bb81d8c7378e7e8778f9a4fff
Reviewed-on: http://codereview.qt.nokia.com/739
Reviewed-by: Qt Sanity Bot <qt_sanity_bot@ovi.com>
Reviewed-by: Eskil Abrahamsen Blomfeldt <eskil.abrahamsen-blomfeldt@nokia.com>
This commit is contained in:
Jiang Jiang 2011-06-09 15:58:09 +02:00 committed by Qt by Nokia
parent a06c8405d0
commit 1fda23c0c6
3 changed files with 38 additions and 12 deletions

View File

@ -218,16 +218,17 @@ struct QtFontStyle
Key(const QString &styleString);
Key() : style(QFont::StyleNormal),
weight(QFont::Normal), stretch(0) { }
Key(const Key &o) : style(o.style),
Key(const Key &o) : styleName(o.styleName), style(o.style),
weight(o.weight), stretch(o.stretch) { }
QString styleName;
uint style : 2;
signed int weight : 8;
signed int stretch : 12;
bool operator==(const Key & other) {
return (style == other.style &&
return styleName == other.styleName && style == other.style &&
weight == other.weight &&
(stretch == 0 || other.stretch == 0 || stretch == other.stretch));
(stretch == 0 || other.stretch == 0 || stretch == other.stretch);
}
bool operator!=(const Key &other) {
return !operator==(other);
@ -292,7 +293,7 @@ struct QtFontStyle
};
QtFontStyle::Key::Key(const QString &styleString)
: style(QFont::StyleNormal), weight(QFont::Normal), stretch(0)
: styleName(styleString), style(QFont::StyleNormal), weight(QFont::Normal), stretch(0)
{
weight = getFontWeight(styleString);
@ -1139,6 +1140,12 @@ static QtFontStyle *bestStyle(QtFontFoundry *foundry, const QtFontStyle::Key &st
for ( int i = 0; i < foundry->count; i++ ) {
QtFontStyle *style = foundry->styles[i];
if (!styleKey.styleName.isEmpty() && styleKey.styleName == style->key.styleName) {
dist = 0;
best = i;
break;
}
int d = qAbs( styleKey.weight - style->key.weight );
if ( styleKey.stretch != 0 && style->key.stretch != 0 ) {
@ -1532,7 +1539,8 @@ static QString styleStringHelper(int weight, QFont::Style style)
*/
QString QFontDatabase::styleString(const QFont &font)
{
return styleStringHelper(font.weight(), font.style());
return font.styleName().isEmpty() ? styleStringHelper(font.weight(), font.style())
: font.styleName();
}
/*!
@ -1542,7 +1550,8 @@ QString QFontDatabase::styleString(const QFont &font)
*/
QString QFontDatabase::styleString(const QFontInfo &fontInfo)
{
return styleStringHelper(fontInfo.weight(), fontInfo.style());
return fontInfo.styleName().isEmpty() ? styleStringHelper(fontInfo.weight(), fontInfo.style())
: fontInfo.styleName();
}
@ -1793,8 +1802,12 @@ QStringList QFontDatabase::styles(const QString &family) const
}
}
for (int i = 0; i < allStyles.count; i++)
l.append(styleStringHelper(allStyles.styles[i]->key.weight, (QFont::Style)allStyles.styles[i]->key.style));
for (int i = 0; i < allStyles.count; i++) {
l.append(allStyles.styles[i]->key.styleName.isEmpty() ?
styleStringHelper(allStyles.styles[i]->key.weight,
(QFont::Style)allStyles.styles[i]->key.style) :
allStyles.styles[i]->key.styleName);
}
return l;
}
@ -2017,9 +2030,16 @@ QFont QFontDatabase::font(const QString &family, const QString &style,
if (!s) // no styles found?
return QApplication::font();
QFont fnt(family, pointSize, s->key.weight);
fnt.setStyle((QFont::Style)s->key.style);
return fnt;
if (s->key.styleName.isEmpty()) {
QFont fnt(family, pointSize, s->key.weight);
fnt.setStyle((QFont::Style)s->key.style);
return fnt;
} else {
// found a perfect match
QFont fnt(family, pointSize);
fnt.setStyleName(s->key.styleName);
return fnt;
}
}

View File

@ -106,12 +106,14 @@ if (QSysInfo::MacintoshVersion >= QSysInfo::MV_10_5) {
CTFontDescriptorRef font = (CTFontDescriptorRef)CFArrayGetValueAtIndex(fonts, i);
QCFString family_name = (CFStringRef)CTFontDescriptorCopyAttribute(font, kCTFontFamilyNameAttribute);
QCFString style_name = (CFStringRef)CTFontDescriptorCopyAttribute(font, kCTFontStyleNameAttribute);
QtFontFamily *family = db->family(family_name, true);
for(int ws = 1; ws < QFontDatabase::WritingSystemsCount; ++ws)
family->writingSystems[ws] = QtFontFamily::Supported;
QtFontFoundry *foundry = family->foundry(foundry_name, true);
QtFontStyle::Key styleKey;
styleKey.styleName = style_name;
if(QCFType<CFDictionaryRef> styles = (CFDictionaryRef)CTFontDescriptorCopyAttribute(font, kCTFontTraitsAttribute)) {
if(CFNumberRef weight = (CFNumberRef)CFDictionaryGetValue(styles, kCTFontWeightTrait)) {
Q_ASSERT(CFNumberIsFloatType(weight));

View File

@ -1034,13 +1034,14 @@ static void loadFontConfig()
FcChar8 *file_value;
int index_value;
FcChar8 *foundry_value;
FcChar8 *style_value;
FcBool scalable;
{
FcObjectSet *os = FcObjectSetCreate();
FcPattern *pattern = FcPatternCreate();
const char *properties [] = {
FC_FAMILY, FC_WEIGHT, FC_SLANT,
FC_FAMILY, FC_STYLE, FC_WEIGHT, FC_SLANT,
FC_SPACING, FC_FILE, FC_INDEX,
FC_LANG, FC_CHARSET, FC_FOUNDRY, FC_SCALABLE, FC_PIXEL_SIZE, FC_WEIGHT,
FC_WIDTH,
@ -1085,6 +1086,8 @@ static void loadFontConfig()
scalable = FcTrue;
if (FcPatternGetString(fonts->fonts[i], FC_FOUNDRY, 0, &foundry_value) != FcResultMatch)
foundry_value = 0;
if (FcPatternGetString(fonts->fonts[i], FC_STYLE, 0, &style_value) != FcResultMatch)
style_value = 0;
QtFontFamily *family = db->family(familyName, true);
FcLangSet *langset = 0;
@ -1142,6 +1145,7 @@ static void loadFontConfig()
family->fontFileIndex = index_value;
QtFontStyle::Key styleKey;
styleKey.styleName = style_value ? QString::fromUtf8((const char *) style_value) : QString();
styleKey.style = (slant_value == FC_SLANT_ITALIC)
? QFont::StyleItalic
: ((slant_value == FC_SLANT_OBLIQUE)