* Copyright (c) 2021-2023 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef FOUNDATION_ACE_INTERFACES_NAPI_KITS_UTILS_H
#define FOUNDATION_ACE_INTERFACES_NAPI_KITS_UTILS_H
#include <chrono>
#include <cmath>
#include <cstdint>
#include <regex>
#include <vector>
#include "native_engine/native_value.h"
#include "base/log/log.h"
#include "bridge/common/utils/utils.h"
#include "core/common/container.h"
#include "core/components/theme/resource_adapter.h"
#include "core/components/common/layout/position_param.h"
#include "core/components/common/properties/color.h"
namespace OHOS::Ace::Napi {
struct ResourceInfo {
int32_t resId = 0;
int32_t type = 0;
std::vector<std::string> params;
std::optional<std::string> bundleName = std::nullopt;
std::optional<std::string> moduleName = std::nullopt;
bool hasGetter = false;
};
enum class ResourceType : uint32_t {
COLOR = 10001,
FLOAT,
STRING,
PLURAL,
BOOLEAN,
INTARRAY,
INTEGER,
PATTERN,
STRARRAY,
MEDIA = 20000,
RAWFILE = 30000
};
enum class ResourceStruct { CONSTANT, DYNAMIC_V1, DYNAMIC_V2 };
size_t GetParamLen(napi_env env, napi_value param);
bool GetNapiString(napi_env env, napi_value value, std::string& retStr, napi_valuetype& valueType);
bool NapiStringToString(napi_env env, napi_value value, std::string& retStr);
void NapiThrow(napi_env env, const std::string& message, int32_t errCode);
napi_value ParseCurve(napi_env env, napi_value value, std::string& curveTypeString, std::vector<float>& curveValue);
napi_value CreateNapiString(napi_env env, const std::string& rawStr);
ResourceStruct CheckResourceStruct(napi_env env, napi_value value, bool hasGetter);
void CompleteResourceParam(napi_env env, napi_value value, bool hasGetter);
void CompleteResourceParamV1(napi_env env, napi_value value);
void CompleteResourceParamV2(napi_env env, napi_value value);
void ModifyResourceParam(napi_env env, napi_value value, const ResourceType& resType, const std::string& resName);
bool ConvertResourceType(const std::string& typeName, ResourceType& resType);
void PreFixEmptyBundleName(napi_env env, napi_value value);
bool ParseDollarResource(
napi_env env, napi_value value, ResourceType& resType, std::string& resName, std::string& moduleName);
void ParseCurveInfo(const std::string& curveString, std::string& curveTypeString, std::vector<float>& curveValue);
bool ParseColor(napi_env env, napi_value value, Color& info);
bool ParseResourceParam(napi_env env, napi_value value, ResourceInfo& info);
bool ParseString(const ResourceInfo& info, std::string& result);
bool MatchValueTypeLuminance(napi_env env, napi_value value, napi_valuetype targetType);
bool ParseStringLuminance(napi_env env, napi_value propertyNapi, std::string& property);
bool ParseIntLuminance(napi_env env, napi_value propertyNapi, int32_t& property);
bool ParseEdgesLengthMetrics(napi_env env, napi_value value, EdgesParam& edges);
bool ParseRightEdge(napi_env env, napi_value obj, EdgesParam& edges);
bool ParseLeftEdge(napi_env env, napi_value obj, EdgesParam& edges);
bool ParseBottomEdge(napi_env env, napi_value obj, EdgesParam& edges);
bool ParseTopEdge(napi_env env, napi_value obj, EdgesParam& edges);
bool ParseLengthMetricValue(napi_env env, napi_value value, CalcDimension& dimension);
std::string ErrorToMessage(int32_t code);
bool GetSingleParam(napi_env env, napi_callback_info info, napi_value* argv, napi_valuetype& valueType);
std::optional<Color> GetOptionalColor(napi_env env, napi_value argv, napi_valuetype& valueType);
napi_valuetype GetValueType(napi_env env, napi_value value);
RefPtr<ResourceAdapter> CreateResourceAdapter(const ResourceInfo& info);
std::optional<std::string> GetStringFromValueUtf8(napi_env env, napi_value value);
bool ParseIntegerToString(const ResourceInfo& info, std::string& result);
bool ParseColorFromResourceObject(napi_env env, napi_value value, Color& colorResult);
napi_value GetReturnObject(napi_env env, std::string callbackString);
bool HasProperty(napi_env env, napi_value value, const std::string& targetStr);
bool ParseNapiDimension(napi_env env, CalcDimension& result, napi_value napiValue, DimensionUnit defaultUnit);
bool ParseNapiDimensionNG(
napi_env env, CalcDimension& result, napi_value napiValue, DimensionUnit defaultUnit, bool isSupportPercent);
bool CheckDarkResource(const RefPtr<ResourceObject>& resObj);
RefPtr<ResourceObject> ParseResourceParamToObj(napi_env env, napi_value value);
bool ParseNapiColor(napi_env env, napi_value value, Color& result, RefPtr<ResourceObject>& resObj);
bool ParseNapiColor(napi_env env, napi_value value, Color& result);
bool ParseStyle(napi_env env, napi_value value, std::optional<BorderStyle>& style);
bool ParseShadowColorStrategy(napi_env env, napi_value value, ShadowColorStrategy& strategy);
bool HasGetter(napi_env env, napi_value value, const std::string& key);
int32_t GetStringFormatStartIndex(bool hasGetter);
int32_t GetUIContextInstanceId(napi_env env, napi_value uiContext);
std::string GetLocalizedParamStr(const std::string& paramStr, const std::string& type);
bool GetBoolProperty(napi_env env, napi_value object, const char* name, bool& result);
bool GetInt32Property(napi_env env, napi_value object, const char* name, int32_t& result);
bool GetDoubleProperty(napi_env env, napi_value object, const char* name, double& result);
bool GetStringProperty(napi_env env, napi_value object, const char* name, std::string& result);
bool GetFunctionProperty(napi_env env, napi_value object, const char* name, napi_ref& result);
bool GetVoidCallbackProperty(napi_env env, napi_value object, const char* name, std::function<void()>& result);
}
#endif