~aleteoryx/muditaos

ref: 7fbaf735ed99f5b0d26adb686cfa6ed2bb4267d5 muditaos/module-gui/gui/widgets/TextBlockCursor.cpp -rw-r--r-- 9.7 KiB
7fbaf735 — Przemyslaw Brudny [EGD-7813] Option Window titles localizations fix 4 years ago
                                                                                
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
// Copyright (c) 2017-2021, Mudita Sp. z.o.o. All rights reserved.
// For licensing, see https://github.com/mudita/MuditaOS/LICENSE.md

#include "TextBlockCursor.hpp"
#include "TextBlock.hpp"
#include "TextDocument.hpp"
#include "TextParse.hpp"
#include <log/log.hpp>
#include <cassert>

static const int last_char_inclusive = 0; // if then -1 / else 0

#define debug_cursor(...)
// #define debug_cursor(...) LOG_DEBUG(__VA_ARGS__)

namespace gui
{
    auto BlockCursor::currentBlock() const -> std::list<TextBlock>::iterator
    {
        if (currentBlockNumber == text::npos) {
            return document->blocks.end();
        }
        return std::next(document->blocks.begin(), currentBlockNumber);
    }

    auto BlockCursor::blocksEnd() const -> std::list<TextBlock>::iterator
    {
        return std::end(document->blocks);
    }

    auto BlockCursor::blocksBegin() const -> std::list<TextBlock>::iterator
    {
        return std::begin(document->blocks);
    }

    BlockCursor::BlockCursor(TextDocument *document,
                             unsigned int blockPosition,
                             unsigned int blockNumber,
                             RawFont *default_font)
        : document(document), default_font(default_font)
    {
        this->pos                = text::npos;
        this->currentBlockNumber = text::npos;
        if (checkDocument()) {
            this->currentBlockNumber =
                blockNumber < document->blocks.size() ? blockNumber : document->blocks.size() - 1;
            auto blockLength = std::next(document->blocks.begin(), this->currentBlockNumber)->length();
            this->pos        = blockPosition < blockLength ? blockPosition : blockLength + last_char_inclusive;
        }
    }

    [[nodiscard]] auto BlockCursor::checkNpos() const -> bool
    {
        return pos == text::npos || currentBlockNumber == text::npos;
    }

    void BlockCursor::resetNpos()
    {
        if (pos == text::npos)
            pos = 0;
        if (currentBlockNumber == text::npos)
            currentBlockNumber = 0;
    }

    [[nodiscard]] auto BlockCursor::checkDocument() const -> bool
    {
        return document != nullptr && document->blocks.size() > 0;
    }

    auto BlockCursor::atEnd() const -> bool
    {
        if (!checkDocument() || checkNpos()) {
            return false;
        }

        auto lastBlock = document->blocks.back();

        return currentBlockNumber == document->blocks.size() - 1 &&
               pos >= lastBlock.length() + (lastBlock.getEnd() != TextBlock::End::Newline ? last_char_inclusive : -1);
    }

    auto BlockCursor::operator+=(unsigned int val) -> BlockCursor &
    {
        // just use operator ++
        for (unsigned int i = 0; i < val; i++) {
            ++*this;
        }

        return *this;
    }

    BlockCursor &BlockCursor::operator++()
    {
        if (!checkDocument()) {
            return *this;
        }

        resetNpos();

        bool endOfCurrentBlockReached = false;

        debug_cursor("Addition: Position: %d, Current block number %d", pos, currentBlockNumber);

        size_t blockSize = currentBlock()->length();

        // Skip newline at end
        if (currentBlock()->getEnd() == TextBlock::End::Newline && (pos + 1) == blockSize) {
            endOfCurrentBlockReached = true;
        }
        else if (pos >= blockSize) {
            endOfCurrentBlockReached = true;
        }

        bool lastBlockReached = currentBlockNumber + 1 == document->blocks.size();

        if (endOfCurrentBlockReached && lastBlockReached) {
            return *this;
        }

        if (endOfCurrentBlockReached) {
            return goToNextBlock();
        }

        pos += 1;

        return *this;
    }

    auto BlockCursor::operator-=(unsigned int val) -> BlockCursor &
    {
        // just use operator --
        for (unsigned int i = 0; i < val; ++i, --*this) {}
        return *this;
    }

    BlockCursor &BlockCursor::operator--()
    {
        if (!checkDocument()) {
            return *this;
        }

        resetNpos();

        debug_cursor("Subtraction: Position: %d, Current block number %d", pos, currentBlockNumber);

        if (pos == 0 && currentBlockNumber == 0) {
            return *this;
        }

        if (pos == 0) {
            return goToPreviousBlock();
        }

        pos -= 1;

        return *this;
    } // namespace gui

    auto BlockCursor::goToNextBlock() -> BlockCursor &
    {
        if (currentBlockNumber < document->getBlocks().size()) {
            blockChanged = true;
            currentBlockNumber += 1;
            pos = 0;

            debug_cursor("Addition block changed: Position: %d, Current block number %d", pos, currentBlockNumber);
        }

        return *this;
    }

    auto BlockCursor::goToPreviousBlock() -> BlockCursor &
    {
        if (currentBlockNumber > 0) {
            blockChanged = true;
            currentBlockNumber -= 1;
            pos = currentBlock()->length() +
                  (currentBlock()->getEnd() != TextBlock::End::Newline ? last_char_inclusive : -1);

            debug_cursor("Subtraction block changed: Position: %d, Current block number %d", pos, currentBlockNumber);
        }
        return *this;
    }

    void BlockCursor::addChar(uint32_t utf_val)
    {
        if (document->blocks.size() == 0) {
            if (utf_val == text::newline) {

                /*
                 * Adding new line to empty document means adding new block with only new line and immediately jumps to
                 * another block
                 */
                document->append(TextBlock("", default_font, TextBlock::End::None));
                operator++();
                document->addNewline(*this, TextBlock::End::Newline);

                return;
            }
            document->append(TextBlock("", default_font, TextBlock::End::None));
            resetNpos();
        }
        auto block = currentBlock();
        if (block == blocksEnd()) {
            LOG_ERROR("add char to document with no text blocks shouldn't ever happen");
            return;
        }
        if (utf_val == text::newline) {
            document->addNewline(*this, TextBlock::End::Newline);
            return;
        }
        block->addChar(utf_val, pos);
    }

    void BlockCursor::addTextBlock(TextBlock &&textblock)
    {

        if (textblock.length() == 0 && document->blocks.empty() && textblock.getEnd() != TextBlock::End::Newline) {
            return;
        }
        else if (textblock.length() == 0 && ((--blocksEnd())->getEnd() != TextBlock::End::Newline)) {
            return;
        }

        if (document->isEmpty()) {
            resetNpos();
        }

        // If previously added block is empty without newline remove it.
        if (!document->blocks.empty()) {
            if (document->blocks.back().isEmpty()) {
                document->blocks.pop_back();
            }
        }

        auto blockEnd    = textblock.getEnd();
        auto blockFormat = textblock.getFormat()->getFont();

        document->append(std::move(textblock));

        // If new added block ends with newline add additional empty block at end
        if (blockEnd == TextBlock::End::Newline) {
            addTextBlock(TextBlock("", blockFormat, TextBlock::End::None));
        }
    }

    bool BlockCursor::removeChar()
    {
        if (checkNpos()) {
            LOG_ERROR("cant remove from not initialized/empty cursor");
            return false;
        }

        auto block     = currentBlock();
        auto prevBlock = block;
        auto nextBlock = block;

        if (block != blocksBegin()) {
            prevBlock--;
        }
        if (block != blocksEnd()) {
            nextBlock++;
        }

        if (block == blocksEnd()) {
            LOG_ERROR("removing char from document with no TextBlocks");
            return false;
        }

        if (checkIfNextBlockEmpty(nextBlock)) {
            debug_cursor("Next empty block removed");
            document->removeBlock(nextBlock);
        }

        debug_cursor("From block: [%d], remove pos: [%d] block length [%d], Block count [%lu]",
                     getBlockNumber(),
                     pos,
                     block->length(),
                     document->blocks.size());

        block->removeChar(pos);

        if (checkIfBlockFirstAndEmpty(block)) {
            debug_cursor("First empty block removed");
            document->removeBlock(block);

            if (checkIfNextBlockEmpty(nextBlock)) {
                debug_cursor("Next empty block removed");
                document->removeBlock(nextBlock);
            }
        }

        return true;
    }

    auto BlockCursor::checkIfBlockFirstAndEmpty(std::list<TextBlock>::iterator block) -> bool
    {
        return block == blocksBegin() && block->isEmpty();
    }

    auto BlockCursor::checkIfNextBlockEmpty(std::list<TextBlock>::iterator nextBlock) -> bool
    {
        return nextBlock != blocksEnd() && nextBlock->isEmpty() && checkCurrentBlockNoNewLine();
    }

    const TextBlock &BlockCursor::operator*()
    {
        return *currentBlock();
    }

    auto BlockCursor::getText() -> std::string
    {
        if (currentBlock() == blocksEnd()) {
            return "";
        }
        return currentBlock()->getText(getPosition());
    }

    auto BlockCursor::getUTF8Text() -> UTF8
    {
        if (currentBlock() == blocksEnd()) {
            return "";
        }
        return currentBlock()->getText(getPosition());
    }

    const TextBlock *BlockCursor::operator->()
    {
        return &*currentBlock();
    }

    auto BlockCursor::begin() -> std::list<TextBlock>::iterator
    {
        return document == nullptr ? document->blocks.end() : document->blocks.begin();
    }

    auto BlockCursor::end() -> std::list<TextBlock>::iterator
    {
        return document->blocks.end();
    }
} // namespace gui