• 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_BASE_PROPERTIES_PAINT_STATE_H
17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_BASE_PROPERTIES_PAINT_STATE_H
18 
19 #include "base/memory/ace_type.h"
20 #include "core/components/common/layout/constants.h"
21 #include "core/components/common/properties/color.h"
22 #include "core/components/common/properties/decoration.h"
23 #include "core/components/common/properties/text_style.h"
24 
25 namespace OHOS::Ace {
26 
27 struct BezierCurveParam {
28     double cp1x = 0.0; // first bezier point x
29     double cp1y = 0.0; // first bezier point y
30     double cp2x = 0.0; // second bezier point x
31     double cp2y = 0.0; // second bezier point y
32     double x = 0.0;    // end point x
33     double y = 0.0;    // end point y
34 };
35 
36 struct QuadraticCurveParam {
37     double cpx = 0.0; // bezier point x
38     double cpy = 0.0; // bezier point y
39     double x = 0.0;   // end point x
40     double y = 0.0;   // end point y
41 };
42 
43 struct ArcParam {
44     double x = 0.0;             // point x of the circle
45     double y = 0.0;             // point y of the circle
46     double radius = 0.0;        // radius of the circle
47     double startAngle = 0.0;    // start angle of the circle
48     double endAngle = 0.0;      // end angle of the circle
49     bool anticlockwise = false; // is draw clock wise or not
50 };
51 
52 struct ArcToParam {
53     double x1 = 0.0;     // start point x
54     double y1 = 0.0;     // start point y
55     double x2 = 0.0;     // end point x
56     double y2 = 0.0;     // end point y
57     double radius = 0.0; // radius of the circle
58 };
59 
60 struct EllipseParam {
61     double x = 0.0;             // point x of the ellipse
62     double y = 0.0;             // point y of the ellipse
63     double radiusX = 0.0;       // x axis radius of the ellipse
64     double radiusY = 0.0;       // y axis radius of the ellipse
65     double rotation = 0.0;      // rotation angle of the ellipse
66     double startAngle = 0.0;    // start angle of the ellipse
67     double endAngle = 0.0;      // end angle of the ellipse
68     bool anticlockwise = false; // is draw clock wise or not
69 };
70 
71 struct TransformParam {
72     double scaleX = 0.0;
73     double skewX = 0.0;
74     double skewY = 0.0;
75     double scaleY = 0.0;
76     double translateX = 0.0;
77     double translateY = 0.0;
78 };
79 
80 struct LineDashParam {
81     std::vector<double> lineDash;
82     double dashOffset = 0.0;
83 };
84 
85 struct CanvasImage {
86     int32_t flag = 0;
87     double sx = 0.0;
88     double sy = 0.0;
89     double sWidth = 0.0;
90     double sHeight = 0.0;
91     double dx = 0.0;
92     double dy = 0.0;
93     double dWidth = 0.0;
94     double dHeight = 0.0;
95     std::string src;
96 };
97 
98 struct ImageData {
99     RefPtr<Ace::PixelMap> pixelMap;
100     int32_t x = 0;
101     int32_t y = 0;
102     int32_t dirtyX = 0;
103     int32_t dirtyY = 0;
104     int32_t dirtyWidth = 0;
105     int32_t dirtyHeight = 0;
106     std::vector<Color> data;
107 };
108 
109 struct TextMetrics {
110     double width;
111     double height;
112     double actualBoundingBoxLeft;
113     double actualBoundingBoxRight;
114     double actualBoundingBoxAscent;
115     double actualBoundingBoxDescent;
116 };
117 
118 enum class ContextType {
119     RENDER_2D,
120     RENDER_3D,
121 };
122 
123 // following the definition of FillType in skPath
124 enum class CanvasFillRule {
125     NONZERO = 0,
126     EVENODD,
127 };
128 
129 // following the definition in skPaint
130 enum class LineCapStyle {
131     BUTT = 0,
132     ROUND,
133     SQUARE,
134 };
135 
136 enum class LineJoinStyle {
137     MITER = 0,
138     ROUND,
139     BEVEL,
140 };
141 
142 enum class CompositeOperation {
143     SOURCE_OVER = 0,
144     SOURCE_ATOP,
145     SOURCE_IN,
146     SOURCE_OUT,
147     DESTINATION_OVER,
148     DESTINATION_ATOP,
149     DESTINATION_IN,
150     DESTINATION_OUT,
151     LIGHTER,
152     COPY,
153     XOR,
154 };
155 
156 enum class PaintStyle {
157     NONE = 0,
158     Color,
159     Gradient,
160     ImagePattern
161 };
162 
163 class PaintState {
164 public:
GetColor()165     const Color& GetColor() const
166     {
167         return color_;
168     }
169 
SetColor(const Color & color)170     void SetColor(const Color& color)
171     {
172         paintStyle_ = PaintStyle::Color;
173         color_ = color;
174     }
175 
GetGradient()176     const Gradient& GetGradient() const
177     {
178         return gradient_;
179     }
180 
SetGradient(const Gradient & gradient)181     void SetGradient(const Gradient& gradient)
182     {
183         paintStyle_ = PaintStyle::Gradient;
184         gradient_ = gradient;
185     }
186 
GetTextStyle()187     const TextStyle& GetTextStyle() const
188     {
189         return textStyle_;
190     }
191 
SetTextStyle(const TextStyle & textStyle)192     void SetTextStyle(const TextStyle& textStyle)
193     {
194         textStyle_ = textStyle;
195     }
196 
GetTextAlign()197     TextAlign GetTextAlign() const
198     {
199         return textAlign_;
200     }
201 
SetTextAlign(TextAlign textAlign)202     void SetTextAlign(TextAlign textAlign)
203     {
204         textAlign_ = textAlign;
205     }
206 
GetOffTextDirection()207     TextDirection GetOffTextDirection() const
208     {
209         return textDirection_;
210     }
211 
SetOffTextDirection(TextDirection textDirection)212     void SetOffTextDirection(TextDirection textDirection)
213     {
214         textDirection_ = textDirection;
215     }
216 
SetTextColor(const Color & color)217     void SetTextColor(const Color& color)
218     {
219         textStyle_.SetTextColor(color);
220     }
221 
SetFontSize(const Dimension & size)222     void SetFontSize(const Dimension& size)
223     {
224         textStyle_.SetFontSize(size);
225     }
226 
SetFontStyle(FontStyle style)227     void SetFontStyle(FontStyle style)
228     {
229         textStyle_.SetFontStyle(style);
230     }
231 
SetFontWeight(FontWeight weight)232     void SetFontWeight(FontWeight weight)
233     {
234         textStyle_.SetFontWeight(weight);
235     }
236 
SetFontFamilies(const std::vector<std::string> & fontFamilies)237     void SetFontFamilies(const std::vector<std::string>& fontFamilies)
238     {
239         textStyle_.SetFontFamilies(fontFamilies);
240     }
241 
SetTextBaseline(TextBaseline baseline)242     void SetTextBaseline(TextBaseline baseline)
243     {
244         textStyle_.SetTextBaseline(baseline);
245     }
246 
GetPattern()247     const Pattern& GetPattern() const
248     {
249         return pattern_;
250     }
251 
SetPattern(const Pattern & pattern)252     void SetPattern(const Pattern& pattern)
253     {
254         paintStyle_ = PaintStyle::ImagePattern;
255         pattern_ = pattern;
256     }
257 
GetPatternNG()258     std::weak_ptr<Ace::Pattern> GetPatternNG() const
259     {
260         return patternNG_;
261     }
262 
GetPatternValue()263     Ace::Pattern GetPatternValue() const
264     {
265         Pattern pattern;
266         if (!patternNG_.expired()) {
267             auto value = patternNG_.lock();
268             if (value) {
269                 pattern = *value;
270             }
271         }
272         return pattern;
273     }
274 
SetPatternNG(const std::weak_ptr<Ace::Pattern> & pattern)275     void SetPatternNG(const std::weak_ptr<Ace::Pattern>& pattern)
276     {
277         paintStyle_ = PaintStyle::ImagePattern;
278         patternNG_ = pattern;
279     }
280 
GetId()281     int32_t GetId() const
282     {
283         return id_;
284     }
285 
SetId(int32_t id)286     void SetId(int32_t id)
287     {
288         id_ = id;
289     }
290 
GetPaintStyle()291     PaintStyle GetPaintStyle() const
292     {
293         return paintStyle_;
294     }
295 
296 protected:
297     Color color_ = Color::BLACK;
298     Gradient gradient_;
299     TextStyle textStyle_;
300     TextAlign textAlign_ = TextAlign::LEFT;
301     TextDirection textDirection_ = TextDirection::LTR;
302     int32_t id_ = 0;
303     PaintStyle paintStyle_ = PaintStyle::Color;
304     Pattern pattern_;
305     std::weak_ptr<Ace::Pattern> patternNG_;
306 };
307 
308 class StrokePaintState : public PaintState {
309 public:
GetLineCap()310     LineCapStyle GetLineCap() const
311     {
312         return lineCap_;
313     }
314 
SetLineCap(LineCapStyle lineCap)315     void SetLineCap(LineCapStyle lineCap)
316     {
317         lineCap_ = lineCap;
318     }
319 
GetLineJoin()320     LineJoinStyle GetLineJoin() const
321     {
322         return lineJoin_;
323     }
324 
SetLineJoin(LineJoinStyle lineJoin)325     void SetLineJoin(LineJoinStyle lineJoin)
326     {
327         lineJoin_ = lineJoin;
328     }
329 
GetLineWidth()330     double GetLineWidth() const
331     {
332         return lineWidth_;
333     }
334 
SetLineWidth(double lineWidth)335     void SetLineWidth(double lineWidth)
336     {
337         lineWidth_ = lineWidth;
338     }
339 
GetMiterLimit()340     double GetMiterLimit() const
341     {
342         return miterLimit_;
343     }
344 
SetMiterLimit(double miterLimit)345     void SetMiterLimit(double miterLimit)
346     {
347         miterLimit_ = miterLimit;
348     }
349 
GetLineDash()350     LineDashParam GetLineDash() const
351     {
352         return lineDash_;
353     }
354 
SetLineDash(const LineDashParam & lineDash)355     void SetLineDash(const LineDashParam& lineDash)
356     {
357         lineDash_ = lineDash;
358     }
359 
SetLineDashOffset(double offset)360     void SetLineDashOffset(double offset)
361     {
362         lineDash_.dashOffset = offset;
363     }
364 
SetLineDash(const std::vector<double> & segments)365     void SetLineDash(const std::vector<double>& segments)
366     {
367         lineDash_.lineDash = segments;
368     }
369 
370 private:
371     LineCapStyle lineCap_ = LineCapStyle::BUTT;
372     LineJoinStyle lineJoin_ = LineJoinStyle::MITER;
373 
374     double lineWidth_ = 1.0; // default lineWidth
375 
376     double miterLimit_ = 10.0; // default miterLimit
377     LineDashParam lineDash_;
378 };
379 
380 class GlobalPaintState {
381 public:
GetAlpha()382     double GetAlpha() const
383     {
384         return alpha_;
385     }
386 
SetAlpha(double alpha)387     void SetAlpha(double alpha)
388     {
389         alpha_ = alpha;
390     }
391 
GetType()392     CompositeOperation GetType() const
393     {
394         return type_;
395     }
396 
SetType(CompositeOperation type)397     void SetType(CompositeOperation type)
398     {
399         type_ = type;
400     }
401 
HasGlobalAlpha()402     bool HasGlobalAlpha() const
403     {
404         return !NearEqual(alpha_, -1.0);
405     }
406 
407 private:
408     double alpha_ = -1.0;
409     CompositeOperation type_ = CompositeOperation::SOURCE_OVER;
410 };
411 
412 struct PaintHolder {
413     PaintState fillState;
414     StrokePaintState strokeState;
415     GlobalPaintState globalState;
416     Shadow shadow;
417 };
418 
419 } // namespace OHOS::Ace
420 
421 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_BASE_PROPERTIES_PAINT_STATE_H
422