From 2c0bfd4372472662f968d3285e71a7fdb1e43bc8 Mon Sep 17 00:00:00 2001 From: Eirik Aavitsland Date: Mon, 20 Jun 2022 16:33:55 +0200 Subject: [PATCH] Ensure consistent cursor width under fractional scaling MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Under fractional scaling, an N units wide rectangle can in general cover either M or M+1 pixels, depending on placement. For a tall thin recangle like the cursor, this difference becomes very visible as the cursor moves from position to position. Avoid by instead painting the cursor as a cosmetic line in such cases, since that keeps its width independently of the current transformation. Fixes: QTBUG-95319 Change-Id: I31a31f89fe7eac3037694946aa452a9f2bd6e5be Reviewed-by: Morten Johan Sørvig (cherry picked from commit 3709bc3699ef0632bd2af53b02d44d130b8c0e13) Reviewed-by: Qt Cherry-pick Bot --- src/gui/text/qtextlayout.cpp | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/src/gui/text/qtextlayout.cpp b/src/gui/text/qtextlayout.cpp index 2c22bb06a5b..10dd5f2a154 100644 --- a/src/gui/text/qtextlayout.cpp +++ b/src/gui/text/qtextlayout.cpp @@ -1327,7 +1327,20 @@ void QTextLayout::drawCursor(QPainter *p, const QPointF &pos, int cursorPosition QPainter::CompositionMode origCompositionMode = p->compositionMode(); if (p->paintEngine()->hasFeature(QPaintEngine::RasterOpModes)) p->setCompositionMode(QPainter::RasterOp_NotDestination); - p->fillRect(QRectF(x, y, qreal(width), (base + cursorDescent).toReal()), p->pen().brush()); + const QTransform &deviceTransform = p->deviceTransform(); + const qreal xScale = deviceTransform.m11(); + if (deviceTransform.type() != QTransform::TxScale || std::trunc(xScale) == xScale) { + p->fillRect(QRectF(x, y, qreal(width), (base + cursorDescent).toReal()), p->pen().brush()); + } else { + // Ensure consistently rendered cursor width under fractional scaling + const QPen origPen = p->pen(); + QPen pen(origPen.brush(), qRound(width * xScale), Qt::SolidLine, Qt::FlatCap); + pen.setCosmetic(true); + const qreal center = x + qreal(width) / 2; + p->setPen(pen); + p->drawLine(QPointF(center, y), QPointF(center, y + (base + cursorDescent).toReal())); + p->setPen(origPen); + } p->setCompositionMode(origCompositionMode); if (toggleAntialiasing) p->setRenderHint(QPainter::Antialiasing, false);