• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 #ifndef UPDATER_UI_TEXT_LABLE_H
16 #define UPDATER_UI_TEXT_LABLE_H
17 
18 #include <cstdio>
19 #include <iostream>
20 #include <string>
21 #include "frame.h"
22 #include "view.h"
23 
24 namespace updater {
25 constexpr int MAX_FONT_BUFFER_SIZE_HW  = 4096;
26 constexpr int MAX_TEXT_SIZE = 512;
27 constexpr int FONT_BUFFER_SIZE = 96;
28 const std::string DEFAULT_FONT_NAME = "font";
29 
30 class TextLabel : public View {
31 using ClickCallback = std::function<void(int id)>;
32 enum FontType {
33     DEFAULT_FONT,
34 };
35 
36 public:
37     enum AlignmentMethod {
38         ALIGN_CENTER,
39         ALIGN_TO_LEFT,
40         ALIGN_TO_TOP,
41     };
42 
43     TextLabel(int startX, int startY, int w, int h, Frame *parent);
~TextLabel()44     ~TextLabel() override {};
45     void SetText(const char *str);
46     void SetTextColor(BRGA888Pixel color);
47     void SetFont(FontType fType);
48     void SetOutLineBold(bool topBold, bool bottomBold);
49     void SetTextAlignmentMethod(AlignmentMethod methodH, AlignmentMethod methodV);
50     void SetOnClickCallback(ClickCallback cb);
51     void OnKeyEvent(int key) override;
52     void OnDraw() override;
53 private:
54     void InitFont();
55     void DrawText();
56     void DrawOutline();
57     void DrawFocus();
58 
59     ClickCallback callBack_;
60     char textBuf_[MAX_TEXT_SIZE + 1] {};
61     Frame *parent_ {};
62 
63     AlignmentMethod fontAligMethodLevel_ = ALIGN_TO_LEFT;
64     AlignmentMethod fontAligMethodUpright_ = ALIGN_CENTER;
65 
66     BRGA888Pixel outlineColor_ {};
67     BRGA888Pixel actionBgColor_ {};
68     BRGA888Pixel normalBgColor_ {};
69     BRGA888Pixel textColor_ {};
70 
71     bool boldTopLine_ = false;
72     bool boldBottomLine_ = false;
73 
74     FontType fontType_ { DEFAULT_FONT };
75     char fontBuf_[MAX_FONT_BUFFER_SIZE_HW * FONT_BUFFER_SIZE] {};
76     unsigned int fontWidth_ = 0;
77     unsigned int fontHeight_ = 0;
78     uint32_t offset_ = 2;
79     const int defaultFontWidth_ = 96;
80     const int defaultFontBitDepth_ = 8;
81     const int headerNumber_ = 8;
82 };
83 } // namespace updater
84 #endif
85