Updating Doxygen styling and Licenses
[baulk.git] / src / Widgets / BaulkTerm / qtermwidget / TerminalCharacterDecoder.cpp
blobcb2b558b375659cf9e31c0786eb4ce0bc9316b90
1 // This file is part of Konsole, an X terminal.
2 //
3 // Copyright (C) 2006 by Robert Knight <robertknight@gmail.com>
4 //
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>
7 //
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/>.
21 // Own
22 #include "TerminalCharacterDecoder.h"
24 // Qt
25 #include <QtCore/QTextStream>
28 using namespace Konsole;
30 PlainTextDecoder::PlainTextDecoder()
31 : _output(0)
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)
46 _output = output;
48 void PlainTextDecoder::end()
50 _output = 0;
52 void PlainTextDecoder::decodeLine(const Character* const characters, int count, LineProperty /*properties*/
55 Q_ASSERT( _output );
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)
62 QString plainText;
63 plainText.reserve(count);
65 int outputCount = count;
67 // if inclusion of trailing whitespace is disabled then find the end of the
68 // line
69 if ( !_includeTrailingWhitespace )
71 for (int i = count-1 ; i >= 0 ; i--)
73 if ( characters[i].character != ' ' )
74 break;
75 else
76 outputCount--;
80 for (int i=0;i<outputCount;i++)
82 plainText.append( QChar(characters[i].character) );
85 *_output << plainText;
88 HTMLDecoder::HTMLDecoder() :
89 _output(0)
90 ,_colorTable(base_color_table)
91 ,_innerSpanOpen(false)
92 ,_lastRendition(DEFAULT_RENDITION)
97 void HTMLDecoder::begin(QTextStream* output)
99 _output = output;
101 QString text;
103 //open monospace span
104 openSpan(text,"font-family:monospace");
106 *output << text;
109 void HTMLDecoder::end()
111 Q_ASSERT( _output );
113 QString text;
115 closeSpan(text);
117 *_output << text;
119 _output = 0;
123 //TODO: Support for LineProperty (mainly double width , double height)
124 void HTMLDecoder::decodeLine(const Character* const characters, int count, LineProperty /*properties*/
127 Q_ASSERT( _output );
129 QString text;
131 int spaceCount = 0;
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 )
143 closeSpan(text);
145 _lastRendition = characters[i].rendition;
146 _lastForeColor = characters[i].foregroundColor;
147 _lastBackColor = characters[i].backgroundColor;
149 //build up style string
150 QString style;
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
161 if ( _colorTable )
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;
176 //handle whitespace
177 if (ch.isSpace())
178 spaceCount++;
179 else
180 spaceCount = 0;
183 //output current character
184 if (spaceCount < 2)
186 //escape HTML tag characters and just display others as they are
187 if ( ch == '<' )
188 text.append("&lt;");
189 else if (ch == '>')
190 text.append("&gt;");
191 else
192 text.append(ch);
194 else
196 text.append("&nbsp;"); //HTML truncates multiple spaces, so use a space marker instead
201 //close any remaining open inner spans
202 if ( _innerSpanOpen )
203 closeSpan(text);
205 //start new line
206 text.append("<br>");
208 *_output << text;
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)
223 _colorTable = table;