123b3eb3cSopenharmony_ci/* 223b3eb3cSopenharmony_ci * Copyright (c) 2022 Shenzhen Kaihong Digital Industry Development Co., Ltd. 323b3eb3cSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 423b3eb3cSopenharmony_ci * you may not use this file except in compliance with the License. 523b3eb3cSopenharmony_ci * You may obtain a copy of the License at 623b3eb3cSopenharmony_ci * 723b3eb3cSopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 823b3eb3cSopenharmony_ci * 923b3eb3cSopenharmony_ci * Unless required by applicable law or agreed to in writing, software 1023b3eb3cSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 1123b3eb3cSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 1223b3eb3cSopenharmony_ci * See the License for the specific language governing permissions and 1323b3eb3cSopenharmony_ci * limitations under the License. 1423b3eb3cSopenharmony_ci */ 1523b3eb3cSopenharmony_ci 1623b3eb3cSopenharmony_ci 1723b3eb3cSopenharmony_ci#include "interfaces/napi/kits/utils/napi_utils.h" 1823b3eb3cSopenharmony_ci 1923b3eb3cSopenharmony_ci#include "bridge/common/utils/engine_helper.h" 2023b3eb3cSopenharmony_ci#include "core/common/font_manager.h" 2123b3eb3cSopenharmony_ci 2223b3eb3cSopenharmony_cinamespace OHOS::Ace::Napi { 2323b3eb3cSopenharmony_cinamespace { 2423b3eb3cSopenharmony_ciconstexpr size_t STR_BUFFER_SIZE = 1024; 2523b3eb3cSopenharmony_ciconstexpr int32_t FONT_INFO_INDEX_PATH = 0; 2623b3eb3cSopenharmony_ciconstexpr int32_t FONT_INFO_INDEX_POST_SCRIPT_NAME = 1; 2723b3eb3cSopenharmony_ciconstexpr int32_t FONT_INFO_INDEX_FULL_NAME = 2; 2823b3eb3cSopenharmony_ciconstexpr int32_t FONT_INFO_INDEX_FAMILY = 3; 2923b3eb3cSopenharmony_ciconstexpr int32_t FONT_INFO_INDEX_SUB_FAMILY = 4; 3023b3eb3cSopenharmony_ciconstexpr int32_t FONT_INFO_INDEX_WEIGHT = 5; 3123b3eb3cSopenharmony_ciconstexpr int32_t FONT_INFO_INDEX_WIDTH = 6; 3223b3eb3cSopenharmony_ciconstexpr int32_t FONT_INFO_INDEX_ITALIC = 7; 3323b3eb3cSopenharmony_ciconstexpr int32_t FONT_INFO_INDEX_MONOSPACE = 8; 3423b3eb3cSopenharmony_ciconstexpr int32_t FONT_INFO_INDEX_SYMBOLIC = 9; 3523b3eb3cSopenharmony_ciconstexpr int32_t FONT_INFO_INDEX_MAX = 10; 3623b3eb3cSopenharmony_ci} 3723b3eb3cSopenharmony_ci 3823b3eb3cSopenharmony_cistatic bool ParseFamilyNameOrSrc(napi_env env, napi_value familyNameOrSrcNApi, std::string& familyNameOrSrc, 3923b3eb3cSopenharmony_ci napi_valuetype valueType, ResourceInfo& info) 4023b3eb3cSopenharmony_ci{ 4123b3eb3cSopenharmony_ci napi_typeof(env, familyNameOrSrcNApi, &valueType); 4223b3eb3cSopenharmony_ci if (valueType == napi_string) { 4323b3eb3cSopenharmony_ci size_t nameLen = 0; 4423b3eb3cSopenharmony_ci napi_get_value_string_utf8(env, familyNameOrSrcNApi, nullptr, 0, &nameLen); 4523b3eb3cSopenharmony_ci std::unique_ptr<char[]> name = std::make_unique<char[]>(nameLen + 1); 4623b3eb3cSopenharmony_ci napi_get_value_string_utf8(env, familyNameOrSrcNApi, name.get(), nameLen + 1, &nameLen); 4723b3eb3cSopenharmony_ci familyNameOrSrc = name.get(); 4823b3eb3cSopenharmony_ci } else if (valueType == napi_object) { 4923b3eb3cSopenharmony_ci if (!ParseResourceParam(env, familyNameOrSrcNApi, info)) { 5023b3eb3cSopenharmony_ci return false; 5123b3eb3cSopenharmony_ci } 5223b3eb3cSopenharmony_ci if (!ParseString(info, familyNameOrSrc)) { 5323b3eb3cSopenharmony_ci return false; 5423b3eb3cSopenharmony_ci } 5523b3eb3cSopenharmony_ci } else { 5623b3eb3cSopenharmony_ci return false; 5723b3eb3cSopenharmony_ci } 5823b3eb3cSopenharmony_ci return true; 5923b3eb3cSopenharmony_ci} 6023b3eb3cSopenharmony_ci 6123b3eb3cSopenharmony_cistatic napi_value JSRegisterFont(napi_env env, napi_callback_info info) 6223b3eb3cSopenharmony_ci{ 6323b3eb3cSopenharmony_ci size_t argc = 1; 6423b3eb3cSopenharmony_ci napi_value argv = nullptr; 6523b3eb3cSopenharmony_ci napi_value thisVar = nullptr; 6623b3eb3cSopenharmony_ci void* data = nullptr; 6723b3eb3cSopenharmony_ci napi_get_cb_info(env, info, &argc, &argv, &thisVar, &data); 6823b3eb3cSopenharmony_ci 6923b3eb3cSopenharmony_ci napi_value familyNameNApi = nullptr; 7023b3eb3cSopenharmony_ci napi_value familySrcNApi = nullptr; 7123b3eb3cSopenharmony_ci std::string familyName; 7223b3eb3cSopenharmony_ci std::string familySrc; 7323b3eb3cSopenharmony_ci 7423b3eb3cSopenharmony_ci napi_valuetype valueType = napi_undefined; 7523b3eb3cSopenharmony_ci napi_typeof(env, argv, &valueType); 7623b3eb3cSopenharmony_ci if (valueType == napi_object) { 7723b3eb3cSopenharmony_ci napi_get_named_property(env, argv, "familyName", &familyNameNApi); 7823b3eb3cSopenharmony_ci napi_get_named_property(env, argv, "familySrc", &familySrcNApi); 7923b3eb3cSopenharmony_ci } else { 8023b3eb3cSopenharmony_ci return nullptr; 8123b3eb3cSopenharmony_ci } 8223b3eb3cSopenharmony_ci 8323b3eb3cSopenharmony_ci ResourceInfo resourceInfo; 8423b3eb3cSopenharmony_ci if (!ParseFamilyNameOrSrc(env, familyNameNApi, familyName, valueType, resourceInfo)) { 8523b3eb3cSopenharmony_ci return nullptr; 8623b3eb3cSopenharmony_ci } 8723b3eb3cSopenharmony_ci if (!ParseFamilyNameOrSrc(env, familySrcNApi, familySrc, valueType, resourceInfo)) { 8823b3eb3cSopenharmony_ci return nullptr; 8923b3eb3cSopenharmony_ci } 9023b3eb3cSopenharmony_ci 9123b3eb3cSopenharmony_ci std::string bundleName = resourceInfo.bundleName.has_value() ? resourceInfo.bundleName.value() : ""; 9223b3eb3cSopenharmony_ci std::string moduleName = resourceInfo.moduleName.has_value() ? resourceInfo.moduleName.value() : ""; 9323b3eb3cSopenharmony_ci auto container = Container::CurrentSafely(); 9423b3eb3cSopenharmony_ci if (bundleName.empty() && container) { 9523b3eb3cSopenharmony_ci bundleName = container->GetBundleName(); 9623b3eb3cSopenharmony_ci } 9723b3eb3cSopenharmony_ci if (moduleName.empty() && container) { 9823b3eb3cSopenharmony_ci moduleName = container->GetModuleName(); 9923b3eb3cSopenharmony_ci } 10023b3eb3cSopenharmony_ci auto delegate = EngineHelper::GetCurrentDelegateSafely(); 10123b3eb3cSopenharmony_ci if (!delegate) { 10223b3eb3cSopenharmony_ci return nullptr; 10323b3eb3cSopenharmony_ci } 10423b3eb3cSopenharmony_ci TAG_LOGI(AceLogTag::ACE_FONT, "begin to register font."); 10523b3eb3cSopenharmony_ci delegate->RegisterFont(familyName, familySrc, bundleName, moduleName); 10623b3eb3cSopenharmony_ci return nullptr; 10723b3eb3cSopenharmony_ci} 10823b3eb3cSopenharmony_ci 10923b3eb3cSopenharmony_cistatic napi_value JSgetSystemFontList(napi_env env, napi_callback_info info) 11023b3eb3cSopenharmony_ci{ 11123b3eb3cSopenharmony_ci napi_value arrayResult = nullptr; 11223b3eb3cSopenharmony_ci napi_create_array(env, &arrayResult); 11323b3eb3cSopenharmony_ci bool isArray = false; 11423b3eb3cSopenharmony_ci if (napi_is_array(env, arrayResult, &isArray) != napi_ok || !isArray) { 11523b3eb3cSopenharmony_ci return arrayResult; 11623b3eb3cSopenharmony_ci } 11723b3eb3cSopenharmony_ci std::vector<std::string> fontList; 11823b3eb3cSopenharmony_ci auto delegate = EngineHelper::GetCurrentDelegateSafely(); 11923b3eb3cSopenharmony_ci if (!delegate) { 12023b3eb3cSopenharmony_ci return nullptr; 12123b3eb3cSopenharmony_ci } 12223b3eb3cSopenharmony_ci delegate->GetSystemFontList(fontList); 12323b3eb3cSopenharmony_ci 12423b3eb3cSopenharmony_ci int32_t index = 0; 12523b3eb3cSopenharmony_ci for (const std::string& font : fontList) { 12623b3eb3cSopenharmony_ci napi_value result = nullptr; 12723b3eb3cSopenharmony_ci napi_create_string_utf8(env, font.c_str(), font.length(), &result); 12823b3eb3cSopenharmony_ci napi_set_element(env, arrayResult, index++, result); 12923b3eb3cSopenharmony_ci } 13023b3eb3cSopenharmony_ci return arrayResult; 13123b3eb3cSopenharmony_ci} 13223b3eb3cSopenharmony_ci 13323b3eb3cSopenharmony_cistatic napi_value JSgetFontByName(napi_env env, napi_callback_info info) 13423b3eb3cSopenharmony_ci{ 13523b3eb3cSopenharmony_ci size_t argc = 1; 13623b3eb3cSopenharmony_ci napi_value argv = nullptr; 13723b3eb3cSopenharmony_ci napi_value thisVar = nullptr; 13823b3eb3cSopenharmony_ci void* data = nullptr; 13923b3eb3cSopenharmony_ci NAPI_CALL(env, napi_get_cb_info(env, info, &argc, &argv, &thisVar, &data)); 14023b3eb3cSopenharmony_ci NAPI_ASSERT(env, argc == 1, "requires 1 parameter"); 14123b3eb3cSopenharmony_ci 14223b3eb3cSopenharmony_ci napi_valuetype type; 14323b3eb3cSopenharmony_ci NAPI_CALL(env, napi_typeof(env, argv, &type)); 14423b3eb3cSopenharmony_ci NAPI_ASSERT(env, type == napi_string, "type mismatch"); 14523b3eb3cSopenharmony_ci char fontName[STR_BUFFER_SIZE] = { 0 }; 14623b3eb3cSopenharmony_ci size_t len = 0; 14723b3eb3cSopenharmony_ci napi_get_value_string_utf8(env, argv, fontName, STR_BUFFER_SIZE, &len); 14823b3eb3cSopenharmony_ci NAPI_ASSERT(env, len < STR_BUFFER_SIZE, "condition string too long"); 14923b3eb3cSopenharmony_ci std::string fontNameStr(fontName, len); 15023b3eb3cSopenharmony_ci 15123b3eb3cSopenharmony_ci FontInfo fontInfo; 15223b3eb3cSopenharmony_ci auto delegate = EngineHelper::GetCurrentDelegateSafely(); 15323b3eb3cSopenharmony_ci if (!delegate) { 15423b3eb3cSopenharmony_ci return nullptr; 15523b3eb3cSopenharmony_ci } 15623b3eb3cSopenharmony_ci if (!delegate->GetSystemFont(fontNameStr, fontInfo)) { 15723b3eb3cSopenharmony_ci return nullptr; 15823b3eb3cSopenharmony_ci } 15923b3eb3cSopenharmony_ci 16023b3eb3cSopenharmony_ci napi_value resultArray[FONT_INFO_INDEX_MAX] = { 0 }; 16123b3eb3cSopenharmony_ci napi_create_string_utf8(env, fontInfo.path.c_str(), NAPI_AUTO_LENGTH, &resultArray[FONT_INFO_INDEX_PATH]); 16223b3eb3cSopenharmony_ci napi_create_string_utf8(env, fontInfo.postScriptName.c_str(), NAPI_AUTO_LENGTH, 16323b3eb3cSopenharmony_ci &resultArray[FONT_INFO_INDEX_POST_SCRIPT_NAME]); 16423b3eb3cSopenharmony_ci napi_create_string_utf8(env, fontInfo.fullName.c_str(), NAPI_AUTO_LENGTH, &resultArray[FONT_INFO_INDEX_FULL_NAME]); 16523b3eb3cSopenharmony_ci napi_create_string_utf8(env, fontInfo.family.c_str(), NAPI_AUTO_LENGTH, &resultArray[FONT_INFO_INDEX_FAMILY]); 16623b3eb3cSopenharmony_ci napi_create_string_utf8(env, fontInfo.subfamily.c_str(), NAPI_AUTO_LENGTH, 16723b3eb3cSopenharmony_ci &resultArray[FONT_INFO_INDEX_SUB_FAMILY]); 16823b3eb3cSopenharmony_ci napi_create_int32(env, fontInfo.weight, &resultArray[FONT_INFO_INDEX_WEIGHT]); 16923b3eb3cSopenharmony_ci napi_create_int32(env, fontInfo.width, &resultArray[FONT_INFO_INDEX_WIDTH]); 17023b3eb3cSopenharmony_ci napi_get_boolean(env, fontInfo.italic, &resultArray[FONT_INFO_INDEX_ITALIC]); 17123b3eb3cSopenharmony_ci napi_get_boolean(env, fontInfo.monoSpace, &resultArray[FONT_INFO_INDEX_MONOSPACE]); 17223b3eb3cSopenharmony_ci napi_get_boolean(env, fontInfo.symbolic, &resultArray[FONT_INFO_INDEX_SYMBOLIC]); 17323b3eb3cSopenharmony_ci 17423b3eb3cSopenharmony_ci napi_value result = nullptr; 17523b3eb3cSopenharmony_ci napi_create_object(env, &result); 17623b3eb3cSopenharmony_ci napi_set_named_property(env, result, "path", resultArray[FONT_INFO_INDEX_PATH]); 17723b3eb3cSopenharmony_ci napi_set_named_property(env, result, "postScriptName", resultArray[FONT_INFO_INDEX_POST_SCRIPT_NAME]); 17823b3eb3cSopenharmony_ci napi_set_named_property(env, result, "fullName", resultArray[FONT_INFO_INDEX_FULL_NAME]); 17923b3eb3cSopenharmony_ci napi_set_named_property(env, result, "family", resultArray[FONT_INFO_INDEX_FAMILY]); 18023b3eb3cSopenharmony_ci napi_set_named_property(env, result, "subfamily", resultArray[FONT_INFO_INDEX_SUB_FAMILY]); 18123b3eb3cSopenharmony_ci napi_set_named_property(env, result, "weight", resultArray[FONT_INFO_INDEX_WEIGHT]); 18223b3eb3cSopenharmony_ci napi_set_named_property(env, result, "width", resultArray[FONT_INFO_INDEX_WIDTH]); 18323b3eb3cSopenharmony_ci napi_set_named_property(env, result, "italic", resultArray[FONT_INFO_INDEX_ITALIC]); 18423b3eb3cSopenharmony_ci napi_set_named_property(env, result, "monoSpace", resultArray[FONT_INFO_INDEX_MONOSPACE]); 18523b3eb3cSopenharmony_ci napi_set_named_property(env, result, "symbolic", resultArray[FONT_INFO_INDEX_SYMBOLIC]); 18623b3eb3cSopenharmony_ci 18723b3eb3cSopenharmony_ci return result; 18823b3eb3cSopenharmony_ci} 18923b3eb3cSopenharmony_ci 19023b3eb3cSopenharmony_cistatic napi_value GetUIFontGenericInfo(napi_env env, const FontConfigJsonInfo& fontConfigJsonInfo) 19123b3eb3cSopenharmony_ci{ 19223b3eb3cSopenharmony_ci napi_value genericSetResult = nullptr; 19323b3eb3cSopenharmony_ci napi_create_array(env, &genericSetResult); 19423b3eb3cSopenharmony_ci int32_t index = 0; 19523b3eb3cSopenharmony_ci for (const FontGenericInfo& generic: fontConfigJsonInfo.genericSet) { 19623b3eb3cSopenharmony_ci napi_value genericResult = nullptr; 19723b3eb3cSopenharmony_ci napi_create_object(env, &genericResult); 19823b3eb3cSopenharmony_ci napi_value familyResult = nullptr; 19923b3eb3cSopenharmony_ci napi_create_string_utf8(env, generic.familyName.c_str(), generic.familyName.length(), &familyResult); 20023b3eb3cSopenharmony_ci napi_value aliasSetResult = nullptr; 20123b3eb3cSopenharmony_ci napi_create_array(env, &aliasSetResult); 20223b3eb3cSopenharmony_ci int32_t index2 = 0; 20323b3eb3cSopenharmony_ci for (const AliasInfo& alias: generic.aliasSet) { 20423b3eb3cSopenharmony_ci napi_value aliasResult = nullptr; 20523b3eb3cSopenharmony_ci napi_create_object(env, &aliasResult); 20623b3eb3cSopenharmony_ci napi_value familyNameResult = nullptr; 20723b3eb3cSopenharmony_ci napi_create_string_utf8(env, alias.familyName.c_str(), alias.familyName.length(), &familyNameResult); 20823b3eb3cSopenharmony_ci napi_value weightResult = nullptr; 20923b3eb3cSopenharmony_ci napi_create_int32(env, alias.weight, &weightResult); 21023b3eb3cSopenharmony_ci napi_set_named_property(env, aliasResult, "name", familyNameResult); 21123b3eb3cSopenharmony_ci napi_set_named_property(env, aliasResult, "weight", weightResult); 21223b3eb3cSopenharmony_ci napi_set_element(env, aliasSetResult, index2++, aliasResult); 21323b3eb3cSopenharmony_ci } 21423b3eb3cSopenharmony_ci index2 = 0; 21523b3eb3cSopenharmony_ci napi_value adjustSetResult = nullptr; 21623b3eb3cSopenharmony_ci napi_create_array(env, &adjustSetResult); 21723b3eb3cSopenharmony_ci for (const AdjustInfo& adjust: generic.adjustSet) { 21823b3eb3cSopenharmony_ci napi_value adjustResult = nullptr; 21923b3eb3cSopenharmony_ci napi_create_object(env, &adjustResult); 22023b3eb3cSopenharmony_ci napi_value weightResult = nullptr; 22123b3eb3cSopenharmony_ci napi_create_int32(env, adjust.origValue, &weightResult); 22223b3eb3cSopenharmony_ci napi_value toResult = nullptr; 22323b3eb3cSopenharmony_ci napi_create_int32(env, adjust.newValue, &toResult); 22423b3eb3cSopenharmony_ci napi_set_named_property(env, adjustResult, "weight", weightResult); 22523b3eb3cSopenharmony_ci napi_set_named_property(env, adjustResult, "to", toResult); 22623b3eb3cSopenharmony_ci napi_set_element(env, adjustSetResult, index2++, adjustResult); 22723b3eb3cSopenharmony_ci } 22823b3eb3cSopenharmony_ci napi_set_named_property(env, genericResult, "family", familyResult); 22923b3eb3cSopenharmony_ci napi_set_named_property(env, genericResult, "alias", aliasSetResult); 23023b3eb3cSopenharmony_ci napi_set_named_property(env, genericResult, "adjust", adjustSetResult); 23123b3eb3cSopenharmony_ci napi_set_element(env, genericSetResult, index++, genericResult); 23223b3eb3cSopenharmony_ci } 23323b3eb3cSopenharmony_ci return genericSetResult; 23423b3eb3cSopenharmony_ci} 23523b3eb3cSopenharmony_ci 23623b3eb3cSopenharmony_cistatic napi_value GetUIFontFallbackInfo(napi_env env, const FontConfigJsonInfo& fontConfigJsonInfo) 23723b3eb3cSopenharmony_ci{ 23823b3eb3cSopenharmony_ci napi_value fallbackGroupSetResult = nullptr; 23923b3eb3cSopenharmony_ci napi_create_array(env, &fallbackGroupSetResult); 24023b3eb3cSopenharmony_ci int32_t index = 0; 24123b3eb3cSopenharmony_ci for (const FallbackGroup& fallbackGroup: fontConfigJsonInfo.fallbackGroupSet) { 24223b3eb3cSopenharmony_ci napi_value fallbackGroupResult = nullptr; 24323b3eb3cSopenharmony_ci napi_create_object(env, &fallbackGroupResult); 24423b3eb3cSopenharmony_ci napi_value fontSetNameResult = nullptr; 24523b3eb3cSopenharmony_ci napi_create_string_utf8(env, fallbackGroup.groupName.c_str(), 24623b3eb3cSopenharmony_ci fallbackGroup.groupName.length(), &fontSetNameResult); 24723b3eb3cSopenharmony_ci napi_value fallbackListResult = nullptr; 24823b3eb3cSopenharmony_ci napi_create_array(env, &fallbackListResult); 24923b3eb3cSopenharmony_ci int32_t index2 = 0; 25023b3eb3cSopenharmony_ci for (const FallbackInfo& fallback: fallbackGroup.fallbackInfoSet) { 25123b3eb3cSopenharmony_ci napi_value fallbackResult = nullptr; 25223b3eb3cSopenharmony_ci napi_create_object(env, &fallbackResult); 25323b3eb3cSopenharmony_ci napi_value familyResult = nullptr; 25423b3eb3cSopenharmony_ci napi_create_string_utf8(env, fallback.familyName.c_str(), fallback.familyName.length(), &familyResult); 25523b3eb3cSopenharmony_ci napi_value languageResult = nullptr; 25623b3eb3cSopenharmony_ci napi_create_string_utf8(env, fallback.font.c_str(), fallback.font.length(), &languageResult); 25723b3eb3cSopenharmony_ci 25823b3eb3cSopenharmony_ci napi_set_named_property(env, fallbackResult, "language", languageResult); 25923b3eb3cSopenharmony_ci napi_set_named_property(env, fallbackResult, "family", familyResult); 26023b3eb3cSopenharmony_ci napi_set_element(env, fallbackListResult, index2++, fallbackResult); 26123b3eb3cSopenharmony_ci } 26223b3eb3cSopenharmony_ci napi_set_named_property(env, fallbackGroupResult, "fontSetName", fontSetNameResult); 26323b3eb3cSopenharmony_ci napi_set_named_property(env, fallbackGroupResult, "fallback", fallbackListResult); 26423b3eb3cSopenharmony_ci napi_set_element(env, fallbackGroupSetResult, index++, fallbackGroupResult); 26523b3eb3cSopenharmony_ci } 26623b3eb3cSopenharmony_ci return fallbackGroupSetResult; 26723b3eb3cSopenharmony_ci} 26823b3eb3cSopenharmony_ci 26923b3eb3cSopenharmony_cistatic napi_value JsGetUIFontConfig(napi_env env, napi_callback_info info) 27023b3eb3cSopenharmony_ci{ 27123b3eb3cSopenharmony_ci FontConfigJsonInfo fontConfigJsonInfo; 27223b3eb3cSopenharmony_ci auto delegate = EngineHelper::GetCurrentDelegateSafely(); 27323b3eb3cSopenharmony_ci if (!delegate) { 27423b3eb3cSopenharmony_ci return nullptr; 27523b3eb3cSopenharmony_ci } 27623b3eb3cSopenharmony_ci delegate->GetUIFontConfig(fontConfigJsonInfo); 27723b3eb3cSopenharmony_ci napi_value result = nullptr; 27823b3eb3cSopenharmony_ci napi_create_object(env, &result); 27923b3eb3cSopenharmony_ci napi_value fontDirSetResult = nullptr; 28023b3eb3cSopenharmony_ci napi_create_array(env, &fontDirSetResult); 28123b3eb3cSopenharmony_ci int32_t index = 0; 28223b3eb3cSopenharmony_ci for (const std::string& fontDir : fontConfigJsonInfo.fontDirSet) { 28323b3eb3cSopenharmony_ci napi_value fontDirResult = nullptr; 28423b3eb3cSopenharmony_ci napi_create_string_utf8(env, fontDir.c_str(), fontDir.length(), &fontDirResult); 28523b3eb3cSopenharmony_ci napi_set_element(env, fontDirSetResult, index++, fontDirResult); 28623b3eb3cSopenharmony_ci } 28723b3eb3cSopenharmony_ci napi_value genericSetResult = GetUIFontGenericInfo(env, fontConfigJsonInfo); 28823b3eb3cSopenharmony_ci napi_value fallbackGroupSetResult = GetUIFontFallbackInfo(env, fontConfigJsonInfo); 28923b3eb3cSopenharmony_ci 29023b3eb3cSopenharmony_ci napi_set_named_property(env, result, "fontDir", fontDirSetResult); 29123b3eb3cSopenharmony_ci napi_set_named_property(env, result, "generic", genericSetResult); 29223b3eb3cSopenharmony_ci napi_set_named_property(env, result, "fallbackGroups", fallbackGroupSetResult); 29323b3eb3cSopenharmony_ci return result; 29423b3eb3cSopenharmony_ci} 29523b3eb3cSopenharmony_ci 29623b3eb3cSopenharmony_cistatic napi_value FontExport(napi_env env, napi_value exports) 29723b3eb3cSopenharmony_ci{ 29823b3eb3cSopenharmony_ci napi_property_descriptor fontDesc[] = { 29923b3eb3cSopenharmony_ci DECLARE_NAPI_FUNCTION("registerFont", JSRegisterFont), 30023b3eb3cSopenharmony_ci DECLARE_NAPI_FUNCTION("getSystemFontList", JSgetSystemFontList), 30123b3eb3cSopenharmony_ci DECLARE_NAPI_FUNCTION("getFontByName", JSgetFontByName), 30223b3eb3cSopenharmony_ci DECLARE_NAPI_FUNCTION("getUIFontConfig", JsGetUIFontConfig) 30323b3eb3cSopenharmony_ci }; 30423b3eb3cSopenharmony_ci NAPI_CALL(env, napi_define_properties(env, exports, sizeof(fontDesc) / sizeof(fontDesc[0]), fontDesc)); 30523b3eb3cSopenharmony_ci return exports; 30623b3eb3cSopenharmony_ci} 30723b3eb3cSopenharmony_ci 30823b3eb3cSopenharmony_cistatic napi_module fontModule = { 30923b3eb3cSopenharmony_ci .nm_version = 1, 31023b3eb3cSopenharmony_ci .nm_flags = 0, 31123b3eb3cSopenharmony_ci .nm_filename = nullptr, 31223b3eb3cSopenharmony_ci .nm_register_func = FontExport, 31323b3eb3cSopenharmony_ci .nm_modname = "font", 31423b3eb3cSopenharmony_ci .nm_priv = ((void*)0), 31523b3eb3cSopenharmony_ci .reserved = { 0 }, 31623b3eb3cSopenharmony_ci}; 31723b3eb3cSopenharmony_ci 31823b3eb3cSopenharmony_ciextern "C" __attribute__((constructor)) void FontRegister() 31923b3eb3cSopenharmony_ci{ 32023b3eb3cSopenharmony_ci napi_module_register(&fontModule); 32123b3eb3cSopenharmony_ci} 32223b3eb3cSopenharmony_ci} // namespace OHOS::Ace::Napi 323