~aleteoryx/muditaos

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

#include <log/log.hpp>
#include "utf8/UTF8.hpp"

#include "../core/DrawCommand.hpp"

#include "Label.hpp"
#include <Style.hpp>
#include <cassert>
#include "TextConstants.hpp"
#include <FontManager.hpp>
#include <RawFont.hpp>

namespace gui
{

    Label::Label()
        : Rect(), text{""}, textDisplayed{""}, charDrawableCount{0}, stringPixelWidth{0}, textColor{0, 0}, font{nullptr}
    {
        setFont(style::window::font::medium);
    }

    Label::Label(
        Item *parent, const uint32_t &x, const uint32_t &y, const uint32_t &w, const uint32_t &h, const UTF8 &newText)
        : Rect{parent, x, y, w, h},
          text(newText), textDisplayed{""}, charDrawableCount{0}, stringPixelWidth{0}, textColor{0, 0}
    {
        setFont(style::window::font::medium);
    }

    Label::Label(Item *parent, meta::Label label) : Label(parent, label.x, label.y, label.w, label.h, label.text)
    {
        setPenFocusWidth(style::window::default_border_no_focus_w);
        setPenWidth(style::window::default_border_no_focus_w);
        setFont(label.font);
        setEllipsis(Ellipsis::Right);
        setAlignment(label.align);
        setRadius(label.radius);
        setEdges(label.edges);
    }

    void Label::calculateDisplayText()
    {
        uint32_t availableSpace = widgetArea.w;
        if (font == nullptr) {
            LOG_ERROR("No font loaded!");
            return;
        }
        charDrawableCount = font->getCharCountInSpace(text, availableSpace);
        textArea.w        = font->getPixelWidth(text.substr(0, charDrawableCount));
        textDisplayed     = text;
        /// do not try to draw text::newline in label
        if (text.length() > 0 && text[text.length() - 1] == text::newline) {
            textDisplayed.removeChar(textDisplayed.length() - 1);
        }
        textDisplayed    = font->getTextWithElipsis(textDisplayed, availableSpace, ellipsis);
        stringPixelWidth = font->getPixelWidth(textDisplayed, 0, textDisplayed.length());
        textArea.h       = font->info.line_height;

        // calculate vertical position of text

        switch (alignment.vertical) {
        case (Alignment::Vertical::Center):
            textArea.y = widgetArea.h > font->info.line_height
                             ? (widgetArea.h - font->info.line_height) / 2 + font->info.base
                             : font->info.base;
            break;
        case Alignment::Vertical::Top:
            textArea.y = font->info.base + padding.top;
            break;
        case Alignment::Vertical::Bottom:
            textArea.y = widgetArea.h - font->info.line_height + font->info.base - padding.bottom;
            break;
        default:
            break;
        }

        switch (alignment.horizontal) {
        case (Alignment::Horizontal::Center):
            textArea.x = (widgetArea.w - textArea.w) / 2;
            break;
        case Alignment::Horizontal::Left:
            textArea.x = padding.left;
            break;
        case Alignment::Horizontal::Right:
            textArea.x = widgetArea.w - textArea.w - padding.right;
            break;
        default:
            break;
        }

        // if dots mode is disabled and line mode is enabled calculate positiona and width of the line
        if ((ellipsis != Ellipsis::None) && (lineMode) && (lineFront != nullptr)) {
            uint32_t spaceWidth = font->getCharPixelWidth(' ');
            int32_t lineW       = availableSpace - stringPixelWidth;
            uint32_t lineY      = textArea.y - font->getCharPixelHeight('a') / 2;
            if (lineW < 0)
                lineW = 0;

            lineFront->setVisible(true);
            lineBack->setVisible(true);
            // both lines are visible

            switch (alignment.horizontal) {
            case (Alignment::Horizontal::Center):
                lineFront->setPosition(0, lineY);
                lineFront->setSize(lineW / 2 - spaceWidth, 2);
                lineBack->setPosition(lineW / 2 + stringPixelWidth + spaceWidth, lineY);
                lineBack->setSize(lineW / 2 - spaceWidth, 2);
                break;
            case Alignment::Horizontal::Right:
                lineFront->setPosition(0, lineY);
                lineFront->setSize(lineW - spaceWidth - padding.right, 2);
                lineBack->setVisible(false);
                break;
            case Alignment::Horizontal::Left:
                lineBack->setPosition(stringPixelWidth + spaceWidth + padding.left, lineY);
                lineBack->setSize(lineW - spaceWidth - padding.left, 2);
                lineFront->setVisible(false);
                break;
            default:
                break;
            }
        }
    }

    void Label::setText(const UTF8 &text)
    {
        this->text = text;
        auto fits  = textFitsIn(text, area(Area::Normal).w);
        if (!fits) {
            fitTextIn(text);
        }
        calculateDisplayText();
    }

    Label::Fits Label::textFitsIn(const UTF8 &text, uint32_t width)
    {
        Fits fits;
        if (font == nullptr) {
            fits.fits = false;
        }
        else {
            auto cnt            = font->getCharCountInSpace(text, area(Area::Max).w);
            fits.fits           = cnt == text.length();
            fits.space_consumed = font->getPixelWidth(text);
        }
        return fits;
    }

    void Label::fitTextIn(const UTF8 &text)
    {
        Fits fits = textFitsIn(text, area(Item::Area::Max).w);
        if (fits) {
            setSize(fits.space_consumed, getHeight());
        }
        else {
            setSize(area(Item::Area::Max).w, getHeight());
        }
    }

    void Label::clear()
    {
        this->text = UTF8("");
        calculateDisplayText();
    }

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

    unsigned int Label::getTextLength() const noexcept
    {
        return text.length();
    }

    void Label::setAlignment(const Alignment &value)
    {
        this->alignment = value;
        calculateDisplayText();
    }

    void Label::setPadding(const Padding &padding)
    {
        this->padding = padding;
        calculateDisplayText();
    }

    void Label::setEllipsis(Ellipsis ellipsis)
    {
        this->ellipsis = ellipsis;
        calculateDisplayText();
    }

    void Label::setLineMode(const bool &val)
    {
        // if line mode is disable remove the line if it was previously created
        if (val == false) {
            if (lineFront != nullptr) {
                this->removeWidget(lineFront);
                this->removeWidget(lineBack);
                delete lineFront;
                delete lineBack;
                lineFront = nullptr;
                lineBack  = nullptr;
            }
        }
        else {
            if (lineFront == nullptr) {
                lineFront = new Rect(this, 0, 0, 0, 0);
                lineBack  = new Rect(this, 0, 0, 0, 0);
            }
        }
        calculateDisplayText();
    }

    void Label::buildDrawListImplementation(std::list<Command> &commands)
    {
        Rect::buildDrawListImplementation(commands);
        if (font != nullptr) {
            auto cmd    = std::make_unique<DrawText>();
            cmd->str    = textDisplayed;
            cmd->fontID = font->id;
            cmd->color  = textColor;

            cmd->origin     = {drawArea.x, drawArea.y};
            cmd->width      = drawArea.w;
            cmd->height     = drawArea.h;
            cmd->textOrigin = {textArea.x, textArea.y};
            cmd->textHeight = textArea.h;

            cmd->areaX = widgetArea.x;
            cmd->areaY = widgetArea.y;
            cmd->areaW = widgetArea.w;
            cmd->areaH = widgetArea.h;

            commands.emplace_back(std::move(cmd));
        }
    }

    bool Label::onDimensionChanged(const BoundingBox &oldDim, const BoundingBox &newDim)
    {
        calculateDisplayText();
        return true;
    }

    void Label::setFont(const UTF8 &fontName)
    {
        RawFont *newFont = FontManager::getInstance().getFont(fontName);
        setFont(newFont);
    }

    void Label::setFont(RawFont *font)
    {
        this->font = font;
        if (font != nullptr) {
            calculateDisplayText();
        }
    }

    RawFont *Label::getFont() const noexcept
    {
        return font;
    }

    void Label::setTextColor(Color color)
    {
        textColor = color;
    }

    uint32_t Label::getTextNeedSpace() const noexcept
    {
        if (font == nullptr) {
            return 0;
        }
        return font->getPixelWidth(text);
    }

    uint32_t Label::getTextHeight() const noexcept
    {
        if (font == nullptr) {
            return 0;
        }
        return font->info.line_height;
    }

    uint32_t Label::getTextWidth() const noexcept
    {
        return textArea.w;
    }

    void Label::accept(GuiVisitor &visitor)
    {
        visitor.visit(*this);
    }
} /* namespace gui */