aboutsummaryrefslogtreecommitdiff
path: root/src/renderers/renderhelpers.cpp
blob: c9ffc7e74fe5c9109941651bf6043d5f3dbf3f96 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
/**
 * @TODO I'm hardcoding this to ANSI for now, as it's the most common.
 *       Ideally we should have configurable control characters for various
 *       types of terminals that could be emulated via a standard termcap file.
 * @NOTE ANSI escape sequence reference:
 *       https://en.wikipedia.org/wiki/ANSI_escape_code#Escape_sequences
 *       Note that escape sequences for other terminal types are a thing, and
 *       currently aren't implemented yet as mentioned in the TODO above, eg:
 *       https://en.wikipedia.org/wiki/VT52#Escape_sequences
 */
#include "renderhelpers.hpp"
#include "kristall.hpp"

#include <QByteArray>
#include <QString>
#include <QTextCursor>
#include <QTextFrame>
#include <QTextFrameFormat>
#include <QtGlobal>

#include <string>
#include <iostream>

static constexpr const char escapeString = '\033';
bool inverted{false};

static void setColor(QTextCharFormat& format, unsigned char n, bool bg=false)
{
    QColor color;

    if (n < 16)
    {
        // The normal pre-defined typical 16 colors.
        color = QColor(kristall::globals().document_style.ansi_colors[n]);
    }
    else if (n < 232)
    {
        // indexed 8-bit rgb color pallete.
        unsigned int index_R = ((n - 16) / 36);
        unsigned char r = index_R > 0 ? 55 + index_R * 40 : 0;
        unsigned int index_G = (((n - 16) % 36) / 6);
        unsigned char g = index_G > 0 ? 55 + index_G * 40 : 0;
        unsigned int index_B = ((n - 16) % 6);
        unsigned char b = index_B > 0 ? 55 + index_B * 40 : 0;

        color = QColor(r, g, b);
    }
    else
    {
        // grayscale pallete.
        unsigned char g = (n - 232) * 10 + 8;
        color = QColor(g, g, g);
    }

    if (bg)
        format.setBackground(color);
    else
        format.setForeground(color);
}

static QString parseNumber(const QString& input, QString::const_iterator& it)
{
    QString result;
    while (it != input.cend())
    {
        const auto currentCharacter = *it;
        if (!currentCharacter.isNumber()) break;
        result += currentCharacter;
        ++it;
    }
    return result;
}

static void parseSGR(
    std::vector<unsigned char>& args,
    const QString& input,
    QString::const_iterator& it,
    QTextCharFormat& format,
    const QTextCharFormat& defaultFormat,
    QTextCursor& cursor)
{
    if (args.empty()) return;
    for (auto it = args.cbegin(); it != args.cend(); ++it)
    {
        /// @TODO A whole bunch of unimplemented SGR codes are unimplemented
        ///       yet (eg: blink or font switching)
        enum {
            Reset = 0, Bold, Light, Italic, Underline,
            Reverse = 7,
            StrikeOut = 9,

            // some implementations interpret 21 as Bold off
            DoubleUnderline = 21, NormalWeight, ItalicOff, UnderlineOff,
            ReverseOff = 27,
            StrikeOutOff = 29,

            // 30-37 and 40-47 color codes are handled in the default case
            SetForeground = 38,
            DefaultForeground = 39,
            SetBackground = 48,
            DefaultBackground = 49,
        };

        const auto arg = *it;
        switch(arg)
        {
            case Reset:
                format = defaultFormat;
                break;
            case Bold:
                format.setFontWeight(QFont::Bold);
                break;
            case Light:
                format.setFontWeight(QFont::Light);
                break;
            case Italic:
                format.setFontItalic(true);
                break;
            case Underline:
                /// @TODO Underline style should be configurable?
                format.setUnderlineStyle(QTextCharFormat::SingleUnderline);
                break;
            case Reverse:
                if (!inverted)
                {
                    const auto fg = format.foreground();
                    const auto bg = format.background();
                    format.setForeground(bg);
                    format.setBackground(fg);
                    inverted = true;
                }
                break;
            case StrikeOut:
                format.setFontStrikeOut(true);
                break;
            case DoubleUnderline:
                format.setUnderlineStyle(QTextCharFormat::WaveUnderline);
                break;
            case NormalWeight:
                format.setFontWeight(QFont::Normal);
                break;
            case ItalicOff:
                format.setFontItalic(false);
                break;
            case UnderlineOff:
                format.setFontUnderline(QTextCharFormat::NoUnderline);
                break;
            case ReverseOff:
                if (inverted)
                {
                    const auto fg = format.foreground();
                    const auto bg = format.background();
                    format.setForeground(bg);
                    format.setBackground(fg);
                    inverted = false;
                }
                break;
            case StrikeOutOff:
                format.setFontStrikeOut(false);
                break;
            case SetForeground:
                if (args.size() > 2 && std::next(it) != args.cend())
                {
                    const auto colMode = *++it;
                    if (colMode == 5 && std::next(it) != args.cend())
                    {
                        const auto colNum = *++it;
                        setColor(format, colNum);
                    }
                    else if (colMode == 2)
                    {
                        ++it;
                        if (std::next(it, 3) <= args.cend())
                        {
                            const auto red = *it;
                            const auto green = *++it;
                            const auto blue = *++it;
                            format.setForeground(QColor(red, green, blue));
                        }
                    }
                }
                break;
            case DefaultForeground:
                format.setForeground(defaultFormat.foreground());
                break;
            case SetBackground:
                if (args.size() > 2 && std::next(it) != args.cend())
                {
                    const auto colMode = *++it;
                    if (colMode == 5 && std::next(it) != args.cend())
                    {
                        const auto colNum = *++it;
                        setColor(format, colNum, true);
                    }
                    else if (colMode == 2)
                    {
                        ++it;
                        if (std::next(it, 3) <= args.cend())
                        {
                            const auto red = *it;
                            const auto green = *++it;
                            const auto blue = *++it;
                            format.setBackground(QColor(red, green, blue));
                        }
                    }
                }
                break;
            case DefaultBackground:
                format.setBackground(defaultFormat.background());
                break;
            default:
                // foreground, background and their bright equivalents
                if (arg >= 30 && arg < 38)
                    setColor(format, arg - 30);
                else if (arg >= 40 && arg < 48)
                    setColor(format, arg - 40, true);
                else if (arg >= 90 && arg < 98)
                    setColor(format, arg - 90 + 8);
                else if (arg >= 100 && arg < 108)
                    setColor(format, arg - 100 + 8, true);

                break;
        }
    }
}

static std::vector<unsigned char> parseNumericArguments(const QString& input, QString::const_iterator& it)
{
    std::vector<unsigned char> result;
    while (it != input.end())
    {
        const auto currentCharacter = *it;
        const auto numStr = parseNumber(input, it);
        if (numStr.isEmpty())
        {
            if (!(currentCharacter == ' ' || currentCharacter == ';'))
            {
                break;
            }
        }
        else
        {
            result.emplace_back(numStr.toShort());
            continue;
        }
        ++it;
    }
    return result;
}

static void parseCSI(
    const QString& input,
    QString::const_iterator& it,
    QTextCharFormat& format,
    const QTextCharFormat& defaultFormat,
    QTextCursor& cursor)
{
    std::vector<unsigned char> numericArguments = parseNumericArguments(input, it);
    char numericArgument = numericArguments.empty() ? 1 : numericArguments[0];
    if (it != input.cend())
    {
        const auto code = (*it).unicode();
        switch(code)
        {
            case 'A': // cursor up
                cursor.movePosition(QTextCursor::Up, QTextCursor::MoveAnchor, numericArgument);
                break;
            case 'B': // cursor down
                cursor.movePosition(QTextCursor::Down, QTextCursor::MoveAnchor, numericArgument);
                break;
            case 'C': // cursor forward
                cursor.movePosition(QTextCursor::Right, QTextCursor::MoveAnchor, numericArgument);
                break;
            case 'D': // cursor back
                cursor.movePosition(QTextCursor::Left, QTextCursor::MoveAnchor, numericArgument);
                break;
            case 'E': // cursor next line
                cursor.movePosition(QTextCursor::Down, QTextCursor::MoveAnchor, numericArgument);
                cursor.movePosition(QTextCursor::StartOfLine, QTextCursor::MoveAnchor);
                break;
            case 'F': // cursor previous line
                cursor.movePosition(QTextCursor::Up, QTextCursor::MoveAnchor, numericArgument);
                cursor.movePosition(QTextCursor::StartOfLine, QTextCursor::MoveAnchor);
                break;
            case 'G': // cursor horizontal absolute position set
                cursor.movePosition(QTextCursor::StartOfLine, QTextCursor::MoveAnchor);
                cursor.movePosition(QTextCursor::Right, QTextCursor::MoveAnchor, numericArgument);
                break;
            case 'H': // Set cursor position ( row;col )
                if (numericArguments.size() > 1)
                {
                    cursor.setPosition(0, QTextCursor::MoveAnchor);
                    cursor.movePosition(QTextCursor::Down, QTextCursor::MoveAnchor, numericArguments[0]);
                    cursor.movePosition(QTextCursor::Right, QTextCursor::MoveAnchor, numericArguments[1]);
                }
                break;
            case 'J': // Erase in display
                /// @TODO ANSI escape: implement erase in display.
                break;
            case 'K': // Erase in line
                /// @TODO ANSI escape: implement erase in line.
                break;
            case 'S': // Scroll up
                /// @TODO ANSI escape: implement scroll up.
                break;
            case 'T': // Scroll down
                /// @TODO ANSI escape: implement scroll down.
                break;
            case 'f': // Horizontal vertical position
                if (numericArguments.size() > 1)
                {
                    cursor.setPosition(0, QTextCursor::MoveAnchor);
                    cursor.movePosition(QTextCursor::Down, QTextCursor::MoveAnchor, numericArguments[0]);
                    cursor.movePosition(QTextCursor::Right, QTextCursor::MoveAnchor, numericArguments[1]);
                }
                break;
            case 'm': // SGR
                parseSGR(numericArguments, input, it, format, defaultFormat, cursor);
                break;
            default:
                // Stuff we ignore: CSI 5i, CSI 4i, CSI 6n.
                // We do care about SGR (CSI n <m>)
                break;
        }
    }
}

// Replaces CR-LF line endings with pure LF endings,
// and then replaces any remaining CRs with LFs.
static QString cleanLineEndings(QString &input)
{
    // Replace all CR-LF with LF
    input.replace("\r\n", "\n");

    // Replace stray CRs with LF
    input.replace("\r", "\n");

    return input;
}

void renderhelpers::renderEscapeCodes(const QByteArray &input,
    QTextCharFormat& format, const QTextCharFormat& defaultFormat, QTextCursor& cursor)
{
    QString inputString = QString::fromUtf8(input);
    cleanLineEndings(inputString);

    // Don't render escapes if set to 'ignore'
    if (kristall::globals().options.ansi_escapes == AnsiEscRenderMode::ignore)
    {
        cursor.insertText(input, defaultFormat);
        return;
    }

    const auto tokens = input.split(escapeString);
    for (QString::const_iterator it = inputString.cbegin(); it != inputString.cend(); ++it)
    {
        const auto currentCharacter = *it;;
        if (currentCharacter == escapeString)
        {
            it++;
            const auto escSequence = *it;
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
            if (escSequence == "[")
#else
            if (escSequence == QChar::fromLatin1('['))
#endif
            {
                it++;
                parseCSI(input, it, format, defaultFormat, cursor);
            }
        }
        else if (kristall::globals().options.ansi_escapes == AnsiEscRenderMode::strip)
        {
            // 'strip' mode -> we still interpret escapes as above, but just render
            // text in the default format.
            cursor.insertText(currentCharacter, defaultFormat);
        }
        else
        {
            // 'render' mode -> we use the interpreted ANSI format
            cursor.insertText(currentCharacter, format);
        }
    }
}

QByteArray renderhelpers::replace_quotes(QByteArray &line)
{
    if (!kristall::globals().options.fancy_quotes)
        return line;

    int last_d = -1,
        last_s = -1;

    for (int i = 0; i < line.length(); ++i)
    {
        // Double quotes
        if (line[i] == '"')
        {
            if (last_d == -1)
            {
                last_d = i;
            }
            else
            {
                // Replace quote at first position:
                QByteArray first = QString("“").toUtf8();
                line.replace(last_d, 1, first);

                // Replace quote at second position:
                line.replace(i + first.size() - 1, 1, QString("”").toUtf8());

                last_d = -1;
            }
        }
        else if (line[i] == '\'')
        {
            if (last_s == -1)
            {
                // Skip if it looks like a contraction rather
                // than a quote.
                if (i > 0 && line[i - 1] != ' ')
                {
                    line.replace(i, 1, QString("’").toUtf8());
                    continue;
                }

                // For shortenings like 'till
                int len = line.length();
                if ((i + 1) < len && line[i + 1] != ' ')
                {
                    line.replace(i, 1, QString("‘").toUtf8());
                    continue;
                }

                last_s = i;
            }
            else
            {
                // Replace quote at first position:
                QByteArray first = QString("‘").toUtf8();
                line.replace(last_s, 1, first);

                // Replace quote at second position:
                line.replace(i + first.size() - 1, 1, QString("’").toUtf8());

                last_s = -1;
            }
        }
    }

    return line;
}

void renderhelpers::setPageMargins(QTextDocument *doc, int mh, int mv)
{
    QTextFrame *root = doc->rootFrame();
    QTextFrameFormat fmt = root->frameFormat();
    fmt.setLeftMargin(mh);
    fmt.setRightMargin(mh);
    fmt.setTopMargin(mv);
    fmt.setBottomMargin(mv);
    root->setFrameFormat(fmt);
}