• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021 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 
SetTextAlign(TextAlign align)125     void SetTextAlign(TextAlign align)
126     {
127         fillState_.SetTextAlign(align);
128         strokeState_.SetTextAlign(align);
129     }
130 
SetTextBaseline(TextBaseline baseline)131     void SetTextBaseline(TextBaseline baseline)
132     {
133         fillState_.SetTextBaseline(baseline);
134         strokeState_.SetTextBaseline(baseline);
135     }
136 
SetFontWeight(FontWeight weight)137     void SetFontWeight(FontWeight weight)
138     {
139         fillState_.SetFontWeight(weight);
140         strokeState_.SetFontWeight(weight);
141     }
142 
SetFontFamilies(const std::vector<std::string> & fontFamilies)143     void SetFontFamilies(const std::vector<std::string>& fontFamilies)
144     {
145         fillState_.SetFontFamilies(fontFamilies);
146         strokeState_.SetFontFamilies(fontFamilies);
147     }
148 
SetFontStyle(FontStyle style)149     void SetFontStyle(FontStyle style)
150     {
151         fillState_.SetFontStyle(style);
152         strokeState_.SetFontStyle(style);
153     }
154 
SetFontSize(const Dimension & size)155     void SetFontSize(const Dimension& size)
156     {
157         fillState_.SetFontSize(size);
158         strokeState_.SetFontSize(size);
159     }
160 
SetTextStyle(const TextStyle & style)161     void SetTextStyle(const TextStyle& style)
162     {
163         fillState_.SetTextStyle(style);
164         strokeState_.SetTextStyle(style);
165     }
166 
SetLineWidth(double width)167     void SetLineWidth(double width)
168     {
169         strokeState_.SetLineWidth(width);
170     }
171 
SetLineCap(LineCapStyle style)172     void SetLineCap(LineCapStyle style)
173     {
174         strokeState_.SetLineCap(style);
175     }
176 
SetLineJoin(LineJoinStyle style)177     void SetLineJoin(LineJoinStyle style)
178     {
179         strokeState_.SetLineJoin(style);
180     }
181 
SetMiterLimit(double limit)182     void SetMiterLimit(double limit)
183     {
184         strokeState_.SetMiterLimit(limit);
185     }
186 
SaveStates()187     void SaveStates()
188     {
189         PaintHolder holder;
190         holder.shadow = shadow_;
191         holder.fillState = fillState_;
192         holder.globalState = globalState_;
193         holder.strokeState = strokeState_;
194         saveStates_.push(holder);
195     }
196 
RestoreStates()197     void RestoreStates()
198     {
199         if (saveStates_.empty()) {
200             return;
201         }
202         auto saveState = saveStates_.top();
203         shadow_ = saveState.shadow;
204         fillState_ = saveState.fillState;
205         strokeState_ = saveState.strokeState;
206         globalState_ = saveState.globalState;
207         saveStates_.pop();
208     }
209 
210     // method
211     virtual std::unique_ptr<ImageData> GetImageData(double left, double top, double width, double height) = 0;
212     virtual std::string ToDataURL(const std::string& type, const double quality) = 0;
213     virtual void SetAntiAlias(bool isEnabled) = 0;
214     virtual void FillRect(Rect rect) = 0;
215     virtual void ClearRect(Rect rect) = 0;
216     virtual void StrokeRect(Rect rect) = 0;
217     virtual void BeginPath() = 0;
218     virtual void Arc(const ArcParam& param) = 0;
219     virtual void Stroke() = 0;
220     virtual void Stroke(const RefPtr<CanvasPath2D>& path) = 0;
221     virtual void ArcTo(const ArcToParam& param) = 0;
222     virtual void MoveTo(double x, double y) = 0;
223     virtual void ClosePath() = 0;
224     virtual void Rotate(double angle) = 0;
225     virtual void Scale(double x, double y) = 0;
226     virtual void FillText(const std::string& text, double x, double y, const PaintState& state) = 0;
227     virtual void StrokeText(const std::string& text, double x, double y, const PaintState& state) = 0;
228     virtual double MeasureText(const std::string& text, const PaintState& state) = 0;
229     virtual double MeasureTextHeight(const std::string& text, const PaintState& state) = 0;
230     virtual TextMetrics MeasureTextMetrics(const std::string& text, const PaintState& state) = 0;
231     virtual void AddRect(const Rect& rect) = 0;
232     virtual void Fill() = 0;
233     virtual void Clip() = 0;
234     virtual void PutImageData(const ImageData& imageData) = 0;
235     virtual void DrawImage(const CanvasImage& image, double width, double height) = 0;
236     virtual void DrawPixelMap(RefPtr<PixelMap> pixelMap, const CanvasImage& image) = 0;
237     virtual void LineTo(double x, double y) = 0;
238     virtual void BezierCurveTo(const BezierCurveParam& param) = 0;
239     virtual void QuadraticCurveTo(const QuadraticCurveParam& param) = 0;
240     virtual void Ellipse(const EllipseParam& param) = 0;
241     virtual void SetTransform(const TransformParam& param) = 0;
242     virtual void Transform(const TransformParam& param) = 0;
243     virtual void Translate(double x, double y) = 0;
244     virtual void Restore() = 0;
245     virtual void Save() = 0;
246     virtual bool IsPointInStroke(double x, double y) = 0;
247     virtual bool IsPointInStroke(const RefPtr<CanvasPath2D>& path, double x, double y) = 0;
248     virtual bool IsPointInPath(double x, double y) = 0;
249     virtual bool IsPointInPath(const RefPtr<CanvasPath2D>& path, double x, double y) = 0;
250     virtual void ResetTransform() = 0;
251 protected:
252     int32_t width_;
253     int32_t height_;
254     PaintState fillState_;
255     StrokePaintState strokeState_;
256     GlobalPaintState globalState_;
257     Shadow shadow_;
258     Shadow imageShadow_;
259     bool smoothingEnabled_ = true;
260     std::string smoothingQuality_ = "low";
261     std::string filterParam_ = "";
262 
263     // PaintHolder includes fillState, strokeState, globalState and shadow for save
264     std::stack<PaintHolder> saveStates_;
265 };
266 }
267 
268 #endif
269 
270