2 This file is part of Konsole, an X terminal.
4 Copyright 2006-2008 by Robert Knight <robertknight@gmail.com>
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.
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.
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
23 #include "TerminalCharacterDecoder.h"
26 #include <QtCore/QTextStream>
32 #include "konsole_wcwidth.h"
34 using namespace Konsole
;
35 PlainTextDecoder::PlainTextDecoder()
37 , _includeTrailingWhitespace(true)
38 , _recordLinePositions(false)
42 void PlainTextDecoder::setTrailingWhitespace(bool enable
)
44 _includeTrailingWhitespace
= enable
;
46 bool PlainTextDecoder::trailingWhitespace() const
48 return _includeTrailingWhitespace
;
50 void PlainTextDecoder::begin(QTextStream
* output
)
53 if (!_linePositions
.isEmpty())
54 _linePositions
.clear();
56 void PlainTextDecoder::end()
61 void PlainTextDecoder::setRecordLinePositions(bool record
)
63 _recordLinePositions
= record
;
65 QList
<int> PlainTextDecoder::linePositions() const
67 return _linePositions
;
69 void PlainTextDecoder::decodeLine(const Character
* const characters
, int count
, LineProperty
/*properties*/
74 if (_recordLinePositions
&& _output
->string())
76 int pos
= _output
->string()->count();
77 _linePositions
<< pos
;
80 //TODO should we ignore or respect the LINE_WRAPPED line property?
82 //note: we build up a QString and send it to the text stream rather writing into the text
83 //stream a character at a time because it is more efficient.
84 //(since QTextStream always deals with QStrings internally anyway)
86 plainText
.reserve(count
);
88 int outputCount
= count
;
90 // if inclusion of trailing whitespace is disabled then find the end of the
92 if ( !_includeTrailingWhitespace
)
94 for (int i
= count
-1 ; i
>= 0 ; i
--)
96 if ( characters
[i
].character
!= ' ' )
103 for (int i
=0;i
<outputCount
;)
105 plainText
.append( QChar(characters
[i
].character
) );
106 i
+= qMax(1,konsole_wcwidth(characters
[i
].character
));
108 *_output
<< plainText
;
111 HTMLDecoder::HTMLDecoder() :
113 ,_colorTable(base_color_table
)
114 ,_innerSpanOpen(false)
115 ,_lastRendition(DEFAULT_RENDITION
)
120 void HTMLDecoder::begin(QTextStream
* output
)
126 //open monospace span
127 openSpan(text
,"font-family:monospace");
132 void HTMLDecoder::end()
146 //TODO: Support for LineProperty (mainly double width , double height)
147 void HTMLDecoder::decodeLine(const Character
* const characters
, int count
, LineProperty
/*properties*/
156 for (int i
=0;i
<count
;i
++)
158 QChar
ch(characters
[i
].character
);
160 //check if appearance of character is different from previous char
161 if ( characters
[i
].rendition
!= _lastRendition
||
162 characters
[i
].foregroundColor
!= _lastForeColor
||
163 characters
[i
].backgroundColor
!= _lastBackColor
)
165 if ( _innerSpanOpen
)
168 _lastRendition
= characters
[i
].rendition
;
169 _lastForeColor
= characters
[i
].foregroundColor
;
170 _lastBackColor
= characters
[i
].backgroundColor
;
172 //build up style string
176 ColorEntry::FontWeight weight
= characters
[i
].fontWeight(_colorTable
);
177 if (weight
== ColorEntry::UseCurrentFormat
)
178 useBold
= _lastRendition
& RE_BOLD
;
180 useBold
= weight
== ColorEntry::Bold
;
183 style
.append("font-weight:bold;");
185 if ( _lastRendition
& RE_UNDERLINE
)
186 style
.append("font-decoration:underline;");
188 //colours - a colour table must have been defined first
191 style
.append( QString("color:%1;").arg(_lastForeColor
.color(_colorTable
).name() ) );
193 if (!characters
[i
].isTransparent(_colorTable
))
195 style
.append( QString("background-color:%1;").arg(_lastBackColor
.color(_colorTable
).name() ) );
199 //open the span with the current style
200 openSpan(text
,style
);
201 _innerSpanOpen
= true;
211 //output current character
214 //escape HTML tag characters and just display others as they are
224 text
.append(" "); //HTML truncates multiple spaces, so use a space marker instead
229 //close any remaining open inner spans
230 if ( _innerSpanOpen
)
238 void HTMLDecoder::openSpan(QString
& text
, const QString
& style
)
240 text
.append( QString("<span style=\"%1\">").arg(style
) );
243 void HTMLDecoder::closeSpan(QString
& text
)
245 text
.append("</span>");
248 void HTMLDecoder::setColorTable(const ColorEntry
* table
)