• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_path.h"
17 
18 namespace OHOS::Rosen {
19 namespace Drawing {
20 const char* ANI_CLASS_PATH_NAME = "L@ohos/graphics/drawing/drawing/Path;";
21 
AniInit(ani_env * env)22 ani_status AniPath::AniInit(ani_env *env)
23 {
24     ani_class cls = nullptr;
25     ani_status ret = env->FindClass(ANI_CLASS_PATH_NAME, &cls);
26     if (ret != ANI_OK) {
27         ROSEN_LOGE("[ANI] can't find class: %{public}s", ANI_CLASS_PATH_NAME);
28         return ANI_NOT_FOUND;
29     }
30 
31     std::array methods = {
32         ani_native_function { "constructorNative", ":V", reinterpret_cast<void*>(Constructor) },
33         ani_native_function { "constructorNative", "L@ohos/graphics/drawing/drawing/Path;:V",
34             reinterpret_cast<void*>(ConstructorWithPath) },
35         ani_native_function { "arcTo", "DDDDDD:V", reinterpret_cast<void*>(ArcTo) },
36         ani_native_function { "reset", ":V", reinterpret_cast<void*>(Reset) },
37     };
38 
39     ret = env->Class_BindNativeMethods(cls, methods.data(), methods.size());
40     if (ret != ANI_OK) {
41         ROSEN_LOGE("[ANI] bind methods fail: %{public}s", ANI_CLASS_PATH_NAME);
42         return ANI_NOT_FOUND;
43     }
44 
45     return ANI_OK;
46 }
47 
Constructor(ani_env * env,ani_object obj)48 void AniPath::Constructor(ani_env* env, ani_object obj)
49 {
50     AniPath* aniPath = new AniPath();
51     if (ANI_OK != env->Object_SetFieldByName_Long(obj, NATIVE_OBJ, reinterpret_cast<ani_long>(aniPath))) {
52         ROSEN_LOGE("AniPath::Constructor failed create AniPath");
53         delete aniPath;
54         return;
55     }
56 }
57 
ConstructorWithPath(ani_env * env,ani_object obj,ani_object aniPathObj)58 void AniPath::ConstructorWithPath(ani_env* env, ani_object obj, ani_object aniPathObj)
59 {
60     auto aniPath = GetNativeFromObj<AniPath>(env, aniPathObj);
61     if (aniPath == nullptr) {
62         AniThrowError(env, "Invalid params. "); // message length must be a multiple of 4, for example 16, 20, etc
63         return;
64     }
65 
66     AniPath* newAniPath = new AniPath(aniPath->GetPath());
67     if (ANI_OK != env->Object_SetFieldByName_Long(obj, NATIVE_OBJ, reinterpret_cast<ani_long>(newAniPath))) {
68         ROSEN_LOGE("AniPath::Constructor failed create AniPath");
69         delete newAniPath;
70         return;
71     }
72 }
73 
Reset(ani_env * env,ani_object obj)74 void AniPath::Reset(ani_env* env, ani_object obj)
75 {
76     auto aniPath = GetNativeFromObj<AniPath>(env, obj);
77     if (aniPath == nullptr) {
78         AniThrowError(env, "Invalid params. ");
79         return;
80     }
81 
82     aniPath->GetPath().Reset();
83 }
84 
ArcTo(ani_env * env,ani_object obj,ani_double x1,ani_double y1,ani_double x2,ani_double y2,ani_double startDeg,ani_double sweepDeg)85 void AniPath::ArcTo(ani_env* env, ani_object obj, ani_double x1, ani_double y1, ani_double x2, ani_double y2,
86     ani_double startDeg, ani_double sweepDeg)
87 {
88     auto aniPath = GetNativeFromObj<AniPath>(env, obj);
89     if (aniPath == nullptr) {
90         AniThrowError(env, "Invalid params. ");
91         return;
92     }
93 
94     aniPath->GetPath().ArcTo(x1, y1, x2, y2, startDeg, sweepDeg);
95     return;
96 }
97 
98 
GetPath()99 Path& AniPath::GetPath()
100 {
101     return path_;
102 }
103 } // namespace Drawing
104 } // namespace OHOS::Rosen