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
16 #include "stylemgr/app_style_item.h"
17 #include "ace_log.h"
18 #include "keys.h"
19 #include "number_parser.h"
20
21 namespace OHOS {
22 namespace ACELite {
EstimatePseudoClassType(const char * const styleKey,uint16_t * keyLength)23 uint8_t AppStyleItem::EstimatePseudoClassType(const char * const styleKey, uint16_t *keyLength)
24 {
25 char *p = strchr(const_cast<char *>(styleKey), ':');
26 if (p == nullptr) {
27 return PSEUDO_CLASS_UNKNOWN;
28 }
29
30 uint8_t type = PSEUDO_CLASS_UNKNOWN;
31 // :active meets
32 if (!strcmp(p, PSEUDO_CLASS_TYPE_ACTIVE)) {
33 type = PSEUDO_CLASS_ACTIVE;
34 } else if (!strcmp(p, PSEUDO_CLASS_TYPE_CHECKED)) {
35 type = PSEUDO_CLASS_CHECKED;
36 }
37
38 // drop :xxxx
39 if (type != PSEUDO_CLASS_UNKNOWN) {
40 *p = '\0';
41 *keyLength = (uint16_t)(p - styleKey);
42 }
43 return type;
44 }
45
SetStringValue(const char * const value)46 void AppStyleItem::SetStringValue(const char * const value)
47 {
48 if (value == nullptr) {
49 return;
50 }
51 size_t len = strlen(value);
52 if (len >= UINT16_MAX) {
53 return;
54 }
55 valueType_ = STYLE_PROP_VALUE_TYPE_STRING;
56 styleValue_.string = static_cast<char *>(ace_malloc(sizeof(char) * (len + 1)));
57 if (styleValue_.string == nullptr) {
58 HILOG_ERROR(HILOG_MODULE_ACE, "create style item string failed.");
59 return;
60 }
61 if (memcpy_s(styleValue_.string, len, value, len) != 0) {
62 HILOG_ERROR(HILOG_MODULE_ACE, "app_style set string value error");
63 ace_free(styleValue_.string);
64 styleValue_.string = nullptr;
65 return;
66 }
67 *(styleValue_.string + len) = '\0';
68 }
69
UpdateNumValToStr()70 bool AppStyleItem::UpdateNumValToStr()
71 {
72 if (GetValueType() == STYLE_PROP_VALUE_TYPE_NUMBER) {
73 int32_t numVal = GetNumValue();
74 const uint8_t len = 11; // max size of int32_t
75 char strVal[len] = { 0 };
76 if (sprintf_s(strVal, len, "%d", numVal) < 0) {
77 HILOG_ERROR(HILOG_MODULE_ACE, "style item transform num to string fail");
78 return false;
79 }
80 SetStringValue(static_cast<const char *>(strVal));
81 }
82 return true;
83 }
84
GenerateFromJSValue(jerry_value_t stylePropName,jerry_value_t stylePropValue)85 AppStyleItem *AppStyleItem::GenerateFromJSValue(jerry_value_t stylePropName, jerry_value_t stylePropValue)
86 {
87 uint16_t strLen = 0;
88 char *keyNameBuffer = MallocStringOf(stylePropName, &strLen);
89 if (keyNameBuffer == nullptr) {
90 HILOG_ERROR(HILOG_MODULE_ACE, "convert style prop name to char failed, will be dropped");
91 return nullptr;
92 }
93
94 if (strLen == 0) {
95 HILOG_ERROR(HILOG_MODULE_ACE, "style prop name length is 0, will be dropped");
96 ace_free(keyNameBuffer);
97 keyNameBuffer = nullptr;
98 return nullptr;
99 }
100
101 uint8_t pseudoType_ = EstimatePseudoClassType(static_cast<char *>(keyNameBuffer), &strLen);
102 AppStyleItem *styleItem =
103 CreateStyleItem(KeyParser::ParseKeyId(static_cast<const char *>(keyNameBuffer), strLen),
104 stylePropValue, pseudoType_);
105 ace_free(keyNameBuffer);
106 keyNameBuffer = nullptr;
107 return styleItem;
108 }
109
CreateStyleItem(uint16_t keyId,const jerry_value_t stylePropValue,uint8_t pseudoClassType)110 AppStyleItem *AppStyleItem::CreateStyleItem(uint16_t keyId, const jerry_value_t stylePropValue, uint8_t pseudoClassType)
111 {
112 AppStyleItem *newStyleItem = new AppStyleItem();
113 if (newStyleItem == nullptr) {
114 HILOG_ERROR(HILOG_MODULE_ACE, "new styleItem error");
115 return nullptr;
116 }
117
118 char *strValueBuffer = nullptr;
119
120 newStyleItem->propNameId_ = keyId;
121 newStyleItem->pseudoClassType_ = pseudoClassType;
122
123 if (jerry_value_is_number(stylePropValue)) {
124 #if FEATURE_COMPONENT_TEXT_SPANNABLE
125 if (keyId == K_OPACITY || keyId == K_RELATIVESIZESPANSIZE) {
126 #else
127 if (keyId == K_OPACITY) {
128 #endif
129 newStyleItem->SetFloatingValue(jerry_get_number_value(stylePropValue));
130 } else {
131 newStyleItem->SetNumValue((int32_t)(jerry_get_number_value(stylePropValue)));
132 }
133 } else if (jerry_value_is_boolean(stylePropValue)) {
134 newStyleItem->SetBoolValue(jerry_get_boolean_value(stylePropValue));
135 } else {
136 uint16_t strLength = 0;
137 strValueBuffer = MallocStringOf(stylePropValue, &strLength);
138 if (strValueBuffer == nullptr) {
139 HILOG_ERROR(HILOG_MODULE_ACE, "convert style value to char failed, will be dropped");
140 delete newStyleItem;
141 newStyleItem = nullptr;
142 return nullptr;
143 }
144 float percentValue = 0;
145 if (NumberParser::ParsePercentValue(strValueBuffer, strLength, percentValue)) {
146 newStyleItem->SetPercentValue(percentValue);
147 } else {
148 newStyleItem->SetStringValue(static_cast<const char *>(strValueBuffer));
149 }
150 ace_free(strValueBuffer);
151 strValueBuffer = nullptr;
152 }
153
154 return newStyleItem;
155 }
156
157 AppStyleItem *AppStyleItem::CopyFrom(const AppStyleItem *from)
158 {
159 if (from == nullptr) {
160 return nullptr;
161 }
162
163 AppStyleItem *styleItem = new AppStyleItem();
164 if (styleItem == nullptr) {
165 HILOG_ERROR(HILOG_MODULE_ACE, "new styleItem error");
166 return nullptr;
167 }
168
169 styleItem->UpdateValueFrom(*from);
170
171 return styleItem;
172 }
173
174 void AppStyleItem::UpdateValueFrom(const AppStyleItem &from)
175 {
176 if (valueType_ == STYLE_PROP_VALUE_TYPE_STRING) {
177 ACE_FREE(styleValue_.string);
178 }
179 propNameId_ = from.propNameId_;
180 pseudoClassType_ = from.pseudoClassType_;
181 switch (from.GetValueType()) {
182 case STYLE_PROP_VALUE_TYPE_STRING: {
183 const char *strValue = from.GetStrValue();
184 if (strValue != nullptr) {
185 SetStringValue(strValue);
186 }
187 break;
188 }
189 case STYLE_PROP_VALUE_TYPE_BOOL:
190 SetBoolValue(from.GetBoolValue());
191 break;
192 case STYLE_PROP_VALUE_TYPE_NUMBER:
193 SetNumValue(from.GetNumValue());
194 break;
195 case STYLE_PROP_VALUE_TYPE_FLOATING:
196 SetFloatingValue(from.GetFloatingValue());
197 break;
198 case STYLE_PROP_VALUE_TYPE_PERCENT:
199 SetPercentValue(from.GetPercentValue());
200 break;
201 default:
202 break;
203 }
204 }
205 } // namespace ACELite
206 } // namespace OHOS
207