1fb299fa2Sopenharmony_ci/*
2fb299fa2Sopenharmony_ci * Copyright (c) 2022 Huawei Device Co., Ltd.
3fb299fa2Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4fb299fa2Sopenharmony_ci * you may not use this file except in compliance with the License.
5fb299fa2Sopenharmony_ci * You may obtain a copy of the License at
6fb299fa2Sopenharmony_ci *
7fb299fa2Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
8fb299fa2Sopenharmony_ci *
9fb299fa2Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10fb299fa2Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11fb299fa2Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12fb299fa2Sopenharmony_ci * See the License for the specific language governing permissions and
13fb299fa2Sopenharmony_ci * limitations under the License.
14fb299fa2Sopenharmony_ci */
15fb299fa2Sopenharmony_ci
16fb299fa2Sopenharmony_ci#include "view_api.h"
17fb299fa2Sopenharmony_ci#include "language/language_ui.h"
18fb299fa2Sopenharmony_ci#include "utils.h"
19fb299fa2Sopenharmony_ci
20fb299fa2Sopenharmony_cinamespace Updater {
21fb299fa2Sopenharmony_ciusing namespace OHOS;
22fb299fa2Sopenharmony_ciUITextLanguageAlignment GetAlign(const std::string &align)
23fb299fa2Sopenharmony_ci{
24fb299fa2Sopenharmony_ci    static const std::unordered_map<std::string, OHOS::UITextLanguageAlignment> alignMap {
25fb299fa2Sopenharmony_ci        {"left", TEXT_ALIGNMENT_LEFT},
26fb299fa2Sopenharmony_ci        {"right", TEXT_ALIGNMENT_RIGHT},
27fb299fa2Sopenharmony_ci        {"center", TEXT_ALIGNMENT_CENTER},
28fb299fa2Sopenharmony_ci        {"top", TEXT_ALIGNMENT_TOP},
29fb299fa2Sopenharmony_ci        {"bottom", TEXT_ALIGNMENT_BOTTOM}
30fb299fa2Sopenharmony_ci    };
31fb299fa2Sopenharmony_ci    if (auto it = alignMap.find(align); it != alignMap.end()) {
32fb299fa2Sopenharmony_ci        return it->second;
33fb299fa2Sopenharmony_ci    }
34fb299fa2Sopenharmony_ci    LOG(ERROR) << "not recognized align, must be one of left,right,center,top,bottom, use center as default align";
35fb299fa2Sopenharmony_ci    return TEXT_ALIGNMENT_CENTER;
36fb299fa2Sopenharmony_ci}
37fb299fa2Sopenharmony_ci
38fb299fa2Sopenharmony_cistd::string TranslateText(const std::string &id)
39fb299fa2Sopenharmony_ci{
40fb299fa2Sopenharmony_ci    constexpr std::string_view emptyContent = "[]";
41fb299fa2Sopenharmony_ci    constexpr size_t idStartPos = 1;
42fb299fa2Sopenharmony_ci    if (id.size() > emptyContent.size() && *id.begin() == '[' && *id.rbegin() == ']') {
43fb299fa2Sopenharmony_ci        // format is [tag], then find by tag
44fb299fa2Sopenharmony_ci        return Lang::LanguageUI::GetInstance().Translate(id.substr(idStartPos, id.size() - emptyContent.size()));
45fb299fa2Sopenharmony_ci    }
46fb299fa2Sopenharmony_ci    // format is not [tag], then directly return id
47fb299fa2Sopenharmony_ci    return id;
48fb299fa2Sopenharmony_ci}
49fb299fa2Sopenharmony_ci
50fb299fa2Sopenharmony_cibool CheckColor(const std::string &color)
51fb299fa2Sopenharmony_ci{
52fb299fa2Sopenharmony_ci    constexpr char colorBegin = '#';
53fb299fa2Sopenharmony_ci    constexpr std::size_t len = 9uL; // #rrggbbaa
54fb299fa2Sopenharmony_ci    if (color.empty() || color[0] != colorBegin || color.size() != len) {
55fb299fa2Sopenharmony_ci        LOG(ERROR) << "color format error: " << color;
56fb299fa2Sopenharmony_ci        return false;
57fb299fa2Sopenharmony_ci    }
58fb299fa2Sopenharmony_ci    if (std::find_if_not(std::next(begin(color)), end(color), [](unsigned char c) { return isxdigit(c) != 0; }) !=
59fb299fa2Sopenharmony_ci        end(color)) {
60fb299fa2Sopenharmony_ci        LOG(ERROR) << "color format error: " << color;
61fb299fa2Sopenharmony_ci        return false;
62fb299fa2Sopenharmony_ci    }
63fb299fa2Sopenharmony_ci    return true;
64fb299fa2Sopenharmony_ci}
65fb299fa2Sopenharmony_ci
66fb299fa2Sopenharmony_ci/*
67fb299fa2Sopenharmony_ci * should call CheckColor before StrToColor. when directly calling StrToColor,
68fb299fa2Sopenharmony_ci * invalid color will be regarded as black color;
69fb299fa2Sopenharmony_ci */
70fb299fa2Sopenharmony_ciOHOS::ColorType StrToColor(const std::string &hexColor)
71fb299fa2Sopenharmony_ci{
72fb299fa2Sopenharmony_ci    std::size_t startPos = 1uL;
73fb299fa2Sopenharmony_ci    auto getNextField = [&startPos, &hexColor] () {
74fb299fa2Sopenharmony_ci        constexpr std::size_t width = 2uL;
75fb299fa2Sopenharmony_ci        constexpr uint8_t colorMaxSize = 255;
76fb299fa2Sopenharmony_ci        int reset = Utils::String2Int<int>(hexColor.substr(startPos, width));
77fb299fa2Sopenharmony_ci        if (reset < 0 || reset > static_cast<int>(colorMaxSize)) {
78fb299fa2Sopenharmony_ci            LOG(ERROR) << "String2Int error, reset = " << reset;
79fb299fa2Sopenharmony_ci            return colorMaxSize;
80fb299fa2Sopenharmony_ci        }
81fb299fa2Sopenharmony_ci        uint8_t ret = (startPos > hexColor.size()) ? 0 : static_cast<uint8_t>(reset);
82fb299fa2Sopenharmony_ci        startPos += width;
83fb299fa2Sopenharmony_ci        return ret;
84fb299fa2Sopenharmony_ci    };
85fb299fa2Sopenharmony_ci    auto r = getNextField();
86fb299fa2Sopenharmony_ci    auto g = getNextField();
87fb299fa2Sopenharmony_ci    auto b = getNextField();
88fb299fa2Sopenharmony_ci    auto a = getNextField();
89fb299fa2Sopenharmony_ci    return OHOS::Color::GetColorFromRGBA(r, g, b, a);
90fb299fa2Sopenharmony_ci}
91fb299fa2Sopenharmony_ci}