summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorEskil Abrahamsen Blomfeldt <eskil.abrahamsen-blomfeldt@qt.io>2022-12-12 11:04:54 +0100
committerQt Cherry-pick Bot <cherrypick_bot@qt-project.org>2022-12-15 13:44:04 +0000
commitd37ef31674d4de6907011e9c0cb7de34c1a4a03f (patch)
tree4b032429090bdc58cd775109b92da9026fdb3c5d
parent70b6896a820cf96fed6dd49657bd0a7df9c35df2 (diff)
Don't hide object replacement char except in rich textv6.5.0-beta1
The object replacement character (U+FFFC) is used to represent inline objects such as images in rich-text. To enable this, we have special handling of it in QTextEngine. For classes where inline images are not supported, it will just be hidden from the visual text, which is unexpected. Instead of always special-casing it, we make this dependent on whether the document layout has registered any object handlers. If they have not, then there will be no visual representation of the object, and it is better to show the glyph for it. For anything based on QTextDocument, there will always be the image handler, so U+FFFC will still have special handling there, but for non-rich labels and plain text editors the glyph will be shown instead. Note that there was also a bug in QLineEdit, where the object replacement character was always replaced by a space. This was introduced in 2007, in a patch which replaced a !ch.isPrint() with a check for "the most obvious non-printable characters" to reduce the number of characters that were not shown. However, U+FFFC is a printable character and would thus not have been filtered by the !isPrint() condition, so I think this was a mistake at the time. However, due to the special-casing of the character in Qt, it would not have had any effect until now. This also changes the QTextLayout::cursorToXForInlineObject() test to actually test proper inline objects, as this was previously using a hack which depended on the inline object code to be used even for plain QTextLayouts with no handlers for these. [ChangeLog][Text] The object replacement character (U+FFFC) is now only filtered out in rich text controls, where they represent inline objects. In other controls, its glyphs will be shown as with other text. Fixes: QTBUG-101526 Change-Id: I7fcaf2b10918feb41589e1098016efbf79a0e62d Reviewed-by: Lars Knoll <lars@knoll.priv.no> Reviewed-by: Qt CI Bot <qt_ci_bot@qt-project.org> (cherry picked from commit ca64365e3a7245f6ef0f8f6962db3c1b85421cef) Reviewed-by: Qt Cherry-pick Bot <cherrypick_bot@qt-project.org>
-rw-r--r--src/gui/text/qabstracttextdocumentlayout_p.h11
-rw-r--r--src/gui/text/qtextengine.cpp13
-rw-r--r--src/widgets/widgets/qwidgetlinecontrol.cpp3
-rw-r--r--tests/auto/gui/text/qglyphrun/tst_qglyphrun.cpp16
-rw-r--r--tests/auto/gui/text/qtextlayout/tst_qtextlayout.cpp24
-rw-r--r--tests/auto/shared/resources/test.ttfbin2128 -> 2196 bytes
6 files changed, 55 insertions, 12 deletions
diff --git a/src/gui/text/qabstracttextdocumentlayout_p.h b/src/gui/text/qabstracttextdocumentlayout_p.h
index 34eccd89af..f10c1d9bd2 100644
--- a/src/gui/text/qabstracttextdocumentlayout_p.h
+++ b/src/gui/text/qabstracttextdocumentlayout_p.h
@@ -18,6 +18,7 @@
#include <QtGui/private/qtguiglobal_p.h>
#include "private/qobject_p.h"
#include "qtextdocument_p.h"
+#include "qabstracttextdocumentlayout.h"
#include "QtCore/qhash.h"
QT_BEGIN_NAMESPACE
@@ -46,6 +47,16 @@ public:
docPrivate = QTextDocumentPrivate::get(doc);
}
+ static QAbstractTextDocumentLayoutPrivate *get(QAbstractTextDocumentLayout *layout)
+ {
+ return layout->d_func();
+ }
+
+ bool hasHandlers() const
+ {
+ return !handlers.isEmpty();
+ }
+
inline int _q_dynamicPageCountSlot() const
{ return q_func()->pageCount(); }
inline QSizeF _q_dynamicDocumentSizeSlot() const
diff --git a/src/gui/text/qtextengine.cpp b/src/gui/text/qtextengine.cpp
index c174eefa70..eded3e3f33 100644
--- a/src/gui/text/qtextengine.cpp
+++ b/src/gui/text/qtextengine.cpp
@@ -7,6 +7,7 @@
#include "qtextformat_p.h"
#include "qtextengine_p.h"
#include "qabstracttextdocumentlayout.h"
+#include "qabstracttextdocumentlayout_p.h"
#include "qtextlayout.h"
#include "qtextboundaryfinder.h"
#include <QtCore/private/qunicodetables_p.h>
@@ -1933,7 +1934,17 @@ void QTextEngine::itemize() const
while (uc < e) {
switch (*uc) {
case QChar::ObjectReplacementCharacter:
- analysis->flags = QScriptAnalysis::Object;
+ {
+ const QTextDocumentPrivate *doc_p = QTextDocumentPrivate::get(block);
+ if (doc_p != nullptr
+ && doc_p->layout() != nullptr
+ && QAbstractTextDocumentLayoutPrivate::get(doc_p->layout()) != nullptr
+ && QAbstractTextDocumentLayoutPrivate::get(doc_p->layout())->hasHandlers()) {
+ analysis->flags = QScriptAnalysis::Object;
+ } else {
+ analysis->flags = QScriptAnalysis::None;
+ }
+ }
break;
case QChar::LineSeparator:
analysis->flags = QScriptAnalysis::LineOrParagraphSeparator;
diff --git a/src/widgets/widgets/qwidgetlinecontrol.cpp b/src/widgets/widgets/qwidgetlinecontrol.cpp
index 30465b95fa..ed7bc035ec 100644
--- a/src/widgets/widgets/qwidgetlinecontrol.cpp
+++ b/src/widgets/widgets/qwidgetlinecontrol.cpp
@@ -85,8 +85,7 @@ void QWidgetLineControl::updateDisplayText(bool forceUpdate)
for (int i = 0; i < (int)str.size(); ++i) {
if ((uc[i].unicode() < 0x20 && uc[i].unicode() != 0x09)
|| uc[i] == QChar::LineSeparator
- || uc[i] == QChar::ParagraphSeparator
- || uc[i] == QChar::ObjectReplacementCharacter)
+ || uc[i] == QChar::ParagraphSeparator)
uc[i] = QChar(0x0020);
}
diff --git a/tests/auto/gui/text/qglyphrun/tst_qglyphrun.cpp b/tests/auto/gui/text/qglyphrun/tst_qglyphrun.cpp
index efd590a730..9660d829b8 100644
--- a/tests/auto/gui/text/qglyphrun/tst_qglyphrun.cpp
+++ b/tests/auto/gui/text/qglyphrun/tst_qglyphrun.cpp
@@ -42,6 +42,7 @@ private slots:
void stringIndexes();
void retrievalFlags_data();
void retrievalFlags();
+ void objectReplacementCharacter();
private:
int m_testFontId;
@@ -954,6 +955,21 @@ void tst_QGlyphRun::retrievalFlags()
QCOMPARE(firstGlyphRun.positions().isEmpty(), !expectedGlyphPositions);
}
+void tst_QGlyphRun::objectReplacementCharacter()
+{
+ QTextLayout layout;
+ layout.setFont(m_testFont);
+ layout.setText(QStringLiteral("\uFFFC"));
+ layout.beginLayout();
+ layout.createLine();
+ layout.endLayout();
+
+ QList<QGlyphRun> glyphRuns = layout.glyphRuns();
+ QCOMPARE(glyphRuns.size(), 1);
+ QCOMPARE(glyphRuns.first().glyphIndexes().size(), 1);
+ QCOMPARE(glyphRuns.first().glyphIndexes().first(), 5);
+}
+
#endif // QT_NO_RAWFONT
QTEST_MAIN(tst_QGlyphRun)
diff --git a/tests/auto/gui/text/qtextlayout/tst_qtextlayout.cpp b/tests/auto/gui/text/qtextlayout/tst_qtextlayout.cpp
index 3958e2a01c..9e6daf81b1 100644
--- a/tests/auto/gui/text/qtextlayout/tst_qtextlayout.cpp
+++ b/tests/auto/gui/text/qtextlayout/tst_qtextlayout.cpp
@@ -512,18 +512,24 @@ void tst_QTextLayout::noWrap()
void tst_QTextLayout::cursorToXForInlineObjects()
{
- QChar ch(QChar::ObjectReplacementCharacter);
- QString text(ch);
- QTextLayout layout(text, testFont);
- layout.beginLayout();
+ QString text = QStringLiteral("<html><body><img src=\"\" width=\"32\" height=\"32\" /></body></html>");
- QTextEngine *engine = layout.engine();
- const int item = engine->findItem(0);
- engine->layoutData->items[item].width = 32;
+ QTextDocument document;
+ document.setHtml(text);
+ QCOMPARE(document.blockCount(), 1);
- QTextLine line = layout.createLine();
- line.setLineWidth(0x10000);
+ // Trigger layout
+ {
+ QImage img(1, 1, QImage::Format_ARGB32_Premultiplied);
+ QPainter p(&img);
+ document.drawContents(&p);
+ }
+
+ QTextLayout *layout = document.firstBlock().layout();
+ QVERIFY(layout != nullptr);
+ QCOMPARE(layout->lineCount(), 1);
+ QTextLine line = layout->lineAt(0);
QCOMPARE(line.cursorToX(0), qreal(0));
QCOMPARE(line.cursorToX(1), qreal(32));
}
diff --git a/tests/auto/shared/resources/test.ttf b/tests/auto/shared/resources/test.ttf
index b2bb6cd036..8b56c046d4 100644
--- a/tests/auto/shared/resources/test.ttf
+++ b/tests/auto/shared/resources/test.ttf
Binary files differ