1 /*
2 * Copyright (c) 2023-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 #include "drawing_path_effect.h"
17
18 #include <mutex>
19 #include <unordered_map>
20
21 #include "drawing_canvas_utils.h"
22 #include "drawing_helper.h"
23
24 #include "effect/path_effect.h"
25
26 using namespace OHOS;
27 using namespace Rosen;
28 using namespace Drawing;
29
CastToPath(const OH_Drawing_Path & cPath)30 static const Path& CastToPath(const OH_Drawing_Path& cPath)
31 {
32 return reinterpret_cast<const Path&>(cPath);
33 }
34
OH_Drawing_CreateComposePathEffect(OH_Drawing_PathEffect * outer,OH_Drawing_PathEffect * inner)35 OH_Drawing_PathEffect* OH_Drawing_CreateComposePathEffect(OH_Drawing_PathEffect* outer, OH_Drawing_PathEffect* inner)
36 {
37 if (outer == nullptr || inner == nullptr) {
38 return nullptr;
39 }
40 NativeHandle<PathEffect>* pathEffect1 = Helper::CastTo<OH_Drawing_PathEffect*, NativeHandle<PathEffect>*>(outer);
41 NativeHandle<PathEffect>* pathEffect2 = Helper::CastTo<OH_Drawing_PathEffect*, NativeHandle<PathEffect>*>(inner);
42 if (!pathEffect1->value || !pathEffect2->value) {
43 return nullptr;
44 }
45 NativeHandle<PathEffect>* pathEffectHandle = new NativeHandle<PathEffect>;
46 pathEffectHandle->value = PathEffect::CreateComposePathEffect(*pathEffect1->value, *pathEffect2->value);
47 if (pathEffectHandle->value == nullptr) {
48 delete pathEffectHandle;
49 return nullptr;
50 }
51 return Helper::CastTo<NativeHandle<PathEffect>*, OH_Drawing_PathEffect*>(pathEffectHandle);
52 }
53
OH_Drawing_CreateCornerPathEffect(float radius)54 OH_Drawing_PathEffect* OH_Drawing_CreateCornerPathEffect(float radius)
55 {
56 if (radius <= 0) {
57 return nullptr;
58 }
59 NativeHandle<PathEffect>* pathEffectHandle = new NativeHandle<PathEffect>;
60 pathEffectHandle->value = PathEffect::CreateCornerPathEffect(radius);
61 if (pathEffectHandle->value == nullptr) {
62 delete pathEffectHandle;
63 return nullptr;
64 }
65 return Helper::CastTo<NativeHandle<PathEffect>*, OH_Drawing_PathEffect*>(pathEffectHandle);
66 }
67
OH_Drawing_CreateDashPathEffect(float * intervals,int count,float phase)68 OH_Drawing_PathEffect* OH_Drawing_CreateDashPathEffect(float* intervals, int count, float phase)
69 {
70 if (intervals == nullptr || count <= 0) {
71 g_drawingErrorCode = OH_DRAWING_ERROR_INVALID_PARAMETER;
72 return nullptr;
73 }
74 NativeHandle<PathEffect>* pathEffectHandle = new NativeHandle<PathEffect>;
75 pathEffectHandle->value = PathEffect::CreateDashPathEffect(intervals, count, phase);
76 if (pathEffectHandle->value == nullptr) {
77 delete pathEffectHandle;
78 return nullptr;
79 }
80 return Helper::CastTo<NativeHandle<PathEffect>*, OH_Drawing_PathEffect*>(pathEffectHandle);
81 }
82
OH_Drawing_CreateDiscretePathEffect(float segLength,float deviation)83 OH_Drawing_PathEffect* OH_Drawing_CreateDiscretePathEffect(float segLength, float deviation)
84 {
85 NativeHandle<PathEffect>* pathEffectHandle = new NativeHandle<PathEffect>;
86 pathEffectHandle->value = PathEffect::CreateDiscretePathEffect(segLength, deviation);
87 if (pathEffectHandle->value == nullptr) {
88 delete pathEffectHandle;
89 return nullptr;
90 }
91 return Helper::CastTo<NativeHandle<PathEffect>*, OH_Drawing_PathEffect*>(pathEffectHandle);
92 }
93
OH_Drawing_CreatePathDashEffect(const OH_Drawing_Path * path,float advance,float phase,OH_Drawing_PathDashStyle type)94 OH_Drawing_PathEffect* OH_Drawing_CreatePathDashEffect(const OH_Drawing_Path* path, float advance, float phase,
95 OH_Drawing_PathDashStyle type)
96 {
97 if (path == nullptr || advance <= 0) {
98 return nullptr;
99 }
100 NativeHandle<PathEffect>* pathEffectHandle = new NativeHandle<PathEffect>;
101 pathEffectHandle->value = PathEffect::CreatePathDashEffect(CastToPath(*path), advance,
102 phase, static_cast<PathDashStyle>(type));
103 if (pathEffectHandle->value == nullptr) {
104 delete pathEffectHandle;
105 return nullptr;
106 }
107 return Helper::CastTo<NativeHandle<PathEffect>*, OH_Drawing_PathEffect*>(pathEffectHandle);
108 }
109
OH_Drawing_CreateSumPathEffect(OH_Drawing_PathEffect * firstPathEffect,OH_Drawing_PathEffect * secondPathEffect)110 OH_Drawing_PathEffect* OH_Drawing_CreateSumPathEffect(OH_Drawing_PathEffect* firstPathEffect,
111 OH_Drawing_PathEffect* secondPathEffect)
112 {
113 if (firstPathEffect == nullptr || secondPathEffect == nullptr) {
114 return nullptr;
115 }
116 NativeHandle<PathEffect>* pathEffectHandleFirst = Helper::CastTo<OH_Drawing_PathEffect*,
117 NativeHandle<PathEffect>*>(firstPathEffect);
118 NativeHandle<PathEffect>* pathEffectHandleSecond = Helper::CastTo<OH_Drawing_PathEffect*,
119 NativeHandle<PathEffect>*>(secondPathEffect);
120 if (!pathEffectHandleFirst->value || !pathEffectHandleSecond->value) {
121 return nullptr;
122 }
123 NativeHandle<PathEffect>* pathEffectHandle = new NativeHandle<PathEffect>;
124 pathEffectHandle->value = PathEffect::CreateSumPathEffect(*pathEffectHandleFirst->value,
125 *pathEffectHandleSecond->value);
126 if (pathEffectHandle->value == nullptr) {
127 delete pathEffectHandle;
128 return nullptr;
129 }
130 return Helper::CastTo<NativeHandle<PathEffect>*, OH_Drawing_PathEffect*>(pathEffectHandle);
131 }
132
OH_Drawing_PathEffectDestroy(OH_Drawing_PathEffect * cPathEffect)133 void OH_Drawing_PathEffectDestroy(OH_Drawing_PathEffect* cPathEffect)
134 {
135 if (!cPathEffect) {
136 return;
137 }
138 delete Helper::CastTo<OH_Drawing_PathEffect*, NativeHandle<PathEffect>*>(cPathEffect);
139 }
140