4#include "TinyMaterialDesign/Core/Widget.h"
5#include "TinyMaterialDesign/Widgets/Label.h"
10
11
12
13
14
15
16
17
18
19
20
29 this->bounds = bounds;
32 void setText(
const char* text) { text_ = text !=
nullptr ? text :
""; }
33 const std::string&
text()
const {
return text_; }
45 void draw(tinygpu::ISurface<RGB_T>& target)
override {
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,
62 tinygpu::IFont<RGB_T>& font = *fontForTypographyRole(role_,
this->theme());
63 return lineCount(text_, width, font) * (
static_cast<int32_t>(font.getHeight(1)) + lineSpacing_);
67 static int32_t
lineCount(
const std::string& text, int32_t width, tinygpu::IFont<RGB_T>& font) {
69 forEachLine(text, width, font, [&](
const std::string&) { lines++; });
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') {
85 const std::string candidate = line.empty() ? word : line +
" " + word;
86 if (!line.empty() && !fits(candidate)) {
99 if (!line.empty()) fn(line);
106 bool hasColor_ =
false;
107 bool onSurface_ =
false;
108 int32_t lineSpacing_ = 3;
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
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
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