diff --git a/src/gui/text/qfont.cpp b/src/gui/text/qfont.cpp index 0fa32dc56d..37b8c22920 100644 --- a/src/gui/text/qfont.cpp +++ b/src/gui/text/qfont.cpp @@ -1376,6 +1376,7 @@ void QFont::setStyleStrategy(StyleStrategy s) Predefined stretch values that follow the CSS naming convention. The higher the value, the more stretched the text is. + \value AnyStretch 0 Accept any stretch matched using the other QFont properties (added in Qt 5.8) \value UltraCondensed 50 \value ExtraCondensed 62 \value Condensed 75 @@ -1402,20 +1403,25 @@ int QFont::stretch() const /*! Sets the stretch factor for the font. - The stretch factor changes the width of all characters in the font - by \a factor percent. For example, setting \a factor to 150 + The stretch factor matches a condensed or expanded version of the font or + applies a stretch transform that changes the width of all characters + in the font by \a factor percent. For example, setting \a factor to 150 results in all characters in the font being 1.5 times (ie. 150%) - wider. The default stretch factor is 100. The minimum stretch - factor is 1, and the maximum stretch factor is 4000. + wider. The minimum stretch factor is 1, and the maximum stretch factor + is 4000. The default stretch factor is \c AnyStretch, which will accept + any stretch factor and not apply any transform on the font. The stretch factor is only applied to outline fonts. The stretch factor is ignored for bitmap fonts. + \note When matching a font with a native non-default stretch factor, + requesting a stretch of 100 will stretch it back to a medium width font. + \sa stretch(), QFont::Stretch */ void QFont::setStretch(int factor) { - if (factor < 1 || factor > 4000) { + if (factor < 0 || factor > 4000) { qWarning("QFont::setStretch: Parameter '%d' out of range", factor); return; } diff --git a/src/gui/text/qfont.h b/src/gui/text/qfont.h index b295e13f61..6f0dd27fbe 100644 --- a/src/gui/text/qfont.h +++ b/src/gui/text/qfont.h @@ -113,6 +113,7 @@ public: }; enum Stretch { + AnyStretch = 0, UltraCondensed = 50, ExtraCondensed = 62, Condensed = 75, diff --git a/src/gui/text/qfont_p.h b/src/gui/text/qfont_p.h index 21823dc12f..9e5d0b4329 100644 --- a/src/gui/text/qfont_p.h +++ b/src/gui/text/qfont_p.h @@ -71,7 +71,7 @@ struct QFontDef inline QFontDef() : pointSize(-1.0), pixelSize(-1), styleStrategy(QFont::PreferDefault), styleHint(QFont::AnyStyle), - weight(50), fixedPitch(false), style(QFont::StyleNormal), stretch(100), + weight(50), fixedPitch(false), style(QFont::StyleNormal), stretch(QFont::AnyStretch), hintingPreference(QFont::PreferDefaultHinting), ignorePitch(true), fixedPitchComputed(0), reserved(0) { diff --git a/src/gui/text/qfontdatabase.cpp b/src/gui/text/qfontdatabase.cpp index 594d791e9c..cb1619e690 100644 --- a/src/gui/text/qfontdatabase.cpp +++ b/src/gui/text/qfontdatabase.cpp @@ -951,12 +951,14 @@ QFontEngine *loadSingleEngine(int script, } } - // If the font data's native stretch matches the requested stretch we need to set stretch to 100 - // to avoid the fontengine synthesizing stretch. If they didn't match exactly we need to calculate - // the new stretch factor. This only done if not matched by styleName. + // To avoid synthesized stretch we need a matching stretch to be 100 after this point. + // If stretch didn't match exactly we need to calculate the new stretch factor. + // This only done if not matched by styleName. if (style->key.stretch != 0 && request.stretch != 0 && (request.styleName.isEmpty() || request.styleName != style->styleName)) { - def.stretch = (request.stretch * 100 + 50) / style->key.stretch; + def.stretch = (request.stretch * 100 + style->key.stretch / 2) / style->key.stretch; + } else { + def.stretch = 100; } engine = pfdb->fontEngine(def, size->handle); @@ -1219,7 +1221,8 @@ static int match(int script, const QFontDef &request, QtFontStyle::Key styleKey; styleKey.style = request.style; styleKey.weight = request.weight; - styleKey.stretch = request.stretch; + // Prefer a stretch closest to 100. + styleKey.stretch = request.stretch ? request.stretch : 100; char pitch = request.ignorePitch ? '*' : request.fixedPitch ? 'm' : 'p'; @@ -2740,8 +2743,6 @@ void QFontDatabase::load(const QFontPrivate *d, int script) } if (req.pointSize < 0) req.pointSize = req.pixelSize*72.0/d->dpi; - if (req.stretch == 0) - req.stretch = 100; // respect the fallback families that might be passed through the request const QStringList fallBackFamilies = familyList(req); diff --git a/src/platformsupport/fontdatabases/mac/qfontengine_coretext.mm b/src/platformsupport/fontdatabases/mac/qfontengine_coretext.mm index a0047c0b7d..339212db25 100644 --- a/src/platformsupport/fontdatabases/mac/qfontengine_coretext.mm +++ b/src/platformsupport/fontdatabases/mac/qfontengine_coretext.mm @@ -172,7 +172,7 @@ QFontEngine::GlyphFormat QCoreTextFontEngine::defaultGlyphFormat = QFontEngine:: CGAffineTransform qt_transform_from_fontdef(const QFontDef &fontDef) { CGAffineTransform transform = CGAffineTransformIdentity; - if (fontDef.stretch != 100) + if (fontDef.stretch && fontDef.stretch != 100) transform = CGAffineTransformMakeScale(float(fontDef.stretch) / float(100), 1); return transform; } diff --git a/tests/auto/gui/text/qfontcache/tst_qfontcache.cpp b/tests/auto/gui/text/qfontcache/tst_qfontcache.cpp index 54bc802cf0..fbca313ea3 100644 --- a/tests/auto/gui/text/qfontcache/tst_qfontcache.cpp +++ b/tests/auto/gui/text/qfontcache/tst_qfontcache.cpp @@ -101,8 +101,6 @@ void tst_QFontCache::engineData() } if (req.pointSize < 0) req.pointSize = req.pixelSize*72.0/d->dpi; - if (req.stretch == 0) - req.stretch = 100; req.family = cacheKey; diff --git a/tests/auto/gui/text/qfontdatabase/tst_qfontdatabase.cpp b/tests/auto/gui/text/qfontdatabase/tst_qfontdatabase.cpp index adaf3b1f7a..f71d808390 100644 --- a/tests/auto/gui/text/qfontdatabase/tst_qfontdatabase.cpp +++ b/tests/auto/gui/text/qfontdatabase/tst_qfontdatabase.cpp @@ -64,6 +64,8 @@ private slots: void aliases(); void fallbackFonts(); + void liberationFont(); + private: const QString m_testFont; }; @@ -275,5 +277,23 @@ void tst_QFontDatabase::fallbackFonts() } } +void tst_QFontDatabase::liberationFont() +{ + QString libSans("Liberation Sans"); + QString libSansNarrow("Liberation Sans Narrow"); + + QFontDatabase db; + if (!db.hasFamily(libSans) || !db.hasFamily(libSansNarrow)) + QSKIP("Requires Liberation Sans installed"); + + QFont fontLS(libSans); + QFont fontLSN(libSansNarrow); + + QFontMetrics fmLS(fontLS); + QFontMetrics fmLSN(fontLSN); + + QVERIFY(fmLS.width(QStringLiteral("foo bar")) > fmLSN.width(QStringLiteral("foo bar"))); +} + QTEST_MAIN(tst_QFontDatabase) #include "tst_qfontdatabase.moc"