Skip to content

Commit

Permalink
Fix DirectWrite subpixel antialiasing on BGR screens
Browse files Browse the repository at this point in the history
On monitors where the subpixel arrangement is blue, green, red,
our DirectWrite rendering would give the wrong subppixel
antialiasing, causing color fringes on text.

Like we do with Freetype, we determine subpixel arrangement of the
primary screen and use this as the default.

Pick-to: 6.8 6.9
Change-Id: I9ce7025449106a2376bd0ed02ce07b59c79438bd
Reviewed-by: Eirik Aavitsland <[email protected]>
  • Loading branch information
eskilblomfeldt committed Dec 16, 2024
1 parent a90d9f4 commit d5cef74
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 3 deletions.
19 changes: 16 additions & 3 deletions src/gui/text/windows/qwindowsfontenginedirectwrite.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,12 @@ QWindowsFontEngineDirectWrite::QWindowsFontEngineDirectWrite(IDWriteFontFace *di
m_fontEngineData->directWriteFactory->AddRef();
m_directWriteFontFace->AddRef();

IDWriteRenderingParams *renderingParams = nullptr;
if (SUCCEEDED(m_fontEngineData->directWriteFactory->CreateRenderingParams(&renderingParams))) {
m_pixelGeometry = renderingParams->GetPixelGeometry();
renderingParams->Release();
}

fontDef.pixelSize = pixelSize;
collectMetrics();
cache_cost = m_xHeight.toInt() * m_xHeight.toInt() * 2000;
Expand Down Expand Up @@ -1029,6 +1035,9 @@ void QWindowsFontEngineDirectWrite::renderGlyphRun(QImage *destination,
float blueAlpha = a * *src++ / 255.0;
float averageAlpha = (redAlpha + greenAlpha + blueAlpha) / 3.0;

if (m_pixelGeometry == DWRITE_PIXEL_GEOMETRY_BGR)
qSwap(redAlpha, blueAlpha);

QRgb currentRgb = dest[x];
dest[x] = qRgba(qRound(qRed(currentRgb) * (1.0 - averageAlpha) + averageAlpha * r),
qRound(qGreen(currentRgb) * (1.0 - averageAlpha) + averageAlpha * g),
Expand All @@ -1052,10 +1061,14 @@ void QWindowsFontEngineDirectWrite::renderGlyphRun(QImage *destination,
BYTE *src = alphaValues + width * 3 * y;

for (int x = 0; x < width; ++x) {
dest[x] = *(src + 0) << 16
| *(src + 1) << 8
| *(src + 2);
BYTE redAlpha = *(src + 0);
BYTE greenAlpha = *(src + 1);
BYTE blueAlpha = *(src + 2);

if (m_pixelGeometry == DWRITE_PIXEL_GEOMETRY_BGR)
qSwap(redAlpha, blueAlpha);

dest[x] = qRgb(redAlpha, greenAlpha, blueAlpha);
src += 3;
}
}
Expand Down
1 change: 1 addition & 0 deletions src/gui/text/windows/qwindowsfontenginedirectwrite_p.h
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ class Q_GUI_EXPORT QWindowsFontEngineDirectWrite : public QFontEngine
FaceId m_faceId;
QString m_uniqueFamilyName;
QList<QFontVariableAxis> m_variableAxes;
DWRITE_PIXEL_GEOMETRY m_pixelGeometry = DWRITE_PIXEL_GEOMETRY_RGB;
};

QT_END_NAMESPACE
Expand Down

0 comments on commit d5cef74

Please sign in to comment.