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 #ifndef FRAMEWORKS_CORE_COMPONENTS_NG_PATTERN_CANVAS_CANVAS_RENDERING_CONTEXT_2D_MODEL_NG_H
17 #define FRAMEWORKS_CORE_COMPONENTS_NG_PATTERN_CANVAS_CANVAS_RENDERING_CONTEXT_2D_MODEL_NG_H
18 
19 #include "base/utils/macros.h"
20 #include "core/components_ng/pattern/canvas/canvas_pattern.h"
21 #include "core/components_ng/pattern/canvas/rendering_context_2d_model.h"
22 
23 namespace OHOS::Ace::NG {
24 
25 class ACE_FORCE_EXPORT CanvasRenderingContext2DModelNG : public OHOS::Ace::CanvasRenderingContext2DModel {
26     DECLARE_ACE_TYPE(CanvasRenderingContext2DModelNG, CanvasRenderingContext2DModel)
27 
28 public:
29     CanvasRenderingContext2DModelNG() = default;
30     ~CanvasRenderingContext2DModelNG() override = default;
31     int32_t GetId() override;
32     void Release() override;
33     void SetOnAttach(std::function<void()>&& callback) override;
34     void SetOnDetach(std::function<void()>&& callback) override;
35     void SetPattern(RefPtr<AceType> pattern) override;
36     void SetFillText(const PaintState& state, const FillTextInfo& fillTextInfo) override;
37     void SetStrokeText(const PaintState& state, const FillTextInfo& fillTextInfo) override;
38     void SetAntiAlias(bool anti) override;
39     void SetFontWeight(const FontWeight& weight) override;
40     void SetFontStyle(const Ace::FontStyle& fontStyle) override;
41     void SetFontFamilies(const std::vector<std::string>& families) override;
42     void SetFontSize(const Dimension& size) override;
43     std::vector<double> GetLineDash() override;
44     void SetFillGradient(const std::shared_ptr<Ace::Gradient>& gradient) override;
45     void SetFillPattern(const std::shared_ptr<Ace::Pattern>& pattern) override;
46     void SetFillColor(const Color& color, bool colorFlag) override;
47     void SetStrokeGradient(const std::shared_ptr<Ace::Gradient>& gradient) override;
48     void SetStrokePattern(const std::shared_ptr<Ace::Pattern>& pattern) override;
49     void SetStrokeColor(const Color& color, bool colorFlag) override;
50     void DrawImage(const ImageInfo& imageInfo) override;
51     void DrawSvgImage(const ImageInfo& imageInfo) override;
52     void PutImageData(const Ace::ImageData& imageData) override;
53     void CloseImageBitmap(const std::string& src) override;
54     std::unique_ptr<Ace::ImageData> GetImageData(const ImageSize& imageSize) override;
55     void DrawPixelMap(const ImageInfo& imageInfo) override;
56     void SetFilterParam(const std::string& src) override;
57     void SetTextDirection(const TextDirection& direction) override;
58     std::string GetJsonData(const std::string& path) override;
59     std::string ToDataURL(const std::string& dataUrl, double quality) override;
60     void SetLineCap(const LineCapStyle& lineCap) override;
61     void SetLineJoin(const LineJoinStyle& lineJoin) override;
62     void SetMiterLimit(double limit) override;
63     void SetLineWidth(double lineWidth) override;
64     void SetGlobalAlpha(double alpha) override;
65     void SetCompositeType(const CompositeOperation& type) override;
66     void SetLineDashOffset(double lineDashOffset) override;
67     void SetShadowBlur(double blur) override;
68     void SetShadowColor(const Color& color) override;
69     void SetShadowOffsetX(double offsetX) override;
70     void SetShadowOffsetY(double offsetY) override;
71     void SetSmoothingEnabled(bool enabled) override;
72     void SetSmoothingQuality(const std::string& quality) override;
73     void MoveTo(double x, double y) override;
74     void LineTo(double x, double y) override;
75     void BezierCurveTo(const BezierCurveParam& param) override;
76     void QuadraticCurveTo(const QuadraticCurveParam& param) override;
77     void ArcTo(const ArcToParam& param) override;
78     void Arc(const ArcParam& param) override;
79     void Ellipse(const EllipseParam& param) override;
80     void SetFillRuleForPath(const CanvasFillRule& fillRule) override;
81     void SetFillRuleForPath2D(const CanvasFillRule& fillRule, const RefPtr<CanvasPath2D>& path) override;
82     void SetStrokeRuleForPath2D(const CanvasFillRule& fillRule, const RefPtr<CanvasPath2D>& path) override;
83     void SetStrokeRuleForPath(const CanvasFillRule& fillRule) override;
84     void SetClipRuleForPath(const CanvasFillRule& fillRule) override;
85     void SetClipRuleForPath2D(const CanvasFillRule& fillRule, const RefPtr<CanvasPath2D>& path) override;
86     void AddRect(const Rect& rect) override;
87     void BeginPath() override;
88     void ClosePath() override;
89     void Restore() override;
90     void CanvasRendererSave() override;
91     void CanvasRendererRotate(double angle) override;
92     void CanvasRendererScale(double x, double y) override;
93     void SetTransform(TransformParam& param, bool lengthFlag) override;
94     void ResetTransform() override;
95     void Transform(const TransformParam& param) override;
96     void Translate(double x, double y) override;
97     void SetLineDash(const std::vector<double>& lineDash) override;
98     void SetTextAlign(const TextAlign& align) override;
99     void SetTextBaseline(const TextBaseline& baseline) override;
100     void FillRect(const Rect& rect) override;
101     void StrokeRect(const Rect& rect) override;
102     void ClearRect(const Rect& rect) override;
103     TransformParam GetTransform() override;
104     RefPtr<Ace::PixelMap> GetPixelMap(const ImageSize& imageSize) override;
105     void GetImageDataModel(const ImageSize& imageSize, uint8_t* buffer) override;
106     void SaveLayer() override;
107     void RestoreLayer() override;
108     void Reset() override;
109     TextMetrics GetMeasureTextMetrics(const PaintState& state, const std::string& text) override;
110     void SetDensity(double density) override;
111 
112     // All interfaces that only the 'CanvasRenderingContext2D' has.
113     void GetWidth(double& width) override;
114     void GetHeight(double& height) override;
115     void StartImageAnalyzer(void* config, OnAnalyzedCallback& onAnalyzed) override;
116     void StopImageAnalyzer() override;
117 #ifdef PIXEL_MAP_SUPPORTED
118     void TransferFromImageBitmap(const RefPtr<AceType>& pixelMap) override;
119 #else
120     void TransferFromImageBitmap(const std::shared_ptr<Ace::ImageData>& imageData) override;
121 #endif
122 
123 private:
124     void GetImageData(const std::shared_ptr<Ace::ImageData>& imageData);
125     void OnAttachToCanvas();
126     void OnDetachFromCanvas();
127     WeakPtr<CanvasPattern> weakPattern_;
128     bool isAttached_ = false;
129     ACE_DISALLOW_COPY_AND_MOVE(CanvasRenderingContext2DModelNG);
130 };
131 } // namespace OHOS::Ace::NG
132 #endif // FRAMEWORKS_CORE_COMPONENTS_NG_PATTERN_CANVAS_CANVAS_RENDERING_CONTEXT_2D_MODEL_NG_H
133