diff options
author | Eskil Abrahamsen Blomfeldt <[email protected]> | 2024-03-15 12:25:41 +0100 |
---|---|---|
committer | Eskil Abrahamsen Blomfeldt <[email protected]> | 2024-03-18 20:28:36 +0100 |
commit | e205edfff611922ddf04d8de71ed9cb92704eafc (patch) | |
tree | 791c3eea2b026124d031798f147b1142e6eb5745 | |
parent | 58796ac177cde3f558aec92c00e7ee8e174ba228 (diff) |
Implement support for stroke color and width in CSS parser
CSS does not have text outline properties, instead different
browsers have custom properties for this. That currently means
that you can have a QTextDocument where you applied a stroke to
text and textEdit.setHtml(textEdit.toHtml()) will remove it.
Since a primary goal of the HTML support in QTextDocument is that
it can be used to save and faithfully restore its contents, we
implement qt specific properties for stroke.
Task-number: QTBUG-123357
Change-Id: Id9cf63abfabe2109ffb6fd74f9cb013304763ccb
Reviewed-by: Eirik Aavitsland <[email protected]>
-rw-r--r-- | src/gui/text/qcssparser.cpp | 2 | ||||
-rw-r--r-- | src/gui/text/qcssparser_p.h | 2 | ||||
-rw-r--r-- | src/gui/text/qtextdocument.cpp | 12 | ||||
-rw-r--r-- | src/gui/text/qtexthtmlparser.cpp | 18 | ||||
-rw-r--r-- | tests/auto/gui/text/qtextdocument/tst_qtextdocument.cpp | 25 |
5 files changed, 59 insertions, 0 deletions
diff --git a/src/gui/text/qcssparser.cpp b/src/gui/text/qcssparser.cpp index 2082263d7c0..5861003de6c 100644 --- a/src/gui/text/qcssparser.cpp +++ b/src/gui/text/qcssparser.cpp @@ -44,6 +44,8 @@ static const QCssKnownValue properties[NumProperties - 1] = { { "-qt-list-number-prefix", QtListNumberPrefix }, { "-qt-list-number-suffix", QtListNumberSuffix }, { "-qt-paragraph-type", QtParagraphType }, + { "-qt-stroke-color", QtStrokeColor }, + { "-qt-stroke-width", QtStrokeWidth }, { "-qt-style-features", QtStyleFeatures }, { "-qt-table-type", QtTableType }, { "-qt-user-state", QtUserState }, diff --git a/src/gui/text/qcssparser_p.h b/src/gui/text/qcssparser_p.h index 1369bdf162f..a2d3622c7d7 100644 --- a/src/gui/text/qcssparser_p.h +++ b/src/gui/text/qcssparser_p.h @@ -167,6 +167,8 @@ enum Property { TextDecorationColor, QtPlaceHolderTextColor, QtAccent, + QtStrokeWidth, + QtStrokeColor, NumProperties }; diff --git a/src/gui/text/qtextdocument.cpp b/src/gui/text/qtextdocument.cpp index b5dd8c2b830..dc94643e8ae 100644 --- a/src/gui/text/qtextdocument.cpp +++ b/src/gui/text/qtextdocument.cpp @@ -2701,6 +2701,18 @@ bool QTextHtmlExporter::emitCharFormatStyle(const QTextCharFormat &format) attributesEmitted = true; } + if (format.hasProperty(QTextFormat::TextOutline)) { + QPen outlinePen = format.textOutline(); + html += " -qt-stroke-color:"_L1; + html += colorValue(outlinePen.color()); + html += u';'; + + html += " -qt-stroke-width:"_L1; + html += QString::number(outlinePen.widthF()); + html += "px;"_L1; + attributesEmitted = true; + } + return attributesEmitted; } diff --git a/src/gui/text/qtexthtmlparser.cpp b/src/gui/text/qtexthtmlparser.cpp index 1168c6d5cd8..df6e6788277 100644 --- a/src/gui/text/qtexthtmlparser.cpp +++ b/src/gui/text/qtexthtmlparser.cpp @@ -1387,6 +1387,24 @@ void QTextHtmlParserNode::applyCssDeclarations(const QList<QCss::Declaration> &d } break; } + case QCss::QtStrokeColor: + { + QPen pen = charFormat.textOutline(); + pen.setStyle(Qt::SolidLine); + pen.setColor(decl.colorValue()); + charFormat.setTextOutline(pen); + break; + } + case QCss::QtStrokeWidth: + { + qreal width; + if (decl.realValue(&width, "px")) { + QPen pen = charFormat.textOutline(); + pen.setWidthF(width); + charFormat.setTextOutline(pen); + } + break; + } default: break; } } diff --git a/tests/auto/gui/text/qtextdocument/tst_qtextdocument.cpp b/tests/auto/gui/text/qtextdocument/tst_qtextdocument.cpp index d233f6be182..ae6b0b28df3 100644 --- a/tests/auto/gui/text/qtextdocument/tst_qtextdocument.cpp +++ b/tests/auto/gui/text/qtextdocument/tst_qtextdocument.cpp @@ -183,6 +183,8 @@ private slots: void delayedLayout(); void undoContentChangeIndices(); + void restoreStrokeFromHtml(); + private: void backgroundImage_checkExpectedHtml(const QTextDocument &doc); void buildRegExpData(); @@ -4045,5 +4047,28 @@ void tst_QTextDocument::undoContentChangeIndices() // QTBUG-113865 QVERIFY(documentLength >= changeEnd); } +void tst_QTextDocument::restoreStrokeFromHtml() +{ + QTextDocument document; + QTextCursor textCursor(&document); + QTextCharFormat textOutline; + textOutline.setTextOutline(QPen(Qt::red, 2.3)); + textCursor.insertText("Outlined text", textOutline); + + { + QTextDocument otherDocument; + otherDocument.setHtml(document.toHtml()); + QCOMPARE(otherDocument.blockCount(), 1); + QTextBlock block = otherDocument.firstBlock(); + QTextFragment fragment = block.begin().fragment(); + QCOMPARE(fragment.text(), QStringLiteral("Outlined text")); + QTextCharFormat fmt = fragment.charFormat(); + QVERIFY(fmt.hasProperty(QTextCharFormat::TextOutline)); + QPen pen = fmt.textOutline(); + QCOMPARE(pen.color(), QColor(Qt::red)); + QCOMPARE(pen.widthF(), 2.3); + } +} + QTEST_MAIN(tst_QTextDocument) #include "tst_qtextdocument.moc" |