1 /* 2 * Copyright (c) 2021-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_CUSTOM_PAINT_OFFSCREEN_CANVAS_H 17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_CUSTOM_PAINT_OFFSCREEN_CANVAS_H 18 19 #include <stack> 20 #include "base/log/log.h" 21 #include "base/memory/ace_type.h" 22 #include "base/geometry/rect.h" 23 #include "core/components/common/properties/paint_state.h" 24 25 namespace OHOS::Ace { 26 class OffscreenCanvas : public virtual AceType { 27 DECLARE_ACE_TYPE(OffscreenCanvas, AceType); 28 29 public: GetWidth()30 int32_t GetWidth() 31 { 32 return width_; 33 } GetHeight()34 int32_t GetHeight() 35 { 36 return height_; 37 } 38 // priority set SetFillColor(const Color & color)39 void SetFillColor(const Color& color) 40 { 41 fillState_.SetColor(color); 42 fillState_.SetTextColor(color); 43 } 44 SetStrokeColor(const Color & color)45 void SetStrokeColor(const Color& color) 46 { 47 strokeState_.SetColor(color); 48 } 49 SetFillGradient(const Gradient & gradient)50 void SetFillGradient(const Gradient& gradient) 51 { 52 fillState_.SetGradient(gradient); 53 } 54 SetStrokeGradient(const Gradient & gradient)55 void SetStrokeGradient(const Gradient& gradient) 56 { 57 strokeState_.SetGradient(gradient); 58 } 59 SetFillPattern(const Pattern & pattern)60 void SetFillPattern(const Pattern& pattern) 61 { 62 fillState_.SetPattern(pattern); 63 } 64 SetStrokePattern(const Pattern & pattern)65 void SetStrokePattern(const Pattern& pattern) 66 { 67 strokeState_.SetPattern(pattern); 68 } 69 SetShadowBlur(double blur)70 void SetShadowBlur(double blur) 71 { 72 shadow_.SetBlurRadius(blur); 73 } 74 SetShadowOffsetX(double x)75 void SetShadowOffsetX(double x) 76 { 77 shadow_.SetOffsetX(x); 78 } 79 SetShadowOffsetY(double y)80 void SetShadowOffsetY(double y) 81 { 82 shadow_.SetOffsetY(y); 83 } 84 SetSmoothingEnabled(bool enabled)85 void SetSmoothingEnabled(bool enabled) 86 { 87 smoothingEnabled_ = enabled; 88 } 89 SetSmoothingQuality(const std::string & quality)90 void SetSmoothingQuality(const std::string& quality) 91 { 92 smoothingQuality_ = quality; 93 } 94 SetFilterParam(const std::string & quality)95 void SetFilterParam(const std::string& quality) 96 { 97 filterParam_ = quality; 98 } 99 SetShadowColor(const Color & color)100 void SetShadowColor(const Color& color) 101 { 102 shadow_.SetColor(color); 103 } 104 SetAlpha(double alpha)105 void SetAlpha(double alpha) 106 { 107 globalState_.SetAlpha(alpha); 108 } 109 SetCompositeType(CompositeOperation operation)110 void SetCompositeType(CompositeOperation operation) 111 { 112 globalState_.SetType(operation); 113 } 114 SetLineDashOffset(double offset)115 void SetLineDashOffset(double offset) 116 { 117 strokeState_.SetLineDashOffset(offset); 118 } 119 SetLineDash(const std::vector<double> & segments)120 void SetLineDash(const std::vector<double>& segments) 121 { 122 strokeState_.SetLineDash(segments); 123 } 124 GetLineDash()125 LineDashParam GetLineDash() const 126 { 127 return strokeState_.GetLineDash(); 128 } 129 SetTextAlign(TextAlign align)130 void SetTextAlign(TextAlign align) 131 { 132 fillState_.SetTextAlign(align); 133 strokeState_.SetTextAlign(align); 134 } 135 SetTextBaseline(TextBaseline baseline)136 void SetTextBaseline(TextBaseline baseline) 137 { 138 fillState_.SetTextBaseline(baseline); 139 strokeState_.SetTextBaseline(baseline); 140 } 141 SetFontWeight(FontWeight weight)142 void SetFontWeight(FontWeight weight) 143 { 144 fillState_.SetFontWeight(weight); 145 strokeState_.SetFontWeight(weight); 146 } 147 SetFontFamilies(const std::vector<std::string> & fontFamilies)148 void SetFontFamilies(const std::vector<std::string>& fontFamilies) 149 { 150 fillState_.SetFontFamilies(fontFamilies); 151 strokeState_.SetFontFamilies(fontFamilies); 152 } 153 SetFontStyle(FontStyle style)154 void SetFontStyle(FontStyle style) 155 { 156 fillState_.SetFontStyle(style); 157 strokeState_.SetFontStyle(style); 158 } 159 SetFontSize(const Dimension & size)160 void SetFontSize(const Dimension& size) 161 { 162 fillState_.SetFontSize(size); 163 strokeState_.SetFontSize(size); 164 } 165 SetTextStyle(const TextStyle & style)166 void SetTextStyle(const TextStyle& style) 167 { 168 fillState_.SetTextStyle(style); 169 strokeState_.SetTextStyle(style); 170 } 171 SetLineWidth(double width)172 void SetLineWidth(double width) 173 { 174 strokeState_.SetLineWidth(width); 175 } 176 SetLineCap(LineCapStyle style)177 void SetLineCap(LineCapStyle style) 178 { 179 strokeState_.SetLineCap(style); 180 } 181 SetLineJoin(LineJoinStyle style)182 void SetLineJoin(LineJoinStyle style) 183 { 184 strokeState_.SetLineJoin(style); 185 } 186 SetMiterLimit(double limit)187 void SetMiterLimit(double limit) 188 { 189 strokeState_.SetMiterLimit(limit); 190 } 191 SaveStates()192 void SaveStates() 193 { 194 PaintHolder holder; 195 holder.shadow = shadow_; 196 holder.fillState = fillState_; 197 holder.globalState = globalState_; 198 holder.strokeState = strokeState_; 199 saveStates_.push(holder); 200 } 201 RestoreStates()202 void RestoreStates() 203 { 204 if (saveStates_.empty()) { 205 return; 206 } 207 auto saveState = saveStates_.top(); 208 shadow_ = saveState.shadow; 209 fillState_ = saveState.fillState; 210 strokeState_ = saveState.strokeState; 211 globalState_ = saveState.globalState; 212 saveStates_.pop(); 213 } 214 215 // method 216 virtual std::unique_ptr<ImageData> GetImageData(double left, double top, double width, double height) = 0; 217 virtual std::string ToDataURL(const std::string& type, const double quality) = 0; 218 virtual void SetAntiAlias(bool isEnabled) = 0; 219 virtual void FillRect(Rect rect) = 0; 220 virtual void ClearRect(Rect rect) = 0; 221 virtual void StrokeRect(Rect rect) = 0; 222 virtual void BeginPath() = 0; 223 virtual void Arc(const ArcParam& param) = 0; 224 virtual void Stroke() = 0; 225 virtual void Stroke(const RefPtr<CanvasPath2D>& path) = 0; 226 virtual void ArcTo(const ArcToParam& param) = 0; 227 virtual void MoveTo(double x, double y) = 0; 228 virtual void ClosePath() = 0; 229 virtual void Rotate(double angle) = 0; 230 virtual void Scale(double x, double y) = 0; 231 virtual void FillText(const std::string& text, double x, double y, const PaintState& state) = 0; 232 virtual void StrokeText(const std::string& text, double x, double y, const PaintState& state) = 0; 233 virtual double MeasureText(const std::string& text, const PaintState& state) = 0; 234 virtual double MeasureTextHeight(const std::string& text, const PaintState& state) = 0; 235 virtual TextMetrics MeasureTextMetrics(const std::string& text, const PaintState& state) = 0; 236 virtual void AddRect(const Rect& rect) = 0; 237 virtual void Fill() = 0; 238 virtual void Fill(const RefPtr<CanvasPath2D>& path) = 0; 239 virtual void Clip() = 0; 240 virtual void Clip(const RefPtr<CanvasPath2D>& path) = 0; 241 virtual void PutImageData(const ImageData& imageData) = 0; 242 virtual void DrawImage(const CanvasImage& image, double width, double height) = 0; 243 virtual void DrawPixelMap(RefPtr<PixelMap> pixelMap, const CanvasImage& image) = 0; 244 virtual void LineTo(double x, double y) = 0; 245 virtual void BezierCurveTo(const BezierCurveParam& param) = 0; 246 virtual void QuadraticCurveTo(const QuadraticCurveParam& param) = 0; 247 virtual void Ellipse(const EllipseParam& param) = 0; 248 virtual void SetTransform(const TransformParam& param) = 0; 249 virtual void Transform(const TransformParam& param) = 0; 250 virtual void Translate(double x, double y) = 0; 251 virtual void Restore() = 0; 252 virtual void Save() = 0; 253 virtual bool IsPointInStroke(double x, double y) = 0; 254 virtual bool IsPointInStroke(const RefPtr<CanvasPath2D>& path, double x, double y) = 0; 255 virtual bool IsPointInPath(double x, double y) = 0; 256 virtual bool IsPointInPath(const RefPtr<CanvasPath2D>& path, double x, double y) = 0; 257 virtual void ResetTransform() = 0; 258 virtual void SetFillRuleForPath(const CanvasFillRule& rule) = 0; 259 virtual void SetFillRuleForPath2D(const CanvasFillRule& rule) = 0; 260 protected: 261 int32_t width_; 262 int32_t height_; 263 PaintState fillState_; 264 StrokePaintState strokeState_; 265 GlobalPaintState globalState_; 266 Shadow shadow_; 267 Shadow imageShadow_; 268 bool smoothingEnabled_ = true; 269 std::string smoothingQuality_ = "low"; 270 std::string filterParam_ = ""; 271 272 // PaintHolder includes fillState, strokeState, globalState and shadow for save 273 std::stack<PaintHolder> saveStates_; 274 }; 275 } // namespace OHOS::Ace 276 277 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_CUSTOM_PAINT_OFFSCREEN_CANVAS_H 278 279