1 /*
2  * Copyright (c) 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 RENDER_SERVICE_CLIENT_CORE_RENDER_RS_BORDER_H
17 #define RENDER_SERVICE_CLIENT_CORE_RENDER_RS_BORDER_H
18 
19 #include <iostream>
20 #include <sstream>
21 #include <vector>
22 #include <cinttypes>
23 
24 #include "common/rs_color_palette.h"
25 #include "common/rs_vector4.h"
26 #include "property/rs_properties_def.h"
27 
28 #include "draw/canvas.h"
29 #include "draw/brush.h"
30 #include "draw/pen.h"
31 #include "utils/rect.h"
32 #include "utils/round_rect.h"
33 
34 namespace OHOS {
35 namespace Rosen {
36 enum class BorderStyle : uint32_t {
37     SOLID = 0,
38     DASHED,
39     DOTTED,
40     NONE
41 };
42 
43 #define MAX_BORDER_NUM 4
44 
45 class RSBorderGeo {
46 public:
RSBorderGeo()47     RSBorderGeo() {};
~RSBorderGeo()48     ~RSBorderGeo() {};
49 
50     Drawing::RoundRect rrect;
51     Drawing::RoundRect innerRRect;
52     Drawing::Point center;
53     std::vector<Drawing::Path> pathVec { MAX_BORDER_NUM };
54 };
55 
56 // also used for Outline
57 class RSBorder final {
58 public:
59     RSBorder() = default;
60     RSBorder(const bool& isOutline);
~RSBorder()61     ~RSBorder() {}
62 
63     enum BorderType : int {
64         LEFT = 0,
65         TOP,
66         RIGHT,
67         BOTTOM,
68     };
69 
70     void SetColor(Color color);
71     void SetWidth(float width);
72     void SetStyle(BorderStyle style);
73     void SetDashWidth(float dashWidth);
74     void SetDashGap(float dashGap);
75     Color GetColor(int idx = RSBorder::LEFT) const;
76     float GetWidth(int idx = RSBorder::LEFT) const;
77     BorderStyle GetStyle(int idx = RSBorder::LEFT) const;
78     float GetDashWidth(int idx = RSBorder::LEFT) const;
79     float GetDashGap(int idx = RSBorder::LEFT) const;
80 
81     void SetColorFour(const Vector4<Color>& color);
82     void SetWidthFour(const Vector4f& width);
83     void SetStyleFour(const Vector4<uint32_t>& style);
84     void SetRadiusFour(const Vector4f& radius);
85     void SetDashWidthFour(const Vector4f& dashWidth);
86     void SetDashGapFour(const Vector4f& dashGap);
87     Vector4<Color> GetColorFour() const;
88     Vector4f GetWidthFour() const;
89     Vector4<uint32_t> GetStyleFour() const;
90     Vector4f GetDashWidthFour() const;
91     Vector4f GetDashGapFour() const;
92     Vector4f GetRadiusFour() const;
93 
94     bool HasBorder() const;
95 
96     std::string ToString() const;
97 
98     void SetBorderEffect(Drawing::Pen& pen, int idx, float spaceBetweenDot, float borderLength) const;
99     bool ApplyFillStyle(Drawing::Brush& brush) const;
100     bool ApplyPathStyle(Drawing::Pen& pen) const;
101     bool ApplyFourLine(Drawing::Pen& pen) const;
102     bool ApplyLineStyle(Drawing::Pen& pen, int borderIdx, float length) const;
103     bool ApplySimpleBorder(const RRect& rrect) const;
104     void PaintFourLine(Drawing::Canvas& canvas, Drawing::Pen& pen, RectF rect) const;
105     void DrawBorders(Drawing::Canvas& canvas, Drawing::Pen& pen, RSBorderGeo& borderGeo) const;
106 
107 private:
108     void DrawBorderImpl(Drawing::Canvas& canvas, Drawing::Pen& pen, RSBorderGeo& borderGeo, int idx) const;
109     void DrawBorderImpl(Drawing::Canvas& canvas, Drawing::Pen& pen, RSBorderGeo& borderGeo, int idx1, int idx2) const;
110     void DrawBorderImpl(
111         Drawing::Canvas& canvas, Drawing::Pen& pen, RSBorderGeo& borderGeo, int idx1, int idx2, int idx3) const;
112     void DrawNestedRoundRect(Drawing::Canvas& canvas, const RSBorderGeo& borderGeo, uint32_t color) const;
113     void CalcBorderPath(RSBorderGeo& borderGeo) const;
114     bool CanBeCombined(int border1, int border2) const;
115 
116     Drawing::Point GetTLIP(const Drawing::RoundRect& rrect, const Drawing::Point& innerRectCenter) const;
117     Drawing::Point GetTRIP(const Drawing::RoundRect& rrect, const Drawing::Point& innerRectCenter) const;
118     Drawing::Point GetBLIP(const Drawing::RoundRect& rrect, const Drawing::Point& innerRectCenter) const;
119     Drawing::Point GetBRIP(const Drawing::RoundRect& rrect, const Drawing::Point& innerRectCenter) const;
120 
121     void DrawLeftBorder(Drawing::Canvas& canvas, Drawing::Pen& pen, const RSBorderGeo& borderGeo) const;
122     void DrawTopBorder(Drawing::Canvas& canvas, Drawing::Pen& pen, const RSBorderGeo& borderGeo) const;
123     void DrawRightBorder(Drawing::Canvas& canvas, Drawing::Pen& pen, const RSBorderGeo& borderGeo) const;
124     void DrawBottomBorder(Drawing::Canvas& canvas, Drawing::Pen& pen, const RSBorderGeo& borderGeo) const;
125 
126     // Vectors containing uniform or four-sided border attributes.
127     // If four-sided, the order of contents is left, top, right, bottom.
128     std::vector<Color> colors_;
129     std::vector<float> widths_;
130     std::vector<BorderStyle> styles_;
131     // Dash params dashWidth and dashGap
132     std::vector<float> dashWidth_;
133     std::vector<float> dashGap_;
134 
135     // only be used by outline, innerBorder(border_) uses corner radius.
136     Vector4f radius_;
137 };
138 } // namespace Rosen
139 } // namespace OHOS
140 
141 #endif // RENDER_SERVICE_CLIENT_CORE_RENDER_RS_BORDER_H
142