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 #include "bridge/cj_frontend/interfaces/cj_ffi/cj_shape_ffi.h"
17
18 #include <cinttypes>
19
20 #include "bridge/cj_frontend/cppview/shape_abstract.h"
21 #include "bridge/cj_frontend/interfaces/cj_ffi/cj_view_abstract_ffi.h"
22 #include "core/components/common/properties/paint_state.h"
23 #include "core/components_ng/base/view_abstract_model_ng.h"
24 #include "core/components_ng/pattern/shape/shape_model_ng.h"
25
26 #ifndef _NON_OHOS_
27 #include "foundation/multimedia/image_framework/frameworks/kits/cj/include/pixel_map_impl.h"
28 #endif
29
30 using namespace OHOS::Ace;
31
32 namespace {
33 using VectorDoublePtr = void*;
34 using VectorInt32Ptr = void*;
35 const std::vector<LineCapStyle> LINE_CAP_STYLE_LIST = { LineCapStyle::BUTT, LineCapStyle::ROUND, LineCapStyle::SQUARE };
36 const std::vector<LineJoinStyle> LINE_JOIN_STYLE_LIST = { LineJoinStyle::MITER, LineJoinStyle::ROUND,
37 LineJoinStyle::BEVEL };
38 } // namespace
39 extern "C" {
FfiOHOSAceFrameworkShapeCreate()40 void FfiOHOSAceFrameworkShapeCreate()
41 {
42 ShapeModel::GetInstance()->Create();
43 ViewAbstractModel::GetInstance()->SetFocusable(true);
44 RefPtr<PixelMap> pixMapOhos = nullptr;
45 ShapeModel::GetInstance()->InitBox(pixMapOhos);
46 }
47
FfiOHOSAceFrameworkShapeCreateWithPixelMap(int64_t pixelMapId)48 void FfiOHOSAceFrameworkShapeCreateWithPixelMap(int64_t pixelMapId)
49 {
50 #ifndef _NON_OHOS_
51 ShapeModel::GetInstance()->Create();
52 ViewAbstractModel::GetInstance()->SetFocusable(true);
53 RefPtr<PixelMap> pixMapOhos = nullptr;
54 do {
55 if (pixelMapId <= 0) {
56 LOGI("pixel map ID not valid");
57 break;
58 }
59 auto instance = OHOS::FFI::FFIData::GetData<OHOS::Media::PixelMapImpl>(pixelMapId);
60 if (instance == nullptr) {
61 LOGE("create with pixel map error, Cannot get PixelMapProxy by id: %{public}" PRId64, pixelMapId);
62 break;
63 }
64 std::shared_ptr<OHOS::Media::PixelMap> pixelMap = instance->GetRealPixelMap();
65 if (pixelMap == nullptr) {
66 LOGE("create with pixel map error, Cannot get pixMap in PixelMapProxy");
67 break;
68 }
69 pixMapOhos = PixelMap::CreatePixelMap(&pixelMap);
70 if (pixMapOhos == nullptr) {
71 LOGE("create with pixel map error, Cannot create PixelMapOhos by pixMap");
72 break;
73 }
74 } while (false);
75 ShapeModel::GetInstance()->InitBox(pixMapOhos);
76 #endif
77 }
78
FfiOHOSAceFrameworkShapeSetViewPort(FFIAtCViewPort viewPort)79 void FfiOHOSAceFrameworkShapeSetViewPort(FFIAtCViewPort viewPort)
80 {
81 Dimension dimLeft(viewPort.x, static_cast<DimensionUnit>(viewPort.unitX));
82 Dimension dimTop(viewPort.y, static_cast<DimensionUnit>(viewPort.unitY));
83 Dimension dimWidth(viewPort.width, static_cast<DimensionUnit>(viewPort.unitWidth));
84 Dimension dimHeight(viewPort.height, static_cast<DimensionUnit>(viewPort.unitHeight));
85 ShapeModel::GetInstance()->SetViewPort(dimLeft, dimTop, dimWidth, dimHeight);
86 }
87
FfiOHOSAceFrameworkShapeSetFill(uint32_t color)88 void FfiOHOSAceFrameworkShapeSetFill(uint32_t color)
89 {
90 ShapeModel::GetInstance()->SetFill(Color(color));
91 }
92
FfiOHOSAceFrameworkShapeSetFillOpacity(double fillOpacity)93 void FfiOHOSAceFrameworkShapeSetFillOpacity(double fillOpacity)
94 {
95 // Not display when fill opacity is negative
96 if (fillOpacity < 0.0f) {
97 fillOpacity = 0.0f;
98 }
99 ShapeModel::GetInstance()->SetFillOpacity(fillOpacity);
100 }
101
FfiOHOSAceFrameworkShapeSetStroke(uint32_t color)102 void FfiOHOSAceFrameworkShapeSetStroke(uint32_t color)
103 {
104 ShapeModel::GetInstance()->SetStroke(Color(color));
105 }
106
FfiOHOSAceFrameworkShapeSetStrokeDashArray(VectorFloat64Ptr vecValue,VectorInt32Ptr vecUnit)107 void FfiOHOSAceFrameworkShapeSetStrokeDashArray(VectorFloat64Ptr vecValue, VectorInt32Ptr vecUnit)
108 {
109 const auto& dashValue = *reinterpret_cast<std::vector<double>*>(vecValue);
110 const auto& dashUnit = *reinterpret_cast<std::vector<int32_t>*>(vecUnit);
111 std::vector<Dimension> dashArray;
112
113 for (size_t i = 0; i < dashValue.size(); ++i) {
114 DimensionUnit defaultUnit = DimensionUnit::VP;
115 if (i < dashUnit.size()) {
116 defaultUnit = static_cast<DimensionUnit>(dashUnit.at(i));
117 }
118 dashArray.emplace_back(Dimension(dashValue.at(i), defaultUnit));
119 }
120 // if odd,add twice
121 if ((static_cast<uint32_t>(dashValue.size()) & 1)) {
122 for (size_t i = 0; i < dashValue.size(); i++) {
123 dashArray.emplace_back(dashArray[i]);
124 }
125 }
126 ShapeModel::GetInstance()->SetStrokeDashArray(dashArray);
127 }
128
FfiOHOSAceFrameworkShapeSetStrokeDashOffset(double offset,int32_t offsetUnit)129 void FfiOHOSAceFrameworkShapeSetStrokeDashOffset(double offset, int32_t offsetUnit)
130 {
131 Dimension dimOffset(offset, static_cast<DimensionUnit>(offsetUnit));
132 ShapeModel::GetInstance()->SetStrokeDashOffset(dimOffset);
133 }
134
FfiOHOSAceFrameworkShapeSetStrokeLineCap(int32_t lineCap)135 void FfiOHOSAceFrameworkShapeSetStrokeLineCap(int32_t lineCap)
136 {
137 ShapeModel::GetInstance()->SetStrokeLineCap(static_cast<int32_t>(LINE_CAP_STYLE_LIST[lineCap]));
138 }
139
FfiOHOSAceFrameworkShapeSetStrokeLineJoin(int32_t lineJoin)140 void FfiOHOSAceFrameworkShapeSetStrokeLineJoin(int32_t lineJoin)
141 {
142 ShapeModel::GetInstance()->SetStrokeLineJoin(static_cast<int32_t>(LINE_JOIN_STYLE_LIST[lineJoin]));
143 }
144
FfiOHOSAceFrameworkShapeSetStrokeMiterLimit(double miterLimit)145 void FfiOHOSAceFrameworkShapeSetStrokeMiterLimit(double miterLimit)
146 {
147 ShapeModel::GetInstance()->SetStrokeMiterLimit(miterLimit);
148 }
149
FfiOHOSAceFrameworkShapeSetStrokeOpacity(double strokeOpacity)150 void FfiOHOSAceFrameworkShapeSetStrokeOpacity(double strokeOpacity)
151 {
152 // Not display when fill stroke is negative
153 if (strokeOpacity < 0.0f) {
154 strokeOpacity = 0.0f;
155 }
156 ShapeModel::GetInstance()->SetStrokeOpacity(strokeOpacity);
157 }
158
FfiOHOSAceFrameworkShapeSetStrokeWidth(double strokeWidth,int32_t widthUnit)159 void FfiOHOSAceFrameworkShapeSetStrokeWidth(double strokeWidth, int32_t widthUnit)
160 {
161 Dimension dimStrokeWidth(strokeWidth, static_cast<DimensionUnit>(widthUnit));
162 ShapeModel::GetInstance()->SetStrokeWidth(dimStrokeWidth);
163 }
164
FfiOHOSAceFrameworkShapeSetAntiAlias(bool antiAlias)165 void FfiOHOSAceFrameworkShapeSetAntiAlias(bool antiAlias)
166 {
167 ShapeModel::GetInstance()->SetAntiAlias(antiAlias);
168 }
169
FfiOHOSAceFrameworkShapeSetMesh(VectorFloat64Ptr vecValue,uint32_t column,uint32_t row)170 void FfiOHOSAceFrameworkShapeSetMesh(VectorFloat64Ptr vecValue, uint32_t column, uint32_t row)
171 {
172 if (vecValue == nullptr) {
173 LOGE("mesh array is empty");
174 return;
175 }
176 auto meshValue = *reinterpret_cast<std::vector<double>*>(vecValue);
177 ShapeModel::GetInstance()->SetBitmapMesh(meshValue, static_cast<int32_t>(column), static_cast<int32_t>(row));
178 }
179
FfiOHOSAceFrameworkShapeSetWidth(double width,int32_t unit)180 void FfiOHOSAceFrameworkShapeSetWidth(double width, int32_t unit)
181 {
182 FfiOHOSAceFrameworkViewAbstractSetWidth(width, unit);
183 ShapeModel::GetInstance()->SetWidth();
184 }
185
FfiOHOSAceFrameworkShapeSetHeight(double height,int32_t unit)186 void FfiOHOSAceFrameworkShapeSetHeight(double height, int32_t unit)
187 {
188 FfiOHOSAceFrameworkViewAbstractSetHeight(height, unit);
189 ShapeModel::GetInstance()->SetHeight();
190 }
191
FfiOHOSAceFrameworkShapeSetSize(double width,int32_t widthUnit,double height,int32_t heightUnit)192 void FfiOHOSAceFrameworkShapeSetSize(double width, int32_t widthUnit, double height, int32_t heightUnit)
193 {
194 FfiOHOSAceFrameworkShapeSetWidth(width, widthUnit);
195 FfiOHOSAceFrameworkShapeSetHeight(height, heightUnit);
196 }
197
FfiOHOSAceFrameworkShapeSetInsWidth(int64_t shapeId,double value,int32_t unit)198 void FfiOHOSAceFrameworkShapeSetInsWidth(int64_t shapeId, double value, int32_t unit)
199 {
200 auto context = OHOS::FFI::FFIData::GetData<OHOS::Ace::Framework::NativeShapeAbstract>(shapeId);
201 if (context != nullptr) {
202 Dimension dValue(value, static_cast<DimensionUnit>(unit));
203 if (LessNotEqual(dValue.Value(), 0.0)) {
204 dValue.SetValue(0.0);
205 }
206 context->SetWidth(dValue);
207 } else {
208 LOGI("set shape width error, Cannot get NativeShape by id: %{public}" PRId64, shapeId);
209 }
210 }
211
FfiOHOSAceFrameworkShapeSetInsHeight(int64_t shapeId,double value,int32_t unit)212 void FfiOHOSAceFrameworkShapeSetInsHeight(int64_t shapeId, double value, int32_t unit)
213 {
214 auto context = OHOS::FFI::FFIData::GetData<OHOS::Ace::Framework::NativeShapeAbstract>(shapeId);
215 if (context != nullptr) {
216 Dimension dValue(value, static_cast<DimensionUnit>(unit));
217 if (LessNotEqual(dValue.Value(), 0.0)) {
218 dValue.SetValue(0.0);
219 }
220 context->SetHeight(dValue);
221 } else {
222 LOGI("set shape height error, Cannot get NativeShape by id: %{public}" PRId64, shapeId);
223 }
224 }
225
FfiOHOSAceFrameworkShapeSetInsSize(int64_t shapeId,double width,int32_t widthUnit,double height,int32_t heightUnit)226 void FfiOHOSAceFrameworkShapeSetInsSize(
227 int64_t shapeId, double width, int32_t widthUnit, double height, int32_t heightUnit)
228 {
229 auto context = OHOS::FFI::FFIData::GetData<OHOS::Ace::Framework::NativeShapeAbstract>(shapeId);
230 if (context != nullptr) {
231 Dimension dWidth(width, static_cast<DimensionUnit>(widthUnit));
232 Dimension dHeight(height, static_cast<DimensionUnit>(heightUnit));
233 if (LessNotEqual(dWidth.Value(), 0.0)) {
234 dWidth.SetValue(0.0);
235 }
236 if (LessNotEqual(dHeight.Value(), 0.0)) {
237 dHeight.SetValue(0.0);
238 }
239 context->SetSize(dWidth, dHeight);
240 } else {
241 LOGI("set shape size error, Cannot get NativeShape by id: %{public}" PRId64, shapeId);
242 }
243 }
244
FfiOHOSAceFrameworkShapeSetInsOffset(int64_t shapeId,double x,int32_t xUnit,double y,int32_t yUnit)245 void FfiOHOSAceFrameworkShapeSetInsOffset(int64_t shapeId, double x, int32_t xUnit, double y, int32_t yUnit)
246 {
247 Dimension offsetX(x, static_cast<DimensionUnit>(xUnit));
248 Dimension offsetY(y, static_cast<DimensionUnit>(yUnit));
249 auto context = OHOS::FFI::FFIData::GetData<OHOS::Ace::Framework::NativeShapeAbstract>(shapeId);
250 if (context != nullptr) {
251 context->SetOffset(offsetX, offsetY);
252 } else {
253 LOGI("set shape offset error, Cannot get NativeShape by id: %{public}" PRId64, shapeId);
254 }
255 }
256
FfiOHOSAceFrameworkShapeSetInsFill(int64_t shapeId,uint32_t color)257 void FfiOHOSAceFrameworkShapeSetInsFill(int64_t shapeId, uint32_t color)
258 {
259 auto context = OHOS::FFI::FFIData::GetData<OHOS::Ace::Framework::NativeShapeAbstract>(shapeId);
260 if (context != nullptr) {
261 context->SetFill(Color(color));
262 } else {
263 LOGI("set shape fill error, Cannot get NativeShape by id: %{public}" PRId64, shapeId);
264 }
265 }
266 }
267