From 2d5d6c8fbc4e0575bd4388a8ccc6d198619a3a03 Mon Sep 17 00:00:00 2001 From: Alexey Chernov <4ernov@gmail.com> Date: Mon, 2 Jul 2012 23:14:31 +0400 Subject: Color transparency support in html import/export Export of color transparency component is added for cases where color is exported to html. New static function colorValue() is added to prepare CSS string representation of QColor. When the color is opaque, it falls down to QColor::name() method which was used previously, otherwise it returns 'rgba()' CSS statement or 'transparent' keyword in case transparency is 0. 6-digit precision is used for alpha value as it's maximum which can be processed properly by Gecko and Webkit engines (http://lists.w3.org/Archives/Public/www-style/2009Dec/0295.html). Import part for rgba() statement was also added to QCssParser. It supports rgba() color values as stated in CSS Color Module Level 3 (http://www.w3.org/TR/css3-color/#rgba-color). Import of undocumented statement 'rgba(int,int,int,int);' was also added to preserve regression test success and to provide compatibility with previous code relying on this behaviour. Test cases added to QCssParser autotest for rgba(int,int,int,float) statement and to QTextDocument autotest for rgba(int,int,int,float) and 'transparent' statements for certain 'color', 'background-color' and 'bgcolor' properties. Change-Id: Id341c4e800249820d52edef8003e50f9a74d062b Reviewed-by: Eskil Abrahamsen Blomfeldt --- tests/auto/gui/text/qcssparser/tst_qcssparser.cpp | 1 + .../gui/text/qtextdocument/tst_qtextdocument.cpp | 122 +++++++++++++++++++++ 2 files changed, 123 insertions(+) (limited to 'tests') diff --git a/tests/auto/gui/text/qcssparser/tst_qcssparser.cpp b/tests/auto/gui/text/qcssparser/tst_qcssparser.cpp index 4d50601664..8523be094e 100644 --- a/tests/auto/gui/text/qcssparser/tst_qcssparser.cpp +++ b/tests/auto/gui/text/qcssparser/tst_qcssparser.cpp @@ -855,6 +855,7 @@ void tst_QCssParser::colorValue_data() QTest::newRow("functional1") << "color: rgb(21, 45, 73)" << QColor(21, 45, 73); QTest::newRow("functional2") << "color: rgb(100%, 0%, 100%)" << QColor(0xff, 0, 0xff); QTest::newRow("rgba") << "color: rgba(10, 20, 30, 40)" << QColor(10, 20, 30, 40); + QTest::newRow("rgbaf") << "color: rgba(10, 20, 30, 0.5)" << QColor(10, 20, 30, 127); QTest::newRow("rgb") << "color: rgb(10, 20, 30, 40)" << QColor(10, 20, 30, 40); QTest::newRow("hsl") << "color: hsv(10, 20, 30)" << QColor::fromHsv(10, 20, 30, 255); QTest::newRow("hsla") << "color: hsva(10, 20, 30, 40)" << QColor::fromHsv(10, 20, 30, 40); diff --git a/tests/auto/gui/text/qtextdocument/tst_qtextdocument.cpp b/tests/auto/gui/text/qtextdocument/tst_qtextdocument.cpp index 6fb3ab9cd5..a24d17dc3d 100644 --- a/tests/auto/gui/text/qtextdocument/tst_qtextdocument.cpp +++ b/tests/auto/gui/text/qtextdocument/tst_qtextdocument.cpp @@ -110,6 +110,8 @@ private slots: void setFragmentMarkersInHtmlExport(); void toHtmlBodyBgColor(); + void toHtmlBodyBgColorRgba(); + void toHtmlBodyBgColorTransparent(); void toHtmlRootFrameProperties(); void capitalizationHtmlInExport(); void wordspacingHtmlExport(); @@ -894,6 +896,32 @@ void tst_QTextDocument::toHtml_data() QString("

Blah

"); } + { + CREATE_DOC_AND_CURSOR(); + + QTextBlockFormat fmt; + fmt.setBackground(QColor(255, 0, 0, 51)); + cursor.insertBlock(fmt); + cursor.insertText("Blah"); + + QTest::newRow("bgcolor-rgba") << QTextDocumentFragment(&doc) + << QString("EMPTYBLOCK") + + QString("

Blah

"); + } + + { + CREATE_DOC_AND_CURSOR(); + + QTextBlockFormat fmt; + fmt.setBackground(QColor(255, 0, 0, 0)); + cursor.insertBlock(fmt); + cursor.insertText("Blah"); + + QTest::newRow("bgcolor-transparent") << QTextDocumentFragment(&doc) + << QString("EMPTYBLOCK") + + QString("

Blah

"); + } + { CREATE_DOC_AND_CURSOR(); @@ -939,6 +967,28 @@ void tst_QTextDocument::toHtml_data() << QString("

Blah

"); } + { + CREATE_DOC_AND_CURSOR(); + + QTextCharFormat fmt; + fmt.setForeground(QColor(0, 255, 0, 51)); + cursor.insertText("Blah", fmt); + + QTest::newRow("color-rgba") << QTextDocumentFragment(&doc) + << QString("

Blah

"); + } + + { + CREATE_DOC_AND_CURSOR(); + + QTextCharFormat fmt; + fmt.setForeground(QColor(0, 255, 0, 0)); + cursor.insertText("Blah", fmt); + + QTest::newRow("color-transparent") << QTextDocumentFragment(&doc) + << QString("

Blah

"); + } + { CREATE_DOC_AND_CURSOR(); @@ -950,6 +1000,28 @@ void tst_QTextDocument::toHtml_data() << QString("

Blah

"); } + { + CREATE_DOC_AND_CURSOR(); + + QTextCharFormat fmt; + fmt.setBackground(QColor(0, 255, 0, 51)); + cursor.insertText("Blah", fmt); + + QTest::newRow("span-bgcolor-rgba") << QTextDocumentFragment(&doc) + << QString("

Blah

"); + } + + { + CREATE_DOC_AND_CURSOR(); + + QTextCharFormat fmt; + fmt.setBackground(QColor(0, 255, 0, 0)); + cursor.insertText("Blah", fmt); + + QTest::newRow("span-bgcolor-transparent") << QTextDocumentFragment(&doc) + << QString("

Blah

"); + } + { CREATE_DOC_AND_CURSOR(); @@ -1706,6 +1778,56 @@ void tst_QTextDocument::toHtmlBodyBgColor() QCOMPARE(doc.toHtml(), expectedHtml); } +void tst_QTextDocument::toHtmlBodyBgColorRgba() +{ + CREATE_DOC_AND_CURSOR(); + + cursor.insertText("Blah"); + + QTextFrameFormat fmt = doc.rootFrame()->frameFormat(); + fmt.setBackground(QColor(255, 0, 0, 51)); + doc.rootFrame()->setFrameFormat(fmt); + + QString expectedHtml("\n" + "" + "\n" + "

Blah

" + ""); + + expectedHtml = expectedHtml.arg(defaultFont.family()).arg(defaultFont.pointSizeF()).arg(defaultFont.weight() * 8).arg((defaultFont.italic() ? "italic" : "normal")); + + QCOMPARE(doc.toHtml(), expectedHtml); +} + +void tst_QTextDocument::toHtmlBodyBgColorTransparent() +{ + CREATE_DOC_AND_CURSOR(); + + cursor.insertText("Blah"); + + QTextFrameFormat fmt = doc.rootFrame()->frameFormat(); + fmt.setBackground(QColor(255, 0, 0, 0)); + doc.rootFrame()->setFrameFormat(fmt); + + QString expectedHtml("\n" + "" + "\n" + "

Blah

" + ""); + + expectedHtml = expectedHtml.arg(defaultFont.family()).arg(defaultFont.pointSizeF()).arg(defaultFont.weight() * 8).arg((defaultFont.italic() ? "italic" : "normal")); + + QCOMPARE(doc.toHtml(), expectedHtml); +} + void tst_QTextDocument::toHtmlRootFrameProperties() { CREATE_DOC_AND_CURSOR(); -- cgit v1.2.3