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 ImageData {
86     RefPtr<Ace::PixelMap> pixelMap;
87     int32_t x = 0;
88     int32_t y = 0;
89     int32_t dirtyX = 0;
90     int32_t dirtyY = 0;
91     int32_t dirtyWidth = 0;
92     int32_t dirtyHeight = 0;
93     std::vector<uint32_t> data;
94 };
95 
96 struct CanvasImage {
97     int32_t flag = 0;
98     double sx = 0.0;
99     double sy = 0.0;
100     double sWidth = 0.0;
101     double sHeight = 0.0;
102     double dx = 0.0;
103     double dy = 0.0;
104     double dWidth = 0.0;
105     double dHeight = 0.0;
106     std::string src;
107     int32_t instanceId = 0;
108     std::shared_ptr<ImageData> imageData;
109 };
110 
111 struct TextMetrics {
112     double width = 0;
113     double height = 0;
114     double actualBoundingBoxLeft = 0;
115     double actualBoundingBoxRight = 0;
116     double actualBoundingBoxAscent = 0;
117     double actualBoundingBoxDescent = 0;
118     double alphabeticBaseline = 0;
119     double emHeightAscent = 0;
120     double emHeightDescent = 0;
121     double fontBoundingBoxAscent = 0;
122     double fontBoundingBoxDescent = 0;
123     double hangingBaseline = 0;
124     double ideographicBaseline = 0;
125 };
126 
127 enum class ContextType {
128     RENDER_2D,
129     RENDER_3D,
130 };
131 
132 enum class CanvasUnit {
133     DEFAULT = 0,
134     PX,
135 };
136 
137 // following the definition of FillType in skPath
138 enum class CanvasFillRule {
139     NONZERO = 0,
140     EVENODD,
141 };
142 
143 // following the definition in skPaint
144 enum class LineCapStyle {
145     BUTT = 0,
146     ROUND,
147     SQUARE,
148 };
149 
150 enum class LineJoinStyle {
151     MITER = 0,
152     ROUND,
153     BEVEL,
154 };
155 
156 enum class CompositeOperation {
157     SOURCE_OVER = 0,
158     SOURCE_ATOP,
159     SOURCE_IN,
160     SOURCE_OUT,
161     DESTINATION_OVER,
162     DESTINATION_ATOP,
163     DESTINATION_IN,
164     DESTINATION_OUT,
165     LIGHTER,
166     COPY,
167     XOR,
168 };
169 
170 enum class PaintStyle {
171     NONE = 0,
172     Color,
173     Gradient,
174     ImagePattern
175 };
176 
177 class PaintState {
178 public:
GetColor()179     const Color& GetColor() const
180     {
181         return color_;
182     }
183 
SetColor(const Color & color)184     void SetColor(const Color& color)
185     {
186         paintStyle_ = PaintStyle::Color;
187         color_ = color;
188     }
189 
GetGradient()190     const Gradient& GetGradient() const
191     {
192         return gradient_;
193     }
194 
SetGradient(const Gradient & gradient)195     void SetGradient(const Gradient& gradient)
196     {
197         paintStyle_ = PaintStyle::Gradient;
198         gradient_ = gradient;
199     }
200 
GetTextStyle()201     const TextStyle& GetTextStyle() const
202     {
203         return textStyle_;
204     }
205 
SetTextStyle(const TextStyle & textStyle)206     void SetTextStyle(const TextStyle& textStyle)
207     {
208         textStyle_ = textStyle;
209     }
210 
GetTextAlign()211     TextAlign GetTextAlign() const
212     {
213         return textAlign_;
214     }
215 
SetTextAlign(TextAlign textAlign)216     void SetTextAlign(TextAlign textAlign)
217     {
218         textAlign_ = textAlign;
219     }
220 
GetOffTextDirection()221     TextDirection GetOffTextDirection() const
222     {
223         return textDirection_;
224     }
225 
SetOffTextDirection(TextDirection textDirection)226     void SetOffTextDirection(TextDirection textDirection)
227     {
228         textDirection_ = textDirection;
229     }
230 
SetTextColor(const Color & color)231     void SetTextColor(const Color& color)
232     {
233         textStyle_.SetTextColor(color);
234     }
235 
SetFontSize(const Dimension & size)236     void SetFontSize(const Dimension& size)
237     {
238         textStyle_.SetFontSize(size);
239     }
240 
SetFontStyle(FontStyle style)241     void SetFontStyle(FontStyle style)
242     {
243         textStyle_.SetFontStyle(style);
244     }
245 
SetFontWeight(FontWeight weight)246     void SetFontWeight(FontWeight weight)
247     {
248         textStyle_.SetFontWeight(weight);
249     }
250 
SetFontFamilies(const std::vector<std::string> & fontFamilies)251     void SetFontFamilies(const std::vector<std::string>& fontFamilies)
252     {
253         textStyle_.SetFontFamilies(fontFamilies);
254     }
255 
SetTextBaseline(TextBaseline baseline)256     void SetTextBaseline(TextBaseline baseline)
257     {
258         textStyle_.SetTextBaseline(baseline);
259     }
260 
GetPattern()261     const Pattern& GetPattern() const
262     {
263         return pattern_;
264     }
265 
SetPattern(const Pattern & pattern)266     void SetPattern(const Pattern& pattern)
267     {
268         paintStyle_ = PaintStyle::ImagePattern;
269         pattern_ = pattern;
270     }
271 
GetPatternNG()272     std::weak_ptr<Ace::Pattern> GetPatternNG() const
273     {
274         return patternNG_;
275     }
276 
GetPatternValue()277     Ace::Pattern GetPatternValue() const
278     {
279         Pattern pattern;
280         if (!patternNG_.expired()) {
281             auto value = patternNG_.lock();
282             if (value) {
283                 pattern = *value;
284             }
285         }
286         return pattern;
287     }
288 
SetPatternNG(const std::weak_ptr<Ace::Pattern> & pattern)289     void SetPatternNG(const std::weak_ptr<Ace::Pattern>& pattern)
290     {
291         paintStyle_ = PaintStyle::ImagePattern;
292         patternNG_ = pattern;
293     }
294 
GetId()295     int32_t GetId() const
296     {
297         return id_;
298     }
299 
SetId(int32_t id)300     void SetId(int32_t id)
301     {
302         id_ = id;
303     }
304 
GetPaintStyle()305     PaintStyle GetPaintStyle() const
306     {
307         return paintStyle_;
308     }
309 
310 protected:
311     Color color_ = Color::BLACK;
312     Gradient gradient_;
313     TextStyle textStyle_;
314     TextAlign textAlign_ = TextAlign::LEFT;
315     TextDirection textDirection_ = TextDirection::LTR;
316     int32_t id_ = 0;
317     PaintStyle paintStyle_ = PaintStyle::Color;
318     Pattern pattern_;
319     std::weak_ptr<Ace::Pattern> patternNG_;
320 };
321 
322 class StrokePaintState : public PaintState {
323 public:
GetLineCap()324     LineCapStyle GetLineCap() const
325     {
326         return lineCap_;
327     }
328 
SetLineCap(LineCapStyle lineCap)329     void SetLineCap(LineCapStyle lineCap)
330     {
331         lineCap_ = lineCap;
332     }
333 
GetLineJoin()334     LineJoinStyle GetLineJoin() const
335     {
336         return lineJoin_;
337     }
338 
SetLineJoin(LineJoinStyle lineJoin)339     void SetLineJoin(LineJoinStyle lineJoin)
340     {
341         lineJoin_ = lineJoin;
342     }
343 
GetLineWidth()344     double GetLineWidth() const
345     {
346         return lineWidth_;
347     }
348 
SetLineWidth(double lineWidth)349     void SetLineWidth(double lineWidth)
350     {
351         lineWidth_ = lineWidth;
352     }
353 
GetMiterLimit()354     double GetMiterLimit() const
355     {
356         return miterLimit_;
357     }
358 
SetMiterLimit(double miterLimit)359     void SetMiterLimit(double miterLimit)
360     {
361         miterLimit_ = miterLimit;
362     }
363 
GetLineDash()364     LineDashParam GetLineDash() const
365     {
366         return lineDash_;
367     }
368 
SetLineDash(const LineDashParam & lineDash)369     void SetLineDash(const LineDashParam& lineDash)
370     {
371         lineDash_ = lineDash;
372     }
373 
SetLineDashOffset(double offset)374     void SetLineDashOffset(double offset)
375     {
376         lineDash_.dashOffset = offset;
377     }
378 
SetLineDash(const std::vector<double> & segments)379     void SetLineDash(const std::vector<double>& segments)
380     {
381         lineDash_.lineDash = segments;
382     }
383 
384 private:
385     LineCapStyle lineCap_ = LineCapStyle::BUTT;
386     LineJoinStyle lineJoin_ = LineJoinStyle::MITER;
387 
388     double lineWidth_ = 1.0; // default lineWidth
389 
390     double miterLimit_ = 10.0; // default miterLimit
391     LineDashParam lineDash_;
392 };
393 
394 class GlobalPaintState {
395 public:
GetAlpha()396     double GetAlpha() const
397     {
398         return alpha_;
399     }
400 
SetAlpha(double alpha)401     void SetAlpha(double alpha)
402     {
403         alpha_ = alpha;
404     }
405 
GetType()406     CompositeOperation GetType() const
407     {
408         return type_;
409     }
410 
SetType(CompositeOperation type)411     void SetType(CompositeOperation type)
412     {
413         type_ = type;
414     }
415 
HasGlobalAlpha()416     bool HasGlobalAlpha() const
417     {
418         return !NearEqual(alpha_, -1.0);
419     }
420 
421 private:
422     double alpha_ = -1.0;
423     CompositeOperation type_ = CompositeOperation::SOURCE_OVER;
424 };
425 
426 struct PaintHolder {
427     PaintState fillState;
428     StrokePaintState strokeState;
429     GlobalPaintState globalState;
430     Shadow shadow;
431 };
432 
433 } // namespace OHOS::Ace
434 
435 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_BASE_PROPERTIES_PAINT_STATE_H
436