1 /*
2  * Copyright (c) 2021-2023 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_CUSTOM_PAINT_ROSEN_RENDER_OFFSCREEN_CANVAS_H
17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_CUSTOM_PAINT_ROSEN_RENDER_OFFSCREEN_CANVAS_H
18 
19 #include "modules/svg/include/SkSVGDOM.h"
20 
21 #ifndef USE_GRAPHIC_TEXT_GINE
22 #include "txt/paragraph.h"
23 #else
24 #include "rosen_text/text_style.h"
25 #include "rosen_text/typography.h"
26 #endif
27 #ifndef USE_ROSEN_DRAWING
28 #include "include/core/SkCanvas.h"
29 #include "include/core/SkPath.h"
30 #endif
31 
32 #include "core/components/custom_paint/offscreen_canvas.h"
33 #include "core/components/custom_paint/render_custom_paint.h"
34 #include "core/image/image_object.h"
35 #include "core/image/image_provider.h"
36 #include "core/image/image_source_info.h"
37 
38 namespace OHOS::Ace {
39 using setColorFunc = std::function<void(const std::string&)>;
40 class RosenRenderOffscreenCanvas : public RenderOffscreenCanvas {
41     DECLARE_ACE_TYPE(RosenRenderOffscreenCanvas, RenderOffscreenCanvas);
42 
43 public:
44     RosenRenderOffscreenCanvas(const WeakPtr<PipelineBase>& context, int32_t width, int32_t height);
45     ~RosenRenderOffscreenCanvas() override = default;
46     void SetAntiAlias(bool isEnabled) override;
47     void FillRect(Rect rect) override;
48     void ClearRect(Rect rect) override;
49     void StrokeRect(Rect rect) override;
50     void FillText(const std::string& text, double x, double y, const PaintState& state) override;
51     void StrokeText(const std::string& text, double x, double y, const PaintState& state) override;
52     double MeasureText(const std::string& text, const PaintState& state) override;
53     double MeasureTextHeight(const std::string& text, const PaintState& state) override;
54     TextMetrics MeasureTextMetrics(const std::string& text, const PaintState& state) override;
55     std::string ToDataURL(const std::string& type, const double quality) override;
56     std::unique_ptr<ImageData> GetImageData(double left, double top, double width, double height) override;
57     void BeginPath() override;
58     void Arc(const ArcParam& param) override;
59     void Stroke() override;
60     void Stroke(const RefPtr<CanvasPath2D>& path) override;
61     void ArcTo(const ArcToParam& param) override;
62     void MoveTo(double x, double y) override;
63     void ClosePath() override;
64     void Rotate(double angle) override;
65     void Scale(double x, double y) override;
66     void AddRect(const Rect& rect) override;
67     void Fill() override;
68     void Fill(const RefPtr<CanvasPath2D>& path) override;
69     void Clip() override;
70     void Clip(const RefPtr<CanvasPath2D>& path) override;
71     void PutImageData(const ImageData& imageData) override;
72     void DrawImage(const CanvasImage& canvasImage, double width, double height) override;
73     void DrawPixelMap(RefPtr<PixelMap> pixelMap, const CanvasImage& image) 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 Ellipse(const EllipseParam& param) override;
78     void SetTransform(const TransformParam& param) override;
79     void Transform(const TransformParam& param) override;
80     void Translate(double x, double y) override;
81     void Restore() override;
82     void Save() override;
83     bool IsPointInStroke(double x, double y) override;
84     bool IsPointInStroke(const RefPtr<CanvasPath2D>& path, double x, double y) override;
85     bool IsPointInPath(double x, double y) override;
86     bool IsPointInPath(const RefPtr<CanvasPath2D>& path, double x, double y) override;
87     void ResetTransform() override;
88     void SetFillRuleForPath(const CanvasFillRule& rule) override;
89     void SetFillRuleForPath2D(const CanvasFillRule& rule) override;
90 
91 private:
92     void InitImagePaint();
93     void InitCachePaint();
94     bool antiAlias_ = true;
95 #ifndef USE_ROSEN_DRAWING
96     SkPaint GetStrokePaint();
97 
98     SkSamplingOptions options_;
99 
100 #else
101     RSPen GetStrokePaint();
102     RSSamplingOptions options_;
103 #endif
104     WeakPtr<PipelineBase> pipelineContext_;
105 #ifndef USE_ROSEN_DRAWING
106     SkBitmap skBitmap_;
107     SkPath skPath_;
108     SkPath skPath2d_;
109     SkPaint imagePaint_;
110     SkPaint cachePaint_;
111     SkBitmap cacheBitmap_;
112     std::unique_ptr<SkCanvas> cacheCanvas_;
113     std::unique_ptr<SkCanvas> skCanvas_;
114 #else
115     RSBitmap bitmap_;
116     RSRecordingPath path_;
117     RSRecordingPath path2d_;
118     RSBrush imageBrush_;
119     RSBrush cacheBrush_;
120     RSBitmap cacheBitmap_;
121     std::unique_ptr<RSCanvas> cacheCanvas_;
122     std::unique_ptr<RSCanvas> canvas_;
123 #endif
124     std::map<std::string, setColorFunc> filterFunc_;
125     ImageSourceInfo loadingSource_;
126     ImageSourceInfo currentSource_;
127     ImageObjSuccessCallback imageObjSuccessCallback_;
128     UploadSuccessCallback uploadSuccessCallback_;
129     FailedCallback failedCallback_;
130     OnPostBackgroundTask onPostBackgroundTask_;
131     sk_sp<SkSVGDOM> skiaDom_ = nullptr;
132     CanvasImage canvasImage_;
133 
134 #ifndef USE_ROSEN_DRAWING
135     void UpdatePaintShader(SkPaint& paint, const Gradient& gradient);
136     void UpdatePaintShader(const Pattern& pattern, SkPaint& paint);
137 #else
138     void UpdatePaintShader(RSPen* pen, RSBrush* brush, const Gradient& gradient);
139     void UpdatePaintShader(const Pattern& pattern, RSPen* pen, RSBrush* brush);
140 #endif
141     void PaintText(const std::string& text, double x, double y, bool isStroke, bool hasShadow = false);
142 #ifndef USE_GRAPHIC_TEXT_GINE
143     double GetBaselineOffset(TextBaseline baseline, std::unique_ptr<txt::Paragraph>& paragraph);
144     std::unique_ptr<txt::Paragraph> paragraph_;
145 #else
146     double GetBaselineOffset(TextBaseline baseline, std::unique_ptr<Rosen::Typography>& paragraph);
147     std::unique_ptr<Rosen::Typography> paragraph_;
148 #endif
149     bool HasShadow() const;
150     bool HasImageShadow() const;
151 #ifndef USE_GRAPHIC_TEXT_GINE
152     void UpdateTextStyleForeground(bool isStroke, txt::TextStyle& style, bool hasShadow);
153     double GetAlignOffset(const std::string& text, TextAlign align, std::unique_ptr<txt::Paragraph>& paragraph);
154 #else
155     void UpdateTextStyleForeground(bool isStroke, Rosen::TextStyle& style, bool hasShadow);
156     double GetAlignOffset(const std::string& text, TextAlign align, std::unique_ptr<Rosen::Typography>& paragraph);
157 #endif
158     TextDirection GetTextDirection(const std::string& text);
159     bool UpdateOffParagraph(const std::string& text, bool isStroke, const PaintState& state, bool hasShadow = false);
160 #ifndef USE_ROSEN_DRAWING
161     void UpdateLineDash(SkPaint& paint);
162 #else
163     void UpdateLineDash(RSPen& pen);
164 #endif
165     void Path2DAddPath(const PathArgs& args);
166     void Path2DSetTransform(const PathArgs& args);
167     void Path2DMoveTo(const PathArgs& args);
168     void Path2DLineTo(const PathArgs& args);
169     void Path2DArc(const PathArgs& args);
170     void Path2DArcTo(const PathArgs& args);
171     void Path2DQuadraticCurveTo(const PathArgs& args);
172     void Path2DBezierCurveTo(const PathArgs& args);
173     void Path2DEllipse(const PathArgs& args);
174     void Path2DRect(const PathArgs& args);
175     void Path2DClosePath(const PathArgs& args);
176     void Path2DStroke();
177     void Path2DFill();
178     void Path2DClip();
179     void ParsePath2D(const RefPtr<CanvasPath2D>& path);
180     void TranspareCmdToPath(const RefPtr<CanvasPath2D>& path);
181 #ifndef USE_ROSEN_DRAWING
182     bool IsPointInPathByColor(double x, double y, SkPath& path, SkColor colorMatch);
183 #else
184     bool IsPointInPathByColor(double x, double y, RSPath& path, RSColorQuad colorMatch);
185 #endif
186     void SetPaintImage();
187     void InitFilterFunc();
188     bool GetFilterType(std::string& filterType, std::string& filterParam);
189     void SetGrayFilter(const std::string& percent);
190     void SetSepiaFilter(const std::string& percent);
191     void SetInvertFilter(const std::string& percent);
192     void SetOpacityFilter(const std::string& percent);
193     void SetBrightnessFilter(const std::string& percent);
194     void SetContrastFilter(const std::string& percent);
195     void SetBlurFilter(const std::string& percent);
196     void SetDropShadowFilter(const std::string& percent);
197     void SetSaturateFilter(const std::string& percent);
198     void SetHueRotateFilter(const std::string& percent);
199     double PxStrToDouble(const std::string& str);
200     double BlurStrToDouble(const std::string& str);
201     bool IsPercentStr(std::string& percentStr);
202     void SetColorFilter(float matrix[20]);
203     void InitImageCallbacks();
204     void ImageObjReady(const RefPtr<ImageObject>& imageObj);
205     void ImageObjFailed();
206     void DrawSvgImage(const CanvasImage& canvasImage);
207 };
208 } // namespace OHOS::Ace
209 
210 #endif // !FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_CUSTOM_PAINT_ROSEN_RENDER_OFFSCREEN_CANVAS_H
211