~aleteoryx/muditaos

ref: 0ef0d615f34e275f088890a1244dd480dd467920 muditaos/module-gui/gui/widgets/TextLine.cpp -rw-r--r-- 8.8 KiB
0ef0d615 — Krzysztof Mozdzynski [EGD-4150] Change filename i18 to i18n (#1108) 5 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
// Copyright (c) 2017-2020, Mudita Sp. z.o.o. All rights reserved.
// For licensing, see https://github.com/mudita/MuditaOS/LICENSE.md

#include "TextLine.hpp"
#include "Common.hpp"
#include "Label.hpp"
#include "TextBlock.hpp"
#include "TextDocument.hpp"
#include "TextCursor.hpp"
#include <cstdio>
#include <RawFont.hpp>
#include <numeric>

namespace gui
{

    /// helper function to get our text representation
    Label *buildUITextPart(const UTF8 &text, const TextFormat *format)
    {
        auto item = new gui::Label(nullptr);
        item->setText(text);
        item->setFont(format->getFont());
        item->setTextColor(format->getColor());
        item->setSize(item->getTextNeedSpace(), item->getTextHeight());
        item->setEdges(RectangleEdge::None);
        return item;
    }

    /// Note - line breaking could be done here with different TextLines to return
    /// or via different block types (i.e. numeric block tyle could be not "breakable"
    TextLine::TextLine(BlockCursor &localCursor, unsigned int maxWidth) : maxWidth(maxWidth)
    {
        do {
            if (!localCursor) { // cursor is faulty
                lineEnd = true;
                return;
            }

            if (localCursor.atEnd() && !localCursor.checkLastNewLine()) {
                lineEnd = true;
                return;
            }

            // take text we want to show
            auto text = localCursor.getUTF8Text();

            if (text.length() == 0 && localCursor.checkCurrentBlockNoNewLine() && !localCursor.atEnd()) {
                localCursor.goToNextBlock();
                continue;
            }

            auto textFormat = localCursor->getFormat();
            if (textFormat->getFont() == nullptr) {
                return;
            }

            // check if max provided width is enough to enter one char at least
            if (maxWidth < textFormat->getFont()->getCharPixelWidth(text[0])) {
                lineEnd = true;
                return;
            }

            auto canShow = textFormat->getFont()->getCharCountInSpace(text, maxWidth - widthUsed);

            // we can show nothing - this is the end of this line
            if (canShow == 0) {

                auto item  = buildUITextPart("", textFormat);
                widthUsed  = item->getTextNeedSpace();
                heightUsed = std::max(heightUsed, item->getTextHeight());
                lineContent.emplace_back(item);
                end = localCursor->getEnd();

                return;
            }

            // create item for show and update Line data
            auto item = buildUITextPart(text.substr(0, canShow), textFormat);
            shownLetterCount += canShow;
            widthUsed += item->getTextNeedSpace();
            heightUsed = std::max(heightUsed, item->getTextHeight());
            lineContent.emplace_back(item);
            end = localCursor->getEnd();

            localCursor += canShow;

            if (localCursor.checkAndInvalidateBlockChanged() && localCursor.checkPreviousBlockNewLine()) {
                return;
            }

            // not whole text shown, try again for next line if you want
            if (canShow < text.length()) {
                return;
            }

        } while (true);
    }

    TextLine::TextLine(TextLine &&from) noexcept
    {
        lineContent       = std::move(from.lineContent);
        shownLetterCount  = from.shownLetterCount;
        widthUsed         = from.widthUsed;
        heightUsed        = from.heightUsed;
        underline         = from.underline;
        drawUnderline     = from.drawUnderline;
        drawUnderlineMode = from.drawUnderlineMode;
        underlinePadding  = from.underlinePadding;
        lineEnd           = from.lineEnd;
        end               = from.end;
        maxWidth          = from.maxWidth;
    }

    TextLine::~TextLine()
    {
        for (auto &el : lineContent) {
            if (el->parent == nullptr) {
                delete el;
            }
        }

        if (underline != nullptr && underline->parent == nullptr) {
            delete underline;
        }
    }

    /// class to disown Item temporary to ignore callback
    class ScopedParentDisown
    {
        Item *parent = nullptr;
        Item *item   = nullptr;

      public:
        ScopedParentDisown(Item *it) : item(it)
        {
            if (item != nullptr) {
                parent = item->parent;
            }
        }

        ~ScopedParentDisown()
        {
            if (item != nullptr) {
                item->parent = parent;
            }
        }
    };

    void TextLine::setPosition(const short &x, const short &y)
    {
        auto lineXPosition = x;

        updateUnderline(x, y);

        for (auto &el : lineContent) {
            auto scopedDisown = ScopedParentDisown(el);
            el->setArea({lineXPosition, y - underlinePadding, el->getWidth(), el->getHeight()});
            lineXPosition += el->getWidth();
        }
    }

    void TextLine::setParent(Item *parent)
    {
        if (parent == nullptr) {
            return;
        }

        parent->addWidget(underline);

        for (auto &el : lineContent) {
            parent->addWidget(el);
        }
    }

    Length TextLine::getWidth() const
    {
        Length width = 0;
        for (auto &line : lineContent) {
            width += line->getWidth();
        }
        return width;
    }

    uint32_t TextLine::getWidthTo(unsigned int pos) const
    {
        uint32_t width  = 0;
        auto currentPos = 0;
        if (pos == text::npos) {
            return 0;
        }
        for (auto &el : lineContent) {
            if (el->getFont() == nullptr) {
                continue;
            }
            if (currentPos + el->getTextLength() > pos) {
                width += el->getFont()->getPixelWidth(el->getText(), 0, pos - currentPos);
                return width;
            }
            else {
                width += el->getWidth();
            }
            currentPos += el->getTextLength();
        }
        return width;
    }

    UTF8 TextLine::getText(unsigned int pos) const
    {
        UTF8 text;
        for (auto &label : lineContent) {
            if (label->getFont() == nullptr) {
                continue;
            }
            text += label->getText();
        }

        return text;
    }

    void TextLine::erase()
    {
        for (auto &el : lineContent) {
            if (el->parent != nullptr) {
                auto p = el->parent;
                p->erase(el);
            }
            else {
                delete el;
            }
        }

        if (underline != nullptr && underline->parent != nullptr) {
            auto p = underline->parent;
            p->removeWidget(underline);
        }

        lineContent.clear();
    }

    void TextLine::alignH(Alignment lineAlign, Length parentLength) const
    {
        Position xOffset = lineAlign.calculateHAlignment(parentLength, getWidth());

        if (xOffset >= 0) {

            if (underline != nullptr && drawUnderlineMode == UnderlineDrawMode::Concurrent)
                underline->setPosition(underline->getPosition(Axis::X) + xOffset, Axis::X);

            for (auto &el : lineContent) {
                auto scopedDisown = ScopedParentDisown(el);
                el->setPosition(el->getPosition(Axis::X) + xOffset, Axis::X);
            }
        }
    }

    void TextLine::alignV(Alignment lineAlign, Length parentLength, Length linesHeight)
    {
        Position yOffset = lineAlign.calculateVAlignment(parentLength, linesHeight);

        if (yOffset >= 0 && yOffset != storedYOffset) {

            // Refactor - workaround for multiple offset addition.
            storedYOffset = yOffset;

            if (underline != nullptr)
                underline->setPosition(underline->getPosition(Axis::Y) + yOffset, Axis::Y);

            for (auto &el : lineContent) {
                auto scopedDisown = ScopedParentDisown(el);
                el->setPosition(el->getPosition(Axis::Y) + yOffset, Axis::Y);
            }
        }
    }

    void TextLine::createUnderline(unsigned int width, unsigned int initHeight)
    {
        if (drawUnderline) {

            underline = new Rect(nullptr, 0, 0, maxWidth, initHeight);
            underline->setEdges(RectangleEdge::Bottom);

            if (drawUnderlineMode == UnderlineDrawMode::WholeLine) {
                heightUsed = std::max(heightUsed, (Length)initHeight);
            }
        }
    }

    void TextLine::updateUnderline(const short &x, const short &y)
    {
        if (underline != nullptr && drawUnderlineMode == UnderlineDrawMode::WholeLine) {
            underline->setArea({x, y, underline->widgetArea.w, height()});
        }
        else if (underline != nullptr && drawUnderlineMode == UnderlineDrawMode::Concurrent) {
            underline->setArea({x, y, getWidth(), height()});
        }
    }

} // namespace gui