~aleteoryx/muditaos

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

#include "Lines.hpp"
#include "TextLineCursor.hpp"
#include "Text.hpp"
#include "RawFont.hpp"

#if DEBUG_GUI_TEXT_LINES == 1
#define debug_text_lines(...) LOG_DEBUG(__VA_ARGS__)
#else
#define debug_text_lines(...)
#endif

namespace gui
{
    // LEFT/RIGHT/UP/DOWN

    auto Lines::linesVAlign(Length parentSize) -> void
    {
        for (auto &line : lines) {
            line.alignV(text->getAlignment(Axis::Y), parentSize, linesHeight());
        }
    }

    auto Lines::linesHAlign(Length parentSize) -> void
    {
        for (auto &line : lines) {
            line.alignH(text->getAlignment(Axis::X), parentSize);
        }
    }

    auto Lines::draw(BlockCursor &drawCursor, Length w, Length h, Position lineYPosition, Position lineXPosition)
        -> void
    {
        while (true) {
            auto textLine = gui::TextLine(drawCursor, w);

            if (textLine.length() == 0 && textLine.getLineEnd()) {
                debug_text_lines("cant show more text from this document");
                break;
            }

            if (lineYPosition + textLine.height() > h) { // no more space for next line
                debug_text_lines("no more space for next text_line: %d + %" PRIu32 " > %" PRIu32,
                                 lineYPosition,
                                 textLine.height(),
                                 h);
                break;
            }

            emplace(std::move(textLine));
            auto &line = last();

            line.setPosition(lineXPosition, lineYPosition);
            line.setParent(text);

            lineYPosition += line.height();
        }
    }

    auto Lines::draw(BlockCursor &drawCursor,
                     Length w,
                     Length h,
                     Position lineYPosition,
                     Position lineXPosition,
                     unsigned int linesCount) -> void
    {
        Length initHeight = text->getTextFormat().getFont()->info.line_height;

        while (true) {
            auto textLine =
                gui::TextLine(drawCursor, w, initHeight, underLine, UnderlineDrawMode::WholeLine, underLinePadding);

            if (textLine.height() > 0 && initHeight != textLine.height()) {
                initHeight = textLine.height();
            }

            if (lineYPosition + initHeight > h) {
                break;
            }

            if (lines.size() >= linesCount) {
                break;
            }

            emplace(std::move(textLine));
            auto &line = last();
            line.setPosition(lineXPosition, lineYPosition);
            line.setParent(text);

            lineYPosition += line.height();
        }
    }

    TextLine *Lines::getLine(unsigned int lineNr)
    {
        if (lines.empty() || lineNr >= lines.size()) {
            return nullptr;
        }

        auto it = std::next(lines.begin(), lineNr);
        return &*it;
    }

} // namespace gui