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
/*
 * Copyright © 2018–2019 Egmont Koblinger
 *
 * This library 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.1 of the License, or (at your option) any later version.
 *
 * This library 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 */

#include <config.h>

#include "bidi.hh"
#include "debug.h"
#include "btedefines.hh"
#include "bteinternal.hh"

using namespace bte::base;

RingView::RingView()
{
        m_bidirunner = std::make_unique<BidiRunner>(this);
}

RingView::~RingView()
{
        pause();
}

/* Pausing a RingView frees up pretty much all of its memory.
 *
 * This is to be used when the terminal is unlikely to be painted or interacted with
 * in the near future, e.g. the widget is unmapped. Not to be called too frequently,
 * in order to avoid memory fragmentation.
 *
 * The RingView is resumed automatically on demand.
 */
void
RingView::pause()
{
        int i;

        if (m_paused)
                return;

        _bte_debug_print (BTE_DEBUG_RINGVIEW, "Ringview: pause, freeing %d rows, %d bidirows.\n",
                                              m_rows_alloc_len, m_bidirows_alloc_len);

        for (i = 0; i < m_rows_alloc_len; i++) {
                _bte_row_data_fini(m_rows[i]);
                g_free (m_rows[i]);
        }
        g_free (m_rows);
        m_rows_alloc_len = 0;

        for (i = 0; i < m_bidirows_alloc_len; i++) {
                delete m_bidirows[i];
        }
        g_free (m_bidirows);
        m_bidirows_alloc_len = 0;

        m_invalid = true;
        m_paused = true;
}

/* Allocate (again) the required memory. */
void
RingView::resume()
{
        g_assert_cmpint (m_len, >=, 1);

        /* +16: A bit of arbitrary heuristics to likely prevent a quickly following
         * realloc for the required context lines. */
        m_rows_alloc_len = m_len + 16;
        m_rows = (BteRowData **) g_malloc (sizeof (BteRowData *) * m_rows_alloc_len);
        for (int i = 0; i < m_rows_alloc_len; i++) {
                m_rows[i] = (BteRowData *) g_malloc (sizeof (BteRowData));
                _bte_row_data_init (m_rows[i]);
        }

        /* +2: Likely prevent a quickly following realloc.
         * The number of lines of interest keeps jumping up and down by one
         * due to per-pixel scrolling, and by another one due sometimes having
         * to reshuffle another line below the bottom for the overflowing bits
         * of the outline rectangle cursor. */
        m_bidirows_alloc_len = m_len + 2;
        m_bidirows = (BidiRow **) g_malloc (sizeof (BidiRow *) * m_bidirows_alloc_len);
        for (int i = 0; i < m_bidirows_alloc_len; i++) {
                m_bidirows[i] = new BidiRow();
        }

        _bte_debug_print (BTE_DEBUG_RINGVIEW, "Ringview: resume, allocating %d rows, %d bidirows\n",
                                              m_rows_alloc_len, m_bidirows_alloc_len);

        m_paused = false;
}

void
RingView::set_ring(Ring *ring)
{
        if (G_LIKELY (ring == m_ring))
                return;

        m_ring = ring;
        m_invalid = true;
}

void
RingView::set_width(bte::grid::column_t width)
{
        if (G_LIKELY (width == m_width))
                return;

        m_width = width;
        m_invalid = true;
}

void
RingView::set_rows(bte::grid::row_t start, bte::grid::row_t len)
{
        /* Force at least 1 row, see bug 134. */
        len = MAX(len, 1);

        if (start == m_start && len == m_len)
                return;

        /* With per-pixel scrolling, the desired viewport often shrinks by
         * one row at one end, and remains the same at the other end.
         * Save work by just keeping the current valid data in this case. */
        if (!m_invalid && start >= m_start && start + len <= m_start + m_len)
                return;

        /* m_rows is expanded on demand in update() */

        /* m_bidirows needs exactly this many lines */
        if (G_UNLIKELY (!m_paused && len > m_bidirows_alloc_len)) {
                int i = m_bidirows_alloc_len;
                while (len > m_bidirows_alloc_len) {
                        /* Don't realloc too aggressively. */
                        m_bidirows_alloc_len = std::max(m_bidirows_alloc_len + 1, m_bidirows_alloc_len * 5 / 4 /* whatever */);
                }
                _bte_debug_print (BTE_DEBUG_RINGVIEW, "Ringview: reallocate to %d bidirows\n",
                                                      m_bidirows_alloc_len);
                m_bidirows = (BidiRow **) g_realloc (m_bidirows, sizeof (BidiRow *) * m_bidirows_alloc_len);
                for (; i < m_bidirows_alloc_len; i++) {
                        m_bidirows[i] = new BidiRow();
                }
        }

        m_start = start;
        m_len = len;
        m_invalid = true;
}

BteRowData const*
RingView::get_row(bte::grid::row_t row) const
{
        g_assert_cmpint(row, >=, m_top);
        g_assert_cmpint(row, <, m_top + m_rows_len);

        return m_rows[row - m_top];
}

void
RingView::set_enable_bidi(bool enable_bidi)
{
        if (G_LIKELY (enable_bidi == m_enable_bidi))
                return;

        m_enable_bidi = enable_bidi;
        m_invalid = true;
}

void
RingView::set_enable_shaping(bool enable_shaping)
{
        if (G_LIKELY (enable_shaping == m_enable_shaping))
                return;

        m_enable_shaping = enable_shaping;
        m_invalid = true;
}

void
RingView::update()
{
        if (!m_invalid)
                return;
        if (m_paused)
                resume();

        /* Find the beginning of the topmost paragraph.
         *
         * Extract at most BTE_RINGVIEW_PARAGRAPH_LENGTH_MAX context rows.
         * If this safety limit is reached then together with the first
         * non-context row this paragraph fragment is already longer
         * than BTE_RINGVIEW_PARAGRAPH_LENGTH_MAX lines, and thus the
         * BiDi code will skip it. */
        bte::grid::row_t row = m_start;
        const BteRowData *row_data;

        _bte_debug_print (BTE_DEBUG_RINGVIEW, "Ringview: updating for [%ld..%ld] (%ld rows).\n",
                                              m_start, m_start + m_len - 1, m_len);

        int i = BTE_RINGVIEW_PARAGRAPH_LENGTH_MAX;
        while (i--) {
                if (!m_ring->is_soft_wrapped(row - 1))
                        break;
                row--;
        }

        /* Extract the data beginning at the found row.
         *
         * Extract at most BTE_RINGVIEW_PARAGRAPH_LENGTH_MAX rows
         * beyond the end of the specified area. Again, if this safety
         * limit is reached then together with the last non-context row
         * this paragraph fragment is already longer than
         * BTE_RINGVIEW_PARAGRAPH_LENGTH_MAX lines, and thus the
         * BiDi code will skip it. */
        m_top = row;
        m_rows_len = 0;
        while (row < m_start + m_len + BTE_RINGVIEW_PARAGRAPH_LENGTH_MAX) {
                if (G_UNLIKELY (m_rows_len == m_rows_alloc_len)) {
                        /* Don't realloc too aggressively. */
                        m_rows_alloc_len = std::max(m_rows_alloc_len + 1, m_rows_alloc_len * 5 / 4 /* whatever */);
                        _bte_debug_print (BTE_DEBUG_RINGVIEW, "Ringview: reallocate to %d rows\n",
                                                              m_rows_alloc_len);
                        m_rows = (BteRowData **) g_realloc (m_rows, sizeof (BteRowData *) * m_rows_alloc_len);
                        for (int j = m_rows_len; j < m_rows_alloc_len; j++) {
                                m_rows[j] = (BteRowData *) g_malloc (sizeof (BteRowData));
                                _bte_row_data_init (m_rows[j]);
                        }
                }

                row_data = _bte_ring_contains(m_ring, row) ? m_ring->index(row) : nullptr;
                if (G_LIKELY (row_data != nullptr)) {
                        _bte_row_data_copy (row_data, m_rows[m_rows_len]);
                        /* Make sure that the extracted data is not wider than the screen,
                         * something that can happen if the window was narrowed with rewrapping disabled.
                         * Also make sure that we won't end up with unfinished characters.
                         * FIXME remove this once bug 135 is addressed. */
                        if (G_UNLIKELY (_bte_row_data_length(m_rows[m_rows_len]) > m_width)) {
                                int j = m_width;
                                while (j > 0) {
                                        BteCell const* cell = _bte_row_data_get(m_rows[m_rows_len], j);
                                        if (!cell->attr.fragment())
                                                break;
                                        j--;
                                }
                                _bte_row_data_shrink(m_rows[m_rows_len], j);
                        }
                } else {
                        _bte_row_data_clear (m_rows[m_rows_len]);
                }
                m_rows_len++;
                row++;

                /* Once the bottom of the specified area is reached, stop at a hard newline. */
                if (row >= m_start + m_len && (!row_data || !row_data->attr.soft_wrapped))
                        break;
        }

        _bte_debug_print (BTE_DEBUG_RINGVIEW, "Ringview: extracted %ld+%ld context lines: [%ld..%ld] (%d rows).\n",
                                              m_start - m_top, (m_top + m_rows_len) - (m_start + m_len),
                                              m_top, m_top + m_rows_len - 1, m_rows_len);

        /* Loop through paragraphs of the extracted text, and do whatever we need to do on each paragraph. */
        auto top = m_top;
        row = top;
        while (row < m_top + m_rows_len) {
                row_data = m_rows[row - m_top];
                if (!row_data->attr.soft_wrapped || row == m_top + m_rows_len - 1) {
                        /* Found a paragraph from @top to @row, inclusive. */

                        /* Run the BiDi algorithm. */
                        m_bidirunner->paragraph(top, row + 1,
                                                m_enable_bidi, m_enable_shaping);

                        /* Doing syntax highlighting etc. come here in the future. */

                        top = row + 1;
                }
                row++;
        }

        m_invalid = false;
}

BidiRow const* RingView::get_bidirow(bte::grid::row_t row) const
{
        g_assert_cmpint (row, >=, m_start);
        g_assert_cmpint (row, <, m_start + m_len);
        g_assert_false (m_invalid);
        g_assert_false (m_paused);

        return m_bidirows[row - m_start];
}

/* For internal use by BidiRunner. Get where the BiDi mapping for the given row
 * needs to be stored, of nullptr if it's a context row. */
BidiRow* RingView::get_bidirow_writable(bte::grid::row_t row) const
{
        if (row < m_start || row >= m_start + m_len)
                return nullptr;

        return m_bidirows[row - m_start];
}