~aleteoryx/muditaos

ref: ba8f0ac32d86266ac0082ceecb98970b1f19a6de muditaos/module-gui/gui/widgets/Lines.cpp -rw-r--r-- 4.2 KiB
ba8f0ac3 — Krzysztof Móżdżyński [EGD-5448] Add EULA window 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
// 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
{

    void Lines::addToInvisibleLines(TextLine line)
    {
        line.setVisible(false);
        emplace(std::move(line));
    }

    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");
                stopCondition = LinesDrawStop::OutOfText;
                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);
                stopCondition = LinesDrawStop::OutOfSpace;
                addToInvisibleLines(std::move(textLine));
                break;
            }

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

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

            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) {
                stopCondition = LinesDrawStop::OutOfSpace;
                addToInvisibleLines(std::move(textLine));
                break;
            }

            if (lines.size() >= linesCount) {
                stopCondition = LinesDrawStop::OutOfSpace;
                addToInvisibleLines(std::move(textLine));
                break;
            }

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

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

            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;
    }

    auto Lines::addToPreviousLinesStartList(unsigned int lineStartBlockNumber, unsigned int lineStartBlockPosition)
        -> void
    {
        auto it = std::find_if(
            previousLinesStart.begin(),
            previousLinesStart.end(),
            [&lineStartBlockNumber, &lineStartBlockPosition](const std::tuple<unsigned int, unsigned int> &lineStart) {
                return std::get<0>(lineStart) == lineStartBlockNumber &&
                       std::get<1>(lineStart) == lineStartBlockPosition;
            });

        if (it == previousLinesStart.end()) {
            previousLinesStart.emplace_back(lineStartBlockNumber, lineStartBlockPosition);
        }
    }

} // namespace gui