1 // This file is part of Konsole, an X terminal.
3 // Copyright (C) 2006 by Robert Knight <robertknight@gmail.com>
5 // Rewritten for QT4 by e_k <e_k at users.sourceforge.net>, Copyright (C)2008
6 // Forked for Baulk - Copyright (C) 2008-2009 - Jacob Alexander <haata at users.sf.net>
8 // This program is free software; you can redistribute it and/or modify
9 // it under the terms of the GNU General Public License as published by
10 // the Free Software Foundation; either version 2 of the License, or
11 // any later version, including version 3 of the License.
13 // This program is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 // GNU General Public License for more details.
18 // You should have received a copy of the GNU General Public License
19 // along with this program. If not, see <http://www.gnu.org/licenses/>.
22 #include "TerminalCharacterDecoder.h"
25 #include <QtCore/QTextStream>
28 using namespace Konsole
;
30 PlainTextDecoder::PlainTextDecoder()
32 , _includeTrailingWhitespace(true)
36 void PlainTextDecoder::setTrailingWhitespace(bool enable
)
38 _includeTrailingWhitespace
= enable
;
40 bool PlainTextDecoder::trailingWhitespace() const
42 return _includeTrailingWhitespace
;
44 void PlainTextDecoder::begin(QTextStream
* output
)
48 void PlainTextDecoder::end()
52 void PlainTextDecoder::decodeLine(const Character
* const characters
, int count
, LineProperty
/*properties*/
57 //TODO should we ignore or respect the LINE_WRAPPED line property?
59 //note: we build up a QString and send it to the text stream rather writing into the text
60 //stream a character at a time because it is more efficient.
61 //(since QTextStream always deals with QStrings internally anyway)
63 plainText
.reserve(count
);
65 int outputCount
= count
;
67 // if inclusion of trailing whitespace is disabled then find the end of the
69 if ( !_includeTrailingWhitespace
)
71 for (int i
= count
-1 ; i
>= 0 ; i
--)
73 if ( characters
[i
].character
!= ' ' )
80 for (int i
=0;i
<outputCount
;i
++)
82 plainText
.append( QChar(characters
[i
].character
) );
85 *_output
<< plainText
;
88 HTMLDecoder::HTMLDecoder() :
90 ,_colorTable(base_color_table
)
91 ,_innerSpanOpen(false)
92 ,_lastRendition(DEFAULT_RENDITION
)
97 void HTMLDecoder::begin(QTextStream
* output
)
103 //open monospace span
104 openSpan(text
,"font-family:monospace");
109 void HTMLDecoder::end()
123 //TODO: Support for LineProperty (mainly double width , double height)
124 void HTMLDecoder::decodeLine(const Character
* const characters
, int count
, LineProperty
/*properties*/
133 for (int i
=0;i
<count
;i
++)
135 QChar
ch(characters
[i
].character
);
137 //check if appearance of character is different from previous char
138 if ( characters
[i
].rendition
!= _lastRendition
||
139 characters
[i
].foregroundColor
!= _lastForeColor
||
140 characters
[i
].backgroundColor
!= _lastBackColor
)
142 if ( _innerSpanOpen
)
145 _lastRendition
= characters
[i
].rendition
;
146 _lastForeColor
= characters
[i
].foregroundColor
;
147 _lastBackColor
= characters
[i
].backgroundColor
;
149 //build up style string
152 if ( _lastRendition
& RE_BOLD
||
153 (_colorTable
&& characters
[i
].isBold(_colorTable
)) )
154 style
.append("font-weight:bold;");
157 if ( _lastRendition
& RE_UNDERLINE
)
158 style
.append("font-decoration:underline;");
160 //colours - a colour table must have been defined first
163 style
.append( QString("color:%1;").arg(_lastForeColor
.color(_colorTable
).name() ) );
165 if (!characters
[i
].isTransparent(_colorTable
))
167 style
.append( QString("background-color:%1;").arg(_lastBackColor
.color(_colorTable
).name() ) );
171 //open the span with the current style
172 openSpan(text
,style
);
173 _innerSpanOpen
= true;
183 //output current character
186 //escape HTML tag characters and just display others as they are
196 text
.append(" "); //HTML truncates multiple spaces, so use a space marker instead
201 //close any remaining open inner spans
202 if ( _innerSpanOpen
)
211 void HTMLDecoder::openSpan(QString
& text
, const QString
& style
)
213 text
.append( QString("<span style=\"%1\">").arg(style
) );
216 void HTMLDecoder::closeSpan(QString
& text
)
218 text
.append("</span>");
221 void HTMLDecoder::setColorTable(const ColorEntry
* table
)