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