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 bool useBold;
00176 ColorEntry::FontWeight weight = characters[i].fontWeight(_colorTable);
00177 if (weight == ColorEntry::UseCurrentFormat)
00178 useBold = _lastRendition & RE_BOLD;
00179 else
00180 useBold = weight == ColorEntry::Bold;
00181
00182 if (useBold)
00183 style.append("font-weight:bold;");
00184
00185 if ( _lastRendition & RE_UNDERLINE )
00186 style.append("font-decoration:underline;");
00187
00188
00189 if ( _colorTable )
00190 {
00191 style.append( QString("color:%1;").arg(_lastForeColor.color(_colorTable).name() ) );
00192
00193 if (!characters[i].isTransparent(_colorTable))
00194 {
00195 style.append( QString("background-color:%1;").arg(_lastBackColor.color(_colorTable).name() ) );
00196 }
00197 }
00198
00199
00200 openSpan(text,style);
00201 _innerSpanOpen = true;
00202 }
00203
00204
00205 if (ch.isSpace())
00206 spaceCount++;
00207 else
00208 spaceCount = 0;
00209
00210
00211
00212 if (spaceCount < 2)
00213 {
00214
00215 if ( ch == '<' )
00216 text.append("<");
00217 else if (ch == '>')
00218 text.append(">");
00219 else
00220 text.append(ch);
00221 }
00222 else
00223 {
00224 text.append(" ");
00225 }
00226
00227 }
00228
00229
00230 if ( _innerSpanOpen )
00231 closeSpan(text);
00232
00233
00234 text.append("<br>");
00235
00236 *_output << text;
00237 }
00238 void HTMLDecoder::openSpan(QString& text , const QString& style)
00239 {
00240 text.append( QString("<span style=\"%1\">").arg(style) );
00241 }
00242
00243 void HTMLDecoder::closeSpan(QString& text)
00244 {
00245 text.append("</span>");
00246 }
00247
00248 void HTMLDecoder::setColorTable(const ColorEntry* table)
00249 {
00250 _colorTable = table;
00251 }