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_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 ACE_EXPORT OffscreenCanvasPattern : public Pattern { 29 DECLARE_ACE_TYPE(OffscreenCanvasPattern, Pattern); 30 31 public: 32 OffscreenCanvasPattern(int32_t width, int32_t height); 33 OffscreenCanvasPattern(const RefPtr<PipelineBase> context, int32_t width, int32_t height); 34 ~OffscreenCanvasPattern() override = default; 35 36 void FillRect(const Rect& rect); 37 void StrokeRect(const Rect& rect); 38 void ClearRect(const Rect& rect); 39 void Fill(); 40 void Fill(const RefPtr<CanvasPath2D>& path); 41 void Stroke(); 42 void Stroke(const RefPtr<CanvasPath2D>& path); 43 void Clip(); 44 void Clip(const RefPtr<CanvasPath2D>& path); 45 void BeginPath(); 46 void ClosePath(); 47 void MoveTo(double x, double y); 48 void LineTo(double x, double y); 49 void Arc(const ArcParam& param); 50 void ArcTo(const ArcToParam& param); 51 void AddRect(const Rect& rect); 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, const PaintState& state); 57 void StrokeText( 58 const std::string& text, double x, double y, std::optional<double> maxWidth, const PaintState& state); 59 double MeasureText(const std::string& text, const PaintState& state); 60 double MeasureTextHeight(const std::string& text, const PaintState& state); 61 TextMetrics MeasureTextMetrics(const std::string& text, const PaintState& state); 62 63 void DrawImage(const Ace::CanvasImage& image, double width, double height); 64 void DrawSvgImage(RefPtr<SvgDomBase> svgDom, const Ace::CanvasImage& image, const ImageFit& imageFit); 65 void DrawPixelMap(RefPtr<PixelMap> pixelMap, const Ace::CanvasImage& image); 66 std::unique_ptr<Ace::ImageData> GetImageData(double left, double top, double width, double height); 67 void GetImageData(const std::shared_ptr<Ace::ImageData>& imageData); 68 void PutImageData(const Ace::ImageData& imageData); 69 70 void SetAntiAlias(bool isEnabled); 71 void SetFillRuleForPath(const CanvasFillRule rule); 72 void SetFillRuleForPath2D(const CanvasFillRule rule); 73 void SetFillPattern(const std::weak_ptr<Ace::Pattern>& pattern); 74 void SetFillGradient(const Ace::Gradient& gradient); 75 void SetAlpha(double alpha); 76 void SetCompositeType(CompositeOperation operation); 77 void SetLineWidth(double width); 78 void SetLineCap(LineCapStyle style); 79 void SetLineJoin(LineJoinStyle style); 80 void SetMiterLimit(double limit); 81 void SetTextAlign(TextAlign align); 82 void SetTextBaseline(TextBaseline baseline); 83 void SetShadowBlur(double blur); 84 void SetShadowOffsetX(double x); 85 void SetShadowOffsetY(double y); 86 void SetSmoothingEnabled(bool enabled); 87 void SetSmoothingQuality(const std::string& quality); 88 void SetLineDashOffset(double offset); 89 void SetShadowColor(const Color& color); 90 void SetStrokePattern(const std::weak_ptr<Ace::Pattern>& pattern); 91 void SetStrokeGradient(const Ace::Gradient& gradient); 92 void SetStrokeColor(const Color& color); 93 void SetFontWeight(FontWeight weight); 94 void SetFontStyle(FontStyle style); 95 void SetFontFamilies(const std::vector<std::string>& fontFamilies); 96 void SetFontSize(const Dimension& size); 97 void SetFillColor(const Color& color); 98 int32_t GetWidth(); 99 int32_t GetHeight(); 100 101 LineDashParam GetLineDash() const; 102 void SetLineDash(const std::vector<double>& segments); 103 104 void SetTextDirection(TextDirection direction); 105 void SetFilterParam(const std::string& filterStr); 106 107 void Save(); 108 void Restore(); 109 void Scale(double x, double y); 110 void Rotate(double angle); 111 void SetTransform(const TransformParam& param); 112 void ResetTransform(); 113 void Transform(const TransformParam& param); 114 void Translate(double x, double y); 115 TransformParam GetTransform() const; 116 std::string ToDataURL(const std::string& type, const double quality); 117 void SaveLayer(); 118 void RestoreLayer(); 119 void UpdateSize(int32_t width, int32_t height); 120 bool IsSucceed(); 121 122 size_t GetBitmapSize(); 123 private: 124 RefPtr<OffscreenCanvasPaintMethod> offscreenPaintMethod_; 125 ACE_DISALLOW_COPY_AND_MOVE(OffscreenCanvasPattern); 126 }; 127 } // namespace OHOS::Ace::NG 128 129 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERN_CUSTOM_PAINT_OFFSCREEN_CANVAS_PATTERN_H 130