diff options
author | Eirik Aavitsland <eirik.aavitsland@qt.io> | 2022-06-20 16:33:55 +0200 |
---|---|---|
committer | Eirik Aavitsland <eirik.aavitsland@qt.io> | 2022-06-28 11:04:10 +0200 |
commit | 3709bc3699ef0632bd2af53b02d44d130b8c0e13 (patch) | |
tree | f60f133231456366614dc3e05610c870c7722f0a /src/gui/text/qtextlayout.cpp | |
parent | 40ddf2c7f34e429af4d18063ca947490cdc12ba9 (diff) |
Ensure consistent cursor width under fractional scaling
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
Pick-to: 6.4 6.3 6.2 5.15
Change-Id: I31a31f89fe7eac3037694946aa452a9f2bd6e5be
Reviewed-by: Morten Johan Sørvig <morten.sorvig@qt.io>
Diffstat (limited to 'src/gui/text/qtextlayout.cpp')
-rw-r--r-- | src/gui/text/qtextlayout.cpp | 15 |
1 files changed, 14 insertions, 1 deletions
diff --git a/src/gui/text/qtextlayout.cpp b/src/gui/text/qtextlayout.cpp index eb71b46526..d4146757c5 100644 --- a/src/gui/text/qtextlayout.cpp +++ b/src/gui/text/qtextlayout.cpp @@ -1291,7 +1291,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); |