diff --git a/src/element/text/Text.cpp b/src/element/text/Text.cpp index 23ab096..6f8d032 100644 --- a/src/element/text/Text.cpp +++ b/src/element/text/Text.cpp @@ -1,5 +1,6 @@ #include "Text.hpp" +#include #include #include #include @@ -533,12 +534,58 @@ static std::string formatColor(uint32_t col) { return std::format("#{0:08x}", ((col & 0x00ffffffu) << 8) | (col >> 24)); } +// a link's begin/end index into the text pango lays out, which is markup-stripped, not the raw +// markup assembled here. counting displayed bytes keeps the region on the link's glyphs instead of +// drifting by the length of the styling tags in front of it. valid markup only: a broken fragment +// would already have failed the whole-string parse in setPangoText. +size_t STextImpl::displayedLength(std::string_view s) { + size_t n = 0; + for (size_t i = 0; i < s.size();) { + const char c = s[i]; + if (c == '<') { + // pango takes a raw '>' inside a quoted attribute value, that one does not close the tag + char quote = 0; + for (++i; i < s.size(); ++i) { + if (quote && s[i] == quote) + quote = 0; + else if (!quote && (s[i] == '"' || s[i] == '\'')) + quote = s[i]; + else if (!quote && s[i] == '>') + break; + } + ++i; + } else if (c == '&') { + const size_t semi = s.find(';', i); + if (semi == std::string_view::npos) { + n++; + i++; + continue; + } + const auto body = s.substr(i + 1, semi - i - 1); + if (!body.empty() && body[0] == '#') { + const int base = (body.size() > 1 && body[1] == 'x') ? 16 : 10; + const auto digits = body.substr(base == 16 ? 2 : 1); + unsigned long cp = 0; + std::from_chars(digits.data(), digits.data() + digits.size(), cp, base); + n += cp ? sc(g_unichar_to_utf8(sc(cp), nullptr)) : 1; + } else + n++; // amp/lt/gt/quot/apos each strip to one byte + i = semi + 1; + } else { + n++; + i++; + } + } + return n; +} + void STextImpl::parseText() { parsedLinks.clear(); hoveredTextLink = nullptr; - size_t lastTagClose = 0; - const auto& ORIGINAL = data.text; + size_t lastTagClose = 0; + size_t displayedOffset = 0; // byte offset into the markup-stripped text, tracked in parallel with newString + const auto& ORIGINAL = data.text; std::string newString; while (true) { @@ -550,7 +597,9 @@ void STextImpl::parseText() { break; } - newString += ORIGINAL.substr(lastTagClose, tagOpen - lastTagClose); + const std::string_view PRE = std::string_view{ORIGINAL}.substr(lastTagClose, tagOpen - lastTagClose); + newString += PRE; + displayedOffset += displayedLength(PRE); // find the close size_t linkOpen = tagOpen + 9; @@ -576,16 +625,18 @@ void STextImpl::parseText() { if (contentClose == std::string::npos) break; // broken tag - std::string replaceWith = std::format("{}", formatColor(g_palette->m_colors.linkText.getAsHex()), - std::string_view{ORIGINAL}.substr(contentOpen, contentClose - contentOpen)); + const std::string_view CONTENT = std::string_view{ORIGINAL}.substr(contentOpen, contentClose - contentOpen); + const size_t contentLen = displayedLength(CONTENT); + std::string replaceWith = std::format("{}", formatColor(g_palette->m_colors.linkText.getAsHex()), CONTENT); parsedLinks.emplace_back(STextLink{ - .begin = newString.size(), - .end = newString.size() + replaceWith.size(), + .begin = displayedOffset, + .end = displayedOffset + contentLen, .link = std::string{LINK}, }); newString += replaceWith; + displayedOffset += contentLen; // the wrapper adds no displayed bytes lastTagClose = contentClose + 4; } @@ -598,7 +649,7 @@ void STextImpl::parseText() { void STextImpl::recheckTextBoxes() { for (auto& link : parsedLinks) { link.region.clear(); - for (size_t i = link.begin; i < link.end + 1; ++i) { + for (size_t i = link.begin; i < link.end; ++i) { auto box = getCharBox(i); link.region.add(box); } diff --git a/src/element/text/Text.hpp b/src/element/text/Text.hpp index 1ff8d23..70dcda8 100644 --- a/src/element/text/Text.hpp +++ b/src/element/text/Text.hpp @@ -93,6 +93,7 @@ namespace Hyprtoolkit { void postTexLoad(); void parseText(); void recheckTextBoxes(); + static size_t displayedLength(std::string_view markup); void onMouseDown(); void onMouseMove(); Hyprutils::Math::Vector2D applyClampSize(Hyprutils::Math::Vector2D); diff --git a/tests/unit/elements/Text.cpp b/tests/unit/elements/Text.cpp index 05b9383..dfdf474 100644 --- a/tests/unit/elements/Text.cpp +++ b/tests/unit/elements/Text.cpp @@ -1,5 +1,7 @@ #include +#include + #include #include #include @@ -19,6 +21,17 @@ using namespace Hyprtoolkit; using namespace Hyprutils::Math; +// the text pango actually lays out, with markup stripped. the ground truth a link's begin/end index into. +static std::string strippedMarkup(const std::string& markup) { + char* buf = nullptr; + if (!pango_parse_markup(markup.c_str(), -1, 0, nullptr, &buf, nullptr, nullptr)) + return markup; + std::string out = buf ? buf : ""; + if (buf) + g_free(buf); + return out; +} + TEST(Element, text) { Tests::Tricks::createBackendSupport(); @@ -29,9 +42,67 @@ TEST(Element, text) { EXPECT_EQ(text->m_impl->parsedText, "Hello link! Hi link2!"); + // a link's begin/end must index into the text pango lays out, which is markup-stripped, so the + // region lands on the glyphs. the styling markup before the second link used to shift it. (#127) + const std::string STRIPPED = strippedMarkup(text->m_impl->parsedText); + ASSERT_EQ(STRIPPED, "Hello link! Hi link2!"); + ASSERT_EQ(text->m_impl->parsedLinks.size(), 2u); + const auto& l0 = text->m_impl->parsedLinks[0]; + const auto& l1 = text->m_impl->parsedLinks[1]; + EXPECT_EQ(STRIPPED.substr(l0.begin, l0.end - l0.begin), "link"); + EXPECT_EQ(STRIPPED.substr(l1.begin, l1.end - l1.begin), "link2"); + text.reset(); } +TEST(Element, textLinkOffsetsWithMarkupPrefix) { + Tests::Tricks::createBackendSupport(); + + g_palette->m_colors.linkText = CHyprColor(1.F, 0.F, 0.F, 1.F); + + // an entity and a styling tag before the link both shrink once pango strips them, so counting + // raw markup bytes put the region past the link. count displayed bytes instead. (#127) + auto text = CTextBuilder::begin()->text(R"(a & see LINK now)")->commence(); + + const std::string STRIPPED = strippedMarkup(text->m_impl->parsedText); + ASSERT_EQ(STRIPPED, "a & see LINK now"); + ASSERT_EQ(text->m_impl->parsedLinks.size(), 1u); + const auto& link = text->m_impl->parsedLinks[0]; + EXPECT_EQ(STRIPPED.substr(link.begin, link.end - link.begin), "LINK"); + + text.reset(); +} + +TEST(Element, textDisplayedLength) { + // whole, valid markup: pango is the ground truth for how many bytes survive the strip + const std::vector> VALID = { + {"", 0}, + {"plain", 5}, + {"h\xc3\xa9llo", 6}, + {"bold", 4}, + {"ab", 2}, + {"a red b", 7}, + {"x", 1}, + {"&<>"'", 5}, + {"AB", 2}, + {"é", 2}, + {"中", 3}, + {"😀", 4}, + {"x", 1}, + {"b\">x", 1}, + {"x", 1}, + }; + for (const auto& [markup, len] : VALID) { + SCOPED_TRACE(markup); + EXPECT_EQ(STextImpl::displayedLength(markup), len); + EXPECT_EQ(STextImpl::displayedLength(markup), strippedMarkup(markup).size()); + } + + // parseText measures the pieces between links, which are not valid markup on their own + EXPECT_EQ(STextImpl::displayedLength("see "), 4); + EXPECT_EQ(STextImpl::displayedLength(" now"), 4); +} + TEST(Element, synchronousTextRebuildSchedulesTextureRefresh) { Tests::Tricks::createBackendSupport();