QTextLayout: Reconsider cursor drawing on TextObject

Revert: e99a883bd382ca950192bd66cafb2a1de6394ce7
Revert: 33238ea2c63b372ee8795eaafbfc5a859a778f8d

These two commits made the drawing of the cursor incomprehensible,
but their purpose was to fix the problem of abnormal cursor drawing
when QScriptAnalysis::Object is present. Because objects require
some special handling, they can be specially aligned or floated.

Anyway, the alignment is already reflected by ascent and descent,
and when drawing, y = position.y() + (sl.y + sl.base() - base).toReal();
works fine. So roll them back now.

We just need to specially consider the case where the QScriptItem is a
QScriptAnalysis::Object, keeping the base and descent the same as the row.

Task-number: QTBUG-92468
Task-number: QTBUG-86823
Task-number: QTBUG-96288
Change-Id: I6d9a0e00fbc3823e0cc8e0e8bd061da5782d1f8a
Reviewed-by: Shawn Rutledge <shawn.rutledge@qt.io>
(cherry picked from commit de16300661bc498eb02d8d5b36ccc07ebe595ca2)
Reviewed-by: Qt Cherry-pick Bot <cherrypick_bot@qt-project.org>
This commit is contained in:
Tang Haixiang 2022-10-14 10:42:07 +08:00 committed by Qt Cherry-pick Bot
parent 9aa0e21db9
commit 0fecbc0f4b

View File

@ -1246,7 +1246,6 @@ void QTextLayout::drawCursor(QPainter *p, const QPointF &pos, int cursorPosition
QFixed base = sl.base();
QFixed descent = sl.descent;
QFixed cursorDescent = descent;
bool rightToLeft = d->isRightToLeft();
const int realCursorPosition = cursorPosition;
@ -1275,15 +1274,16 @@ void QTextLayout::drawCursor(QPainter *p, const QPointF &pos, int cursorPosition
si = &d->layoutData->items[itm];
}
}
if (si->ascent >= 0)
base = si->ascent;
if (si->descent == 0)
descent = si->descent;
else if (si->descent > 0 && si->descent < descent)
cursorDescent = si->descent;
// objects need some special treatment as they can have special alignment or be floating
if (si->analysis.flags != QScriptAnalysis::Object) {
if (si->ascent > 0)
base = si->ascent;
if (si->descent > 0)
descent = si->descent;
}
rightToLeft = si->analysis.bidiLevel % 2;
}
qreal y = position.y() + (sl.y + sl.base() + sl.descent - base - descent).toReal();
qreal y = position.y() + (sl.y + sl.base() - base).toReal();
bool toggleAntialiasing = !(p->renderHints() & QPainter::Antialiasing)
&& (p->transform().type() > QTransform::TxTranslate);
if (toggleAntialiasing)
@ -1294,7 +1294,7 @@ void QTextLayout::drawCursor(QPainter *p, const QPointF &pos, int cursorPosition
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());
p->fillRect(QRectF(x, y, qreal(width), (base + descent).toReal()), p->pen().brush());
} else {
// Ensure consistently rendered cursor width under fractional scaling
const QPen origPen = p->pen();
@ -1302,7 +1302,7 @@ void QTextLayout::drawCursor(QPainter *p, const QPointF &pos, int cursorPosition
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->drawLine(QPointF(center, y), QPointF(center, y + (base + descent).toReal()));
p->setPen(origPen);
}
p->setCompositionMode(origCompositionMode);