1 /* 2 * Copyright (c) 2022-2024 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_OFFSCREEN_CANVAS_PATTERN_H 17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERN_CUSTOM_PAINT_OFFSCREEN_CANVAS_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/pattern.h" 23 #include "core/pipeline_ng/pipeline_context.h" 24 25 namespace OHOS::Ace::NG { 26 class OffscreenCanvasPaintMethod; 27 // OffscreenCanvasPattern is the base class for custom paint render node to perform paint canvas. 28 class CJ_FORCE_EXPORT OffscreenCanvasPattern : public Pattern { 29 DECLARE_ACE_TYPE(OffscreenCanvasPattern, Pattern); 30 31 public: 32 OffscreenCanvasPattern(int32_t width, int32_t height); 33 ~OffscreenCanvasPattern() override = default; 34 35 void FillRect(const Rect& rect); 36 void StrokeRect(const Rect& rect); 37 void ClearRect(const Rect& rect); 38 void Fill(); 39 void Fill(const RefPtr<CanvasPath2D>& path); 40 void Stroke(); 41 void Stroke(const RefPtr<CanvasPath2D>& path); 42 void Clip(); 43 void Clip(const RefPtr<CanvasPath2D>& path); 44 void BeginPath(); 45 void ClosePath(); 46 void MoveTo(double x, double y); 47 void LineTo(double x, double y); 48 void Arc(const ArcParam& param); 49 void ArcTo(const ArcToParam& param); 50 void AddRect(const Rect& rect); 51 void AddRoundRect(const Rect& rect, const std::vector<double>& radii); 52 void Ellipse(const EllipseParam& param); 53 void BezierCurveTo(const BezierCurveParam& param); 54 void QuadraticCurveTo(const QuadraticCurveParam& param); 55 56 void FillText(const std::string& text, double x, double y, std::optional<double> maxWidth); 57 void StrokeText(const std::string& text, double x, double y, std::optional<double> maxWidth); 58 TextMetrics MeasureTextMetrics(const std::string& text, const PaintState& state); 59 60 void DrawImage(const Ace::CanvasImage& image, double width, double height); 61 void DrawSvgImage(RefPtr<SvgDomBase> svgDom, const Ace::CanvasImage& image, const ImageFit& imageFit); 62 void DrawPixelMap(RefPtr<PixelMap> pixelMap, const Ace::CanvasImage& image); 63 std::unique_ptr<Ace::ImageData> GetImageData(double left, double top, double width, double height); 64 void GetImageData(const std::shared_ptr<Ace::ImageData>& imageData); 65 void PutImageData(const Ace::ImageData& imageData); 66 67 void SetAntiAlias(bool isEnabled); 68 void SetFillRuleForPath(const CanvasFillRule rule); 69 void SetFillRuleForPath2D(const CanvasFillRule rule); 70 void SetFillPattern(const std::weak_ptr<Ace::Pattern>& pattern); 71 void SetFillGradient(const Ace::Gradient& gradient); 72 void SetAlpha(double alpha); 73 void SetCompositeType(CompositeOperation operation); 74 void SetLineWidth(double width); 75 void SetLineCap(LineCapStyle style); 76 void SetLineJoin(LineJoinStyle style); 77 void SetMiterLimit(double limit); 78 void SetTextAlign(TextAlign align); 79 void SetTextBaseline(TextBaseline baseline); 80 void SetShadowBlur(double blur); 81 void SetShadowOffsetX(double x); 82 void SetShadowOffsetY(double y); 83 void SetSmoothingEnabled(bool enabled); 84 void SetSmoothingQuality(const std::string& quality); 85 void SetLineDashOffset(double offset); 86 void SetShadowColor(const Color& color); 87 void SetStrokePattern(const std::weak_ptr<Ace::Pattern>& pattern); 88 void SetStrokeGradient(const Ace::Gradient& gradient); 89 void SetStrokeColor(const Color& color); 90 void SetFontWeight(FontWeight weight); 91 void SetFontStyle(FontStyle style); 92 void SetFontFamilies(const std::vector<std::string>& fontFamilies); 93 void SetFontSize(const Dimension& size); 94 void SetLetterSpacing(const Dimension& letterSpacing); 95 void SetFillColor(const Color& color); 96 int32_t GetWidth(); 97 int32_t GetHeight(); 98 99 LineDashParam GetLineDash() const; 100 void SetLineDash(const std::vector<double>& segments); 101 102 void SetTextDirection(TextDirection direction); 103 void SetFilterParam(const std::string& filterStr); 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 TransformParam GetTransform() const; 114 std::string ToDataURL(const std::string& type, const double quality); 115 void SaveLayer(); 116 void RestoreLayer(); 117 void UpdateSize(int32_t width, int32_t height); 118 void Reset(); 119 RefPtr<PixelMap> TransferToImageBitmap(); 120 void SetDensity(double density); 121 void SetTransform(std::shared_ptr<Ace::Pattern> pattern, const TransformParam& transform); 122 123 size_t GetBitmapSize(); 124 private: 125 void UpdateTextDefaultDirection(); 126 RefPtr<OffscreenCanvasPaintMethod> offscreenPaintMethod_; 127 TextDirection currentSetTextDirection_ = TextDirection::INHERIT; 128 ACE_DISALLOW_COPY_AND_MOVE(OffscreenCanvasPattern); 129 }; 130 } // namespace OHOS::Ace::NG 131 132 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERN_CUSTOM_PAINT_OFFSCREEN_CANVAS_PATTERN_H 133