1 /*
2 * Copyright (c) 2025 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 #include "ani_typeface.h"
17 #include "typeface_arguments_ani/ani_typeface_arguments.h"
18
19 namespace OHOS::Rosen {
20 namespace Drawing {
21 const char* ANI_CLASS_TYPEFACE_NAME = "L@ohos/graphics/drawing/drawing/Typeface;";
22
AniInit(ani_env * env)23 ani_status AniTypeface::AniInit(ani_env *env)
24 {
25 ani_class cls = nullptr;
26 ani_status ret = env->FindClass(ANI_CLASS_TYPEFACE_NAME, &cls);
27 if (ret != ANI_OK) {
28 ROSEN_LOGE("[ANI] can't find class: %{public}s", ANI_CLASS_TYPEFACE_NAME);
29 return ANI_NOT_FOUND;
30 }
31
32 std::array methods = {
33 ani_native_function { "getFamilyName", ":Lstd/core/String;",
34 reinterpret_cast<void*>(GetFamilyName) },
35 ani_native_function { "makeFromFile", "Lstd/core/String;:L@ohos/graphics/drawing/drawing/Typeface;",
36 reinterpret_cast<void*>(MakeFromFile) },
37 ani_native_function { "makeFromFileWithArguments", "Lstd/core/String;"
38 "L@ohos/graphics/drawing/drawing/TypefaceArguments;:L@ohos/graphics/drawing/drawing/Typeface;",
39 reinterpret_cast<void*>(MakeFromFileWithArguments) },
40 };
41
42 ret = env->Class_BindNativeMethods(cls, methods.data(), methods.size());
43 if (ret != ANI_OK) {
44 ROSEN_LOGE("[ANI] bind methods fail: %{public}s", ANI_CLASS_TYPEFACE_NAME);
45 return ANI_NOT_FOUND;
46 }
47
48 return ANI_OK;
49 }
50
GetFamilyName(ani_env * env,ani_object obj)51 ani_string AniTypeface::GetFamilyName(ani_env* env, ani_object obj)
52 {
53 auto aniTypeface = GetNativeFromObj<AniTypeface>(env, obj);
54 if (aniTypeface == nullptr) {
55 AniThrowError(env, "Invalid params. "); // message length must be a multiple of 4, for example 16, 20, etc
56 return ani_string{};
57 }
58
59 std::shared_ptr<Typeface> typeface = aniTypeface->GetTypeface();
60 if (typeface == nullptr) {
61 AniThrowError(env, "Invalid params. "); // message length must be a multiple of 4, for example 16, 20, etc
62 return ani_string{};
63 }
64 return CreateAniString(env, typeface->GetFamilyName());
65 }
66
MakeFromFile(ani_env * env,ani_object obj,ani_string aniFilePath)67 ani_object AniTypeface::MakeFromFile(ani_env* env, ani_object obj, ani_string aniFilePath)
68 {
69 std::string filePath = CreateStdString(env, aniFilePath);
70 std::shared_ptr<Typeface> typeface = Typeface::MakeFromFile(filePath.c_str());
71 AniTypeface* aniTypeface = new AniTypeface(typeface);
72 ani_object aniObj = CreateAniObjectStatic(env, "L@ohos/graphics/drawing/drawing/Typeface;", aniTypeface);
73 return aniObj;
74 }
75
MakeFromFileWithArguments(ani_env * env,ani_object obj,ani_string aniFilePath,ani_object argumentsObj)76 ani_object AniTypeface::MakeFromFileWithArguments(ani_env* env, ani_object obj, ani_string aniFilePath,
77 ani_object argumentsObj)
78 {
79 std::string filePath = CreateStdString(env, aniFilePath);
80 auto aniTypefaceArguments = GetNativeFromObj<AniTypefaceArguments>(env, argumentsObj);
81 if (aniTypefaceArguments == nullptr) {
82 AniThrowError(env, "Invalid params. "); // message length must be a multiple of 4, for example 16, 20, etc
83 return CreateAniUndefined(env);
84 }
85 FontArguments fontArguments;
86 AniTypefaceArguments::ConvertToFontArguments(aniTypefaceArguments->GetTypefaceArgumentsHelper(), fontArguments);
87 std::shared_ptr<Typeface> typeface = Typeface::MakeFromFile(filePath.c_str(), fontArguments);
88 AniTypeface* aniTypeface = new AniTypeface(typeface);
89 ani_object aniObj = CreateAniObjectStatic(env, "L@ohos/graphics/drawing/drawing/Typeface;", aniTypeface);
90 return aniObj;
91 }
92
GetTypeface()93 std::shared_ptr<Typeface> AniTypeface::GetTypeface()
94 {
95 return typeface_;
96 }
97 } // namespace Drawing
98 } // namespace OHOS::Rosen
99