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_CUSTOM_PAINT_H 17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_CUSTOM_PAINT_ROSEN_RENDER_CUSTOM_PAINT_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/SkBitmap.h" 29 #include "include/core/SkCanvas.h" 30 #include "include/core/SkPaint.h" 31 #include "include/core/SkPath.h" 32 #endif 33 34 #include "base/utils/measure_util.h" 35 #include "core/components/custom_paint/offscreen_canvas.h" 36 #include "core/components/custom_paint/render_custom_paint.h" 37 #include "core/image/image_object.h" 38 #include "core/image/image_provider.h" 39 #include "core/image/image_source_info.h" 40 41 namespace OHOS::Ace { 42 class RosenRenderCustomPaint : public RenderCustomPaint { 43 DECLARE_ACE_TYPE(RosenRenderCustomPaint, RenderCustomPaint); 44 45 public: 46 RosenRenderCustomPaint(); 47 ~RosenRenderCustomPaint() override; 48 49 void Paint(RenderContext& context, const Offset& offset) override; 50 51 void TransferFromImageBitmap(const RefPtr<OffscreenCanvas>& offscreenCanvas) override; 52 void DrawBitmapMesh(const RefPtr<OffscreenCanvas>& offscreenCanvas, 53 const std::vector<double>& mesh, int32_t column, int32_t row) override; 54 #ifndef USE_ROSEN_DRAWING 55 void Mesh(SkBitmap& bitmap, int column, int row, 56 const float* vertices, const int* colors, const SkPaint* paint); 57 #else 58 void Mesh(RSBitmap& bitmap, int column, int row, 59 const float* vertices, const int* colors, const RSBrush* brush); 60 #endif 61 std::string ToDataURL(const std::string& args) override; 62 void SetAntiAlias(bool isEnabled) override; 63 void FillRect(const Offset& offset, const Rect& rect) override; 64 void StrokeRect(const Offset& offset, const Rect& rect) override; 65 void ClearRect(const Offset& offset, const Rect& rect) override; 66 void FillText(const Offset& offset, const std::string& text, double x, double y) override; 67 void StrokeText(const Offset& offset, const std::string& text, double x, double y) override; 68 static double MeasureTextInner(const MeasureContext& context); 69 static Size MeasureTextSizeInner(const MeasureContext& context); 70 double MeasureText(const std::string& text, const PaintState& state) override; 71 double MeasureTextHeight(const std::string& text, const PaintState& state) override; 72 TextMetrics MeasureTextMetrics(const std::string& text, const PaintState& state) override; 73 void MoveTo(const Offset& offset, double x, double y) override; 74 void LineTo(const Offset& offset, double x, double y) override; 75 void BezierCurveTo(const Offset& offset, const BezierCurveParam& param) override; 76 void QuadraticCurveTo(const Offset& offset, const QuadraticCurveParam& param) override; 77 void Arc(const Offset& offset, const ArcParam& param) override; 78 void ArcTo(const Offset& offset, const ArcToParam& param) override; 79 void Ellipse(const Offset& offset, const EllipseParam& param) override; 80 void AddRect(const Offset& offset, const Rect& rect) override; 81 void Fill(const Offset& offset) override; 82 void Fill(const Offset& offset, const RefPtr<CanvasPath2D>& path) override; 83 void Stroke(const Offset& offset) override; 84 void Stroke(const Offset& offset, const RefPtr<CanvasPath2D>& path) override; 85 void Clip() override; 86 void Clip(const RefPtr<CanvasPath2D>& path) override; 87 void BeginPath() override; 88 void ClosePath() override; 89 void ResetTransform() override; 90 void Restore() override; 91 void Save() override; 92 void Rotate(double angle) override; 93 void Scale(double x, double y) override; 94 void SetTransform(const TransformParam& param) override; 95 void Transform(const TransformParam& param) override; 96 void Translate(double x, double y) override; 97 void DrawImage(const Offset& offset, const CanvasImage& canvasImage, double width, double height) override; 98 void DrawPixelMap(RefPtr<PixelMap> pixelMap, const CanvasImage& canvasImage) override; 99 void PutImageData(const Offset& offset, const ImageData& imageData) override; 100 std::unique_ptr<ImageData> GetImageData(double left, double top, double width, double height) override; 101 std::string GetJsonData(const std::string& path) override; 102 103 void WebGLInit(CanvasRenderContextBase* context) override; 104 void WebGLUpdate() override; 105 106 void SetFillRuleForPath(const CanvasFillRule& rule) override; 107 void SetFillRuleForPath2D(const CanvasFillRule& rule) override; 108 static bool IsApplyIndent(const MeasureContext& context, double& indent); 109 static void ApplyLineHeightInNumUnit(const MeasureContext& context, Rosen::TextStyle& txtStyle); 110 IsRepaintBoundary()111 bool IsRepaintBoundary() const override 112 { 113 return RenderNode::IsRepaintBoundary(); 114 } 115 116 private: 117 void InitImagePaint(); 118 #ifndef USE_ROSEN_DRAWING 119 void InitPaintBlend(SkPaint& paint); 120 #else 121 void InitPaintBlend(RSBrush& brush); 122 #endif 123 bool UpdateParagraph(const Offset& offset, const std::string& text, bool isStroke, bool hasShadow = false); 124 void PaintText(const Offset& offset, double x, double y, bool isStroke, bool hasShadow = false); 125 #ifndef USE_GRAPHIC_TEXT_GINE 126 double GetAlignOffset(TextAlign align, std::unique_ptr<txt::Paragraph>& paragraph); 127 double GetBaselineOffset(TextBaseline baseline, std::unique_ptr<txt::Paragraph>& paragraph); 128 #else 129 double GetAlignOffset(TextAlign align, std::unique_ptr<Rosen::Typography>& paragraph); 130 double GetBaselineOffset(TextBaseline baseline, std::unique_ptr<Rosen::Typography>& paragraph); 131 #endif 132 #ifndef USE_ROSEN_DRAWING 133 SkPaint GetStrokePaint(); 134 #else 135 RSPen GetStrokePaint(); 136 #endif 137 bool HasShadow() const; 138 #ifndef USE_ROSEN_DRAWING 139 void UpdatePaintShader(const Offset& offset, SkPaint& paint, const Gradient& gradient); 140 void UpdatePaintShader(const Pattern& pattern, SkPaint& paint); 141 void UpdateLineDash(SkPaint& paint); 142 #else 143 void UpdatePaintShader(const Offset& offset, RSPen* pen, RSBrush* brush, const Gradient& gradient); 144 void UpdatePaintShader(const Pattern& pattern, RSPen* pen, RSBrush* brush); 145 void UpdateLineDash(RSPen& paint); 146 #endif 147 #ifndef USE_GRAPHIC_TEXT_GINE 148 void UpdateTextStyleForeground(const Offset& offset, bool isStroke, txt::TextStyle& style, bool hasShadow); 149 #else 150 void UpdateTextStyleForeground(const Offset& offset, bool isStroke, Rosen::TextStyle& style, bool hasShadow); 151 #endif 152 153 void Path2DAddPath(const Offset& offset, const PathArgs& args); 154 void Path2DSetTransform(const Offset& offset, const PathArgs& args); 155 void Path2DMoveTo(const Offset& offset, const PathArgs& args); 156 void Path2DLineTo(const Offset& offset, const PathArgs& args); 157 void Path2DArc(const Offset& offset, const PathArgs& args); 158 void Path2DArcTo(const Offset& offset, const PathArgs& args); 159 void Path2DQuadraticCurveTo(const Offset& offset, const PathArgs& args); 160 void Path2DBezierCurveTo(const Offset& offset, const PathArgs& args); 161 void Path2DEllipse(const Offset& offset, const PathArgs& args); 162 void Path2DRect(const Offset& offset, const PathArgs& args); 163 void Path2DClosePath(const Offset& offset, const PathArgs& args); 164 void Path2DStroke(const Offset& offset); 165 void Path2DFill(const Offset& offset); 166 void Path2DClip(); 167 void ParsePath2D(const Offset& offset, const RefPtr<CanvasPath2D>& path); 168 void InitImageCallbacks(); 169 void ImageObjReady(const RefPtr<ImageObject>& imageObj); 170 void ImageObjFailed(); 171 void DrawSvgImage(const Offset& offset, const CanvasImage& canvasImage); 172 #ifndef USE_ROSEN_DRAWING 173 sk_sp<SkImage> GetImage(const std::string& src); 174 #else 175 std::shared_ptr<RSImage> GetImage(const std::string& src); 176 #endif 177 void CreateBitmap(double viewScale); 178 bool CreateSurface(double viewScale); 179 180 bool antiAlias_ = false; 181 #ifndef USE_GRAPHIC_TEXT_GINE 182 std::unique_ptr<txt::Paragraph> paragraph_; 183 #else 184 std::unique_ptr<Rosen::Typography> paragraph_; 185 #endif 186 #ifndef USE_ROSEN_DRAWING 187 SkPath skPath_; 188 // Specifically refers to the class Path2D in canvas.d.ts 189 SkPath skPath2d_; 190 SkPaint imagePaint_; 191 SkPaint cachePaint_; 192 SkSamplingOptions options_; 193 SkBitmap cacheBitmap_; 194 SkBitmap canvasCache_; 195 SkBitmap webglBitmap_; 196 std::unique_ptr<SkCanvas> skCanvas_; 197 std::unique_ptr<SkCanvas> cacheCanvas_; 198 #else 199 RSRecordingPath drawingPath_; 200 // Specifically refers to the class Path2D in canvas.d.ts 201 RSRecordingPath drawingPath2d_; 202 RSBrush imageBrush_; 203 RSBrush cacheBrush_; 204 RSSamplingOptions options_; 205 RSBitmap cacheBitmap_; 206 RSBitmap canvasCache_; 207 RSBitmap webglBitmap_; 208 std::unique_ptr<RSCanvas> drawingCanvas_; 209 std::unique_ptr<RSCanvas> cacheCanvas_; 210 #endif 211 ImageSourceInfo loadingSource_; 212 ImageSourceInfo currentSource_; 213 ImageObjSuccessCallback imageObjSuccessCallback_; 214 UploadSuccessCallback uploadSuccessCallback_; 215 FailedCallback failedCallback_; 216 OnPostBackgroundTask onPostBackgroundTask_; 217 RefPtr<ImageObject> imageObj_ = nullptr; 218 sk_sp<SkSVGDOM> skiaDom_ = nullptr; 219 CanvasImage canvasImage_; 220 Size lastLayoutSize_; 221 RefPtr<ImageCache> imageCache_; 222 }; 223 } // namespace OHOS::Ace 224 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_CUSTOM_PAINT_ROSEN_RENDER_CUSTOM_PAINT_H 225