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