1 /*
2  * Copyright (c) 2020-2022 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 OHOS_ACELITE_TEXT_COMPONENT_H
16 #define OHOS_ACELITE_TEXT_COMPONENT_H
17 
18 #include "component.h"
19 
20 #include "js_app_context.h"
21 #include "js_fwk_common.h"
22 #include "non_copyable.h"
23 #include "ui_label.h"
24 #ifdef FEATURE_EXTRA_TEXT_X_SUPPORT
25 #include "ui_label_x.h"
26 #define UI_LABEL_TYPE_WRAPPER UILabelX
27 #else
28 #define UI_LABEL_TYPE_WRAPPER UILabel
29 #endif // FEATURE_EXTRA_TEXT_X_SUPPORT
30 
31 namespace OHOS {
32 namespace ACELite {
33 #if (defined(FEATURE_COMPONENT_TEXT_SPANNABLE) && (FEATURE_COMPONENT_TEXT_SPANNABLE == 1))
34 struct AbsoluteSizeSpan {
35     int16_t start;
36     int16_t end;
37     uint8_t size;
AbsoluteSizeSpanAbsoluteSizeSpan38     AbsoluteSizeSpan() : start(-1), end(-1), size(0) {}
39 };
40 
41 struct RelativeSizeSpan {
42     int16_t start;
43     int16_t end;
44     float size;
RelativeSizeSpanRelativeSizeSpan45     RelativeSizeSpan() : start(-1), end(-1), size(0) {}
46 };
47 
48 struct StringStyleSpan {
49     int16_t start;
50     int16_t end;
51     TextStyle style;
StringStyleSpanStringStyleSpan52     StringStyleSpan() : start(-1), end(-1), style(TextStyle::TEXT_STYLE_NORMAL) {}
53 };
54 #endif // FEATURE_COMPONENT_TEXT_SPANNABLE
55 
56 class TextComponent : public Component {
57 public:
58     ACE_DISALLOW_COPY_AND_MOVE(TextComponent);
59     TextComponent() = delete;
60     TextComponent(jerry_value_t options, jerry_value_t children, AppStyleManager* styleManager);
~TextComponent()61     ~TextComponent() override {}
62 
63 protected:
64     bool CreateNativeViews() override;
65     void ReleaseNativeViews() override;
66     UIView *GetComponentRootView() const override;
67     bool SetPrivateAttribute(uint16_t attrKeyId, jerry_value_t attrValue) override;
68     bool ApplyPrivateStyle(const AppStyleItem* styleItem) override;
69     UI_LABEL_TYPE_WRAPPER *GetUILabelView() const;
70     void OnViewAttached() override;
71     void PostUpdate(uint16_t attrKeyId) override;
72 
73 private:
74     // parse js text align style to ui_label
75     void SetTextAlign(UI_LABEL_TYPE_WRAPPER& label, const AppStyleItem* styleItem);
76     void UpdateTextAlignToLabel(UI_LABEL_TYPE_WRAPPER& label);
77     // parse js text size style to fontSize_
78     void SetTextSize(const AppStyleItem* styleItem);
79     // parse js text color style to ui_label
80     void SetTextColor(UI_LABEL_TYPE_WRAPPER& label, const AppStyleItem* styleItem) const;
81     // parse js text overflow style to ui_label
82     void SetTextOverflow(UI_LABEL_TYPE_WRAPPER& label, const AppStyleItem* styleItem);
83     // parse js text letter space style to ui_label
84     void SetTextLetterSpace(UI_LABEL_TYPE_WRAPPER& label, const AppStyleItem* styleItem) const;
85     // parse js text line height style to ui_label
86     void SetTextLineHeight(UI_LABEL_TYPE_WRAPPER& label, const AppStyleItem* styleItem) const;
87 #if FEATURE_COMPONENT_TEXT_SPANNABLE
88     void SetRichTextSpan();
89 #endif
90     UI_LABEL_TYPE_WRAPPER uiLabel_;
91     uint8_t fontSize_;
92     char* fontFamily_;
93     char* textValue_;
94     uint8_t overflowMode_;
95     UITextLanguageAlignment horizontalAlign_;
96 #if FEATURE_COMPONENT_TEXT_SPANNABLE
97     BackgroundColor backgroundColorSpan_;
98     ForegroundColor foregroundColorSpan_;
99     LineBackgroundColor lineBackgroundColorSpan_;
100     AbsoluteSizeSpan absoluteSizeSpan_;
101     RelativeSizeSpan relativeSizeSpan_;
102     StringStyleSpan stringStyleSpan_;
103 #endif // FEATURE_COMPONENT_TEXT_SPANNABLE
104 };
105 } // namespace ACELite
106 } // namespace OHOS
107 
108 #endif // OHOS_ACELITE_TEXT_COMPONENT_H
109