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 #include "js_number_format_addon.h" 30 31 namespace OHOS { 32 namespace Global { 33 namespace I18n { 34 class StyledNumberFormatAddon { 35 public: 36 StyledNumberFormatAddon(); 37 ~StyledNumberFormatAddon(); 38 static void Destructor(napi_env env, void *nativeObject, void *hint); 39 static napi_value InitStyledNumberFormat(napi_env env, napi_value exports); 40 41 private: 42 static napi_value constructor(napi_env env, napi_callback_info info); 43 static napi_value Format(napi_env env, napi_callback_info info); 44 static napi_value CreateStyledString(napi_env env, const std::string &formattedNumber, napi_value styleOption); 45 static napi_value CreateStyleOptions(napi_env env, const std::vector<StyledNumberFormat::NumberPart> &numberParts, 46 const std::unordered_map<std::string, napi_ref> &styledNumberFormatOptions); 47 static napi_value CreateStyleOption(napi_env env, StyledNumberFormat::NumberPart numberPart, 48 napi_ref textStyleRef); 49 static void GetNumberFmtOrSimpleNumberFmt(napi_env env, napi_value arg, 50 JSNumberFormatAddon **jsNumberformatAddon, SimpleNumberFormatAddon **simpleNumberFormatAddon, 51 NumberFormatAddon **numberFormatAddon); 52 static bool IsNumberFormatInstance(napi_env env, napi_value arg, const std::string &moduleName); 53 static bool IsIntlNumberFormatInstance(napi_env env, napi_value arg); 54 static void SetNamedProperty(napi_env env, napi_value &option, const std::string& name, napi_value &property); 55 56 bool InitStyledNumberFormatContent(napi_env env, JSNumberFormatAddon* jsNumberFormatAddon, 57 SimpleNumberFormatAddon* simpleNumberFormatAddon, NumberFormatAddon *numberFormatAddon, 58 napi_value options); 59 void SetTextStyle(napi_env env, napi_value options, const std::string &optionName); 60 61 napi_env env_ = nullptr; 62 std::unordered_map<std::string, napi_ref> styledNumberFormatOptions_ {}; 63 std::unique_ptr<StyledNumberFormat> styledNumberFormat_ = nullptr; 64 }; 65 } // namespace I18n 66 } // namespace Global 67 } // namespace OHOS 68 #endif