| 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"cxx . . $ `<file>/lib/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"cxx . . $ `<file>/lib/konsole_wcwidth.h`/ |
| 36 | |
| 37 | #include <cwctype> |
| 38 | |
| 39 | using namespace Konsole; |
| 40 | PlainTextDecodercxx . . $ Konsole/PlainTextDecoder#::PlainTextDecodercxx . . $ Konsole/PlainTextDecoder#PlainTextDecoder(49f6e7a06ebc5aa8).() |
| 41 | : _outputcxx . . $ Konsole/PlainTextDecoder#_output.(nullptr) |
| 42 | , _includeTrailingWhitespacecxx . . $ Konsole/PlainTextDecoder#_includeTrailingWhitespace.(true) |
| 43 | , _recordLinePositionscxx . . $ Konsole/PlainTextDecoder#_recordLinePositions.(false) |
| 44 | { |
| 45 | |
| 46 | } |
| 47 | void PlainTextDecodercxx . . $ Konsole/PlainTextDecoder#::setTrailingWhitespacecxx . . $ Konsole/PlainTextDecoder#setTrailingWhitespace(b45ddcbc8423892c).(bool enable) |
| 48 | { |
| 49 | _includeTrailingWhitespacecxx . . $ Konsole/PlainTextDecoder#_includeTrailingWhitespace. = enable; |
| 50 | } |
| 51 | bool PlainTextDecodercxx . . $ Konsole/PlainTextDecoder#::trailingWhitespacecxx . . $ Konsole/PlainTextDecoder#trailingWhitespace(50ce9a9e25b4a850).() const |
| 52 | { |
| 53 | return _includeTrailingWhitespacecxx . . $ Konsole/PlainTextDecoder#_includeTrailingWhitespace.; |
| 54 | } |
| 55 | void PlainTextDecodercxx . . $ Konsole/PlainTextDecoder#::begincxx . . $ Konsole/PlainTextDecoder#begin(aca783e314870a73).(QTextStreamcxx . . $ QTextStream#* output) |
| 56 | { |
| 57 | _outputcxx . . $ Konsole/PlainTextDecoder#_output. = output; |
| 58 | if (!_linePositionscxx . . $ Konsole/PlainTextDecoder#_linePositions..isEmptycxx . . $ QList#isEmpty(3482b152b9333168).()) |
| 59 | _linePositionscxx . . $ Konsole/PlainTextDecoder#_linePositions..clearcxx . . $ QList#clear(49f6e7a06ebc5aa8).(); |
| 60 | } |
| 61 | void PlainTextDecodercxx . . $ Konsole/PlainTextDecoder#::endcxx . . $ Konsole/PlainTextDecoder#end(49f6e7a06ebc5aa8).() |
| 62 | { |
| 63 | _outputcxx . . $ Konsole/PlainTextDecoder#_output. = nullptr; |
| 64 | } |
| 65 | |
| 66 | void PlainTextDecodercxx . . $ Konsole/PlainTextDecoder#::setRecordLinePositionscxx . . $ Konsole/PlainTextDecoder#setRecordLinePositions(b45ddcbc8423892c).(bool record) |
| 67 | { |
| 68 | _recordLinePositionscxx . . $ Konsole/PlainTextDecoder#_recordLinePositions. = record; |
| 69 | } |
| 70 | QListcxx . . $ QList#<int> PlainTextDecodercxx . . $ Konsole/PlainTextDecoder#::linePositionscxx . . $ Konsole/PlainTextDecoder#linePositions(acfbda32237c20a3).() const |
| 71 | { |
| 72 | return _linePositionscxx . . $ QList#QList(3d2301cfd89f773a).; |
| 73 | } |
| 74 | void PlainTextDecodercxx . . $ Konsole/PlainTextDecoder#::decodeLinecxx . . $ Konsole/PlainTextDecoder#decodeLine(aa119f8849ce0e23).(const Charactercxx . . $ Konsole/Character#* const characters, int count, LinePropertycxx . . $ Konsole/LineProperty# /*properties*/ |
| 75 | ) |
| 76 | { |
| 77 | Q_ASSERTcxx . . $ `<external>/15045578679651060896/qglobal.h:859:13`!( _output ); |
| 78 | |
| 79 | if (_recordLinePositionscxx . . $ Konsole/PlainTextDecoder#_recordLinePositions. && _outputcxx . . $ Konsole/PlainTextDecoder#_output.->stringcxx . . $ QTextStream#string(5ed8436d3dc1fecf).()) |
| 80 | { |
| 81 | int pos = _outputcxx . . $ Konsole/PlainTextDecoder#_output.->stringcxx . . $ QTextStream#string(5ed8436d3dc1fecf).()->countcxx . . $ QString#count(455f465bc33b4cdf).(); |
| 82 | _linePositionscxx . . $ Konsole/PlainTextDecoder#_linePositions. <<cxx . . $ QList#`operator<<`(309d1580d8f5dc58). 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 | stdcxx . . $ std/::wstringcxx . . $ std/wstring# plainTextcxx . . $ std/__cxx11/basic_string#basic_string(925dcc5e17f50391).; |
| 101 | plainText.reservecxx . . $ std/__cxx11/basic_string#reserve(9dac5f4bebd06212).(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 ( !_includeTrailingWhitespacecxx . . $ Konsole/PlainTextDecoder#_includeTrailingWhitespace. ) |
| 108 | { |
| 109 | for (int i = count-1 ; i >= 0 ; i--) |
| 110 | { |
| 111 | if ( characters[i].charactercxx . . $ Konsole/Character#$anonymous_type_0#character. != L' ' ) |
| 112 | break; |
| 113 | else |
| 114 | outputCount--; |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | for (int i=0;i<outputCount;) |
| 119 | { |
| 120 | plainText.push_backcxx . . $ std/__cxx11/basic_string#push_back(9b289cee16747614).( characters[i].charactercxx . . $ Konsole/Character#$anonymous_type_0#character. ); |
| 121 | i += qMaxcxx . . $ qMax(2a338fce5842aa6a).(1,konsole_wcwidthcxx . . $ konsole_wcwidth(9ddfcb434f4b4cce).(characters[i].charactercxx . . $ Konsole/Character#$anonymous_type_0#character.)); |
| 122 | } |
| 123 | *_outputcxx . . $ Konsole/PlainTextDecoder#_output. <<cxx . . $ QTextStream#`operator<<`(f61e69e0d177108f). QStringcxx . . $ QString#::fromStdWStringcxx . . $ QString#fromStdWString(f337decc2b7aedbe).(plainText); |
| 124 | } |
| 125 | |
| 126 | HTMLDecodercxx . . $ Konsole/HTMLDecoder#::HTMLDecodercxx . . $ Konsole/HTMLDecoder#HTMLDecoder(49f6e7a06ebc5aa8).() : |
| 127 | _outputcxx . . $ Konsole/HTMLDecoder#_output.(nullptr) |
| 128 | ,_colorTablecxx . . $ Konsole/HTMLDecoder#_colorTable.(base_color_tablecxx . . $ Konsole/base_color_table.) |
| 129 | ,_innerSpanOpencxx . . $ Konsole/HTMLDecoder#_innerSpanOpen.(false) |
| 130 | ,_lastRenditioncxx . . $ Konsole/HTMLDecoder#_lastRendition.(DEFAULT_RENDITIONcxx . . $ `lib/Character.h:42:9`!) |
| 131 | { |
| 132 | |
| 133 | } |
| 134 | |
| 135 | void HTMLDecodercxx . . $ Konsole/HTMLDecoder#::begincxx . . $ Konsole/HTMLDecoder#begin(aca783e314870a73).(QTextStreamcxx . . $ QTextStream#* output) |
| 136 | { |
| 137 | _outputcxx . . $ Konsole/HTMLDecoder#_output. = output; |
| 138 | |
| 139 | stdcxx . . $ std/::wstringcxx . . $ std/wstring# textcxx . . $ std/__cxx11/basic_string#basic_string(925dcc5e17f50391).; |
| 140 | |
| 141 | //open monospace span |
| 142 | openSpancxx . . $ Konsole/HTMLDecoder#openSpan(f0d40cabe90ae651).(text,QLatin1Stringcxx . . $ QString#QString(4641beeb24e75d60).("font-family:monospace")); |
| 143 | |
| 144 | *output <<cxx . . $ QTextStream#`operator<<`(f61e69e0d177108f). QStringcxx . . $ QString#::fromStdWStringcxx . . $ QString#fromStdWString(f337decc2b7aedbe).(text); |
| 145 | } |
| 146 | |
| 147 | void HTMLDecodercxx . . $ Konsole/HTMLDecoder#::endcxx . . $ Konsole/HTMLDecoder#end(49f6e7a06ebc5aa8).() |
| 148 | { |
| 149 | Q_ASSERTcxx . . $ `<external>/15045578679651060896/qglobal.h:859:13`!( _output ); |
| 150 | |
| 151 | stdcxx . . $ std/::wstringcxx . . $ std/wstring# textcxx . . $ std/__cxx11/basic_string#basic_string(925dcc5e17f50391).; |
| 152 | |
| 153 | closeSpancxx . . $ Konsole/HTMLDecoder#closeSpan(641fc13601a5d57f).(text); |
| 154 | |
| 155 | *_outputcxx . . $ Konsole/HTMLDecoder#_output. <<cxx . . $ QTextStream#`operator<<`(f61e69e0d177108f). QStringcxx . . $ QString#::fromStdWStringcxx . . $ QString#fromStdWString(f337decc2b7aedbe).(text); |
| 156 | |
| 157 | _outputcxx . . $ Konsole/HTMLDecoder#_output. = nullptr; |
| 158 | |
| 159 | } |
| 160 | |
| 161 | //TODO: Support for LineProperty (mainly double width , double height) |
| 162 | void HTMLDecodercxx . . $ Konsole/HTMLDecoder#::decodeLinecxx . . $ Konsole/HTMLDecoder#decodeLine(aa119f8849ce0e23).(const Charactercxx . . $ Konsole/Character#* const characters, int count, LinePropertycxx . . $ Konsole/LineProperty# /*properties*/ |
| 163 | ) |
| 164 | { |
| 165 | Q_ASSERTcxx . . $ `<external>/15045578679651060896/qglobal.h:859:13`!( _output ); |
| 166 | |
| 167 | stdcxx . . $ std/::wstringcxx . . $ std/wstring# textcxx . . $ std/__cxx11/basic_string#basic_string(925dcc5e17f50391).; |
| 168 | |
| 169 | int spaceCount = 0; |
| 170 | |
| 171 | for (int i=0;i<count;i++) |
| 172 | { |
| 173 | wchar_t ch(characters[i].charactercxx . . $ Konsole/Character#$anonymous_type_0#character.); |
| 174 | |
| 175 | //check if appearance of character is different from previous char |
| 176 | if ( characters[i].renditioncxx . . $ Konsole/Character#rendition. != _lastRenditioncxx . . $ Konsole/HTMLDecoder#_lastRendition. || |
| 177 | characters[i].foregroundColorcxx . . $ Konsole/Character#foregroundColor. !=cxx . . $ Konsole/`operator!=`(40f11079b1efd097). _lastForeColorcxx . . $ Konsole/HTMLDecoder#_lastForeColor. || |
| 178 | characters[i].backgroundColorcxx . . $ Konsole/Character#backgroundColor. !=cxx . . $ Konsole/`operator!=`(40f11079b1efd097). _lastBackColorcxx . . $ Konsole/HTMLDecoder#_lastBackColor. ) |
| 179 | { |
| 180 | if ( _innerSpanOpencxx . . $ Konsole/HTMLDecoder#_innerSpanOpen. ) |
| 181 | closeSpancxx . . $ Konsole/HTMLDecoder#closeSpan(641fc13601a5d57f).(text); |
| 182 | |
| 183 | _lastRenditioncxx . . $ Konsole/HTMLDecoder#_lastRendition. = characters[i].renditioncxx . . $ Konsole/Character#rendition.; |
| 184 | _lastForeColorcxx . . $ Konsole/HTMLDecoder#_lastForeColor. =cxx . . $ Konsole/CharacterColor#`operator=`(d3b4b4caeab41583). characters[i].foregroundColorcxx . . $ Konsole/Character#foregroundColor.; |
| 185 | _lastBackColorcxx . . $ Konsole/HTMLDecoder#_lastBackColor. =cxx . . $ Konsole/CharacterColor#`operator=`(d3b4b4caeab41583). characters[i].backgroundColorcxx . . $ Konsole/Character#backgroundColor.; |
| 186 | |
| 187 | //build up style string |
| 188 | QStringcxx . . $ QString# stylecxx . . $ QString#QString(ced63f7c635d850d).; |
| 189 | |
| 190 | bool useBold; |
| 191 | ColorEntrycxx . . $ Konsole/ColorEntry#::FontWeightcxx . . $ Konsole/ColorEntry#FontWeight# weight = characters[i].fontWeightcxx . . $ Konsole/Character#fontWeight(d801bd6c3059e593).(_colorTablecxx . . $ Konsole/HTMLDecoder#_colorTable.); |
| 192 | if (weight == ColorEntrycxx . . $ Konsole/ColorEntry#::UseCurrentFormatcxx . . $ Konsole/ColorEntry#UseCurrentFormat.) |
| 193 | useBold = _lastRenditioncxx . . $ Konsole/HTMLDecoder#_lastRendition. & RE_BOLDcxx . . $ `lib/Character.h:43:9`!; |
| 194 | else |
| 195 | useBold = weight == ColorEntrycxx . . $ Konsole/ColorEntry#::Boldcxx . . $ Konsole/ColorEntry#Bold.; |
| 196 | |
| 197 | if (useBold) |
| 198 | style.appendcxx . . $ QString#append(10913c921d46e09a).(QLatin1Stringcxx . . $ QLatin1String#QLatin1String(aac1607cfe1c1740).("font-weight:bold;")); |
| 199 | |
| 200 | if ( _lastRenditioncxx . . $ Konsole/HTMLDecoder#_lastRendition. & RE_UNDERLINEcxx . . $ `lib/Character.h:45:9`! ) |
| 201 | style.appendcxx . . $ QString#append(10913c921d46e09a).(QLatin1Stringcxx . . $ QLatin1String#QLatin1String(aac1607cfe1c1740).("font-decoration:underline;")); |
| 202 | |
| 203 | //colours - a colour table must have been defined first |
| 204 | if ( _colorTablecxx . . $ Konsole/HTMLDecoder#_colorTable. ) |
| 205 | { |
| 206 | style.appendcxx . . $ QString#append(6a89d0b7f4d2681c).( QStringcxx . . $ QString#::fromLatin1cxx . . $ QString#fromLatin1(5ac70cc0fa2a874e).("color:%1;").argcxx . . $ QString#arg(bc823072288cbbb5).(_lastForeColorcxx . . $ Konsole/HTMLDecoder#_lastForeColor..colorcxx . . $ Konsole/CharacterColor#color(b83b316448997199).(_colorTablecxx . . $ Konsole/HTMLDecoder#_colorTable.).namecxx . . $ QColor#name(6ef7b6c27b00b43d).() ) ); |
| 207 | |
| 208 | if (!characters[i].isTransparentcxx . . $ Konsole/Character#isTransparent(1438919534f6f1e6).(_colorTablecxx . . $ Konsole/HTMLDecoder#_colorTable.)) |
| 209 | { |
| 210 | style.appendcxx . . $ QString#append(6a89d0b7f4d2681c).( QStringcxx . . $ QString#::fromLatin1cxx . . $ QString#fromLatin1(5ac70cc0fa2a874e).("background-color:%1;").argcxx . . $ QString#arg(bc823072288cbbb5).(_lastBackColorcxx . . $ Konsole/HTMLDecoder#_lastBackColor..colorcxx . . $ Konsole/CharacterColor#color(b83b316448997199).(_colorTablecxx . . $ Konsole/HTMLDecoder#_colorTable.).namecxx . . $ QColor#name(6ef7b6c27b00b43d).() ) ); |
| 211 | } |
| 212 | } |
| 213 | |
| 214 | //open the span with the current style |
| 215 | openSpancxx . . $ Konsole/HTMLDecoder#openSpan(f0d40cabe90ae651).(text,style); |
| 216 | _innerSpanOpencxx . . $ Konsole/HTMLDecoder#_innerSpanOpen. = true; |
| 217 | } |
| 218 | |
| 219 | //handle whitespace |
| 220 | if (stdcxx . . $ std/::iswspacecxx . . $ std/iswspace(fc2db35e70a7f92c).(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.appendcxx . . $ std/__cxx11/basic_string#append(978ca1133b5f6e74).(L"<"); |
| 232 | else if (ch == '>') |
| 233 | text.appendcxx . . $ std/__cxx11/basic_string#append(978ca1133b5f6e74).(L">"); |
| 234 | else |
| 235 | text.push_backcxx . . $ std/__cxx11/basic_string#push_back(9b289cee16747614).(ch); |
| 236 | } |
| 237 | else |
| 238 | { |
| 239 | text.appendcxx . . $ std/__cxx11/basic_string#append(978ca1133b5f6e74).(L" "); //HTML truncates multiple spaces, so use a space marker instead |
| 240 | } |
| 241 | |
| 242 | } |
| 243 | |
| 244 | //close any remaining open inner spans |
| 245 | if ( _innerSpanOpencxx . . $ Konsole/HTMLDecoder#_innerSpanOpen. ) |
| 246 | closeSpancxx . . $ Konsole/HTMLDecoder#closeSpan(641fc13601a5d57f).(text); |
| 247 | |
| 248 | //start new line |
| 249 | text.appendcxx . . $ std/__cxx11/basic_string#append(978ca1133b5f6e74).(L"<br>"); |
| 250 | |
| 251 | *_outputcxx . . $ Konsole/HTMLDecoder#_output. <<cxx . . $ QTextStream#`operator<<`(f61e69e0d177108f). QStringcxx . . $ QString#::fromStdWStringcxx . . $ QString#fromStdWString(f337decc2b7aedbe).(text); |
| 252 | } |
| 253 | void HTMLDecodercxx . . $ Konsole/HTMLDecoder#::openSpancxx . . $ Konsole/HTMLDecoder#openSpan(f0d40cabe90ae651).(stdcxx . . $ std/::wstringcxx . . $ std/wstring#& text , const QStringcxx . . $ QString#& style) |
| 254 | { |
| 255 | text.appendcxx . . $ std/__cxx11/basic_string#append(c0b47be1fddc0e75).( QStringcxx . . $ QString#QString(4641beeb24e75d60).(QLatin1Stringcxx . . $ QLatin1String#QLatin1String(aac1607cfe1c1740).("<span style=\"%1\">")).argcxx . . $ QString#arg(bc823072288cbbb5).(style).toStdWStringcxx . . $ QString#toStdWString(d07e96ea086800fd).() ); |
| 256 | } |
| 257 | |
| 258 | void HTMLDecodercxx . . $ Konsole/HTMLDecoder#::closeSpancxx . . $ Konsole/HTMLDecoder#closeSpan(641fc13601a5d57f).(stdcxx . . $ std/::wstringcxx . . $ std/wstring#& text) |
| 259 | { |
| 260 | text.appendcxx . . $ std/__cxx11/basic_string#append(978ca1133b5f6e74).(L"</span>"); |
| 261 | } |
| 262 | |
| 263 | void HTMLDecodercxx . . $ Konsole/HTMLDecoder#::setColorTablecxx . . $ Konsole/HTMLDecoder#setColorTable(b25cc3fd5c7c482d).(const ColorEntrycxx . . $ Konsole/ColorEntry#* table) |
| 264 | { |
| 265 | _colorTablecxx . . $ Konsole/HTMLDecoder#_colorTable. = table; |
| 266 | } |