roo_display
API Documentation for roo_display
Loading...
Searching...
No Matches
text_label.h
Go to the documentation of this file.
1#pragma once
2
3#include <inttypes.h>
4
5#include <string>
6
7#include "roo_backport.h"
8#include "roo_backport/string_view.h"
12#include "roo_display/ui/tile.h"
13
14namespace roo_display {
15
16/// Single-line, single-color text label.
17///
18/// Extents have height based on the font size, not the specific text. The
19/// origin is at the text baseline, and horizontally affected by the text
20/// bearing.
21///
22/// When using smooth fonts, you should generally specify `bgcolor` when
23/// drawing text labels, even over pre-filled solid backgrounds. This is
24/// because many device drivers don't support alpha blending, which is required
25/// for smooth font edges. Exceptions: offscreen/double-buffered devices, or
26/// drivers that do support alpha blending, when you want to preserve the
27/// existing non-solid background.
28///
29/// For changing text fields, wrap `TextLabel` in a `Tile` to reduce flicker.
30///
31/// See also `StringViewLabel`.
32class TextLabel : public Drawable {
33 public:
34 /// Construct from a string-like value (moves into internal storage).
35 template <typename String>
39
40 /// Construct from an owned string.
41 TextLabel(std::string label, const Font& font, Color color,
43 : font_(&font),
44 label_(std::move(label)),
45 color_(color),
46 fill_mode_(fill_mode),
47 metrics_(font.getHorizontalStringMetrics(label_)) {}
48
49 void drawTo(const Surface& s) const override {
50 Surface news = s;
52 news.set_fill_mode(FillMode::kExtents);
53 }
55 }
56
57 Box extents() const override { return metrics_.screen_extents(); }
58
59 Box anchorExtents() const override {
60 return Box(0, -font().metrics().ascent() - font().metrics().linegap(),
61 metrics_.advance() - 1, -font().metrics().descent());
62 }
63
64 /// Return the font used by the label.
65 const Font& font() const { return *font_; }
66 /// Return cached string metrics.
67 const GlyphMetrics& metrics() const { return metrics_; }
68 /// Return the label text.
69 const std::string& label() const { return label_; }
70 /// Return the label color.
71 const Color color() const { return color_; }
72 /// Return the fill mode.
73 const FillMode fill_mode() const { return fill_mode_; }
74
75 /// Set the label color.
76 void setColor(Color color) { color_ = color; }
77 /// Set the fill mode.
78 void setFillMode(FillMode fill_mode) { fill_mode_ = fill_mode; }
79
80 private:
81 const Font* font_;
82 std::string label_;
83 Color color_;
84 FillMode fill_mode_;
85 GlyphMetrics metrics_;
86};
87
88/// Single-line, single-color label with tight extents.
89///
90/// Extents are the smallest rectangle that fits all rendered pixels. The
91/// origin is at the text baseline and horizontally affected by the bearing.
93 public:
95
96 void drawTo(const Surface& s) const override {
97 Surface news(s);
99 news.set_fill_mode(FillMode::kExtents);
100 }
101 // news.clipToExtents(metrics().screen_extents());
103 }
104
105 Box anchorExtents() const override { return metrics().screen_extents(); }
106};
107
108/// Like `TextLabel`, but does not own the text content.
109///
110/// Uses no dynamic allocation. Ideal for string literals or temporary labels.
111class StringViewLabel : public Drawable {
112 public:
113 /// Construct from a string-like value without copying.
114 template <typename String>
119
120 /// Construct from a `string_view`.
121 StringViewLabel(roo::string_view label, const Font& font, Color color,
123 : font_(&font),
124 label_(std::move(label)),
125 color_(color),
126 fill_mode_(fill_mode),
127 metrics_(font.getHorizontalStringMetrics(label)) {}
128
129 void drawTo(const Surface& s) const override {
130 Surface news = s;
131 if (fill_mode() == FillMode::kExtents) {
132 news.set_fill_mode(FillMode::kExtents);
133 }
135 }
136
137 Box extents() const override { return metrics_.screen_extents(); }
138
139 Box anchorExtents() const override {
140 return Box(0, -font().metrics().ascent() - font().metrics().linegap(),
141 metrics_.advance() - 1, -font().metrics().descent());
142 }
143
144 /// Return the font used by the label.
145 const Font& font() const { return *font_; }
146 /// Return cached string metrics.
147 const GlyphMetrics& metrics() const { return metrics_; }
148 /// Return the label text view.
149 const roo::string_view label() const { return label_; }
150 /// Return the label color.
151 const Color color() const { return color_; }
152 /// Return the fill mode.
153 const FillMode fill_mode() const { return fill_mode_; }
154
155 /// Set the label color.
156 void setColor(Color color) { color_ = color; }
157 /// Set the fill mode.
158 void setFillMode(FillMode fill_mode) { fill_mode_ = fill_mode; }
159
160 private:
161 const Font* font_;
162 roo::string_view label_;
163 Color color_;
164 FillMode fill_mode_;
165 GlyphMetrics metrics_;
166};
167
168/// Like `ClippedTextLabel`, but does not own the text content.
169///
170/// Uses no dynamic allocation. Ideal for string literals or temporary labels.
172 public:
174
175 void drawTo(const Surface& s) const override {
176 Surface news(s);
177 if (fill_mode() == FillMode::kExtents) {
178 news.set_fill_mode(FillMode::kExtents);
179 }
181 }
182
183 Box extents() const override { return metrics().screen_extents(); }
184};
185
186} // namespace roo_display
Axis-aligned integer rectangle.
Definition box.h:12
Like ClippedTextLabel, but does not own the text content.
Definition text_label.h:171
Box extents() const override
Return the bounding box encompassing all pixels that need to be drawn.
Definition text_label.h:183
void drawTo(const Surface &s) const override
Draw this object's content, respecting the fill mode.
Definition text_label.h:175
Single-line, single-color label with tight extents.
Definition text_label.h:92
void drawTo(const Surface &s) const override
Draw this object's content, respecting the fill mode.
Definition text_label.h:96
Box anchorExtents() const override
Return the bounds used for alignment.
Definition text_label.h:105
ARGB8888 color stored as a 32-bit unsigned integer.
Definition color.h:16
Interface for objects that can be drawn to an output device.
Definition drawable.h:229
Abstract font interface.
Definition font.h:185
void drawHorizontalString(const Surface &s, roo::string_view text, Color color) const
Draw a UTF-8 string horizontally using a string view.
Definition font.h:209
Per-glyph metrics (bounding box and advance).
Definition font.h:139
int advance() const
Advance in pixels.
Definition font.h:177
const Box & screen_extents() const
Bounding box in screen coordinates (Y down).
Definition font.h:154
Like TextLabel, but does not own the text content.
Definition text_label.h:111
void drawTo(const Surface &s) const override
Draw this object's content, respecting the fill mode.
Definition text_label.h:129
const Font & font() const
Return the font used by the label.
Definition text_label.h:145
const GlyphMetrics & metrics() const
Return cached string metrics.
Definition text_label.h:147
void setFillMode(FillMode fill_mode)
Set the fill mode.
Definition text_label.h:158
const roo::string_view label() const
Return the label text view.
Definition text_label.h:149
void setColor(Color color)
Set the label color.
Definition text_label.h:156
Box extents() const override
Return the bounding box encompassing all pixels that need to be drawn.
Definition text_label.h:137
Box anchorExtents() const override
Return the bounds used for alignment.
Definition text_label.h:139
const FillMode fill_mode() const
Return the fill mode.
Definition text_label.h:153
const Color color() const
Return the label color.
Definition text_label.h:151
StringViewLabel(roo::string_view label, const Font &font, Color color, FillMode fill_mode=FillMode::kVisible)
Construct from a string_view.
Definition text_label.h:121
StringViewLabel(String &label, const Font &font, const Color color, FillMode fill_mode=FillMode::kVisible)
Construct from a string-like value without copying.
Definition text_label.h:115
Low-level handle used to draw to an underlying device.
Definition drawable.h:60
Single-line, single-color text label.
Definition text_label.h:32
const Color color() const
Return the label color.
Definition text_label.h:71
void setColor(Color color)
Set the label color.
Definition text_label.h:76
TextLabel(std::string label, const Font &font, Color color, FillMode fill_mode=FillMode::kVisible)
Construct from an owned string.
Definition text_label.h:41
const GlyphMetrics & metrics() const
Return cached string metrics.
Definition text_label.h:67
void drawTo(const Surface &s) const override
Draw this object's content, respecting the fill mode.
Definition text_label.h:49
const Font & font() const
Return the font used by the label.
Definition text_label.h:65
const FillMode fill_mode() const
Return the fill mode.
Definition text_label.h:73
Box anchorExtents() const override
Return the bounds used for alignment.
Definition text_label.h:59
const std::string & label() const
Return the label text.
Definition text_label.h:69
Box extents() const override
Return the bounding box encompassing all pixels that need to be drawn.
Definition text_label.h:57
void setFillMode(FillMode fill_mode)
Set the fill mode.
Definition text_label.h:78
TextLabel(const String &label, const Font &font, Color color, FillMode fill_mode=FillMode::kVisible)
Construct from a string-like value (moves into internal storage).
Definition text_label.h:36
Defines 140 opaque HTML named colors.
FillMode
Specifies whether a Drawable should fill its entire extents box, including fully transparent pixels.
Definition drawable.h:15
@ kVisible
Fully transparent pixels do not need to be filled.
@ kExtents
Fill the entire extents box (possibly with fully transparent pixels).