TinyMaterialDesign
Loading...
Searching...
No Matches
TextBlock.h
Go to the documentation of this file.
1#pragma once
2#include <string>
3
4#include "TinyMaterialDesign/Core/Widget.h"
5#include "TinyMaterialDesign/Widgets/Label.h"
6
7namespace tinymd {
8
9/**
10 * @brief Multi-line text that is wrapped at word boundaries to the widget
11 * width ('\n' starts a new line) - for notes, explanations and messages.
12 * Unlike Label it can span several lines; heightFor() tells how much room
13 * the text needs, so a layout can size the widget to its text:
14 *
15 * TextBlock<RGB565> note(Bounds(8, y, 224, 0), "A long explanation ...");
16 * page.addChild(note); // gives it the theme
17 * note.bounds.h = note.heightFor(note.bounds.w);
18 *
19 * Default: label font, onSurfaceVariant color, drawn on the page background.
20 */
21template <typename RGB_T = TINYMD_DEFAULT_RGB_T>
22class TextBlock : public Widget<RGB_T> {
23 public:
24 TextBlock() = default;
25 explicit TextBlock(const char* text, TypographyRole role = TypographyRole::kLabel)
26 : text_(text != nullptr ? text : ""), role_(role) {}
27 TextBlock(Bounds bounds, const char* text, TypographyRole role = TypographyRole::kLabel)
28 : text_(text != nullptr ? text : ""), role_(role) {
29 this->bounds = bounds;
30 }
31
32 void setText(const char* text) { text_ = text != nullptr ? text : ""; }
33 const std::string& text() const { return text_; }
34 void setRole(TypographyRole role) { role_ = role; }
35 void setColor(RGB_T color) {
36 color_ = color;
37 hasColor_ = true;
38 }
39 void clearColor() { hasColor_ = false; }
40 /// Draw on a surface (e.g. inside a Card) instead of the page background.
41 void setOnSurface(bool onSurface) { onSurface_ = onSurface; }
42 /// Extra pixels between the lines.
43 void setLineSpacing(int32_t pixels) { lineSpacing_ = pixels; }
44
45 void draw(tinygpu::ISurface<RGB_T>& target) override {
46 const MaterialTheme<RGB_T>& t = this->theme();
47 tinygpu::IFont<RGB_T>& font = *fontForTypographyRole(role_, t);
48 const RGB_T background = onSurface_ ? t.colors.surface : t.colors.background;
49 RGB_T color = hasColor_ ? color_ : t.colors.onSurfaceVariant;
50 if (!this->enabled) color = blend(color, background, 0.5f);
51 int32_t y = this->bounds.y;
52 const int32_t lineHeight = static_cast<int32_t>(font.getHeight(1)) + lineSpacing_;
53 forEachLine(text_, this->bounds.w, font, [&](const std::string& line) {
54 font.drawText(target, static_cast<int16_t>(this->bounds.x), static_cast<int16_t>(y), line.c_str(), color,
55 background, false);
56 y += lineHeight;
57 });
58 }
59
60 /// Height that the text needs at the given width (needs the theme).
61 int32_t heightFor(int32_t width) const {
62 tinygpu::IFont<RGB_T>& font = *fontForTypographyRole(role_, this->theme());
63 return lineCount(text_, width, font) * (static_cast<int32_t>(font.getHeight(1)) + lineSpacing_);
64 }
65
66 /// Number of lines of `text` wrapped to `width` with `font`.
67 static int32_t lineCount(const std::string& text, int32_t width, tinygpu::IFont<RGB_T>& font) {
68 int32_t lines = 0;
69 forEachLine(text, width, font, [&](const std::string&) { lines++; });
70 return lines;
71 }
72
73 /// Calls fn(line) for each line of `text` wrapped to `width`.
74 template <typename Fn>
75 static void forEachLine(const std::string& text, int32_t width, tinygpu::IFont<RGB_T>& font, Fn fn) {
76 std::string line, word;
77 auto fits = [&](const std::string& s) { return static_cast<int32_t>(font.measureTextWidth(s.c_str())) <= width; };
78 for (size_t i = 0; i <= text.size(); ++i) {
79 const char c = i < text.size() ? text[i] : ' ';
80 if (c != ' ' && c != '\n') {
81 word += c;
82 continue;
83 }
84 if (!word.empty()) {
85 const std::string candidate = line.empty() ? word : line + " " + word;
86 if (!line.empty() && !fits(candidate)) {
87 fn(line);
88 line = word;
89 } else {
90 line = candidate;
91 }
92 word.clear();
93 }
94 if (c == '\n') {
95 fn(line); // also empty lines
96 line.clear();
97 }
98 }
99 if (!line.empty()) fn(line);
100 }
101
102 private:
103 std::string text_;
105 RGB_T color_{};
106 bool hasColor_ = false;
107 bool onSurface_ = false;
108 int32_t lineSpacing_ = 3;
109};
110
111} // namespace tinymd
Multi-line text that is wrapped at word boundaries to the widget width (' ' starts a new line) - for ...
Definition: TextBlock.h:22
static void forEachLine(const std::string &text, int32_t width, tinygpu::IFont< RGB_T > &font, Fn fn)
Calls fn(line) for each line of text wrapped to width.
Definition: TextBlock.h:75
int32_t heightFor(int32_t width) const
Height that the text needs at the given width (needs the theme).
Definition: TextBlock.h:61
void setRole(TypographyRole role)
Definition: TextBlock.h:34
TextBlock(Bounds bounds, const char *text, TypographyRole role=TypographyRole::kLabel)
Definition: TextBlock.h:27
TextBlock()=default
void setOnSurface(bool onSurface)
Draw on a surface (e.g. inside a Card) instead of the page background.
Definition: TextBlock.h:41
void setText(const char *text)
Definition: TextBlock.h:32
void clearColor()
Definition: TextBlock.h:39
void draw(tinygpu::ISurface< RGB_T > &target) override
Definition: TextBlock.h:45
void setColor(RGB_T color)
Definition: TextBlock.h:35
const std::string & text() const
Definition: TextBlock.h:33
static int32_t lineCount(const std::string &text, int32_t width, tinygpu::IFont< RGB_T > &font)
Number of lines of text wrapped to width with font.
Definition: TextBlock.h:67
void setLineSpacing(int32_t pixels)
Extra pixels between the lines.
Definition: TextBlock.h:43
TextBlock(const char *text, TypographyRole role=TypographyRole::kLabel)
Definition: TextBlock.h:25
Base class every TinyMaterialDesign widget derives from.
Definition: Widget.h:58
Definition: AnchorLayout.h:4
TypographyRole
Definition: Label.h:11
Axis-aligned pixel rectangle used for widget bounds and hit-testing.
Definition: Bounds.h:21
Bundles a color scheme, shape tokens, typography, and spacing into one theme, passed to widgets so th...
Definition: MaterialTheme.h:50