~aleteoryx/muditaos

ref: 7fbaf735ed99f5b0d26adb686cfa6ed2bb4267d5 muditaos/module-gui/gui/widgets/TextBlock.cpp -rw-r--r-- 2.9 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
// Copyright (c) 2017-2021, Mudita Sp. z.o.o. All rights reserved.
// For licensing, see https://github.com/mudita/MuditaOS/LICENSE.md

#include "TextBlock.hpp"
#include "TextConstants.hpp"
#include <log/log.hpp>
#include <cassert>
#include <TextFormat.hpp>
#include <RawFont.hpp>

namespace gui
{

    TextBlock::TextBlock(const UTF8 text, std::unique_ptr<TextFormat> format) : format(std::move(format)), text{text}
    {}

    TextBlock::TextBlock(const UTF8 text, const RawFont *font, TextBlock::End eol)
        : TextBlock(text, std::make_unique<TextFormat>(font))
    {
        if (getEnd() != End::Newline && eol == End::Newline) {
            this->text.insertCode(text::newline);
        }
        end = eol;
    }

    TextBlock::TextBlock(const TextBlock &p)
    {
        text   = p.text;
        format = std::make_unique<TextFormat>(*p.format);
        end    = p.end;
    }

    TextBlock &TextBlock::operator=(const TextBlock &p)
    {
        if (this != &p) {
            text   = p.text;
            format = std::make_unique<TextFormat>(*p.format);
            end    = p.end;
        }
        return *this;
    }

    const UTF8 &TextBlock::getText() const
    {
        return text;
    }

    UTF8 TextBlock::getText(uint32_t start_position) const
    {
        return text.substr(start_position, text.length() - start_position);
    }

    const TextFormat *TextBlock::getFormat() const
    {
        return format.get();
    }

    void TextBlock::setText(const UTF8 text)
    {
        this->text = text;
    }

    /// sick there is no add/append in UTF8 - there is insert...
    void TextBlock::insertChar(const uint32_t value, const uint32_t pos)
    {
        text.insertCode(value, pos);
    }

    void TextBlock::removeChar(const uint32_t pos)
    {
        text.removeChar(pos);
    }

    uint32_t TextBlock::getWidth() const
    {
        auto font = format->getFont();
        if (font != nullptr) {
            return font->getPixelWidth(text);
        }
        return 0;
    }

    unsigned int TextBlock::length() const
    {
        return text.length();
    }

    TextBlock::End TextBlock::getEnd() const
    {
        if (text.length() > 0 && text[text.length() - 1] == text::newline) {
            return End::Newline;
        }
        return End::None;
    }

    void TextBlock::setEnd(End _end)
    {
        if (_end == End::Newline && getEnd() != End::Newline) {
            text.insertCode(text::newline);
        }
        else if (_end == End::None && getEnd() == End::Newline) {
            text.removeChar(text.length() - 1);
        }
        this->end = _end;
    }

    void TextBlock::addChar(uint32_t utf_val, unsigned int pos)
    {
        if (pos == text::npos) {
            pos = 0;
        }
        text.insertCode(utf_val, pos);
    }

    bool TextBlock::isEmpty() const
    {
        return text.length() == 0 && getEnd() == End::None;
    }

} // namespace gui