From e11939afee9b79127247d3b5ba0ce57b306a7dd6 Mon Sep 17 00:00:00 2001 From: Dominik Holland Date: Fri, 12 Jun 2020 13:50:18 +0200 Subject: Fix the vertical alignment of images in a text document Try to align with the HTML standard as much as possible. AlignBottom is now really the bottom of the line (not the text). AlignMiddle is between AlignTop and AlignBottom. Change-Id: Ia067b96209c2ab0becbff99cf15bb8ab193ee2c6 Fixes: QTBUG-84981 Pick-to: 5.15 Reviewed-by: Lars Knoll --- src/gui/text/qtextlayout.cpp | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/src/gui/text/qtextlayout.cpp b/src/gui/text/qtextlayout.cpp index 72d409f638..53523f3abd 100644 --- a/src/gui/text/qtextlayout.cpp +++ b/src/gui/text/qtextlayout.cpp @@ -2097,6 +2097,10 @@ found: current.ascent = line.ascent; current.descent = height - line.ascent; break; + case QTextCharFormat::AlignMiddle: + current.ascent = (line.ascent + line.descent) / 2 - line.descent + height / 2; + current.descent = height - line.ascent; + break; case QTextCharFormat::AlignBottom: current.descent = line.descent; current.ascent = height - line.descent; @@ -2554,6 +2558,7 @@ void QTextLine::draw(QPainter *p, const QPointF &pos, const QTextLayout::FormatR QFont f = eng->font(si); QTextCharFormat format; + if (eng->hasFormats() || selection) { format = eng->format(&si); if (suppressColors) { @@ -2584,10 +2589,18 @@ void QTextLine::draw(QPainter *p, const QPointF &pos, const QTextLayout::FormatR p->save(); if (si.analysis.flags == QScriptAnalysis::Object && QTextDocumentPrivate::get(eng->block)) { QFixed itemY = y - si.ascent; - if (format.verticalAlignment() == QTextCharFormat::AlignTop) { + switch (format.verticalAlignment()) { + case QTextCharFormat::AlignTop: itemY = y - lineBase; - } else if (format.verticalAlignment() == QTextCharFormat::AlignBottom) { - itemY = y + line.descent - si.ascent - si.descent; + break; + case QTextCharFormat::AlignMiddle: + itemY = y - lineBase + (line.height() - si.height()) / 2; + break; + case QTextCharFormat::AlignBottom: + itemY = y - lineBase + line.height() - si.height(); + break; + default: + break; } QRectF itemRect(iterator.x.toReal(), itemY.toReal(), iterator.itemWidth.toReal(), si.height().toReal()); -- cgit v1.2.3