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_PAINTER_ROSEN_SVG_PAINTER_H 17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_PAINTER_ROSEN_SVG_PAINTER_H 18 19 #ifndef USE_ROSEN_DRAWING 20 #include "include/core/SkCanvas.h" 21 #include "include/core/SkPaint.h" 22 #include "include/core/SkPoint.h" 23 #endif 24 #include "include/core/SkTypeface.h" 25 26 #ifdef USE_ROSEN_DRAWING 27 #include "core/components_ng/render/drawing.h" 28 #endif 29 #include "frameworks/base/geometry/matrix4.h" 30 #include "frameworks/core/components/common/properties/motion_path_evaluator.h" 31 #include "frameworks/core/components/common/properties/svg_paint_state.h" 32 #include "frameworks/core/components/svg/render_svg_base.h" 33 #include "frameworks/core/pipeline/base/render_node.h" 34 35 namespace OHOS::Ace { 36 37 struct SvgTextInfo { 38 FillState fillState; 39 StrokeState strokeState; 40 SvgTextStyle textStyle; 41 std::string data; 42 double opacity = 1.0; 43 }; 44 45 struct PathDrawInfo { 46 std::string path; 47 double offset = 0.0; 48 double rotate = 0.0; 49 }; 50 51 struct TextDrawInfo { 52 Offset offset; 53 double rotate = 0.0; 54 }; 55 56 struct RenderInfo { 57 RefPtr<RenderSvgBase> node = nullptr; 58 Offset offset; 59 uint8_t opacity = UINT8_MAX; 60 bool antiAlias = true; 61 }; 62 63 class RosenSvgPainter { 64 public: 65 RosenSvgPainter() = default; 66 ~RosenSvgPainter() = default; 67 68 static void CheckFontType(); 69 static double GetPathLength(const std::string& path); 70 static Offset GetPathOffset(const std::string& path, double current); 71 static bool GetMotionPathPosition(const std::string& path, double percent, MotionPathPosition& result); 72 #ifndef USE_ROSEN_DRAWING 73 static void SetMask(SkCanvas* canvas); 74 static void SetFillStyle( 75 SkPaint& skPaint, const FillState& fillState, uint8_t opacity = UINT8_MAX, bool antiAlias = true); 76 static void SetFillStyle(SkCanvas* canvas, const SkPath& skPath, const FillState& fillState, 77 uint8_t opacity = UINT8_MAX, bool antiAlias = true); 78 static void SetFillStyle( 79 SkCanvas* canvas, const SkPath& skPath, const FillState& fillState, RenderInfo& renderInfo); 80 static void SetStrokeStyle( 81 SkPaint& skPaint, const StrokeState& strokeState, uint8_t opacity = UINT8_MAX, bool antiAlias = true); 82 static void SetStrokeStyle(SkCanvas* canvas, const SkPath& skPath, const StrokeState& strokeState, 83 uint8_t opacity = UINT8_MAX, bool antiAlias = true); 84 static void SetStrokeStyle( 85 SkCanvas* canvas, const SkPath& skPath, const StrokeState& strokeState, RenderInfo& renderInfo); 86 static void UpdateLineDash(SkPaint& paint, const StrokeState& strokeState); 87 static Offset UpdateText(SkCanvas* canvas, const SvgTextInfo& svgTextInfo, const TextDrawInfo& textDrawInfo); 88 static double UpdateTextPath(SkCanvas* canvas, const SvgTextInfo& svgTextInfo, const PathDrawInfo& pathDrawInfo); 89 #else 90 static void SetMask(RSCanvas* canvas); 91 static void SetFillStyle( 92 RSBrush& brush, const FillState& fillState, uint8_t opacity = UINT8_MAX, bool antiAlias = true); 93 static void SetFillStyle(RSCanvas* canvas, const RSPath& path, 94 const FillState& fillState, uint8_t opacity = UINT8_MAX, bool antiAlias = true); 95 static void SetFillStyle(RSCanvas* canvas, const RSPath& path, 96 const FillState& fillState, RenderInfo& renderInfo); 97 static void SetStrokeStyle(RSPen& pen, const StrokeState& strokeState, 98 uint8_t opacity = UINT8_MAX, bool antiAlias = true); 99 static void SetStrokeStyle(RSCanvas* canvas, const RSPath& path, 100 const StrokeState& strokeState, uint8_t opacity = UINT8_MAX, bool antiAlias = true); 101 static void SetStrokeStyle(RSCanvas* canvas, const RSPath& path, 102 const StrokeState& strokeState, RenderInfo& renderInfo); 103 static void UpdateLineDash(RSPen& pen, const StrokeState& strokeState); 104 static Offset UpdateText(RSCanvas* canvas, const SvgTextInfo& svgTextInfo, 105 const TextDrawInfo& textDrawInfo); 106 static double UpdateTextPath(RSCanvas* canvas, const SvgTextInfo& svgTextInfo, 107 const PathDrawInfo& pathDrawInfo); 108 #endif 109 static Offset MeasureTextBounds(const SvgTextInfo& svgTextInfo, const TextDrawInfo& textDrawInfo, Rect& bounds); 110 static double MeasureTextPathBounds(const SvgTextInfo& svgTextInfo, const PathDrawInfo& pathDrawInfo, Rect& bounds); 111 #ifndef USE_ROSEN_DRAWING 112 static void StringToPoints(const char str[], std::vector<SkPoint>& points); 113 #else 114 static void StringToPoints(const char str[], std::vector<RSPoint>& points); 115 #endif 116 static void UpdateMotionMatrix( 117 const std::shared_ptr<RSNode>& rsNode, const std::string& path, const std::string& rotate, double percent); 118 #ifndef USE_ROSEN_DRAWING 119 static SkMatrix ToSkMatrix(const Matrix4& matrix4); 120 121 static void SetGradientStyle(SkPaint& skPaint, const FillState& fillState, double opacity); 122 #else 123 static RSMatrix ToDrawingMatrix(const Matrix4& matrix4); 124 static void SetGradientStyle(RSBrush& brush, const FillState& fillState, double opacity); 125 #endif 126 127 #ifndef USE_ROSEN_DRAWING 128 static sk_sp<SkTypeface> fontTypeChinese_; 129 static sk_sp<SkTypeface> fontTypeNormal_; 130 #else 131 static std::shared_ptr<RSTypeface> fontTypeChinese_; 132 static std::shared_ptr<RSTypeface> fontTypeNormal_; 133 #endif 134 }; 135 136 } // namespace OHOS::Ace 137 138 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_PAINTER_ROSEN_SVG_PAINTER_H 139