diff options
author | Eskil Abrahamsen Blomfeldt <[email protected]> | 2024-12-09 13:27:10 +0100 |
---|---|---|
committer | Eskil Abrahamsen Blomfeldt <[email protected]> | 2025-02-07 18:29:27 +0100 |
commit | 407a98d94fb05780f30e77463fb2bd535041a044 (patch) | |
tree | 8edd438f69c5f437286745262b0d3f56a29c0ab2 /src | |
parent | eb03784510c037a377c576b298b9b2fccd16759f (diff) |
Improve hinted rendering quality on Windows
When rendering hinted text on Windows with DirectWrite, the goal was
to keep the rendering and metrics as close to GDI as possible,
minimizing the impact of changing the default font backend.
Therefore, the DWRITE_RENDERING_MODE_GDI_CLASSIC was always preferred
when hinting was on.
However, DWRITE_RENDERING_MODE_GDI_CLASSIC only applies antialiasing
in the horizontal direction, but GDI applies vertical antialiasing
as well. The result is that text will look more aliased with
DirectWrite than it did with GDI, which arguably looks ugly
especially at large sizes and can be perceived as a regression.
Microsoft documentation recommends using symmetric antialiasing
when the pixel size exceeds 16, so this patch enables
DWRITE_RENDERING_MODE_NATURAL_SYMMETRIC for fonts larger than
16px regardless of whether hinting is on.
It's worth noting that for fonts with heavy hinting, such as
Times New Roman, the rendering using
DWRITE_RENDERING_MODE_NATURAL_SYMMETRIC is different from
GDI, although more similar to Freetype. However, the impact
of not having vertical antialiasing is worse and as native apps
are moving towards using DirectWrite, the GDI rendering is not
going to be the definition of a "native" look anymore.
A second thing to note in this patch is that we always pass in false
for the useGdiNatural argument in GetGdiCompatibleGlyphMetrics().
According to the documentation, we should be passing in true for text
rendered with CLEARTYPE_NATURAL_QUALITY. However, doing this causes
wider kerning in certain cases. Since the tighter kerning matches
the layouts we get in native apps, as well as when using Freetype,
I've chosen to pass false for now, to be consistent.
This change also adds a manual test which can be used to switch
between DirectWrite, GDI and Freetype rendering on Windows, so
that it's easy to compare.
[ChangeLog][Windows] Improved hinted text rendering at font sizes
larger than 16px.
Pick-to: 6.8 6.9
Fixes: QTBUG-131946
Change-Id: Iebbe5c7affe7df6266ade6b161c31bde3d2caa84
Reviewed-by: Eirik Aavitsland <[email protected]>
Reviewed-by: Oliver Wolff <[email protected]>
Diffstat (limited to 'src')
-rw-r--r-- | src/gui/text/windows/qwindowsfontenginedirectwrite.cpp | 51 |
1 files changed, 32 insertions, 19 deletions
diff --git a/src/gui/text/windows/qwindowsfontenginedirectwrite.cpp b/src/gui/text/windows/qwindowsfontenginedirectwrite.cpp index 2cf6aa92dd9..791e17f1d69 100644 --- a/src/gui/text/windows/qwindowsfontenginedirectwrite.cpp +++ b/src/gui/text/windows/qwindowsfontenginedirectwrite.cpp @@ -163,27 +163,39 @@ static DWRITE_MEASURING_MODE renderModeToMeasureMode(DWRITE_RENDERING_MODE rende } } +static QFont::HintingPreference determineHinting(const QFontDef &fontDef) +{ + QFont::HintingPreference hintingPreference = QFont::HintingPreference(fontDef.hintingPreference); + if (hintingPreference == QFont::PreferDefaultHinting) { + if (!qFuzzyCompare(qApp->devicePixelRatio(), 1.0)) { + // Microsoft documentation recommends using asymmetric rendering for small fonts + // at pixel size 16 and less, and symmetric for larger fonts. + hintingPreference = fontDef.pixelSize > 16.0 + ? QFont::PreferNoHinting + : QFont::PreferVerticalHinting; + } else { + hintingPreference = QFont::PreferFullHinting; + } + } + + return hintingPreference; +} + DWRITE_RENDERING_MODE QWindowsFontEngineDirectWrite::hintingPreferenceToRenderingMode(const QFontDef &fontDef) const { if ((fontDef.styleStrategy & QFont::NoAntialias) && glyphFormat != QFontEngine::Format_ARGB) return DWRITE_RENDERING_MODE_ALIASED; - QFont::HintingPreference hintingPreference = QFont::HintingPreference(fontDef.hintingPreference); - if (!qFuzzyCompare(qApp->devicePixelRatio(), 1.0) && hintingPreference == QFont::PreferDefaultHinting) { - // Microsoft documentation recommends using asymmetric rendering for small fonts - // at pixel size 16 and less, and symmetric for larger fonts. - hintingPreference = fontDef.pixelSize > 16.0 - ? QFont::PreferNoHinting - : QFont::PreferVerticalHinting; - } - + QFont::HintingPreference hintingPreference = determineHinting(fontDef); switch (hintingPreference) { case QFont::PreferNoHinting: return DWRITE_RENDERING_MODE_CLEARTYPE_NATURAL_SYMMETRIC; case QFont::PreferVerticalHinting: return DWRITE_RENDERING_MODE_CLEARTYPE_NATURAL; default: - return DWRITE_RENDERING_MODE_CLEARTYPE_GDI_CLASSIC; + return fontDef.pixelSize > 16.0 + ? DWRITE_RENDERING_MODE_NATURAL_SYMMETRIC + : DWRITE_RENDERING_MODE_GDI_CLASSIC; } } @@ -558,15 +570,17 @@ void QWindowsFontEngineDirectWrite::recalcAdvances(QGlyphLayout *glyphs, QFontEn QVarLengthArray<DWRITE_GLYPH_METRICS> glyphMetrics(glyphIndices.size()); HRESULT hr; - DWRITE_RENDERING_MODE renderMode = hintingPreferenceToRenderingMode(fontDef); + QFont::HintingPreference hint = determineHinting(fontDef); bool needsDesignMetrics = shaperFlags & QFontEngine::DesignMetrics; - if (!needsDesignMetrics && (renderMode == DWRITE_RENDERING_MODE_GDI_CLASSIC - || renderMode == DWRITE_RENDERING_MODE_GDI_NATURAL - || renderMode == DWRITE_RENDERING_MODE_ALIASED)) { + if (!needsDesignMetrics && hint == QFont::PreferFullHinting) { + const DWRITE_RENDERING_MODE renderMode = hintingPreferenceToRenderingMode(fontDef); + const bool needsNaturalMetrics = renderMode == DWRITE_RENDERING_MODE_NATURAL + || renderMode == DWRITE_RENDERING_MODE_NATURAL_SYMMETRIC; + hr = m_directWriteFontFace->GetGdiCompatibleGlyphMetrics(float(fontDef.pixelSize), 1.0f, NULL, - renderMode == DWRITE_RENDERING_MODE_GDI_NATURAL, + needsNaturalMetrics, glyphIndices.data(), glyphIndices.size(), glyphMetrics.data()); @@ -763,11 +777,10 @@ QImage QWindowsFontEngineDirectWrite::alphaMapForGlyph(glyph_t glyph, bool QWindowsFontEngineDirectWrite::supportsHorizontalSubPixelPositions() const { - DWRITE_RENDERING_MODE renderMode = hintingPreferenceToRenderingMode(fontDef); + QFont::HintingPreference hinting = determineHinting(fontDef); return (!isColorFont() - && renderMode != DWRITE_RENDERING_MODE_GDI_CLASSIC - && renderMode != DWRITE_RENDERING_MODE_GDI_NATURAL - && renderMode != DWRITE_RENDERING_MODE_ALIASED); + && hinting != QFont::PreferFullHinting + && !(fontDef.styleStrategy & QFont::NoAntialias)); } QFontEngine::Properties QWindowsFontEngineDirectWrite::properties() const |