• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2024 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 OHOS_ROSEN_JS_TOOL_H
17 #define OHOS_ROSEN_JS_TOOL_H
18 
19 #include <memory>
20 #include <native_engine/native_engine.h>
21 #include <native_engine/native_value.h>
22 
23 #ifdef ROSEN_OHOS
24 #include "resource_manager.h"
25 #endif
26 
27 namespace OHOS::Rosen {
28 namespace Drawing {
29 
30 #ifdef ROSEN_OHOS
31 struct ResourceInfo {
32     int32_t resId = 0;
33     int32_t type = 0;
34     std::vector<std::string> params;
35 };
36 
37 enum class ResourceType {
38     COLOR = 10001,
39     STRING = 10003,
40     INTEGER = 10007,
41     RAWFILE = 30000,
42 };
43 #endif
44 
45 class JsTool final {
46 public:
47     JsTool();
48     ~JsTool();
49 
50     static napi_value Init(napi_env env, napi_value exportObj);
51     static napi_value Constructor(napi_env env, napi_callback_info info);
52     static void Destructor(napi_env env, void* nativeObject, void* finalize);
53 
54     static napi_value makeColorFromResourceColor(napi_env env, napi_callback_info info);
55 
56 #ifdef ROSEN_OHOS
57     static size_t GetParamLen(napi_env env, napi_value param);
58     static std::shared_ptr<Global::Resource::ResourceManager> GetResourceManager();
59     static bool GetResourceInfo(napi_env env, napi_value value, ResourceInfo& info);
60     static bool GetResourceInfoParams(napi_env env, ResourceInfo& info, napi_value paramsNApi);
61     static bool GetResourceRawFileDataBuffer(std::unique_ptr<uint8_t[]>&& buffer,
62         size_t& len, ResourceInfo& info);
63 
64     static uint32_t HandleIncorrectColor(const std::string& newColorStr);
65     static bool GetResourceColor(napi_env env, napi_value res, uint32_t& result);
66     static uint32_t GetColorNumberResult(uint32_t origin);
67     static bool GetColorStringResult(std::string colorStr, uint32_t& result);
68     static bool MatchColorWithMagic(std::string& colorStr, uint32_t& result);
69     static bool MatchColorWithMagicMini(std::string& colorStr, uint32_t& result);
70     static bool MatchColorWithRGB(const std::string& colorStr, uint32_t& result);
71     static bool MatchColorWithRGBA(const std::string& colorStr, uint32_t& result);
72     static bool FastCheckColorType(const std::string& colorStr, const std::string& expectPrefix,
73         const std::vector<size_t>& expectLengths);
74     static bool HandleRGBValue(int value, int& result);
75     static bool IsOpacityValid(double value);
76     static bool GetColorObjectResult(napi_env env, napi_value value, uint32_t& result);
77 #endif
78 
79 private:
80     static thread_local napi_ref constructor_;
81 };
82 } // namespace Drawing
83 } // namespace OHOS::Rosen
84 #endif // OHOS_ROSEN_JS_TOOL_H