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 "font_rendering.h" 22 #include "utils.h" 23 24 // #define DRAW_DEBUG_MARKERS 25 26 namespace teeui { 27 28 class FontBuffer { 29 const uint8_t* data_; 30 size_t size_; 31 32 public: FontBuffer()33 constexpr FontBuffer() : data_(nullptr), size_(0) {} FontBuffer(const uint8_t * data,size_t size)34 constexpr FontBuffer(const uint8_t* data, size_t size) noexcept : data_(data), size_(size) {} 35 template <size_t size> FontBuffer(const uint8_t (& data)[size])36 explicit constexpr FontBuffer(const uint8_t (&data)[size]) noexcept 37 : data_(&data[0]), size_(size) {} 38 constexpr FontBuffer(const FontBuffer&) noexcept = default; 39 constexpr FontBuffer(FontBuffer&&) noexcept = default; 40 FontBuffer& operator=(FontBuffer&&) noexcept = default; 41 FontBuffer& operator=(const FontBuffer&) noexcept = default; 42 43 constexpr operator bool() const { return data_ != nullptr; } 44 data()45 const uint8_t* data() const { return data_; } size()46 size_t size() const { return size_; } 47 }; 48 49 class LabelImpl { 50 using text_t = UTF8Range<const char*>; 51 52 public: 53 struct LineInfo { 54 struct info_t { 55 Point<pxs> lineStart; 56 text_t lineText; 57 }; 58 size_t size_; 59 info_t* info_; beginLineInfo60 info_t* begin() { return &info_[0]; } endLineInfo61 info_t* end() { return &info_[size_]; } beginLineInfo62 const info_t* begin() const { return &info_[0]; } endLineInfo63 const info_t* end() const { return &info_[size_]; } 64 }; 65 LabelImpl()66 LabelImpl() 67 : fontSize_(10_px), lineHeight_(12_px), text_{}, rightJustified_(false), 68 verticallyCentered_(false), textColor_(0), font_{}, textId_(0) {} LabelImpl(pxs fontSize,pxs lineHeight,text_t text,bool rightJustified,bool verticallyCentered,Color textColor,FontBuffer font,uint64_t textId)69 LabelImpl(pxs fontSize, pxs lineHeight, text_t text, bool rightJustified, 70 bool verticallyCentered, Color textColor, FontBuffer font, uint64_t textId) 71 : fontSize_(fontSize), lineHeight_(lineHeight), text_(text), 72 rightJustified_(rightJustified), verticallyCentered_(verticallyCentered), 73 textColor_(textColor), font_(font), textId_(textId) {} 74 fontSize()75 pxs fontSize() const { return fontSize_; } 76 setText(text_t text)77 void setText(text_t text) { text_ = text; } setTextColor(Color color)78 void setTextColor(Color color) { textColor_ = color; } 79 text()80 text_t text() const { return text_; } textId()81 uint64_t textId() const { return textId_; } 82 83 Error draw(const PixelDrawer& drawPixel, const Box<pxs>& bounds, LineInfo* lineInfo); 84 85 private: 86 pxs fontSize_; 87 pxs lineHeight_; 88 text_t text_; 89 bool rightJustified_; 90 bool verticallyCentered_; 91 Color textColor_; 92 FontBuffer font_; 93 uint64_t textId_; 94 }; 95 96 /** 97 * Label is a LayoutElement and should be used as second argument in the BEGIN_ELEMENT() macro. 98 * The template argument Derived is the new class derived from Label, that is created by the 99 * BEGIN_ELEMENT() macro. 100 */ 101 template <typename Derived> class Label : public LayoutElement<Derived>, public LabelImpl { 102 public: 103 static const constexpr bool label_right_justified = false; 104 static const constexpr bool label_vertically_centered = false; 105 static const constexpr Color label_text_color = 0xff000000; 106 static const constexpr int label_font = 0; 107 static const constexpr uint64_t text_id = 0; 108 109 Label() = default; 110 template <typename Context> Label(const Context & context)111 Label(const Context& context) 112 : LayoutElement<Derived>(context), 113 LabelImpl( 114 context = Derived::label_font_size, context = Derived::label_line_height, 115 {&Derived::label_text[0], &Derived::label_text[sizeof(Derived::label_text) - 1]}, 116 Derived::label_right_justified, Derived::label_vertically_centered, 117 context = Derived::label_text_color, getFont(Derived::label_font), Derived::text_id) { 118 } 119 draw(const PixelDrawer & drawPixel)120 Error draw(const PixelDrawer& drawPixel) { 121 LabelImpl::LineInfo::info_t lines[Derived::label_number_of_lines]; 122 LabelImpl::LineInfo lineInfo = {Derived::label_number_of_lines, lines}; 123 return LabelImpl::draw(drawPixel, this->bounds_, &lineInfo); 124 } 125 }; 126 127 } // namespace teeui 128 129 #define FontSize(fs) static const constexpr auto label_font_size = fs 130 131 #define DefaultText(text) static const constexpr char label_text[] = text 132 133 #define LineHeight(height) static const constexpr auto label_line_height = height 134 135 #define NumberOfLines(lines) static const constexpr auto label_number_of_lines = lines 136 137 #define HeightFromLines (label_line_height * pxs(label_number_of_lines)) 138 139 #define RightJustified static const constexpr bool label_right_justified = true 140 141 #define VerticallyCentered static const constexpr bool label_vertically_centered = true 142 143 #define TextColor(color) static const constexpr auto label_text_color = color 144 145 #define FONT(name) TEEUI_FONT_##name() 146 147 #define DECLARE_FONT_BUFFER(name, buffer, ...) \ 148 struct TEEUI_FONT_##name {}; \ 149 inline FontBuffer getFont(TEEUI_FONT_##name) { return FontBuffer(buffer, ##__VA_ARGS__); } 150 151 #define Font(fontbuffer) static const constexpr auto label_font = fontbuffer 152 153 #define TextID(tid) static const constexpr uint64_t text_id = tid 154 155 #endif // LIBTEEUI_LABEL_H_ 156