• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 ARKGEN_UTILS_H
17 #define ARKGEN_UTILS_H
18 
19 #include <ani.h>
20 #include <cmath>
21 #include <regex>
22 #include <vector>
23 #include <string>
24 #include <iostream>
25 #include <array>
26 
27 #include "core/common/container.h"
28 #include "core/common/resource/resource_wrapper.h"
29 #include "core/components/common/properties/color.h"
30 #include "core/components/theme/shadow_theme.h"
31 #include "core/common/resource/resource_manager.h"
32 #include "frameworks/base/log/log_wrapper.h"
33 
34 enum PromptActionColor {
35     PROMPT_ACTION_COLOR_WHITE,
36     PROMPT_ACTION_COLOR_BLACK,
37     PROMPT_ACTION_COLOR_BLUE,
38     PROMPT_ACTION_COLOR_BROWN,
39     PROMPT_ACTION_COLOR_GRAY,
40     PROMPT_ACTION_COLOR_GREEN,
41     PROMPT_ACTION_COLOR_GREY,
42     PROMPT_ACTION_COLOR_ORANGE,
43     PROMPT_ACTION_COLOR_PINK,
44     PROMPT_ACTION_COLOR_RED,
45     PROMPT_ACTION_COLOR_YELLOW,
46     PROMPT_ACTION_COLOR_TRANSPARENT
47 };
48 
49 enum class ResourceType : uint32_t {
50     COLOR = 10001,
51     FLOAT,
52     STRING,
53     PLURAL,
54     BOOLEAN,
55     INTARRAY,
56     INTEGER,
57     PATTERN,
58     STRARRAY,
59     MEDIA = 20000,
60     RAWFILE = 30000
61 };
62 struct ResourceInfo {
63     int32_t resId = 0;
64     int32_t type = 0;
65     std::vector<std::string> params;
66     std::optional<std::string> bundleName = std::nullopt;
67     std::optional<std::string> moduleName = std::nullopt;
68 };
69 enum class ResourceStruct { CONSTANT, DYNAMIC_V1, DYNAMIC_V2 };
70 const std::regex RESOURCE_APP_STRING_PLACEHOLDER(R"(\%((\d+)(\$)){0,1}([dsf]))", std::regex::icase);
71 constexpr int32_t UNKNOWN_RESOURCE_ID = -1;
72 const std::vector<std::string> RESOURCE_HEADS = { "app", "sys" };
73 const int32_t NUMBER_ZERO = 0;
74 const int32_t NUMBER_ONE = 1;
75 const int32_t NUMBER_TWO = 2;
76 const int32_t NUMBER_THREE = 3;
77 std::string ANIUtils_ANIStringToStdString(ani_env *env, ani_string ani_str);
78 void PreFixEmptyBundleName(ani_env *env, ani_object value);
79 void processResourceType(ani_env *env, ani_object value, ani_ref params_ref, size_t length);
80 void ModifyResourceParam(ani_env *env, ani_object value, const ResourceType& resType, const std::string& resName);
81 void CompleteResourceParamV1(ani_env *env, ani_object value);
82 void CompleteResourceParamV2(ani_env *env, ani_object value);
83 bool ParseDollarResource(
84     ani_env *env, ani_object params_ref, ResourceType& resType, std::string& resName, std::string& moduleName);
85 bool ConvertResourceType(const std::string& typeName, ResourceType& resType);
86 ResourceStruct CheckResourceStruct(ani_env *env, ani_object value);
87 void CompleteResourceParam(ani_env *env, ani_object value);
88 bool ParseResourceParamId(ani_env *env, ani_object objects, ResourceInfo& info);
89 bool ParseResourceParamBundleName(ani_env *env, ani_object objects, ResourceInfo& info);
90 bool ParseResourceParamModuleName(ani_env *env, ani_object objects, ResourceInfo& info);
91 bool ParseResourceParamType(ani_env *env, ani_object objects, ResourceInfo& info);
92 bool ParseResourceParamName(ani_env *env, ani_object objects, ResourceInfo& info);
93 bool ParseResourceParam(ani_env *env, ani_object options, ResourceInfo& info);
94 OHOS::Ace::RefPtr<OHOS::Ace::ThemeConstants> GetThemeConstants(
95     const std::optional<std::string>& bundleName = std::nullopt,
96     const std::optional<std::string>& moduleName = std::nullopt);
97 OHOS::Ace::RefPtr<OHOS::Ace::ResourceWrapper> CreateResourceWrapper(const ResourceInfo& info);
98 void ReplaceHolder(std::string& originStr, const std::vector<std::string>& params, uint32_t containCount);
99 std::string DimensionToString(OHOS::Ace::Dimension dimension);
100 bool ParseString(const ResourceInfo& info, std::string& result);
101 bool ParseAniColor(ani_env *env, ani_ref resourceColor_ref, OHOS::Ace::Color& resourceColor);
102 bool ParseResourceColor(ani_env *env, ani_ref resourceColor_ref, OHOS::Ace::Color& resourceColor);
103 bool ParseColorMapToColor(PromptActionColor colorenum, OHOS::Ace::Color& Color);
104 bool ParseLengthToDimension(ani_env *env, ani_ref dy_ref, OHOS::Ace::DimensionUnit defaultUnit,
105                             OHOS::Ace::CalcDimension& result);
106 bool GetShadowFromTheme(OHOS::Ace::ShadowStyle shadowStyle, OHOS::Ace::Shadow& shadow);
107 bool GetToastObjectShadow(ani_env *env, ani_object objects, OHOS::Ace::Shadow& shadow);
108 bool ParseShadowOptionsFill(ani_env *env, ani_object objects, bool& isFilled);
109 bool ParseShadowOptionsOffsetX(ani_env *env, ani_object objects, double& offset);
110 bool ParseShadowOptionsOffsetY(ani_env *env, ani_object objects, double& offset);
111 bool ParseShadowOptionsOffset(ani_env *env, ani_ref offset_ref, double& offset);
112 bool ParseShadowColorStrategy(ani_env *env, ani_ref resourceColor_ref, OHOS::Ace::ShadowColorStrategy& strategy);
113 bool ParseShadowOptionsColor(ani_env *env, ani_object objects, OHOS::Ace::Shadow& shadowProps);
114 bool ParseShadowOptionsType(ani_env *env, ani_object objects, int32_t& shadowType);
115 bool ParseShadowOptionsRadius(ani_env *env, ani_object object, double& result);
116 bool ParseRadiusNumberToDouble(ani_env *env, ani_object resource_object, double& result);
117 bool ParseRadiusResourceToDouble(ani_env *env, ani_object resource_object, double& result);
118 bool ParseResource(const ResourceInfo resource, OHOS::Ace::CalcDimension& result);
119 bool GetIsResourceObject(ani_env *env, ani_ref object_ref);
120 bool GetIsStringObject(ani_env *env, ani_ref object_ref);
121 bool GetIsNumberObject(ani_env *env, ani_ref object_ref);
122 bool GetIsArrayObject(ani_env *env, ani_ref object_ref);
123 bool GetIsShadowOptionsObject(ani_env *env, ani_ref object_ref);
124 bool GetIsShowToastOptionsObject(ani_env *env, ani_object object);
125 bool GetIsBlurStyleEnum(ani_env *env, ani_ref object_ref);
126 bool GetIsToastShowModeEnum(ani_env *env, ani_ref object_ref);
127 bool GetIsShadowStyleEnum(ani_env *env, ani_ref object_ref);
128 bool GetIsAlignmentEnum(ani_env *env, ani_ref object_ref);
129 bool GetIsShadowTypeEnum(ani_env *env, ani_ref object_ref);
130 bool GetIsColoringStrategyEnum(ani_env *env, ani_ref object_ref);
131 bool GetIsColorEnum(ani_env *env, ani_ref object_ref);
132 bool GetIsHoverModeAreaEnum(ani_env *env, ani_ref object_ref);
133 bool GetIsUndefinedObject(ani_env *env, ani_ref object_ref);
134 #endif