1 /*
2 * Copyright (c) 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 "common/spannable_string.h"
17 #include "gfx_utils/graphic_log.h"
18 #include "securec.h"
19 namespace OHOS {
20 #if defined(ENABLE_SPANNABLE_STRING) && ENABLE_SPANNABLE_STRING
~SpannableString()21 SpannableString::~SpannableString()
22 {
23 if (text_ != nullptr) {
24 UIFree(text_);
25 text_ = nullptr;
26 }
27 }
SpannableString(const char * text)28 SpannableString::SpannableString(const char* text)
29 {
30 if (text == nullptr) {
31 return;
32 }
33 uint32_t textLen = static_cast<uint32_t>(strlen(text));
34 if (textLen > MAX_TEXT_LENGTH) {
35 return;
36 }
37 text_ = static_cast<char*>(UIMalloc(++textLen));
38 if (text_ == nullptr) {
39 return;
40 }
41 if (memcpy_s(text_, textLen, text, textLen) != EOK) {
42 UIFree(text_);
43 text_ = nullptr;
44 return;
45 }
46 }
SetTextStyle(TextStyle textStyle,uint16_t startIndex,uint16_t endIndex)47 void SpannableString::SetTextStyle(TextStyle textStyle, uint16_t startIndex, uint16_t endIndex)
48 {
49 StyleSpan* span = new StyleSpan(textStyle, startIndex, endIndex);
50 spanList_.PushBack(span);
51 }
52 #endif
53 }
54