#pragma once // Spec: SPEC.md UX-TEXT-01. Rich text: spans with their own font/color, word wrapping, line limit, tail // truncation, alignment. All values in points. #include #include #include "ui/Font.hpp" #include "ui/Geometry.hpp" namespace dp::ui { struct TextStyle { FontSpec font = FontSpec::body(); Color color = Color::rgb(0x000000); bool underline = false; bool strikethrough = false; std::optional background; // inline code chip std::string link; // non-empty: tappable run bool operator==(const TextStyle&) const = default; }; struct TextSpan { std::string text; TextStyle style; }; enum class TextAlign { Leading, Center, Trailing }; struct TextRun { std::string text; TextStyle style; float x = 0; float width = 0; }; struct TextLine { std::vector runs; float y = 0; float width = 0; float height = 0; float baseline = 0; }; struct TextBlock { std::vector lines; Size size; bool truncated = false; }; class TextLayout { public: // maxWidth: kInf for a single unwrapped line. lineLimit 0 = unlimited. static TextBlock layout(const std::vector& spans, float maxWidth, int lineLimit, TextAlign align, float lineSpacing = 0); // Widest unbreakable token (the minimum width a wrapping text can take). static float minWidth(const std::vector& spans); static std::vector codepoints(const std::string& utf8); }; } // namespace dp::ui