1 /* 2 * Copyright (c) 2025 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 #ifndef FOUNDATION_ACE_FRAMEWORKS_CORE_COMMON_RESOURCE_REOURCE_PARSE_UTILS_H 17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMMON_RESOURCE_REOURCE_PARSE_UTILS_H 18 19 #include <functional> 20 #include <map> 21 #include <string> 22 23 #include "base/memory/ace_type.h" 24 #include "core/common/resource/resource_manager.h" 25 #include "core/common/resource/resource_wrapper.h" 26 #include "core/common/resource/resource_object.h" 27 28 namespace OHOS::Ace { 29 30 enum class ResourceType : uint32_t { 31 COLOR = 10001, 32 FLOAT, 33 STRING, 34 PLURAL, 35 BOOLEAN, 36 INTARRAY, 37 INTEGER, 38 PATTERN, 39 STRARRAY, 40 MEDIA = 20000, 41 RAWFILE = 30000, 42 SYMBOL = 40000 43 }; 44 45 class ResourceParseUtils final : public AceType { 46 DECLARE_ACE_TYPE(ResourceParseUtils, AceType); 47 48 public: 49 static RefPtr<ThemeConstants> GetThemeConstants(const RefPtr<ResourceObject>& resObj); 50 static bool ParseResString(const RefPtr<ResourceObject>& resObj, std::string& result); 51 static bool ParseResString(const RefPtr<ResourceObject>& resObj, std::u16string& result); 52 static bool ParseResColor(const RefPtr<ResourceObject>& resObj, Color& result); 53 static bool ParseResColorWithColorMode(const RefPtr<ResourceObject>& resObj, Color& result, 54 const ColorMode& colorMode); 55 static bool ParseResourceToDouble(const RefPtr<ResourceObject>& resObj, double& result); 56 static bool ParseResInteger(const RefPtr<ResourceObject>& resObj, int32_t& result); 57 static bool ParseResInteger(const RefPtr<ResourceObject>& resObj, uint32_t& result); 58 static bool ParseResIntegerArray(const RefPtr<ResourceObject>& resObj, std::vector<uint32_t>& result); 59 static bool ParseResStrArray(const RefPtr<ResourceObject>& resObj, std::vector<std::string>& result); 60 static bool ParseResFontFamilies(const RefPtr<ResourceObject>& resObj, std::vector<std::string>& result); 61 static bool ParseResDouble(const RefPtr<ResourceObject>& resObj, double& result); 62 static bool ParseResBool(const RefPtr<ResourceObject>& resObj, bool& result); 63 static bool ParseResMedia(const RefPtr<ResourceObject>& resObj, std::string& result); 64 static bool ParseResResource(const RefPtr<ResourceObject>& resObj, CalcDimension& result); 65 static bool ParseResDimensionFpNG(const RefPtr<ResourceObject>& resObj, 66 CalcDimension& result, bool isSupportPercent = true); 67 static bool ParseResDimensionVpNG(const RefPtr<ResourceObject>& resObj, 68 CalcDimension& result, bool isSupportPercent = true); 69 static bool ParseResDimensionNG(const RefPtr<ResourceObject>& resObj, CalcDimension& result, 70 DimensionUnit defaultUnit, bool isSupportPercent = true); 71 static bool ParseResDimension(const RefPtr<ResourceObject>& resObj, CalcDimension& result, 72 DimensionUnit defaultUnit); 73 static bool ParseResDimensionVp(const RefPtr<ResourceObject>& resObj, CalcDimension& result); 74 static bool ParseResDimensionFp(const RefPtr<ResourceObject>& resObj, CalcDimension& result); 75 static bool ParseResDimensionPx(const RefPtr<ResourceObject>& resObj, CalcDimension& result); 76 template<class T> 77 static bool ConvertFromResObjNG(const RefPtr<ResourceObject>& resObj, T& result); 78 template<class T> 79 static bool ConvertFromResObj(const RefPtr<ResourceObject>& resObj, T& result); 80 IsNumberType(int32_t type)81 static bool IsNumberType(int32_t type) 82 { 83 return type == static_cast<int32_t>(ResourceObjectParamType::FLOAT) || 84 type == static_cast<int32_t>(ResourceObjectParamType::INT); 85 } 86 87 template<typename T> ParseResInteger(const RefPtr<ResourceObject> & resObj,T & result)88 static bool ParseResInteger(const RefPtr<ResourceObject>& resObj, T& result) 89 { 90 CHECK_NULL_RETURN(resObj, false); 91 auto resIdNum = resObj->GetId(); 92 auto type = resObj->GetType(); 93 auto resourceAdapter = ResourceManager::GetInstance().GetOrCreateResourceAdapter(resObj); 94 RefPtr<ThemeConstants> themeConstants = nullptr; 95 auto resourceWrapper = AceType::MakeRefPtr<ResourceWrapper>(themeConstants, resourceAdapter); 96 if (resIdNum == -1) { 97 auto param = resObj->GetParams()[0]; 98 if (type == static_cast<int32_t>(ResourceType::INTEGER)) { 99 result = static_cast<T>(resourceWrapper->GetIntByName(param.value.value())); 100 return true; 101 } 102 return false; 103 } 104 if (type == static_cast<int32_t>(ResourceType::INTEGER)) { 105 result = static_cast<T>(resourceWrapper->GetInt(resIdNum)); 106 return true; 107 } 108 return false; 109 } 110 SetIsReloading(bool isReloading)111 static void SetIsReloading(bool isReloading) 112 { 113 isReloading_ = isReloading; 114 } 115 IsReloading()116 static bool IsReloading() 117 { 118 return isReloading_; 119 } 120 121 private: 122 static void InvertColorWithResource(const RefPtr<ResourceObject>& resObj, Color& result, 123 const ColorMode& colorMode); 124 static bool ParseResColorWithName(const RefPtr<ResourceObject>& resObj, Color& result, 125 RefPtr<ResourceWrapper>& resourceWrapper, const ColorMode& colorMode); 126 static bool ParseResStringObj(const std::vector<ResourceObjectParams>& params, 127 RefPtr<ResourceWrapper>& resourceWrapper, std::string& result, int32_t type); 128 static bool isReloading_; 129 }; 130 } 131 #endif 132