1 /*
2  * Copyright (c) 2022-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 FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERN_CUSTOM_PAINT_OFFSCREEN_CANVAS_PATTERN_H
17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERN_CUSTOM_PAINT_OFFSCREEN_CANVAS_PATTERN_H
18 
19 #include "base/memory/referenced.h"
20 #include "core/components/common/properties/paint_state.h"
21 #include "core/components_ng/image_provider/svg_dom_base.h"
22 #include "core/components_ng/pattern/pattern.h"
23 #include "core/pipeline_ng/pipeline_context.h"
24 
25 namespace OHOS::Ace::NG {
26 class OffscreenCanvasPaintMethod;
27 // OffscreenCanvasPattern is the base class for custom paint render node to perform paint canvas.
28 class ACE_EXPORT OffscreenCanvasPattern : public Pattern {
29     DECLARE_ACE_TYPE(OffscreenCanvasPattern, Pattern);
30 
31 public:
32     OffscreenCanvasPattern(int32_t width, int32_t height);
33     ~OffscreenCanvasPattern() override = default;
34 
35     void FillRect(const Rect& rect);
36     void StrokeRect(const Rect& rect);
37     void ClearRect(const Rect& rect);
38     void Fill();
39     void Fill(const RefPtr<CanvasPath2D>& path);
40     void Stroke();
41     void Stroke(const RefPtr<CanvasPath2D>& path);
42     void Clip();
43     void Clip(const RefPtr<CanvasPath2D>& path);
44     void BeginPath();
45     void ClosePath();
46     void MoveTo(double x, double y);
47     void LineTo(double x, double y);
48     void Arc(const ArcParam& param);
49     void ArcTo(const ArcToParam& param);
50     void AddRect(const Rect& rect);
51     void Ellipse(const EllipseParam& param);
52     void BezierCurveTo(const BezierCurveParam& param);
53     void QuadraticCurveTo(const QuadraticCurveParam& param);
54 
55     void FillText(const std::string& text, double x, double y, std::optional<double> maxWidth);
56     void StrokeText(const std::string& text, double x, double y, std::optional<double> maxWidth);
57     TextMetrics MeasureTextMetrics(const std::string& text, const PaintState& state);
58 
59     void DrawImage(const Ace::CanvasImage& image, double width, double height);
60     void DrawSvgImage(RefPtr<SvgDomBase> svgDom, const Ace::CanvasImage& image, const ImageFit& imageFit);
61     void DrawPixelMap(RefPtr<PixelMap> pixelMap, const Ace::CanvasImage& image);
62     std::unique_ptr<Ace::ImageData> GetImageData(double left, double top, double width, double height);
63     void GetImageData(const std::shared_ptr<Ace::ImageData>& imageData);
64     void PutImageData(const Ace::ImageData& imageData);
65 
66     void SetAntiAlias(bool isEnabled);
67     void SetFillRuleForPath(const CanvasFillRule rule);
68     void SetFillRuleForPath2D(const CanvasFillRule rule);
69     void SetFillPattern(const std::weak_ptr<Ace::Pattern>& pattern);
70     void SetFillGradient(const Ace::Gradient& gradient);
71     void SetAlpha(double alpha);
72     void SetCompositeType(CompositeOperation operation);
73     void SetLineWidth(double width);
74     void SetLineCap(LineCapStyle style);
75     void SetLineJoin(LineJoinStyle style);
76     void SetMiterLimit(double limit);
77     void SetTextAlign(TextAlign align);
78     void SetTextBaseline(TextBaseline baseline);
79     void SetShadowBlur(double blur);
80     void SetShadowOffsetX(double x);
81     void SetShadowOffsetY(double y);
82     void SetSmoothingEnabled(bool enabled);
83     void SetSmoothingQuality(const std::string& quality);
84     void SetLineDashOffset(double offset);
85     void SetShadowColor(const Color& color);
86     void SetStrokePattern(const std::weak_ptr<Ace::Pattern>& pattern);
87     void SetStrokeGradient(const Ace::Gradient& gradient);
88     void SetStrokeColor(const Color& color);
89     void SetFontWeight(FontWeight weight);
90     void SetFontStyle(FontStyle style);
91     void SetFontFamilies(const std::vector<std::string>& fontFamilies);
92     void SetFontSize(const Dimension& size);
93     void SetFillColor(const Color& color);
94     int32_t GetWidth();
95     int32_t GetHeight();
96 
97     LineDashParam GetLineDash() const;
98     void SetLineDash(const std::vector<double>& segments);
99 
100     void SetTextDirection(TextDirection direction);
101     void SetFilterParam(const std::string& filterStr);
102 
103     void Save();
104     void Restore();
105     void Scale(double x, double y);
106     void Rotate(double angle);
107     void SetTransform(const TransformParam& param);
108     void ResetTransform();
109     void Transform(const TransformParam& param);
110     void Translate(double x, double y);
111     TransformParam GetTransform() const;
112     std::string ToDataURL(const std::string& type, const double quality);
113     void SaveLayer();
114     void RestoreLayer();
115     void UpdateSize(int32_t width, int32_t height);
116     void Reset();
117     RefPtr<PixelMap> TransferToImageBitmap();
118     void SetDensity(double density);
119 
120     size_t GetBitmapSize();
121 private:
122     void UpdateTextDefaultDirection();
123     RefPtr<OffscreenCanvasPaintMethod> offscreenPaintMethod_;
124     TextDirection currentSetTextDirection_ = TextDirection::INHERIT;
125     ACE_DISALLOW_COPY_AND_MOVE(OffscreenCanvasPattern);
126 };
127 } // namespace OHOS::Ace::NG
128 
129 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERN_CUSTOM_PAINT_OFFSCREEN_CANVAS_PATTERN_H
130