~aleteoryx/muditaos

621304168edc83453e0f60a7c4a459401f68f822 — Przemyslaw Brudny 4 years ago 6ff1d9d
[EGD-7203] Added extra space removal in Text draw line ending

Added extra space removal in Text draw line ending.
Fixed crash in Notes.
M module-apps/application-notes/style/NotesListStyle.hpp => module-apps/application-notes/style/NotesListStyle.hpp +1 -1
@@ 9,7 9,7 @@ namespace app::notes::style::list
{
    constexpr inline auto X      = ::style::window::default_left_margin;
    constexpr inline auto Y      = ::style::window::default_vertical_pos + ::style::margins::small - 1;
    constexpr inline auto Width  = ::style::window::default_body_width;
    constexpr inline auto Width  = ::style::listview::body_width_with_scroll;
    constexpr inline auto Height = ::style::window::default_body_height - ::style::margins::small;

    constexpr inline auto PenWidth        = 0;

M module-gui/gui/widgets/TextCursor.cpp => module-gui/gui/widgets/TextCursor.cpp +1 -1
@@ 1,4 1,4 @@
// Copyright (c) 2017-2020, Mudita Sp. z.o.o. All rights reserved.
// Copyright (c) 2017-2021, Mudita Sp. z.o.o. All rights reserved.
// For licensing, see https://github.com/mudita/MuditaOS/LICENSE.md

#include "TextCursor.hpp"

M module-gui/gui/widgets/TextLine.cpp => module-gui/gui/widgets/TextLine.cpp +41 -3
@@ 78,8 78,7 @@ namespace gui
            }

            // create item for show and update Line data
            auto item =
                buildUITextPart(text.substr(0, signsCountToShow) + (breakLineDashAddition ? "-" : ""), textFormat);
            auto item = buildUITextPart(textToPrint(signsCountToShow, text), textFormat);
            shownLetterCount += signsCountToShow;
            widthUsed += item->getTextNeedSpace();
            heightUsed = std::max(heightUsed, item->getTextHeight());


@@ 89,6 88,11 @@ namespace gui

            localCursor += signsCountToShow;

            if (removeTrailingSpace) {
                end = TextBlock::End::Newline;
                return;
            }

            if (localCursor.checkAndInvalidateBlockChanged() && localCursor.checkPreviousBlockNewLine()) {
                end = TextBlock::End::Newline;
                return;


@@ 124,8 128,14 @@ namespace gui

        // check if space found and no newline at end
        if (spacePos != UTF8::npos && newlinePos == UTF8::npos) {
            // if line ends on space remove it when drawing
            if (spacePos >= signsCountToShow) {
                removeTrailingSpace = true;
            }

            // add one to include space in the line
            signsCountToShow      = spacePos + 1;
            signsCountToShow = spacePos + 1;

            breakLineDashAddition = false;
        }
        // if sings still left in text add dash sign


@@ 138,6 148,21 @@ namespace gui
        return signsCountToShow;
    }

    UTF8 TextLine::textToPrint(unsigned int signsCountToShow, UTF8 &text)
    {
        auto textToPrint = text.substr(0, signsCountToShow);

        if (removeTrailingSpace) {
            textToPrint = text.substr(0, signsCountToShow - 1);
        }

        if (breakLineDashAddition) {
            textToPrint = textToPrint + "-";
        }

        return textToPrint;
    }

    TextLine::TextLine(TextLine &&from) noexcept
    {
        lineContent            = std::move(from.lineContent);


@@ 153,6 178,7 @@ namespace gui
        end                    = from.end;
        maxWidth               = from.maxWidth;
        breakLineDashAddition  = from.breakLineDashAddition;
        removeTrailingSpace    = from.removeTrailingSpace;
        lineStartBlockNumber   = from.lineStartBlockNumber;
        lineStartBlockPosition = from.lineStartBlockPosition;
        lineVisible            = from.lineVisible;


@@ 260,6 286,18 @@ namespace gui
        return width;
    }

    const Item *TextLine::getElement(unsigned int pos) const noexcept
    {
        unsigned int local_pos = 0;
        for (auto &el : lineContent) {
            local_pos += el->getTextLength();
            if (local_pos >= pos) {
                return el;
            }
        }
        return nullptr;
    }

    UTF8 TextLine::getText(unsigned int pos) const
    {
        UTF8 text;

M module-gui/gui/widgets/TextLine.hpp => module-gui/gui/widgets/TextLine.hpp +3 -12
@@ 37,10 37,12 @@ namespace gui
        bool lineEnd                        = false;
        bool lineVisible                    = true;
        bool breakLineDashAddition          = false;
        bool removeTrailingSpace            = false;
        unsigned int lineStartBlockNumber   = text::npos;
        unsigned int lineStartBlockPosition = text::npos;

        unsigned int calculateSignsToShow(BlockCursor &localCursor, UTF8 &text, unsigned int space);
        UTF8 textToPrint(unsigned int signsCountToShow, UTF8 &text);
        void createUnderline(unsigned int max_w, unsigned int max_height);
        void updateUnderline(const short &x, const short &y);
        void setLineStartConditions(unsigned int startBlockNumber, unsigned int startBlockPosition);


@@ 126,18 128,6 @@ namespace gui
            return lineStartBlockPosition;
        }

        [[nodiscard]] const Item *getElement(unsigned int pos) const noexcept
        {
            unsigned int local_pos = 0;
            for (auto &el : lineContent) {
                local_pos += el->getTextLength();
                if (local_pos >= pos) {
                    return el;
                }
            }
            return nullptr;
        }

        [[nodiscard]] int32_t getX() const noexcept
        {
            return lineContent.front()->area().pos(Axis::X);


@@ 153,6 143,7 @@ namespace gui
        /// moves Text parts in Text. To not call n times callbacks on resize, call prior to setting parent
        void alignH(Alignment align, Length parent_length) const;
        void alignV(Alignment align, Length parent_length, Length lines_height);
        [[nodiscard]] const Item *getElement(unsigned int pos) const noexcept;
        [[nodiscard]] auto getText(unsigned int pos) const -> UTF8;
    };
} // namespace gui

M module-gui/test/test-catch-text/test-gui-TextFixedSize.cpp => module-gui/test/test-catch-text/test-gui-TextFixedSize.cpp +2 -2
@@ 165,7 165,7 @@ TEST_CASE("TextFixedSize remove Char")
    SECTION("Remove char from last line on size limited space")
    {
        auto text = TestTextFixedSize();
        text.setSize(210, 30);
        text.setSize(220, 30);

        text.setFont(style::window::font::medium);
        text.setText(testStringLongLine);


@@ 181,7 181,7 @@ TEST_CASE("TextFixedSize remove Char")
    SECTION("Remove char from last line on line limited space")
    {
        auto text = TestTextFixedSize();
        text.setSize(210, 120);
        text.setSize(220, 120);

        text.setFont(style::window::font::medium);
        text.setLines(1);