Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 59 additions & 8 deletions src/element/text/Text.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include "Text.hpp"

#include <charconv>
#include <cmath>
#include <glib-object.h>
#include <hyprtoolkit/palette/Palette.hpp>
Expand Down Expand Up @@ -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<size_t>(g_unichar_to_utf8(sc<gunichar>(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) {
Expand All @@ -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;
Expand All @@ -576,16 +625,18 @@ void STextImpl::parseText() {
if (contentClose == std::string::npos)
break; // broken tag

std::string replaceWith = std::format("<u><span foreground=\"{}\">{}</span></u>", 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("<u><span foreground=\"{}\">{}</span></u>", 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 <u><span> wrapper adds no displayed bytes

lastTagClose = contentClose + 4;
}
Expand All @@ -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);
}
Expand Down
1 change: 1 addition & 0 deletions src/element/text/Text.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
71 changes: 71 additions & 0 deletions tests/unit/elements/Text.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#include <gtest/gtest.h>

#include <pango/pango.h>

#include <element/text/Text.hpp>
#include <hyprtoolkit/core/Backend.hpp>
#include <hyprtoolkit/palette/Color.hpp>
Expand All @@ -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();

Expand All @@ -29,9 +42,67 @@ TEST(Element, text) {

EXPECT_EQ(text->m_impl->parsedText, "Hello <u><span foreground=\"#ff0000ff\">link</span></u>! Hi <u><span foreground=\"#ff0000ff\">link2</span></u>!");

// 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 &amp; <b>see <a href="http://x">LINK</a></b> 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<std::pair<std::string, size_t>> VALID = {
{"", 0},
{"plain", 5},
{"h\xc3\xa9llo", 6},
{"<b>bold</b>", 4},
{"<b><i>a</i>b</b>", 2},
{"a <span foreground=\"#ff0000ff\">red</span> b", 7},
{"<u><span foreground='#abc'>x</span></u>", 1},
{"&amp;&lt;&gt;&quot;&apos;", 5},
{"&#65;&#x42;", 2},
{"&#233;", 2},
{"&#x4E2D;", 3},
{"&#x1F600;", 4},
{"<span font_family=\"a&gt;b\">x</span>", 1},
{"<span font_family=\"a>b\">x</span>", 1},
{"<span font_family='a>b'>x</span>", 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("<b>see "), 4);
EXPECT_EQ(STextImpl::displayedLength(" now</b>"), 4);
}

TEST(Element, synchronousTextRebuildSchedulesTextureRefresh) {
Tests::Tricks::createBackendSupport();

Expand Down
Loading