File: tileweb-text.cc

package info (click to toggle)
crawl 2%3A0.23.0-1
  • links: PTS
  • area: main
  • in suites: buster
  • size: 55,948 kB
  • sloc: cpp: 303,973; ansic: 28,797; python: 4,074; perl: 3,247; makefile: 1,632; java: 792; sh: 327; objc: 250; xml: 32; cs: 15; sed: 9; lisp: 3
file content (201 lines) | stat: -rw-r--r-- 4,458 bytes parent folder | download | duplicates (2)
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

#include "AppHdr.h"

#ifdef USE_TILE_WEB

#include "tileweb-text.h"

#include "stringutil.h"
#include "tiles-build-specific.h"
#include "unicode.h"

WebTextArea::WebTextArea(string name) :
    mx(0),
    my(0),
    m_cbuf(nullptr),
    m_abuf(nullptr),
    m_old_cbuf(nullptr),
    m_old_abuf(nullptr),
    m_client_side_name(name),
    m_dirty(true)
{
}

WebTextArea::~WebTextArea()
{
    if (m_cbuf != nullptr)
    {
        delete[] m_cbuf;
        delete[] m_abuf;
        delete[] m_old_cbuf;
        delete[] m_old_abuf;
    }
}

void WebTextArea::resize(int x, int y)
{
    if (mx == x && my == y)
        return;

    mx = x;
    my = y;

    if (m_cbuf != nullptr)
    {
        delete[] m_cbuf;
        delete[] m_abuf;
        delete[] m_old_cbuf;
        delete[] m_old_abuf;
    }

    int size = mx * my;
    m_cbuf = new char32_t[size];
    m_abuf = new uint8_t[size];
    m_old_cbuf = new char32_t[size];
    m_old_abuf = new uint8_t[size];

    for (int i = 0; i < mx * my; i++)
    {
        m_cbuf[i] = ' ';
        m_abuf[i] = 0;
        m_old_cbuf[i] = ' ';
        m_old_abuf[i] = 0;
    }

    m_dirty = true;

    on_resize();
}

void WebTextArea::clear()
{
    for (int i = 0; i < mx * my; i++)
    {
        m_cbuf[i] = ' ';
        m_abuf[i] = 0;
    }

    m_dirty = true;
}

void WebTextArea::put_character(char32_t chr, int fg, int bg, int x, int y)
{
    ASSERT_RANGE(x, 0, mx);
    ASSERT_RANGE(y, 0, my);
    uint8_t col = (fg & 0xf) + (bg << 4);

    if (m_cbuf[x + y * mx] != chr || m_abuf[x + y * mx] != col)
        m_dirty = true;

    m_cbuf[x + y * mx] = chr;
    m_abuf[x + y * mx] = col;
}

void WebTextArea::send(bool force)
{
    if (m_cbuf == nullptr) return;
    if (!force && !m_dirty) return;
    m_dirty = false;

    int last_col = -1;
    int space_count = 0;
    bool dirty = false;
    string html;

    bool sending = false;

    for (int y = 0; y < my; ++y)
    {
        last_col = -1;
        space_count = 0;
        dirty = false;
        html.clear();

        for (int x = 0; x < mx; ++x)
        {
            char32_t chr = m_cbuf[x + y * mx];
            uint8_t col = m_abuf[x + y * mx];

            if (chr != m_old_cbuf[x + y * mx] ||
                col != m_old_abuf[x + y * mx])
            {
                dirty = true;
                m_old_cbuf[x + y * mx] = chr;
                m_old_abuf[x + y * mx] = col;
            }

            if (chr != ' ' || ((col >> 4) & 0xF) != 0)
            {
                while (space_count)
                {
                    html.push_back(' ');
                    space_count--;
                }
            }

            if (col != last_col && chr != ' ')
            {
                if (last_col != -1)
                    html += "</span>";
                html += make_stringf("<span class=\\\"fg%d bg%d\\\">",
                                     col & 0xf, (col >> 4) & 0xf);
                last_col = col;
            }

            if (chr == ' ' && ((col >> 4) & 0xF) == 0)
                space_count++;
            else
            {
                switch (chr)
                {
                case '<':
                    html += "&lt;";
                    break;
                case '>':
                    html += "&gt;";
                    break;
                case '&':
                    html += "&amp;";
                    break;
                case '\\':
                    html += "\\\\";
                    break;
                case '"':
                    html += "&quot;";
                    break;
                default:
                    char buf[5];
                    buf[wctoutf8(buf, chr)] = 0;
                    html += buf;
                    break;
                }
            }
        }

        if (dirty || (force && !html.empty()))
        {
            if (!sending)
            {
                tiles.write_message("{\"msg\":\"txt\",\"id\":\"%s\"",
                                    m_client_side_name.c_str());
                if (force)
                    tiles.write_message(",\"clear\":true");
                tiles.write_message(",\"lines\":{");
                sending = true;
            }

            tiles.json_write_comma();
            tiles.write_message("\"%u\":\"%s\"", y, html.c_str());
        }
    }
    if (sending)
    {
        tiles.write_message("}}");
        tiles.finish_message();
    }
}

void WebTextArea::on_resize()
{
}
#endif