Changeset View
Changeset View
Standalone View
Standalone View
src/lib/htmlhighlighter.cpp
1 | /* | 1 | /* | ||
---|---|---|---|---|---|
2 | Copyright (C) 2016 Volker Krause <vkrause@kde.org> | 2 | Copyright (C) 2016 Volker Krause <vkrause@kde.org> | ||
3 | Copyright (C) 2018 Christoph Cullmann <cullmann@kde.org> | ||||
3 | 4 | | |||
4 | Permission is hereby granted, free of charge, to any person obtaining | 5 | Permission is hereby granted, free of charge, to any person obtaining | ||
5 | a copy of this software and associated documentation files (the | 6 | a copy of this software and associated documentation files (the | ||
6 | "Software"), to deal in the Software without restriction, including | 7 | "Software"), to deal in the Software without restriction, including | ||
7 | without limitation the rights to use, copy, modify, merge, publish, | 8 | without limitation the rights to use, copy, modify, merge, publish, | ||
8 | distribute, sublicense, and/or sell copies of the Software, and to | 9 | distribute, sublicense, and/or sell copies of the Software, and to | ||
9 | permit persons to whom the Software is furnished to do so, subject to | 10 | permit persons to whom the Software is furnished to do so, subject to | ||
10 | the following conditions: | 11 | the following conditions: | ||
Show All 16 Lines | |||||
27 | #include "state.h" | 28 | #include "state.h" | ||
28 | #include "theme.h" | 29 | #include "theme.h" | ||
29 | #include "ksyntaxhighlighting_logging.h" | 30 | #include "ksyntaxhighlighting_logging.h" | ||
30 | 31 | | |||
31 | #include <QDebug> | 32 | #include <QDebug> | ||
32 | #include <QFile> | 33 | #include <QFile> | ||
33 | #include <QFileInfo> | 34 | #include <QFileInfo> | ||
34 | #include <QTextStream> | 35 | #include <QTextStream> | ||
36 | #include <QVarLengthArray> | ||||
35 | 37 | | |||
36 | using namespace KSyntaxHighlighting; | 38 | using namespace KSyntaxHighlighting; | ||
37 | 39 | | |||
38 | class KSyntaxHighlighting::HtmlHighlighterPrivate | 40 | class KSyntaxHighlighting::HtmlHighlighterPrivate | ||
39 | { | 41 | { | ||
40 | public: | 42 | public: | ||
41 | std::unique_ptr<QTextStream> out; | 43 | std::unique_ptr<QTextStream> out; | ||
42 | std::unique_ptr<QFile> file; | 44 | std::unique_ptr<QFile> file; | ||
▲ Show 20 Lines • Show All 66 Lines • ▼ Show 20 Line(s) | 75 | { | |||
109 | d->file.reset(); | 111 | d->file.reset(); | ||
110 | } | 112 | } | ||
111 | 113 | | |||
112 | void HtmlHighlighter::applyFormat(int offset, int length, const Format& format) | 114 | void HtmlHighlighter::applyFormat(int offset, int length, const Format& format) | ||
113 | { | 115 | { | ||
114 | if (length == 0) | 116 | if (length == 0) | ||
115 | return; | 117 | return; | ||
116 | 118 | | |||
117 | if (!format.isDefaultTextStyle(theme())) { | 119 | // collect potential output, cheaper than thinking about "is there any?" | ||
118 | *d->out << "<span style=\""; | 120 | QVarLengthArray<QString, 16> formatOutput; | ||
119 | if (format.hasTextColor(theme())) | 121 | if (format.hasTextColor(theme())) | ||
120 | *d->out << "color:" << format.textColor(theme()).name() << ";"; | 122 | formatOutput << QStringLiteral("color:") << format.textColor(theme()).name() << QStringLiteral(";"); | ||
121 | if (format.hasBackgroundColor(theme())) | 123 | if (format.hasBackgroundColor(theme())) | ||
122 | *d->out << "background-color:" << format.backgroundColor(theme()).name() << ";"; | 124 | formatOutput << QStringLiteral("background-color:") << format.backgroundColor(theme()).name() << QStringLiteral(";"); | ||
123 | if (format.isBold(theme())) | 125 | if (format.isBold(theme())) | ||
124 | *d->out << "font-weight:bold;"; | 126 | formatOutput << QStringLiteral("font-weight:bold;"); | ||
125 | if (format.isItalic(theme())) | 127 | if (format.isItalic(theme())) | ||
126 | *d->out << "font-style:italic;"; | 128 | formatOutput << QStringLiteral("font-style:italic;"); | ||
127 | if (format.isUnderline(theme())) | 129 | if (format.isUnderline(theme())) | ||
128 | *d->out << "text-decoration:underline;"; | 130 | formatOutput << QStringLiteral("text-decoration:underline;"); | ||
129 | if (format.isStrikeThrough(theme())) | 131 | if (format.isStrikeThrough(theme())) | ||
130 | *d->out << "text-decoration:line-through;"; | 132 | formatOutput << QStringLiteral("text-decoration:line-through;"); | ||
133 | | ||||
134 | if (!formatOutput.isEmpty()) { | ||||
135 | *d->out << "<span style=\""; | ||||
136 | for (const auto &out : qAsConst(formatOutput)) { | ||||
137 | *d->out << out; | ||||
138 | } | ||||
131 | *d->out << "\">"; | 139 | *d->out << "\">"; | ||
132 | } | 140 | } | ||
133 | 141 | | |||
134 | *d->out << d->currentLine.mid(offset, length).toHtmlEscaped(); | 142 | *d->out << d->currentLine.mid(offset, length).toHtmlEscaped(); | ||
135 | 143 | | |||
136 | if (!format.isDefaultTextStyle(theme())) | 144 | if (!formatOutput.isEmpty()) { | ||
137 | *d->out << "</span>"; | 145 | *d->out << "</span>"; | ||
138 | } | 146 | } | ||
147 | } |