Make a branch to make krunner Good Enough For Aaron™.
[kdebase/uwolfer.git] / apps / konsole / src / TerminalCharacterDecoder.cpp
blob23a736920c32ac40337a79fc202370a2aa525dcd
1 /*
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
19 02110-1301 USA.
22 // Own
23 #include "TerminalCharacterDecoder.h"
25 // Qt
26 #include <QtCore/QTextStream>
28 // KDE
29 #include <kdebug.h>
31 using namespace Konsole;
33 PlainTextDecoder::PlainTextDecoder()
34 : _output(0)
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)
49 _output = output;
51 void PlainTextDecoder::end()
53 _output = 0;
55 void PlainTextDecoder::decodeLine(const Character* const characters, int count, LineProperty /*properties*/
58 Q_ASSERT( _output );
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)
65 QString plainText;
66 plainText.reserve(count);
68 int outputCount = count;
70 // if inclusion of trailing whitespace is disabled then find the end of the
71 // line
72 if ( !_includeTrailingWhitespace )
74 for (int i = count-1 ; i >= 0 ; i--)
76 if ( characters[i].character != ' ' )
77 break;
78 else
79 outputCount--;
83 for (int i=0;i<outputCount;i++)
85 plainText.append( QChar(characters[i].character) );
88 *_output << plainText;
91 HTMLDecoder::HTMLDecoder() :
92 _output(0)
93 ,_colorTable(base_color_table)
94 ,_innerSpanOpen(false)
95 ,_lastRendition(DEFAULT_RENDITION)
100 void HTMLDecoder::begin(QTextStream* output)
102 _output = output;
104 QString text;
106 //open monospace span
107 openSpan(text,"font-family:monospace");
109 *output << text;
112 void HTMLDecoder::end()
114 Q_ASSERT( _output );
116 QString text;
118 closeSpan(text);
120 *_output << text;
122 _output = 0;
126 //TODO: Support for LineProperty (mainly double width , double height)
127 void HTMLDecoder::decodeLine(const Character* const characters, int count, LineProperty /*properties*/
130 Q_ASSERT( _output );
132 QString text;
134 int spaceCount = 0;
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 )
146 closeSpan(text);
148 _lastRendition = characters[i].rendition;
149 _lastForeColor = characters[i].foregroundColor;
150 _lastBackColor = characters[i].backgroundColor;
152 //build up style string
153 QString style;
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
164 if ( _colorTable )
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;
179 //handle whitespace
180 if (ch.isSpace())
181 spaceCount++;
182 else
183 spaceCount = 0;
186 //output current character
187 if (spaceCount < 2)
189 //escape HTML tag characters and just display others as they are
190 if ( ch == '<' )
191 text.append("&lt;");
192 else if (ch == '>')
193 text.append("&gt;");
194 else
195 text.append(ch);
197 else
199 text.append("&nbsp;"); //HTML truncates multiple spaces, so use a space marker instead
204 //close any remaining open inner spans
205 if ( _innerSpanOpen )
206 closeSpan(text);
208 //start new line
209 text.append("<br>");
211 *_output << text;
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)
226 _colorTable = table;