// SPDX-License-Identifier: GPL-3.0-only // SPDX-FileCopyrightText: Copyright (C) 2026 Catcrafts® module Nostro.Ui:Text_impl; import std; import :Text; import :Geometry; using namespace Nostro::Ui; std::uint32_t Nostro::Ui::DecodeUtf8(std::string_view text, std::size_t& i) { if(i >= text.size()) { return 0; } const std::uint8_t first = static_cast(text[i]); i++; if(first < 0x80) { return first; } std::size_t more = 0; std::uint32_t codepoint = 0; if((first & 0xE0) == 0xC0) { more = 1; codepoint = first & 0x1F; } else if((first & 0xF0) == 0xE0) { more = 2; codepoint = first & 0x0F; } else if((first & 0xF8) == 0xF0) { more = 3; codepoint = first & 0x07; } else { return 0xFFFD; } for(std::size_t k = 0; k < more; k++) { // A sequence cut short, by the text's end or by a byte that continues nothing, leaves that byte for the next call. if(i >= text.size() || (static_cast(text[i]) & 0xC0) != 0x80) { return 0xFFFD; } codepoint = (codepoint << 6) | (static_cast(text[i]) & 0x3Fu); i++; } return codepoint; } std::size_t Nostro::Ui::PreviousCodepoint(std::string_view text, std::size_t at) { if(at == 0) { return 0; } std::size_t i = std::min(at, text.size()) - 1; while(i > 0 && (static_cast(text[i]) & 0xC0) == 0x80) { i--; } return i; } std::size_t Nostro::Ui::NextCodepoint(std::string_view text, std::size_t at) { if(at >= text.size()) { return text.size(); } std::size_t i = at; DecodeUtf8(text, i); return i; } std::size_t Nostro::Ui::CountCodepoints(std::string_view text) { std::size_t count = 0; for(std::size_t i = 0; i < text.size(); count++) { DecodeUtf8(text, i); } return count; } float Nostro::Ui::TextWidth(const TextMeasure& measure, std::string_view text, FontFace face, float px) { float width = 0; for(std::size_t i = 0; i < text.size();) { const std::uint32_t codepoint = DecodeUtf8(text, i); if(codepoint != '\n') { width += measure.advance(face, codepoint, px); } } return width; } std::size_t Nostro::Ui::NearestByte(const TextMeasure& measure, std::string_view line, FontFace face, float px, float x) { if(x <= 0) { return 0; } float width = 0; float bestDistance = x; std::size_t best = 0; for(std::size_t i = 0; i < line.size();) { width += measure.advance(face, DecodeUtf8(line, i), px); const float distance = std::abs(x - width); // The widths only grow, so the first offset further away ends the search; at a tie the later offset wins, which // puts the cursor past a mark that has no width. if(distance > bestDistance) { break; } bestDistance = distance; best = i; } return best; } float Nostro::Ui::CenteredBaseline(const TextMeasure& measure, Box box, FontFace face, float px) { return box.y + (box.h - measure.lineHeight(face, px)) / 2.0f + measure.ascent(face, px); } std::string Nostro::Ui::Ellipsize(const TextMeasure& measure, std::string_view text, FontFace face, float px, float maxWidthPx) { const float ellipsis = measure.advance(face, 0x2026, px); float width = 0; std::size_t fit = 0; // the longest prefix that leaves room for the ellipsis for(std::size_t i = 0; i < text.size();) { const std::uint32_t codepoint = DecodeUtf8(text, i); if(codepoint != '\n') { width += measure.advance(face, codepoint, px); } if(codepoint == '\n' || width > maxWidthPx) { return std::format("{}…", text.substr(0, fit)); } if(width + ellipsis <= maxWidthPx) { fit = i; } } return std::string(text); } WordWrapper::WordWrapper(TextMeasure measure) : measure(std::move(measure)) { } const std::vector& WordWrapper::Wrap(std::string_view text, FontFace face, float px, float maxWidthPx) { // The font, the size to a quarter pixel and the width, then the text itself. std::string key = std::format("{} {} {} {}", static_cast(face), std::lround(px * 4), std::lround(maxWidthPx), text); if(const auto found = cache.find(key); found != cache.end()) { return found->second; } // A bounded cache without bookkeeping: once full everything goes, and the next frame fills in what it needs. if(cache.size() >= cacheLimit) { cache.clear(); } return cache.emplace(std::move(key), Compute(text, face, px, maxWidthPx)).first->second; } std::size_t WordWrapper::CacheSize() const noexcept { return cache.size(); } const TextMeasure& WordWrapper::Measure() const noexcept { return measure; } std::vector WordWrapper::Compute(std::string_view text, FontFace face, float px, float maxWidthPx) const { struct Break { std::size_t end; // where the line ends std::size_t next; // where the next line begins float width; // the line's width up to `end` }; // Where the line after a break opens: past the spaces there. Spaces that end the text, or that a hard break // follows, hang off the line just ended and open no line of their own: nothing is returned for the first, and the // newline is passed over for the second. const auto afterSpaces = [text](std::size_t from) -> std::optional { std::size_t begin = from; while(begin < text.size() && text[begin] == ' ') { begin++; } if(begin == text.size()) { return std::nullopt; } return text[begin] == '\n' ? begin + 1 : begin; }; std::vector lines; std::size_t lineBegin = 0; float lineWidth = 0; // A code point that is no space since lineBegin: before the first, a space is no break and opens no line. bool lineHasText = false; std::optional lastBreak; std::size_t i = 0; while(i < text.size()) { const std::size_t at = i; const std::uint32_t codepoint = DecodeUtf8(text, i); if(codepoint == '\n') { lines.push_back({lineBegin, at, lineWidth}); lineBegin = i; lineWidth = 0; lineHasText = false; lastBreak.reset(); continue; } const float width = measure.advance(face, codepoint, px); if(lineWidth + width > maxWidthPx && at > lineBegin) { if(lastBreak && !(codepoint == ' ' && lineHasText && lastBreak->next != at)) { // The line ends at its last break: before the run of spaces there, or after the hyphen. A space that // overflows right behind that run is part of it. lines.push_back({lineBegin, lastBreak->end, lastBreak->width}); if(codepoint == ' ' && lineHasText) { const std::optional next = afterSpaces(i); if(!next) { return lines; } lineBegin = *next; i = lineBegin; lineWidth = 0; lineHasText = false; } else { // The next line starts after the break, which is past the spaces there, with what of it was already // looked at: the code point that overflowed is looked at again, on its new line. lineBegin = lastBreak->next; i = at; lineWidth = TextWidth(measure, text.substr(lineBegin, at - lineBegin), face, px); lineHasText = at > lineBegin; } } else { // No break to go back to, or the space itself overflows: the line ends here, with every word that fit, or // in the middle of a word wider than the line. lines.push_back({lineBegin, at, lineWidth}); const std::optional next = afterSpaces(at); if(!next) { return lines; } lineBegin = *next; i = lineBegin; lineWidth = 0; lineHasText = false; } lastBreak.reset(); continue; } lineWidth += width; if(codepoint != ' ') { lineHasText = true; } if(codepoint == ' ' && lastBreak && lastBreak->next == at) { // A run of spaces is one break: the line ends before the run. lastBreak->next = i; } else if(codepoint == ' ' && lineHasText) { // Not before a line's first word: the line before such a break would be empty. lastBreak = Break{at, i, lineWidth - width}; } else if(codepoint == '-') { lastBreak = Break{i, i, lineWidth}; } } lines.push_back({lineBegin, text.size(), lineWidth}); return lines; }