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 20namespace Updater { 21using namespace OHOS; 22UITextLanguageAlignment 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 38std::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 50bool 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 */ 70OHOS::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 constexpr uint8_t colorMaxSize = 255; 76 int reset = Utils::String2Int<int>(hexColor.substr(startPos, width)); 77 if (reset < 0 || reset > static_cast<int>(colorMaxSize)) { 78 LOG(ERROR) << "String2Int error, reset = " << reset; 79 return colorMaxSize; 80 } 81 uint8_t ret = (startPos > hexColor.size()) ? 0 : static_cast<uint8_t>(reset); 82 startPos += width; 83 return ret; 84 }; 85 auto r = getNextField(); 86 auto g = getNextField(); 87 auto b = getNextField(); 88 auto a = getNextField(); 89 return OHOS::Color::GetColorFromRGBA(r, g, b, a); 90} 91}