Android-cuttlefish cvd tool
label.h
Go to the documentation of this file.
1/*
2 *
3 * Copyright 2019, The Android Open Source Project
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18#ifndef LIBTEEUI_LABEL_H_
19#define LIBTEEUI_LABEL_H_
20
21#include "utf8range.h"
22#include "utils.h"
23
24// #define DRAW_DEBUG_MARKERS
25
26namespace teeui {
27
28enum class Alignment : int8_t { LEFT, CENTER, RIGHT, TOP, BOTTOM };
29
31 const uint8_t* data_;
32 size_t size_;
33
34 public:
35 constexpr FontBuffer() : data_(nullptr), size_(0) {}
36 constexpr FontBuffer(const uint8_t* data, size_t size) noexcept : data_(data), size_(size) {}
37 template <size_t size>
38 explicit constexpr FontBuffer(const uint8_t (&data)[size]) noexcept
39 : data_(&data[0]), size_(size) {}
40 constexpr FontBuffer(const FontBuffer&) noexcept = default;
41 constexpr FontBuffer(FontBuffer&&) noexcept = default;
42 FontBuffer& operator=(FontBuffer&&) noexcept = default;
43 FontBuffer& operator=(const FontBuffer&) noexcept = default;
44
45 constexpr operator bool() const { return data_ != nullptr; }
46
47 const uint8_t* data() const { return data_; }
48 size_t size() const { return size_; }
49};
50
51class LabelImpl {
53
54 public:
55 struct LineInfo {
56 struct info_t {
59 };
60 size_t size_;
62 info_t* begin() { return &info_[0]; }
63 info_t* end() { return &info_[size_]; }
64 const info_t* begin() const { return &info_[0]; }
65 const info_t* end() const { return &info_[size_]; }
66 };
67
71 LabelImpl(pxs fontSize, pxs lineHeight, text_t text, Alignment horizontal,
72 Alignment verticalJustified, Color textColor, FontBuffer font, uint64_t textId)
73 : fontSize_(fontSize), lineHeight_(lineHeight), text_(text),
74 horizontalTextAlignment_(horizontal), verticalTextAlignment_(verticalJustified),
75 textColor_(textColor), font_(font), textId_(textId) {}
76
77 pxs fontSize() const { return fontSize_; }
78
80 void setTextColor(Color color) { textColor_ = color; }
81
82 text_t text() const { return text_; }
83 uint64_t textId() const { return textId_; }
84
85 Error draw(const PixelDrawer& drawPixel, const Box<pxs>& bounds, LineInfo* lineInfo);
86 void setCB(CallbackEvent cbEvent) { cbEvent_ = std::move(cbEvent); }
87 optional<CallbackEvent> getCB() { return cbEvent_; }
88 Error hit(const Event& event, const Box<pxs>& bounds);
89
90 private:
98 uint64_t textId_;
99 optional<CallbackEvent> cbEvent_;
100};
101
107template <typename Derived> class Label : public LayoutElement<Derived>, public LabelImpl {
108 public:
111 static const constexpr Color label_text_color = 0xff000000;
112 static const constexpr int label_font = 0;
113 static const constexpr uint64_t text_id = 0;
114
115 Label() = default;
116 template <typename Context>
117 Label(const Context& context)
118 : LayoutElement<Derived>(context),
119 LabelImpl(
120 context = Derived::label_font_size, context = Derived::label_line_height,
121 {&Derived::label_text[0], &Derived::label_text[sizeof(Derived::label_text) - 1]},
122 Derived::label_horizontal_text_alignment, Derived::label_vertical_text_alignment,
123 context = Derived::label_text_color, getFont(Derived::label_font), Derived::text_id) {
124 }
125
126 Error draw(const PixelDrawer& drawPixel) {
127 LabelImpl::LineInfo::info_t lines[Derived::label_number_of_lines];
128 LabelImpl::LineInfo lineInfo = {Derived::label_number_of_lines, lines};
129 return LabelImpl::draw(drawPixel, this->bounds_, &lineInfo);
130 }
131
132 Error hit(const Event& event) { return LabelImpl::hit(event, this->bounds_); }
133};
134
135} // namespace teeui
136
137#define FontSize(fs) static const constexpr auto label_font_size = fs
138
139#define DefaultText(text) static const constexpr char label_text[] = text
140
141#define LineHeight(height) static const constexpr auto label_line_height = height
142
143#define NumberOfLines(lines) static const constexpr auto label_number_of_lines = lines
144
145#define HeightFromLines (label_line_height * pxs(label_number_of_lines))
146
147#define HorizontalTextAlignment(horizontalAligment) \
148 static const constexpr Alignment label_horizontal_text_alignment = horizontalAligment;
149
150#define LeftJustified HorizontalTextAlignment(Alignment::LEFT)
151#define CenterJustified HorizontalTextAlignment(Alignment::CENTER)
152#define RightJustified HorizontalTextAlignment(Alignment::RIGHT)
153
154#define VerticalTextAlignment(verticalAligment) \
155 static const constexpr Alignment label_vertical_text_alignment = verticalAligment;
156
157#define VerticallyTop VerticalTextAlignment(Alignment::TOP)
158#define VerticallyCentered VerticalTextAlignment(Alignment::CENTER)
159
160#define TextColor(color) static const constexpr auto label_text_color = color
161
162#define FONT(name) TEEUI_FONT_##name()
163
164#define DECLARE_FONT_BUFFER(name, buffer, ...) \
165 struct TEEUI_FONT_##name {}; \
166 inline FontBuffer getFont(TEEUI_FONT_##name) { return FontBuffer(buffer, ##__VA_ARGS__); }
167
168#define Font(fontbuffer) static const constexpr auto label_font = fontbuffer
169
170#define TextID(tid) static const constexpr uint64_t text_id = tid
171
172#endif // LIBTEEUI_LABEL_H_
Definition: utils.h:722
Definition: error.h:26
Definition: label.h:30
constexpr FontBuffer()
Definition: label.h:35
constexpr FontBuffer(const FontBuffer &) noexcept=default
constexpr FontBuffer(const uint8_t(&data)[size]) noexcept
Definition: label.h:38
size_t size() const
Definition: label.h:48
const uint8_t * data_
Definition: label.h:31
size_t size_
Definition: label.h:32
const uint8_t * data() const
Definition: label.h:47
constexpr FontBuffer(const uint8_t *data, size_t size) noexcept
Definition: label.h:36
constexpr FontBuffer(FontBuffer &&) noexcept=default
Definition: label.h:51
LabelImpl()
Definition: label.h:68
optional< CallbackEvent > getCB()
Definition: label.h:87
pxs fontSize() const
Definition: label.h:77
pxs fontSize_
Definition: label.h:91
text_t text() const
Definition: label.h:82
void setText(text_t text)
Definition: label.h:79
void setCB(CallbackEvent cbEvent)
Definition: label.h:86
Error draw(const PixelDrawer &drawPixel, const Box< pxs > &bounds, LineInfo *lineInfo)
Definition: label.cpp:25
uint64_t textId() const
Definition: label.h:83
FontBuffer font_
Definition: label.h:97
Alignment verticalTextAlignment_
Definition: label.h:95
optional< CallbackEvent > cbEvent_
Definition: label.h:99
void setTextColor(Color color)
Definition: label.h:80
pxs lineHeight_
Definition: label.h:92
LabelImpl(pxs fontSize, pxs lineHeight, text_t text, Alignment horizontal, Alignment verticalJustified, Color textColor, FontBuffer font, uint64_t textId)
Definition: label.h:71
Alignment horizontalTextAlignment_
Definition: label.h:94
Error hit(const Event &event, const Box< pxs > &bounds)
Definition: label.cpp:155
text_t text_
Definition: label.h:93
Color textColor_
Definition: label.h:96
uint64_t textId_
Definition: label.h:98
Definition: label.h:107
static const constexpr Alignment label_horizontal_text_alignment
Definition: label.h:109
static const constexpr uint64_t text_id
Definition: label.h:113
static const constexpr int label_font
Definition: label.h:112
static const constexpr Color label_text_color
Definition: label.h:111
Label(const Context &context)
Definition: label.h:117
static const constexpr Alignment label_vertical_text_alignment
Definition: label.h:110
Error hit(const Event &event)
Definition: label.h:132
Error draw(const PixelDrawer &drawPixel)
Definition: label.h:126
Label()=default
Definition: utils.h:531
Definition: utils.h:437
Event event
Definition: kernel_log_server.cc:56
Definition: layout.h:28
Alignment
Definition: label.h:28
uint32_t Color
Definition: utils.h:594
Definition: utils.h:860
Definition: utils.h:854
text_t lineText
Definition: label.h:58
Point< pxs > lineStart
Definition: label.h:57
Definition: label.h:55
size_t size_
Definition: label.h:60
info_t * end()
Definition: label.h:63
info_t * begin()
Definition: label.h:62
const info_t * end() const
Definition: label.h:65
const info_t * begin() const
Definition: label.h:64
info_t * info_
Definition: label.h:61
Definition: utils.h:894
Box< pxs > bounds_
Definition: utils.h:895