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