TerminalCharacterDecoder.cpp 9.17 KB
Newer Older
1 2
/*
    This file is part of Konsole, an X terminal.
3

4
    Copyright 2006-2008 by Robert Knight <robertknight@gmail.com>
5

6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU Lesser General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU Lesser General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
    02110-1301  USA.
*/

22 23 24
// Own
#include "TerminalCharacterDecoder.h"

25
// Qt
26
#include <QTextStream>
27

Robert Knight's avatar
 
Robert Knight committed
28 29
// Konsole
#include "konsole_wcwidth.h"
30
#include "ExtendedCharTable.h"
31
#include "ColorScheme.h"
Robert Knight's avatar
 
Robert Knight committed
32

33
using namespace Konsole;
34
PlainTextDecoder::PlainTextDecoder()
Kurt Hindenburg's avatar
Kurt Hindenburg committed
35
    : _output(nullptr)
36
    , _includeLeadingWhitespace(true)
Kurt Hindenburg's avatar
Kurt Hindenburg committed
37 38
    , _includeTrailingWhitespace(true)
    , _recordLinePositions(false)
39 40
{
}
41 42 43 44 45 46 47 48
void PlainTextDecoder::setLeadingWhitespace(bool enable)
{
    _includeLeadingWhitespace = enable;
}
bool PlainTextDecoder::leadingWhitespace() const
{
    return _includeLeadingWhitespace;
}
49 50 51 52 53 54 55 56
void PlainTextDecoder::setTrailingWhitespace(bool enable)
{
    _includeTrailingWhitespace = enable;
}
bool PlainTextDecoder::trailingWhitespace() const
{
    return _includeTrailingWhitespace;
}
57 58
void PlainTextDecoder::begin(QTextStream* output)
{
Kurt Hindenburg's avatar
Kurt Hindenburg committed
59
    _output = output;
Kurt Hindenburg's avatar
Kurt Hindenburg committed
60
    if (!_linePositions.isEmpty()) {
Kurt Hindenburg's avatar
Kurt Hindenburg committed
61
        _linePositions.clear();
Kurt Hindenburg's avatar
Kurt Hindenburg committed
62
    }
63 64
}
void PlainTextDecoder::end()
65
{
Kurt Hindenburg's avatar
Kurt Hindenburg committed
66
    _output = nullptr;
67
}
68 69 70

void PlainTextDecoder::setRecordLinePositions(bool record)
{
71
    _recordLinePositions = record;
72 73 74
}
QList<int> PlainTextDecoder::linePositions() const
{
75
    return _linePositions;
76
}
77
void PlainTextDecoder::decodeLine(const Character* const characters, int count, LineProperty /*properties*/
Kurt Hindenburg's avatar
Kurt Hindenburg committed
78
                                 )
79
{
Kurt Hindenburg's avatar
Kurt Hindenburg committed
80
    Q_ASSERT(_output);
81

82
    if (_recordLinePositions && (_output->string() != nullptr)) {
83 84 85
        int pos = _output->string()->count();
        _linePositions << pos;
    }
86

87
    //TODO should we ignore or respect the LINE_WRAPPED line property?
88

89 90 91 92 93
    //note:  we build up a QString and send it to the text stream rather writing into the text
    //stream a character at a time because it is more efficient.
    //(since QTextStream always deals with QStrings internally anyway)
    QString plainText;
    plainText.reserve(count);
94

95 96 97 98 99 100 101 102 103 104 105 106 107 108 109
    // If we should remove leading whitespace find the first non-space character
    int start = 0;
    if (!_includeLeadingWhitespace) {
        for (start = 0; start < count; start++) {
            if (!characters[start].isSpace()) {
                break;
            }
        }
    }

    int outputCount = count - start;

    if (outputCount <= 0) {
        return;
    }
110 111 112

    // if inclusion of trailing whitespace is disabled then find the end of the
    // line
Kurt Hindenburg's avatar
Kurt Hindenburg committed
113
    if (!_includeTrailingWhitespace) {
114
        for (int i = count - 1 ; i >= start ; i--) {
Kurt Hindenburg's avatar
Kurt Hindenburg committed
115
            if (!characters[i].isSpace()) {
116
                break;
Kurt Hindenburg's avatar
Kurt Hindenburg committed
117
            } else {
118
                outputCount--;
Kurt Hindenburg's avatar
Kurt Hindenburg committed
119
            }
120 121
        }
    }
122

123
    // find out the last technically real character in the line
124
    int realCharacterGuard = -1;
125
    for (int i = count - 1 ; i >= start ; i--) {
126 127 128
        // FIXME: the special case of '\n' here is really ugly
        // Maybe the '\n' should be added after calling this method in
        // Screen::copyLineToStream()
Kurt Hindenburg's avatar
Kurt Hindenburg committed
129
        if (characters[i].isRealCharacter && characters[i].character != '\n') {
130
            realCharacterGuard = i;
131 132 133 134
            break;
        }
    }

135
    for (int i = start; i < outputCount;) {
136
        if ((characters[i].rendition & RE_EXTENDED_CHAR) != 0) {
137 138
            ushort extendedCharLength = 0;
            const ushort* chars = ExtendedCharTable::instance.lookupExtendedChar(characters[i].character, extendedCharLength);
139
            if (chars != nullptr) {
140 141
                const QString s = QString::fromUtf16(chars, extendedCharLength);
                plainText.append(s);
142
                i += qMax(1, string_width(s));
143 144
            } else {
                ++i;
145
            }
Kurt Hindenburg's avatar
Kurt Hindenburg committed
146
        } else {
147 148 149 150 151 152 153
            // All characters which appear before the last real character are
            // seen as real characters, even when they are technically marked as
            // non-real.
            //
            // This feels tricky, but otherwise leading "whitespaces" may be
            // lost in some situation. One typical example is copying the result
            // of `dialog --infobox "qwe" 10 10` .
154
            if (characters[i].isRealCharacter || i <= realCharacterGuard) {
Kurt Hindenburg's avatar
Kurt Hindenburg committed
155 156 157
                plainText.append(QChar(characters[i].character));
                i += qMax(1, konsole_wcwidth(characters[i].character));
            } else {
158 159
                ++i;  // should we 'break' directly here?
            }
160
        }
161 162
    }
    *_output << plainText;
163 164 165
}

HTMLDecoder::HTMLDecoder() :
Kurt Hindenburg's avatar
Kurt Hindenburg committed
166
    _output(nullptr)
167
    , _colorTable(ColorScheme::defaultTable)
Kurt Hindenburg's avatar
Kurt Hindenburg committed
168 169
    , _innerSpanOpen(false)
    , _lastRendition(DEFAULT_RENDITION)
170 171 172
{
}

173 174 175 176 177 178
void HTMLDecoder::begin(QTextStream* output)
{
    _output = output;

    QString text;

179
    //open monospace span
180
    openSpan(text, QStringLiteral("font-family:monospace"));
181 182 183 184 185 186

    *output << text;
}

void HTMLDecoder::end()
{
Kurt Hindenburg's avatar
Kurt Hindenburg committed
187
    Q_ASSERT(_output);
188 189 190 191 192 193 194

    QString text;

    closeSpan(text);

    *_output << text;

Kurt Hindenburg's avatar
Kurt Hindenburg committed
195
    _output = nullptr;
196 197
}

198
//TODO: Support for LineProperty (mainly double width , double height)
199
void HTMLDecoder::decodeLine(const Character* const characters, int count, LineProperty /*properties*/
200
                            )
201
{
Kurt Hindenburg's avatar
Kurt Hindenburg committed
202
    Q_ASSERT(_output);
203

204 205 206
    QString text;

    int spaceCount = 0;
207

Kurt Hindenburg's avatar
Kurt Hindenburg committed
208
    for (int i = 0; i < count; i++) {
209
        //check if appearance of character is different from previous char
Kurt Hindenburg's avatar
Kurt Hindenburg committed
210 211 212
        if (characters[i].rendition != _lastRendition  ||
                characters[i].foregroundColor != _lastForeColor  ||
                characters[i].backgroundColor != _lastBackColor) {
213
            if (_innerSpanOpen) {
Kurt Hindenburg's avatar
Kurt Hindenburg committed
214
                closeSpan(text);
215 216
                _innerSpanOpen = false;
            }
217 218 219 220

            _lastRendition = characters[i].rendition;
            _lastForeColor = characters[i].foregroundColor;
            _lastBackColor = characters[i].backgroundColor;
221

222 223 224
            //build up style string
            QString style;

Jekyll Wu's avatar
Jekyll Wu committed
225
            //colors - a color table must have been defined first
226
            if (_colorTable != nullptr) {
227
                bool useBold = (_lastRendition & RE_BOLD) != 0;
Kurt Hindenburg's avatar
Kurt Hindenburg committed
228
                if (useBold) {
229
                    style.append(QLatin1String("font-weight:bold;"));
Kurt Hindenburg's avatar
Kurt Hindenburg committed
230
                }
231

Kurt Hindenburg's avatar
Kurt Hindenburg committed
232
                if ((_lastRendition & RE_UNDERLINE) != 0) {
233
                    style.append(QLatin1String("font-decoration:underline;"));
Kurt Hindenburg's avatar
Kurt Hindenburg committed
234
                }
235

236
                style.append(QStringLiteral("color:%1;").arg(_lastForeColor.color(_colorTable).name()));
237

238
                style.append(QStringLiteral("background-color:%1;").arg(_lastBackColor.color(_colorTable).name()));
239
            }
240

Kurt Hindenburg's avatar
Kurt Hindenburg committed
241 242
            //open the span with the current style
            openSpan(text, style);
243 244 245 246
            _innerSpanOpen = true;
        }

        //handle whitespace
Kurt Hindenburg's avatar
Kurt Hindenburg committed
247
        if (characters[i].isSpace()) {
248
            spaceCount++;
Kurt Hindenburg's avatar
Kurt Hindenburg committed
249
        } else {
250
            spaceCount = 0;
Kurt Hindenburg's avatar
Kurt Hindenburg committed
251
        }
252

253
        //output current character
Kurt Hindenburg's avatar
Kurt Hindenburg committed
254
        if (spaceCount < 2) {
255
            if ((characters[i].rendition & RE_EXTENDED_CHAR) != 0) {
256 257
                ushort extendedCharLength = 0;
                const ushort* chars = ExtendedCharTable::instance.lookupExtendedChar(characters[i].character, extendedCharLength);
258
                if (chars != nullptr) {
259 260
                    text.append(QString::fromUtf16(chars, extendedCharLength));
                }
Kurt Hindenburg's avatar
Kurt Hindenburg committed
261
            } else {
262 263
                //escape HTML tag characters and just display others as they are
                const QChar ch = characters[i].character;
Kurt Hindenburg's avatar
Kurt Hindenburg committed
264
                if (ch == QLatin1Char('<')) {
265
                    text.append(QLatin1String("&lt;"));
Kurt Hindenburg's avatar
Kurt Hindenburg committed
266
                } else if (ch == QLatin1Char('>')) {
267
                    text.append(QLatin1String("&gt;"));
268 269
                } else if (ch == QLatin1Char('&')) {
                    text.append(QLatin1String("&amp;"));
Kurt Hindenburg's avatar
Kurt Hindenburg committed
270
                } else {
271
                    text.append(ch);
Kurt Hindenburg's avatar
Kurt Hindenburg committed
272
                }
273
            }
Kurt Hindenburg's avatar
Kurt Hindenburg committed
274
        } else {
275 276
            // HTML truncates multiple spaces, so use a space marker instead
            // Use &#160 instead of &nbsp so xmllint will work.
277
            text.append(QLatin1String("&#160;"));
278 279 280 281
        }
    }

    //close any remaining open inner spans
282
    if (_innerSpanOpen) {
283
        closeSpan(text);
284 285
        _innerSpanOpen = false;
    }
286 287

    //start new line
288
    text.append(QLatin1String("<br>"));
289

290
    *_output << text;
291 292 293
}
void HTMLDecoder::openSpan(QString& text , const QString& style)
{
294
    text.append(QStringLiteral("<span style=\"%1\">").arg(style));
295 296 297 298
}

void HTMLDecoder::closeSpan(QString& text)
{
299
    text.append(QLatin1String("</span>"));
300 301 302 303
}

void HTMLDecoder::setColorTable(const ColorEntry* table)
{
304
    _colorTable = table;
305
}