Pulp 1.88.0
Cross-platform audio plugin framework
Loading...
Searching...
No Matches
text_layout.hpp
Go to the documentation of this file.
1#pragma once
2
4#include <string>
5#include <vector>
6
7namespace pulp::canvas {
8
10struct FontSpec {
11 std::string family = "Inter";
12 float size = 14.0f;
13 int weight = 400;
14 bool italic = false;
15 float letter_spacing = 0.0f;
16};
17
19struct TextRun {
20 std::string text;
23 size_t start = 0;
24 size_t length = 0;
25};
26
29 float x = 0, y = 0;
30 float advance = 0;
31 uint32_t codepoint = 0;
32 size_t text_index = 0;
33};
34
36struct TextLine {
37 std::vector<GlyphPosition> glyphs;
38 float width = 0;
39 float ascent = 0;
40 float descent = 0;
41 float leading = 0;
42 float baseline_y = 0;
43};
44
54public:
55 GlyphArrangement() = default;
56
58 void add_line(TextLine line) { lines_.push_back(std::move(line)); }
59
61 const std::vector<TextLine>& lines() const { return lines_; }
62 int line_count() const { return static_cast<int>(lines_.size()); }
63
65 float total_width() const {
66 float w = 0;
67 for (auto& line : lines_) w = std::max(w, line.width);
68 return w;
69 }
70
71 float total_height() const {
72 if (lines_.empty()) return 0;
73 auto& last = lines_.back();
74 return last.baseline_y + last.descent;
75 }
76
78 size_t hit_test(float x, float y) const;
79
81 float position_for_index(size_t index) const;
82
84 void clear() { lines_.clear(); }
85
86private:
87 std::vector<TextLine> lines_;
88};
89
101 const FontSpec& font,
102 float max_width = 0,
103 Color color = Color::rgba(1.0f, 1.0f, 1.0f));
104
107 float x0 = 0, y0 = 0;
108 float x1 = 0, y1 = 0;
109 float x2 = 0, y2 = 0;
110 float x3 = 0, y3 = 0;
111
113 static Parallelogram from_rect_shear(float x, float y, float w, float h, float shear) {
114 return {x + shear, y, x + w + shear, y, x + w, y + h, x, y + h};
115 }
116
118 bool contains(float px, float py) const {
119 // Cross product method for convex quad
120 auto cross = [](float ax, float ay, float bx, float by) { return ax * by - ay * bx; };
121 float d1 = cross(x1 - x0, y1 - y0, px - x0, py - y0);
122 float d2 = cross(x2 - x1, y2 - y1, px - x1, py - y1);
123 float d3 = cross(x3 - x2, y3 - y2, px - x2, py - y2);
124 float d4 = cross(x0 - x3, y0 - y3, px - x3, py - y3);
125 return (d1 >= 0 && d2 >= 0 && d3 >= 0 && d4 >= 0) ||
126 (d1 <= 0 && d2 <= 0 && d3 <= 0 && d4 <= 0);
127 }
128};
129
130} // namespace pulp::canvas
Glyph arrangement — the result of text layout, containing positioned glyphs for each line.
Definition text_layout.hpp:53
float total_height() const
Definition text_layout.hpp:71
float total_width() const
Get the bounding box of all laid-out text.
Definition text_layout.hpp:65
size_t hit_test(float x, float y) const
Find the text index nearest to a point (for cursor positioning).
void clear()
Clear all layout data.
Definition text_layout.hpp:84
void add_line(TextLine line)
Add a line of laid-out text.
Definition text_layout.hpp:58
const std::vector< TextLine > & lines() const
Get all lines.
Definition text_layout.hpp:61
float position_for_index(size_t index) const
Get the x position for a given text index (for cursor rendering).
int line_count() const
Definition text_layout.hpp:62
Definition affine_transform.hpp:24
GlyphArrangement layout_paragraph(const std::string &text, const FontSpec &font, float max_width=0, Color color=Color::rgba(1.0f, 1.0f, 1.0f))
Layout a paragraph of text with word wrapping.
@ text
Definition scene_node.hpp:96
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
Font style specification for text layout.
Definition text_layout.hpp:10
int weight
100-900, 400=normal, 700=bold
Definition text_layout.hpp:13
bool italic
Definition text_layout.hpp:14
float size
Definition text_layout.hpp:12
std::string family
Definition text_layout.hpp:11
float letter_spacing
Definition text_layout.hpp:15
Result of laying out a line of text — position and metrics for each glyph.
Definition text_layout.hpp:28
float x
Definition text_layout.hpp:29
size_t text_index
Index into the source text.
Definition text_layout.hpp:32
float advance
Horizontal advance to next glyph.
Definition text_layout.hpp:30
float y
Position relative to the line origin.
Definition text_layout.hpp:29
uint32_t codepoint
Unicode codepoint.
Definition text_layout.hpp:31
Parallelogram — a skewed rectangle defined by base point and two edge vectors.
Definition text_layout.hpp:106
float x2
Definition text_layout.hpp:109
bool contains(float px, float py) const
Check if a point is inside.
Definition text_layout.hpp:118
float x1
Definition text_layout.hpp:108
float y0
Top-left corner.
Definition text_layout.hpp:107
float y1
Top-right corner.
Definition text_layout.hpp:108
static Parallelogram from_rect_shear(float x, float y, float w, float h, float shear)
Create from a rectangle with horizontal shear.
Definition text_layout.hpp:113
float y3
Bottom-left corner.
Definition text_layout.hpp:110
float x3
Definition text_layout.hpp:110
float x0
Definition text_layout.hpp:107
float y2
Bottom-right corner.
Definition text_layout.hpp:109
A single line of laid-out text.
Definition text_layout.hpp:36
float descent
Distance below baseline.
Definition text_layout.hpp:40
std::vector< GlyphPosition > glyphs
Definition text_layout.hpp:37
float leading
Extra spacing between lines.
Definition text_layout.hpp:41
float width
Total line width.
Definition text_layout.hpp:38
float ascent
Distance above baseline.
Definition text_layout.hpp:39
float baseline_y
Y position of baseline in the layout.
Definition text_layout.hpp:42
A run of text with uniform style within a paragraph.
Definition text_layout.hpp:19
Color color
Definition text_layout.hpp:22
std::string text
Definition text_layout.hpp:20
FontSpec font
Definition text_layout.hpp:21
size_t length
Length in code units.
Definition text_layout.hpp:24
size_t start
Start index in the full text.
Definition text_layout.hpp:23