2 This file is part of Konsole, an X terminal.
4 Copyright (C) 2006 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>
31 using namespace Konsole
;
33 PlainTextDecoder::PlainTextDecoder()
35 , _includeTrailingWhitespace(true)
39 void PlainTextDecoder::setTrailingWhitespace(bool enable
)
41 _includeTrailingWhitespace
= enable
;
43 bool PlainTextDecoder::trailingWhitespace() const
45 return _includeTrailingWhitespace
;
47 void PlainTextDecoder::begin(QTextStream
* output
)
51 void PlainTextDecoder::end()
55 void PlainTextDecoder::decodeLine(const Character
* const characters
, int count
, LineProperty
/*properties*/
60 //TODO should we ignore or respect the LINE_WRAPPED line property?
62 //note: we build up a QString and send it to the text stream rather writing into the text
63 //stream a character at a time because it is more efficient.
64 //(since QTextStream always deals with QStrings internally anyway)
66 plainText
.reserve(count
);
68 int outputCount
= count
;
70 // if inclusion of trailing whitespace is disabled then find the end of the
72 if ( !_includeTrailingWhitespace
)
74 for (int i
= count
-1 ; i
>= 0 ; i
--)
76 if ( characters
[i
].character
!= ' ' )
83 for (int i
=0;i
<outputCount
;i
++)
85 plainText
.append( QChar(characters
[i
].character
) );
88 *_output
<< plainText
;
91 HTMLDecoder::HTMLDecoder() :
93 ,_colorTable(base_color_table
)
94 ,_innerSpanOpen(false)
95 ,_lastRendition(DEFAULT_RENDITION
)
100 void HTMLDecoder::begin(QTextStream
* output
)
106 //open monospace span
107 openSpan(text
,"font-family:monospace");
112 void HTMLDecoder::end()
126 //TODO: Support for LineProperty (mainly double width , double height)
127 void HTMLDecoder::decodeLine(const Character
* const characters
, int count
, LineProperty
/*properties*/
136 for (int i
=0;i
<count
;i
++)
138 QChar
ch(characters
[i
].character
);
140 //check if appearance of character is different from previous char
141 if ( characters
[i
].rendition
!= _lastRendition
||
142 characters
[i
].foregroundColor
!= _lastForeColor
||
143 characters
[i
].backgroundColor
!= _lastBackColor
)
145 if ( _innerSpanOpen
)
148 _lastRendition
= characters
[i
].rendition
;
149 _lastForeColor
= characters
[i
].foregroundColor
;
150 _lastBackColor
= characters
[i
].backgroundColor
;
152 //build up style string
155 if ( _lastRendition
& RE_BOLD
||
156 (_colorTable
&& characters
[i
].isBold(_colorTable
)) )
157 style
.append("font-weight:bold;");
160 if ( _lastRendition
& RE_UNDERLINE
)
161 style
.append("font-decoration:underline;");
163 //colours - a colour table must have been defined first
166 style
.append( QString("color:%1;").arg(_lastForeColor
.color(_colorTable
).name() ) );
168 if (!characters
[i
].isTransparent(_colorTable
))
170 style
.append( QString("background-color:%1;").arg(_lastBackColor
.color(_colorTable
).name() ) );
174 //open the span with the current style
175 openSpan(text
,style
);
176 _innerSpanOpen
= true;
186 //output current character
189 //escape HTML tag characters and just display others as they are
199 text
.append(" "); //HTML truncates multiple spaces, so use a space marker instead
204 //close any remaining open inner spans
205 if ( _innerSpanOpen
)
214 void HTMLDecoder::openSpan(QString
& text
, const QString
& style
)
216 text
.append( QString("<span style=\"%1\">").arg(style
) );
219 void HTMLDecoder::closeSpan(QString
& text
)
221 text
.append("</span>");
224 void HTMLDecoder::setColorTable(const ColorEntry
* table
)