1 | /* |
---|
2 | This file is part of Konsole, an X terminal. |
---|
3 | |
---|
4 | Copyright (C) 2006 by Robert Knight <robertknight@gmail.com> |
---|
5 | |
---|
6 | Rewritten for QT4 by e_k <e_k at users.sourceforge.net>, Copyright (C)2008 |
---|
7 | |
---|
8 | This program is free software; you can redistribute it and/or modify |
---|
9 | it under the terms of the GNU Lesser General Public License as published by |
---|
10 | the Free Software Foundation; either version 2 of the License, or |
---|
11 | (at your option) any later version. |
---|
12 | |
---|
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. |
---|
17 | |
---|
18 | You should have received a copy of the GNU Lesser General Public License |
---|
19 | along with this program; if not, write to the Free Software |
---|
20 | Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
---|
21 | 02110-1301 USA. |
---|
22 | */ |
---|
23 | |
---|
24 | // Own |
---|
25 | #include "TerminalCharacterDecoder.h" |
---|
26 | |
---|
27 | // Qt |
---|
28 | #include <QtCore/QTextStream> |
---|
29 | |
---|
30 | |
---|
31 | using namespace Konsole; |
---|
32 | |
---|
33 | PlainTextDecoder::PlainTextDecoder() |
---|
34 | : _output(0) |
---|
35 | , _includeTrailingWhitespace(true) |
---|
36 | { |
---|
37 | |
---|
38 | } |
---|
39 | void PlainTextDecoder::setTrailingWhitespace(bool enable) |
---|
40 | { |
---|
41 | _includeTrailingWhitespace = enable; |
---|
42 | } |
---|
43 | bool PlainTextDecoder::trailingWhitespace() const |
---|
44 | { |
---|
45 | return _includeTrailingWhitespace; |
---|
46 | } |
---|
47 | void PlainTextDecoder::begin(QTextStream* output) |
---|
48 | { |
---|
49 | _output = output; |
---|
50 | } |
---|
51 | void PlainTextDecoder::end() |
---|
52 | { |
---|
53 | _output = 0; |
---|
54 | } |
---|
55 | void PlainTextDecoder::decodeLine(const Character* const characters, int count, LineProperty /*properties*/ |
---|
56 | ) |
---|
57 | { |
---|
58 | Q_ASSERT( _output ); |
---|
59 | |
---|
60 | //TODO should we ignore or respect the LINE_WRAPPED line property? |
---|
61 | |
---|
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); |
---|
67 | |
---|
68 | int outputCount = count; |
---|
69 | |
---|
70 | // if inclusion of trailing whitespace is disabled then find the end of the |
---|
71 | // line |
---|
72 | if ( !_includeTrailingWhitespace ) |
---|
73 | { |
---|
74 | for (int i = count-1 ; i >= 0 ; i--) |
---|
75 | { |
---|
76 | if ( characters[i].character != ' ' ) |
---|
77 | break; |
---|
78 | else |
---|
79 | outputCount--; |
---|
80 | } |
---|
81 | } |
---|
82 | |
---|
83 | for (int i=0;i<outputCount;i++) |
---|
84 | { |
---|
85 | plainText.append( QChar(characters[i].character) ); |
---|
86 | } |
---|
87 | |
---|
88 | *_output << plainText; |
---|
89 | } |
---|
90 | |
---|
91 | HTMLDecoder::HTMLDecoder() : |
---|
92 | _output(0) |
---|
93 | ,_colorTable(base_color_table) |
---|
94 | ,_innerSpanOpen(false) |
---|
95 | ,_lastRendition(DEFAULT_RENDITION) |
---|
96 | { |
---|
97 | |
---|
98 | } |
---|
99 | |
---|
100 | void HTMLDecoder::begin(QTextStream* output) |
---|
101 | { |
---|
102 | _output = output; |
---|
103 | |
---|
104 | QString text; |
---|
105 | |
---|
106 | //open monospace span |
---|
107 | openSpan(text,"font-family:monospace"); |
---|
108 | |
---|
109 | *output << text; |
---|
110 | } |
---|
111 | |
---|
112 | void HTMLDecoder::end() |
---|
113 | { |
---|
114 | Q_ASSERT( _output ); |
---|
115 | |
---|
116 | QString text; |
---|
117 | |
---|
118 | closeSpan(text); |
---|
119 | |
---|
120 | *_output << text; |
---|
121 | |
---|
122 | _output = 0; |
---|
123 | |
---|
124 | } |
---|
125 | |
---|
126 | //TODO: Support for LineProperty (mainly double width , double height) |
---|
127 | void HTMLDecoder::decodeLine(const Character* const characters, int count, LineProperty /*properties*/ |
---|
128 | ) |
---|
129 | { |
---|
130 | Q_ASSERT( _output ); |
---|
131 | |
---|
132 | QString text; |
---|
133 | |
---|
134 | int spaceCount = 0; |
---|
135 | |
---|
136 | for (int i=0;i<count;i++) |
---|
137 | { |
---|
138 | QChar ch(characters[i].character); |
---|
139 | |
---|
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 ) |
---|
144 | { |
---|
145 | if ( _innerSpanOpen ) |
---|
146 | closeSpan(text); |
---|
147 | |
---|
148 | _lastRendition = characters[i].rendition; |
---|
149 | _lastForeColor = characters[i].foregroundColor; |
---|
150 | _lastBackColor = characters[i].backgroundColor; |
---|
151 | |
---|
152 | //build up style string |
---|
153 | QString style; |
---|
154 | |
---|
155 | if ( _lastRendition & RE_BOLD || |
---|
156 | (_colorTable && characters[i].isBold(_colorTable)) ) |
---|
157 | style.append("font-weight:bold;"); |
---|
158 | |
---|
159 | |
---|
160 | if ( _lastRendition & RE_UNDERLINE ) |
---|
161 | style.append("font-decoration:underline;"); |
---|
162 | |
---|
163 | //colours - a colour table must have been defined first |
---|
164 | if ( _colorTable ) |
---|
165 | { |
---|
166 | style.append( QString("color:%1;").arg(_lastForeColor.color(_colorTable).name() ) ); |
---|
167 | |
---|
168 | if (!characters[i].isTransparent(_colorTable)) |
---|
169 | { |
---|
170 | style.append( QString("background-color:%1;").arg(_lastBackColor.color(_colorTable).name() ) ); |
---|
171 | } |
---|
172 | } |
---|
173 | |
---|
174 | //open the span with the current style |
---|
175 | openSpan(text,style); |
---|
176 | _innerSpanOpen = true; |
---|
177 | } |
---|
178 | |
---|
179 | //handle whitespace |
---|
180 | if (ch.isSpace()) |
---|
181 | spaceCount++; |
---|
182 | else |
---|
183 | spaceCount = 0; |
---|
184 | |
---|
185 | |
---|
186 | //output current character |
---|
187 | if (spaceCount < 2) |
---|
188 | { |
---|
189 | //escape HTML tag characters and just display others as they are |
---|
190 | if ( ch == '<' ) |
---|
191 | text.append("<"); |
---|
192 | else if (ch == '>') |
---|
193 | text.append(">"); |
---|
194 | else |
---|
195 | text.append(ch); |
---|
196 | } |
---|
197 | else |
---|
198 | { |
---|
199 | text.append(" "); //HTML truncates multiple spaces, so use a space marker instead |
---|
200 | } |
---|
201 | |
---|
202 | } |
---|
203 | |
---|
204 | //close any remaining open inner spans |
---|
205 | if ( _innerSpanOpen ) |
---|
206 | closeSpan(text); |
---|
207 | |
---|
208 | //start new line |
---|
209 | text.append("<br>"); |
---|
210 | |
---|
211 | *_output << text; |
---|
212 | } |
---|
213 | |
---|
214 | void HTMLDecoder::openSpan(QString& text , const QString& style) |
---|
215 | { |
---|
216 | text.append( QString("<span style=\"%1\">").arg(style) ); |
---|
217 | } |
---|
218 | |
---|
219 | void HTMLDecoder::closeSpan(QString& text) |
---|
220 | { |
---|
221 | text.append("</span>"); |
---|
222 | } |
---|
223 | |
---|
224 | void HTMLDecoder::setColorTable(const ColorEntry* table) |
---|
225 | { |
---|
226 | _colorTable = table; |
---|
227 | } |
---|