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 "view_api.h"
17 #include "language/language_ui.h"
18 #include "utils.h"
19
20 namespace Updater {
21 using namespace OHOS;
GetAlign(const std::string & align)22 UITextLanguageAlignment GetAlign(const std::string &align)
23 {
24 static const std::unordered_map<std::string, OHOS::UITextLanguageAlignment> alignMap {
25 {"left", TEXT_ALIGNMENT_LEFT},
26 {"right", TEXT_ALIGNMENT_RIGHT},
27 {"center", TEXT_ALIGNMENT_CENTER},
28 {"top", TEXT_ALIGNMENT_TOP},
29 {"bottom", TEXT_ALIGNMENT_BOTTOM}
30 };
31 if (auto it = alignMap.find(align); it != alignMap.end()) {
32 return it->second;
33 }
34 LOG(ERROR) << "not recognized align, must be one of left,right,center,top,bottom, use center as default align";
35 return TEXT_ALIGNMENT_CENTER;
36 }
37
TranslateText(const std::string & id)38 std::string TranslateText(const std::string &id)
39 {
40 constexpr std::string_view emptyContent = "[]";
41 constexpr size_t idStartPos = 1;
42 if (id.size() > emptyContent.size() && *id.begin() == '[' && *id.rbegin() == ']') {
43 // format is [tag], then find by tag
44 return Lang::LanguageUI::GetInstance().Translate(id.substr(idStartPos, id.size() - emptyContent.size()));
45 }
46 // format is not [tag], then directly return id
47 return id;
48 }
49
CheckColor(const std::string & color)50 bool CheckColor(const std::string &color)
51 {
52 constexpr char colorBegin = '#';
53 constexpr std::size_t len = 9ul; // #rrggbbaa
54 if (color.empty() || color[0] != colorBegin || color.size() != len) {
55 LOG(ERROR) << "color format error: " << color;
56 return false;
57 }
58 if (std::find_if_not(std::next(begin(color)), end(color), [](unsigned char c) { return isxdigit(c) != 0; }) !=
59 end(color)) {
60 LOG(ERROR) << "color format error: " << color;
61 return false;
62 }
63 return true;
64 }
65
66 /*
67 * should call CheckColor before StrToColor. when directly calling StrToColor,
68 * invalid color will be regarded as black color;
69 */
StrToColor(const std::string & hexColor)70 OHOS::ColorType StrToColor(const std::string &hexColor)
71 {
72 std::size_t startPos = 1ul;
73 auto getNextField = [&startPos, &hexColor] () {
74 constexpr std::size_t width = 2ul;
75 uint8_t ret = (startPos > hexColor.size()) ? 0 : Utils::String2Int<uint8_t>(hexColor.substr(startPos, width));
76 startPos += width;
77 return ret;
78 };
79 auto r = getNextField();
80 auto g = getNextField();
81 auto b = getNextField();
82 auto a = getNextField();
83 return OHOS::Color::GetColorFromRGBA(r, g, b, a);
84 }
85 }