aboutsummaryrefslogtreecommitdiffstats
path: root/src/libs/3rdparty/syntax-highlighting/src/lib/htmlhighlighter.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/libs/3rdparty/syntax-highlighting/src/lib/htmlhighlighter.cpp')
-rw-r--r--src/libs/3rdparty/syntax-highlighting/src/lib/htmlhighlighter.cpp50
1 files changed, 33 insertions, 17 deletions
diff --git a/src/libs/3rdparty/syntax-highlighting/src/lib/htmlhighlighter.cpp b/src/libs/3rdparty/syntax-highlighting/src/lib/htmlhighlighter.cpp
index a95888f40fc..a191fab7e9f 100644
--- a/src/libs/3rdparty/syntax-highlighting/src/lib/htmlhighlighter.cpp
+++ b/src/libs/3rdparty/syntax-highlighting/src/lib/htmlhighlighter.cpp
@@ -44,21 +44,13 @@ void HtmlHighlighter::setOutputFile(const QString &fileName)
return;
}
d->out.reset(new QTextStream(d->file.get()));
-#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
- d->out->setEncoding(QStringConverter::Utf8);
-#else
d->out->setCodec("UTF-8");
-#endif
}
void HtmlHighlighter::setOutputFile(FILE *fileHandle)
{
d->out.reset(new QTextStream(fileHandle, QIODevice::WriteOnly));
-#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
- d->out->setEncoding(QStringConverter::Utf8);
-#else
d->out->setCodec("UTF-8");
-#endif
}
void HtmlHighlighter::highlightFile(const QString &fileName, const QString &title)
@@ -76,6 +68,33 @@ void HtmlHighlighter::highlightFile(const QString &fileName, const QString &titl
highlightData(&f, title);
}
+/**
+ * @brief toHtmlRgba
+ * Converts QColor -> rgba(r, g, b, a) if there is an alpha channel
+ * otherwise it will just return the hexcode. This is because QColor
+ * outputs #AARRGGBB, whereas browser support #RRGGBBAA.
+ *
+ * @param color
+ * @return
+ */
+static QString toHtmlRgbaString(const QColor &color)
+{
+ if (color.alpha() == 0xFF)
+ return color.name();
+
+ QString rgba = QStringLiteral("rgba(");
+ rgba.append(QString::number(color.red()));
+ rgba.append(QLatin1Char(','));
+ rgba.append(QString::number(color.green()));
+ rgba.append(QLatin1Char(','));
+ rgba.append(QString::number(color.blue()));
+ rgba.append(QLatin1Char(','));
+ // this must be alphaF
+ rgba.append(QString::number(color.alphaF()));
+ rgba.append(QLatin1Char(')'));
+ return rgba;
+}
+
void HtmlHighlighter::highlightData(QIODevice *dev, const QString &title)
{
if (!d->out) {
@@ -94,19 +113,16 @@ void HtmlHighlighter::highlightData(QIODevice *dev, const QString &title)
*d->out << "<html><head>\n";
*d->out << "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\"/>\n";
*d->out << "<title>" << htmlTitle << "</title>\n";
- *d->out << "<meta name=\"generator\" content=\"KF5::SyntaxHighlighting - Definition (" << definition().name() << ") - Theme (" << theme().name() << ")\"/>\n";
+ *d->out << "<meta name=\"generator\" content=\"KF5::SyntaxHighlighting - Definition (" << definition().name() << ") - Theme (" << theme().name()
+ << ")\"/>\n";
*d->out << "</head><body";
- *d->out << " style=\"background-color:" << QColor(theme().editorColor(Theme::BackgroundColor)).name();
+ *d->out << " style=\"background-color:" << toHtmlRgbaString(QColor::fromRgba(theme().editorColor(Theme::BackgroundColor)));
if (theme().textColor(Theme::Normal))
- *d->out << ";color:" << QColor(theme().textColor(Theme::Normal)).name();
+ *d->out << ";color:" << toHtmlRgbaString(QColor::fromRgba(theme().textColor(Theme::Normal)));
*d->out << "\"><pre>\n";
QTextStream in(dev);
-#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
- in.setEncoding(QStringConverter::Utf8);
-#else
in.setCodec("UTF-8");
-#endif
while (!in.atEnd()) {
d->currentLine = in.readLine();
state = highlightLine(d->currentLine, state);
@@ -128,9 +144,9 @@ void HtmlHighlighter::applyFormat(int offset, int length, const Format &format)
// collect potential output, cheaper than thinking about "is there any?"
QVarLengthArray<QString, 16> formatOutput;
if (format.hasTextColor(theme()))
- formatOutput << QStringLiteral("color:") << format.textColor(theme()).name() << QStringLiteral(";");
+ formatOutput << QStringLiteral("color:") << toHtmlRgbaString(format.textColor(theme())) << QStringLiteral(";");
if (format.hasBackgroundColor(theme()))
- formatOutput << QStringLiteral("background-color:") << format.backgroundColor(theme()).name() << QStringLiteral(";");
+ formatOutput << QStringLiteral("background-color:") << toHtmlRgbaString(format.backgroundColor(theme())) << QStringLiteral(";");
if (format.isBold(theme()))
formatOutput << QStringLiteral("font-weight:bold;");
if (format.isItalic(theme()))