Konsole
TerminalCharacterDecoder.cpp
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023 #include "TerminalCharacterDecoder.h"
00024
00025
00026 #include <QtCore/QTextStream>
00027
00028
00029 #include <kdebug.h>
00030
00031
00032 #include "konsole_wcwidth.h"
00033
00034 using namespace Konsole;
00035 PlainTextDecoder::PlainTextDecoder()
00036 : _output(0)
00037 , _includeTrailingWhitespace(true)
00038 , _recordLinePositions(false)
00039 {
00040
00041 }
00042 void PlainTextDecoder::setTrailingWhitespace(bool enable)
00043 {
00044 _includeTrailingWhitespace = enable;
00045 }
00046 bool PlainTextDecoder::trailingWhitespace() const
00047 {
00048 return _includeTrailingWhitespace;
00049 }
00050 void PlainTextDecoder::begin(QTextStream* output)
00051 {
00052 _output = output;
00053 if (!_linePositions.isEmpty())
00054 _linePositions.clear();
00055 }
00056 void PlainTextDecoder::end()
00057 {
00058 _output = 0;
00059 }
00060
00061 void PlainTextDecoder::setRecordLinePositions(bool record)
00062 {
00063 _recordLinePositions = record;
00064 }
00065 QList<int> PlainTextDecoder::linePositions() const
00066 {
00067 return _linePositions;
00068 }
00069 void PlainTextDecoder::decodeLine(const Character* const characters, int count, LineProperty
00070 )
00071 {
00072 Q_ASSERT( _output );
00073
00074 if (_recordLinePositions && _output->string())
00075 {
00076 int pos = _output->string()->count();
00077 _linePositions << pos;
00078 }
00079
00080
00081
00082
00083
00084
00085 QString plainText;
00086 plainText.reserve(count);
00087
00088 int outputCount = count;
00089
00090
00091
00092 if ( !_includeTrailingWhitespace )
00093 {
00094 for (int i = count-1 ; i >= 0 ; i--)
00095 {
00096 if ( characters[i].character != ' ' )
00097 break;
00098 else
00099 outputCount--;
00100 }
00101 }
00102
00103 for (int i=0;i<outputCount;)
00104 {
00105 plainText.append( QChar(characters[i].character) );
00106 i += qMax(1,konsole_wcwidth(characters[i].character));
00107 }
00108 *_output << plainText;
00109 }
00110
00111 HTMLDecoder::HTMLDecoder() :
00112 _output(0)
00113 ,_colorTable(base_color_table)
00114 ,_innerSpanOpen(false)
00115 ,_lastRendition(DEFAULT_RENDITION)
00116 {
00117
00118 }
00119
00120 void HTMLDecoder::begin(QTextStream* output)
00121 {
00122 _output = output;
00123
00124 QString text;
00125
00126
00127 openSpan(text,"font-family:monospace");
00128
00129 *output << text;
00130 }
00131
00132 void HTMLDecoder::end()
00133 {
00134 Q_ASSERT( _output );
00135
00136 QString text;
00137
00138 closeSpan(text);
00139
00140 *_output << text;
00141
00142 _output = 0;
00143
00144 }
00145
00146
00147 void HTMLDecoder::decodeLine(const Character* const characters, int count, LineProperty
00148 )
00149 {
00150 Q_ASSERT( _output );
00151
00152 QString text;
00153
00154 int spaceCount = 0;
00155
00156 for (int i=0;i<count;i++)
00157 {
00158 QChar ch(characters[i].character);
00159
00160
00161 if ( characters[i].rendition != _lastRendition ||
00162 characters[i].foregroundColor != _lastForeColor ||
00163 characters[i].backgroundColor != _lastBackColor )
00164 {
00165 if ( _innerSpanOpen )
00166 closeSpan(text);
00167
00168 _lastRendition = characters[i].rendition;
00169 _lastForeColor = characters[i].foregroundColor;
00170 _lastBackColor = characters[i].backgroundColor;
00171
00172
00173 QString style;
00174
00175 if ( _lastRendition & RE_BOLD ||
00176 (_colorTable && characters[i].isBold(_colorTable)) )
00177 style.append("font-weight:bold;");
00178
00179
00180 if ( _lastRendition & RE_UNDERLINE )
00181 style.append("font-decoration:underline;");
00182
00183
00184 if ( _colorTable )
00185 {
00186 style.append( QString("color:%1;").arg(_lastForeColor.color(_colorTable).name() ) );
00187
00188 if (!characters[i].isTransparent(_colorTable))
00189 {
00190 style.append( QString("background-color:%1;").arg(_lastBackColor.color(_colorTable).name() ) );
00191 }
00192 }
00193
00194
00195 openSpan(text,style);
00196 _innerSpanOpen = true;
00197 }
00198
00199
00200 if (ch.isSpace())
00201 spaceCount++;
00202 else
00203 spaceCount = 0;
00204
00205
00206
00207 if (spaceCount < 2)
00208 {
00209
00210 if ( ch == '<' )
00211 text.append("<");
00212 else if (ch == '>')
00213 text.append(">");
00214 else
00215 text.append(ch);
00216 }
00217 else
00218 {
00219 text.append(" ");
00220 }
00221
00222 }
00223
00224
00225 if ( _innerSpanOpen )
00226 closeSpan(text);
00227
00228
00229 text.append("<br>");
00230
00231 *_output << text;
00232 }
00233 void HTMLDecoder::openSpan(QString& text , const QString& style)
00234 {
00235 text.append( QString("<span style=\"%1\">").arg(style) );
00236 }
00237
00238 void HTMLDecoder::closeSpan(QString& text)
00239 {
00240 text.append("</span>");
00241 }
00242
00243 void HTMLDecoder::setColorTable(const ColorEntry* table)
00244 {
00245 _colorTable = table;
00246 }