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