• 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_GLOBAL_I18N_STYLED_NUMBER_FORMAT_ADDON_H
17 #define OHOS_GLOBAL_I18N_STYLED_NUMBER_FORMAT_ADDON_H
18 
19 #include <memory>
20 #include <unordered_map>
21 
22 #include "napi/native_api.h"
23 #include "napi/native_node_api.h"
24 #include "simple_number_format.h"
25 #include "simple_number_format_addon.h"
26 #include "styled_number_format.h"
27 #include "number_format.h"
28 #include "number_format_addon.h"
29 
30 namespace OHOS {
31 namespace Global {
32 namespace I18n {
33 class StyledNumberFormatAddon {
34 public:
35     StyledNumberFormatAddon();
36     ~StyledNumberFormatAddon();
37     static void Destructor(napi_env env, void *nativeObject, void *hint);
38     static napi_value InitStyledNumberFormat(napi_env env, napi_value exports);
39 
40 private:
41     static napi_value constructor(napi_env env, napi_callback_info info);
42     static napi_value Format(napi_env env, napi_callback_info info);
43     static napi_value CreateStyledString(napi_env env, const std::string &formattedNumber, napi_value styleOption);
44     static napi_value CreateStyleOptions(napi_env env, const std::vector<StyledNumberFormat::NumberPart> &numberParts,
45         const std::unordered_map<std::string, napi_ref> &styledNumberFormatOptions);
46     static napi_value CreateStyleOption(napi_env env, StyledNumberFormat::NumberPart numberPart,
47         napi_ref textStyleRef);
48     static void GetNumberFmtOrSimpleNumberFmt(napi_env env, napi_value arg,
49         NumberFormatAddon **numberformataddon, SimpleNumberFormatAddon **simpleNumberFormatAddon);
50     static void SetNamedProperty(napi_env env, napi_value &option, const std::string& name, napi_value &property);
51 
52     bool InitStyledNumberFormatContent(napi_env env, NumberFormatAddon* numberFormatAddon,
53         SimpleNumberFormatAddon* simpleNumberFormatAddon, napi_value options);
54     void SetTextStyle(napi_env env, napi_value options, const std::string &optionName);
55 
56     napi_env env_ = nullptr;
57     std::unordered_map<std::string, napi_ref> styledNumberFormatOptions_ {};
58     std::unique_ptr<StyledNumberFormat> styledNumberFormat_ = nullptr;
59 };
60 } // namespace I18n
61 } // namespace Global
62 } // namespace OHOS
63 #endif