Nostro/implementations/Nostro.Ui-Text.cpp
Jorijn van der Graaf 5b4037955b feat(app): add the Nostro app, a window for phones and desktops
Linux phones have no banking apps, and a command line is no answer on a
phone. The app does everything the nostro command does and, like it, only
talks to nostrod: it holds no key and no token.

- Nostro.Ui, a toolkit whose screens are functions that fill a frame, ported
  from imsd-messages ca5cd72d; Nostro.App, the app's state, forms and screens;
  Nostro.Ui.Shell, which puts them on a Wayland window with Crafter.Graphics;
  nostro-app; and Nostro.DebugCommands for the --debug-api build.
- Client: every method also in an asynchronous form for an sd-event loop,
  with DaemonMissing and OutcomeUnknown.
- nostrod's Flatpak is net.catcrafts.Nostrod now and the app takes
  net.catcrafts.Nostro; install.sh moves the state. The app's Flatpak, both
  Flatpaks cross-built for aarch64, and the licence texts they carry.
- nostro-fake-system, a made-up bus and bank with a real nostrod on them, to
  look at the app against. The README's screenshots are taken on it.
- The nostro command: an API key's environment is the key's own to say, so
  --sandbox is for bunq-oauth alone, and a virtual card is listed as VIRTUAL.

The published contract changes in three places, before any release (design
rule 10):
- Transactions' query takes `stored`: answered from what nostrod has stored,
  without asking the bank. The app searches with it; before, every search was
  a call to bunq.
- AddBunq's `environment` no longer chooses over what the key says of itself:
  a key of the other environment is refused.
- SetCardStatus refuses to freeze or unfreeze a virtual card. bunq reports one
  as DEACTIVATED however well it pays, and leaves it as it is when asked,
  without an error.

The app is GPL-3.0-only, the library stays LGPL-3.0-only, and no code moved
from one to the other (design rule 13). Transaction's schema is unchanged.

Verified: the 17 tests pass with every line, function and region of what they
link run, and the five lint runs are clean. Every screen was looked at on the
made-up system, at a phone's and a desktop's sizes in the light and the dark
look, on Plasma under Wayland. What is said of bunq's cards comes from its
sandbox, with throwaway users, and from one real virtual card. Known to have
been used against production: connecting, and reading accounts, payments and
cards. Not known to have been tried: a payment from the app against bunq,
OAuth against bunq, and anything on a phone.

Building the app takes --graphics=, a Crafter.Graphics checkout on its branch
claude/nostro-app, until that branch is merged and pinned here.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
2026-11-19 12:27:14 +01:00

245 lines
9 KiB
C++

// 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<std::uint8_t>(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<std::uint8_t>(text[i]) & 0xC0) != 0x80) {
return 0xFFFD;
}
codepoint = (codepoint << 6) | (static_cast<std::uint8_t>(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<std::uint8_t>(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<WrappedLine>& 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<std::uint8_t>(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<WrappedLine> 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> {
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<WrappedLine> 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<Break> 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<std::size_t> 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<std::size_t> 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;
}