1 /* 2 * Copyright (c) 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 FRAMEWORKS_BRIDGE_CJ_FRONTEND_CPP_VIEW_CAVANS_RENDERER_H 17 #define FRAMEWORKS_BRIDGE_CJ_FRONTEND_CPP_VIEW_CAVANS_RENDERER_H 18 19 #include <cstdint> 20 #include <string> 21 22 #include "ffi_remote_data.h" 23 #include "pixel_map_impl.h" 24 25 #include "base/memory/referenced.h" 26 #include "bridge/cj_frontend/cppview/canvas_gradient.h" 27 #include "bridge/cj_frontend/cppview/canvas_image_data.h" 28 #include "bridge/cj_frontend/cppview/canvas_path.h" 29 #include "bridge/cj_frontend/cppview/matrix2d.h" 30 #include "bridge/cj_frontend/cppview/render_image.h" 31 #include "bridge/cj_frontend/interfaces/cj_ffi/cj_macro.h" 32 #include "bridge/common/utils/utils.h" 33 #include "core/components/common/properties/decoration.h" 34 #include "core/components/common/properties/paint_state.h" 35 #include "core/components/custom_paint/custom_paint_component.h" 36 #include "core/components_ng/pattern/canvas/canvas_rendering_context_2d_model_ng.h" 37 38 namespace OHOS::Ace::Framework { 39 40 class NativeCanvasPattern; 41 class NativeCanvasPath; 42 43 struct RenderImage { 44 CanvasUnit unit = CanvasUnit::DEFAULT; 45 double width_ = 0; 46 double height_ = 0; 47 std::string src; 48 std::shared_ptr<ImageData> imageData; 49 int64_t pixMapID = -1; 50 }; 51 52 struct Metrics { 53 double width; 54 double height; 55 double actualBoundingBoxLeft; 56 double actualBoundingBoxRight; 57 double actualBoundingBoxAscent; 58 double actualBoundingBoxDescent; 59 double alphabeticBaseline; 60 double emHeightAscent; 61 double emHeightDescent; 62 double fontBoundingBoxAscent; 63 double fontBoundingBoxDescent; 64 double hangingBaseline; 65 double ideographicBaseline; 66 }; 67 68 class ACE_EXPORT NativeCanvasRenderer : public OHOS::FFI::FFIData, public Referenced { 69 DECL_TYPE(NativeCanvasRenderer, OHOS::FFI::FFIData) 70 public: 71 explicit NativeCanvasRenderer(bool antialias); 72 NativeCanvasRenderer(); 73 ~NativeCanvasRenderer() override; 74 75 void SetCanvasPattern(const RefPtr<AceType>& canvas); 76 void SetAntiAlias(); 77 78 static RefPtr<CanvasPath2D> MakePath2D(const std::string& capStr); 79 80 void SetFillStyle(const Color& color); 81 void SetFillStyle(const sptr<NativeCanvasGradient>& nativeCanvasGradient); 82 void SetFillStyle(const sptr<NativeCanvasPattern>& nativeCanvasPattern); 83 void SetLineWidth(const double lineWidth); 84 void SetStrokeStyle(const Color& color); 85 void SetStrokeStyle(const sptr<NativeCanvasGradient>& nativeCanvasGradient); 86 void SetStrokeStyle(const sptr<NativeCanvasPattern>& nativeCanvasPattern); 87 void SetLineCap(const LineCapStyle lineCap); 88 void SetLineJoin(const LineJoinStyle lineJoin); 89 void SetMiterLimit(double limit); 90 void SetFont(const FontStyle style, const std::string& weight, const Dimension& size, const std::string& family); 91 void SetTextAlign(const TextAlign align); 92 void SetTextBaseline(const TextBaseline baseline); 93 void SetGlobalAlpha(double alpha); 94 void SetLineDash(std::vector<double>& lineDash); 95 void SetLineDashOffset(double lineDashOffset); 96 void SetGlobalCompositeOperation(const CompositeOperation type); 97 void SetShadowBlur(double blur); 98 void SetShadowColor(const Color& color); 99 void SetShadowOffsetX(double offsetX); 100 void SetShadowOffsetY(double offsetY); 101 void SetImageSmoothingEnabled(bool enabled); 102 void SetImageSmoothingQuality(const std::string& quality); 103 void RestoreLayer(); 104 void SaveLayer(); 105 void SetFilter(const std::string& value); 106 double GetHeight(); 107 void SetDirection(const std::string& directionStr); 108 void SetDensity(); 109 int64_t GetTransform(); 110 int64_t CreateImageData(const double height, const double width); 111 int64_t CreateImageData(const sptr<NativeImageData> imageData); 112 void PutImageData(const sptr<NativeImageData> imageData, const double dx, const double dy, const double dirtyX, 113 const double dirtyY, const double dirtyWidth, const double dirtyHeight); 114 void PutImageData(const sptr<NativeImageData> imageData, const double dx, const double dy); 115 116 void FillRect(const Rect& rect); 117 void StrokeRect(const Rect& rect); 118 void ClearRect(const Rect& rect); 119 void FillText(double x, double y, const std::string& text); 120 void FillText(double x, double y, const std::string& text, double maxWidth); 121 void StrokeText(double x, double y, const std::string& text); 122 void StrokeText(double x, double y, const std::string& text, double maxWidth); 123 Metrics MeasureText(const std::string& text); 124 void Stroke(); 125 void Stroke(const sptr<NativeCanvasPath>& canvasPath); 126 void BeginPath(); 127 void MoveTo(double x, double y); 128 void LineTo(double x, double y); 129 void ClosePath(); 130 void BezierCurveTo(const BezierCurveParam& param); 131 void QuadraticCurveTo(const QuadraticCurveParam& param); 132 void Arc(const ArcParam& param); 133 void ArcTo(const ArcToParam& param); 134 void Ellipse(const EllipseParam& param); 135 void NormalRect(const Rect& rect); 136 void Fill(); 137 void Fill(const std::string& ruleStr); 138 void Fill(const sptr<NativeCanvasPath>& canvasPath, const std::string& ruleStr); 139 void Clip(); 140 void Clip(const std::string& ruleStr); 141 void Clip(const sptr<NativeCanvasPath>& canvasPath, const std::string& ruleStr); 142 void Rotate(double angle); 143 void Scale(double x, double y); 144 void Transform(const TransformParam& param); 145 void ResetTransform(); 146 void SetTransform(TransformParam param); 147 void SetTransform(unsigned int id, const TransformParam& transform); 148 void SetTransformByMatrix(const sptr<NativeMatrix2d>& matrix2d); 149 void Translate(double x, double y); 150 void Restore(); 151 void Save(); 152 void Reset(); 153 int64_t CreateLinearGradient(double x0, double y0, double x1, double y1); 154 int64_t CreateRadialGradient(double x0, double y0, double r0, double x1, double y1, double r1); 155 int64_t CreateConicGradient(const double startAngle, const double x, const double y); 156 void DrawImage(const CanvasImage& image); 157 void DrawImage(const RefPtr<OHOS::Ace::PixelMap>& pixelMap, const CanvasImage& image); 158 std::unique_ptr<ImageData> GetImageData(double fLeft, double fTop, double fWidth, double fHeight); 159 int64_t GetNativeImageData(const double left, const double top, const double width, const double height); 160 int64_t GetPixelMap(double left, double top, double width, double height); 161 std::string ToDataURL(const std::string& dataUrl, double quality); 162 std::string GetJsonData(const std::string& path); 163 std::vector<double> GetLineDash(); 164 int64_t CreatePattern(int64_t bitMapId, const std::string& repeat); 165 void SetPixelMap(const RefPtr<OHOS::Ace::PixelMap>& pixelMap); 166 std::string ToDataUrl(const std::string type, const double quality); 167 double GetWidth(); 168 void TransferFromImageBitmap(const sptr<CJRenderImage> cjImage); 169 SetUnit(CanvasUnit unit)170 void SetUnit(CanvasUnit unit) 171 { 172 unit_ = unit; 173 } 174 GetUnit()175 CanvasUnit GetUnit() 176 { 177 return unit_; 178 } 179 SetAntiAlias(bool antialias)180 void SetAntiAlias(bool antialias) 181 { 182 antialias_ = antialias; 183 } 184 185 inline double GetDensity(bool useSystemDensity = false) 186 { 187 if (useSystemDensity) { 188 return !NearZero(density_) ? density_ : 1.0; 189 } else { 190 return ((GetUnit() == CanvasUnit::DEFAULT) && !NearZero(density_)) ? density_ : 1.0; 191 } 192 } 193 SetInstanceId(int32_t id)194 void SetInstanceId(int32_t id) 195 { 196 instanceId_ = id; 197 } 198 SetOffscreenPattern(const RefPtr<AceType> & offscreenCanvas)199 void SetOffscreenPattern(const RefPtr<AceType>& offscreenCanvas) 200 { 201 canvasPattern_ = offscreenCanvas; 202 renderingContext2DModel_->SetPattern(offscreenCanvas); 203 if (isOffscreenInitializeShadow_) { 204 return; 205 } 206 isOffscreenInitializeShadow_ = true; 207 renderingContext2DModel_->SetShadowColor(Color::TRANSPARENT); 208 } 209 210 protected: 211 bool antialias_ = false; 212 213 RefPtr<AceType> canvasPattern_; 214 RefPtr<RenderingContext2DModel> renderingContext2DModel_; 215 216 int32_t instanceId_ = INSTANCE_ID_UNDEFINED; 217 218 private: 219 double density_ = 1.0; 220 std::vector<PaintState> savePaintState_; 221 CanvasUnit unit_ = CanvasUnit::DEFAULT; 222 PaintState paintState_; 223 TextStyle style_; 224 ImageData imageData_; 225 bool isOffscreenInitializeShadow_ = false; 226 bool isInitializeShadow_ = false; 227 static std::unordered_map<int32_t, std::shared_ptr<Pattern>> pattern_; 228 std::shared_ptr<Pattern> GetPatternPtr(int32_t id); 229 static unsigned int patternCount_; 230 }; 231 232 } // namespace OHOS::Ace::Framework 233 #endif // FRAMEWORKS_BRIDGE_CJ_FRONTEND_CPP_VIEW_CAVANS_RENDERER_H 234