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 #ifndef FRAMEWORKS_BRIDGE_CJ_FRONTEND_CPP_VIEW_CAVANS_RENDERER_H
17 #define FRAMEWORKS_BRIDGE_CJ_FRONTEND_CPP_VIEW_CAVANS_RENDERER_H
18 
19 #include "ffi_remote_data.h"
20 #include "pixel_map_impl.h"
21 
22 #include "base/memory/referenced.h"
23 #include "bridge/cj_frontend/cppview/canvas_gradient.h"
24 #include "bridge/cj_frontend/cppview/canvas_path.h"
25 #include "bridge/cj_frontend/interfaces/cj_ffi/cj_macro.h"
26 #include "bridge/common/utils/utils.h"
27 #include "core/components/custom_paint/custom_paint_component.h"
28 #include "core/components_ng/pattern/canvas/canvas_rendering_context_2d_model_ng.h"
29 #include "core/components_ng/pattern/canvas/offscreen_canvas_pattern.h"
30 
31 namespace OHOS::Ace::Framework {
32 
33 struct Metrics {
34     double width;
35     double height;
36 };
37 
38 class ACE_EXPORT NativeCanvasRenderer : public OHOS::FFI::FFIData {
39     DECL_TYPE(NativeCanvasRenderer, OHOS::FFI::FFIData)
40 public:
41     explicit NativeCanvasRenderer(bool antialias);
42     ~NativeCanvasRenderer() override;
43 
44     void SetCanvasPattern(const RefPtr<AceType>& canvas);
45     void SetAntiAlias();
46 
47     static RefPtr<CanvasPath2D> MakePath2D(const std::string& capStr);
48 
49     void SetFillStyle(const Color& color);
50     void SetFillStyle(const sptr<NativeCanvasGradient>& nativeCanvasGradient);
51     void SetLineWidth(const double lineWidth);
52     void SetStrokeStyle(const Color& color);
53     void SetStrokeStyle(const sptr<NativeCanvasGradient>& nativeCanvasGradient);
54     void SetLineCap(const LineCapStyle lineCap);
55     void SetLineJoin(const LineJoinStyle lineJoin);
56     void SetMiterLimit(double limit);
57     void SetFont(const FontStyle style, const std::string& weight, const Dimension& size, const std::string& family);
58     void SetTextAlign(const TextAlign align);
59     void SetTextBaseline(const TextBaseline baseline);
60     void SetGlobalAlpha(double alpha);
61     void SetLineDash(std::vector<double>& lineDash);
62     void SetLineDashOffset(double lineDashOffset);
63     void SetGlobalCompositeOperation(const CompositeOperation type);
64     void SetShadowBlur(double blur);
65     void SetShadowColor(const Color& color);
66     void SetShadowOffsetX(double offsetX);
67     void SetShadowOffsetY(double offsetY);
68     void SetImageSmoothingEnabled(bool enabled);
69     void SetImageSmoothingQuality(const std::string& quality);
70 
71     void FillRect(const Rect& rect);
72     void StrokeRect(const Rect& rect);
73     void ClearRect(const Rect& rect);
74     void FillText(double x, double y, const std::string& text);
75     void StrokeText(double x, double y, const std::string& text);
76     Metrics MeasureText(const std::string& text);
77     void Stroke();
78     void Stroke(const sptr<NativeCanvasPath>& canvasPath);
79     void BeginPath();
80     void MoveTo(double x, double y);
81     void LineTo(double x, double y);
82     void ClosePath();
83     void BezierCurveTo(const BezierCurveParam& param);
84     void QuadraticCurveTo(const QuadraticCurveParam& param);
85     void Arc(const ArcParam& param);
86     void ArcTo(const ArcToParam& param);
87     void Ellipse(const EllipseParam& param);
88     void NormalRect(const Rect& rect);
89     void Fill();
90     void Clip();
91     void Rotate(double angle);
92     void Scale(double x, double y);
93     void Transform(const TransformParam& param);
94     void SetTransform(TransformParam param);
95     void Translate(double x, double y);
96     void Restore();
97     void Save();
98     int64_t CreateLinearGradient(double x0, double y0, double x1, double y1);
99     int64_t CreateRadialGradient(double x0, double y0, double r0, double x1, double y1, double r1);
100     void DrawImage(const CanvasImage& image);
101     void DrawImage(const RefPtr<OHOS::Ace::PixelMap>& pixelMap, const CanvasImage& image);
102     std::unique_ptr<ImageData> GetImageData(double fLeft, double fTop, double fWidth, double fHeight);
103     int64_t GetPixelMap(double left, double top, double width, double height);
104 
105 private:
106     bool antialias_ = false;
107     bool isInitializeShadow_ = false;
108 
109     RefPtr<AceType> canvasPattern_;
110     RefPtr<RenderingContext2DModel> renderingContext2DModel_;
111 
112     PaintState paintState_;
113     TextStyle style_;
114     ImageData imageData_;
115 };
116 
117 } // namespace OHOS::Ace::Framework
118 #endif // FRAMEWORKS_BRIDGE_CJ_FRONTEND_CPP_VIEW_CAVANS_RENDERER_H
119