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_CUSTOM_PAINT_PATTERN_H 17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERN_CUSTOM_PAINT_CUSTOM_PAINT_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/custom_paint/custom_paint_event_hub.h" 23 #include "core/components_ng/pattern/custom_paint/custom_paint_layout_algorithm.h" 24 #include "core/components_ng/pattern/pattern.h" 25 #include "core/pipeline_ng/pipeline_context.h" 26 27 namespace OHOS::Ace::NG { 28 class CanvasPaintMethod; 29 class OffscreenCanvasPattern; 30 class RenderingContext2DModifier; 31 // CustomPaintPattern is the base class for custom paint render node to perform paint canvas. 32 class ACE_EXPORT CustomPaintPattern : public Pattern { 33 DECLARE_ACE_TYPE(CustomPaintPattern, Pattern); 34 35 public: 36 CustomPaintPattern() = default; 37 ~CustomPaintPattern() override = default; 38 GetContextParam()39 std::optional<RenderContext::ContextParam> GetContextParam() const override 40 { 41 return RenderContext::ContextParam { RenderContext::ContextType::INCREMENTAL_CANVAS }; 42 } 43 44 RefPtr<NodePaintMethod> CreateNodePaintMethod() override; 45 CreateLayoutAlgorithm()46 RefPtr<LayoutAlgorithm> CreateLayoutAlgorithm() override 47 { 48 return MakeRefPtr<CustomPaintLayoutAlgorithm>(); 49 } 50 CreateEventHub()51 RefPtr<EventHub> CreateEventHub() override 52 { 53 return MakeRefPtr<CustomPaintEventHub>(); 54 } 55 SetCanvasSize(std::optional<SizeF> canvasSize)56 void SetCanvasSize(std::optional<SizeF> canvasSize) 57 { 58 canvasSize_ = canvasSize; 59 } 60 61 void OnPixelRoundFinish(const SizeF& pixelGridRoundSize) override; 62 63 void SetAntiAlias(bool isEnabled); 64 65 void FillRect(const Rect& rect); 66 void StrokeRect(const Rect& rect); 67 void ClearRect(const Rect& rect); 68 void Fill(); 69 void Fill(const RefPtr<CanvasPath2D>& path); 70 void Stroke(); 71 void Stroke(const RefPtr<CanvasPath2D>& path); 72 void Clip(); 73 void Clip(const RefPtr<CanvasPath2D>& path); 74 void BeginPath(); 75 void ClosePath(); 76 void MoveTo(double x, double y); 77 void LineTo(double x, double y); 78 void Arc(const ArcParam& param); 79 void ArcTo(const ArcToParam& param); 80 void AddRect(const Rect& rect); 81 void Ellipse(const EllipseParam& param); 82 void BezierCurveTo(const BezierCurveParam& param); 83 void QuadraticCurveTo(const QuadraticCurveParam& param); 84 85 void FillText(const std::string& text, double x, double y, std::optional<double> maxWidth); 86 void StrokeText(const std::string& text, double x, double y, std::optional<double> maxWidth); 87 double MeasureText(const std::string& text, const PaintState& state); 88 double MeasureTextHeight(const std::string& text, const PaintState& state); 89 TextMetrics MeasureTextMetrics(const std::string& text, const PaintState& state); 90 91 void DrawImage(const Ace::CanvasImage& image, double width, double height); 92 void DrawSvgImage(RefPtr<SvgDomBase> svgDom, const Ace::CanvasImage& image, const ImageFit& imageFit); 93 void DrawPixelMap(RefPtr<PixelMap> pixelMap, const Ace::CanvasImage& image); 94 std::unique_ptr<Ace::ImageData> GetImageData(double left, double top, double width, double height); 95 void GetImageData(const std::shared_ptr<Ace::ImageData>& imageData); 96 void PutImageData(const Ace::ImageData& imageData); 97 void TransferFromImageBitmap(const RefPtr<OffscreenCanvasPattern>& offscreenCanvasPattern); 98 void CloseImageBitmap(const std::string& src); 99 100 void UpdateFillColor(const Color& color); 101 void UpdateFillRuleForPath(const CanvasFillRule rule); 102 void UpdateFillRuleForPath2D(const CanvasFillRule rule); 103 double GetWidth(); 104 double GetHeight(); 105 106 LineDashParam GetLineDash() const; 107 void UpdateLineDash(const std::vector<double>& segments); 108 109 void Save(); 110 void Restore(); 111 void Scale(double x, double y); 112 void Rotate(double angle); 113 void SetTransform(const TransformParam& param); 114 void ResetTransform(); 115 void Transform(const TransformParam& param); 116 void Translate(double x, double y); 117 std::string ToDataURL(const std::string& args); 118 std::string GetJsonData(const std::string& path); 119 120 void UpdateGlobalAlpha(double alpha); 121 void UpdateCompositeOperation(CompositeOperation type); 122 void UpdateSmoothingEnabled(bool enabled); 123 void UpdateSmoothingQuality(const std::string& quality); 124 void UpdateLineCap(LineCapStyle cap); 125 void UpdateLineDashOffset(double dash); 126 void UpdateLineWidth(double width); 127 void UpdateMiterLimit(double limit); 128 void UpdateShadowBlur(double blur); 129 void UpdateShadowOffsetX(double offsetX); 130 void UpdateShadowOffsetY(double offsetY); 131 void UpdateTextAlign(TextAlign align); 132 void UpdateTextBaseline(TextBaseline baseline); 133 void UpdateStrokePattern(const std::weak_ptr<Ace::Pattern>& pattern); 134 void UpdateStrokeColor(const Color& color); 135 void UpdateFontWeight(FontWeight weight); 136 void UpdateFontStyle(FontStyle style); 137 void UpdateFontFamilies(const std::vector<std::string>& families); 138 void UpdateFontSize(const Dimension& size); 139 void UpdateLineJoin(LineJoinStyle join); 140 void UpdateFillGradient(const Ace::Gradient& gradient); 141 void UpdateFillPattern(const std::weak_ptr<Ace::Pattern>& pattern); 142 void UpdateShadowColor(const Color& color); 143 void UpdateStrokeGradient(const Ace::Gradient& grad); 144 void SetTextDirection(TextDirection direction); 145 void SetFilterParam(const std::string& filterStr); 146 TransformParam GetTransform() const; 147 148 void SaveLayer(); 149 void RestoreLayer(); 150 151 void DumpAdvanceInfo() override; 152 153 private: 154 void OnAttachToFrameNode() override; 155 bool OnDirtyLayoutWrapperSwap(const RefPtr<LayoutWrapper>& dirty, const DirtySwapConfig& config) override; 156 157 RefPtr<CanvasPaintMethod> paintMethod_; 158 std::optional<SizeF> canvasSize_; 159 bool isCanvasInit_ = false; 160 SizeF dirtyPixelGridRoundSize_ = { -1, -1 }; 161 SizeF lastDirtyPixelGridRoundSize_ = { -1, -1 }; 162 DirtySwapConfig recordConfig_; 163 164 RefPtr<RenderingContext2DModifier> contentModifier_; 165 166 ACE_DISALLOW_COPY_AND_MOVE(CustomPaintPattern); 167 }; 168 } // namespace OHOS::Ace::NG 169 170 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERN_CUSTOM_PAINT_CUSTOM_PAINT_PATTERN_H 171