Pulp 1.88.0
Cross-platform audio plugin framework
Loading...
Searching...
No Matches
attributed_string.hpp
Go to the documentation of this file.
1#pragma once
2
3// AttributedString — styled text spans for rich text rendering.
4// Each span has font, color, size, weight, and decoration attributes.
5
7#include <string>
8#include <vector>
9
10namespace pulp::canvas {
11
14
16struct TextSpan {
17 std::string text;
18 std::string font_family = "system";
19 float font_size = 14.0f;
20 int font_weight = 400; // 100-900
21 bool italic = false;
22 int font_slant = 0; // 0 normal, 1 italic, 2 oblique
23 Color color = Color::rgba(255, 255, 255);
25 // True when this span explicitly overrides the surrounding decoration.
26 // In particular, an explicit `none` must be distinguishable from an
27 // omitted property that inherits the Label-wide decoration.
28 bool decoration_override = false;
30 float letter_spacing = 0;
31 bool inherit_font_family = false;
32 bool inherit_font_size = false;
33 bool inherit_font_weight = false;
34 bool inherit_font_slant = false;
35 bool inherit_color = false;
37};
38
41public:
42 AttributedString() = default;
43 explicit AttributedString(std::string text) {
44 append(std::move(text));
45 }
46
48 void append(std::string text) {
49 TextSpan span;
50 span.text = std::move(text);
51 spans_.push_back(std::move(span));
52 }
53
55 void append(TextSpan span) {
56 spans_.push_back(std::move(span));
57 }
58
60 void append(std::string text, Color color) {
61 TextSpan span;
62 span.text = std::move(text);
63 span.color = color;
64 spans_.push_back(std::move(span));
65 }
66
68 void append(std::string text, float font_size, Color color) {
69 TextSpan span;
70 span.text = std::move(text);
71 span.font_size = font_size;
72 span.color = color;
73 spans_.push_back(std::move(span));
74 }
75
77 const std::vector<TextSpan>& spans() const { return spans_; }
78
80 std::string plain_text() const {
81 std::string result;
82 for (auto& s : spans_)
83 result += s.text;
84 return result;
85 }
86
88 size_t length() const {
89 size_t len = 0;
90 for (auto& s : spans_)
91 len += s.text.size();
92 return len;
93 }
94
96 bool empty() const { return spans_.empty(); }
97
99 void clear() { spans_.clear(); }
100
102 void set_font(std::string family, float size) {
103 for (auto& s : spans_) {
104 s.font_family = family;
105 s.font_size = size;
106 }
107 }
108
110 void set_color(Color color) {
111 for (auto& s : spans_)
112 s.color = color;
113 }
114
115private:
116 std::vector<TextSpan> spans_;
117};
118
121 float x = 0, y = 0; // Position of this line
122 float width = 0, height = 0; // Dimensions
123 std::vector<TextSpan> spans; // Spans on this line
124};
125
127 std::vector<TextLayoutLine> lines;
128 float total_width = 0;
129 float total_height = 0;
130};
131
135 float line_height = 0);
136
137} // namespace pulp::canvas
Attributed string — a sequence of styled text spans.
Definition attributed_string.hpp:40
void append(std::string text, Color color)
Append text with specific color.
Definition attributed_string.hpp:60
void append(std::string text)
Append plain text with default styling.
Definition attributed_string.hpp:48
AttributedString(std::string text)
Definition attributed_string.hpp:43
size_t length() const
Total character count.
Definition attributed_string.hpp:88
void clear()
Clear all spans.
Definition attributed_string.hpp:99
void append(TextSpan span)
Append a fully styled span.
Definition attributed_string.hpp:55
void set_font(std::string family, float size)
Set font for all spans.
Definition attributed_string.hpp:102
void set_color(Color color)
Set color for all spans.
Definition attributed_string.hpp:110
bool empty() const
Whether empty.
Definition attributed_string.hpp:96
void append(std::string text, float font_size, Color color)
Append text with specific font size and color.
Definition attributed_string.hpp:68
const std::vector< TextSpan > & spans() const
Get all spans.
Definition attributed_string.hpp:77
std::string plain_text() const
Get the full plain text (concatenated).
Definition attributed_string.hpp:80
Definition affine_transform.hpp:24
TextLayout layout_attributed_string(const AttributedString &text, float max_width, float line_height=0)
Lay out an attributed string within a given width.
@ text
Definition scene_node.hpp:96
TextDecoration
Text decoration style.
Definition attributed_string.hpp:13
@ overline
Definition attributed_string.hpp:13
@ none
Definition attributed_string.hpp:13
@ underline
Definition attributed_string.hpp:13
@ strikethrough
Definition attributed_string.hpp:13
Definition canvas.hpp:52
static constexpr Color rgba(float r, float g, float b, float a=1.0f)
Construct from float channels [0,1] (>1.0 allowed for HDR).
Definition canvas.hpp:56
Multi-line text layout result.
Definition attributed_string.hpp:120
float x
Definition attributed_string.hpp:121
float width
Definition attributed_string.hpp:122
std::vector< TextSpan > spans
Definition attributed_string.hpp:123
float y
Definition attributed_string.hpp:121
float height
Definition attributed_string.hpp:122
Definition attributed_string.hpp:126
std::vector< TextLayoutLine > lines
Definition attributed_string.hpp:127
float total_height
Definition attributed_string.hpp:129
float total_width
Definition attributed_string.hpp:128
A span of text with consistent styling.
Definition attributed_string.hpp:16
int font_slant
Definition attributed_string.hpp:22
TextDecoration decoration
Definition attributed_string.hpp:24
float letter_spacing
Definition attributed_string.hpp:30
bool italic
Definition attributed_string.hpp:21
std::string text
Definition attributed_string.hpp:17
bool inherit_letter_spacing
Definition attributed_string.hpp:36
Color color
Definition attributed_string.hpp:23
bool inherit_color
Definition attributed_string.hpp:35
bool inherit_font_weight
Definition attributed_string.hpp:33
std::string font_family
Definition attributed_string.hpp:18
Color decoration_color
Definition attributed_string.hpp:29
int font_weight
Definition attributed_string.hpp:20
bool decoration_override
Definition attributed_string.hpp:28
bool inherit_font_family
Definition attributed_string.hpp:31
bool inherit_font_slant
Definition attributed_string.hpp:34
float font_size
Definition attributed_string.hpp:19
bool inherit_font_size
Definition attributed_string.hpp:32