| 1 | /* |
| 2 | This file is part of Konsole, an X terminal. |
| 3 | |
| 4 | Copyright 2006-2008 by Robert Knight <[email protected]> |
| 5 | |
| 6 | This program is free software; you can redistribute it and/or modify |
| 7 | it under the terms of the GNU Lesser General Public License as published by |
| 8 | the Free Software Foundation; either version 2 of the License, or |
| 9 | (at your option) any later version. |
| 10 | |
| 11 | This program is distributed in the hope that it will be useful, |
| 12 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | GNU General Public License for more details. |
| 15 | |
| 16 | You should have received a copy of the GNU Lesser General Public License |
| 17 | along with this program; if not, write to the Free Software |
| 18 | Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
| 19 | 02110-1301 USA. |
| 20 | */ |
| 21 | |
| 22 | // Own |
| 23 | #include "TerminalCharacterDecoder.h" |
| 24 | |
| 25 | // stdlib |
| 26 | #include <cwctype> |
| 27 | |
| 28 | // Qt |
| 29 | #include <QTextStream> |
| 30 | |
| 31 | // KDE |
| 32 | //#include <kdebug.h> |
| 33 | |
| 34 | // Konsole |
| 35 | #include "konsole_wcwidth.h" |
| 36 | |
| 37 | #include <cwctype> |
| 38 | |
| 39 | using namespace Konsole; |
| 40 | PlainTextDecoder::PlainTextDecoder() |
| 41 | : _output(nullptr) |
| 42 | , _includeTrailingWhitespace(true) |
| 43 | , _recordLinePositions(false) |
| 44 | { |
| 45 | |
| 46 | } |
| 47 | void PlainTextDecoder::setTrailingWhitespace(bool enable) |
| 48 | { |
| 49 | _includeTrailingWhitespace = enable; |
| 50 | } |
| 51 | bool PlainTextDecoder::trailingWhitespace() const |
| 52 | { |
| 53 | return _includeTrailingWhitespace; |
| 54 | } |
| 55 | void PlainTextDecoder::begin(QTextStream* output) |
| 56 | { |
| 57 | _output = output; |
| 58 | if (!_linePositions.isEmpty()) |
| 59 | _linePositions.clear(); |
| 60 | } |
| 61 | void PlainTextDecoder::end() |
| 62 | { |
| 63 | _output = nullptr; |
| 64 | } |
| 65 | |
| 66 | void PlainTextDecoder::setRecordLinePositions(bool record) |
| 67 | { |
| 68 | _recordLinePositions = record; |
| 69 | } |
| 70 | QList<int> PlainTextDecoder::linePositions() const |
| 71 | { |
| 72 | return _linePositions; |
| 73 | } |
| 74 | void PlainTextDecoder::decodeLine(const Character* const characters, int count, LineProperty /*properties*/ |
| 75 | ) |
| 76 | { |
| 77 | Q_ASSERT( _output ); |
| 78 | |
| 79 | if (_recordLinePositions && _output->string()) |
| 80 | { |
| 81 | int pos = _output->string()->count(); |
| 82 | _linePositions << pos; |
| 83 | } |
| 84 | |
| 85 | // check the real length |
| 86 | for (int i = 0 ; i < count ; i++) |
| 87 | { |
| 88 | if (characters + i == nullptr) |
| 89 | { |
| 90 | count = i; |
| 91 | break; |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | //TODO should we ignore or respect the LINE_WRAPPED line property? |
| 96 | |
| 97 | //note: we build up a QString and send it to the text stream rather writing into the text |
| 98 | //stream a character at a time because it is more efficient. |
| 99 | //(since QTextStream always deals with QStrings internally anyway) |
| 100 | std::wstring plainText; |
| 101 | plainText.reserve(count); |
| 102 | |
| 103 | int outputCount = count; |
| 104 | |
| 105 | // if inclusion of trailing whitespace is disabled then find the end of the |
| 106 | // line |
| 107 | if ( !_includeTrailingWhitespace ) |
| 108 | { |
| 109 | for (int i = count-1 ; i >= 0 ; i--) |
| 110 | { |
| 111 | if ( characters[i].character != L' ' ) |
| 112 | break; |
| 113 | else |
| 114 | outputCount--; |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | for (int i=0;i<outputCount;) |
| 119 | { |
| 120 | plainText.push_back( characters[i].character ); |
| 121 | i += qMax(1,konsole_wcwidth(characters[i].character)); |
| 122 | } |
| 123 | *_output << QString::fromStdWString(plainText); |
| 124 | } |
| 125 | |
| 126 | HTMLDecoder::HTMLDecoder() : |
| 127 | _output(nullptr) |
| 128 | ,_colorTable(base_color_table) |
| 129 | ,_innerSpanOpen(false) |
| 130 | ,_lastRendition(DEFAULT_RENDITION) |
| 131 | { |
| 132 | |
| 133 | } |
| 134 | |
| 135 | void HTMLDecoder::begin(QTextStream* output) |
| 136 | { |
| 137 | _output = output; |
| 138 | |
| 139 | std::wstring text; |
| 140 | |
| 141 | //open monospace span |
| 142 | openSpan(text,QLatin1String("font-family:monospace")); |
| 143 | |
| 144 | *output << QString::fromStdWString(text); |
| 145 | } |
| 146 | |
| 147 | void HTMLDecoder::end() |
| 148 | { |
| 149 | Q_ASSERT( _output ); |
| 150 | |
| 151 | std::wstring text; |
| 152 | |
| 153 | closeSpan(text); |
| 154 | |
| 155 | *_output << QString::fromStdWString(text); |
| 156 | |
| 157 | _output = nullptr; |
| 158 | |
| 159 | } |
| 160 | |
| 161 | //TODO: Support for LineProperty (mainly double width , double height) |
| 162 | void HTMLDecoder::decodeLine(const Character* const characters, int count, LineProperty /*properties*/ |
| 163 | ) |
| 164 | { |
| 165 | Q_ASSERT( _output ); |
| 166 | |
| 167 | std::wstring text; |
| 168 | |
| 169 | int spaceCount = 0; |
| 170 | |
| 171 | for (int i=0;i<count;i++) |
| 172 | { |
| 173 | wchar_t ch(characters[i].character); |
| 174 | |
| 175 | //check if appearance of character is different from previous char |
| 176 | if ( characters[i].rendition != _lastRendition || |
| 177 | characters[i].foregroundColor != _lastForeColor || |
| 178 | characters[i].backgroundColor != _lastBackColor ) |
| 179 | { |
| 180 | if ( _innerSpanOpen ) |
| 181 | closeSpan(text); |
| 182 | |
| 183 | _lastRendition = characters[i].rendition; |
| 184 | _lastForeColor = characters[i].foregroundColor; |
| 185 | _lastBackColor = characters[i].backgroundColor; |
| 186 | |
| 187 | //build up style string |
| 188 | QString style; |
| 189 | |
| 190 | bool useBold; |
| 191 | ColorEntry::FontWeight weight = characters[i].fontWeight(_colorTable); |
| 192 | if (weight == ColorEntry::UseCurrentFormat) |
| 193 | useBold = _lastRendition & RE_BOLD; |
| 194 | else |
| 195 | useBold = weight == ColorEntry::Bold; |
| 196 | |
| 197 | if (useBold) |
| 198 | style.append(QLatin1String("font-weight:bold;")); |
| 199 | |
| 200 | if ( _lastRendition & RE_UNDERLINE ) |
| 201 | style.append(QLatin1String("font-decoration:underline;")); |
| 202 | |
| 203 | //colours - a colour table must have been defined first |
| 204 | if ( _colorTable ) |
| 205 | { |
| 206 | style.append( QString::fromLatin1("color:%1;").arg(_lastForeColor.color(_colorTable).name() ) ); |
| 207 | |
| 208 | if (!characters[i].isTransparent(_colorTable)) |
| 209 | { |
| 210 | style.append( QString::fromLatin1("background-color:%1;").arg(_lastBackColor.color(_colorTable).name() ) ); |
| 211 | } |
| 212 | } |
| 213 | |
| 214 | //open the span with the current style |
| 215 | openSpan(text,style); |
| 216 | _innerSpanOpen = true; |
| 217 | } |
| 218 | |
| 219 | //handle whitespace |
| 220 | if (std::iswspace(ch)) |
| 221 | spaceCount++; |
| 222 | else |
| 223 | spaceCount = 0; |
| 224 | |
| 225 | |
| 226 | //output current character |
| 227 | if (spaceCount < 2) |
| 228 | { |
| 229 | //escape HTML tag characters and just display others as they are |
| 230 | if ( ch == '<' ) |
| 231 | text.append(L"<"); |
| 232 | else if (ch == '>') |
| 233 | text.append(L">"); |
| 234 | else |
| 235 | text.push_back(ch); |
| 236 | } |
| 237 | else |
| 238 | { |
| 239 | text.append(L" "); //HTML truncates multiple spaces, so use a space marker instead |
| 240 | } |
| 241 | |
| 242 | } |
| 243 | |
| 244 | //close any remaining open inner spans |
| 245 | if ( _innerSpanOpen ) |
| 246 | closeSpan(text); |
| 247 | |
| 248 | //start new line |
| 249 | text.append(L"<br>"); |
| 250 | |
| 251 | *_output << QString::fromStdWString(text); |
| 252 | } |
| 253 | void HTMLDecoder::openSpan(std::wstring& text , const QString& style) |
| 254 | { |
| 255 | text.append( QString(QLatin1String("<span style=\"%1\">")).arg(style).toStdWString() ); |
| 256 | } |
| 257 | |
| 258 | void HTMLDecoder::closeSpan(std::wstring& text) |
| 259 | { |
| 260 | text.append(L"</span>"); |
| 261 | } |
| 262 | |
| 263 | void HTMLDecoder::setColorTable(const ColorEntry* table) |
| 264 | { |
| 265 | _colorTable = table; |
| 266 | } |